Repository navigation
Fix CollapsingMergeTree merges and FINAL splitting a cancel and a state row of one key from a merged part - #131513
Conversation
…te row of one key from a merged part `IMergingAlgorithmWithSharedChunks::rowsHaveDifferentSortColumns` skipped the key comparison for two rows of one source whose part level is non-zero (added by ClickHouse#58142 in 24.1), on the premise that a merged part holds one row per sort key. That holds for ReplacingMergeTree, but a CollapsingMergeTree merge keeps the first cancel row and the last state row of a key when their counts are equal and the last row is a state row. When such a part was the oldest source of a key in a later merge or FINAL, the pair was split into two key groups: the group head (the cancel row) was collapsed on its own. A merge then kept the stale cancel row next to the newer state row (`sum(sign)` 1 -> 0, written to disk), and FINAL could return a row of an object whose rows all cancel. The shortcut is now opt-in per algorithm: `ReplacingSortedAlgorithm` sets `nonzero_level_parts_have_unique_keys`, so `CollapsingSortedAlgorithm` compares the sort columns of every pair of rows again (the 23.8 behaviour) in merges, vertical merges and FINAL. Summing, Aggregating and Coalescing use `IMergingAlgorithmWithDelayedChunk` and keep one row per key, so their shortcut is unchanged; VersionedCollapsing compares keys directly. Parts already written by affected versions are not repaired. Closes ClickHouse#120649 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261114-024000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261114-015302 |
|
Workflow [PR], commit [c222b7a] Summary: ✅
AI ReviewSummaryReviewed #131513 at Final Verdict✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units15 translation units recompiled, 83 s compile time in total, 15 of them have a recent master baseline. |
2b5b2ac
…g-merge-no-per-row-rowref Conflicts resolved against master: - ClickHouse#126158 added [[nodiscard]] to lines this PR edits: kept, except on RowRefWithOwnedChunk::firstNonEqualSortColumnsWith, which this PR removes. - ClickHouse#131513 gated the part-level shortcut of rowsHaveDifferentSortColumns with nonzero_level_parts_have_unique_keys: the cursor overload keeps that gate, so CollapsingMergeTree still compares rows of merged parts. - ClickHouse#131560 counts selected rows in vertical FINAL: kept, the selection is appended with push_back instead of insert(Field). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #120649
Related: #58142
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of
CollapsingMergeTree: a merge could keep a stale cancel row and changesum(sign)of a key, andSELECT ... FINALcould return a row of a cancelled object, when the oldest part holding the key was a merged part with a cancel and a state row of that key. The bug existed since 24.1.Description
On 26.10.2.103, merging a
CollapsingMergeTreepart that kept a cancel/state pair of a key with a newer state row keeps the stale cancel row and returnssum(sign) = 0. With default settings:IMergingAlgorithmWithSharedChunks::rowsHaveDifferentSortColumnsskips the key comparison for two rows of one source with a non-zero part level (#58142), assuming a merged part has one row per key. A Collapsing merge keeps up to two (the first cancel row and the last state row), so the pair was split into two key groups and the cancel row was collapsed on its own.SELECT ... FINALcould likewise return a row of a key whose rows all cancel.The shortcut is now opt-in: only
ReplacingSortedAlgorithm, whose merges keep one row per key, sets it, so Collapsing compares keys again, as in 23.8. Summing, Aggregating and Coalescing (IMergingAlgorithmWithDelayedChunk) also keep one row per key and are unchanged; VersionedCollapsing compares keys directly. Parts already written by affected versions are not repaired.The new test fails on master in every randomized run and passes 50/50 with the fix. Worst-case cost (one 20M-row merged part plus a small overlapping cancel part, release build): +3 to 5% CPU for
FINALandOPTIMIZE ... FINALon aUInt64key, +0.4% forOPTIMIZE ... FINALon a(String, UInt64)key, reads withoutFINALunchanged.My open #131494 changes the same helper (shortcut kept); whichever merges second needs a one-line rebase.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131513]
Version info
26.11.1.2554-master(included in26.11and later)