Skip to content

Commit 4188b0a

Browse files
committed
fix: reserved keywords as column names with trailing comma detection
When trailing commas are enabled (ClickHouse/BigQuery/Snowflake), keywords like FORMAT, SETTINGS, SAMPLE in RESERVED_FOR_COLUMN_ALIAS were incorrectly treated as clause terminators even when used as column names. Now checks the token after the reserved keyword: if followed by ',' or another reserved keyword (like FROM), it's a column name, not a clause start. Fixes: SELECT database, table, format, sample FROM t
1 parent 7441dd6 commit 4188b0a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/parser/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,12 +3519,20 @@ impl<'a> Parser<'a> {
35193519
Token::Word(ref kw)
35203520
if keywords::RESERVED_FOR_COLUMN_ALIAS.contains(&kw.keyword) =>
35213521
{
3522-
// If the reserved keyword is followed by '(', it's likely a
3523-
// function call (e.g., `format(...)`) not a trailing comma
3524-
if self.peek_nth_token_ref(1).token == Token::LParen {
3525-
false
3526-
} else {
3527-
true
3522+
// Check the token after the reserved keyword to decide if this
3523+
// is a trailing comma or a column named with a reserved word.
3524+
match &self.peek_nth_token_ref(1).token {
3525+
// `format(...)` - function call, not trailing comma
3526+
Token::LParen => false,
3527+
// `format, col2` - column name followed by more columns
3528+
Token::Comma => false,
3529+
// `sample FROM t` - column name followed by clause keyword
3530+
Token::Word(w2)
3531+
if keywords::RESERVED_FOR_COLUMN_ALIAS.contains(&w2.keyword) =>
3532+
{
3533+
false
3534+
}
3535+
_ => true,
35283536
}
35293537
}
35303538
Token::RParen | Token::SemiColon | Token::EOF | Token::RBracket | Token::RBrace => {

tests/sqlparser_clickhouse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,8 @@ fn test_clickhouse_trailing_commas() {
16221622
"SELECT (number, toDate('2019-05-20')), dictGetOrNull('range_key_dictionary', 'value', number, toDate('2019-05-20')), FROM system.numbers LIMIT 5 FORMAT TabSeparated",
16231623
"SELECT (number, toDate('2019-05-20')), dictGetOrNull('range_key_dictionary', 'value', number, toDate('2019-05-20')) FROM system.numbers LIMIT 5 FORMAT TabSeparated",
16241624
);
1625+
// Reserved keywords used as column names should not trigger trailing comma detection
1626+
clickhouse().verified_stmt("SELECT database, table, format, sample, settings FROM t");
16251627
}
16261628

16271629
#[test]

0 commit comments

Comments
 (0)