Repository navigation
Conversation
Both writers of the query condition cache key their entries on the hash of `filter_actions_dag`, which `optimizePrimaryKeyConditionAndLimit` builds from the filters present at that point. When join runtime filters are added, filter push-down runs again, and the join-key equivalence can turn an `ON` conjunct such as `t.v + a.v >= 2048` into `v + v >= 2048` for the other input's read, which `optimizePrewhere` then moves into that read's PREWHERE. The gates added in ClickHouse#119823 only checked that PREWHERE is deterministic, which such a conjunct is, so the rows it removed were recorded as not matching the key: for MergeTree through the re-tagged residual filter, for Parquet reads through `FormatFilterInfo::condition_hash`. A later read with the same WHERE lost them. Both writers now require every PREWHERE conjunct to be a conjunct of the hashed condition (`VirtualColumnUtils::isCoveredByFilter`). The comparison is structural: aliases are looked through, except as argument names of functions that are not name-insensitive; a function's derived result name is ignored, also as an argument, since the key spells it over the storage columns; non-deterministic, PLACEHOLDER and ARRAY_JOIN nodes refuse, and so do `formatRow*`, whose result holds the argument names it was built with, and every function outside `FunctionFactory`: a lambda computed from columns, whose name omits its body (a constant lambda is a column whose name holds it), and a user-defined function, which may read its arguments' names (a WebAssembly UDF with an argument declared by type only and `JSONEachRow` sends the runtime column name). `__topKFilter` stays allowed for MergeTree, whose key is salted with the TopK plan. The change only adds refusals. On the format path this also keeps a read with such a lambda in PREWHERE from reusing the entry of a read whose lambda differs only in its body; the MergeTree WHERE key has the same flaw without PREWHERE and needs a separate fix to the key. The Parquet cases reproduce on 26.8.2.7, the MergeTree case on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the format-path PREWHERE gate comment, the coverage helper's header and the hashing notes to the invariants they state. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 6 findings over 4 rounds), plus my own review (row 5).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20260929-045400 |
|
Workflow [PR], commit [b9f3502] Summary: ✅
AI ReviewSummaryThis PR prevents query condition cache poisoning when a read's Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. Parquet runtime validation remains pending in full CI. |
Build profile diff (arm_release)Commit See the job log for details. |
# Conflicts: # src/Formats/FormatFilterInfo.cpp
…ge-check Master moved the `PREWHERE` gate in `updateQueryConditionCache` below the derivation of deterministic time conditions and accepts a current-time `PREWHERE` there. Keep that, and additionally require the `PREWHERE` to be covered by `filter_actions_dag` via `isCoveredByFilter`. On the derived time-condition path, `isCoveredByFilter` now compares constants folded from non-deterministic functions such as `today()` by value (new `allow_non_deterministic_constants` parameter), so a current-time conjunct moved to `PREWHERE` still counts as covered. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`updateConditionHash` identified a function by its name, arguments and result type. Two separately built copies with equal names can still differ: - a function captures settings, which a subquery's own `SETTINGS` can change (`splitByChar` with `splitby_max_substrings_includes_remaining_string`, `visibleWidth` with `function_visible_width_behavior`, the inner function of a constant lambda), and some of them do not report those in `updateHash`; - without argument counts, a preorder walk gives `(X AND (B OR C)) OR D OR E` and `(X AND (B OR C OR D)) OR E` the same bytes. A JOIN's `ON` copy pushed into `PREWHERE` then counted as covered by the subquery's `WHERE`, and the read cached granules as not matching that `WHERE` although only the `ON` copy had removed their rows: a later plain read returned 0 instead of 904. Hash the function object itself. A PREWHERE conjunct that comes from the hashed condition shares its function objects with it (the filter DAG reuses `function_base`, also when it is built from an existing PREWHERE), so the existing cache-hit controls still hit, while a copy built elsewhere no longer counts as covered. Found by the AI review on ClickHouse#122804. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #119823
Related: #119823 (comment)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a query silently returning fewer rows after a
JOINwhoseONcondition the optimizer had moved into thePREWHEREof a read: the query condition cache recorded granules, or Parquet row groups, as not matching that read'sWHEREalthough only the moved condition had removed their rows. A Parquet read with a lambda inPREWHEREalso no longer reuses the entry of one whose lambda differs only in its body.Description
On 26.8, for a
File(Parquet)tablef (k UInt64, v Int64, w Int64)with 2000 rowsnumber, number, numberin row groups of 64, written more than 3 s earlier, runningSELECT count() FROM f RIGHT JOIN (SELECT * FROM f WHERE v + w < 100) AS a ON f.v = a.v AND f.v + a.v >= 2048 WHERE f.v > 5makesSELECT count() FROM f WHERE v + w < 100 AND v > 5return 0 instead of 44. On masterMergeTreedoes the same.Through
f.v = a.v, filter push-down turns theONconjunct intov + v >= 2048for the subquery's read andoptimizePrewheremoves it intoPREWHERE. Both cache writers key their entries onfilter_actions_dag, built before that conjunct existed, while the gates from #119823 only checked thatPREWHEREis deterministic, which it is.Both writers (
updateQueryConditionCacheforMergeTree,FormatFilterInfofor file and object-storage reads) now require eachPREWHEREconjunct to be a conjunct of the hashed condition (VirtualColumnUtils::isCoveredByFilter) made of the same function objects: an equally spelled copy built elsewhere may differ in shape or in captured settings.formatRow*and user-defined functions, which can read their arguments' names, and a lambda computed from columns never count as covered: the key spells such a lambda without its body, so on 26.8arrayExists(x -> x != val, [5001, 7001])in a ParquetPREWHEREreused the entry ofx = val. TheMergeTreeWHEREkey has the same flaw withoutPREWHERE, which needs its own fix. The change only adds refusals; both comments now say what is checked.The new arms in both
03229_query_condition_cache_join_runtime_filtertests fail without the change; controls withIN,=, a nested function and a constant lambda still hit the cache. #121946 handles the same problem for the tagged filter step.Workflow [PR]
Sync PR [sync-upstream/pr/122804]