Repository navigation
Fix wrong results after a JOIN poisons the query condition cache - #121946
Conversation
`updateQueryConditionCache` hashes the read step's `filter_actions_dag` and
attaches that hash to the filter step above the read, without checking that the
step's own predicate is what the hash describes. Three things make them diverge:
* `ReadFromMergeTree::applyFilters` rebuilds `filter_actions_dag` only on its
first call, because `indexes` caches index analysis, so the DAG is frozen at
that point;
* filter push-down and `tryMergeFilters` then replace the steps above the read
with a new `FilterStep` whose predicate is `and(child, parent)`, dropping the
key;
* the re-walk that exists to restore those dropped keys re-attaches one derived
from the frozen DAG onto the widened step.
A `JOIN` supplies the widening conjunct: once the join is inner, push-down moves
its `ON` conjunct below the `WHERE`, and the merge folds both into one filter.
`FilterTransform` empties the granule because of the `ON` conjunct and records
"no matching rows" under the hash of the `WHERE` predicate alone, so every later
read of that part with that predicate skips the granule. At default settings a
`SELECT count() FROM t WHERE v > 5` over 50000 rows then returns 0 instead of
49994; `enable_join_runtime_filters` has been on by default since 26.2 and
`use_query_condition_cache` is on by default, so no other setting is needed.
Record which filter conditions the DAG was built from and refuse to attach the
key to a step that is not one of them. The soundness requirement is one-sided: an
entry means "no row of this granule matches C", so the write is sound exactly when
C implies the step's predicate. The DAG describing more than the step does stays
sound and is the common case, because the read's PREWHERE and row-level filter are
in the DAG too. A refused write loses the optimization, never a row.
`adoptFiltersFrom` carries the names with the DAG, which is what keeps automatic
parallel replicas populating the cache: it builds its candidate plan with
`query_plan_optimize_primary_key` off and hands its reads the analysis that way.
This completes ClickHouse#119823, which established the same invariant for the case where
the narrowing step is the read's PREWHERE and left the merged-filter case open.
Found by the AST fuzzer's `Identity WHERE (NOT(NOT p))` oracle on master, which
reported it because `not(not(p))` hashes to a different key and so kept reading
the rows the reference had lost.
Co-Authored-By: Claude Opus 5 <[email protected]>
Delete the design/soundness narration above the new guard, the new member's write site and its accessor; keep one line on the frozen-DAG invariant at the guard and the member's own meaning.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold read of the
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260924-092500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260924-071200 |
|
Workflow [PR], commit [56439ce] Summary: ✅
AI Review
|
Build profile diff (arm_release)Commit See the job log for details. |
…ter-guard # Conflicts: # src/Processors/QueryPlan/Optimizations/updateQueryConditionCache.cpp
The test is taken from ClickHouse#122271 unchanged. It covers the shape of ClickHouse#123845: an outer filter separated from the read by a LIMIT, with the read's whole condition in PREWHERE (explicit, or moved there after join runtime filters were added). On master the granules emptied by the outer filter were recorded as not matching the read's condition. Co-authored-by: Kirill Shokhin <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 9cae427Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The build failure dropped 166 dependent jobs, so no functional test has run on this head yet. Session id: cron:our-pr-ci-monitor:20261016-130004 |
|
Dear @shankar-iyer, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
…sh-down test After the master merge, `03229_query_condition_cache_join_runtime_filter` failed in Fast test and in the private sync: master now keeps the self-join's always-false `ON` conjunct `not(equals(...))` as a post-join filter, so the plan assertion that pinned it merged into the build side's filter returned 0. The row counts of the arm are unchanged and correct, so keep them and drop only the plan assertion. Add `05453_query_condition_cache_filter_merged_after_collection` from ClickHouse#122271: with `query_plan_filter_push_down = 0` the outer filter is pushed down only by join runtime filters, after the read's `filter_actions_dag` was collected, which is the case the `filterActionsDAGWasBuiltFrom` check guards. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121946&sha=f023dbbb345e0f0ab6dd76e75bd5f2fe3726aa54&name_0=PR&name_1=Fast%20test
|
Pushed 5175ef2 to fix the Current master keeps the self-join's always-false Because the |
|
Thanks. Without the fix, |
CI finish ledger - 5175ef2Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261028-123007 |
…ns`, as the style check now requires
…on cache guard `filterActionsDAGWasBuiltFrom` matched the step to tag only by its filter column name. After index analysis, predicate propagation and join runtime filters run `tryMergeFilters` again, and it can merge two of the collected filters (for example the pushed-down `WHERE` and the predicate copied from the other side of an equi-join) into one step named `and(...)`. That name is not in the record, so the re-walk refused the key and the cache was no longer populated, although the merged step filters exactly the conjunction `filter_actions_dag` hashes. Accept a step whose condition is one of the collected conditions or an `and` of them. A granule it empties has no row matching that conjunction, which `filter_actions_dag` implies, so the entry is sound. An `and` with any other operand, e.g. a join conjunct merged in later or a runtime filter, is still refused. Measured on a server with default settings: after `SELECT count() FROM (SELECT k FROM t1 WHERE k > 50000) AS s JOIN t2 ON s.k = t2.k WHERE intDiv(t2.k, 1000) % 10 = 3` the cache records the granules of `t2` the filter emptied on master and with this change, and only the primary key pruning without it. Also say "case" instead of "arm" in the test comments this PR added. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #123845
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed silently wrong results from the query condition cache. Once a query with a
JOIN, or with a filter above aLIMIT, had read a part, a later query using the sameWHEREpredicate on that part could return rows from fewer granules, or no rows at all. Reachable with default settings.Description
On 26.9 with default settings,
SELECT count() FROM t WHERE v > 5returns 0 instead of 49994 once an earlierJOINover the same part has run.use_query_condition_cacheandenable_join_runtime_filtersare both on by default:Root cause:
updateQueryConditionCachehashes the read step'sfilter_actions_dagand attaches that hash to the filter step above the read. The DAG is frozen at index analysis, but push-down andtryMergeFiltersthen rebuild that step with theJOIN'sONconjunct merged in, and a re-walk re-attaches the stale key to it. TheONconjunct empties the granule, which is then recorded as matching no row of theWHEREpredicate.The fix records which conditions
filter_actions_dagwas built from and refuses the key on a step whose condition is neither one of them nor anandof them. A refused write costs only the optimization, never a row. This completes #119823, which established the same invariant where the narrowing step is the read'sPREWHERE.Since 26.10 the
ONconjunct stays above the join, so that query is correct there. On 26.10.3.7 and master two other shapes still return 0 instead of 500: join runtime filters pushing the outer filter down after index analysis (05453_query_condition_cache_filter_merged_after_collection), and an outer filter above aLIMITorLIMIT BY(#123845,05243_query_condition_cache_filter_above_limit). Both tests come from #122271.Found by the AST fuzzer's
Identity WHERE (NOT(NOT p))oracle on master (report forf61f67b3). TheJOINtest is added to #119823's own file, its existing assertions unchanged.Workflow [PR]
Sync PR [sync-upstream/pr/121946]
Version info
26.12.1.194-master(included in26.12and later)