Repository navigation
Keep the primary key conditions at the end of PREWHERE next to a condition that cannot use the key - #131360
Conversation
…ition that cannot use the key `move_primary_key_columns_to_end_of_prewhere` keeps the conditions on a contiguous primary key prefix at the end of PREWHERE, computed from the key positions of the conditions. `analyzeImpl` groups conditions by column set and gives each group one key position, which is "none" as soon as one member cannot use the key. So a condition on a key column that cannot use the key (a join runtime filter `__applyFilter(rf, key)` on the join key, `length(key) = 2`, `key LIKE '%1'`) joined the group of `key IN (...)`, the prefix lost position 0, and every key condition fell back to the cost order: `PREWHERE` could first run a condition that keeps almost all rows (`c2 = 'e3'` before `c9 = 'i9'` on the second query of ClickHouse#131131). A conjunct now joins a group only if it agrees with it on whether it can use the primary key. Groups that keep a condition that may throw behind its guard are not split; the key prefix counts each of their members that can use the key. Related: ClickHouse#131131 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; 2 findings, then 0 on the fix round) plus my own review (4 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261113-153700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261113-152500 |
|
Workflow [PR], commit [fe1775a] Summary: ✅
AI ReviewSummaryThe PR preserves the primary key prefix at the end of Missing context / blind spots
Final VerdictStatus: ✅ Approve. No review changes required. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline. |
977d79f
Backport #131360 to 26.10: Keep the primary key conditions at the end of PREWHERE next to a condition that cannot use the key
Related: #131131
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A condition on a primary key column that cannot use the primary key, such as a join runtime filter on the join key or
key LIKE '%x', no longer moves the other primary key conditions out of the end ofPREWHERE, so the more selective conditions run first. The second query of #131131 withenable_join_runtime_filters = 1is about 1.4x faster.Description
Second query of #131131 (#131131 (comment)): with
enable_join_runtime_filters = 1,PREWHEREont2 ORDER BY (c1, c2, c7)first ranc2 = 'e3', which keeps 99% of the rows, and only laterc9 = 'i9', which rejects all of them.move_primary_key_columns_to_end_of_prewherekeeps the conditions on a contiguous key prefix at the end ofPREWHERE. Since #106337,MergeTreeWhereOptimizer::analyzeImplgroups conditions by column set and gives each group one key position, which is none when a member cannot use the key. The runtime filter__applyFilter(rf, c1)reads onlyc1, so it joined the group ofc1 IN (...), key position 0 was lost, and no key condition stayed at the end.key IN (...) AND length(key) = 2orAND key LIKE '%1'does the same.Change, in
analyzeImplonly:Validation: the new test prints
0 0 0before and1 1 1after (runtime filter,length(key),position(key)guard group); 50/50 randomized runs. New perf test (the issue's query, 60M rows), local releaseperf.py: PR / master = 0.69-0.71. On the issue's data (120M rows, release, n=10): 0.700 s -> 0.478 s with runtime filters (0.450 s without), reading 2.69 GB instead of 4.48 GB; results identical.05056_pr_local_plan_runtime_filter_read_mode: its EXPLAIN now prints the remote branch's filter as one expression (display only), andallow_reorder_prewhere_conditionsis clamped to 1 there.The first query of #131131 is still not reproduced as a slowdown on OSS builds.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131360]
Version info
26.11.1.2272-master(included in26.11and later)26.10.5.26