Repository navigation
Conversation
…merging sorted streams `MergingSortedAlgorithm::mergeBatchImpl` ran the same per-batch bookkeeping for every batch, but two batch shapes make all of it a no-op when there is no row limit (merges never have one): - A chunk moved whole (keys of the sources do not overlap) first went through `MergedData::rowsToInsertBeforeFlush`, which for merges walks every row of the chunk calling `byteSizeAt` per column, and the whole-chunk branch then ignored the result (it never reads `updated_batch_size` / `batch_skip_last_row` without a limit). - A one-row batch (keys interleaved row by row) that is not the last row of its chunk paid the out-of-line `rowsToInsertBeforeFlush` call and the clip / limit / whole-chunk checks: `hasEnoughRows` has just checked the block limits, `rowsToInsertBeforeFlush(.., 1, ..)` returns 1, and `insertRows(cur, 1)` is `insertRow(cur)`. It now takes `insertRow`, as `mergeImpl` does. With a row limit the old code runs unchanged. Merged parts are byte-identical with and without the change (5 table shapes x 16 KiB and 10 MiB `merge_max_block_size_bytes` x horizontal and vertical), and query results are identical. Release build, 10 parts x 1M rows, 12 paired runs, geomean of fix / base ratios (two runs with the server core sets swapped): batch-queue merge with disjoint keys -8.1% / -6.6%, 5 columns with `String` -4.0% / -3.4%, keys interleaved row by row -2.8% / -3.9%, runs of 100 rows -0.1% / -0.7% (n.s.); `SELECT * ... ORDER BY b` read in order, 4 threads, -5.1% / -8.8%. Merges with the default queue: no change. Adds the perf test `merge_batch_sorting_queue.xml`. Proposed and measured by @ snelheidai: ClickHouse#127826 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reword the two new comments: name `limit` instead of the ambiguous "row limit", and say a whole chunk ignores all block limits (bytes and average size), not only the byte limit. 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) plus my own review (2 nits). No code change.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261116-222700 |
|
Workflow [PR], commit [bff98e9] Summary: ✅
AI ReviewSummaryReviewed commit Final Verdict✅ Approve |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Processors/Merges/Algorithms/MergingSortedAlgorithm.cpp.o |
3.88 MiB | 4.20 MiB | +334.32 KiB (+8.42%) |
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
7 translation units recompiled, 46 s compile time in total, 7 of them have a recent master baseline.
Related: #127826
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Merging sorted streams with the batch sorting queue is faster when the keys of the sources interleave row by row or do not overlap. This speeds up queries that merge sorted streams without
LIMIT, for exampleORDER BYread in order, and merges of tables withmerge_use_batch_sorting_queue.Description
MergingSortedAlgorithm::mergeBatchImplran the same bookkeeping for every batch. Without a row limit (merges never have one) two shapes make it a no-op:MergedData::rowsToInsertBeforeFlush, which for merges callsbyteSizeAtper row and column, and the whole-chunk branch then ignored the result. The call is now skipped for it.insertRow, asmergeImpldoes:hasEnoughRowshas just checked the block limits androwsToInsertBeforeFlushreturns 1 for one row.With a row limit the old code runs. Output does not change: merged parts are byte-identical with and without the change (5 shapes x 16 KiB / 10 MiB
merge_max_block_size_bytesx horizontal / vertical), as are read-in-order results.Release build, 10 parts x 1M rows, geomean of 12 paired PR / master ratios, two runs with the server core sets swapped:
StringSELECT * ... ORDER BY bread in order, 4 threadsThe new perf test covers the first, third and last rows; the CI comparison flags only changes above 15%, so it will show them as unchanged.
The finding and the patch are by @ snelheidai (#127826 (comment)).
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132178]