Repository navigation
Conversation
Three checks decide whether a WHERE conjunct may be evaluated below a JOIN, and each one inspected only the outer ActionsDAG. A lambda body is not part of that DAG. A capturing lambda keeps it in an ActionsDAG owned by its FunctionCapture, and a lambda whose unsafe call consumes the lambda parameter cannot be hoisted out, so it is folded to a ColumnConst(ColumnFunction(FunctionExpression)) - a COLUMN node, which the main gate's own type == FUNCTION clause could not even reach. Neither wrapper overrides isDeterministicInScopeOfQuery or isStateful, so both reported themselves safe whatever the body computed. getConjunctionNodes therefore classified such a conjunct as pushable, and because splitActionsForJOINFilterPushDown ends with removeUnusedConjunctions the conjunct was moved rather than copied: it was never evaluated on the post-join row set, so an outer join fabricated rows that escaped a filter that no longer constrained them. onlyDependsOnAvailableColumns, the disjunction path that is enabled by default, had no determinism test at all. There even a bare, fully visible WHERE rand(a) % 2 = 0 was copied below the join while being kept above it, so it was drawn twice per row and about a quarter of the rows survived instead of about a half. That half of the defect involves no lambda. hasStatefulFunctions, the step-level veto in tryPushDownFilter, was blind the same way. One shared helper, inspectLambdaBodies, walks the bodies reachable from a node, follows nested lambdas through the captured columns of a ColumnFunction, and reports the two unsafe classes separately, because the consumers relax them differently. Non-determinism is rejected per conjunct in both classifiers, with the existing allow_non_deterministic_functions parameterisation preserved. Statefulness is rejected at the pre-gate only: the veto there is all-or-nothing for a whole step, and rejecting statefulness per conjunct instead would let a deterministic sibling move and hand the stateful function a different input row set, which would be a behaviour change beside a correctness fix. The helper never throws, unlike the serialization code it borrows the unwrap from, because an unrecognized shape is simply not a lambda. The new helper is called from filterPushDown.cpp rather than folded into ActionsDAG::hasStatefulFunctions, so the other callers of that method keep their exact current behaviour. Present unchanged from 25.8 through master. Co-Authored-By: Claude Fable 5 <[email protected]>
… guard Review round 1 found that the hidden-lambda rejection in onlyDependsOnAvailableColumns had no test row failing when it alone was removed: every hidden-lambda row read the fabricated-NULL or plan-shape oracle, while the surviving-row-count oracle (the only one that can observe the partial path copying a conjunct below the join) was used only by rows with a visible rand, which the node's own metadata already rejects. Row B3 closes that cell: B1's shape with the rand moved into a lambda body. Deleting only the hidden-lambda rejection reddens B3 and no other row. The row pins use_join_disjunctions_push_down because it observes only the partial path, and both test runners can turn that path off: clickhouse-test randomizes the setting directly, and the stress runner's randomized compatibility reverts it below the 26.1 block where its default flipped. Without the pin the row reads 1 whatever the optimizer does. Also record two deliberate design choices in comments: inspectLambdaBodies shares no visited state with the DAG-wide entry point, and the cheap metadata test must stay the first disjunct so the DAG walk stays off the common path. Co-Authored-By: Claude Fable 5 <[email protected]>
The B1 row is the only one that observes the partial push-down path's own-metadata determinism rejection. The main path rejects a visible non-deterministic function node on master already, so B1's verdict can move only via the partial path, and that path is entirely inside a block gated on use_join_disjunctions_push_down. B1 left the setting unpinned, so on any draw where the setting is off both of its arms evaluate the visible rand exactly once, the ratio is about 1, and the row passes even with the rejection deleted. Two channels turn the setting off in CI: the functional runner randomizes it directly at about 5% of draws, and the stress runner's randomized compatibility value reverts its pre-26.1 default, which 72 of its 84 draws do. Pin it on the push-down-enabled arm only, leaving the reference arm at defaults. Measured on a build with only that rejection deleted: the pinned row reads 0 while the unpinned row reads 1 under a session SET, a client option and compatibility='25.1'. The same mutation reddens B1 and no other row, so each of the two rejections in that classifier carries its own coverage. Also reword the A-oracle comments, which claimed the fabricated-NULL oracle observes both push-down paths. It observes the main path's move only: the partial filter lands on the preserved side, so a copy fabricates no NULL. The copy is observed by B1 and B3. Co-Authored-By: Claude Fable 5 <[email protected]>
Filter push-down now leaves an unsafe predicate above the JOIN instead of moving it below. The very next optimization reads that filter to decide whether a LEFT JOIN can become an INNER one, and its own determinism guard, dagContainsNonDeterministicFunction, scans only the outer DAG. So the fix newly hands the hidden-lambda class to a guard that cannot see it, and the rewrite would then drop rows on the strength of one fabricated dry-run row. Add a whole-DAG entry point for the non-determinism class beside the existing stateful one and consult it from that single choke point, which covers all six call sites. The cheap outer scan stays first, so the common path is unchanged. Measured: for a LEFT JOIN whose filter hides rand in a lambda body, master converts the join to INNER while the visible form is already vetoed. That asymmetry is what identifies the class as a consequence of retaining the predicate rather than a pre-existing gap. The same guard also repairs a wrong-results case where roughly half the rows were dropped. Tests: J1 asserts the join kind survives the hidden form, J2 pins the visible control, J3 asserts a deterministic lambda body still lets the rewrite happen. A9 covers the capturing-lambda carrier, where the unsafe call consumes both the lambda parameter and an outer column so the body stays in a FunctionCapture rather than folding to a column; the branch that enters it had no witness for this class before. J1 pins query_plan_convert_outer_join_to_inner_join because the runner draws it off in about 5 percent of runs and a randomized compatibility below 24.4 reverts its default. Also correct an in-diff comment that described the disjunct order as short-circuiting on the common path, which is the opposite of what it does.
Internal second-model review (4 rounds, 8 findings adjudicated)Before opening this PR I ran four rounds of independent review: a cold read of the resulting code by Agreed and fixed
Disagreed, with evidence, and split out
Noted, not blocking
|
Pre-PR validation gate (a-i)
Twelve mutations were run, each with a predicted failing set. Four predictions were wrong and the A 56-test sweep scoped by feature (the four gating settings plus the lambda-and-join tests) was run The first tag-free 50-run randomized pass failed 11 times out of 50, which found a real fragility in |
|
cc @antaljanosbenjamin @yariks5s, could you review this? The two filter push-down classifiers tested |
|
Workflow [PR], commit [f0fd79f] Summary: ✅
AI ReviewSummaryThis PR narrows the remaining fix to the disjunction-side partial Final Verdict✅ No blocking or major findings in the current diff. LLVM Coverage ReportMeasured on commit f0fd79f.
Changed lines: Changed C/C++ lines covered: 4/4 (100.00%) · Uncovered code |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 72/75 (96.00%) · Uncovered code |
CI finish ledger - 3b14c6fEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260803-180000 |
|
Dear @yariks5s, 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. |
…hdown-unsafe-predicate
Master fixed the main filter push-down and the step-level stateful veto in 4f223c8 (ClickHouse#117756): getConjunctionNodes and hasStatefulFunctions now go through allNodeFunctions, which enters a lambda body, so the ActionsDAG.cpp and filterPushDown.cpp changes here are redundant and the local lambdaBodySafety helper has no remaining caller. The Utils.cpp guard on dagContainsNonDeterministicFunction is conceded to ClickHouse#117248, whose replacement expression is character-identical to the one this branch carried, and which ships its own test and credits issue ClickHouse#117016. partialJoinFilterPushDown.cpp is reverted here too, so this commit compiles on its own once the helper is gone; the next commit re-adds the guard in master's own idiom.
`use_join_disjunctions_push_down` extracts a per-side partial predicate and pushes it below the join with `addFilterOnTop`, which leaves the original filter in place. A conjunct that is non-deterministic within the query is therefore drawn twice per row, independently, and only a row that passes both draws survives, so `WHERE rand(a) % 2 = 0` returns about a quarter of the rows instead of about half. `onlyDependsOnAvailableColumns` gated only on input-name availability and ARRAY_JOIN, with no determinism test of any kind. The predicate is `isDeterministicInScopeOfQuery`, matching `getConjunctionNodes` and `dagContainsNonDeterministicFunction`: `now`/`today`/`currentUser` are per-row constant within a query and keep their push-down. `allNodeFunctions` rather than a test on `node.function_base` because constant folding turns the lambda of `arrayExists(z -> rand(z) % 2 = 0, [a])` into a `ColumnFunction` living in a COLUMN node, whose own metadata says nothing about the body, and the body is a separate DAG. A test that inspects FUNCTION nodes only classifies that conjunct as pushable. Statefulness needs no test here: `tryPushDownFilter` returns early when the filter expression `hasStatefulFunctions`, and 4f223c8 made that walk lambda-aware, so a stateful call never reaches this extraction. The check can only turn a `true` answer into `false`, so the worst outcome of a misclassification is a pre-filter that is not extracted, never a new wrong result.
Four rows over one 200000-row fixture. B1 draws a visible `rand`, B3 draws one hidden in an `arrayExists` lambda: both compare the surviving row count with the push-down on and off, and without the guard the ratio is about 0.5 rather than 1, which at this size is about 16 standard deviations from the asserted 5% band. B2 runs the same oracle on a deterministic predicate, so a red B1 or B3 is the optimizer and not the fixture. B4 asserts the other direction, that a deterministic lambda is still extracted: a blanket refusal would keep B2 green, because B2's count does not depend on whether the extraction happens, so the row counts the plan lines mentioning the lambda instead. Its filter is a disjunction whose every branch spans both sides of the join, which the main push-down cannot move, so the per-side extraction is the only thing that can add a plan element. `use_join_disjunctions_push_down` and `enable_join_runtime_filters` are pinned in each query's own SETTINGS clause, not by a SET: the runner passes randomized settings on the client command line, and those are per-query settings that outrank a SET issued earlier in the session. `enable_analyzer` cannot go there (INCORRECT_QUERY when a subquery changes it) and the runner never randomizes it. The file is renamed from 04658_join_filter_push_down_unsafe_predicate, which covered carriers this branch no longer fixes.
The four rows could not tell `isDeterministicInScopeOfQuery` from the stricter `isDeterministic`: two of them draw `rand`, which fails both, and the other two use functions accepted by both. Built with the predicate weakened to `isDeterministic`, the file stayed green, so a change that loses this pre-filter for every function that is non-deterministic only across queries (41 of them under src/Functions, among them `now`, `today`, `currentUser`, `queryID` and `randConstant`) had no test in its way. `isDeterministic` is also the live spelling at the sibling copy-below-a-join site in propagatePredicateAcrossEquiJoin.cpp and in `ActionsDAG::hasNonDeterministic`, so unifying these predicates later is a real regression path. `randConstant` draws once per query, so duplicating the extracted copy cannot change any row's verdict and the extraction is legitimate; under the stricter reading it would be refused. The row asserts that the extraction still happens, and reads 0 under that weakening. Its lambda captures a column, so the check reaches the body through a `FunctionCapture` node, where the hidden-`rand` row reaches it through a folded `ColumnFunction`: both carriers of the one `allNodeFunctions` walk are now driven from the test. It replaces the deterministic-lambda row rather than joining it, because a blanket refusal reddens it just as it reddened that one, and it additionally reddens the weakening. Co-Authored-By: Claude Opus 5 <[email protected]>
`FunctionExpression::isDeterministicInScopeOfQuery` and
`FunctionCapture::isDeterministicInScopeOfQuery` are two separate virtual
overrides that happen to call the same free helper, so a regression in one is
invisible to a row that only drives the other. B1 drives a plain `rand` FUNCTION
node and B3 the folded `ColumnFunction` a non-capturing lambda becomes, while B4
drives a capture that must be accepted. Nothing drove a capture that must be
refused.
The new row is B3's count band with the lambda capturing a column, which turns
its carrier into a `FunctionCapture` FUNCTION node: the plan prints
`Capture[UInt64, UInt8, UInt8](UInt64) -> UInt8(a, 2, 0)` for it and prints no
capture for B3. Blinding this guard to FUNCTION nodes while leaving its folded
COLUMN path alone reddens the new row and B1 and leaves B3 green, so the two
rows separate the two carriers of the one `allNodeFunctions` walk.
Weakening `FunctionCapture::isDeterministicInScopeOfQuery` itself to `true` does
not redden any row, and that is a property of the other caller rather than of
this test: the same virtual is read by the main push-down
(`ActionsDAG.cpp:3263`), which moves a filter below the join and removes the
original instead of copying it. For a predicate reading only the preserved side
of a left join a move leaves the row count unchanged, so no count band can see
it. Measured on the 200000-row fixture: 99702 surviving rows with the push-down
on against 99950 with it off, and the filter gone from above the join entirely.
B4's comment loses the paragraphs recoverable from the SQL, since `-- { echo }`
reprints it in the reference and the file gains a row in the same commit.
Co-Authored-By: Claude Opus 5 <[email protected]>
The previous version of this row was a count band on a predicate touching only the preserved side of a left join, and that shape cannot witness a regression of the virtual the guard delegates to. `FunctionCapture::isDeterministicInScopeOfQuery` is read by two callers: the guard added here, which copies the extracted predicate below the join and keeps the original above, and the main push-down at `ActionsDAG.cpp:3263`, which moves the predicate and removes the original. A copy draws a per-row value twice and moves the surviving count to about a quarter of the input; a move draws it once and leaves the count unchanged. Both callers were eligible for the old shape, so when the virtual was weakened to `true` the main push-down took the predicate and moved it, and the band read the same 99702 against 99950 that a correct refusal reads. The row's comment claimed a regression in either override had to redden a row, which that measurement disproves. A disjunction spanning both sides of the join cannot be moved below it by the main push-down at all, so on that shape the partial path is the only optimizer that can act. The row now asserts that the plan prints `arrayExists` the same number of times with `use_join_disjunctions_push_down` on as with it off, which says the guarded extraction refused the capturing lambda and says nothing about any other optimizer. Weakening either the guard or the virtual it delegates to now reddens it. B4 is the non-vacuity witness: it asserts the strict inequality on this same shape with `randConstant`, a value drawn once per query that must still be extracted, so a plan that stopped printing the lambda at all, or an extraction that degraded into refusing every lambda, reddens B4 before this row could go green for the wrong reason. `enable_join_runtime_filters` is pinned in both arms, as B4 does, because this is a plan-shape oracle rather than a count band. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review notesRescoped to the one defect no open PR covers: the lambda leg of the disjunction push-down. The
Each carrier of a lambda has its own rejection row. Building Statefulness deliberately gets no test at this site: One review finding disagreed with, on evidence:
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
Related: #116930
Related: #117248
Related: #117249
Related: #117756
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the default-enabled
use_join_disjunctions_push_downsilently dropping rows when the filter abovea join hides a non-deterministic function inside a lambda body, as in
arrayExists(z -> rand(z) % 2 = 0, [a]). The extracted pre-filter was pushed below the join while theoriginal filter stayed on top, so the lambda was drawn twice per row and only rows passing both draws
survived.
Description
At shipped defaults, no
SETTINGSclause, 200000-row tables:onlyDependsOnAvailableColumnsgated only on input-name availability andARRAY_JOIN, with nodeterminism test. Constant folding turns the lambda into a
ColumnFunctionin a COLUMN node, and itsbody is a separate DAG, so a test that inspects FUNCTION nodes cannot see it.
allNodeFunctionsfrom#117756 covers both shapes in one call.
The predicate is
isDeterministicInScopeOfQuery, matchinggetConjunctionNodes, sonow/today/currentUserkeep their push-down; measured, the stricterisDeterministicloses the pre-filter forrandConstant(a). Statefulness needs no test here:tryPushDownFilterreturns early onhasStatefulFunctions, which #117756 made lambda-aware.#117249 guards this same function for the visible-
randcase; this subsumes it. Built with itsFUNCTION-only check in place of this call, the hidden-lambda row still fails while the visible one
passes. If #117249 lands first, upgrading its check to this call gives the same effect.
Three carriers of the same invariant are deliberately not touched: the plan-time JOIN-conversion guard
in
Utils.cpp, whose fix in #117248 is character-identical to the one this branch used to carry; and,neither reachable at defaults, the inlined copy of the predicate in
AggregationPushdown.cpp(needsenable_cascades_optimizer = 1) andActionsDAG::hasNonDeterministic, which asks the differentacross-queries question.
Test
05139_join_partial_push_down_nondeterministic_lambdaasserts the count band for the visible andthe hidden call, and that a deterministic predicate and a query-constant lambda keep their pre-filter.
Both defect rows fail on master.
Workflow [PR]
Sync PR [sync-upstream/pr/112700]
Version info
26.9.1.978(included in26.9and later)