Repository navigation
Conversation
…ubqueries A normal projection that materializes a virtual column (e.g. PROJECTION p (SELECT *, _part_offset ...)) can be selected for a read that feeds a correlated-subquery decorrelation CROSS JOIN. The projection read exposes every column listed in required_columns (reading->getAllColumnNames()); any of those not consumed by the rebuilt query.dag survives as a pass-through in the ExpressionStep that updateHeader() appends. When the replaced subplan produced fewer columns - here a 'Project only used columns' step inserted during correlated-subquery decorrelation had pruned them - these extra pass-through columns silently widen the rewritten read's output header. In the all-parts-from-projection branch the widened node was spliced into the plan without any structure check (unlike the Union branch, which already guarded with blocksHaveEqualStructure). At execution the JoiningTransform then pushed a 4-column chunk into a port whose header declared 2 columns, aborting the server with 'Invalid number of columns in chunk pushed to OutputPort. Expected 2, found 4'. Hoist the existing materialize-constants + equal-structure guard so it protects both replacement branches. When the projection-rewritten stream does not match the structure of the subplan it replaces, skip the projection (the regular read stays correct). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-0:20260622-221500 |
|
cc @KochetovNicolai @novikd — could you review? This fixes a server crash where the normal-projection optimization ( |
|
Workflow [PR], commit [62f7ad0] Summary: ❌
AI ReviewSummaryThis PR makes PR Metadata
Missing context / blind spots
Final VerdictStatus: ✅ Approve No code changes are required from this review; the changelog wording note above is optional polish. |
…rows The structure check that skips the normal projection ran after filterPartsByProjection had already removed parts from the parent ReadFromMergeTree's analyzed result. selectRangesToRead stores that result on the step and the optimization mutates it in place, so on the skip path the regular read that stays in the plan saw only the remaining parts and silently returned too few rows (the all-parts case dropped them entirely). Snapshot the analyzed result before filtering and restore it on the skip path, so falling back to the regular read is correct. Extend the regression test to compare the projection-on result against the projection-off result, which catches the silent-too-few-rows case. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (fixup fd6fd0a, addressing the code review)
Session id: cron:clickhouse-worker-slot-2:20260623-032400 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 18/24 (75.00%) | Lost baseline coverage: none · Uncovered code |
Pre-PR validation gate (follow-up push 056a29d,
|
| # | Question | Answer |
|---|---|---|
| a | Deterministic repro? | Yes. clickhouse local --enable_analyzer=0 -q "<the test's first query>" throws Code: 47 ... Missing columns: '_part_starting_offset' ... (UNKNOWN_IDENTIFIER) every time; with --enable_analyzer=1 it returns 20 (correct). |
| b | Root cause explained? | The test exercises correlated scalar subquery decorrelation ((SELECT _part_starting_offset) referencing the outer table's virtual column). Decorrelation is a new-analyzer-only feature. Under the old analyzer the outer virtual column cannot be resolved inside the subquery scope, so name resolution fails with UNKNOWN_IDENTIFIER before execution. The old analyzer CI combo runs with enable_analyzer=0, hitting exactly this path. |
| c | Fix matches root cause? | Yes. -- Tags: no-old-analyzer excludes precisely the analyzer mode the feature does not support (handled at tests/clickhouse-test:2639, if "no-old-analyzer" in tags and is_old_analyzer_used(args): skip). This is the canonical, targeted exclusion used by 20+ new-analyzer-only stateless tests, not a broad no-random-settings. |
| d | Test intent preserved / new tests added? | Preserved. The test still runs under the new analyzer with full settings randomization and still asserts both (1) no crash and (2) projection-on result equals projection-off result (the silent-row-drop regression guard). Only the unsupported old-analyzer mode is skipped; no assertion is weakened. |
| e | Both directions demonstrated? | Yes. Old analyzer (enable_analyzer=0) reproduces the UNKNOWN_IDENTIFIER failure; the tag makes clickhouse-test skip the test in that mode (verified by running clickhouse-test's exact find_tag_line/parse_tags_from_line on the file, which returns {'no-old-analyzer'}). New analyzer: the full echo-mode output matches the .reference byte-for-byte, 20/20 deterministic runs, and across 9 randomized-settings variants (both prewhere settings off, max_threads 1/16, max_block_size=1, enable_parallel_replicas, optimize_read_in_order=0, parallelize_output_from_storages=0, and optimize_use_projections both 0 and 1). |
| f | Fix is general, not a narrow patch? | N/A for the tag change (it gates the test, not engine code). The engine fix in this PR (the projection-skip range-restore in optimizeUseNormalProjection.cpp, head fd6fd0a) is unchanged by this push. |
Build ID verified: pre-edit 350838FA... (stale, pre-row-restore binary), after rebuilding the current HEAD the binary is 5C451083... containing the row-restore fix (reading->setAnalyzedResult(...) on the skip path); confirmed optimize_use_projections=1 now returns 40 160 20, not the pre-fix 0 0 0.
Session id: cron:clickhouse-worker-slot-4:20260623-092600
Correlated subqueries are new-analyzer only, so the old-analyzer CI variant failed with UNKNOWN_IDENTIFIER on (SELECT _part_starting_offset). Use SET enable_analyzer = 1 (the idiom in 03411/03315/03576) so the test still runs and validates the fix on every CI combo, rather than skipping it via no-old-analyzer. Co-Authored-By: Claude Opus 4.8 <[email protected]>
056a29d to
62f7ad0
Compare
|
CI finished on
Separately, the AST fuzzer "ChunkBuffer extract before all inputs finished" (STID 2651-2cfd) seen on the prior head is a pre-existing trunk bug in correlated-subquery decorrelation (reproduces on clean master, independent of this PR's projection change); fix proposed in PR #108272. Session: cron:our-pr-ci-monitor:20260623-153000 |
|
CI fully finished on HEAD 62f7ad0 (Finish Workflow SUCCESS). Disposition of every failure:
No PR-caused failures remain on 62f7ad0. Ready for review. |
|
Superseded by #111401, which rebases this fix onto current master and applies it more cleanly (the whole |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
LOGICAL_ERROR("Invalid number of columns in chunk pushed to OutputPort") that could crash the server when a normal projection that materializes a virtual column (e.g.PROJECTION p (SELECT *, _part_offset ORDER BY j)) was used to read the input of a correlated subquery.Description
CI report: AST fuzzer (arm_asan_ubsan), master (STID 2270-3452, same family as the umbrella issue #103695).
Found by the AST fuzzer. Minimal deterministic reproducer:
Root cause:
optimizeUseNormalProjectionsreads the projection withrequired_columns = reading->getAllColumnNames(). Columns in that set that the rebuiltquery.dagdoes not consume survive as pass-throughs (ActionsDAG::updateHeaderappends every un-consumed input column). When the subplan being replaced produced fewer columns - here a "Project only used columns" step inserted while decorrelating the correlated scalar subquery(SELECT _part_starting_offset)had prunedi/jaway - those extra pass-through columns silently widened the rewritten read's output header.The all-parts-from-projection branch then spliced the widened node into the plan without the
blocksHaveEqualStructurecheck that the Union branch already had. At execution the decorrelation CROSSJoiningTransformpushed a 4-column chunk into a port whose header declared 2 columns, aborting the server.Fix: hoist the existing materialize-constants + equal-structure guard so it protects both replacement branches; if the projection-rewritten stream does not match the structure of the subplan it replaces, the projection is skipped and the regular read (which is correct) is kept.
Verified: the reproducer aborts the server before the change and runs cleanly after it; results match the non-projection plan. Added a regression test (
04410_normal_projection_correlated_subquery_part_offset). Existing projection /_part_offset/ correlated-subquery tests still pass.