Skip to content

Split dynamic PREWHERE filters off user predicates on the same column - #131511

Merged
leshikus merged 1 commit into
ClickHouse:masterfrom
olegkkruglov:issue-131263-prewhere-stats-fused-runtime-filter
Nov 14, 2026
Merged

leshikus merged 1 commit into
ClickHouse:masterfrom
olegkkruglov:issue-131263-prewhere-stats-fused-runtime-filter

Conversation

@olegkkruglov

@olegkkruglov olegkkruglov commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #131263
Related: #131075

The PREWHERE-effectiveness ProfileEvents (GranulesReadByPrewhereReaders,
GranulesSkippedByPrewhereReaders, RowsSkippedByPrewhereReaders) count a read
step only when it is entirely a user query predicate; a step carrying a
synthesized dynamic filter (the __topKFilter of ORDER BY ... LIMIT, a join
runtime filter __applyFilter) is marked is_query_prewhere = false and
excluded (#131075).

But tryBuildPrewhereSteps groups adjacent conjuncts that read the same columns
into one step. A user predicate on a join key (less(b, 8192)) and the runtime
filter __applyFilter on that same key were fused into one step, so the whole
step — user predicate included — was excluded, and the events reported 0 0 0
even though PREWHERE dropped 96% of the granules. Only __topKFilter escaped
this, because follows_top_k_filter already split it off (and only when
read_ahead_columns was set).

This replaces the __topKFilter-specific split rule with a symmetric one: a
synthesized dynamic filter and a user predicate never share a step, regardless
of read_ahead_columns. Each is then attributed on its own, and the shared
column is still read once through group_read_step. The symmetric rule subsumes
follows_top_k_filter and additionally fixes two cases the old rule missed: a
build-side-key predicate the planner pushes into the probe scan
(WHERE d.x < 8192), and a user conjunct on the ORDER BY column when the
caller passes read_ahead_columns = false.

Query results are unchanged — splitting only adds a step boundary; the per-step
filters are still combined with AND. Covered by a new case in
05572_prewhere_events_exclude_dynamic_filters.

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):

...


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

Version info

  • Merged into: 26.11.1.2316-master (included in 26.11 and later)

`tryBuildPrewhereSteps` groups adjacent PREWHERE conjuncts that read the same
columns into one read step. A user predicate on a join key (`less(b, 8192)`)
and the join runtime filter `__applyFilter` on that same key were therefore
fused into one step. Since ClickHouse#131075 a step carrying any synthesized dynamic
filter is marked `is_query_prewhere = false`, which excludes the whole step
from the PREWHERE-effectiveness events (`GranulesReadByPrewhereReaders`,
`GranulesSkippedByPrewhereReaders`, `RowsSkippedByPrewhereReaders`). The fused
step then reported `0 0 0` even though the user predicate dropped 96% of the
granules.

Replace the `__topKFilter`-specific `follows_top_k_filter` split rule with a
symmetric one: a synthesized dynamic filter and a user predicate never share a
step, regardless of `read_ahead_columns`. Each is attributed on its own; the
shared column is still read once through `group_read_step`. This also covers a
build-side-key predicate the planner pushes into the probe scan
(`WHERE d.x < 8192`) and a user conjunct on the `ORDER BY` column when the
caller passes `read_ahead_columns = false`, both of which the old top-K rule
missed.

Covered by a new case in `05572_prewhere_events_exclude_dynamic_filters`.

Closes: ClickHouse#131263
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 14, 2026
@leshikus leshikus added the can be tested Allows running workflows for external contributors label Nov 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bce5746]

Summary: ✅


AI Review

Summary

Separates synthesized dynamic filters from user predicates in tryBuildPrewhereSteps, restoring PREWHERE effectiveness counters when both predicates read the same columns. No actionable correctness, safety, or compatibility findings. The focused regression test 05572_prewhere_events_exclude_dynamic_filters passed in Fast test CI. Validation used source inspection and existing CI results; no local build or tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bce5746dd with master b05f92e01 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.59, programs/clickhouse-keeper ×0.68 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.4 s 7.1 s +5.0 s (+250%)
Compile time of recompiled translation units

7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline.

Job report

@leshikus leshikus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This PR puts the optimizer's filters (__topKFilter, __applyFilter) in a separate PREWHERE read step from the user's conditions on the same column, so the PREWHERE counters count only the user's conditions. Looks good.

@leshikus
leshikus added this pull request to the merge queue Nov 14, 2026
Merged via the queue into ClickHouse:master with commit 9ce63cc Nov 14, 2026
210 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 14, 2026
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Nov 15, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 16, 2026
…rewhere-attribution

Conflict in MergeTreeSplitPrewhereIntoReadSteps.cpp with ClickHouse#131511: keep its
`crosses_dynamic_filter_boundary` split rule, drop only `is_first_unattributed`
added by ClickHouse#122380.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 16, 2026
PedroTadim pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 20, 2026
Port the unconditional dynamic-filter boundary into `appendGroups`, preserving alias splitting, query-cache attribution, exception guards, and shared column reads.

Related: ClickHouse#129445
Related: ClickHouse#131511
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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 v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PREWHERE stats read 0 when join runtime filter fused with join-key WHERE

5 participants