Repository navigation
Cherry pick #110695 to 26.3: Combine I/O cost with selectivity in PREWHERE condition ordering - #126934
Merged
robot-ch-test-poll4 merged 8 commits intoOct 21, 2026
Conversation
When `use_statistics=1` (the default since `auto_statistics_types` was introduced), the PREWHERE optimizer sorted conditions by `estimated_row_count` alone, with `columns_size` only as a tiebreaker. This caused expensive conditions (e.g. Map column, ~500KB) to be placed before cheap ones (e.g. scalar column, ~1KB) whenever the expensive condition appeared more selective — ignoring the I/O cost difference. Apply the classic conjunctive filter ordering rule: sort by `cost / (1 - selectivity)`, i.e. the I/O cost per rejected row. This is computed as `columns_size / max(1, total_rows - estimated_row_count)` and replaces the separate `estimated_row_count, columns_size` pair in the condition comparison tuple. When statistics are unavailable (`estimated_row_count=0`, `total_rows=0`), the formula degrades to `columns_size`, preserving the existing behavior. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…re-cost-with-selectivity
…ilable The new `cost_with_selectivity` score is `columns_size / max(1, total_rows - estimated_row_count)`. On compact parts per-column compressed sizes are not tracked, so `columns_size` is 0 and the score collapses to 0 for every condition. Since this score is the only selectivity-bearing key in the sort tuple, all conditions tie and the original WHERE order is kept, discarding the selectivity estimate entirely. Fall back to `estimated_row_count` when `columns_size == 0` so ordering by selectivity is preserved. On wide parts (sizes known) the cost formula is unchanged. This fixes the `02864_statistics_usage` and `04266_statistics_basic_prewhere` fast-test failures. Adds `04811_prewhere_statistics_ordering_compact_parts` to cover the compact-part case. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The cost-based PREWHERE ordering score divides by columns_size (compressed column size), so the condition order depends on part type and serialization - settings CI randomizes. This made several prewhere-ordering tests flaky. Pin the selectivity-oriented tests (04266, 04304, 03580, 04053) to compact parts, where per-column sizes are 0 and ordering falls back to selectivity alone, independent of size-affecting randomization. For 04513 (which must stay wide to exercise the cost path) split `modality` 50/50 so the reject-count ratio is ~2x while the Map column dwarfs the scalar, keeping cheap-filter-first stable. Verified with clickhouse-test --test-runs 100 -j 10: all affected tests pass 100/100 under randomized settings. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The cost-per-rejected-row score used max(1, total_rows - estimated_row_count) as the denominator, so a predicate that rejects no rows (estimated_row_count == total_rows) got a finite score equal to columns_size. A cheap tautology could then be scheduled before a genuinely selective but expensive predicate, which regresses the old estimated_row_count ordering. Give such predicates an infinite score so they always sort last, and keep the explicit no-statistics (total_rows == 0) and compact-part (columns_size == 0) fallbacks. Adds 04812_prewhere_statistics_tautology_last. Addresses review comment: #110695 (comment) Co-Authored-By: Claude Opus 4.8 <[email protected]>
countmin statistics are not available in the fast-test build (hence the other countmin tests are tagged no-fasttest). The tautology test does not need it: tdigest on `cheap` drives the tautology detection, and `expensive = 'absent'` stays selective via the default equality selectivity. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…vity Combine I/O cost with selectivity in PREWHERE condition ordering
Resolve the conflicts of backporting #110695 ("Combine I/O cost with selectivity in PREWHERE condition ordering") to 26.3. The release branch has no grouping of PREWHERE conjuncts by column set (#106337), so: - `cost_with_selectivity` is computed per conjunct in `analyzeImpl`, right before the condition is added. - The early return of `analyze` for disabled reordering is not taken: it belongs to the grouping, and every conjunct already gets its cost in `analyzeImpl`. - `Condition::toString` keeps the single `node`. - Tests 04053, 04266 and 04304 cover features absent on 26.3 and stay deleted. - The new tests drop `explain_query_plan_default`, which 26.3 does not have; its `EXPLAIN` output is the legacy one already. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #110695
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job