Skip to content

Fix query condition cache misuse for folded constants - #90692

Merged
nickitat merged 6 commits into
ClickHouse:masterfrom
korowa:fix-qcc-folded-const
Dec 1, 2025
Merged

nickitat merged 6 commits into
ClickHouse:masterfrom
korowa:fix-qcc-folded-const

Conversation

@korowa

@korowa korowa commented Nov 24, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

  • Documentation is written (mandatory for new features)

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_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.

@nickitat nickitat added the can be tested Allows running workflows for external contributors label Nov 24, 2025
@nickitat nickitat self-assigned this Nov 24, 2025
@clickhouse-gh

clickhouse-gh Bot commented Nov 24, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8f0f16e]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan, targeted) failure
00091_prewhere_two_conditions FAIL cidb
00596_limit_on_expanded_ast FAIL cidb, issue
00596_limit_on_expanded_ast FAIL cidb, issue
00596_limit_on_expanded_ast FAIL cidb, issue
00596_limit_on_expanded_ast FAIL cidb, issue
00091_prewhere_two_conditions FAIL cidb
00596_limit_on_expanded_ast FAIL cidb, issue
AST fuzzer (arm_asan) failure
Logical error: 'col.name == prewhere_info->prewhere_column_name' FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: FAIL cidb, issue
BuzzHouse (amd_ubsan) failure
UndefinedBehaviorSanitizer: undefined behavior (STID: 4443-51fd) FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 24, 2025
@nickitat

Copy link
Copy Markdown
Member

AST fuzzer (amd_ubsan) - #87016
Integration tests (amd_tsan, 5/6) - #90805

Comment thread src/Analyzer/ConstantNode.cpp Resolved
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
Comment thread src/Storages/VirtualColumnUtils.cpp Resolved
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
korowa force-pushed the fix-qcc-folded-const branch from 95e1acc to 434c2fe Compare November 26, 2025 07:11
Comment thread src/Planner/PlannerActionsVisitor.cpp Outdated
column.column = std::move(column_set);

actions_stack[0].addConstantIfNecessary(column.name, column);
actions_stack[0].addConstantIfNecessary(column.name, column, /*is_deterministic=*/ true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if we have a condition like col IN (1, 2, 3, shardNum())?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's not supported (checked some random versions from head back to 24.6 and they all have this behavior)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's maybe then add an assert

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perhaps the following will be fine:

  1. Set it to true only for deterministic contstants. In case of IN (1, 2, shardNum()) the node will have type FUNCTION, so it won't use query condition cache.
  2. Set::isDeterministic essentially is a duplicate of ActionsDAG::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)

@korowa
korowa force-pushed the fix-qcc-folded-const branch from ad8126f to 94a3b93 Compare November 28, 2025 09:12
@nickitat
nickitat enabled auto-merge December 1, 2025 12:12
@nickitat

nickitat commented Dec 1, 2025

Copy link
Copy Markdown
Member

AST fuzzer (arm_asan) - #90265

@nickitat
nickitat added this pull request to the merge queue Dec 1, 2025
Merged via the queue into ClickHouse:master with commit b7504e5 Dec 1, 2025
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 1, 2025
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]>
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 pr-bugfix Pull request with bugfix, not backported by default 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