Skip to content

Fix inconsistent AST formatting for EXPLAIN with UNION and trailing SETTINGS - #99485

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-ast-formatting-explain-union-settings
Mar 16, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-ast-formatting-explain-union-settings

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix AST formatting round-trip inconsistency when EXPLAIN wraps a SelectWithUnionQuery and has output-level SETTINGS.
  • The last SELECT in the UNION chain was not parenthesized, causing ParserSelectQuery to consume SETTINGS during re-parse, moving it from the EXPLAIN node to the last SelectQuery.
  • Propagate a parent_has_trailing_settings flag through FormatStateStacked so ASTSelectWithUnionQuery parenthesizes individual SELECTs when a parent will append SETTINGS.

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

Test plan

  • Added 04039_explain_union_settings_formatting_roundtrip test verifying formatQuerySingleLine output has parenthesized SELECTs
  • Verified the original fuzzer query formats correctly
  • Build and local test pass

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):

...

🤖 Generated with Claude Code

Version info

  • Merged into: 26.3.1.703

…ETTINGS

When an `EXPLAIN` query wraps a `SelectWithUnionQuery` and has output-level
`SETTINGS` (from `ASTQueryWithOutput`), the SETTINGS clause was formatted
after the UNION chain without parenthesizing the last SELECT. During
re-parsing, `ParserSelectQuery` eagerly consumed SETTINGS as part of the
last SELECT, causing the SETTINGS to migrate from the `EXPLAIN` node to
the last `SelectQuery` - an inconsistent AST round-trip.

The fix propagates a `parent_has_trailing_settings` flag through
`FormatStateStacked`, set in `ASTQueryWithOutput::formatImpl` when
`settings_ast` is present without preceding `out_file` or `format_ast`.
`ASTSelectWithUnionQuery::formatQueryImpl` checks this flag (alongside
its existing check for its own `settings_ast`) and wraps individual
SELECTs in parentheses, preventing the parser from consuming the
trailing SETTINGS.

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

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [26625b2]

Summary: ✅


AI Review

Summary

This PR fixes AST formatting round-trip for queries where EXPLAIN wraps UNION/INTERSECT and has trailing SETTINGS, by propagating a parent-context flag so ASTSelectWithUnionQuery adds protective parentheses when needed. It also avoids redundant inner parentheses for CREATE ... AS SELECT ... SETTINGS by clearing the propagated flag at the outer AS (...) boundary. Based on the diff and provided CI context, I found no high-confidence blocker or major issue.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 14, 2026
alexey-milovidov and others added 2 commits March 13, 2026 22:28
… SETTINGS

`ASTCreateQuery` already wraps the AS-select in parentheses when
`settings_ast` is present, which prevents `ParserSelectQuery` from
consuming the trailing SETTINGS. Clear `parent_has_trailing_settings`
before formatting the inner select to avoid `ASTSelectWithUnionQuery`
adding redundant parentheses around individual SELECTs.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.80% +0.10%
Functions 23.90% 23.90% +0.00%
Branches 76.20% 76.30% +0.10%

PR changed lines: PR changed-lines coverage: 100.00% (15/15, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 16, 2026
@alexey-milovidov alexey-milovidov self-assigned this Mar 16, 2026
Merged via the queue into master with commit cd63f6f Mar 16, 2026
159 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-ast-formatting-explain-union-settings branch March 16, 2026 12:25
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 16, 2026
alexey-milovidov added a commit that referenced this pull request Mar 17, 2026
…`parent_has_trailing_settings`

The merge from master brought PR #99485 which renamed the field. Also
rename test files to avoid index collision with tests added on master.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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.

2 participants