Skip to content

Make the primary key exclusion search cheap when the filter has non-key conditions - #123535

Merged
CurtizJ merged 3 commits into
ClickHouse:masterfrom
CurtizJ:keycondition-unknown
Oct 7, 2026
Merged

CurtizJ merged 3 commits into
ClickHouse:masterfrom
CurtizJ:keycondition-unknown

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

An atom that KeyCondition cannot evaluate (e.g. a condition on a non-key column) is (true, true) on every range. So key_range AND non_key_condition is never certainly true, and the generic exclusion search splits every matching range down to single marks, one check per mark.

For this search, markRangesFromPKRange now uses a copy of the condition where such atoms are replaced with true (false under an odd number of NOTs). The copy has the same can_be_true, so the same ranges are excluded, but the search stops where nothing more can be excluded. Its can_be_false is not exact, so it is not used when exact ranges are collected.

Changelog category (leave one):

  • Performance Improvement

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

Improved performance of primary index analysis in queries that filter both by the primary key and by other columns, for example, WHERE timestamp BETWEEN ... AND level = 'error' where level is not in the primary key.


Workflow [PR]
Sync PR [sync-upstream/pr/123535]

Version info

  • Merged into: 26.10.1.1780-master (included in 26.10 and later)

…ey conditions

`KeyCondition::checkInHyperrectangle` evaluates an atom it cannot analyze
(`FUNCTION_UNKNOWN`, e.g. a condition on a column that is not part of the key)
as `(true, true)` on every range. `and(range, unknown)` is then never certainly
true, so the generic exclusion search split every range that may match down to
single marks, although such an atom cannot exclude any of them.

`KeyCondition::createWithUnknownAtomsAssumedTrue` returns a copy where every
unknown atom is replaced with `ALWAYS_TRUE`, or with `ALWAYS_FALSE` under an odd
number of `NOT`s. The copy has the same `can_be_true` on every range, so it
excludes the same ranges, and it is certainly true on a range where no subrange
can be excluded, so the search stops splitting there. Its `can_be_false` is not
exact, so `markRangesFromPKRange` uses it only for the generic exclusion search
without exact ranges, and only if the condition has unknown atoms.

On a part with 781k marks, a key range combined with a non-key condition takes
0.013 s instead of 0.38 s, the same as the key range alone, with the same
selected marks. On 5000 small parts the primary key analysis takes 14.7 ms
instead of 23.7 ms; a condition without unknown atoms is not affected.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2200d0d]

Summary: ✅


AI Review

Summary

This PR teaches markRangesFromPKRange to substitute FUNCTION_UNKNOWN atoms with truth-preserving constants when only can_be_true matters, which should stop the generic exclusion search from descending to single marks on filters that mix primary-key and non-key predicates. I did not find a correctness bug in the current code, but I still cannot approve a Performance Improvement PR without a focused before/after measurement for the new benchmarked case.

Tests
  • ⚠️ tests/performance/primary_key_exclusion_search_non_key_conditions.xml:18 adds the exact workload this PR claims to speed up, but the PR still does not include one focused master vs PR measurement for it. The broad perf jobs on this PR do not cover the intended path well enough, so please add a small targeted run here — median wall time is fine, and IndexGenericExclusionSearch* step counts would also be enough — to show that the new search actually gets cheaper on the scenario this change targets.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: add one focused before/after result for the new exclusion-search benchmark, or an equivalent step-count measurement, so the Performance Improvement claim is backed by data.

LLVM Coverage Report

Measured on commit 2200d0d.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 67/71 (94.37%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 2, 2026
@nihalzp nihalzp self-assigned this Oct 2, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 32 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 354ca187-3c67-4547-888b-f6da8564b869
  • MIRAI run: 118258ad-62ef-42ef-a044-fca4f0fa4d8a
  • PR check IDs:
    • clickbench_203479_1791387543
    • clickbench_203485_1791387543
    • clickbench_203491_1791387543
    • tpch_adapted_1_official_203502_1791387543
    • tpch_adapted_1_official_203513_1791387543
    • tpch_adapted_1_official_203549_1791387543

@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2200d0de8 with master 2963d7f2b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

356 translation units recompiled, 3771 s compile time in total, 356 of them have a recent master baseline.

Job report

@CurtizJ
CurtizJ requested a review from nihalzp October 6, 2026 09:35
@CurtizJ

CurtizJ commented Oct 7, 2026 •

Copy link
Copy Markdown
Member Author

Synthetic perf test:

Screenshot 2026-10-07 at 17 59 04

@CurtizJ
CurtizJ added this pull request to the merge queue Oct 7, 2026
Merged via the queue into ClickHouse:master with commit 4f656cf Oct 7, 2026
151 of 152 checks passed
@CurtizJ
CurtizJ deleted the keycondition-unknown branch October 7, 2026 22:14
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 2026
alexey-milovidov added a commit that referenced this pull request Oct 8, 2026
…rand-overflow-117237

Conflicts in `KeyCondition.cpp` resolved in favor of `master`: #123535 and #117355 made
`KeyCondition::applyMonotonicFunctionsChainToRange` treat any non-fatal exception at a range endpoint
as "cannot analyze" and `applyFunction` cache a failure sentinel for every exception. That subsumes
this PR's `DECIMAL_OVERFLOW`-only handling, so the PR's `applyMonotonicFunctionsChainToRangeImpl`
split is dropped from `KeyCondition.cpp` and `KeyCondition.h`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... pr-performance Pull request with some performance 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