Skip to content

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 into
backport/26.3/110695from
cherrypick/26.3/110695
Oct 21, 2026
Merged

robot-ch-test-poll4 merged 8 commits into
backport/26.3/110695from
cherrypick/26.3/110695

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

Avogar and others added 7 commits July 16, 2026 13:12
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]>
…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
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Oct 20, 2026
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]>
@robot-ch-test-poll4
robot-ch-test-poll4 merged commit 106f70b into backport/26.3/110695 Oct 21, 2026
1 check passed
@robot-ch-test-poll4
robot-ch-test-poll4 deleted the cherrypick/26.3/110695 branch October 21, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants