Skip to content

Commit

Permalink
fix(parser): more error reporting with locations (#12129)
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao authored Sep 7, 2023
1 parent 3ee8a87 commit b578db6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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<Ident, ParserError> {
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()?),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/sqlparser/tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions src/sqlparser/tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,31 +1047,31 @@ 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()
))
);

let sql = "SELECT ARRAY[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()
))
);

let sql = "SELECT ARRAY[[1, 2], ARRAY[3, 4]]";
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()
))
);

let sql = "SELECT ARRAY[[], 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()
))
);

Expand Down
2 changes: 1 addition & 1 deletion src/sqlparser/tests/testdata/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- input: CREATE TABLE T (a STRUCT<v1 INT>)
formatted_sql: CREATE TABLE T (a STRUCT<v1 INT>)
- 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'
Expand Down
4 changes: 2 additions & 2 deletions src/sqlparser/tests/testdata/select.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 })'
Expand Down

0 comments on commit b578db6

Please sign in to comment.