Repository navigation
Do not read in order for DISTINCT from a FINAL read that uses vertical FINAL - #131547
Conversation
…l FINAL `ReadFromMergeTree::requestReadingInOrder` turns vertical `FINAL` off, so a preliminary `DISTINCT` over a `ReplacingMergeTree` `FINAL` read traded the cheaper vertical `FINAL` for an in-order `DISTINCT` (ClickHouse#131268, option 2 as ruled by the owner). Decline the in-order request in `buildInputOrderInfo(DistinctStep &)` when the read would use vertical `FINAL`. WIP (IMPL r1): the plan's STOP rule fired on early-exit DISTINCT ... LIMIT N shapes; see the replan request. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Vertical `FINAL` delivers its first block only after the whole merge, so a `DISTINCT ... LIMIT N` that reads in order and stops after the first merged blocks is faster than one over vertical `FINAL`. Decline the in-order request for a vertical-`FINAL` read only when no `LIMIT` (own, or above it through streaming steps) and no hinted `DISTINCT` above it can stop reading early. Related: ClickHouse#131268 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: ClickHouse#131268 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: ClickHouse#131268 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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; 1 full pass).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261114-053601 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261114-041300 |
|
Workflow [PR], commit [6a79580] Summary: ❌
AI ReviewSummaryPreserves vertical Final Verdict✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units81 translation units recompiled, 862 s compile time in total, 81 of them have a recent master baseline. |
…MIT AFTER `canStopReadingEarly` accepted only a closed list of streaming steps between the preliminary `DISTINCT` and a `LIMIT`, so an outer `LIMIT` above a hash join's probe input or a window, and `LIMIT n AFTER`, lost the in-order read and waited for vertical `FINAL` instead: about 10x slower than master (80 parts x 50,000 rows, `max_threads = 8`: join 0.085 -> 0.909 s, window 0.080 -> 0.915 s, `LIMIT AFTER` 0.082 -> 0.897 s). The walk now ends with "cannot stop early" only at a step that reads its input till the end: a sort, an aggregation, building a set, the build input of a join that fills its right side first, or a `LIMIT` that reads till the end. Any other step continues, so a step that is not listed keeps master's plan rather than the slower vertical one. Which input a join builds its table from is known only after the logical join is converted, so a `JoinStepLogical` defers the decision to the second `optimizeDistinctInOrder` pass, which sees the `JoinStep`. `LimitRangeStep` stops reading outside `ALL` mode when it has a limit or `UNTIL`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - bad2536Every 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 and resolve conflicts |
|
Merged master in c5c3ddd. The only conflict was While re-checking I measured one more case this PR makes slower: |
… runs The test reads `ReplacingMergeTree` tables with `FINAL`, which the Iceberg rewrite of the performance comparison cannot run (`Storage IcebergLocal doesn't support FINAL`). Adding it scheduled the six Iceberg jobs on this test only, and they failed with no query measured on both servers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
AI review of a later commit does not approve the PR, see the AI Review comment.
CI finish ledger - 6a79580Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261116-180018 |
|
Fixing PR for 05401_aggregation_nullable_fixed_keys_batch_pack (Stateless tests (arm_asan_ubsan, azure, targeted)) created: #132147 |
571f1d8
Closes: #131268
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
DISTINCTover aFINALread of aReplacingMergeTreetable no longer reads in the sorting key order when the read would otherwise use verticalFINAL(enable_vertical_final), unless aLIMITlets it stop early. Reading in order turned verticalFINALoff and made such queries up to 2 times slower, also through a join withquery_plan_read_in_order_through_join. Closes #131268.Description
Option 2 as ruled on the issue: #131268 (comment)
buildInputOrderInfo(DistinctStep &)asked theFINALread for the sorting key order, which turns verticalFINALoff (requestReadingInOrder). The order only helps the preliminaryDISTINCT, as the final one stays a hash set. The request is now declined when the read would use verticalFINAL.Exception: a
LIMITthat can stop theDISTINCTearly keeps the plan, unless a sort, an aggregation, a set or a join's build input reads theDISTINCTto the end first. VerticalFINALemits a block only once the merge has passed its last row, so without it these queries got 1.4-11x slower.LIMIT BYin order andMergetables also turn verticalFINALoff when reading in order; unchanged here.clickhouse local,max_threads = 8, the issue's tables asReplacingMergeTree(80 parts, merges stopped), medians:query_plan_read_in_order_through_join = 0: 1.97 s)DISTINCT c1, c2 ... FINAL WHERE c3 LIKE 'v1%'DISTINCT c1, c2 ... FINAL, non-NullablekeyDISTINCT c1, c2 ... FINAL,Nullablekey, no filterDISTINCT c1 ... FINAL LIMIT 10; issue query +LIMIT 10DISTINCT c1, c2 ... FINAL ORDER BY c1, c2The
Nullable-key row without a filter is the cost accepted in the ruling.Validation: a new stateless test checks each plan (master fails the full-read cases, the change without the exception fails the early-stop ones); the new performance test is 1.2-2.9x faster; 170 existing
FINAL+DISTINCTtests are unchanged.Workflow [PR]
Sync PR [sync-upstream/pr/131547]
Version info
26.11.1.2659-master(included in26.11and later)