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
29 changes: 12 additions & 17 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18476,24 +18476,19 @@ impl<'a> Parser<'a> {
}

/// Returns true if the immediate tokens look like the
/// beginning of a subquery. `(SELECT ...`
/// beginning of a subquery. `(SELECT ...` or `((SELECT ...` etc.
fn peek_subquery_start(&mut self) -> bool {
matches!(
self.peek_tokens_ref(),
[
TokenWithSpan {
token: Token::LParen,
..
},
TokenWithSpan {
token: Token::Word(Word {
keyword: Keyword::SELECT,
..
}),
..
},
]
)
// Handle (SELECT, ((SELECT, (((SELECT, etc.
// This makes INSERT consistent with other contexts where nested
// parentheses around subqueries are handled by recursive descent.
let mut i = 0;
loop {
match &self.peek_nth_token_ref(i).token {
Token::LParen => i += 1,
Token::Word(w) if w.keyword == Keyword::SELECT => return i > 0,
_ => return false,
}
}
}

/// Returns true if the immediate tokens look like the
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ fn parse_insert_select_from_returning() {
}
}

#[test]
fn parse_insert_nested_parenthesized_subquery() {
// The source query may be wrapped in one or more layers of parentheses;
// the leading parens must not be mistaken for a column list.
verified_stmt("INSERT INTO t ((SELECT 1))");
verified_stmt("INSERT INTO t (((SELECT 1)))");
}

#[test]
fn parse_returning_as_column_alias() {
verified_stmt("SELECT 1 AS RETURNING");
Expand Down
Loading