Repository navigation
Do optimizePrewhere only after possible runtime filters were added - #95838
Merged
Merged
Conversation
davenger
marked this pull request as draft
February 2, 2026 17:50
Contributor
|
Workflow [PR], commit [2e31130] Summary: ❌
|
novikd
approved these changes
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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
Version info
26.2.1.200