Allow quoted aliases after BigQuery numeric hyphen segments
diff --git a/fuzz/fuzz_seeds/bigquery_2620_hyphen_numeric_boundary.sql b/fuzz/fuzz_seeds/bigquery_2620_hyphen_numeric_boundary.sql index 568b8e1..38a03ee 100644 --- a/fuzz/fuzz_seeds/bigquery_2620_hyphen_numeric_boundary.sql +++ b/fuzz/fuzz_seeds/bigquery_2620_hyphen_numeric_boundary.sql
@@ -2,4 +2,5 @@ SELECT * FROM foo-1; SELECT * FROM (SELECT 1 FROM foo-1); SELECT * FROM foo-1, bar-2; -CREATE TABLE t-1 (a INT64) \ No newline at end of file +CREATE TABLE t-1 (a INT64); +SELECT * FROM foo-1`x`; \ No newline at end of file
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index c1fbfa8..9a7b86d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs
@@ -14419,6 +14419,8 @@ /// 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. @@ -14474,10 +14476,16 @@ } } - // A word or number butted onto a trailing number glues into it, as in `foo-123a`. + // An unquoted word or number can extend a trailing number, as in `foo-123a`. if requires_whitespace { let token = self.peek_token_no_skip(); - if matches!(token.token, Token::Word(_) | Token::Number(_, false)) { + if matches!( + token.token, + Token::Word(Word { + quote_style: None, + .. + }) | Token::Number(_, false) + ) { return self .expected("whitespace following hyphenated identifier", token); }
diff --git a/tests/sqlparser_bigquery.rs b/tests/sqlparser_bigquery.rs index 15a08e4..78518a0 100644 --- a/tests/sqlparser_bigquery.rs +++ b/tests/sqlparser_bigquery.rs
@@ -1770,16 +1770,6 @@ ), 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 - ); } #[test] @@ -3036,3 +3026,17 @@ 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")); + } +}