Repository navigation
Do not JIT-compile a lambda body in a plan that is shipped to another node - #119332
Conversation
… node
With `serialize_query_plan = 1`, a higher-order function whose lambda body is an `AND` of comparisons
failed on the shard with
Code: 46. Unknown function and(UInt8, less(UInt64, 1000 : UInt16)). (UNKNOWN_FUNCTION)
for example
SELECT mapExists((k, v) -> k LIKE '%2' AND v < 1000, col) FROM dist
SETTINGS prefer_localhost_replica = 0, serialize_query_plan = 1;
That "function name" is `CompileDAG::dump` of a JIT-compiled expression. Unlike an ordinary expression,
which travels as an `ActionsDAG` and becomes `ExpressionActions` only when the receiving node builds
its pipeline, a lambda's `ExpressionActions` is built while the plan is built, so its DAG - already
compiled, because JIT happens in the `ExpressionActions` constructor - is the one that gets serialized.
`ActionsDAG::serialize` writes `function_base->getName()`, and for a compiled node that name is not
something `FunctionFactory` knows. The extra conditions in the report follow from what the JIT accepts:
an `AND` of comparisons over native types large enough to be worth compiling, which is also why an
all-`UInt8` variant, a single comparison or an `OR` slipped through.
Keep the lambda body uncompiled when the planner is building a logical plan, which is exactly the plan
that gets serialized and shipped (a shard under `serialize_query_plan`, or a replica in the plan-based
parallel replicas mode). Nothing is lost: that plan is never executed here, and the receiving node
rebuilds the lambda's `ExpressionActions` - with compilation enabled - from the serialized DAG.
Closes: #115310
|
Workflow [PR], commit [cdd93f8] Summary: ✅
AI ReviewSummaryThis PR fixes the Findings
Final Verdict
LLVM Coverage ReportMeasured on commit cdd93f8.
Changed lines: Changed C/C++ lines covered: 20/20 (100.00%) · Uncovered code |
|
The red stateless jobs are all the same unrelated test: |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units52 translation units recompiled, 460 s compile time in total, 52 of them have a recent master baseline. |
…compiled-lambda-115310
…compiled-lambda-115310
Add the same lambda shape read through a trivial view and a `Merge` table over the `Distributed` table with `serialize_query_plan = 1`, the two paths that wrap the `PlannerContext` into a new `Context`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…allel replicas `parallel_replicas_plan_based = 1` builds an ordinary local plan (not a logical one), and `QueryPlanOptimizations::applyParallelReplicas` later serializes a fragment of it for the replicas. A lambda body compiled while that plan was built reached the replicas as a `CompileDAG::dump` name and failed there with `UNKNOWN_FUNCTION`. `PlannerContext::isLogicalPlan` is replaced by `mayBeSerializedForRemoteExecution`, which also holds when plan-based parallel replicas can be used on the initiator. New test `05321_parallel_replicas_plan_based_lambda_and` fails without the change and passes with it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| : query_context(std::move(query_context_)) | ||
| , global_planner_context(std::move(global_planner_context_)) | ||
| , is_ast_level_optimization_allowed(!(query_context->getClientInfo().query_kind == ClientInfo::QueryKind::SECONDARY_QUERY || select_query_options_.ignore_ast_optimizations)) | ||
| , may_be_serialized_for_remote_execution(select_query_options_.build_logical_plan || mayShipPlanFragmentToParallelReplicas(query_context)) |
There was a problem hiding this comment.
may_be_serialized_for_remote_execution is latched too early. Planner::buildQueryPlanIfNeeded can still disable parallel replicas afterwards for several unsupported cases (disableParallelReplicasForMultipleTablesQueryIfNeeded, parallel_replicas_allow_in_with_subquery = 0, additional_table_filters, FINAL / STREAM), but visitLambda will keep seeing true here and turn off JIT for every lambda body even though the query has already fallen back to a pure local plan. The copy constructor at line 102 then preserves the stale bit into wrapped PlannerContexts for trivial views / Merge as well.
That means enabling parallel_replicas_plan_based = 1 can now regress local-only queries that happen to become ineligible for parallel replicas. Please keep only the immutable build_logical_plan carrier latched/copy-propagated, and derive the plan-based-parallel-replicas half from the current Context at use time so a local fallback re-enables lambda compilation.
…tion Since ClickHouse#125615, ReadFromCluster::applyFilters serializes the filter conjuncts that isSerializableFilterNode accepts into cluster_table_function_pushed_down_filter, and each replica rebuilds them with ActionsDAG::deserialize. The gatekeeper also accepted lambda carriers: a constant-folded lambda (ColumnConst(ColumnFunction)) and a capturing lambda (FunctionCapture). A lambda body is an ExpressionActions built while the initiator plans the query, and its constructor JIT-compiles the DAG in place, replacing the nodes with an LLVMFunction named after CompileDAG::dump(). The serialized filter then carries that name, and the replica's FunctionFactory::get throws UNKNOWN_FUNCTION once the body has been compiled (after min_count_to_compile_expression runs). The planner keeps lambda bodies uncompiled only for plans that are shipped as a whole (ClickHouse#119332, mayBeSerializedForRemoteExecution). This plan is executed on the initiator, so refuse Function-typed nodes in the gatekeeper instead. The check runs before the recursion, so the whole atom stays on the initiator and the other conjuncts are still sent. The initiator applies the full filter, so results do not change; the replicas only stop pre-filtering by lambda conjuncts, as before ClickHouse#125615. Extend 05394_cluster_function_filter_pushdown_to_replicas with a folded and a capturing lambda case. Closes ClickHouse#128422 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix
UNKNOWN_FUNCTIONon a shard or a replica for a higher-order function whose lambda body is anANDof comparisons whenserialize_query_plan = 1orparallel_replicas_plan_based = 1: a JIT-compiled lambda body was serialized into the plan.Documentation entry for user-facing changes
...
With
serialize_query_plan = 1, a higher-order function whose lambda body is anANDof comparisons failed on the shard withfor example
That "function name" is
CompileDAG::dumpof a JIT-compiled expression. Unlike an ordinary expression, which travels as anActionsDAGand becomesExpressionActionsonly when the receiving node builds its pipeline, a lambda'sExpressionActionsis built while the plan is built, so its DAG — already compiled, because JIT happens in theExpressionActionsconstructor — is the one that gets serialized.ActionsDAG::serializewritesfunction_base->getName(), and for a compiled node that name is not somethingFunctionFactoryknows. The extra conditions in the report follow from what the JIT accepts: anANDof comparisons over native types large enough to be worth compiling, which is also why an all-UInt8variant, a single comparison or anORslipped through.Keep the lambda body uncompiled when the plan being built may be serialized and shipped: a logical plan for a shard under
serialize_query_plan, or the local plan from which plan-based parallel replicas (parallel_replicas_plan_based) later cut a fragment for the replicas. Nothing is lost: that plan is never executed here, and the receiving node rebuilds the lambda'sExpressionActions— with compilation enabled — from the serialized DAG.New tests:
05199_serialize_query_plan_lambda_andand05321_parallel_replicas_plan_based_lambda_and(the latter fails on the replicas withUNKNOWN_FUNCTIONwithout the change). Verified in both directions, and a differential run of thelambda/arrayMap/arrayExists/mapExists/serialize_query_plan/compile_expressions/distributed_planstateless tests shows no new failures.Closes: #115310
Workflow [PR]
Sync PR [sync-upstream/pr/119332]
Version info
26.10.1.1533-master(included in26.10and later)