Repository navigation
Fix FORMAT not applying to EXPLAIN INSERT SELECT output with SETTINGS or Values - #106686
Conversation
Follow-up to #101772. The FORMAT rewind that makes `FORMAT` apply to the `EXPLAIN` output of `EXPLAIN ... INSERT ... SELECT ... FORMAT ...` was guarded by `!insert_query->settings_ast` and `insert_query->format != "Values"`. Both guards were too broad: - `!settings_ast` also skipped the rewind for `SETTINGS` written before the `FORMAT` (and for settings pushed down from the inner `SELECT` by `InsertQuerySettingsPushDownVisitor`), so `EXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 SETTINGS max_threads = 1 FORMAT JSONEachRow` left `FORMAT JSONEachRow` on the `INSERT` instead of applying it to the `EXPLAIN` output. - `format != "Values"` excluded the valid `Values` output format even though `insert_query->select` already rules out the `INSERT ... VALUES` data path, so `EXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 FORMAT Values` never applied `FORMAT Values` to the `EXPLAIN` output. The original `settings_ast` guard existed because the blind `--pos; --pos;` rewind is only correct when the `FORMAT` clause is the last thing the `INSERT` parser consumed, which is not true when `SETTINGS` follow the `FORMAT` (`allow_settings_after_format_in_insert`). Instead of guarding on `settings_ast`, verify that the two trailing tokens are exactly `FORMAT <name>` before rewinding. This keeps `SETTINGS`/`FORMAT`-after-`SETTINGS` and `FORMAT Values` working while still leaving the `FORMAT` on the `INSERT` for the `SETTINGS`-after-`FORMAT`, `FROM INFILE`, `input`, and explicit double-`FORMAT` forms. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [8db77b4] Summary: ✅
AI ReviewSummaryThis PR fixes Missing context / blind spots
Final VerdictStatus: ✅ Approve |
…format-settings-values
…hout input()
`ParserInsertQuery` extracted inline insert data following the `FORMAT` clause
whenever a format was present and the data did not come `FROM INFILE`. But an
`INSERT ... SELECT` has no inline data -- the rows come from the `SELECT` -- unless
the `SELECT` reads them through the `input` table function. For a plain
`INSERT ... SELECT ... FORMAT <name>` the parser still looked for inline data and
raised `You have excessive ';' symbol before data for INSERT.` on the common
semicolon-terminated form, e.g.
EXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 FORMAT Values;
Guard the inline-data extraction with a check that the `SELECT` actually uses the
`input` table function. The `input` and inline-data (`VALUES`, `FORMAT <name>`
without a `SELECT`) paths are unchanged and still validate the trailing data.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… roundtrip
The previous follow-up made the FORMAT-to-EXPLAIN-output rewind depend on whether
the two trailing tokens the INSERT consumed were exactly `FORMAT <name>`. That is
not stable across a formatting roundtrip: `ASTInsertQuery::formatImpl` always emits
`SETTINGS` before the `SELECT` and the INSERT `FORMAT` last, so a query written with
`SETTINGS` after the `FORMAT` (allow_settings_after_format_in_insert) re-parses into
the `SETTINGS`-before form, flipping a position-dependent decision and tripping the
`Inconsistent AST formatting` assertion found by the AST fuzzer for
EXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 FORMAT JSONEachRow SETTINGS max_threads = 1
Decide based only on the INSERT shape (a `SELECT` source, no `FROM INFILE`, no
`input` table function, no explicit second `FORMAT`), which is invariant under
formatting, and move the `FORMAT` to the EXPLAIN output by setting the output format
on the EXPLAIN query directly instead of rewinding `pos`. The `pos` rewind only
worked when `FORMAT <name>` was the last consumed token; setting the output format
directly is correct regardless of where `SETTINGS` are written.
The test now also covers the SETTINGS-after-FORMAT, double-FORMAT, trailing-semicolon
and roundtrip-stability cases.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Fgrtue
left a comment
There was a problem hiding this comment.
The implementation looks good. Added two nits and one suggestion about the test.
Address the review: the moved output `FORMAT` of `EXPLAIN ... INSERT ... SELECT ... FORMAT ...` was added to the children of `ASTExplainQuery` before `setExplainedQuery`, so the children were `[format, insert]` while `ASTExplainQuery::clone` cloned only `children[0]` as the explained query: clones contained the format node twice and aliased the original explained query. Attach the moved `FORMAT` through `IAST::set` after `setExplainedQuery`, and make `ASTExplainQuery::clone` clone the named children (`query`, `ast_settings`, `table_function`, `table_override`) explicitly instead of assuming their order. The order assumption was already wrong before this PR for `EXPLAIN SETTINGS ... SELECT ...`, where the EXPLAIN settings are the first child and the clone aliased the original explained query. Also remove redundant comments pointed out in the review. Co-Authored-By: Claude Fable 5 <[email protected]>
Address the review: the test only checked the output format wrapper (e.g. the leading
`{"explain"` of `JSONEachRow`) but not the body of the `EXPLAIN` output. Print the full
`EXPLAIN SYNTAX` output for every case, so the reference also verifies that the `FORMAT`
clause is gone from the `INSERT` body (or kept on it for the double-`FORMAT` form).
Co-Authored-By: Claude Fable 5 <[email protected]>
|
Addressed all review comments:
Verified locally: rebuilt and ran The only red check, BuzzHouse (amd_debug), is the known issue (exact STID match) — the fuzzed query is a plain |
Address review: when `EXPLAIN INSERT ... SELECT ... FORMAT <fmt>` is followed by `INTO OUTFILE`, `ParserExplainQuery` attaches the moved `FORMAT` child to the explained query before `ParserQueryWithOutput` parses `INTO OUTFILE`, so the children end up ordered `[query, format, out_file]`. A re-parse of the formatted query (the formatter always emits `INTO OUTFILE` before `FORMAT`) yields `[query, out_file, format]`, so `out_ast->getTreeHash` differs from the re-parsed AST and the debug-only "Inconsistent AST formatting" check trips. Generalize the previous `FORMAT`/`SETTINGS` swap into a full canonicalization of the output-option children (`INTO OUTFILE`, `COMPRESSION`, `LEVEL`, `FORMAT`, `SETTINGS`) into the fixed order used by `ASTQueryWithOutput::formatImpl`, so the tree hash is stable across a formatting roundtrip regardless of the original clause order or of which parser attached the child. Extend `04321_explain_insert_format_settings_values` with the `INTO OUTFILE` case, which executes the query so the `#ifndef NDEBUG` consistency check runs. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The previous commit canonicalized the output-option child order (`INTO OUTFILE`, `COMPRESSION`, `FORMAT`, `SETTINGS`) only in the parser, to match the order `ASTQueryWithOutput::formatImpl` writes them. But `ASTQueryWithOutput::cloneOutputOptions` still appended children in a different order (`out_file`, `format_ast`, `settings_ast`, `compression`, `compression_level`). So a clone of a query that has both an outfile `COMPRESSION` and a trailing `FORMAT`/`SETTINGS`, e.g. `SELECT 1 INTO OUTFILE 'x' COMPRESSION 'gz' FORMAT JSONEachRow`, produced a different child order — and therefore a different `getTreeHash` — than a freshly parsed or roundtripped AST. Make the canonical order a single source of truth, `output_option_members`, and use it from both the parser canonicalization and `cloneOutputOptions`, with a comment tying it to `formatImpl`. Add a focused gtest asserting that the parsed AST, its clone, and a format+reparse roundtrip all share the same tree hash for `INTO OUTFILE ... COMPRESSION ... FORMAT`/`SETTINGS` (and the bare `FORMAT`/`SETTINGS`) cases. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ParserExplainQuery` parses the EXPLAIN-level settings before the explained query, so `EXPLAIN header = 1 SELECT 1` is parsed with `children = [ast_settings, query]`. The clone re-added `query` before `ast_settings`, giving the clone a different child order and therefore a different `getTreeHash` than a freshly parsed AST (the hash includes children in order). Re-add `ast_settings` before `query` in `clone` so that the clone matches the parser order. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ASTExecuteAsQuery` also derives from `ASTQueryWithOutput`, but it is parsed outside `ParserQueryWithOutput`: `ParserExecuteAsQuery` hoists the output options (`INTO OUTFILE`, `COMPRESSION`, `FORMAT`, `SETTINGS`) from the subquery to the outer query. It appended them to `children` before the subquery and in the parsing order (`out_file`, `format_ast`, `compression`, ...), while `ASTExecuteAsQuery::clone` appends the subquery first and then the output options in the canonical `output_option_members` order. A parsed `EXECUTE AS u SELECT 1 INTO OUTFILE 'x' COMPRESSION 'gz' FORMAT JSONEachRow` and its clone therefore had a different child order and a different `getTreeHash`. Assign the hoisted fields without appending to `children`, then append the subquery and the output options through `output_option_members`, so the parsed AST matches its clone. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ility Extend `gtest_Parser` with two cases asserting that the clone and the format+reparse roundtrip both reproduce the same `getTreeHash` as a freshly parsed AST, for `EXPLAIN` with EXPLAIN-level settings and for `EXECUTE AS` with output options. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…format-settings-values
…comment The EXPLAIN-level settings are written inline without the `SETTINGS` keyword (e.g. `EXPLAIN header = 1 SELECT 1`); the `EXPLAIN SETTINGS ...` form is a syntax error. Fix the example in the comment accordingly. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI Review Request changes verdict (both Changes
Prior CI reds (on
|
|
@alexey-milovidov Investigated. Every CIDB instance of this STID, including the one on this PR (report) and on master (report), is Verified locally on a debug build, both directions:
So #106686 was not the cause, and master is fixed as of (session cron:clickhouse-worker-slot-0:20260619-073500) |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 122/126 (96.83%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Make
FORMATapply to theEXPLAINoutput ofEXPLAIN ... INSERT ... SELECT ... FORMAT ...also whenSETTINGSprecede theFORMATor the output format isValues. Follow-up to #101772.Documentation entry for user-facing changes
Follow-up to #101772, addressing two unresolved review comments left by the review bot after merge.
EXPLAIN ... INSERT ... SELECT ... FORMAT ...rewinds the trailingFORMATso it applies to theEXPLAINoutput instead of being consumed by the innerINSERT. The rewind was guarded by!insert_query->settings_astandinsert_query->format != "Values", and both guards were too broad:!settings_astalso skipped the rewind forSETTINGSwritten before theFORMAT(and for settings pushed down from the innerSELECT), soEXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 SETTINGS max_threads = 1 FORMAT JSONEachRowleftFORMAT JSONEachRowon theINSERT.format != "Values"excluded the validValuesoutput format, even thoughinsert_query->selectalready rules out theINSERT ... VALUESdata path, soEXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 FORMAT Valuesnever appliedFORMAT Valuesto theEXPLAINoutput.The
settings_astguard existed because the blind--pos; --pos;rewind is only correct when theFORMATclause is the last thing theINSERTparser consumed — which is not true whenSETTINGSfollow theFORMAT(allow_settings_after_format_in_insert). Instead of guarding onsettings_ast, the rewind now verifies that the two trailing tokens are exactlyFORMAT <name>. This keeps theSETTINGS-before-FORMATandFORMAT Valuescases working while still leaving theFORMATon theINSERTfor theSETTINGS-after-FORMAT,FROM INFILE,input, and explicit double-FORMATforms.Added stateless test
04321_explain_insert_format_settings_values.Related: #101772
Related: #67321
Version info
26.6.1.1078