LucaCappelletti94 commented on code in PR #2969:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2969#discussion_r4579715468
##########
tests/sqlparser_common.rs:
##########
Review Comment:
You should cover a keyword column followed by something other than `FROM`.
These five fail on the current head, the first with `Expected: an expression,
found: EOF`.
```suggestion
"SELECT * FROM (SELECT a, offset FROM t)",
"SELECT a, offset",
"SELECT * FROM (SELECT a, offset) AS s",
"SELECT a, offset WHERE b",
"SELECT a, offset INTO u FROM t",
"SELECT a, offset UNION SELECT 1, 2",
```
##########
tests/sqlparser_common.rs:
##########
@@ -23534,4 +23536,23 @@ fn parse_projection_trailing_comma_keyword_column() {
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()
+ )
+ );
+
+ let dialects = all_dialects_where(|d| d.supports_empty_projections());
+ for sql in [
+ "SELECT offset FROM t",
+ "SELECT limit, offset FROM t",
+ "SELECT offset AS o FROM t",
Review Comment:
You should cover the same in the first position of an empty-projection
dialect. Both fail on the current head, the first with `Expected: an
expression, found: EOF`.
```suggestion
"SELECT offset AS o FROM t",
"SELECT offset",
"SELECT offset WHERE b",
```
##########
src/parser/mod.rs:
##########
@@ -5190,19 +5190,20 @@ impl<'a> Parser<'a> {
trailing_commas,
|kw, parser| {
Self::is_reserved_for_column_alias(kw, parser)
- && !Self::is_keyword_projection_item(parser)
+ && !(matches!(kw, Keyword::LIMIT | Keyword::OFFSET)
+ && parser.peek_select_item_end(0))
},
)
}
- /// 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()
+ /// 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),
Review Comment:
You should treat every token that ends a select item as the end, not only
`,`, `FROM` and `AS`.
```suggestion
Token::Comma | Token::EOF | Token::SemiColon | Token::RParen =>
true,
Token::Word(w) => matches!(
w.keyword,
Keyword::AS
| Keyword::INTO
| Keyword::FROM
| Keyword::WHERE
| Keyword::GROUP
| Keyword::HAVING
| Keyword::WINDOW
| Keyword::ORDER
| Keyword::UNION
| Keyword::INTERSECT
| Keyword::EXCEPT
),
```
--
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]