Repository navigation
Conversation
…ents
`findLazyExecutedNodes` walks the lazily executable arguments of every
short-circuit function (`if`, `multiIf`, `and`, `or`, ...). It only
remembered the nodes it selected as lazy, so a node it expanded and did
not select was expanded again on every path that reaches it. On a shared
DAG such as nested `a_{k+1} = a_k + a_k` subqueries (merged into one DAG
by `query_plan_merge_expressions`), every `plus` is such a node, and
planning took time exponential in the depth (ClickHouse#133492: depth 25 took
5.4-6.6 s instead of 0.43 s).
Remember the nodes found not lazy executed as well and skip them like
nodes that cannot be lazy executed. Within one walk the flag
`force_enable_lazy_execution` is fixed, and a node found not lazy has no
lazy node in its subtree, so a second expansion would return the same
result: the set of lazy executed nodes does not change. An ALIAS is only
remembered when its child chain is not lazy, because a lazy one must keep
reporting `has_lazy_node` to its other parents. The memo is per
short-circuit function because the flag can differ between functions.
Closes ClickHouse#133492
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261123-210205 |
|
Workflow [PR], commit [f92fc72] Summary: ✅
AI ReviewSummaryThe PR removes exponential traversal of shared, non-lazy expression nodes during short-circuit planning. The per-function cache preserves lazy propagation through aliases. The supplied before/after measurements support the performance claim, and Final VerdictStatus: ✅ Approve |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261123-143701 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
The first query of `05871_short_circuit_shared_dag_planning_time` nested 30 subqueries, and planning 30 nested subqueries needs more than the 5% of the stack that `checkStackSize` allows under TSan: the flaky check failed 22 of 50 runs with `TOO_DEEP_RECURSION`. Use 10 nested subqueries whose expression doubles the paths three times per level instead: the DAG still has 2^30 paths, so master still takes about 100 s (103 s in a debug build) and the fix 0.3 s. With the stack limited to the TSan allowance, a debug build trips at 28 levels for both shapes; the new test passed 50 of 50 runs there with random settings, the old one failed every run. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #133492
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Planning a query with a short-circuit function (
if,multiIf,and,or, ...) whose argument reuses a subexpression, such as nested subqueries computinga + a, now takes time linear in the size of the expression instead of exponential in its depth. Closes #133492.Description
In #133492,
if(x < 0, a25, 0)over 25 nested subqueries witha{k+1} = a{k} + a{k}takes 5.4-6.6 s on 26.11 instead of 0.43 s, and each level doubles it.findLazyExecutedNodes(ExpressionActions.cpp) walks the lazily executable arguments of each short-circuit function. It remembered only the nodes it selected as lazy, so a node it expanded and did not select (here everyplus) was expanded again on every path to it: 2^depth expansions of the DAG thatquery_plan_merge_expressionsbuilds from the subqueries.The walk now also remembers, per short-circuit function, the nodes it found not lazy and skips them. Within one walk such a node cannot become lazy later, so the set of lazy nodes does not change. An
ALIASis remembered only when nothing below it is lazy. The memo is per function becauseforce_enable_lazy_executioncan differ between functions. Thesrc/change is under 25 lines.Validation (debug build),
short_circuit_function_evaluation = 'enable': depth 25 3.93 s to 0.28 s, depth 40 0.29 s (disable: 0.25-0.29 s on both). A local copy of the old walk compared both sets on every planning and found no difference in the 118 short-circuit stateless tests with random settings, without them and withforce_enable; it fires on a deliberately broken memo. New test05871_short_circuit_shared_dag_planning_time: 10 nested subqueries, 2^30 paths (0.36 s; 103 s on master), and a node shared by two lazy arguments; 50/50 with random settings, also at the TSan stack budget.Siblings:
compile_expressions = 1andanduse the same walk and were exponential too; both are flat now. The other walks of this step are memoized.WHERE x > 0 AND a30 > 0over the same subqueries is still exponential, also withshort_circuit_function_evaluation = 'disable': that is #123724, not changed here.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/133893]