Skip to content

Fix FORMAT clause not applying to EXPLAIN INSERT SELECT output - #101772

Merged
alexey-milovidov merged 10 commits into
ClickHouse:masterfrom
Onyx2406:fix/explain-insert-select-format
Jun 6, 2026
Merged

alexey-milovidov merged 10 commits into
ClickHouse:masterfrom
Onyx2406:fix/explain-insert-select-format

Conversation

@Onyx2406

@Onyx2406 Onyx2406 commented Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

EXPLAIN INSERT INTO t SELECT 1 FORMAT JSON incorrectly applied FORMAT to the INSERT rather than the EXPLAIN output. The parser now detects when a single FORMAT follows INSERT...SELECT inside EXPLAIN and rewinds it so it applies to the EXPLAIN output.

Fixes #67321

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry:

Fixed FORMAT clause being consumed by INSERT instead of applying to EXPLAIN output in EXPLAIN INSERT INTO ... SELECT ... FORMAT ....

  • Documentation is written (no docs needed)

Version info

  • Merged into: 26.6.1.466

Onyx2406 added 5 commits April 4, 2026 17:08
`ParserInsertQuery` greedily consumed the FORMAT clause after a SELECT
subquery, leaving nothing for `ParserQueryWithOutput` to apply to the
EXPLAIN output. Now, when a single FORMAT follows an INSERT...SELECT
inside EXPLAIN, the FORMAT is rewound so it applies to the EXPLAIN
output instead.

The double-FORMAT form (`EXPLAIN INSERT INTO t SELECT 1 FORMAT CSV
FORMAT JSON`) continues to work: first FORMAT goes to INSERT, second
to EXPLAIN.

Fixes ClickHouse#67321
Rename `04062_explain_insert_format` to `04077_explain_insert_format` to
avoid collision with existing tests on main. Remove unrelated
`04076_union_all_null_type_mismatch` files that don't belong in this PR.
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 31, 2026
@alexey-milovidov alexey-milovidov self-assigned this May 31, 2026
alexey-milovidov and others added 2 commits June 3, 2026 08:55
The 04079_ prefix now collides with four tests on master
(04079_crosstab_window_state_variant_cast, 04079_iceberg_write_retry_explicit_metadata,
04079_memory_engine_table_virtual_column, 04079_pr_view_sql_security_none,
04079_s3_read_request_histogram_metrics). 04309 is free both locally and on master.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

Merged the latest master (the branch was ~2 months / 14733 commits behind) — clean merge, zero conflicts, net diff unchanged (ParserExplainQuery.cpp +24, test +21).

Renumbered the test 04079_explain_insert_format → 04309_explain_insert_format: the 04079_ prefix now collides with four tests on master (04079_crosstab_window_state_variant_cast, 04079_iceberg_write_retry_explicit_metadata, 04079_memory_engine_table_virtual_column, 04079_pr_view_sql_security_none, 04079_s3_read_request_histogram_metrics). 04309 is free both locally and on master.

Rebuilt and verified the fix against the fresh binary:

  • EXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 FORMAT JSONEachRow → {"explain":"INSERT INTO FUNCTION null('x UInt64') SELECT 1"} (FORMAT now applies to the EXPLAIN output as JSON; the inner INSERT no longer carries the FORMAT).
  • The double-FORMAT form keeps FORMAT CSV on the INSERT and applies FORMAT JSONEachRow to the EXPLAIN output.

Both match the reference. No unresolved review threads.

@clickhouse-gh

clickhouse-gh Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [feeb81f]

Summary: ✅


AI Review

Summary

This PR fixes FORMAT being consumed by the inner INSERT ... SELECT instead of applying to the EXPLAIN output. The direction is right and the FROM INFILE/input regression from the earlier version is fixed, but the current guard still leaves supported forms where the single trailing FORMAT is not rewound to ParserQueryWithOutput.

Findings

