Repository navigation
Do not remove zero rows in SELECT FINAL from SummingMergeTree - #115032
Conversation
A merge of SummingMergeTree removes a row when the values in all of the columns of the summation are zero after summing. The FINAL read applied the same rule, but only over the columns the query happened to read: a query reading a subset of the summed columns (for example, bare count(), which reads a single smallest column, plus the internally added sorting key) removed every group whose visible columns summed to zero, even though an unread summed column made the row survive a real merge. The rows silently disappeared from the result. Conversely, a query reading none of the summed columns never removed anything, keeping rows that a real merge deletes. Fix by always including all columns that a merge would aggregate into the FINAL read set (and into the set protected from the plan-level column pruning), so the removal decision is taken over the same set of columns as in a real merge. Found by the SQLancer FinalMerge oracle: SELECT count() FINAL returned 9947 before OPTIMIZE FINAL and 9999 after it on the same data. https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=5c9f28ed5a2c585ad46bf74d6600cdcf3aa07a9e&name_0=NightlySQLancer&name_1=SQLancer%20%28arm_asan_ubsan%29&name_1=SQLancer Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [b3ef481] Summary: ✅
AI ReviewSummaryThis PR changes Final Verdict
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 50 s compile time in total, 9 of them have a recent master baseline. |
`getColumnsAggregatedForSummingFinal` decided which `...Map` arrays participate in the merge by the `endsWith(name, "Map")` name convention alone, while `SummingSortedAlgorithm::defineColumns` promotes a discovered map group only after validating it (at least two columns, an integer-like key, summable values, no member of the sorting or partition key). Legal schemas such as `OnlyOneColumnMap Nested(ID UInt32)` or `NonArithmeticValueMap Nested(ID UInt32, Date Date)` are only copied by a real merge, yet `SELECT count() ... FINAL` read their arrays in full. Instead of duplicating the classification, ask the merge algorithm itself: the new `SummingSortedAlgorithm::getAggregatedColumnNames` runs `defineColumns` over the table header and returns the columns of `columns_to_aggregate` and `maps_to_sum`. The read set now cannot drift from what a merge actually aggregates. Columns of a composite-key map are kept in the set because their presence makes the merge keep every row. Adds a test for a summed column hidden from both the projection and the `PREWHERE` predicate, and a test that `FINAL` no longer reads the arrays of a `...Map` group that the merge rejects.
|
🕵 @groeneai, investigate the failure: It is unrelated to this pull request: the same test fails on master and across many unrelated pull requests (3-13 failures a day over the last month, per |
|
The fix is in progress in #115343, which you opened on 2026-08-18. Same signature as this red: It is the only red on One caveat in case #112828 comes up as the owner: that one merged on 2026-08-13 and only recovers a subscription the client had closed, so it does not cover this window. #115343 is the PR to track. |
…dy read With `allow_tuple_element_aggregation`, the `FINAL` read set is extended with the tuple-flattened leaves of the summed columns. The deduplication compared exact names only, so a query that reads a whole `Tuple` column got both the tuple and each of its leaves, and the merge - which flattens the tuple again on its own - read and aggregated the same numeric leaf twice. Skip a leaf whose ancestor is already requested. Also make `05024_summing_final_hidden_column_and_rejected_map` assert the read set on the pipeline instead of on `read_bytes` of a 200000-row table. The byte threshold depended on the randomized settings of the flaky check (it failed with `result differs with reference`) and the inserts made the test time out under sanitizers (`Test runs too long`). The pipeline assertion needs neither.
…cestors `SELECT ... FINAL` from a `SummingMergeTree` table with `allow_tuple_element_aggregation` skipped a flattened leaf only when the query read the whole top-level tuple, because the covered prefixes were built from the top-level columns of the metadata sample block. An intermediate tuple subcolumn is a column of its own in the read set, so `SELECT tup.inner FROM t FINAL` still appended `tup.inner.c` and `tup.inner.d` next to `tup.inner`, and the merge flattened `tup.inner` again, reading and aggregating the same leaves twice. `Nested::flattenTupleRecursive` already reports, for every leaf, the tuple paths it descends from: the root column and every intermediate tuple. Use them, so a leaf is skipped whenever any of its ancestors is already read, whatever the depth.
The `Stateless tests (amd_msan, flaky check)` job failed on `05024_summing_final_hidden_column_and_rejected_map`: with the randomized settings, the pipeline of `SELECT count() FROM summing_final_rejected_map FINAL` contained `NonArithmeticValueMap.D`, so the assertion that no rejected `...Map` group is read found four matching header lines instead of none. That column was not added by the `FINAL` read set: a bare `count()` reads the column that is cheapest to read, and which column that is follows from the on-disk sizes, so the randomized `MergeTree` settings (sparse serialization, compact parts, file name hashing) can make it a `...Map` array. Assert the read set on `SELECT k` instead, which reads the sorting key plus exactly the columns the merge requires. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115032&sha=64b9870e50cbe21a3fa4582223d42ec502831b6e&name_0=PR&name_1=Stateless%20tests%20%28amd_msan%2C%20flaky%20check%29 #115032
alexey-milovidov
left a comment
There was a problem hiding this comment.
🕵 Publishing the replies to the earlier findings on this pull request.
Replace the previous approach (always reading every summed column for `FINAL`) with the opposite one, as requested in the review: `FINAL` sums the rows with equal keys but never removes the rows whose sums are all zero. A query may read only a subset of the summed columns, so it cannot decide whether a row is zero the way a merge does; reading all summed columns to make that decision would degrade performance too much. This is backward incompatible: `SELECT ... FINAL` now returns all-zero rows that a background merge would delete. The result no longer depends on the set of columns the query reads (e.g. `SELECT count() FROM t FINAL`). Revert all changes of this PR in `ReadFromMergeTree`, `ExpressionAnalyzer`, `PlannerJoinTree` and `SummingSortedAlgorithm`, add a `remove_zero_rows` parameter to `SummingSortedTransform` and pass `false` from `addMergingFinal`. Update `03802_summing_merge_tree_tuple_element`, which pinned the removal of zero rows under `FINAL`, and the engine docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Reworked as requested in 5c39dfe: the approach that read every summed column for |
…ows kept by `FINAL` `SELECT ... FINAL` from `SummingMergeTree` no longer removes the rows whose summed columns are all zero, so the row with `value = 0` is now returned before the merge. After `OPTIMIZE TABLE ... FINAL` it is still removed. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=115032&sha=5c39dfe7245372cc65c2cccb80c4c17478a2e887&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 #115032 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`03280_json_subcolumns_in_primary_key_select_final` expects the row with `value = 0` in `SELECT ... FINAL`, but with `optimize_on_insert = 1` (the default, used by the fast test) the insert itself removes that row, so the result depended on the randomized setting. Pin `optimize_on_insert = 0`. `03802_summing_merge_tree_tuple_element` Test 8 now expects the zero rows in `SELECT ... FINAL`, so stop merges for that table until `OPTIMIZE`, otherwise a background merge could remove them first. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=115032&sha=1379e50b79507a52d1f0b61dc18d64dcddebd204&name_0=PR&name_1=Fast%20test #115032 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The red |
…geTree` to the zero-row removal `FINAL` still depends on the read set for nested `...Map` structures: if the query reads only some of their columns, they are not merged the way a merge does. Say so in the engine documentation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The red |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is a big relief!
@groeneai, please correct the SQLancer oracle (or remove it if it cannot be repaired). Send a new PR. |
|
Acknowledged. I'm fixing the SQLancer |
|
The fix is ClickHouse/sqlancer#22. For |
… SummingMergeTree ClickHouse/ClickHouse#115032 makes `SELECT ... FINAL` from `SummingMergeTree` keep the rows whose summed columns are all zero, while background merges and `OPTIMIZE TABLE ... FINAL` still delete them. The FinalMerge oracle compared `count()` under `FINAL` before and after `OPTIMIZE ... FINAL`, so on a server with that change it reports every all-zero group as a mismatch. For a SummingMergeTree table both reads now count only the rows where at least one summed column is non-zero: `countIf(NOT (c1 = 0 AND ...))`. The columns are the integer and Decimal columns that the merge sums for certain: not in the sorting or partition key, physically stored, and listed in the engine's `columns` argument when there is one. Any row the merge deletes has all of them equal to zero, so it is excluded from both counts and the comparison holds with and without #115032. Float columns are left out because a float sum can be zero in one summation order and not in another. A table without such a column is skipped, and so is a table with an unfinished mutation, whose columns may not be readable. `countIf` rather than `WHERE` keeps the predicate from being moved before the merge under `optimize_move_to_prewhere_if_final`. Other engines keep the plain `count()` comparison. Co-Authored-By: Claude Opus 5.5 <[email protected]>
SELECT ... FINALfrom aSummingMergeTreetable returned results that depended on which columns the query read.A merge removes a row when the values in all of the columns of the summation are zero after summing. The
FINALread applied the same rule, but only over the columns the query happened to read. SoSELECT count() FROM t FINAL(which reads a single small column) orSELECT c0, c3 FROM t FINALcould silently drop rows thatSELECT * FROM t FINALreturns, and a query reading only the sorting key never removed anything.Reading every summed column for each
FINALquery would make the removal decision consistent, but it degrades performance too much. Instead,SELECT ... FINALnow sums the rows with equal keys and never removes zero rows. Its result no longer depends on the set of columns read. Background merges andOPTIMIZE TABLE ... FINALstill remove zero rows as before, soFINALcan now return all-zero rows that a merge would delete. This is a deliberate backward-incompatible change.Implementation: a
remove_zero_rowsparameter onSummingSortedTransform, set tofalseinaddMergingFinal.Found by the nightly SQLancer
FinalMergeoracle (SELECT count() FROM t0 FINALreturned 9947 beforeOPTIMIZE TABLE t0 FINALand 9999 after it on the same data): https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=5c9f28ed5a2c585ad46bf74d6600cdcf3aa07a9e&name_0=NightlySQLancer&name_1=SQLancer%20%28arm_asan_ubsan%29&name_1=SQLancer. If the SQLancer oracle comparesFINALwith a laterOPTIMIZE ... FINAL, it needs to filter out the all-zero rows.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
SELECT ... FINALfromSummingMergeTreeno longer removes the rows whose summed columns are all zero, unlike background merges. Before, this removal looked only at the columns the query read, so results depended on them: for example,SELECT count() FROM table FINALcould return fewer rows thanSELECT * FROM table FINAL.Documentation entry for user-facing changes
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/115032]
Version info
26.10.1.2136-master(included in26.10and later)