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
6 changes: 6 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,6 @@
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);
SELECT * FROM foo-1`x`;
15 changes: 11 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14419,6 +14419,8 @@ impl<'a> Parser<'a> {
/// with a digit. Subsequent segments are either must either be valid identifiers or
/// integers, e.g. foo-123 is allowed, but foo-123a is not.
///
/// A quoted alias may immediately follow the identifier.
///
/// [BigQuery-lexical](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical)
///
/// Return a tuple of the identifier and a boolean indicating it ends with a period.
Expand Down Expand Up @@ -14474,11 +14476,16 @@ 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`.
// An unquoted word or number can extend a trailing number, 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(Word {
quote_style: None,
..
}) | Token::Number(_, false)
) {
return self
.expected("whitespace following hyphenated identifier", token);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,31 @@ fn parse_hyphenated_table_identifiers() {
);
}

#[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
);
}

#[test]
fn parse_table_time_travel() {
let version = "2023-08-18 23:08:18".to_string();
Expand Down Expand Up @@ -3001,3 +3026,17 @@ fn test_byte_and_raw_string_quote_escaping() {
err
);
}

#[test]
fn parse_table_identifier_with_adjacent_quoted_alias() {
for table in ["foo", "foo-bar", "foo-1"] {
let sql = format!("SELECT * FROM {table}`x`");
let canonical = format!("SELECT * FROM {table} `x`");
let select = bigquery().verified_only_select_with_canonical(&sql, canonical.as_str());
let TableFactor::Table { name, alias, .. } = &select.from[0].relation else {
panic!("Expected a table");
};
assert_eq!(name, &ObjectName::from(vec![Ident::new(table)]));
assert_eq!(alias.as_ref().unwrap().name, Ident::with_quote('`', "x"));
}
}
Loading