Repository navigation
Respect UDF determinism in the AST predicate push-down into distributed subqueries - #119865
Conversation
…down respects their determinism `ExpressionInfoVisitor` only consulted `FunctionFactory`, so an `EXECUTABLE` or `WASM` user-defined function looked like an unknown function and was treated as deterministic. With `enable_analyzer = 0` and `enable_optimize_predicate_expression = 1`, `PredicateExpressionsOptimizer` then duplicated a predicate calling a non-`DETERMINISTIC` UDF into the subquery `HAVING` while keeping the outer copy, evaluating it twice per row and silently dropping rows, the same pattern #117249 fixed for the query-plan join disjunctions push-down. Resolve user-defined functions through `UserDefinedExecutableFunctionFactory` and `UserDefinedWebAssemblyFunctionFactory` first, the way `ActionsVisitor` and `TreeOptimizer` do. Follow-up to the review of #117249 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [f1f9e98] Summary: ✅
AI ReviewSummaryThis PR fixes the result-corruption case for non-deterministic Findings
Final Verdict
LLVM Coverage ReportMeasured on commit f1f9e98.
Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
…pressionInfoVisitor` `UserDefinedExecutableFunctionFactory::tryGet` is not non-throwing: it constructs a `UserDefinedFunction` with an empty `parameters` array, whose constructor raises `BAD_ARGUMENTS` for a UDF that declares command parameters, such as `test_function_with_parameter`. That turned a legacy optimizer walk over a predicate (or over a subquery `SELECT` list, through `hasNonRewritableFunction`) into a query failure. `EXECUTABLE` UDFs always report `isDeterministicInScopeOfQuery` as `false` and are not stateful, so nothing has to be instantiated: use `UserDefinedExecutableFunctionFactory::has` and mark the expression as non-deterministic directly. Extend `05212` with the parametric UDF in the outer `WHERE` and in the subquery `SELECT` list. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…e-pushdown-udf-determinism
…xecutable UDF
The analyzer can no longer be disabled by a query since `enable_analyzer` was
deprecated, so the test could not run any more: `--enable_analyzer=0` is refused with
`SETTING_CONSTRAINT_VIOLATION`. `EXPLAIN AST optimize = 1` still runs the old
interpreter, so it remains an entry point into `TreeRewriter` and
`PredicateExpressionsOptimizer`. Assert on the number of occurrences of the function in
the dumped AST: one means the predicate stayed in the outer query, two mean it was
copied into the subquery as well.
Add the parametric executable UDF the review asked for to
`test_executable_user_defined_function`. The stateless configuration has no UDF that
actually declares command parameters - `test_function_with_parameter` puts its
`{param:UInt64}` placeholder in a `command_argument`, and parameters are only extracted
from `command` - while the integration test already ships
`test_function_parameter_python`.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119865&sha=72223ff3247cad9e5bcfe947744ccfd82d022477&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.
#91602 tracked the same STID and was closed on 2026-02-07, so this is a recurrence and there is no open issue for it. |
|
Already fixed and merged: #119728, at 04:30 UTC today ( The header names the cause. Nothing to do on this pull request. The report you linked is from CIDB agrees so far: all 98 rows of this signature in the last 14 days carry the identical One caveat on the two |
…e-pushdown-udf-determinism
…ate push-down `PredicateExpressionsOptimizer` was removed from master by `7d148af437c3`, so the legacy push-down this PR fixed no longer exists, and `05212` (which reached it via `EXPLAIN AST optimize = 1`) failed. The remaining user of `ExpressionInfoVisitor` is `PredicateRewriteVisitor`, which `ReadFromRemote` uses to push a predicate into the `HAVING` of the query sent to a shard (`allow_push_predicate_ast_for_distributed_subqueries`). Two review findings are addressed: - `hasNonRewritableFunction` ignored `is_deterministic_function`, so `SELECT count() FROM (SELECT udf(k) AS v FROM remote(...)) WHERE v % 2 = 0` sent `HAVING v % 2 = 0` to the shard even for a non-deterministic UDF, which refers to the function once more. It now refuses the rewrite. - `EXECUTABLE` UDFs were hard-coded as non-deterministic, although the configuration has a `deterministic` flag. The new `UserDefinedExecutableFunctionFactory::tryGetIsDeterministic` reads it from the loaded configuration without constructing `UserDefinedFunction`, so a parametric UDF still does not throw `BAD_ARGUMENTS` during the walk. `05212` is replaced by `05260`, which checks the query text the shard receives, and the integration test now covers a parametric, a non-deterministic and a deterministic executable UDF through the same path. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
| /// builds a `UserDefinedFunction` with an empty `parameters` array, and that constructor throws | ||
| /// `BAD_ARGUMENTS` for a parametric UDF such as `test_function_parameter_python(1)(k)`, which would | ||
| /// turn a mere optimizer walk into a query failure. | ||
| if (auto is_deterministic = UserDefinedExecutableFunctionFactory::tryGetIsDeterministic(ast_function.name, data.getContext())) |
There was a problem hiding this comment.
EXECUTABLE UDFs are still not safe to rewrite based on deterministic alone. UserDefinedFunction::executeImpl always performs external work and increments ExecutableUserDefinedFunction* profile events on every evaluation, so pushing the outer predicate into shard HAVING still duplicates an observable action even when the returned value is stable. For a function like test_function_bash_deterministic, this keeps the evaluation-count bug alive: the shard now runs the child twice per row, once to produce v and once more for HAVING v != ''.
Please keep executable UDFs out of this rewrite (or teach ExpressionInfoVisitor / hasNonRewritableFunction about hasObservableSideEffects) instead of treating deterministic = true as sufficient here.
|
@alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
Master PR #119865 made `hasNonRewritableFunction` refuse a non-deterministic select list as well, so the query shipped to the replicas no longer carries the condition for `v_nondet_pr_rewrite_shapes` (`rand()`), and the read is `Default`, not `InOrder`. The condition still reaches the local read as a PREWHERE. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122570&sha=9e0efd96ece0309ec7cee2cde51889e0a90af8ae&name_0=PR PR: #122570 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #117249
ExpressionInfoVisitordecides whether a predicate may be pushed into a subquery. It only consultedFunctionFactory, and user-definedEXECUTABLEandWASMfunctions are not registered there, so they looked like unknown functions and were treated as deterministic. #117249 fixed the same pattern for the query-plan join disjunctions push-down, and its review pointed out this gap in the AST visitor.The legacy
PredicateExpressionsOptimizerhas since been removed from master (7d148af437c3). The remaining user of the visitor isPredicateRewriteVisitor:ReadFromRemoteuses it to push a predicate over a column of a distributed subquery into theHAVINGof the query it sends to the shard (allow_push_predicate_ast_for_distributed_subqueries). ForSELECT count() FROM (SELECT udf(k) AS v FROM remote(...)) WHERE v % 2 = 0, the shard receivedHAVING v % 2 = 0. That pushed copy calls the function again, so the shard can filter on a different value than the one it returns, even for a non-deterministic UDF.FunctionFactory, the same wayActionsVisitordoes. For anEXECUTABLEUDF it reads thedeterministicflag from the loaded configuration (UserDefinedExecutableFunctionFactory::tryGetIsDeterministic) instead of constructing the function.tryGetwith an emptyparametersarray raisesBAD_ARGUMENTSfor a UDF that declares command parameters, which would have turned the optimizer walk into a query failure.hasNonRewritableFunctionnow refuses the rewrite when the subquery'sSELECTlist contains a non-deterministic function.A UDF declared deterministic (
WASM ... DETERMINISTIC, or<deterministic>true</deterministic>for anEXECUTABLEUDF) is still pushed down.05260checks the query text the shard receives.test_executable_user_defined_functioncovers a parametric, a non-deterministic and a deterministicEXECUTABLEUDF through the same path.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the AST predicate push-down into distributed subqueries treating non-deterministic
EXECUTABLEandWASMuser-defined functions as deterministic. It pushed a predicate over such a function's result to the shard, which evaluated the function again.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/119865]
Version info
26.10.1.840-master(included in26.10and later)