Repository navigation
Do not move a non-deterministic expression below ARRAY JOIN - #117756
Conversation
`liftUpArrayJoin` moves the part of a filter or an expression that does not depend on the `ARRAY JOIN`-produced columns below the `ARRAY JOIN`, and `ActionsDAG::splitActionsBeforeArrayJoin` decided what to move by data dependency alone. `ARRAY JOIN` multiplies the rows, so an expression that is not deterministic within the query was drawn once per source row instead of once per expanded row, and a filter over it kept or dropped whole arrays at once. Such an expression is now treated as depending on the `ARRAY JOIN`, so it stays where the query wrote it. Deterministic expressions are moved as before. Closes: #117263
|
Workflow [PR], commit [4f223c8] Summary: ✅
AI ReviewSummaryThis PR fixes the plan-based Findings❌ Blockers
Tests
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit 4f223c8.
Changed lines: Changed C/C++ lines covered: 53/55 (96.36%) · Uncovered code |
|
The fix is exact for the pass it targets, and the pre-existing determinism guard in
For cross-reference, this fixes the plan-level |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units16 translation units recompiled, 106 s compile time in total, 16 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.10 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…oin-nondeterministic-117263
The guard in `splitActionsBeforeArrayJoin` only looked at the outer `FUNCTION` node, so a higher-order function with a non-deterministic or stateful lambda, such as `arrayExists(x -> rand() % 2 = 0, arr)`, was still moved across the `ARRAY JOIN` and its lambda was drawn once per source row instead of once per expanded row. The filter pushdown through `ArrayJoinStep` (`getConjunctionNodes`) had the same gap. A lambda now reports `isDeterministic`, `isDeterministicInScopeOfQuery` and `isStateful` from the functions in its body, both as a `FunctionCapture` (a lambda with captured columns, a `FUNCTION` node) and as a `FunctionExpression` (the function of the `ColumnFunction` that constant folding turns a lambda without captured columns into, a `COLUMN` node). `splitActionsBeforeArrayJoin`, `getConjunctionNodes` and `hasStatefulFunctions` look through such constant lambda columns, and through the lambdas nested in them as captured columns, so both routes across the `ARRAY JOIN` are closed. Addresses the review on #117756.
|
🕵 @zlareb1, thank you. On (1): done in 4f223c8 the way you suggested, on the lambda itself rather than in the split. On (2), the legacy analyzer placing a non-deterministic |
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.
The expression-merge optimization refuses to fuse a parent expression
into a child DAG that contains `arrayJoin` only when the parent has
stateful functions. A merely non-deterministic parent (`rand64`,
`generateUUIDv4`, ...) passed that gate and was then computed before the
row replication: one draw per source row, replicated across all the rows
the `arrayJoin` expands, instead of one draw per output row.
SELECT uniqExact(r), count()
FROM (SELECT rand64() AS r FROM (SELECT arrayJoin(range(4)) AS x FROM numbers(100)));
-- 100 400, each draw repeated on all four expanded rows
The gate now declines a parent that is non-deterministic within the
query as well, using the same predicate the `ArrayJoinStep` routes
adopted in ClickHouse#117756: `now`/`today`/`currentUser` are deterministic in the
scope of a query and still merge.
…minism-filter-pushdown-117262 The source fix of this branch landed on master meanwhile, in a more general form, through #117756 (`Keep a non-deterministic lambda on its side of \`ARRAY JOIN\` too`): `FunctionCapture` and `FunctionExpression` derive `isDeterministic`, `isDeterministicInScopeOfQuery` and `isStateful` from the functions of their body, the walk recurses into `COLUMN` nodes holding a `ColumnFunction` and into the lambdas nested in them as captured columns, and `getConjunctionNodes` uses that shared walk (`allNodeFunctions`). Both conflicts are therefore resolved in favour of master: this branch's `lambdaBodySatisfies` and `columnHoldsNonDeterministicLambda` are dropped as duplicates of the shared helpers in `src/Functions/FunctionsMiscellaneous.h`. The regression test of the aggregation route, which no test on master covers, is kept. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The source fix of this pull request landed on `master` through #117756 and #117248, and the latter also brought a row policy test with the same shape as `05188_row_policy_after_final_nondeterministic_lambda` (`05182_row_policy_after_final_lambda_nondeterminism`), so that test is dropped. The aggregation push-down test and the file pruning test are renumbered to a free range, since `05187` and `05189` were claimed on `master` meanwhile. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…mber The control arm of 05054_deduplicated_rows_need_query_determinism pinned `rowNumberInBlock()` over `ARRAY JOIN` to `[0,0,1,1]`, which is the numbering of the source rows, before the replication. ClickHouse#117756 made `ActionsDAG::splitActionsBeforeArrayJoin` treat a node that is stateful or not deterministic in the scope of the query as depending on the `ARRAY JOIN`, so `liftUpArrayJoin` no longer moves this control below it: the function sees the expanded block and reports `[0,1,2,3]`. Merging master into this branch is what turned Fast test red, on that one line. Re-freezing the literal would pin the plan's block layout, which is the class of output this change is about in the first place. The control is here to show that a function with no argument has nothing to compress, so the lazy and the eager representation must agree; that agreement, plus the row count, is now the assertion, and neither arm's sequence is pinned. Measured at 134b858, build a0053ff1cadbced4: the tree as published fails on that one line and on nothing else, the new form passes 30 of 30 randomized runs, and it is not stuck at true, since splitting the second arm into two-row blocks prints `0 1` and shortening one arm's LIMIT prints `0 0`. On master without this branch (45efaa4) the control prints `1 1` while `runningAccumulate` over the same fixture still differs between the arms, so the control stays green on the base binary, as a control must.
liftUpArrayJoinmoves the part of a filter or an expression that does not depend on theARRAY JOIN-produced columns below theARRAY JOIN, andActionsDAG::splitActionsBeforeArrayJoindecided what to move by data dependency alone - the pass had no determinism check at all.ARRAY JOINmultiplies the rows, so an expression that is not deterministic within the query was drawn once per source row instead of once per expanded row, and a filter over it kept or dropped whole arrays at once:Such an expression is now treated as depending on the
ARRAY JOIN, so it stays where the query wrote it. Deterministic expressions are moved as before, which the test asserts withEXPLAIN.The same holds for a higher-order function with a non-deterministic or stateful lambda, such as
arrayExists(x -> rand() % 2 = 0, arr): a lambda (FunctionCapture, and theFunctionExpressionbehind the constant a lambda without captured columns is folded into) now reportsisDeterministic,isDeterministicInScopeOfQueryandisStatefulfrom the functions in its body, and theARRAY JOINsplit, the filter pushdown (getConjunctionNodes) andhasStatefulFunctionslook through such constant lambda columns. Both the lift-up and the pushdown route across theARRAY JOINare covered by the tests.Closes: #117263
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed a filter containing a non-deterministic function being moved below
ARRAY JOIN, which evaluated it once per source row instead of once per expanded row and made whole arrays pass or fail together.Workflow [PR]
Sync PR [sync-upstream/pr/117756]
Version info
26.9.1.871(included in26.9and later)