⚠️ Majors

  • [src/Parsers/ParserExplainQuery.cpp:148] The insert_query->format != "Values" special case violates the same output-format contract for the valid Values output format. With EXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 FORMAT Values, ParserInsertQuery stores Values in insert_query->format, the guard skips the rewind, and the outer ParserQueryWithOutput never sees FORMAT Values. Remove this format-name check, or distinguish the actual INSERT ... VALUES data path from a trailing FORMAT Values after SELECT.
  • [src/Parsers/ParserExplainQuery.cpp:149] Using settings_ast as a blanket guard still skips the rewind when settings come from the inner SELECT or from INSERT ... SETTINGS ... SELECT, even though the trailing FORMAT should still belong to the EXPLAIN output. For example, EXPLAIN SYNTAX INSERT INTO FUNCTION null('x UInt64') SELECT 1 SETTINGS max_threads=1 FORMAT JSONEachRow leaves FORMAT JSONEachRow on the inner INSERT because InsertQuerySettingsPushDownVisitor populates settings_ast. Track whether the settings were actually parsed after the insert FORMAT, or remember the token position of the consumed FORMAT and rewind only that clause while preserving insert/select settings.
Tests

⚠️ Please add focused stateless coverage for the remaining parser boundaries: INSERT ... SELECT ... SETTINGS ... FORMAT JSONEachRow, INSERT ... SETTINGS ... SELECT ... FORMAT JSONEachRow, and INSERT ... SELECT ... FORMAT Values. The current test proves the basic case, double-FORMAT, and input-data exceptions, but not these still-broken branches.

Final Verdict

Status: ⚠️ Request changes

Minimum required actions: make the rewind decision independent of unrelated settings_ast sources, handle FORMAT Values as an EXPLAIN output format for INSERT ... SELECT, and add focused regression tests for those forms.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 3, 2026
Comment thread src/Parsers/ParserExplainQuery.cpp Resolved
The previous change rewound the trailing `FORMAT` from the inner
`INSERT ... SELECT` so it applies to the `EXPLAIN` output. But that also
rewound the `FORMAT` of `INSERT ... FROM INFILE ... SELECT ... FORMAT ...`
and `INSERT ... SELECT ... FROM input(...) ... FORMAT ...`, where the
`FORMAT` describes the insert's input data and must be preserved.

This broke the existing stateless test `02165_insert_from_infile`
(`EXPLAIN SYNTAX INSERT INTO test FROM INFILE 'data.file' SELECT x FROM
input('x UInt32') FORMAT TSV` lost its `FORMAT TSV`).

Now the rewind is skipped when the `INSERT` reads input data, i.e. when
`infile` is set or the `SELECT` contains an `input` table function.
Extended `04309_explain_insert_format` to cover both cases.

CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101772&sha=cb99c6cd9d553135eb425f9afa34d99f2e7899cf&name_0=PR&name_1=Fast%20test

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

Fixed the CI failure (54cf3a4f128).

The Fast test failure on 02165_insert_from_infile (report) was caused by this PR: the FORMAT rewind in ParserExplainQuery.cpp also stripped the FORMAT of INSERT ... FROM INFILE ... SELECT ... FORMAT ... and INSERT ... SELECT x FROM input(...) FORMAT ..., where the FORMAT describes the insert's input data and must stay on the INSERT (exactly as the clickhouse-gh bot predicted).

The rewind is now skipped when the INSERT consumes input data — when infile is set or the SELECT contains an input table function. The original issue (#67321, plain EXPLAIN INSERT ... SELECT ... FORMAT) is unaffected.

Verified locally against a fresh build:

  • 02165_insert_from_infile — [ OK ]
  • 04309_explain_insert_format — [ OK ] (extended with input() and FROM INFILE cases)

All build jobs were already green; 02165 was the only failing test.

Comment thread src/Parsers/ParserExplainQuery.cpp Resolved
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
Comment thread src/Parsers/ParserExplainQuery.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.40% -0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 21/21 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 6, 2026
Merged via the queue into ClickHouse:master with commit cf5f710 Jun 6, 2026
166 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 7, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Both remaining review comments (the over-broad !settings_ast guard skipping SETTINGS-before-FORMAT, and the format != "Values" special case excluding a valid Values output format) are addressed in the follow-up #106686. The rewind there verifies the two trailing tokens are exactly FORMAT <name> instead of guarding on settings_ast, which keeps both cases working while still leaving the FORMAT on the INSERT for the SETTINGS-after-FORMAT (allow_settings_after_format_in_insert), FROM INFILE, input, and double-FORMAT forms.

pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Jun 20, 2026
Follow-up to ClickHouse#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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default 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.

FORMAT does not apply to EXPLAIN INSERT SELECT in the obvious way

4 participants