Repository navigation
Conversation
Pre-PR validation gate (self-assessment)
Session: |
|
cc @KochetovNicolai @alesapin — could you review? The fix pins all summing columns for SummingMergeTree FINAL the same way the existing code pins sign/version/is_deleted for the other merge engines, so SummingSortedAlgorithm sees the full row state and stops dropping rows whose pruned-out columns are non-zero. |
|
Workflow [PR], commit [58aa91e] Summary: ❌
|
add80f5 to
f557421
Compare
|
Thanks for the review. Both concerns were real correctness gaps in the previous revision and are now fixed. I rebased onto current master ( 1. Nested table name in 2. Materialized physical summing columns. Confirmed: the merge header is the table sample block ( Both paths ( Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-22:20260610-184200 |
f557421 to
f01b49f
Compare
|
Addressed the over-broad pinning in The implicit-mode helper no longer pins every physical column. It now pins a non-key physical column only if Verified: the issue reproducer returns all 6 rows; den-crane's no-WHERE reproducer still correctly drops |
f01b49f to
5e675ed
Compare
|
Thanks for the two follow-up reviews. They pull in opposite directions, and you are right on both — the previous revision split A column is kept iff
Everything else ( This fixes both notes at once:
New regression cases added to Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-19:20260610-235100 |
|
CI finished on `5e675eddba54`. All failures are the same infrastructure outage, not caused by this PR (SummingMergeTree FINAL column-pruning fix):
The materialized-physical-summing-column gap flagged by the bot is addressed on this HEAD (selection now mirrors SummingSortedAlgorithm via getAllPhysical, with a materialized-column regression test). Ready for review. |
|
@groeneai check CI |
5e675ed to
74ff199
Compare
|
@fm4v CI was green on the fix itself. On the prior head Root cause was a stale base, not this change. The head was committed 2026-06-11 00:21Z, before #107122 (revert of "Multi-stage distributed queries" #106020) merged at 01:37Z. That revert fixed exactly this ASan heap corruption on master. Fix: rebased onto current master (now includes #107122) and force-pushed. New head |
74ff199 to
ae1795f
Compare
|
Addressed the 2026-06-12 review on Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-3:20260612-121000 |
ae1795f to
b27d516
Compare
|
Addressed the 2026-06-12 review on A note on the over-pin: I could not produce a stable runtime signal for it. On the bot's exact shape ( Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-3:20260612-142100 |
|
CI finished on
|
|
CI finished on HEAD
All other checks pass, including Bugfix validation (functional tests) which independently confirms the fix reproduces the original bug. Ready for re-review. |
|
Dear @shankar-iyer, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
b27d516 to
61d98ee
Compare
|
|
||
| /// Keep all of SummingMergeTree's summing columns under FINAL so on-the-fly merging matches a real | ||
| /// merge's zero-row rule even when column pruning narrows the read set (see helper above, #106125). | ||
| if (data.merging_params.mode == MergeTreeData::MergingParams::Summing) |
There was a problem hiding this comment.
getSummingColumnsRequiredForFinal fixes the storage read set, but the PREWHERE-before-FINAL path still only knows about sorting/sign/version columns. StorageInMemoryMetadata::getColumnsRequiredForFinal() is still just getColumnsRequiredForSortingKey() (src/Storages/StorageInMemoryMetadata.h:267), both ExpressionAnalyzer and PlannerJoinTree use that stale helper to decide which columns must survive PREWHERE (src/Interpreters/ExpressionAnalyzer.cpp:2081, src/Planner/PlannerJoinTree.cpp:586), and spreadMarkRangesAmongStreamsFinal restores only sorting/sign/version/is_deleted inputs (src/Processors/QueryPlan/ReadFromMergeTree.cpp:1988). That means SELECT c0 FROM s FINAL PREWHERE v_zero >= 0 ORDER BY c0 (or WHERE with optimize_move_to_prewhere_if_final = 1) can still read v_keep here and then strip it before SummingSortedTransform, reproducing the same dropped-row bug. Please thread the same Summing-specific required-column set through the generic FINAL/PREWHERE plumbing and add a focused regression for that path.
There was a problem hiding this comment.
The PREWHERE fix still misses subcolumn carriers. getSummingColumnsRequiredForFinal returns the physical summing column name (for the tuple case, metrics), but restorePrewhereInputs only re-adds outputs whose input->result_name matches exactly. PREWHERE on tuple fields is built on the subcolumn name instead (t.a is the input in tests/queries/0_stateless/03445_subcolumns_prewhere_pushdown.reference), so this path still does not preserve the physical tuple before SummingSortedTransform.
That means the original dropped-row bug is still reachable with a supported carrier such as:
CREATE TABLE s (k UInt64, metrics Tuple(a Int64, b Int64))
ENGINE = SummingMergeTree ORDER BY k
SETTINGS allow_tuple_element_aggregation = 1;
INSERT INTO s VALUES (0, (0, 1)), (1, (5, 5));
INSERT INTO s VALUES (0, (0, 0)), (2, (7, 7));
SELECT k FROM s FINAL PREWHERE metrics.a >= 0 ORDER BY k;
metrics.b can still be stripped before the merge, so FINAL sees only metrics.a and can drop k = 0 again. The restore step needs to work by storage column name, not only by exact DAG input name, and the regression test should cover FINAL PREWHERE on tuple/map subcolumns.
There was a problem hiding this comment.
Both of your mechanism claims are correct, but I could not reproduce the dropped row, so I am not changing the restore key. Details, all measured on this head (Build ID dd9fb5438b2c961ab7cc3614ce99aa288a9e0989).
Your two cited facts hold. restoreDAGInputs does match by exact input->result_name (ReadFromMergeTree.cpp:182-196), and subcolumn PREWHERE really is built on the subcolumn name. EXPLAIN actions=1 for your exact table gives:
Prewhere filter column: greaterOrEquals(metrics.a, 0_UInt8) (removed)
Actions: INPUT : 0 -> metrics.a Int64 : 0
so the DAG's only input is metrics.a and the physical metrics never matches the name I insert.
Your SQL does not drop the row. Running it verbatim against an OPTIMIZE TABLE ... FINAL oracle:
| arm | result |
|---|---|
SELECT k FROM s FINAL PREWHERE metrics.a >= 0 ORDER BY k |
0, 1, 2 |
| oracle | 0, 1, 2 |
Why: ActionsDAG::updateHeader only strips what it consumed. ActionsDAG.cpp:1372-1425 removes the header positions it took as DAG inputs and then passes everything else through:
for (size_t i = 0; i < header.columns(); i++)
if (!pos_to_remove.contains(i))
res.insert(header.data[i]);A subcolumn PREWHERE consumes metrics.a, so the physical metrics is never in pos_to_remove and survives to SummingSortedTransform untouched. The exact-name restore exists to rescue a column the DAG does consume by name and would otherwise drop from its outputs. The two shapes are disjoint: metrics in the PREWHERE needs the restore, metrics.a in the PREWHERE never needed it. EXPLAIN header=1, actions=1 confirms the physical tuple is still there after the filter.
Carrier set. k = 0 sums to zero in the referenced leaf and non-zero in a sibling leaf, so a stripped column would drop it. Each arm against its own optimize_move_to_prewhere = 0 oracle, all 0,1,2 on both sides: tuple subcolumn; the same with WHERE + optimize_move_to_prewhere_if_final = 1; enable_analyzer = 0; Nullable leaf via IS NOT NULL (.null); Map subcolumn (m.keys); Nested subcolumn (n.a); LowCardinality beside the tuple; JSON subcolumn (j.a::Int64). Also clean: mixed subcolumn plus physical in one PREWHERE, tupleElement(metrics,'a'), allow_reorder_prewhere_conditions = 1, optimize_read_in_order = 0, do_not_merge_across_partitions_select_final = 1, and an explicit SummingMergeTree((metrics)) columns_to_sum. allow_tuple_element_aggregation = 1 is required for the leaves to be summed at all, but it does not open the gap.
Sibling site. You asked whether the same blindness affects the other pin, so I mutation-tested both. Neutering the read-set pin in spreadMarkRanges drops k = 0 in the WHERE-moved tuple arm. Neutering the restorePrewhereInputs pin, the seam you name, leaves every subcolumn arm unchanged but fails 04301 on the physical-column PREWHERE cases. So neither site is blind; they cover different shapes, and each is load-bearing for its own. Restoring the file rebuilt bit-identically.
I agree exact-name matching does not cover subcolumn-named inputs. I do not think it reopens this bug, since updateHeader never strips a column it did not consume. Switching to storage-column resolution would widen the restored set without changing any result in the arms above, so I have no failing test to justify it, and I would rather not add a change I cannot back with one. If you have a carrier that actually drops a row, post it and I will fix it.
61d98ee to
3d7ddd4
Compare
|
@clickhouse-gh Good catch, confirmed and fixed. The PREWHERE-before-FINAL path did still drop the row on the previous HEAD. Reproduced on the prior HEAD (build Root cause is exactly as you described: pinning the summing columns in Fix: restore the summing columns as PREWHERE outputs there too, alongside the existing sign/version/is_deleted restore, reusing the same The zero-row decision itself is unchanged: genuinely all-zero groups are still dropped (verified below). Pre-PR validation gate (click to expand)
Pushed as |
CI finish ledger — 3d7ddd4Every failure below has an owner (a fixing PR). Only
Session id: cron:our-pr-ci-monitor:20260708-170000 |
…n-zero summing columns Closes ClickHouse#106125 A SELECT ... FINAL on a SummingMergeTree table can silently drop a physically-present row when column pruning narrows the read set to a strict subset of the summing columns and the only summing column read is zero for that row. On-the-fly FINAL merging applies SummingSortedAlgorithm's "drop the group when all summing columns are zero" rule to the pruned subset, while a real on-disk merge sees the full set and keeps the row. The read set under FINAL now keeps every physical column that SummingSortedAlgorithm could sum, mirroring defineColumns' membership exactly so the on-the-fly path and a real merge agree: * implicit columns_to_sum: all summable non-key physical columns; * explicit columns_to_sum: a named scalar is kept, an unnamed scalar is pruned, and a *Map Nested member is kept regardless of columns_to_sum (map discovery ignores the scalar columns_to_sum filter); * materialized non-key columns are kept (the merge header is the table sample block, which includes them); * a non-summable scalar (e.g. String) is pruned (it never affects the zero-row decision), so no unnecessary disk reads; * with allow_tuple_element_aggregation, Tuple columns are flattened and their leaves summed, so a Tuple column is kept when any flattened leaf is summable (the whole physical column is read). The tuple recursion mirrors defineColumns' flattened-leaf gates: a *Map-suffixed array leaf is a summed-map candidate only when its synthesized parent is a real tuple ancestor and, with an explicit columns_to_sum, the leaf or an ancestor is named; otherwise the leaf is copied last-value and is not pinned (so an unlisted tuple *Map is not over-read). Both read sites (spreadMarkRanges and getColumnsRequiredForMergingFinal, which also feeds removeUnusedColumns) call one helper so they cannot diverge. PREWHERE-before-FINAL: pinning the summing columns in the read set is not enough on its own. PREWHERE column survival is decided by StorageInMemoryMetadata::getColumnsRequiredForFinal() (the sorting key only), so a PREWHERE touching only a subset of the summing columns strips the rest before SummingSortedTransform, reintroducing the dropped-row bug (e.g. SELECT c0 FROM s FINAL PREWHERE v_keep >= 0 ORDER BY c0 dropped the row whose v_keep is non-zero but v_zero is zero). spreadMarkRangesAmongStreamsFinal now restores the summing columns as PREWHERE outputs alongside the existing sign/version/is_deleted restore, so they survive PREWHERE and reach the transform. The zero-row decision itself is unchanged: genuinely all-zero groups are still dropped. Co-Authored-By: Claude Opus 4.8 <[email protected]>
3d7ddd4 to
31a56ee
Compare
CI finish ledger — 31a56eeCI fully finished on this head. Every failure below has an owner. The only genuine test failures are one chronic trunk LOGICAL_ERROR (2 stress shards); the remaining reds are cancelled/superseded jobs (no test-level failure).
Session id: cron:our-pr-ci-monitor:20260709-163000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 84/87 (96.55%) · Uncovered code |
CI finish ledger - 6a9bb65Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260729-093000 |
|
This PR is no longer needed. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
SELECT FINALon aSummingMergeTreetable silently dropping a physically-present row when column pruning narrowed the read set to a subset of the summing columns and the only visible summing column was zero for that row. The "drop the group's row when all summing columns are zero" rule is now evaluated over the full set of physical summing columns from the table metadata, matching real on-disk merge semantics.Documentation entry for user-facing changes
Description
SELECT FINALon aSummingMergeTreecould silently drop a physically-present row when column pruning narrowed the read set to a strict subset of the summing columns and the only visible summing column was0for that row. A real on-disk merge keeps the row because another (pruned) summing column has a non-zero value.Root cause:
SummingSortedAlgorithmapplies a "drop the group's row if all summing columns are zero" rule over the summing columns present in its input header. That header is the table sample block, i.e. every physical column (ordinary plus materialized).ReadFromMergeTreealready pinssign_column/version_column/is_deleted_columnforCollapsing/Replacing/VersionedCollapsingso those algorithms see complete row state underFINAL; theSummingbranch was missing the same treatment. The same gap existed ingetColumnsRequiredForMergingFinal, used by theremoveUnusedColumnsquery-plan optimization.Both read-set paths now share one helper that mirrors
SummingSortedAlgorithm::defineColumns' membership exactly, so the on-the-flyFINALpath and a real merge keep/drop the same rows:getAllPhysical(), so a materialized non-key numeric column is preserved. (An earlier revision usedgetOrdinary(), which omits materialized columns and could still drop a row whose only non-zero value lives in one.)columns_to_sumentry that names aNestedtable, e.g.SummingMergeTree(SomeMap)whose stored columns areSomeMap.ID/SomeMap.Num, is expanded to its physical subcolumns the way the engine matchescolumns_to_sum. A*MapNested member is kept regardless ofcolumns_to_sum, because the algorithm discovers and sums maps independently of the scalarcolumns_to_sumfilter.Stringcolumn) is pruned: the algorithm copies it last-value and it never affects the zero-row decision, so it is not read from disk.allow_tuple_element_aggregation, the algorithm flattensTuplecolumns (Nested::flattenTupleRecursive) and sums the leaves, so aTuplecolumn is kept when any flattened leaf is summable. The whole physical column is read, which gives the merge every leaf. The tuple recursion mirrorsdefineColumns' flattened-leaf gates: a*Map-suffixed array leaf is a summed-map candidate only when its synthesized parent is a real tuple ancestor and, with an explicitcolumns_to_sum, the leaf or an ancestor is named; otherwise it is copied last-value and not pinned, so an unlisted tuple*Mapis not over-read.Reproducer from issue #106125:
Tuple-leaf reproducer (
allow_tuple_element_aggregation):Validation:
04301_106125_summing_final_column_pruning_drops_row.sqlcovers implicit, explicit, materialized, explicit-Nested-map, explicit-multi-column, non-summable-String, tuple-leaf, nested-tuple and explicit-excluded-tuple cases underFINALwith column pruning. It fails on the unfixed binary and passes with the fix.SummingMergeTree,Coalescing,AggregatingMergeTree,allow_tuple_element_aggregationand remove-unused-columns-under-FINALstateless tests pass.FINAL; the fix only restores rows whose full stored form has a non-zero summing column. Verified the on-the-flyFINALresult matches a realOPTIMIZE TABLE ... FINALmerge for the tuple case.Closes #106125