Repository navigation
Estimate range conditions from all parts of tdigest statistics - #126385
hanfei1991 merged 2 commits into
Conversation
The selectivity estimator accumulates the `tdigest` statistics of all parts with `StatisticsTDigest::merge`. `QuantileTDigest::merge` only appends the other digest's centroids and sorts them once more than `max_unmerged` (2048) are pending, while `getCountLessThan` is const and walks the centroids in array order, returning 0 when the first centroid is already above the bound. So a range condition was estimated from whichever part came first, and the estimate was often 0 when a part with high values was added first (or after the last internal compress). This made `04950_distributed_plan_runtime_filter_delivery_from_join_stage` flaky on master: with `auto_statistics_types` containing `tdigest`, the 4 parallel-insert parts of `big` were committed in varying order, the planner estimated `v < 90000` as 0 rows in the failing runs and refused to transport the runtime filter (`100 estimated build keys vs 0 estimated site rows`). Compress after merging, so the merged digest is sorted whatever the part order. Digests loaded from parts are already compressed in `deserialize`, and `compress` mutates only the object `merge` is already mutating (the estimator's own `cloneEmpty` copy, or the gathered statistics of a merge). 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 review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261016-235500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261016-213900 |
|
Workflow [PR], commit [c4e7739] Summary: ✅
AI ReviewSummaryThe PR compresses merged Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
|
cc @hanfei1991 @CurtizJ, could you review this and add |
CI finish ledger - 79a7362Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
#126396 reverts #124131 on master, and #126397 re-lands it with the test adjusted. The only other Session id: cron:our-pr-ci-monitor:20261017-090009 |
|
@groeneai check the CI failure |
…atistics-merge-compress
|
The reds are not from this PR (it changes only
|
Backport #126385 to 26.10: Estimate range conditions from all parts of `tdigest` statistics
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Range conditions such as
x < 100on a column withtdigeststatistics are now estimated correctly on tables with several parts. Previously the estimate depended on the order of the parts and could be zero, so the planner could pick a worse join order, PREWHERE order or distributed plan.Description
04950_distributed_plan_runtime_filter_delivery_from_join_stagefailed on master in Stateless tests (amd_debug, distributed plan, s3 storage, parallel), and 37 of 69 reruns with the same randomized settings failed too. Those settings includeauto_statistics_typeswithtdigest. In every failing run the planner estimatedv < 90000onbig(90000 matching rows) as 0 rows and refused to transport the runtime filter. Every passing run estimated 89999.The estimator merges the
tdigeststatistics of all parts withQuantileTDigest::merge, which appends the other digest's centroids and sorts them only once more than 2048 are pending.getCountLessThanreads the centroids in order and returns 0 when the first one is already above the bound. So the estimate depended on which part came first, and that varies with the commit order of a parallel insert.StatisticsTDigest::mergenow compresses the digest after merging, so it is sorted whatever the part order. Digests loaded from parts are already compressed, and the other statistics merges do not depend on order.Validation: the new test
05406_statistics_tdigest_range_estimate_part_orderfails on master (high first 0) and passes with the fix (50/50 with randomized settings). A copy of 04950 that writes the lowest key range as the last part fails 20/20 on master and passes 20/20 with the fix. Planning on a table with 500 parts (debug build):EXPLAINmedian 6 ms before, 10 ms after; merge time unchanged.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126385]
Version info
26.11.1.271-master(included in26.11and later)26.10.5.59