Repository navigation
Stop re-filtering columns that no later PREWHERE step reads - #128452
alexey-milovidov merged 3 commits into
Conversation
…y evaluate defaults `ReadResult::additional_columns` holds the columns each PREWHERE step projects out. Since ClickHouse#111795 they are carried across all later steps, so every later step filters them again in `ReadResult::applyFilter`. Their only consumer is `MergeTreeReadersChain::evaluateMissingDefaults`, which runs only when a later reader's `fillMissingColumns` leaves a requested column unfilled: a column with a default expression, or a subcolumn whose parent is available. `mayLeaveColumnToMissingDefaults`, next to `fillMissingColumns` and built from the same `hasDefault`, is a superset of those two cases. `IMergeTreeReader::mayEvaluateMissingDefaults` applies it to the requested columns. The chain asks every reader but the first one (it has no earlier step) when it is built, once per read task, and the reader caches the answer because it is reused by the consecutive read tasks of a part: without the cache the check took 1.7% of ClickBench Q3 in the reporter's profile. Only the steps before the last reader that may evaluate defaults keep the columns. A `chassert` in `evaluateMissingDefaults` checks the superset on every default evaluation. `executePrewhereActionsAndFilterColumns` takes `keep_additional_columns` instead of `is_last_reader`, whose only use there was this decision. ClickHouse#118630 introduces the same parameter for the same decision. Found and analysed by @ snelheidai in ClickHouse#128432. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g later uses it ClickHouse#71539 (8c2e541) replaced the by-name removal of intermediate filter columns with a check on `Step::original_node`, but never assigned it. So every step but the last kept its condition result until the end of the read, and each later step filtered it again. Assign `original_node` for a single-condition step; the existing `node_to_step` / `all_outputs` check then decides whether a later step or an output of the PREWHERE still uses it. The AND of several conditions of one step is a node the steps create, nothing else references it, so it is always removed. A condition that is a stored column stays: a later step may read it as the parent of a subcolumn missing in the part. Removing it too returned `f.null = 1` for `SELECT f.null ... PREWHERE f AND x = 1` while `MODIFY COLUMN f Nullable(UInt8)` is pending (0 with this change, as on master and with WHERE). This also drops the condition columns of the split PREWHERE on `Memory` (ClickHouse#124498). Tests: the pending-MODIFY query in `04628_multistep_prewhere_default_column`, and a step whose condition a later step reuses in `02559_multiple_read_steps_in_prewhere_reuse_computation`. Both queries pass on master as well; they fail if the stored-column or the later-use guard is dropped. Found and analysed by @ snelheidai in ClickHouse#128432. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the line on why mayEvaluateMissingDefaults is cached (a reader is reused by the consecutive read tasks of a part); it describes the new code's design. 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; 1 finding) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261101-053800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261101-023600 |
|
cc @KochetovNicolai @CurtizJ, could you review this and add the |
|
Workflow [PR], commit [db22610] Summary: ✅
AI ReviewSummaryThis PR reduces repeated filtering in multi-step Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units393 translation units recompiled, 4025 s compile time in total, 393 of them have a recent master baseline. |
054ef0c
…umn-117023 Conflict with #128452 (`keep_additional_columns` passed by the caller): keep master's decision, and additionally keep the projected-out columns when patch parts are read, because `applyPatchesAfterReader` evaluates `DEFAULT` columns again from them.
Master no longer reproduces the AMBIGUOUS_COLUMN_NAME: since ClickHouse#128452 an intermediate PREWHERE step removes its filter column when nothing later uses it, so the ON copy is gone before the WHERE copy with the same name is computed. The renaming in `tryBuildPrewhereSteps` is therefore dropped, and the file is restored to its version at the base of this branch (which also stops the PR from conflicting with ClickHouse#124553, which removed `columns` from that function). `05427` stays as a regression test: it passes on master 5b24a5f and fails on 26.10.2.103 with Code 352 in 8 of its 9 queries. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… runtime filter in the query condition cache" This reverts commit 64da8a0, reversing changes made to be89302. Conflicts resolved by keeping the later changes in the same lines: `is_query_prewhere` (ClickHouse#130645), the `last_reader_evaluating_defaults` argument (ClickHouse#128452), `readsSameColumns` (ClickHouse#124880), `follows_top_k_filter` (ClickHouse#129882), and the PREWHERE query condition cache write of ClickHouse#115522 in its own form before it merged ClickHouse#122380. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #128432
Related: #124498
Related: #118630
Related: #125670
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
PREWHEREsplit into several read steps no longer filters again, at each step, the columns that only earlier steps used (their inputs and the results of their conditions). ClickBench Q42 and Q43 are 12-14% faster.Description
Each step of a multi-step
PREWHEREfilters the whole read result, so every column in it is filtered again by every later step. Two kinds of them were never read again:ReadResult::additional_columns), kept for all later steps since Fix filling of a missing DEFAULT column with multiple PREWHERE read steps #111795, although onlyevaluateMissingDefaultsreads them, for a column with aDEFAULTor a subcolumn missing in the part;Step::original_nodebut never assigned it.Now the readers chain keeps the projected-out columns only up to the last reader that may evaluate defaults (
mayLeaveColumnToMissingDefaults, a superset of the casesfillMissingColumnsdefers), and a step removes its condition unless a later step or an output uses it, or it is a stored column (it can be the parent of a subcolumn missing in the part). The splitPREWHEREonMemorydrops the condition results too. #118630 changes the same decision: whichever lands second ORs its condition intokeep_additional_columns. A stored column named like a split condition (#124484) now fails withNOT_FOUND_COLUMN_IN_BLOCKon MergeTree instead of returning wrong values, and returns its values onMemory; #125670 fixes that class.Measured locally, master
c189bd99vs this PR (ThinLTO, 16 pinned cores, 10 alternating pairs): Q43 -14.2%, Q42 -11.9%, Q41 -9.1% at 4 threads; withuse_columns_cache = 1Q39 -16.8%, Q43 -13.6%;ReadResult::applyFilterfalls from 7.9% to 0.1% of Q43's CPU samples. The first query ofprewhere_combine_filters.xml: -25% and -28%. Nothing got slower.Correctness: 39 differential queries and 856 related stateless tests match master. Two queries added to existing tests fail if either removal guard is dropped.
Analysis and patch by @ snelheidai in #128432.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128452]
Version info
26.11.1.1052-master(included in26.11and later)