Skip to content

Cherry pick #128582 to 26.10: Combine the filters of PREWHERE steps when the later conditions cannot throw - #133584

Merged
robot-ch-test-poll3 merged 17 commits into
backport/26.10/128582from
cherrypick/26.10/128582
Nov 22, 2026
Merged

robot-ch-test-poll3 merged 17 commits into
backport/26.10/128582from
cherrypick/26.10/128582

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #128582

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 16 commits November 2, 2026 00:36
…Throw`

Same change as #125978 (fixes #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 #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 #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 #128442 and #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 #128342, which needs it for Parquet; it drops out when
either PR is merged first.

Found, analysed and measured by @ snelheidai in #128442 and #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]>
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]>
# Conflicts:
#	src/Storages/MergeTree/MergeTreeSplitPrewhereIntoReadSteps.cpp
Combine the filters of `PREWHERE` steps when the later conditions cannot throw
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Nov 22, 2026
Resolve the conflicts with `26.10`:
- `FilterStep.cpp`: keep the `allReachableNodes` / `anyReachableNode` walks of `26.10`, which already visit every node once, so the `visited` sets of the original change are not needed. Drop `static` from `canBeMovedToTheFrontOfAndChain`, which the original change declares in `FilterStep.h`.
- `05259_short_circuit_reorder_arguments.sql`: take the shortened comment of the original change and keep `SET enable_analyzer = 1;`, which `26.10` has before that query.
@robot-ch-test-poll3
robot-ch-test-poll3 merged commit 11e31aa into backport/26.10/128582 Nov 22, 2026
2 checks passed
@robot-ch-test-poll3
robot-ch-test-poll3 deleted the cherrypick/26.10/128582 branch November 22, 2026 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants