From 1754acde8e79ec4e91cda5e57bd692a33ce9a770 Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Thu, 7 Sep 2023 14:35:14 +0800 Subject: [PATCH] fix(parser): more error reporting with locations (#12129) Signed-off-by: Bugen Zhao --- src/sqlparser/src/parser.rs | 10 +++++----- src/sqlparser/tests/sqlparser_common.rs | 3 ++- src/sqlparser/tests/sqlparser_postgres.rs | 8 ++++---- src/sqlparser/tests/testdata/create.yaml | 2 +- src/sqlparser/tests/testdata/select.yaml | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/sqlparser/src/parser.rs b/src/sqlparser/src/parser.rs index 7e8bf9c1042a6..84dc34ada3b64 100644 --- a/src/sqlparser/src/parser.rs +++ b/src/sqlparser/src/parser.rs @@ -549,7 +549,7 @@ impl Parser { })); let token = self.next_token(); - let expr = match token.token { + let expr = match token.token.clone() { Token::Word(w) => match w.keyword { Keyword::TRUE | Keyword::FALSE | Keyword::NULL => { self.prev_token(); @@ -593,7 +593,7 @@ impl Parser { }) } k if keywords::RESERVED_FOR_COLUMN_OR_TABLE_NAME.contains(&k) => { - parser_err!(format!("syntax error at or near \"{w}\"")) + parser_err!(format!("syntax error at or near {token}")) } // Here `w` is a word, check if it's a part of a multi-part // identifier, a function call, or a simple identifier: @@ -1232,7 +1232,7 @@ impl Parser { // for keyword 'array' self.prev_token(); } - parser_err!(format!("syntax error at or near '{}'", self.peek_token()))? + parser_err!(format!("syntax error at or near {}", self.peek_token()))? } else { Ok(()) } @@ -3435,10 +3435,10 @@ impl Parser { /// Parse a simple one-word identifier (possibly quoted, possibly a non-reserved keyword) pub fn parse_identifier_non_reserved(&mut self) -> Result { let token = self.next_token(); - match token.token { + match token.token.clone() { Token::Word(w) => { match keywords::RESERVED_FOR_COLUMN_OR_TABLE_NAME.contains(&w.keyword) { - true => parser_err!(format!("syntax error at or near \"{w}\"")), + true => parser_err!(format!("syntax error at or near {token}")), false => Ok(w.to_ident()?), } } diff --git a/src/sqlparser/tests/sqlparser_common.rs b/src/sqlparser/tests/sqlparser_common.rs index ce3ec095ce283..0fc2f3c2530f7 100644 --- a/src/sqlparser/tests/sqlparser_common.rs +++ b/src/sqlparser/tests/sqlparser_common.rs @@ -253,7 +253,8 @@ fn parse_select_all() { #[test] fn parse_select_all_distinct() { let result = parse_sql_statements("SELECT ALL DISTINCT name FROM customer"); - assert!(format!("{}", result.unwrap_err()).contains("syntax error at or near \"DISTINCT\"")); + assert!(format!("{}", result.unwrap_err()) + .contains("syntax error at or near DISTINCT at line:1, column:20")); } #[test] diff --git a/src/sqlparser/tests/sqlparser_postgres.rs b/src/sqlparser/tests/sqlparser_postgres.rs index 4e45f93a8c8ad..d94f1b06b166b 100644 --- a/src/sqlparser/tests/sqlparser_postgres.rs +++ b/src/sqlparser/tests/sqlparser_postgres.rs @@ -1047,7 +1047,7 @@ fn parse_array() { assert_eq!( parse_sql_statements(sql), Err(ParserError::ParserError( - "syntax error at or near '[ at line:1, column:28'".to_string() + "syntax error at or near [ at line:1, column:28".to_string() )) ); @@ -1055,7 +1055,7 @@ fn parse_array() { assert_eq!( parse_sql_statements(sql), Err(ParserError::ParserError( - "syntax error at or near '[ at line:1, column:24'".to_string() + "syntax error at or near [ at line:1, column:24".to_string() )) ); @@ -1063,7 +1063,7 @@ fn parse_array() { assert_eq!( parse_sql_statements(sql), Err(ParserError::ParserError( - "syntax error at or near 'ARRAY at line:1, column:27'".to_string() + "syntax error at or near ARRAY at line:1, column:27".to_string() )) ); @@ -1071,7 +1071,7 @@ fn parse_array() { assert_eq!( parse_sql_statements(sql), Err(ParserError::ParserError( - "syntax error at or near 'ARRAY at line:1, column:23'".to_string() + "syntax error at or near ARRAY at line:1, column:23".to_string() )) ); diff --git a/src/sqlparser/tests/testdata/create.yaml b/src/sqlparser/tests/testdata/create.yaml index ab5822fc2bc09..92bdabc83048c 100644 --- a/src/sqlparser/tests/testdata/create.yaml +++ b/src/sqlparser/tests/testdata/create.yaml @@ -35,7 +35,7 @@ - input: CREATE TABLE T (a STRUCT) formatted_sql: CREATE TABLE T (a STRUCT) - input: CREATE TABLE T (FULL INT) - error_msg: 'sql parser error: syntax error at or near "FULL"' + error_msg: 'sql parser error: syntax error at or near FULL at line:1, column:21' - input: CREATE TABLE T ("FULL" INT) formatted_sql: CREATE TABLE T ("FULL" INT) - input: CREATE USER user WITH SUPERUSER CREATEDB PASSWORD 'password' diff --git a/src/sqlparser/tests/testdata/select.yaml b/src/sqlparser/tests/testdata/select.yaml index b98b9b6ff4fb2..bbbb5a72bbdab 100644 --- a/src/sqlparser/tests/testdata/select.yaml +++ b/src/sqlparser/tests/testdata/select.yaml @@ -71,9 +71,9 @@ sql parser error: Expected ), found: minutes at line:1, column:62 Near "(t, x, interval '10'" - input: SELECT 1, FROM t - error_msg: 'sql parser error: syntax error at or near "FROM"' + error_msg: 'sql parser error: syntax error at or near FROM at line:1, column:15' - input: SELECT 1, WHERE true - error_msg: 'sql parser error: syntax error at or near "WHERE"' + error_msg: 'sql parser error: syntax error at or near WHERE at line:1, column:16' - input: SELECT timestamp with time zone '2022-10-01 12:00:00Z' AT TIME ZONE 'US/Pacific' formatted_sql: SELECT TIMESTAMP WITH TIME ZONE '2022-10-01 12:00:00Z' AT TIME ZONE 'US/Pacific' formatted_ast: 'Query(Query { with: None, body: Select(Select { distinct: All, projection: [UnnamedExpr(AtTimeZone { timestamp: TypedString { data_type: Timestamp(true), value: "2022-10-01 12:00:00Z" }, time_zone: "US/Pacific" })], from: [], lateral_views: [], selection: None, group_by: [], having: None }), order_by: [], limit: None, offset: None, fetch: None })'