Skip to content

Do optimizePrewhere only after possible runtime filters were added - #95838

Merged
davenger merged 5 commits into
masterfrom
move_prewhere_opt_after_rt_filters
Feb 3, 2026
Merged

davenger merged 5 commits into
masterfrom
move_prewhere_opt_after_rt_filters

Conversation

@davenger

@davenger davenger commented Feb 2, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

When read step already has PREWHERE filters a new filter cannot be added. This change postpones PREWHERE optimization until after JOIN runtime filter optimization so that runtime filters can be also pushed to PREWHERE.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.2.1.200

@davenger
davenger marked this pull request as draft February 2, 2026 17:50
@clickhouse-gh

clickhouse-gh Bot commented Feb 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2e31130]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) failure
Logical error: Block structure mismatch in A stream: different number of columns: (STID: 0993-2da1) FAIL cidb, issue ISSUE EXISTS
Stress test (amd_msan) failure
Logical error: Block structure mismatch in A stream: different number of columns: (STID: 0993-38e6) FAIL cidb, issue ISSUE EXISTS
Upgrade check (amd_msan) failure
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb IGNORED

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Feb 2, 2026
@davenger
davenger marked this pull request as ready for review February 3, 2026 12:35
@davenger
davenger requested a review from novikd February 3, 2026 12:36
@novikd novikd self-assigned this Feb 3, 2026
@davenger
davenger added this pull request to the merge queue Feb 3, 2026
Merged via the queue into master with commit 882b839 Feb 3, 2026
127 of 130 checks passed
@davenger
davenger deleted the move_prewhere_opt_after_rt_filters branch February 3, 2026 16:44
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 3, 2026
pull Bot pushed a commit to AnotherGenZ/ClickHouse that referenced this pull request Sep 21, 2026
A join runtime filter (`enable_join_runtime_filters`, on by default) plants an
`__applyFilter(_runtime_filter_<hash>, key)` filter above a MergeTree read.
Since ClickHouse#95838 the PREWHERE optimization runs after runtime filters are pushed
down, so `optimizePrewhere` moves that filter into the read's PREWHERE while a
conjunct it cannot move (one referencing every queried column, e.g. a view's
HAVING) stays behind in a residual `FilterStep`.

`updateQueryConditionCache` then tags that residual filter with a hash taken
from `query_info.filter_actions_dag`, which does not contain the runtime filter.
The existing guard on the filter step cannot notice: `splitAndFillPrewhereInfo`
rebuilds the residual expression with `__applyFilter` as an *input* column, and
`isDeterministicAllowingTopKFilter` returns true for an input node. At execution
the PREWHERE empties a granule, the tagged filter sees zero rows for it, and
`FilterTransform::writeIntoQueryConditionCache` records "no row matches <the
WHERE condition>" for a granule that does contain matching rows. Later plain
reads of the same view silently return fewer rows; nothing is logged.

Gate the tag on the read step's PREWHERE with the same predicate the filter step
already uses. A deterministic PREWHERE is part of `filter_actions_dag`, so the
recorded key covers it; `__topKFilter` is allowed on purpose and its key is
salted with the TopK plan; a join runtime filter is neither, because its
contents are the build side's key set for one particular execution. Such a read
therefore stops populating the cache (it still consults it), which is why the
key cannot instead be salted the way the TopK filter's is.

`FormatFilterInfo` keys the same cache for reads served by a data format, and
had the identical hole: it hashes `filter_actions_dag` alone while storing the
PREWHERE the plan ends up with. `optimizePrewhere` is generic over
`SourceStepWithFilter`, so a runtime filter reaches a Parquet-backed read's
PREWHERE too, and `getMatchedBuckets` derives unmatched row groups from the
post-PREWHERE row count - a whole row group then gets recorded as matching
nothing. It gets the same gate, which covers `File`, `url()` and object storage
through the one shared constructor. There it can use plain determinism, because
`optimizeTopK` installs `__topKFilter` only through a `ReadFromMergeTree *`.

Found by the `NoREC` oracle of `SQLancerPP (arm_release)` on master
f5ca648: a view read twice with a predicate
true for every non-NULL row answered 6 rows one way and 5 the other.

Locally, 500k rows and a one-key dimension table lose 9362 of 71428 rows
(62066 vs 71428) of a `MergeTree` view, and a ten-row-group `File(Parquet)`
table loses all 1429 matching rows (0 vs 1429), on the parent commit; both
agree with the fix.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements 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