Repository navigation
Limit the stack walk in updateQueryConditionCache - #122271
kirillshokhin wants to merge 1 commit into
Conversation
`updateQueryConditionCache` tagged the first `FilterStep` anywhere above
the read with the key hashed from the read's `filter_actions_dag`, whether
or not that filter's condition was collected into it.
Marks the filter empties were then recorded as not matching the key's
condition, although the filter evaluated a stronger one: that condition
and its own. Later queries skipped them and returned wrong results:
SELECT count() FROM (SELECT a, b FROM t PREWHERE a = 1 LIMIT 100000) WHERE b = 2;
SELECT count() FROM (SELECT a, b FROM t PREWHERE a = 1 LIMIT 100000) WHERE b = 3; -- 0 instead of 500
`optimizePrimaryKeyConditionAndLimit` does not collect past the `LIMIT`, so
the key covers only `a = 1`, but `WHERE b = 2` is the first `FilterStep`
above the read. The same happens on the re-walk after join runtime filters,
once `optimizePrewhere` has moved the inner `WHERE` into PREWHERE.
Walk up only through `ExpressionStep`s, which the collection also passes,
and tag the `FilterStep` reached there, if any.
A filter merged into the chain after the collection is still tagged: see
`tests/queries/bugs/query_condition_cache_filter_merged_after_collection.sql`.
|
Workflow [PR], commit [1ca8a80] Summary: ❌
AI ReviewSummaryThis patch fixes the Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 1ca8a80.
Changed lines: Changed C/C++ lines covered: 24/24 (100.00%) · Uncovered code |
| @@ -0,0 +1,41 @@ | |||
| -- The query condition cache key of a read is built from its `filter_actions_dag`, collected early in the second pass | |||
There was a problem hiding this comment.
This repro does not run in our functional test pipeline: clickhouse-test, changed-test selection, and bugfix validation all enumerate tests/queries/0_stateless, not tests/queries/bugs. That means the extra merged-after-collection case this PR calls out still has no executable regression coverage.
If this sibling path is still relevant, please move it into tests/queries/0_stateless once it passes there; if it is already obsolete, please drop the dead file/comment instead of leaving a non-running "test" behind.
|
@shankar-iyer If you find some time, could you please take a look at this PR? @kirillshokhin The PR message is slop, please rewrite so it clearly mentions:
|
|
I've updated the description, hope it makes more sense now. I've marked the PR as a draft while I resolve the merge conflicts and check whether #121946 provides a better fix for the same problem. |
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]>
…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
Closes: #123845
updateQueryConditionCachewalks the stack of parent nodes from the read step up to the firstFilterStepand tags the found step with the hash and the condition of the read'sfilter_actions_dag. However, it does not check if the found filter step's condition is covered by thefilter_actions_dag.As a result, the filter records empty marks as not matching the
filter_actions_dag's condition, although the filter evaluated a stronger one: thefilter_actions_dag's condition and its own. Thus the query condition cache gets poisoned.For example:
optimizePrimaryKeyConditionAndLimitdoes not collect past theLIMIT, so thefilter_actions_dagcovers onlya = 1, butWHERE b = 2is the firstFilterStepabove the read. The same happens on the re-walk after join runtime filters, onceoptimizePrewherehas moved the innerWHEREinto PREWHERE.See https://fiddle.clickhouse.com/30288fb3-1869-4648-9a61-3e2ae106a0f2 for a complete reproducible example.
To fix it,
updateQueryConditionCachenow walks up only throughExpressionSteps withoutarrayJoin, mirroringoptimizePrimaryKeyConditionAndLimitbehavior, and tags the firstFilterStepreached, if any. This way we guarantee not to tagFilterStepthat was not reachable whenoptimizePrimaryKeyConditionAndLimitran. The mirroring is not complete:updateQueryConditionCachestops atArrayJoinStep/ObjectFilterStepwhileoptimizePrimaryKeyConditionAndLimitdoes not, so we lose QCC for such queries.Another repro, not covered by this fix, is tagging a
FilterSteppushed down by the push-down that join runtime filters force regardless of the settings. It requiresquery_plan_filter_push_down = 0to prevent inclusion of the filter into the read'sfilter_actions_dag. See https://fiddle.clickhouse.com/70962114-d460-4973-90af-f5bcc026031b.Related: #116891
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results from the query condition cache when a filter above a
LIMITwas cached with the key of the condition below it, e.g. with an explicitPREWHEREin a subquery.Workflow [PR]
Sync PR [sync-upstream/pr/122271]