Repository navigation
Combine the filters of PREWHERE steps when the later conditions cannot throw - #128582
Conversation
…Throw` Same change as ClickHouse#125978 (fixes ClickHouse#125919). Co-authored-by: Shoryamishra61 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
Moved unchanged so that the MergeTree PREWHERE splitter can use the same contract. No behaviour change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ated on more rows With `query_plan_merge_filters = 1` (the default since ClickHouse#71539) every PREWHERE read step of a MergeTree read applied its filter to the whole block, to keep short-circuit evaluation of merged filters. So a step that keeps almost all rows still copied every column through its filter. The forced filter is now skipped when the next step's conditions (and, before the last step, the outputs computed in the last step) pass `canBeMovedToTheFrontOfAndChain`, the reader neither converts nor fills a PREWHERE column (`canReadPrewhereColumnsAhead`), and the last step filters. The step filters are then combined and applied together by `ReadResult::optimize`. `groups_requiring_filtered_input` is unchanged. `system.predicate_statistics_log` now counts the rows that passed all steps so far after `optimize`, so `passed_rows` and `total_passed_rows` stay the rows surviving a step when an earlier filter was combined but not applied (also with `query_plan_merge_filters = 0`). Found, analysed and measured by @ snelheidai in ClickHouse#128442. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The same loop over the children of an `ActionsDAG` node was copied in `ExpressionActions.cpp`, `actionsDAGUtils.cpp`, `FilterStep.cpp`, `lazyFrontier.cpp` and the PREWHERE splitter. It is now one function, `getDataTypesWithConstInfoFromNodes`, next to `ActionsDAG`. No behaviour change: `ActionsDAG::Node::column` is a `ColumnConstPtr`, so the `isColumnConst` check of `lazyFrontier.cpp` was always true when the column was set. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A comparison with a constant string was classified by the types only, so `d > '2010-01-01'` with `d Date` counted as able to throw, like a comparison of a `Date` column with a `String` column, which parses every row. But the constant is converted to the type of the other argument once, before any row is compared (`executeWithConstString`), and then values of that type are compared. `canThrowIfKnown` now repeats that conversion, with the same function, format settings and unwrapping of `Nullable` and `LowCardinality`, and asks `comparisonCanThrow` about the converted type. A string that does not convert still counts as able to throw. `DataTypeWithConstInfo` gets the constant column, set by `getDataTypesWithConstInfoFromNodes`. Callers that do not know the value pass nothing and get the previous answer. So such a comparison can be moved in front of a condition that throws (`short_circuit_function_evaluation_reorder_arguments`), and, with the previous commit, a PREWHERE step before it does not have to filter its block. Found, analysed and measured by @ snelheidai in ClickHouse#128442 and ClickHouse#128454. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s cannot throw They read only the sizes or the bytes of the values. `URL <> ''` is rewritten to `notEmpty(URL)` (`optimize_empty_string_comparisons`), so without this a PREWHERE step before it keeps filtering its block. Same change as in ClickHouse#128342, which needs it for Parquet; it drops out when either PR is merged first. Found, analysed and measured by @ snelheidai in ClickHouse#128442 and ClickHouse#128454. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…re_combine_filters` Once an earlier PREWHERE step has filtered, the reader filters a column read by a later step by the filter so far and then by that step's own filter, so the selected `String` of the last step is copied twice. With the filters combined it is copied once. The query selects `s`, so it is read in full (`s != ''` alone would read only `s.size`), and its conditions are those of ClickBench Q37: cheap ones, a `Date` compared with a string and a non-empty string. So the gain needs both the combined filters and the classification of the comparison and of `notEmpty` as unable to throw. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ombine_filters` A column that a PREWHERE step reads and that is needed after PREWHERE stays in the block, so every later step that filters copies it again. With the filters of the steps combined it is copied once. The previous query read `s` in the last step only, where this saves a single copy. `s` is now read by the second of five steps and selected, so it is read in full (`s != ''` alone would read only `s.size`). The step before `s != ''` skips its filter only with the `notEmpty` classification, the step before `d >= '2020-01-06'` only with the classification of a comparison with a constant string. Co-Authored-By: Claude Opus 5.5 <[email protected]>
These walks pushed every child, so a node shared by several parents was visited once per path, which grows exponentially with the depth of a DAG whose levels reuse a column (for example nested subqueries merged into one filter). `canBeMovedToTheFrontOfAndChain` now also runs for the steps of a PREWHERE. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…emark The constant 'abc' is rejected while the query is planned, also on master, so the arm of 05481 could not tell whether the comparison is classified as able to throw. notEmpty now describes whether it can throw. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Without that check the query also passes when the PREWHERE is evaluated as one step, where the counter of a deferred step filter is not exercised. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the obvious comment on getDataTypesWithConstInfoFromNodes and shorten three comments on the deferred PREWHERE step filters to one line each. 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; 3 rounds, 4 findings, the last round none) plus my own review (3 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261102-063400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261102-034300 |
|
cc @KochetovNicolai @Algunenano, could you review this and add the |
|
Workflow [PR], commit [aba6973] AI ReviewSummaryCombines Missing context / blind spots
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 units3368 translation units recompiled, 18971 s compile time in total, 3366 of them have a recent master baseline. Translation units without a recent master baseline:
|
Keep `getDataTypesWithConstInfoFromNodes` file local and `canBeMovedToTheFrontOfAndChain` in `FilterStep.cpp` (declared in `FilterStep.h`), so the other callers keep their argument lists and the classification of comparisons with a constant string does not reach `canBeExecutedOnMoreRows` and `canEvaluateOnPaddedRows`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@copilot resolve the merge conflicts in this pull request |
# Conflicts: # src/Storages/MergeTree/MergeTreeSplitPrewhereIntoReadSteps.cpp
A conjunct on the top-K column that `canBeMovedToTheFrontOfAndChain` used to share the `__topKFilter` step, so it ran on every row of the block, while in WHERE it ran only on the rows the threshold passed: `SearchPhrase != 'zzzz'` (passes every row) was slower with the previous commit alone. With its own step the `__topKFilter` step's filter stays optional (ClickHouse#128582), so it is applied only when it keeps < 60% of a block: while the threshold passes most rows both conditions run on the whole block as before, once it is selective the condition runs only on the surviving rows. Its column is read by the `__topKFilter` step. Only with `read_ahead_columns`: without it ClickHouse#128582 forces the filter, and a column-free step of a non-adaptive part would read its column again. The losing shape is added to the performance test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #128582 to 26.10: Combine the filters of `PREWHERE` steps when the later conditions cannot throw
Backport #128582 to 26.10: Combine the filters of `PREWHERE` steps when the later conditions cannot throw
Closes: #128442
Closes: #128454
Related: #125978
Related: #128342
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster
PREWHEREwith several conditions: the block is filtered once instead of after every condition when the later conditions cannot throw. ClickBench Q38 is up to 10% faster.Description
With
query_plan_merge_filters = 1every PREWHERE read step filters its block, so a later condition never sees the rows an earlier one rejected. When the steps keep most rows, this copies every column once per step.Core change, in
tryBuildPrewhereSteps: a step skips its filter when the next step's conditions passcanBeMovedToTheFrontOfAndChain, the reader neither converts nor fills PREWHERE columns, and the last step filters.ReadResult::optimizethen applies the combined filter once.Enablers, so that common conditions qualify:
EventDate >= '2013-07-01').empty/notEmptycannot throw (URL <> '').Tuplecomparisons), which this relies on; it drops out if that PR lands first.Also:
predicate_statistics_logcounts the rows that passed all steps so far, so its numbers stay right when a filter is deferred.TODO: only conditions whose functions describe that they cannot throw (
canThrowIfKnown) qualify; any other function keeps the step filter. A follow-up could defer every step filter instead and, if a step throws, apply the accumulated filter and evaluate it again, so the plan-time check reduces to stateless, deterministic, no side effects.Measured locally (one-part
hits,max_threads = 4, 20 alternating pairs): Q38 -10.1%, Q43 -5.5%, Q37 -4.7%, Q42 -3.5%, same memory.Analysis and patches by snelheidai.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128582]
Version info
26.11.1.1486-master(included in26.11and later)26.10.11.9