Repository navigation
Cover a non-deterministic lambda in HAVING push-down and file pruning with tests - #117747
Conversation
A lambda reports the properties of `IFunctionBase` from its own defaults, not from the functions it calls, so a non-deterministic call inside a lambda body was invisible to every consumer of `isDeterministicInScopeOfQuery`. The filter push-down guard in `getConjunctionNodes` therefore moved a `HAVING` predicate such as `arrayExists(x -> rand(x) % 2 = 0, arr)` below the aggregation, where it is evaluated once per input row instead of once per group, and every group survived. `FunctionCapture` and `FunctionExpression` now derive `isDeterministic`, `isDeterministicInScopeOfQuery` and `isStateful` from their inner DAG. A lambda whose captured columns are all constant is materialized as a constant `ColumnFunction` and has no function node at all, so the conjunction walk additionally inspects such columns. Closes: #117262
|
Workflow [PR], commit [b8674de] Summary: ✅ AI ReviewSummaryThis PR is now tests-only: it adds Final Verdict
|
This comment was marked as low quality.
This comment was marked as low quality.
|
Two more consumers of the same blind spot, so they do not get lost when this merges — both are still bypassed by the folded-capture shape after this PR, because the fix is per-consumer and these sites keep walking only
Both would also be closed at once by the single-point option: make |
…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 number 05063 was taken by three tests that landed on master meanwhile, so the test is renumbered to 05175. The review asked for a nested case. A lambda nested in another lambda captures nothing while depending on the outer lambda argument, so it stays a `ColumnFunction` among the captured columns of the outer one instead of being hoisted to the top level of the filter; both the value oracle (`rowNumberInAllBlocks() >= 20` passes nothing above the aggregation and everything below it) and the `EXPLAIN` assertion now cover that shape. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The review asked for a nested case. A lambda nested in another lambda captures nothing while depending on the outer lambda argument, so it stays a `ColumnFunction` among the captured columns of the outer one instead of being hoisted to the top level of the filter, where the walk of `getConjunctionNodes` would see it anyway. Both the value oracle (`rowNumberInAllBlocks() >= 20` passes nothing above the aggregation and every group below it) and the `EXPLAIN` assertion now cover that shape. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… before `FINAL` Two more consumers of the determinism of a filter walked only `FUNCTION` nodes, so a lambda without non-constant captured columns — folded into a constant `ColumnFunction`, a `COLUMN` node — was invisible to them, and the overrides of `FunctionExpression` were never asked. Both now use the shared `allNodeFunctions`, which looks into such a column and into the lambdas nested in it as its captured columns: - `VirtualColumnUtils::isDeterministicInScopeOfQuery` gates which part of a filter may be used to decide which files a `file`/`url`/object-storage query reads at all. A predicate such as `arrayExists(x -> rand(x) % 2 = 0, materialize([1]))` was drawn once per file and wiped out whole files, on top of the per-row draw that follows: over 16 files of 2000 rows it kept about a quarter of the rows instead of about a half, and a single-file query returned 0 about half of the time. `isDeterministic`, the across-queries variant next to it, keeps its own check for a non-deterministic constant such as a folded `now()`. - `isNodeDeterministic` in `ReadFromMergeTree` decides whether a row policy may be applied before `FINAL`. A policy over sorting-key columns is applied for every row of a dedup group, so two draws that disagree flip the row that wins the group. Tests `05176_row_policy_after_final_nondeterministic_lambda` and `05177_file_pruning_nondeterministic_lambda`; both were checked against a control binary without the fix, where they fail on the lambda assertion and pass on their controls. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Thank you — all four points of the first comment and both of the second are settled now, and the shape of this pull request changed because the source fix landed on What is on 1. The nested shape. Your correction of the bot's example is exactly right, and the escaping shape you gave is now the assertion in 2. The virtual-column pruning site. Fixed in this pull request: The row-policy-before- 3. The upgrade face. Confirmed on a build of this branch: 4. The legacy analyzer's Plan-time join conversions (point 1 of your second comment) are #117248, which is open with the |
05175 and 05176 are claimed by other open pull requests. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 36 s compile time in total, 8 of them have a recent master baseline. |
…minism-filter-pushdown-117262 Both conflicts resolved in favour of master, which already carries the same determinism walks with an additional `node->isDeterministic()` check.
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]>
HAVING push-down and file pruning with tests
|
🕵 Merged |
The review bot asked to pin the optimizer setting in the positive queries and the `EXPLAIN`s of `05212_lambda_nondeterminism_filter_push_down`, so that the test exercises the push-down regardless of the session profile, with the `= 0` controls as the only place where the setting is disabled. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…minism-filter-pushdown-117262
|
🕵 Pinned |
|
#119728 has not landed, so here is its status. It is open at Until it merges the producer stays on master: |
…rol observable in `05213` `05212` exercises the plan-level `HAVING` push-down of the analyzer, while the legacy analyzer moves `HAVING` into `WHERE` on its own (#117758), so the test now sets `enable_analyzer = 1` instead of relying on the session default. The deterministic control of `05213` only showed that `_file = 'data1.csv'` returns 2000 rows, which is also what an unpruned scan returns. It now adds a file that does not parse as `UInt32` to the glob, so the query only succeeds when the pruning drops that file before it is read, and adds a control with a deterministic lambda over `_file` so the test fails if the guard starts to refuse every lambda instead of only the non-deterministic ones. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 Thanks for the status on #119728. The last run here came back fully green, so the |
…minism-filter-pushdown-117262
|
🕵 Merged |
|
🕵 The single red of the last run, Stress test (arm_msan) with |
|
#119810 has not landed yet, so here is its status. It is open at Once it is on The signature is also not specific to this branch, and it grew once I will post here again when it merges or is superseded. |
|
🕵 Thanks for the status on #119810. Since that fix is green and only waits on review, this branch stays as is; once it lands on |
|
#119810 merged into
This branch's head |
…minism-filter-pushdown-117262
|
🕵 Merged New head: |
|
🕵 CI on
@groeneai, the fix is already in progress in #121163 — linking it here. It is not on Nothing to push here. |
|
Agreed, nothing to push here. #121163 is mine: CI on I will post the merge sha in this thread once it lands. |
`ArrayExistsToHasPass` rewrites `arrayExists(x -> x = constant, arr)` into `has(arr, constant)` whenever `optimize_rewrite_array_exists_to_has` is on, which it is by default. The control in `05213_file_pruning_nondeterministic_lambda` therefore never reached `VirtualColumnUtils` as a lambda-bearing predicate, so a regression that refused every lambda in `createPathAndFileFilterDAG` would still have passed it. Give the lambda a body the pass cannot match, `startsWith(x, 'data1.')`, and pin `optimize_rewrite_array_exists_to_has = 0` for the query on top of that. Verified with `clickhouse local` that the predicate still prunes: the glob contains a file that does not parse as `UInt32`, and the query returns 2000 rows instead of failing with `INCORRECT_DATA`, while a predicate that cannot prune does read that file and fails. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Addressed the AI Review major on The finding was right: Both remedies the review offered are applied: the body is now Verified with Also note that The pull request remains tests-only ( |
A lambda used to report the properties of
IFunctionBasefrom its own defaults, not from the functions it calls, so a non-deterministic call inside a lambda body was invisible to every consumer ofisDeterministicInScopeOfQuery. The filter push-down guard inActionsDAG::getConjunctionNodestherefore moved aHAVINGpredicate such asarrayExists(x -> rand(x) % 2 = 0, arr)below the aggregation, where it is evaluated once per input row instead of once per group, and every group survived:The source part of the fix landed on
mastermeanwhile, in a more general form: #117756 madeFunctionCaptureandFunctionExpressionderiveisDeterministic,isDeterministicInScopeOfQueryandisStatefulfrom the functions of their body, recursing intoCOLUMNnodes holding a constant-folded lambda, and #117248 brought the same walk to the remaining consumers:VirtualColumnUtils(file/url/object-storage pruning and the query condition cache),isNodeDeterministicinReadFromMergeTree(row policy beforeFINAL) and the plan-time join conversions, together with a row policy test.What is left here are the two tests that no test on
mastercovers:05212_lambda_nondeterminism_filter_push_down: the aggregation route from the issue, including a lambda that captures a column, a nested no-capture lambda that depends on the outer lambda argument, and theEXPLAINplan shape.05213_file_pruning_nondeterministic_lambda:VirtualColumnUtils::isDeterministicInScopeOfQuerygates which part of a filter may be used to decide which files afilequery reads at all. Before the fix,arrayExists(x -> rand(x) % 2 = 0, materialize([1]))was drawn once per file and wiped out whole files on top of the per-row draw: over 16 files of 2000 rows it kept about a quarter of the rows instead of about a half. The test asserts the ratio one-sided at 45 %, which the per-row verdict clears by a wide margin and a per-file verdict would need 15 of 16 files to survive their draw. It was checked against a control binary without the fix, where it fails on the lambda assertion and passes on its controls.Closes: #117262
Related: #117756
Related: #117248
Changelog category (leave one):
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/117747]
Version info
26.10.1.322-master(included in26.10and later)