LucaCappelletti94 commented on code in PR #2969:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2969#discussion_r4578885058
##########
src/parser/mod.rs:
##########
@@ -5188,10 +5188,23 @@ impl<'a> Parser<'a> {
self.parse_comma_separated_with_trailing_commas(
|p| p.parse_select_item(),
trailing_commas,
- Self::is_reserved_for_column_alias,
+ |kw, parser| {
+ Self::is_reserved_for_column_alias(kw, parser)
+ && !Self::is_keyword_projection_item(parser)
+ },
)
}
+ /// Called after a reserved keyword that follows a comma in a projection,
e.g.
+ /// `SELECT a, offset FROM t`. Returns true if the next token means the
keyword
+ /// is a select item rather than a clause after a trailing comma.
+ fn is_keyword_projection_item(parser: &mut Parser) -> bool {
+ parser.peek_token_ref().token == Token::Comma
+ || parser
+ .peek_one_of_keywords(&[Keyword::FROM, Keyword::AS])
+ .is_some()
+ }
Review Comment:
You should limit the column reading to `LIMIT` and `OFFSET`. As written it
covers every keyword in `RESERVED_FOR_COLUMN_ALIAS`.
Since you are using claude to write this code, you sure make it fuzz and
test extensively the syntax you want to add.
```suggestion
|kw, parser| {
Self::is_reserved_for_column_alias(kw, parser)
&& !(matches!(kw, Keyword::LIMIT | Keyword::OFFSET)
&& parser.peek_select_item_end(0))
},
)
}
/// Returns true if the `n`th next token ends a select item, so that a
`LIMIT`
/// or `OFFSET` before it names a column, as in `SELECT offset, limit
FROM t`.
fn peek_select_item_end(&self, n: usize) -> bool {
match &self.peek_nth_token_ref(n).token {
Token::Comma => true,
Token::Word(w) => matches!(w.keyword, Keyword::FROM |
Keyword::AS),
_ => false,
}
}
```
##########
tests/sqlparser_common.rs:
##########
@@ -23516,3 +23516,22 @@ fn parse_national_character_string_types() {
ParserError::ParserError("Expected: ), found: EOF".to_string())
);
}
+
+#[test]
+fn parse_projection_trailing_comma_keyword_column() {
+ let dialects = all_dialects_where(|d|
d.supports_projection_trailing_commas());
+
+ for sql in [
+ "SELECT 'a' AS a, offset FROM t",
+ "SELECT a, offset FROM t",
+ "SELECT a, limit FROM t",
+ "SELECT a, offset, b FROM t",
+ "SELECT a, offset AS o FROM t",
+ "SELECT * FROM (SELECT a, offset FROM t)",
+ ] {
+ dialects.verified_stmt(sql);
+ }
+
+ dialects.one_statement_parses_to("SELECT a, FROM t", "SELECT a FROM t");
+ dialects.one_statement_parses_to("SELECT a, LIMIT 1", "SELECT a LIMIT 1");
+}
Review Comment:
You should cover the first-item queries and a keyword that must stay
reserved. The two first-item cases and the `union` case fail on the current
head.
```suggestion
for sql in [
"SELECT offset FROM t",
"SELECT offset, 'a' AS a FROM t",
"SELECT 'a' AS a, offset FROM t",
"SELECT a, offset FROM t",
"SELECT a, limit FROM t",
"SELECT a, offset, b FROM t",
"SELECT a, offset AS o FROM t",
"SELECT * FROM (SELECT a, offset FROM t)",
] {
dialects.verified_stmt(sql);
}
dialects.one_statement_parses_to("SELECT a, FROM t", "SELECT a FROM t");
dialects.one_statement_parses_to("SELECT a, LIMIT 1", "SELECT a LIMIT
1");
assert_eq!(
dialects
.parse_sql_statements("SELECT a, union, b FROM t")
.unwrap_err(),
ParserError::ParserError(
"Expected: SELECT, VALUES, or a subquery in the query body,
found: ,".to_string()
)
);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]