Skip to content

Commit 6e08364

Browse files
committed
Improve accuracy of lookahead in implicit LIMIT alias
1 parent bc2c4e2 commit 6e08364

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/dialect/snowflake.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ impl Dialect for SnowflakeDialect {
351351
match kw {
352352
// The following keywords can be considered an alias as long as
353353
// they are not followed by other tokens that may change their meaning
354-
Keyword::LIMIT
355-
| Keyword::RETURNING
354+
Keyword::RETURNING
356355
| Keyword::INNER
357356
| Keyword::USING
358357
| Keyword::PIVOT
@@ -365,6 +364,18 @@ impl Dialect for SnowflakeDialect {
365364
false
366365
}
367366

367+
// `LIMIT` can be considered an alias as long as it's not followed by a value. For example:
368+
// `SELECT * FROM tbl LIMIT WHERE 1=1` - alias
369+
// `SELECT * FROM tbl LIMIT 3` - not an alias
370+
Keyword::LIMIT
371+
if matches!(
372+
parser.peek_token().token,
373+
Token::Number(_, _) | Token::Placeholder(_)
374+
) =>
375+
{
376+
false
377+
}
378+
368379
// `FETCH` can be considered an alias as long as it's not followed by `FIRST`` or `NEXT`
369380
// which would give it a different meanings, for example:
370381
// `SELECT * FROM tbl FETCH FIRST 10 ROWS` - not an alias
@@ -373,7 +384,10 @@ impl Dialect for SnowflakeDialect {
373384
if parser
374385
.peek_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])
375386
.is_some()
376-
|| matches!(parser.peek_token().token, Token::Number(_, _)) =>
387+
|| matches!(
388+
parser.peek_token().token,
389+
Token::Number(_, _) | Token::Placeholder(_)
390+
) =>
377391
{
378392
false
379393
}
@@ -387,6 +401,7 @@ impl Dialect for SnowflakeDialect {
387401
{
388402
false
389403
}
404+
390405
Keyword::GLOBAL if parser.peek_keyword(Keyword::FULL) => false,
391406

392407
// Reserved keywords by the Snowflake dialect, which seem to be less strictive

tests/sqlparser_snowflake.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,6 +3541,11 @@ fn test_sql_keywords_as_table_aliases() {
35413541
.parse_sql_statements(&format!("SELECT * FROM tbl {kw}"))
35423542
.is_err());
35433543
}
3544+
3545+
// LIMIT as alias and not as alias
3546+
snowflake().one_statement_parses_to("SELECT * FROM tbl LIMIT", "SELECT * FROM tbl AS LIMIT");
3547+
snowflake().verified_stmt("SELECT * FROM tbl LIMIT 1");
3548+
snowflake().verified_stmt("SELECT * FROM tbl LIMIT $1");
35443549
}
35453550

35463551
#[test]

0 commit comments

Comments
 (0)