Skip to content

Fix wrong results of query_plan_convert_join_to_in with the query condition cache - #126072

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-convert-join-to-in-query-condition-cache
Oct 16, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-convert-join-to-in-query-condition-cache

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

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

Fixed missing rows in a JOIN converted to IN by query_plan_convert_join_to_in = 1 when the query condition cache is enabled: the cache could serve the filter result of one query to another query with a different right side, or to the same query after the right table changed.

Description

Related: #90692 (regression source).

On 26.3.42.3, 26.8.18.2 and 26.9.16.11, with query_plan_convert_join_to_in = 1, SELECT count() FROM l ANY INNER JOIN r ON l.k = r.k WHERE r.c = 2 AND l.v >= 5 returns 0 instead of 1 when the same query with r.c = 1 ran before (use_query_condition_cache = 0 returns 1).

buildInConversion adds the right side's set to the left read's filter as a constant column named set column with the default is_deterministic_constant = true. The query condition cache trusts that flag, and its key does not cover the set's contents, so every converted IN with the same remaining conjuncts shares one cache entry. #90692 moved set determinism from FutureSetFromSubquery onto this node flag and set it to false only for the analyzer's IN (subquery) sets; this optimization was not updated.

The fix marks the converted set non-deterministic, as the analyzer does. The conversion and its PREWHERE placement are unchanged; these filters are just no longer cached, like analyzer IN (subquery) filters.

Validation: the new test returns 1 0 0 0 instead of 1 1 1 2 on the unfixed build (20/20 randomized runs) and passes 50/50 randomized runs with the fix; the existing query condition cache and convert_join_to_in tests pass.


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

Version info

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

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding), plus my own cold review of the diff (0 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test stays green if the JOIN to IN rewrite is silently declined (05401_convert_join_to_in_query_condition_cache.sql) DISAGREE The rewrite fires in every configuration measured: the unfixed build fails the test 20/20 under full random settings (including automatic_parallel_replicas_mode = 2, query_plan_join_swap_table = auto, random index_granularity) and without randomization, and only a converted plan can return 0. EXPLAIN on the fixed build shows CreatingSets and k IN subquery1 in PREWHERE. Pinning enable_analyzer is obsolete on master, and join_algorithm is not randomized.
2 💡 An EXPLAIN guard for CreatingSets would catch a future change that stops the rewrite for this query shape noted, not blocking If the rewrite is declined, the converted-set key this bug needs does not exist for that shape either.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261015-163000

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On a server (clickhouse local has query_condition_cache_size = 0), with query_plan_convert_join_to_in = 1: the r.c = 1 query, then the r.c = 2 query with the same filter on l, returns 0 instead of 1 on every run. The new test fails 20/20 randomized runs on the unfixed build.
b Root cause explained? buildInConversion adds the right side's FutureSetFromSubquery as a constant column with the default is_deterministic_constant = true. The query condition cache gates on that flag, and its key covers the fixed name set column but not the set's contents, so two queries with different sets (or the same query after the right table changed) share one entry and the second is served the first's granule mask. #90692 moved set determinism onto this flag and set it false only for the analyzer's IN (subquery) sets.
c Fix matches root cause? Yes. The converted set is declared non-deterministic at its creator, as the analyzer does. No cache-side special case, no tags, no bound changes.
d Test intent preserved / new tests added? New test 05401_convert_join_to_in_query_condition_cache: different sets with the same filter on l, and the same query after INSERT INTO r (a content-hash name would still miss this case).
e Both directions demonstrated? Yes, on Build-ID-verified servers built from the same base: unfixed 1 0 0 0, fixed 1 1 1 2; runner [ FAIL ] / [ OK ] without randomization; 20/20 FAIL vs 50/50 OK with randomized settings.
f Fix is general across code paths? Yes. Every ColumnSet::create site in src/ checked: the analyzer IN path already sets the flag false; the direct-join lookup read and the lazy FINAL key analysis read disable the cache explicitly; runtime-filter and Iceberg equality-delete sets are not cache key inputs; constant IN tuples are content-addressed. All cache writers and readers gate on this one flag.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Two-column keys, Nullable and LowCardinality(String) keys, ALL and ANY strictness: all wrong without the fix, correct with it. Randomized settings including index_granularity pass 50/50.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default, format or protocol change; the flag is already part of plan serialization. Converted filters are no longer cached, matching analyzer IN (subquery) filters.
i Invariants and contracts preserved? Yes. The DAG, its outputs and the PREWHERE placement are unchanged (EXPLAIN still shows CreatingSets and k IN subquery1 in PREWHERE); only the constant's determinism flag differs, and the node hash includes it, so such a filter can never hit an entry written by a deterministic one.

Session id: cron:clickhouse-impl-self-slot-57:20261015-155400

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2da8f4f]

Summary: ✅


AI Review

Summary

The PR fixes missing rows when query_plan_convert_join_to_in interacts with the query condition cache by marking the runtime-built right-side set as non-deterministic. The regression test covers different right-side filters and changes to the right table. No actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai @nickitat, could you review this and add the can be tested label (I cannot add labels at the moment, so CI has not started)? query_plan_convert_join_to_in adds its subquery set as a constant with the default is_deterministic_constant = true, so the query condition cache shares one entry across different right sides; the fix marks it non-deterministic, as #90692 does for the analyzer's IN (subquery) sets.

@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Oct 15, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 16, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 16, 2026
Merged via the queue into ClickHouse:master with commit 64ec938 Oct 16, 2026
524 of 528 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #126072 to 26.10: Fix wrong results of `query_plan_convert_join_to_in` with the query condition cache
leshikus pushed a commit that referenced this pull request Nov 13, 2026
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-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants