Skip to content

Fix INSERT parser failing on double-parenthesized compound SELECT#98247

Merged
alexey-milovidov merged 1 commit intomasterfrom
fix-insert-compound-select-formatting
Feb 27, 2026
Merged

Fix INSERT parser failing on double-parenthesized compound SELECT#98247
alexey-milovidov merged 1 commit intomasterfrom
fix-insert-compound-select-formatting

Conversation

@alexey-milovidov
Copy link
Copy Markdown
Member

Summary

  • Fix the INSERT query parser to rewind on column list parse failure, instead of returning false immediately
  • This fixes "Inconsistent AST formatting" exception when INSERT queries use compound SELECT with INTERSECT/EXCEPT operators
  • When the AST formatter wraps nested unions in parentheses (producing e.g., INSERT INTO t1 ((SELECT 1) EXCEPT ALL (SELECT 2))), the parser now correctly handles the double parentheses

The root cause: the INSERT parser consumed the first ( for column list parsing, but when the second ( couldn't be parsed as a column name, it returned false instead of rewinding pos to try parsing ((SELECT ...) as a compound SELECT query.

Closes #90925

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=bfd26cd518682a2051429b51714e9c8c62806f45&name_0=MasterCI&name_1=BuzzHouse%20%28amd_debug%29&name_1=BuzzHouse%20%28amd_debug%29

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

When `ASTSelectWithUnionQuery::formatQueryImpl` formats a compound SELECT
(e.g., with `INTERSECT`/`EXCEPT`) that is a child of INSERT, it wraps
nested unions in parentheses for correctness. This can produce queries
like `INSERT INTO t1 ((SELECT 1) EXCEPT ALL (SELECT 2)) INTERSECT (SELECT 3)`.

The INSERT parser failed to re-parse such queries because when it
encountered `((SELECT ...`, it consumed the first `(` for the column
list check, then failed to parse `(` as a column name and returned false
immediately, instead of rewinding and letting the `(` be parsed as part
of a SELECT query.

The fix rewinds `pos` to before the `(` when column list parsing fails
entirely, matching the existing behavior when column parsing succeeds
but no closing `)` is found.

Closes #90925

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=bfd26cd518682a2051429b51714e9c8c62806f45&name_0=MasterCI&name_1=BuzzHouse%20%28amd_debug%29&name_1=BuzzHouse%20%28amd_debug%29

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Feb 27, 2026

Workflow [PR], commit [2763882]

Summary:

@clickhouse-gh clickhouse-gh bot added the pr-ci label Feb 27, 2026
@alexey-milovidov alexey-milovidov self-assigned this Feb 27, 2026
@alexey-milovidov alexey-milovidov added this pull request to the merge queue Feb 27, 2026
Merged via the queue into master with commit 02b225a Feb 27, 2026
148 checks passed
@alexey-milovidov alexey-milovidov deleted the fix-insert-compound-select-formatting branch February 27, 2026 22:32
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent AST formatting of INSERT SELECT EXCEPT/INTERSECT

2 participants