Skip to content

Commit 15f35e1

Browse files
authored
Snowflake Reserved SQL Keywords as Implicit Table Alias (#1934)
1 parent fd4934e commit 15f35e1

File tree

4 files changed

+163
-12
lines changed

4 files changed

+163
-12
lines changed

src/dialect/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,11 +992,17 @@ pub trait Dialect: Debug + Any {
992992
explicit || self.is_column_alias(kw, parser)
993993
}
994994

995+
/// Returns true if the specified keyword should be parsed as a table identifier.
996+
/// See [keywords::RESERVED_FOR_TABLE_ALIAS]
997+
fn is_table_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
998+
!keywords::RESERVED_FOR_TABLE_ALIAS.contains(kw)
999+
}
1000+
9951001
/// Returns true if the specified keyword should be parsed as a table factor alias.
9961002
/// When explicit is true, the keyword is preceded by an `AS` word. Parser is provided
9971003
/// to enable looking ahead if needed.
998-
fn is_table_factor_alias(&self, explicit: bool, kw: &Keyword, _parser: &mut Parser) -> bool {
999-
explicit || !keywords::RESERVED_FOR_TABLE_ALIAS.contains(kw)
1004+
fn is_table_factor_alias(&self, explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
1005+
explicit || self.is_table_alias(kw, parser)
10001006
}
10011007

10021008
/// Returns true if this dialect supports querying historical table data

src/dialect/snowflake.rs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ impl Dialect for SnowflakeDialect {
318318
}
319319

320320
// `FETCH` can be considered an alias as long as it's not followed by `FIRST`` or `NEXT`
321-
// which would give it a different meanings, for example: `SELECT 1 FETCH FIRST 10 ROWS` - not an alias
322-
Keyword::FETCH
323-
if parser.peek_keyword(Keyword::FIRST) || parser.peek_keyword(Keyword::NEXT) =>
321+
// which would give it a different meanings, for example:
322+
// `SELECT 1 FETCH FIRST 10 ROWS` - not an alias
323+
// `SELECT 1 FETCH 10` - not an alias
324+
Keyword::FETCH if parser.peek_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT]).is_some()
325+
|| matches!(parser.peek_token().token, Token::Number(_, _)) =>
324326
{
325327
false
326328
}
@@ -345,6 +347,86 @@ impl Dialect for SnowflakeDialect {
345347
}
346348
}
347349

350+
fn is_table_alias(&self, kw: &Keyword, parser: &mut Parser) -> bool {
351+
match kw {
352+
// The following keywords can be considered an alias as long as
353+
// they are not followed by other tokens that may change their meaning
354+
Keyword::LIMIT
355+
| Keyword::RETURNING
356+
| Keyword::INNER
357+
| Keyword::USING
358+
| Keyword::PIVOT
359+
| Keyword::UNPIVOT
360+
| Keyword::EXCEPT
361+
| Keyword::MATCH_RECOGNIZE
362+
| Keyword::OFFSET
363+
if !matches!(parser.peek_token_ref().token, Token::SemiColon | Token::EOF) =>
364+
{
365+
false
366+
}
367+
368+
// `FETCH` can be considered an alias as long as it's not followed by `FIRST`` or `NEXT`
369+
// which would give it a different meanings, for example:
370+
// `SELECT * FROM tbl FETCH FIRST 10 ROWS` - not an alias
371+
// `SELECT * FROM tbl FETCH 10` - not an alias
372+
Keyword::FETCH
373+
if parser
374+
.peek_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])
375+
.is_some()
376+
|| matches!(parser.peek_token().token, Token::Number(_, _)) =>
377+
{
378+
false
379+
}
380+
381+
// All sorts of join-related keywords can be considered aliases unless additional
382+
// keywords change their meaning.
383+
Keyword::RIGHT | Keyword::LEFT | Keyword::SEMI | Keyword::ANTI
384+
if parser
385+
.peek_one_of_keywords(&[Keyword::JOIN, Keyword::OUTER])
386+
.is_some() =>
387+
{
388+
false
389+
}
390+
Keyword::GLOBAL if parser.peek_keyword(Keyword::FULL) => false,
391+
392+
// Reserved keywords by the Snowflake dialect, which seem to be less strictive
393+
// than what is listed in `keywords::RESERVED_FOR_TABLE_ALIAS`. The following
394+
// keywords were tested with the this statement: `SELECT <KW>.* FROM tbl <KW>`.
395+
Keyword::WITH
396+
| Keyword::ORDER
397+
| Keyword::SELECT
398+
| Keyword::WHERE
399+
| Keyword::GROUP
400+
| Keyword::HAVING
401+
| Keyword::LATERAL
402+
| Keyword::UNION
403+
| Keyword::INTERSECT
404+
| Keyword::MINUS
405+
| Keyword::ON
406+
| Keyword::JOIN
407+
| Keyword::INNER
408+
| Keyword::CROSS
409+
| Keyword::FULL
410+
| Keyword::LEFT
411+
| Keyword::RIGHT
412+
| Keyword::NATURAL
413+
| Keyword::USING
414+
| Keyword::ASOF
415+
| Keyword::MATCH_CONDITION
416+
| Keyword::SET
417+
| Keyword::QUALIFY
418+
| Keyword::FOR
419+
| Keyword::START
420+
| Keyword::CONNECT
421+
| Keyword::SAMPLE
422+
| Keyword::TABLESAMPLE
423+
| Keyword::FROM => false,
424+
425+
// Any other word is considered an alias
426+
_ => true,
427+
}
428+
}
429+
348430
/// See: <https://docs.snowflake.com/en/sql-reference/constructs/at-before>
349431
fn supports_timestamp_versioning(&self) -> bool {
350432
true

tests/sqlparser_common.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,7 +5548,8 @@ fn parse_named_window_functions() {
55485548
WINDOW w AS (PARTITION BY x), win AS (ORDER BY y)";
55495549
supported_dialects.verified_stmt(sql);
55505550

5551-
let select = verified_only_select(sql);
5551+
let select = all_dialects_except(|d| d.is_table_alias(&Keyword::WINDOW, &mut Parser::new(d)))
5552+
.verified_only_select(sql);
55525553

55535554
const EXPECTED_PROJ_QTY: usize = 2;
55545555
assert_eq!(EXPECTED_PROJ_QTY, select.projection.len());
@@ -5578,6 +5579,7 @@ fn parse_named_window_functions() {
55785579

55795580
#[test]
55805581
fn parse_window_clause() {
5582+
let dialects = all_dialects_except(|d| d.is_table_alias(&Keyword::WINDOW, &mut Parser::new(d)));
55815583
let sql = "SELECT * \
55825584
FROM mytable \
55835585
WINDOW \
@@ -5590,10 +5592,14 @@ fn parse_window_clause() {
55905592
window7 AS (window1 ROWS UNBOUNDED PRECEDING), \
55915593
window8 AS (window1 PARTITION BY a ORDER BY b ROWS UNBOUNDED PRECEDING) \
55925594
ORDER BY C3";
5593-
verified_only_select(sql);
5595+
dialects.verified_only_select(sql);
55945596

55955597
let sql = "SELECT * from mytable WINDOW window1 AS window2";
5596-
let dialects = all_dialects_except(|d| d.is::<BigQueryDialect>() || d.is::<GenericDialect>());
5598+
let dialects = all_dialects_except(|d| {
5599+
d.is::<BigQueryDialect>()
5600+
|| d.is::<GenericDialect>()
5601+
|| d.is_table_alias(&Keyword::WINDOW, &mut Parser::new(d))
5602+
});
55975603
let res = dialects.parse_sql_statements(sql);
55985604
assert_eq!(
55995605
ParserError::ParserError("Expected: (, found: window2".to_string()),
@@ -5603,14 +5609,15 @@ fn parse_window_clause() {
56035609

56045610
#[test]
56055611
fn test_parse_named_window() {
5612+
let dialects = all_dialects_except(|d| d.is_table_alias(&Keyword::WINDOW, &mut Parser::new(d)));
56065613
let sql = "SELECT \
56075614
MIN(c12) OVER window1 AS min1, \
56085615
MAX(c12) OVER window2 AS max1 \
56095616
FROM aggregate_test_100 \
56105617
WINDOW window1 AS (ORDER BY C12), \
56115618
window2 AS (PARTITION BY C11) \
56125619
ORDER BY C3";
5613-
let actual_select_only = verified_only_select(sql);
5620+
let actual_select_only = dialects.verified_only_select(sql);
56145621
let expected = Select {
56155622
select_token: AttachedToken::empty(),
56165623
distinct: None,
@@ -5759,14 +5766,18 @@ fn test_parse_named_window() {
57595766

57605767
#[test]
57615768
fn parse_window_and_qualify_clause() {
5769+
let dialects = all_dialects_except(|d| {
5770+
d.is_table_alias(&Keyword::WINDOW, &mut Parser::new(d))
5771+
|| d.is_table_alias(&Keyword::QUALIFY, &mut Parser::new(d))
5772+
});
57625773
let sql = "SELECT \
57635774
MIN(c12) OVER window1 AS min1 \
57645775
FROM aggregate_test_100 \
57655776
QUALIFY ROW_NUMBER() OVER my_window \
57665777
WINDOW window1 AS (ORDER BY C12), \
57675778
window2 AS (PARTITION BY C11) \
57685779
ORDER BY C3";
5769-
verified_only_select(sql);
5780+
dialects.verified_only_select(sql);
57705781

57715782
let sql = "SELECT \
57725783
MIN(c12) OVER window1 AS min1 \
@@ -5775,7 +5786,7 @@ fn parse_window_and_qualify_clause() {
57755786
window2 AS (PARTITION BY C11) \
57765787
QUALIFY ROW_NUMBER() OVER my_window \
57775788
ORDER BY C3";
5778-
verified_only_select(sql);
5789+
dialects.verified_only_select(sql);
57795790
}
57805791

57815792
#[test]
@@ -7443,7 +7454,8 @@ fn parse_join_syntax_variants() {
74437454
"SELECT c1 FROM t1 FULL JOIN t2 USING(c1)",
74447455
);
74457456

7446-
let res = parse_sql_statements("SELECT * FROM a OUTER JOIN b ON 1");
7457+
let dialects = all_dialects_except(|d| d.is_table_alias(&Keyword::OUTER, &mut Parser::new(d)));
7458+
let res = dialects.parse_sql_statements("SELECT * FROM a OUTER JOIN b ON 1");
74477459
assert_eq!(
74487460
ParserError::ParserError("Expected: APPLY, found: JOIN".to_string()),
74497461
res.unwrap_err()

tests/sqlparser_snowflake.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,6 +3492,57 @@ fn test_sql_keywords_as_select_item_aliases() {
34923492
}
34933493
}
34943494

3495+
#[test]
3496+
fn test_sql_keywords_as_table_aliases() {
3497+
// Some keywords that should be parsed as an alias implicitly
3498+
let unreserved_kws = vec![
3499+
"VIEW",
3500+
"EXPLAIN",
3501+
"ANALYZE",
3502+
"SORT",
3503+
"PIVOT",
3504+
"UNPIVOT",
3505+
"TOP",
3506+
"LIMIT",
3507+
"OFFSET",
3508+
"FETCH",
3509+
"EXCEPT",
3510+
"CLUSTER",
3511+
"DISTRIBUTE",
3512+
"GLOBAL",
3513+
"ANTI",
3514+
"SEMI",
3515+
"RETURNING",
3516+
"OUTER",
3517+
"WINDOW",
3518+
"END",
3519+
"PARTITION",
3520+
"PREWHERE",
3521+
"SETTINGS",
3522+
"FORMAT",
3523+
"MATCH_RECOGNIZE",
3524+
"OPEN",
3525+
];
3526+
3527+
for kw in unreserved_kws {
3528+
snowflake().verified_stmt(&format!("SELECT * FROM tbl AS {kw}"));
3529+
snowflake().one_statement_parses_to(
3530+
&format!("SELECT * FROM tbl {kw}"),
3531+
&format!("SELECT * FROM tbl AS {kw}"),
3532+
);
3533+
}
3534+
3535+
// Some keywords that should not be parsed as an alias implicitly
3536+
let reserved_kws = vec![
3537+
"FROM", "GROUP", "HAVING", "ORDER", "SELECT", "UNION", "WHERE", "WITH",
3538+
];
3539+
for kw in reserved_kws {
3540+
assert!(snowflake()
3541+
.parse_sql_statements(&format!("SELECT * FROM tbl {kw}"))
3542+
.is_err());
3543+
}
3544+
}
3545+
34953546
#[test]
34963547
fn test_timetravel_at_before() {
34973548
snowflake().verified_only_select("SELECT * FROM tbl AT(TIMESTAMP => '2024-12-15 00:00:00')");

0 commit comments

Comments
 (0)