Repository navigation
Hash each AND-chain operand once in optimize_and_compare_chain - #131361
Conversation
`tryOptimizeAndCompareChain` keyed every container (comparison pairs, constants, visited set, derived-conjunct filters, existing conjuncts) by `QueryTreeNodePtr` and passed raw nodes through the DFS, so every lookup re-ran `getTreeHash`. The hash of a column also hashes its source, so when the operands are columns of a large inlined subquery (many non-materialized CTEs) each lookup walks the whole subquery: about 30 walks per chain for 5 distinct operands. On the query from ClickHouse#131134 (CTE depth 8) the visitor hashed 16.6M nodes for a 15K-node tree. Compute a `QueryTreeNodePtrWithHash` once per operand per call and pass the key through the containers and the DFS (the pattern ClickHouse#108757 used for `resolveLambda`). The existing-conjunct sets hashed every conjunct and every candidate; a derived conjunct is always `comparison(operand, constant)`, so index existing conjuncts of that shape by the key of their first argument and decide with `isEqual` inside the bucket. The derived set is unchanged. Nodes hashed for the ClickHouse#131134 query drop from 16.6M to 3.4M, which is below the default `optimize_and_compare_chain_max_hash_work` budget, so at default settings the derivation now completes instead of being reverted. With the budget off (`compatibility` before 26.7) query analysis is 2.1x faster. Related: ClickHouse#131134 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Hashing each operand once removed every `getTreeHash` call from the DFS of `tryOptimizeAndCompareChain`, so `optimize_and_compare_chain_max_hash_work` no longer bounded the traversal. Each DFS step now charges the budget the hash work its precomputed key took, which is what the step used to pay for looking the operand up. Related: ClickHouse#131134 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…by operand and constant A lookup by an operand key that `tryOptimizeAndCompareChain` already computed is charged the hash work of that key, so `optimize_and_compare_chain_max_hash_work` counts what it counted when every container access re-hashed the operand. Existing conjuncts of the shape `comparison(operand, constant)` are indexed by operand key and then constant key, so deduplicating a derived conjunct no longer scans every conjunct written on the same operand. Related: ClickHouse#131134 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in work budget, as hashing the whole conjunct did. Related: ClickHouse#131134 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cessed, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rewrite the operand-key memo comment as a standalone statement (drop the reference to the previous budget accounting). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (rounds 1-5)
|
Pre-PR validation gate (click to expand)
|
|
Workflow [PR], commit [08460ad] Summary: ❌
AI ReviewSummaryThe PR reduces repeated operand hashing in Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+39.31 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only 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. |
CI finish ledger - 08460adEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261113-210012 |
c14cab4
Cherry pick #131361 to 26.10: Hash each `AND`-chain operand once in `optimize_and_compare_chain`
…imize_and_compare_chain`
Backport #131361 to 26.10: Hash each `AND`-chain operand once in `optimize_and_compare_chain`
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Query analysis is faster for queries whose
ANDchains of comparisons read columns of large subqueries, such as many inlined CTEs:optimize_and_compare_chainnow hashes each operand once instead of on every lookup. Analysis of the query from #131134 is 2x faster with the work budget off and 1.5x faster at default settings.Description
optimize_and_compare_chain = 1made analysis of the query in #131134 (9 levels of inlined CTEs, each a self-join with anANDchain of comparisons) 3.5x slower.tryOptimizeAndCompareChainkeyed its maps and sets byQueryTreeNodePtrand passed raw nodes through its DFS, so every lookup recomputedgetTreeHash. The hash of a column includes its source, and every operand is a column of an inlined subquery, so each lookup walked a whole subquery: 16.6M nodes hashed for a 15K-node tree.Each operand is now hashed once per call. A lookup by an already computed key is still charged that key's hash work, and a lookup of an existing conjunct its full hash work, so
optimize_and_compare_chain_max_hash_workstill bounds the chain traversal. Existing conjuncts of the shape of every derived one,comparison(operand, constant), are indexed by operand and constant; other conjuncts are no longer hashed, so anANDclose to the budget may now fit in it.On the issue query (
EXPLAIN QUERY TREEis byte-identical before and after at depths 1 to 8, budget off and at defaults):compatibilitybefore 26.7, as in the issue), medianQueryAnalysisMicroseconds697 ms -> 350 ms (chain off: 203 ms);countIfchains over window-function andARRAY JOINsubqueries,compatibility = '24.10'): analysis 90 ms -> 39 ms (chain off: 28 ms).The 28 stateless tests of the optimization pass 3 randomized runs.
04492_optimize_and_compare_chain_work_budgetnow also checks that the budget bounds the chain traversal. The new performance test runs the issue query with the budget off and at the default: 1.39 s -> 0.71 s and 0.65 s -> 0.46 s locally.Related: #131134
Related: #108757
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131361]
Version info
26.11.1.2445-master(included in26.11and later)26.10.7.18