Repository navigation
Fix query condition cache misuse for folded constants - #90692
Merged
Merged
Conversation
Contributor
|
Workflow [PR], commit [8f0f16e] Summary: ❌
|
Member
nickitat
reviewed
Nov 25, 2025
When constants in filters are folded from expressions containing non-deterministic functions, the resulting constant should retain non-deterministic property in order to prevent query condition cache being used for such filter conditions. This patch adds `is_deterministic` property to the `ConstantNode` and propagates it across `ActionsDAG` nodes, which are later used to determine if filtering result should be written to query condition cache.
Unlike other constants (txn counters, server constants), math constant functions are deterministic and always allow constant folding since they will return the same value for all shards.
korowa
force-pushed
the
fix-qcc-folded-const
branch
from
November 26, 2025 07:11
95e1acc to
434c2fe
Compare
nickitat
reviewed
Nov 26, 2025
| column.column = std::move(column_set); | ||
|
|
||
| actions_stack[0].addConstantIfNecessary(column.name, column); | ||
| actions_stack[0].addConstantIfNecessary(column.name, column, /*is_deterministic=*/ true); |
Member
There was a problem hiding this comment.
What if we have a condition like col IN (1, 2, 3, shardNum())?
Contributor
Author
There was a problem hiding this comment.
That's not supported (checked some random versions from head back to 24.6 and they all have this behavior)
Member
There was a problem hiding this comment.
Let's maybe then add an assert
Contributor
Author
There was a problem hiding this comment.
Perhaps the following will be fine:
- Set it to
trueonly for deterministic contstants. In case ofIN (1, 2, shardNum())the node will have type FUNCTION, so it won't use query condition cache. Set::isDeterministicessentially is a duplicate ofActionsDAG::Node::isDeterministic, so we can fully rely on the last one, assuming that it's fixed at actions planner level (It's last commit, can revert it if this method is still needed)
It is unneeded since there is already isDeterministic method for COLUMN actions.
korowa
force-pushed
the
fix-qcc-folded-const
branch
from
November 28, 2025 09:12
ad8126f to
94a3b93
Compare
nickitat
approved these changes
Nov 28, 2025
nickitat
enabled auto-merge
December 1, 2025 12:12
Member
|
|
qiuyanjun888
pushed a commit
to qiuyanjun888/ClickHouse
that referenced
this pull request
Oct 16, 2026
With `query_plan_convert_join_to_in = 1`, `buildInConversion` adds the right side's `FutureSetFromSubquery` to the left read's filter as a constant column with the default `is_deterministic_constant = true`. Every query condition cache writer and reader trusts that flag, while the exact-mode DAG hash covers only the fixed name "set column" and not the set's contents. So two queries with the same static conjuncts but different sets (or the same query after the right table changed) share one cache entry, and the second query is served the first one's granule mask: rows are silently dropped. ClickHouse#90692 moved set determinism from `FutureSetFromSubquery::isDeterministic` onto the DAG node flag and set it to false for the analyzer's `IN (subquery|table)` sets only; this optimization was not updated, so the regression is present since 25.12. Mark the converted set non-deterministic, like the analyzer does, so these filters are neither written to nor read from the cache. Co-Authored-By: Claude Opus 5.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):
Fix issue where queries with filter predicates containing constants folded from non-deterministic functions (e.g.
shardNum()) could incorrectly use query condition cache.Documentation entry for user-facing changes
Details
When constants in filters are folded from expressions containing non-deterministic functions, the resulting constant should retain non-deterministic property in order to prevent query condition cache being used for such filter conditions.
This patch adds
is_deterministicproperty to theConstantNodeand propagates it acrossActionsDAGnodes, which are later used to determine if filtering result should be written to query condition cache.