Skip to content

Commit b578db6

Browse files
authored
fix(parser): more error reporting with locations (#12129)
Signed-off-by: Bugen Zhao <[email protected]>
1 parent 3ee8a87 commit b578db6

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

src/sqlparser/src/parser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ impl Parser {
549549
}));
550550

551551
let token = self.next_token();
552-
let expr = match token.token {
552+
let expr = match token.token.clone() {
553553
Token::Word(w) => match w.keyword {
554554
Keyword::TRUE | Keyword::FALSE | Keyword::NULL => {
555555
self.prev_token();
@@ -593,7 +593,7 @@ impl Parser {
593593
})
594594
}
595595
k if keywords::RESERVED_FOR_COLUMN_OR_TABLE_NAME.contains(&k) => {
596-
parser_err!(format!("syntax error at or near \"{w}\""))
596+
parser_err!(format!("syntax error at or near {token}"))
597597
}
598598
// Here `w` is a word, check if it's a part of a multi-part
599599
// identifier, a function call, or a simple identifier:
@@ -1232,7 +1232,7 @@ impl Parser {
12321232
// for keyword 'array'
12331233
self.prev_token();
12341234
}
1235-
parser_err!(format!("syntax error at or near '{}'", self.peek_token()))?
1235+
parser_err!(format!("syntax error at or near {}", self.peek_token()))?
12361236
} else {
12371237
Ok(())
12381238
}
@@ -3435,10 +3435,10 @@ impl Parser {
34353435
/// Parse a simple one-word identifier (possibly quoted, possibly a non-reserved keyword)
34363436
pub fn parse_identifier_non_reserved(&mut self) -> Result<Ident, ParserError> {
34373437
let token = self.next_token();
3438-
match token.token {
3438+
match token.token.clone() {
34393439
Token::Word(w) => {
34403440
match keywords::RESERVED_FOR_COLUMN_OR_TABLE_NAME.contains(&w.keyword) {
3441-
true => parser_err!(format!("syntax error at or near \"{w}\"")),
3441+
true => parser_err!(format!("syntax error at or near {token}")),
34423442
false => Ok(w.to_ident()?),
34433443
}
34443444
}

src/sqlparser/tests/sqlparser_common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ fn parse_select_all() {
253253
#[test]
254254
fn parse_select_all_distinct() {
255255
let result = parse_sql_statements("SELECT ALL DISTINCT name FROM customer");
256-
assert!(format!("{}", result.unwrap_err()).contains("syntax error at or near \"DISTINCT\""));
256+
assert!(format!("{}", result.unwrap_err())
257+
.contains("syntax error at or near DISTINCT at line:1, column:20"));
257258
}
258259

259260
#[test]

src/sqlparser/tests/sqlparser_postgres.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,31 +1047,31 @@ fn parse_array() {
10471047
assert_eq!(
10481048
parse_sql_statements(sql),
10491049
Err(ParserError::ParserError(
1050-
"syntax error at or near '[ at line:1, column:28'".to_string()
1050+
"syntax error at or near [ at line:1, column:28".to_string()
10511051
))
10521052
);
10531053

10541054
let sql = "SELECT ARRAY[ARRAY[], []]";
10551055
assert_eq!(
10561056
parse_sql_statements(sql),
10571057
Err(ParserError::ParserError(
1058-
"syntax error at or near '[ at line:1, column:24'".to_string()
1058+
"syntax error at or near [ at line:1, column:24".to_string()
10591059
))
10601060
);
10611061

10621062
let sql = "SELECT ARRAY[[1, 2], ARRAY[3, 4]]";
10631063
assert_eq!(
10641064
parse_sql_statements(sql),
10651065
Err(ParserError::ParserError(
1066-
"syntax error at or near 'ARRAY at line:1, column:27'".to_string()
1066+
"syntax error at or near ARRAY at line:1, column:27".to_string()
10671067
))
10681068
);
10691069

10701070
let sql = "SELECT ARRAY[[], ARRAY[]]";
10711071
assert_eq!(
10721072
parse_sql_statements(sql),
10731073
Err(ParserError::ParserError(
1074-
"syntax error at or near 'ARRAY at line:1, column:23'".to_string()
1074+
"syntax error at or near ARRAY at line:1, column:23".to_string()
10751075
))
10761076
);
10771077

src/sqlparser/tests/testdata/create.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- input: CREATE TABLE T (a STRUCT<v1 INT>)
3636
formatted_sql: CREATE TABLE T (a STRUCT<v1 INT>)
3737
- input: CREATE TABLE T (FULL INT)
38-
error_msg: 'sql parser error: syntax error at or near "FULL"'
38+
error_msg: 'sql parser error: syntax error at or near FULL at line:1, column:21'
3939
- input: CREATE TABLE T ("FULL" INT)
4040
formatted_sql: CREATE TABLE T ("FULL" INT)
4141
- input: CREATE USER user WITH SUPERUSER CREATEDB PASSWORD 'password'

src/sqlparser/tests/testdata/select.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
sql parser error: Expected ), found: minutes at line:1, column:62
7272
Near "(t, x, interval '10'"
7373
- input: SELECT 1, FROM t
74-
error_msg: 'sql parser error: syntax error at or near "FROM"'
74+
error_msg: 'sql parser error: syntax error at or near FROM at line:1, column:15'
7575
- input: SELECT 1, WHERE true
76-
error_msg: 'sql parser error: syntax error at or near "WHERE"'
76+
error_msg: 'sql parser error: syntax error at or near WHERE at line:1, column:16'
7777
- input: SELECT timestamp with time zone '2022-10-01 12:00:00Z' AT TIME ZONE 'US/Pacific'
7878
formatted_sql: SELECT TIMESTAMP WITH TIME ZONE '2022-10-01 12:00:00Z' AT TIME ZONE 'US/Pacific'
7979
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 })'

0 commit comments

Comments
 (0)