Skip to content

Filter step decouple folded predicate - #123293

Merged
KochetovNicolai merged 1 commit into
masterfrom
filter-step-decouple-folded-predicate
Oct 9, 2026
Merged

KochetovNicolai merged 1 commit into
masterfrom
filter-step-decouple-folded-predicate

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Related: #122952
Related: #105987
Related: #78166

A follow-up to #122952. The FilterStep constructor now folds a filter predicate that is constant through materialize also when the filter column is kept, so the pruning of unused columns does not fold at all any more.

#105987 folds such a predicate by replacing the filter output with the constant. That is safe only while the filter column is removed, so the constructor folded only in that case. A FilterStep that kept the column was folded later, by
the pruning of unused columns, once the column became removable. To do that, the pruning analysis worked on a folded clone of the DAG and carried a fold_filter_predicate flag to the step that applies it. ReadFromMergeTree did the
same for PREWHERE.

Now the constructor handles a kept filter column as well: the column stays an output as it was, the constant becomes a new output under a new name, and the filter reads and removes the constant. The output header does not change. When
nothing reads the kept column any more, the pruning removes it like any other unused column. So the folded clone, fold_filter_predicate and the fold in applyToOutputs are gone, and the analysis works on the DAG as it is.

The materialize that the fold is for comes from other optimizations, such as making the headers of UNION branches compatible, and it always reaches a filter through a FilterStep. An explicit PREWHERE over materialize was
folded only by the pruning of ReadFromMergeTree, and it is not folded any more. We do not expect that in real queries: a user who writes materialize in PREWHERE asks for the column to be materialized.
04926_fold_filter_through_materialize_prewhere is removed with it. The new test 05293_fold_filter_through_materialize_kept_filter_column covers the fold with a kept filter column.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Later


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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [43e8a05]

Summary: ✅


AI Review

Summary

This PR moves the materialize-through-constant fold for filters into the FilterStep constructor, keeps the original predicate column observable by introducing a hidden folded filter output, and removes the old prune-time clone-and-fold path. I reviewed the changed ActionsDAG / FilterStep logic, the affected pruning and pushdown callers, the new stateless coverage, and the existing PR discussion, and I did not find an unresolved correctness, safety, or significant performance issue in the current code.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 43e8a05.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 91.80% -0.30%
Branches 79.80% 79.70% -0.10%

Changed lines: Changed C/C++ lines covered: 51/54 (94.44%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 43e8a056363f9a5e29c89fe018a0e1c02b3416de was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

…structor only

The fold of a filter predicate that is constant through `materialize`
(#78166) replaced the filter output by the constant, so it was safe only
while the filter column was removed. A `FilterStep` with a kept column
was folded later, by the pruning of unused columns, once the column
became removable: the pruning analysis worked on a folded clone of the
DAG, carried `fold_filter_predicate` to the application, and the same
happened for PREWHERE in `ReadFromMergeTree`.

Now the constructor folds a kept filter column as well: the column stays
an output as it was, the constant becomes a new output under a new name,
and the filter reads and removes the constant. The output header does not
change, and once nobody reads the kept column, removing unused columns
drops it like any other. So the pruning does not fold at all any more:
`fold_filter_predicate`, the folded clone and the fold in
`applyToOutputs` are gone.

The `materialize` that the fold is for comes from other optimizations,
such as making the headers of `UNION` branches compatible, and always
reaches a filter through a `FilterStep`. An explicit `PREWHERE` over
`materialize`, which only the pruning of `ReadFromMergeTree` folded, is
not folded any more, and `04926_fold_filter_through_materialize_prewhere`
is removed with it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@KochetovNicolai
KochetovNicolai force-pushed the filter-step-decouple-folded-predicate branch from b288c64 to 43e8a05 Compare October 7, 2026 13:59
EXPLAIN PLAN actions = 1
SELECT count()
FROM prewhere_materialize_fold
PREWHERE materialize(1) = 0

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason the test is removed: I don't care about the case when user specified such a condition manually. The issue was with the optimizations, e.g. when predicate-push-down over UNION took materialized which was needed to fix constantness.

@KochetovNicolai
KochetovNicolai marked this pull request as ready for review October 7, 2026 14:07
@KochetovNicolai KochetovNicolai changed the title [WIP] Filter step decouple folded predicate Filter step decouple folded predicate Oct 7, 2026
@yariks5s yariks5s self-assigned this Oct 7, 2026
@KochetovNicolai
KochetovNicolai added this pull request to the merge queue Oct 9, 2026
Merged via the queue into master with commit b92d7fc Oct 9, 2026
298 checks passed
@KochetovNicolai
KochetovNicolai deleted the filter-step-decouple-folded-predicate branch October 9, 2026 10:56
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-not-for-changelog This PR should not be mentioned in the changelog 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