Repository navigation
Fix inconsistent AST formatting for CREATE TABLE AS SELECT with trailing output options - #99545
Merged
alexey-milovidov merged 8 commits intoMar 18, 2026
Merged
Conversation
…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]>
Contributor
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
…`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]>
| /// 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; |
Contributor
There was a problem hiding this comment.
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.
…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]>
Contributor
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 89.29% (25/28, 0 noise lines excluded) |
alexey-milovidov
deleted the
fix-ast-formatting-create-as-select-settings
branch
March 18, 2026 13:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_optionsflag through the format state stack.ASTQueryWithOutput::formatImplsets it when output options are present, andASTCreateQuery::formatQueryImpluses it (in addition to its ownsettings_ast) to decide whether to wrap the AS-select in parens.ASTExplainQuery::formatQueryImplalso wraps non-ASTQueryWithOutputinner 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):
Version info
26.3.1.802