Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fuzz/fuzz_seeds/bigquery_2620_hyphen_numeric_boundary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT * FROM project-1.dataset-2.table-3 WHERE x = 1;
SELECT * FROM foo-1;
SELECT * FROM (SELECT 1 FROM foo-1);
SELECT * FROM foo-1, bar-2;
CREATE TABLE t-1 (a INT64)
7 changes: 3 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14241,11 +14241,10 @@ impl<'a> Parser<'a> {
}
}

// If the last segment was a number, we must check that it's followed by whitespace,
// otherwise foo-123a will be parsed as `foo-123` with the alias `a`.
// A word or number butted onto a trailing number glues into it, as in `foo-123a`.
if requires_whitespace {
let token = self.next_token();
if !matches!(token.token, Token::EOF | Token::Whitespace(_)) {
let token = self.peek_token_no_skip();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if matches!(token.token, Token::Word(_) | Token::Number(_, false)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foox and `foo-bar`x parse with alias x on main and here, so pinning foo-1x`` as an error is inconsistent; a quoted word cannot glue into the identifier, so Token::Word(Word { quote_style: None, .. }) would make the numeric segment behave like the word segment.

return self
.expected("whitespace following hyphenated identifier", token);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3001,3 +3001,38 @@ fn test_byte_and_raw_string_quote_escaping() {
err
);
}

#[test]
fn parse_hyphenated_identifier_after_numeric_segment() {
bigquery().verified_stmt("SELECT * FROM project-1.dataset-2.table-3 WHERE x = 1");
bigquery().verified_stmt("SELECT * FROM foo-1 AS t");
bigquery().verified_stmt("SELECT * FROM foo-1 JOIN bar-2 USING(id)");
bigquery().verified_stmt("SELECT * FROM foo-1, bar-2");
bigquery().verified_stmt("DELETE FROM foo-1 WHERE a = 1");
bigquery().verified_stmt("UPDATE foo-1 SET a = 1");
bigquery().verified_stmt("CREATE TABLE t-1 (a INT64)");
bigquery().verified_stmt("CREATE TABLE ship_mo-29 (a INT64)");
bigquery().one_statement_parses_to("SELECT * FROM foo-1;", "SELECT * FROM foo-1");
bigquery().verified_stmt("SELECT * FROM (SELECT 1 FROM foo-1)");
bigquery().one_statement_parses_to("CREATE TABLE t-1(a INT64)", "CREATE TABLE t-1 (a INT64)");

let err = bigquery()
.parse_sql_statements("SELECT * FROM foo-123a")
.unwrap_err();
assert_eq!(
ParserError::ParserError(
"Expected: whitespace following hyphenated identifier, found: a".to_string(),
),
err
);

let err = bigquery()
.parse_sql_statements("SELECT * FROM foo-1`x`")
.unwrap_err();
assert_eq!(
ParserError::ParserError(
"Expected: whitespace following hyphenated identifier, found: `x`".to_string(),
),
err
);
}
Loading