Repository navigation
Conversation
`SummingSortedAlgorithm` starts and finishes a group for every row, even where no other row has the same key: `Field` copies of the columns that are not summed, a sum state per summed column and the block byte check per row. Rows of a part with a non-zero level have unique keys (ClickHouse#58142), and the batch queue (ClickHouse#115869) already reports runs of such rows that are less than the current rows of the other sources, so every row strictly inside such a batch is a group of one row. When every summed column is a plain number summed by `sumWithOverflow`, the result of such a group is the row itself, with 0 added to float sums (-0.0 becomes +0.0), and it is removed in merges and INSERT if all sums are zero. Copy these rows with `insertRangeFrom` instead. For INSERT (`optimize_on_insert`, one level-0 source) a row whose key differs from both neighbours is a group of one row too: copy such runs through the sort permutation, skipping zero rows. The batch path is used only when a sample merge of at most 1024 rows (and at most 1/8 of the rows, as in `sourcesHaveRunsWorthSkipping`) shows that long batches from such sources dominate, for single-column and composite keys alike. Merges whose keys interleave keep the per-row loop. Columns with a dynamic structure and replicated inputs keep it too, decided once (the replicated state reuses `MergedData::mayHaveReplicatedColumns`, whose scan helpers move from `MergingSortedAlgorithm.cpp` to `IMergingAlgorithm.h`). Based on the analysis and patch by snelheidai in ClickHouse#131261. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rf test on Iceberg 05633_summing_merge_tree_batch_copy compared the FINAL with max_block_size = 1000 with the GROUP BY oracle in one direction only, so rows missing from that FINAL were not seen. That FINAL is the one where the block limit stops a bulk copy early. Compare it both ways, with the setting on the outer query. The new performance test uses FINAL and named parts, which Iceberg tables do not have, so mark it `iceberg="0"`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the comments on the design of the new code and shorten the ones that state an invariant. 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) plus my own review, two rounds.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261114-024200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261113-232700 |
|
Workflow [PR], commit [61ef58a] Summary: ✅
AI ReviewSummaryReviewed #131514 at Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes9 object files changed (+91.97 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units51 translation units recompiled, 410 s compile time in total, 51 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.92 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
… per row The sample in `sourcesHaveLongBatchesOfUniqueKeys` stopped at a batch that reaches the end of a chunk without counting it, so a merge that starts with such a batch (parts whose key ranges do not overlap, e.g. a sorting key that starts with a date or a time) sampled no rows and kept the per-row loop. Count the batch before stopping (found and measured by snelheidai in ClickHouse#131261). OPTIMIZE DRY RUN, x86, 4 pinned threads, 20 paired rounds against the previous commit: 30 one-day parts ORDER BY (d, sid) -44.2%, 30 parts of consecutive time ranges ORDER BY ts -53.4%, a 29-day part plus 10 parts of the next day -42.1%; the worst case (only the first chunk precedes the other part) +1.4% [-0.7, +3.6]; interleaved keys and overlapping parts +0.1% / +0.9%. 42 FINAL and merge digests (with `hash_of_all_files`) identical to master. `setRow` copies a `Variant` value through a `Field`, and inserting the `Field` picks the first alternative that accepts it (a UInt64 in `Variant(Date, UInt64)` becomes a Date), while the bulk copy keeps the alternative, so one INSERT or FINAL returned both. Columns whose type contains `Variant` keep the per-row loop, as on master. The performance test gets a merge of ten parts of consecutive time ranges; 05633 checks that a `Variant` column gets one alternative on INSERT and FINAL. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`sourcesHaveLongBatchesOfUniqueKeys` stopped at a batch that reaches the end of a chunk without counting it, so a merge whose first batch covers a whole chunk (parts of consecutive key ranges, or a merged part whose first chunks precede the updated keys) sampled no rows, and with a single-column key it kept the per-row loop. Without `is_deleted` the whole-chunk shortcut takes most of those rows; with `is_deleted` it is off, so such merges gained nothing. Count the batch before stopping, as the same sample in ClickHouse#131514 does (found and measured by snelheidai in ClickHouse#131277). `OPTIMIZE TABLE ... FINAL` of a copy, x86, 4 pinned threads, 10 paired rounds against the previous commit: `ReplacingMergeTree(ver, is_deleted)` of 30 parts of consecutive id ranges -38.4% [-39.5, -37.3], a merged part plus 10 parts of updates of its last 200k ids -35.8% [-37.6, -33.9] (instructions 0.44x on the first); the same tables without `is_deleted` -0.3% / +1.4% and the interleaved worst case +1.2%, all with CIs including zero (20 rounds: +1.8% [+0.1, +3.4] elapsed for the second, instructions +0.07%); `cdc1` `FINAL` and `OPTIMIZE` unchanged. 79 result and merged-part digests (`hash_of_all_files`, several merge block sizes, vertical merge, `CLEANUP`, `FINAL` variants) and the 259-digest differential harness are identical to master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #131261
Related: #127826
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
SELECT ... FINAL, merges andINSERTofSummingMergeTreetables copy the rows that have no other row with the same key in bulk instead of summing them one by one. With one large part and a few small parts of new rows for existing keys,FINALis up to 3.5x faster and merges up to 2.3x faster; anINSERTof new keys is up to 2.3x faster. Closes #131261.Description
Analysis and patch by snelheidai in #131261, verified and measured here.
SummingSortedAlgorithmstarts and finishes a group for every row (Fieldcopies, a sum state per column, a byte check per row), even for rows that have no other row with their key. Rows of a part with a non-zero level have unique keys (#58142), and the batch queue (#115869) reports runs of such rows that are less than every other source's current row, so each row strictly inside such a batch is a group of one row. When every summed column is a plain number summed bysumWithOverflow, that group is the row itself (+0 on floats, so -0.0 becomes +0.0; dropped in merges and INSERT when all sums are zero): such rows are now copied withinsertRangeFrom, keeping the block limits. In an INSERT block, a row whose key differs from both neighbours is copied the same way through the sort permutation.The path is used only when a sample merge (at most 1024 rows, 1/8 of the rows) shows long batches dominate; a batch up to the end of a chunk counts, so merges of parts with consecutive key ranges use it too. JSON, Dynamic and
Variantcolumns (the per-rowFieldcopy can change the alternative of aVariantvalue) and replicated inputs keep the per-row loop.Validation (x86, 4 threads, 10-20 paired rounds):
FINAL-47% to -71%, merges -19% to -57% (LowCardinality with #127826), merges of parts with consecutive key ranges -42% to -53%,INSERTof new keys -32% to -57%; interleaved merges, point lookups, AggregatingMergeTree and small INSERTs within noise; peak memory at most +3.6%. 468 result and merged-part digests (hash_of_all_files) identical to master over 22 table shapes and 15 INSERT shapes. New performance test and a stateless test checked against aGROUP BY.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131514]