Repository navigation
Support virtual rows in reading in order with FINAL - #124468
Conversation
|
Workflow [PR], commit [2f136ca] Summary: ✅
AI ReviewSummaryThe PR enables virtual-row bounds after Final Verdict✅ Approve. |
ca42053 to
dfacb80
Compare
Reading in order through an `INNER JOIN` requires a virtual row, and a `FINAL` read refused to build one, so such queries read and sorted the whole left table. The FINAL merges still do not consume virtual rows: a `VirtualRowTransform` after each of them announces the least primary key its parts start from, taken from the primary index (the greatest for a read in the reverse order), so the merge downstream defers the FINAL merges the limit does not reach. The virtual row covers the primary key prefix the sort reads, which is all the merge downstream compares, so the other sorting key columns do not matter. Excluded: `GraphiteMergeTree`, whose rollup lowers the time column, and parallel replicas. The virtual rows are attached only when a step requested reading in order, since a FINAL read is in order for its merges anyway, and the optimizer drops the conversions when `requestReadingInOrder` refuses the request. The test 04489_virtual_row_disabled_with_final pinned the old refusal and is removed; 03928 pins `read_in_order_use_virtual_row`, which its reference now depends on. Co-Authored-By: Claude Fable 5.1 <[email protected]>
08d19c7 to
91e3e3e
Compare
A layer's virtual row was the least index value at its parts' range starts. That bounds the layer loosely: the first granule of a sparse part may span many keys, so the announced key sat far below the layer's start (205k keys below for the second layer of the `final_read_in_order_limit` perf table, whose update parts have gaps between their key blocks). The merge downstream then left the front layer after a few hundred rows, armed its read-ahead window and read every layer, so `WHERE v = 7 ORDER BY k LIMIT 100` read 8.1M rows with virtual rows against 9.3M without and got 25% slower. `splitPartsWithRangesByPrimaryKey` now returns the layer borders, and `spreadMarkRangesAmongStreamsFinal` takes the tighter of the parts' bound and the border a layer starts from: `borders[i - 1]` for a forward read, `borders[i]` for a reverse one. The same query now reads 426k rows from a single layer and is 8% faster than without virtual rows. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: This PR lets virtual rows (the read-in-order optimization) work with SELECT ... FINAL on Replacing, Collapsing, Summing, Aggregating and Coalescing MergeTree tables. For non-FINAL reads in order, the only change is a refactor that moves the virtual-row key header out of the per-part loop. All four flagged improvements are downgraded to not sure. ClickBench Q33 (-11.9%) and Q34 (-12.5%) are full-scan aggregations, and TPC-H Q1 (-18.5%) and Q7 (-24.7%) run on plain MergeTree tables. None of these queries uses FINAL or reads in order, all four have noisy master history, and both ClickBench medians sit inside master's recent range. This run shows no measurable effect from the PR. Its intended speedup, FINAL ORDER BY ... LIMIT reads that skip layers the limit never reaches, is exercised only by the updated final_read_in_order_limit performance test, which is not part of these benchmarks. clickbenchFlagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (2 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
Build profile diff (arm_release)Commit See the job log for details. |
Change speedups one query and slows down another. This is not CREATE TABLE t_vrow2 (k UInt64) ENGINE = MergeTree ORDER BY k;
SYSTEM STOP MERGES t_vrow2;
INSERT INTO t_vrow2 SELECT number FROM numbers(32000000)
SETTINGS max_block_size = 4000000, max_insert_block_size = 4000000, min_insert_block_size_rows = 4000000, min_insert_block_size_bytes = 0;
SELECT k FROM t_vrow2 WHERE sipHash64(k) = 0 ORDER BY k LIMIT 10
SETTINGS max_threads = 8, use_query_condition_cache = 0, read_in_order_use_virtual_row = {1|0};I suppose we can admit it and align queries with FINAL with regular in terms of using virtual row without introducing specific setting for it. |
| <fill_query>INSERT INTO final_inorder_limit SELECT number, cityHash64(number, 2) % 1000, toString(cityHash64(number, 102)) FROM numbers_mt(4000000)</fill_query> | ||
| <fill_query>INSERT INTO final_inorder_limit SELECT number, cityHash64(number, 3) % 1000, toString(cityHash64(number, 103)) FROM numbers_mt(4000000)</fill_query> | ||
|
|
||
| <settings><read_in_order_use_virtual_row>0</read_in_order_use_virtual_row></settings> |
There was a problem hiding this comment.
With virtual row, it now behaves the same as regular queries, with virtual row enabled by default. Some layouts may be slower because of lazy reading and some faster, so I disabled the setting to test the previous code branch.
Master moved `stringToBool` with its `CANNOT_PARSE_BOOL` declaration to `SettingsFieldsConversionHelpers.cpp`, while `SettingFieldTriBool::readBinary` in this branch still throws `CANNOT_PARSE_BOOL`. The style check on the merged tree reported the code as used but not declared: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124468&sha=be990d897a35d190f6fc586accb7a7036ea311d9&name_0=PR&name_1=Style%20check #124468 Co-Authored-By: Claude Fable 5.1 <[email protected]>
Backport #124468 to 26.10: Support virtual rows in reading in order with FINAL
Cherry pick #124468 to 26.8: Support virtual rows in reading in order with FINAL
Cherry pick #124468 to 26.9: Support virtual rows in reading in order with FINAL
Backport #124468 to 26.9: Support virtual rows in reading in order with FINAL
- `Settings.cpp`: drop the old `Bool` declaration of `query_plan_read_in_order_through_join` left by the conflict resolution and terminate the new `BoolForce` one - `optimizeTree.cpp`: convert `read_in_order_through_join` at the second-pass call site too - `ReadFromMergeTree`: add `getPartsForPrewhere` and `MergeTreeDataSelectExecutor::filterParts`, which master has and 26.8 does not - `SettingsChangesHistory.cpp`: record the new `force` value in the 26.8 section - `ReadFromMergeTree.cpp`: remove a whitespace-only line in a lambda capture Build failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128683&sha=d096f2332cff46ae424b5df8a698eb42c356c437&name_0=BackportPR&name_1=Build%20%28amd_debug%29 PR: #128683 Co-Authored-By: Claude Fable 5.1 <[email protected]>
Backport #124468 to 26.8: Support virtual rows in reading in order with FINAL
- `Settings.cpp`: drop the old `Bool` declaration of `query_plan_read_in_order_through_join` left by the conflict resolution and terminate the new `BoolForce` one - `optimizeTree.cpp`: convert `read_in_order_through_join` at the second-pass call site too - `ReadFromMergeTree`: add `getPartsForPrewhere` and `MergeTreeDataSelectExecutor::filterParts`, which master has and 26.8 does not - `SettingsChangesHistory.cpp`: record the new `force` value in the 26.8 section - `ReadFromMergeTree.cpp`: remove a whitespace-only line in a lambda capture Build failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128683&sha=d096f2332cff46ae424b5df8a698eb42c356c437&name_0=BackportPR&name_1=Build%20%28amd_debug%29 PR: #128683 Co-Authored-By: Claude Fable 5.1 <[email protected]>
The test of #130893 does not run on 26.3 as it is: - The settings `read_in_order_use_virtual_row_per_block`, `max_bytes_ratio_before_external_join`, `max_bytes_before_external_join`, `join_runtime_filter_min_probe_rows` and `query_plan_optimize_join_order_randomize` do not exist in 26.3. The two cases with a virtual row after each block are removed. - `EXPLAIN` prints `ReadType:` instead of `Read type:`. - The virtual row is disabled for `FINAL` in 26.3 (#124468 is not there), so `FINAL` is not read in order through `INNER JOIN`, and the case is removed. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
FINALnow use the virtual row optimization of reading in order. Supported forReplacingMergeTree,CollapsingMergeTree,VersionedCollapsingMergeTree,SummingMergeTree,AggregatingMergeTreeandCoalescingMergeTree.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124468]
Version info
26.11.1.396-master(included in26.11and later)26.10.2.56