Skip to content

Fix inconsistent AST formatting for CREATE TABLE AS SELECT with trailing output options - #99545

Merged
alexey-milovidov merged 8 commits into
masterfrom
fix-ast-formatting-create-as-select-settings
Mar 18, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
fix-ast-formatting-create-as-select-settings

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

When an outer query (like EXPLAIN) has trailing output options (SETTINGS, FORMAT, INTO OUTFILE), and the inner query is CREATE TABLE ... AS SELECT, the SELECT must be parenthesized. Otherwise, during re-parsing the trailing SETTINGS clause gets consumed by the inner SELECT instead of remaining on the outer query, causing a tree hash mismatch in debug builds.

The fix propagates a has_trailing_output_options flag through the format state stack. ASTQueryWithOutput::formatImpl sets it when output options are present, and ASTCreateQuery::formatQueryImpl uses it (in addition to its own settings_ast) to decide whether to wrap the AS-select in parens. ASTExplainQuery::formatQueryImpl also wraps non-ASTQueryWithOutput inner queries in parentheses when the flag is set.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98677&sha=5479db65b9f92cc86fffbe82518c9731d1655351&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29
#98677

Changelog category (leave one):

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

Version info

  • Merged into: 26.3.1.802

…ing output options

When an outer query (like EXPLAIN) has trailing output options (SETTINGS,
FORMAT, INTO OUTFILE), and the inner query is CREATE TABLE ... AS SELECT,
the SELECT must be parenthesized. Otherwise, during re-parsing the trailing
SETTINGS clause gets consumed by the inner SELECT instead of remaining on
the outer query, causing a tree hash mismatch in debug builds.

The fix propagates a `has_trailing_output_options` flag through the format
state stack. `ASTQueryWithOutput::formatImpl` sets it when output options
are present, and `ASTCreateQuery::formatQueryImpl` uses it (in addition to
its own `settings_ast`) to decide whether to wrap the AS-select in parens.
`ASTExplainQuery::formatQueryImpl` also wraps non-`ASTQueryWithOutput`
inner queries in parentheses when the flag is set.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98677&sha=5479db65b9f92cc86fffbe82518c9731d1655351&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29

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

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks good.

@clickhouse-gh

clickhouse-gh Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f080f6a]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 16, 2026
The server crash was from `assertHasValidVersionMetadata` in test
`04039_assert_valid_version_metadata_race`, which killed all parallel tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…create-as-select-settings

# Conflicts:
#	src/Parsers/ASTCreateQuery.cpp
#	src/Parsers/ASTQueryWithOutput.cpp
#	src/Parsers/IAST.h
Comment thread src/Parsers/ASTExplainQuery.h Resolved
alexey-milovidov and others added 3 commits March 17, 2026 00:58
…`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]>
The `parent_has_trailing_settings` flag was only set by
`ASTQueryWithOutput::formatImpl` for SETTINGS without FORMAT/OUTFILE
(to avoid unnecessary parens in `ASTSelectWithUnionQuery`). But for
`ASTCreateQuery`, FORMAT and INTO OUTFILE also require parenthesizing
the AS-select. Set the flag in `ASTExplainQuery::formatQueryImpl`
when any output option is present, so the flag reaches the inner
CREATE TABLE query.

Also revert the unnecessary change to `ASTQueryWithOutput.cpp` — the
existing condition is correct for `ASTSelectWithUnionQuery`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…ExplainQuery`

The previous commit wrapped non-`ASTQueryWithOutput` inner queries (like bare
SELECT) in parentheses when the EXPLAIN had trailing output options. This broke
`EXPLAIN SYNTAX SELECT ... FORMAT TSVRaw` by formatting it as
`EXPLAIN SYNTAX (SELECT ...) FORMAT TSVRaw`.

Only `CREATE TABLE ... AS SELECT` needs the parenthesization (handled by
`ASTCreateQuery::formatQueryImpl` via `frame.parent_has_trailing_settings`).
Plain SELECT inside EXPLAIN does not consume FORMAT/SETTINGS, so no wrapping
is needed — the EXPLAIN parser correctly handles these at its own level.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Parsers/ASTExplainQuery.h Outdated
/// the AS-select, preventing the trailing options from being consumed by the inner
/// SELECT during re-parsing.
if (hasOutputOptions())
frame.parent_has_trailing_settings = true;

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.

frame.parent_has_trailing_settings is also consumed by ASTSelectWithUnionQuery to parenthesize inner SELECT when trailing SETTINGS are expected. Setting it here for any output option (FORMAT/INTO OUTFILE too) reintroduces incorrect formatting like EXPLAIN SYNTAX (SELECT 1) FORMAT TSVRaw.

This flag should stay settings-specific, while CREATE TABLE ... AS SELECT needs a separate signal for non-SETTINGS output options. Otherwise plain EXPLAIN ... SELECT ... FORMAT ... round-trip can regress.

alexey-milovidov and others added 2 commits March 17, 2026 11:25
…arent_has_trailing_settings`

The previous commits incorrectly reused `parent_has_trailing_settings` to
propagate the output-options signal from EXPLAIN to inner queries. This
caused `ASTSelectWithUnionQuery` to parenthesize plain SELECTs when only
FORMAT or INTO OUTFILE (not SETTINGS) was present, breaking the
`02882_formatQuery` test with output like `EXPLAIN SYNTAX (SELECT 1) FORMAT TSVRaw`.

The fix introduces a dedicated `has_trailing_output_options` flag in
`FormatStateStacked`, set by `ASTQueryWithOutput::formatImpl` when any
output option exists. This flag is checked by:
- `ASTCreateQuery::formatQueryImpl` to parenthesize AS-select
- `ASTExplainQuery::formatQueryImpl` to wrap non-`ASTQueryWithOutput`
  inner queries in parentheses

The existing `parent_has_trailing_settings` remains settings-specific and
continues to be used by `ASTSelectWithUnionQuery`.

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

clickhouse-gh Bot commented Mar 18, 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.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 89.29% (25/28, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov merged commit e33fd8c into master Mar 18, 2026
159 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-ast-formatting-create-as-select-settings branch March 18, 2026 13:29
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 18, 2026
@george-larionov george-larionov added the post-approved Approved, but after the PR is merged. label Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-approved Approved, but after the PR is merged. 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.

3 participants