Repository navigation
Fix optimize_move_to_prewhere moving non-deterministic functions to PREWHERE under FINAL - #109538
Conversation
|
Workflow [PR], commit [0f52cb6] Summary: ✅
AI ReviewSummaryThis PR fixes real Findings
Final VerdictChanges to the determinism logic look correct on the current head, but the PR is not ready to merge with the AST-path regression coverage removed. LLVM Coverage ReportMeasured on commit 0f52cb6.
Changed lines: Changed C/C++ lines covered: 41/55 (74.55%) · Uncovered code |
|
Updating the branch to fix the |
| SET explain_query_plan_default = 'legacy'; | ||
| SET enable_analyzer = 1; | ||
| SET optimize_move_to_prewhere = 1; | ||
| SET query_plan_optimize_prewhere = 1; |
There was a problem hiding this comment.
This still doesn't exercise the code added in the last two commits. The whole file keeps query_plan_optimize_prewhere = 1, so it only checks the planner ActionsDAG path; there is no case that flips to the legacy AST PREWHERE path and runs a deterministic/non-deterministic WASM UDF on a sorting-key predicate. That means both the new AST fallback in isFunctionDeterministicInScopeOfQuery and the new FunctionUserDefinedWasm::isDeterministicInScopeOfQuery() override can regress without this test catching it. Please add a focused case for query_plan_optimize_prewhere = 0 plus deterministic and non-deterministic WASM filters under FINAL.
…ttings randomization
…final-nondeterministic
…final-nondeterministic
|
Updated the branch with the latest
|
| SET explain_query_plan_default = 'legacy'; | ||
| SET enable_analyzer = 1; | ||
| SET optimize_move_to_prewhere = 1; | ||
| SET query_plan_optimize_prewhere = 1; |
There was a problem hiding this comment.
The generic rand() regression that motivated this PR is still only covered on the planner path. This file pins query_plan_optimize_prewhere = 1, and 04506 only exercises the legacy AST path through the UDF-specific branches. That leaves the FunctionFactory::tryGet(...)->isDeterministicInScopeOfQuery() fallback in isFunctionDeterministicInScopeOfQuery untested for built-in non-deterministic functions: if that lookup regresses, FINAL WHERE (k + rand()) % 2 = 0 can start moving to PREWHERE again with query_plan_optimize_prewhere = 0 and nothing in this patch will fail. Please add a small EXPLAIN SYNTAX / query_plan_optimize_prewhere = 0 case for a built-in predicate like rand().
There was a problem hiding this comment.
Added 04647_prewhere_final_nondeterministic_ast_path, which runs with enable_analyzer = 0 and query_plan_optimize_prewhere = 0 and asserts via EXPLAIN SYNTAX that FINAL WHERE (k + rand()) % 2 = 0 stays in WHERE (and that a deterministic sorting-key condition is still moved). That drives exactly the FunctionFactory::tryGet(...)->isDeterministicInScopeOfQuery() branch of isFunctionDeterministicInScopeOfQuery for a built-in non-deterministic function. Also merged the latest master (the branch was a week behind).
There was a problem hiding this comment.
The AST-path coverage is still missing on the current head. tests/queries/0_stateless/04500_prewhere_final_nondeterministic_condition.sql:4-7 hard-codes query_plan_optimize_prewhere = 1, and there is no remaining FINAL + built-in rand case with query_plan_optimize_prewhere = 0.
That matters because src/Interpreters/InterpreterSelectQuery.cpp:1099 still routes query_plan_optimize_prewhere = 0 through the AST-based MergeTreeWhereOptimizer::optimize, so the fallback added in src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp:809-829 is again untested.
…final-nondeterministic
…tic functions The AI review noted that `04500_prewhere_final_nondeterministic_condition` pins `query_plan_optimize_prewhere = 1`, so the built-in `rand` regression was only asserted on the planner path, and `04506` only drives the UDF branches of the AST fallback. The `FunctionFactory::tryGet(...)->isDeterministicInScopeOfQuery()` branch of `isFunctionDeterministicInScopeOfQuery` was therefore untested. Add a focused test that runs with `enable_analyzer = 0` and `query_plan_optimize_prewhere = 0` and checks via `EXPLAIN SYNTAX` that `FINAL WHERE (k + rand()) % 2 = 0` is not moved to `PREWHERE`, while a deterministic sorting-key condition still is. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…final-nondeterministic
The review asked for focused coverage of the new `FunctionCapture::isDeterministic` override, which is what keeps a filter whose lambda body calls a non-deterministic function from looking deterministic to consumers that only see the capture node in the outer `ActionsDAG`. The new test drives the query condition cache: a `PREWHERE`/`WHERE` filter with `rand` inside a lambda body must not store granule statistics, while the same filter with a deterministic body must. Verified by A/B: with the override removed, the non-deterministic case starts writing an entry to `system.query_condition_cache` (1 instead of 0), so the test discriminates.
…s defence in depth
|
Dear @novikd, 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. |
|
There is a related test failure |
|
The determinism gate this PR adds is bypassed when the non-deterministic call consumes the lambda argument, so the exact bug the PR fixes survives through constant-folded lambda captures. Verified both ways on this PR's CI binary (head af16b6d, 26.9.1.1): CREATE TABLE t (k Int32, data String, v UInt64) ENGINE = ReplacingMergeTree(v) ORDER BY k;
INSERT INTO t SELECT number, 'old', 1 FROM numbers(10000);
INSERT INTO t SELECT number, 'new', 2 FROM numbers(10000);
SET optimize_move_to_prewhere_if_final = 1;
SELECT countIf(data = 'old') FROM t FINAL WHERE arrayExists(x -> rand(x) % 2 = 0, [k]);On the PR binary this still moves to Mechanism: when the only non-deterministic node consumes the lambda argument, it stays inside the captured DAG and all captured children are constants. One correction to the open review thread on this class: the example proposed there, Fix direction consistent with existing precedent in the file (non-deterministic SQL-UDF bodies already refuse folding by overriding the flag): extend Two adjacent observations from the same verification pass, for completeness: the query condition cache leg is safe even for the folded carrier (the folded |
…final-nondeterministic # Conflicts: # src/Functions/FunctionsMiscellaneous.h
…final-nondeterministic # Conflicts: # src/Functions/UserDefined/UserDefinedWebAssembly.cpp
…final-nondeterministic
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes bugs where non-deterministic conditions were moved to PREWHERE under FINAL +
optimize_move_to_prewhereoptimizationValidation:
Workflow [PR]
Sync PR [sync-upstream/pr/109538]