Skip to content

MergeTreeWhereOptimizer: group PREWHERE conjuncts by column set before selectivity estimation - #106337

Merged
hanfei1991 merged 6 commits into
ClickHouse:masterfrom
hanfei1991:prewhere-group-by-column-set
Jun 23, 2026
Merged

hanfei1991 merged 6 commits into
ClickHouse:masterfrom
hanfei1991:prewhere-group-by-column-set

Conversation

@hanfei1991

@hanfei1991 hanfei1991 commented Jun 2, 2026 •

Copy link
Copy Markdown
Member

MergeTreeWhereOptimizer::analyzeImpl split the WHERE AND-chain into individual conjuncts and estimated each one's selectivity in isolation. For a query like

  WHERE a > 2500 AND b = 1 AND a < 2502                                                                                                                                                                                                                                        

with a uniformly distributed over [0, 5000) and b having 200 distinct values, the estimator saw:

  • a > 2500 → ~50% selectivity
  • a < 2502 → ~50% selectivity
  • b = 1 → 0.5% selectivity

Because both a-conditions were individually less selective than b = 1, the optimizer placed b = 1 first in PREWHERE. But the combined range a ∈ (2500, 2502) selects only a = 2501 — ~0.02% selectivity — which is far better than b = 1. The a-conditions should be
evaluated first.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The PREWHERE optimizer now groups conjuncts that reference the same column set
before estimating selectivity, so that conditions like a > 2500 AND a < 2502
are evaluated together as a combined range (~0.02% selectivity) rather than as
two independent predicates (~50% each). This produces a more accurate ordering
of PREWHERE conditions when column statistics are available.

Version info

  • Merged into: 26.6.1.1143

@clickhouse-gh

clickhouse-gh Bot commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [50c3f0a]

Summary: ✅


AI Review

Summary

This PR changes MergeTreeWhereOptimizer to group viable AND conjuncts by exact referenced column set before selectivity estimation, so same-column range predicates are ranked together for PREWHERE while preserving the no-reorder path. I did not find an unresolved correctness or test-coverage issue after checking the diff, current code, prior inline threads, and the Praktika report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 2, 2026
}
};

/// Move condition and all other conditions depend on the same set of columns.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of code will group conditions by columns set. This PR do the grouping before selectivity estimation so this code can be removed

@hanfei1991
hanfei1991 marked this pull request as ready for review June 3, 2026 12:33
@hanfei1991
hanfei1991 force-pushed the prewhere-group-by-column-set branch from e7944a1 to ce739a6 Compare June 18, 2026 15:43
@murphy-4o murphy-4o self-assigned this Jun 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.60% 92.50% -0.10%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 152/171 (88.89%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 37 line(s) · Uncovered code

Full report · Diff report

@hanfei1991
hanfei1991 added this pull request to the merge queue Jun 23, 2026
Merged via the queue into ClickHouse:master with commit e692fa1 Jun 23, 2026
328 of 330 checks passed
@hanfei1991
hanfei1991 deleted the prewhere-group-by-column-set branch June 23, 2026 09:27
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 23, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…y-column-set

MergeTreeWhereOptimizer: group PREWHERE conjuncts by column set before selectivity estimation
Avogar added a commit that referenced this pull request Oct 21, 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants