Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/generic_2049_keyword_after_comma.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a, offset FROM t;
32 changes: 31 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5188,10 +5188,37 @@ 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)
&& !(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 | 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
),
_ => false,
}
}
Comment on lines +5191 to +5220

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
|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()
}
|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,
}
}


/// Parse a list of actions for `GRANT` statements.
pub fn parse_actions_list(&mut self) -> Result<Vec<Action>, ParserError> {
let mut values = vec![];
Expand Down Expand Up @@ -16492,6 +16519,9 @@ impl<'a> Parser<'a> {
fn peek_empty_projection(&self) -> bool {
match &self.peek_token_ref().token {
Token::EOF | Token::SemiColon | Token::RParen => true,
Token::Word(w) if matches!(w.keyword, Keyword::LIMIT | Keyword::OFFSET) => {
!self.peek_select_item_end(1)
}
Token::Word(w) => matches!(
w.keyword,
Keyword::INTO
Expand Down
47 changes: 47 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23517,6 +23517,53 @@ fn parse_national_character_string_types() {
);
}

#[test]
fn parse_projection_trailing_comma_keyword_column() {
let dialects = all_dialects_where(|d| d.supports_projection_trailing_commas());

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)",
"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",
] {
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()
)
);

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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
"SELECT offset AS o FROM t",
"SELECT offset AS o FROM t",
"SELECT offset",
"SELECT offset WHERE b",

"SELECT offset",
"SELECT offset WHERE b",
] {
dialects.verified_stmt(sql);
}
dialects.verified_stmt("SELECT LIMIT 1");
dialects.verified_stmt("SELECT OFFSET 1");
}

#[test]
fn parse_create_procedure_string_body() {
match verified_stmt("CREATE PROCEDURE p (a INT) LANGUAGE sql AS 'INSERT INTO t VALUES (a)'") {
Expand Down
Loading