Repository navigation
Do not rebuild nested ANDs at every level in optimize_and_compare_chain - #131479
Conversation
…chain` `tryOptimizeAndCompareChain` runs for every `and` node, so a left-deep `((a AND b) AND c) AND ...` chain of depth n gets n calls. Each call read its operands through `getFlattenedLogicalExpression`, which recursed into every nested same-name function and, at every level that had nested children, built a throw-away `FunctionNode` and resolved it (`FunctionFactory::get` plus a variadic signature check over all its arguments). A call on a depth-k subtree did O(k^2) argument checks, the chain ~O(n^3); `optimize_and_compare_chain_max_hash_work` counts only tree hashing, so it never bounded this. `collectFlattenedLogicalArguments` collects the same operands (same nodes, same order) without creating any node. `getFlattenedLogicalExpression` uses it and builds and resolves only the top node, which also serves `optimize_extract_common_expressions`; the compare chain uses the list directly. The query tree is unchanged. The nested-AND repro from ClickHouse#131134 (~105 levels, RelWithDebInfo, median QueryAnalysisMicroseconds of 7 interleaved runs): 163.5 ms -> 47.1 ms at defaults, 7.2 ms with the chain off; flat AND and the inlined-CTE repro unchanged. EXPLAIN QUERY TREE and results byte-identical master vs this change on 13 cases. Adds a performance test with that query, and a 04032 case whose derivation needs a conjunct two levels deep, which fails if the flattening stops at one level. Closes ClickHouse#131134 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; 0 findings) plus my own cold review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261113-225700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261113-214900 |
|
Workflow [PR], commit [e03c2b8] Summary: ✅
AI ReviewSummaryThe PR speeds up analysis of deeply nested Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline. |
fa13215
Backport #131479 to 26.10: Do not rebuild nested `AND`s at every level in `optimize_and_compare_chain`
Closes: #131134
Related: #131361
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Query analysis is faster for a condition written as a deeply nested
ANDtree, such as((a AND b) AND c) AND ..., whenoptimize_and_compare_chainis enabled (the default).Description
With
optimize_and_compare_chain, analysis of the nested-ANDworkload in #131134 (a left-deepWHERE ((((a AND b) AND c) AND ...)of ~105 comparisons) is 10x slower on 26.10 (23x on master here); the same conjuncts as a flatANDare not affected, andoptimize_and_compare_chain_max_hash_workdoes not bound it.tryOptimizeAndCompareChainruns for everyandnode of the tree. It read its operands throughgetFlattenedLogicalExpression, which recursively built and resolved (function lookup plus a signature check over all arguments) a throw-awayandnode at every nested level: O(k^2) argument checks per call on a depth-k subtree, ~O(n^3) for the whole chain.The new
collectFlattenedLogicalArgumentscollects the flattened operands without creating nodes.getFlattenedLogicalExpressionuses it and builds and resolves only the top node;tryOptimizeAndCompareChainuses the list directly. The operands (same nodes, same order) are unchanged, and so is the query tree:EXPLAIN QUERY TREEand results are byte-identical between master and this PR on the #131134 workloads and on nested derivation cases.QueryAnalysisMicroseconds, RelWithDebInfoclickhouse local, median of 7 interleaved runs:AND, defaultsAND,compatibility = '24.10'(chain off)AND, chain onTogether with #131361 (per-operand hashing, the inlined-CTE shape of #131134) the nested query takes 22.8 ms.
Tests: new performance test
analyzer_and_compare_chain_nested_and.xml;04032_and_comparison_filter_optimization_2gets a case whose derivation needs a conjunct two levels deep (fails if the flattening is not recursive).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131479]
Version info
26.11.1.2444-master(included in26.11and later)26.10.5.32