Repository navigation
Conversation
…eTree FINAL and merges `ReplacingSortedAlgorithm::merge` handles one row per loop iteration (compare with the selected row, write the previous group, select through a reference-counted `RowRef`, advance the queue), also inside long runs from one part with a non-zero level, which already counts as having no duplicate keys (`rowsHaveDifferentSortColumns`). A batch from such a part holds rows that are all less than the other cursors' rows, except that its last row may equal the next source's row, so every row strictly inside it is a group of one. - A batch of at least 8 rows from such a part writes the current group and takes its inner rows in one go: vertical FINAL appends the row numbers to the chunk's selection, otherwise `MergedData::insertRows` within `max_block_size` and `rowsToInsertBeforeFlush`; row sources, `is_deleted` validation and CLEANUP, and the TTL row filter are applied per row. - When `rowsToInsertBeforeFlush` returns one row for more allowed rows (columns whose byte size it cannot estimate: `AggregateFunction`, `JSON`, `Dynamic`, replicated columns; or a first row that already fills the block), the batch rows are written one at a time with `hasEnoughRows` before each, which is the per-row path's own decision, so block boundaries stay exact. Otherwise such a merge paid that column scan every three rows (+5% on a merge with a LowCardinality column while it was still in that class). - The batch is taken only when the current row is the selected one. The per-row path never compares two rows of such a part, but compares a row with a selected row of another source, so on parts that break the premise (a sorting key shortened by `MODIFY ORDER BY`, ClickHouse#70667; adopted parts, ClickHouse#109686) the result stays identical to the per-row path. - For a single-column key the batch detection is enabled only when a sample merge of at most 1024 rows (and at most 1/8 of the rows of the current chunks) puts at least half of them into such batches, so interleaved parts keep the per-row path. The sample is the same as the one SummingMergeTree uses for the same purpose. - The merge loop is a template instantiated with and without the batch check, and the batch function is out of line and takes the cursor by pointer: the reporter measured the per-row loop 3-6% slower otherwise. - One predicate, `sourceHasUniqueKeys`, now holds every part-level test of these classes. A performance test covers FINAL with and without vertical FINAL, FINAL over a two-column key and OPTIMIZE FINAL of one large merged part plus ten small parts of updates. Measured (non-LTO RelWithDebInfo, Xeon 6975P-C, 4 CPUs, 10 paired rounds, fix/base user CPU): `FINAL` of 8M rows + 10 parts of 20k updates 0.68, the same with `enable_vertical_final = 0` 0.54, `OPTIMIZE FINAL` of it 0.75 (`instructions:u` 0.53 and 0.59); a LowCardinality column 0.90, a JSON column 0.96; two interleaved parts and a two-column key unchanged. Analysis and patch from ClickHouse#131277 by @ snelheidai. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove comments that narrate the new code (NO_INLINE reason, block-limit steps, member docs repeating their names); shorten the remaining ones to one line each, two for the batch-take condition. 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; 1 finding) and my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261114-063400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261114-043401 |
|
Workflow [PR], commit [00b9f2e] Summary: ✅
AI ReviewSummaryThis PR batches rows from parts with unique keys in Final VerdictStatus: ✅ Approve |
`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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes10 object files changed (+93.54 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units15 translation units recompiled, 62 s compile time in total, 15 of them have a recent master baseline. |
CI finish ledger - 00f9039Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261114-120014 |
The six `Performance Comparison (*, iceberg_*)` jobs run a performance test that a PR adds on Iceberg tables. This test cannot run there: it modifies `max_bytes_to_merge_at_max_space_in_pool`, reads with `FINAL` and checks `system.parts`, none of which the Iceberg rewrite supports (`Setting 'max_bytes_to_merge_at_max_space_in_pool' cannot be modified for Iceberg storage`). It measures ReplacingMergeTree FINAL and merges, so opt it out with `<test iceberg="0">`. With it, `changed_iceberg_tests` selects no test and the Iceberg jobs are not scheduled; the `master_head` jobs still run the test. The skip list is not used: `tests/performance/iceberg_skip_list.txt` is in `ICEBERG_PERFORMANCE_PATHS`, so editing it runs the whole Iceberg suite. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-unique-batches # Conflicts: # src/Processors/Merges/Algorithms/IMergingAlgorithmWithSharedChunks.h # src/Processors/Merges/Algorithms/ReplacingSortedAlgorithm.cpp
Since ClickHouse#131560, vertical FINAL emits the rows selected so far from a chunk whose selection waited longer than a merge of its input should take, measured in selected rows (`num_selected_rows`). `insertUniqueRows` appended whole batches to `replace_final_selection` without counting them and without stamping `replace_final_selection_start` on a new selection, so on a merged part the clock advanced by about three rows per batch and the small part's selection waited to the end of the merge. Measured on a debug build: a merged 200000-row part plus a 400-row part holding the newest version of every 500th key, `SELECT k FROM t FINAL WHERE v = 'target' LIMIT 1` with `enable_vertical_final = 1, max_threads = 1` read 200400 rows without this change and reads 4496 with it, the same as master. Results of FINAL with and without vertical FINAL are unchanged. The new case in 05632 fails without this change (5 of 5 randomized runs) and passes with it (30 of 30) and on master.
Closes: #131277
Related: #131494
Related: #131513
Related: #131514
Related: #131560
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
SELECT ... FINALand merges ofReplacingMergeTreetables are faster when most rows come from large merged parts and only a few from small parts of updates: the rows of a part without duplicate keys that lie between the keys of the other parts are now taken in batches instead of one at a time. Closes #131277.Description
Analysis and patch by snelheidai in #131277.
ReplacingSortedAlgorithmhandles one row per loop iteration, even inside long runs from a part with a non-zero level, which the merge already treats as having no duplicate keys (rowsHaveDifferentSortColumns). The batch queue (#115869) reports such runs, and every row strictly inside one is a group of its own. A batch of at least 8 such rows is now written in one go: vertical FINAL appends the row numbers to the selection, otherwiseMergedData::insertRowswithin the block limits (row by row when the byte size cannot be estimated). Row sources,is_deletedchecks, CLEANUP and the TTL filter stay per row. For a single-column key the detection turns on only when a sample of the first rows is dominated by long batches (the same sample as #131514).A batch is taken only when its first row is the selected one, so parts that break the no-duplicates premise (a key shortened by
MODIFY ORDER BY, #70667; adopted parts, #109686) give the same result as master.Validation (non-LTO RelWithDebInfo, Xeon 6975P-C, 4 CPUs, 10 paired rounds, user CPU fix/base): FINAL of one 8M-row part plus 10 update parts 0.68 (0.54 with
enable_vertical_final = 0),OPTIMIZE FINAL0.75, 50 small parts 0.76-0.79, merges with aLowCardinalityorJSONcolumn 0.90 / 0.96; interleaved parts, a two-column key, point lookups and INSERT not slower. The new performance test under CI's LZ4 pin: 0.56 / 0.40 / 0.59 user CPU,OPTIMIZE0.74 elapsed. 259 result and merged-part digests (hash_of_all_files) are identical to master over 9 table shapes, and six mutations of the new path are each caught by them or by existing stateless tests.Batched rows also advance the vertical FINAL selection wait of #131560 (new 05632 case). #131494 and #131514 (mine) touch the same files; whichever merges later adapts.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131552]