Repository navigation
Stop SELECT ... FINAL WHERE ... LIMIT early with vertical FINAL when the matching rows are in a small part - #131560
Conversation
…IT stops early With `enable_vertical_final`, `ReplacingSortedAlgorithm` records the row numbers of the winning rows in the source chunk and emits the chunk only when the merge has passed the chunk's last row. A chunk of a small part spans the whole key range of a layer (a 3,000-row part is a single granule), so its winning rows, which in ClickHouse#131381 are the only rows that pass the filter, reach `Filter` and `Limit` only after the whole layer is merged: `SELECT ... FINAL WHERE <filter> LIMIT 1` reads the whole table (13,003,000 rows at max_threads = 1, against 199,608 without vertical FINAL). The non-vertical path emits every `max_block_size` merged rows. Every `max_block_size` selected rows, the merge now emits the rows selected so far of each source chunk that has waited longer than `max(max_block_size, 2 * num_inputs * chunk_rows)` selected rows, as a chunk that shares the source columns and carries the partial selection; the source chunk stays in the merge and starts a new selection. A chunk of an input holding 1/num_inputs of the keys is merged within about `num_inputs * chunk_rows` selected rows, so chunks of inputs holding at least 1/(2 * num_inputs) of the keys still leave the merge in one piece and dense merges keep emitting whole chunks without a copy (measured: the same output blocks on 4 balanced parts). The `max_block_size` term gives small chunks the same latency bound as the non-vertical path. `saveChunkForSkippingFinalFromSource` now also drops the source's reference when `selected_row` is the last holder of the chunk: a finished source is not consumed again, so its last chunk could otherwise be both queued in `to_be_emitted` and visible in `sources`, and an early emission of its selection would leave `emitChunk` a null selection. Closes ClickHouse#131381 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the waiting-selection member comment and the threshold comment to one line each. 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 cold review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261114-070400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261114-055100 |
|
Workflow [PR], commit [06c6e87] Summary: ✅
AI ReviewSummaryReviewed head Consumer coverage: Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units23 translation units recompiled, 175 s compile time in total, 23 of them have a recent master baseline. |
The test ran over the 180 s limit in `Stateless tests (amd_asan_ubsan, flaky check)`. The 15 of 50 runs that drew Wide parts took 92-188 s, against 27-37 s with Compact parts: the `max_block_size = 64` vertical FINAL scans of the 220,100-row table cost about 10x more CPU on Wide parts. That is the same on master and on this PR. Shrink the fixtures 8x (t_vf_limit) and 4x (t_vf_last), keeping their shape: the z keys still overwrite x and y rows, and the LIMIT query reads 6,244-6,696 rows with the fix against all 27,600 without it, hence the 15,000 threshold. Locally the file costs 0.18x the CPU on Wide parts and 0.16x on Compact parts. Every check still fails on the binary it failed on before: the master base on the read_rows line, the double-emission mutant on the counts, the mutant that keeps a finished chunk in its source with a LOGICAL_ERROR, also in the t_vf_last block alone. Also check that the LIMIT query runs vertical FINAL (`SelectByIndicesTransform` in its pipeline): the plain FINAL merge reads the same 6,244-6,696 rows, so the read_rows line alone cannot tell the two apart. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 93b4110Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261114-150101 |
|
@groeneai merge master |
…t-early-stop Co-Authored-By: Claude Opus 5.5 <[email protected]>
…very run Drop the query that runs with the default `max_threads`. Its FINAL layers are merged in parallel, and the number of rows the other threads read before `LIMIT 1` cancels them depends on thread timing: on the tested server of `Performance Comparison (amd_release, master_head, 5/6)` the measured runs read 1.06M to 1.99M rows (19 to 30 ms, time proportional to rows), and the profile runs 0.84M to 2.92M. Runs 0-3 landed low and runs 4-6 high, so the run-trend check reported "time keeps growing with each run" (correlation 0.86, last/first 1.50) on a read-only query with no state change between runs (marks and mark-cache hits are the same every run). The `max_threads = 1` query reads 183,224 rows on every run and keeps measuring the fix (0.925 s -> 0.019 s on that shard). Also opt the test out of the Iceberg configuration with `<test iceberg="0">`: Iceberg tables do not support FINAL, and the fill asserts the number of active parts, so the six iceberg_* jobs that a PR adding this test schedules cannot run it. The skip list is not edited because it is in ICEBERG_PERFORMANCE_PATHS, and changing it runs the whole Iceberg suite. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Head branch was pushed to by a user without write access
Backport #131560 to 26.10: Stop `SELECT ... FINAL WHERE ... LIMIT` early with vertical `FINAL` when the matching rows are in a small part
…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]>
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: #131381
Related: #131552
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
SELECT ... FINAL WHERE <filter> LIMIT non aReplacingMergeTreetable withenable_vertical_final(the default) now stops reading early when the rows that pass the filter come from a small part, instead of reading the whole table. Closes #131381.Description
With
enable_vertical_final,ReplacingSortedAlgorithmdoes not copy the winning rows: it records their row numbers in the source chunk and emits the chunk only when the merge passes the chunk's last row. A chunk of a small part spans the whole key range (a 3,000-row part is one granule), so its winning rows, here the only rows that pass the filter, reachFilterandLimitonly after the whole layer is merged. Without verticalFINALthe merge emits everymax_block_sizerows.Now, every
max_block_sizeselected rows, the merge emits the rows selected so far of each chunk that has waited longer thanmax(max_block_size, 2 * num_inputs * chunk_rows)selected rows, as a chunk that shares the source columns and carries the partial selection. Chunks of inputs that hold at least1 / (2 * num_inputs)of the keys still leave the merge in one piece, so dense merges keep emitting chunks without a copy.The issue's fixture (13M rows, 4 overlapping parts), local release build, master -> this PR:
max_threads = 8: 13.04M rows / 0.161 s -> 1.11M rows / 0.016 s (enable_vertical_final = 0: 1.56M rows / 0.024 s)max_threads = 1: 13.0M rows / 1.23 s -> 0.18M rows / 0.022 sLIMIT 6, matches in a 3,000-row update part): 6.98M -> 1.20M rowsFull
FINALscans (dense, imbalanced and update-part fixtures, and the queries offinal_big_column,parallel_final,optimized_select_final,replacing_final_non_intersecting) show no difference beyond noise; a dense merge emits the same blocks as before. Adds a performance test and a stateless test.My open #131552 appends row numbers to the same selection in batches; whichever of the two lands second has to count those rows in
num_selected_rows.Workflow [PR]
Sync PR [sync-upstream/pr/131560]
Version info
26.11.1.2646-master(included in26.11and later)26.10.6.16