Skip to content

Handle nested parentheses in INSERT subquery detection#2387

Open
sabir-akhadov-localstack wants to merge 1 commit into
apache:mainfrom
localstack:pr/02-insert-nested-paren-subquery
Open

Handle nested parentheses in INSERT subquery detection#2387
sabir-akhadov-localstack wants to merge 1 commit into
apache:mainfrom
localstack:pr/02-insert-nested-paren-subquery

Conversation

@sabir-akhadov-localstack

@sabir-akhadov-localstack sabir-akhadov-localstack commented Jun 23, 2026

Copy link
Copy Markdown
Contributor
INSERT INTO t ((SELECT 1));

Before this change the statement above failed to parse: a source query wrapped in more than one layer of parentheses was mistaken for an INSERT column list.

When parsing INSERT INTO t <source>, the parser must decide whether the tokens following the table name begin an explicit column list (a, b, ...) or the source query (SELECT ...). It made this decision in peek_subquery_start by matching exactly ( SELECT, so any extra leading paren tripped the column-list branch — even though the equivalent nested-parenthesized subquery parses fine in other positions.

This changes peek_subquery_start to skip any number of leading ( and treat the tokens as a subquery start as long as at least one paren precedes the SELECT, making INSERT consistent with how nested parentheses around subqueries are already handled elsewhere via recursive descent.

A regression test is added in tests/sqlparser_common.rs covering INSERT INTO t ((SELECT 1)) and INSERT INTO t (((SELECT 1))).

When parsing `INSERT INTO t <source>`, the parser must decide whether
the tokens following the table name begin an explicit column list
`(a, b, ...)` or the source query `(SELECT ...)`. `peek_subquery_start`
made this decision by matching exactly `( SELECT`, so a source query
wrapped in more than one layer of parentheses — e.g.
`INSERT INTO t ((SELECT 1))` — was mistaken for a column list and
failed to parse.

Skip any number of leading `(` and treat the tokens as a subquery start
as long as at least one paren precedes the `SELECT`. This matches how
nested parentheses around subqueries are already handled elsewhere via
recursive descent.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sabir-akhadov-localstack sabir-akhadov-localstack force-pushed the pr/02-insert-nested-paren-subquery branch from 6db7f86 to d208e5e Compare June 23, 2026 15:39
@sabir-akhadov-localstack sabir-akhadov-localstack marked this pull request as ready for review June 23, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant