Repository navigation
Hash each node of an ActionsDAG once, fixing a planning hang in the hash table statistics key - #129184
Conversation
`ActionsDAG::Node::updateHash` mixed in the node's own fields and then
recursed into every child with no memo, so a node shared by two parents
was hashed once per path. A chain of `a_{i+1} = a_i + a_i` folded into
one DAG has O(depth) nodes but 2^depth paths, so hashing it was
exponential. `ActionsDAG::updateHash` was worse: it walked every edge
and called the recursive node hash at each visit.
Since `FilterStep::serialize` puts `getOutputIdentity` of the filter into
the hash table statistics key, planning a filter over 30+ nested
`x + x` aliases below a `JOIN` or a keyed `GROUP BY` never finished at
default settings, and the walk does not react to `KILL QUERY`.
Now a node's digest is SipHash128 of its own fields (`updateOwnHash`),
its number of children and its children's digests, computed once per
node by an iterative post-order walk with a per-call memo.
`Node::updateHash` mixes in the digest of the node, and
`ActionsDAG::updateHash` the number of outputs and the digest of each
output, sharing one memo. Both are O(nodes + edges).
Hash values change, equality does not: two subtrees hash equal iff their
own fields and ordered children are equal, whether a child is shared or
duplicated. Every consumer is an in-memory, per-process key (hash table
statistics, query condition cache, prewhere and top-K keys), and nothing
persists these hashes.
The `collect_hash_table_stats_during_joins = 0` pin in 05359 was the
workaround for this bug and is removed, so that test now covers the join
statistics key at depth 36. The new 05359 test covers the aggregation
statistics key, both SQL tests check in `system.text_log` that their
query reached the statistics cache, and the gtest covers
`ActionsDAG::updateHash`, which neither SQL test reaches, and that
different DAGs still hash differently.
Closes ClickHouse#126250
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (Gate B, 3 rounds)
Final round: 0 findings. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261104-052800 |
This comment was marked as resolved.
This comment was marked as resolved.
|
Workflow [PR], commit [3023892] Summary: ✅
AI ReviewSummaryThe PR replaces repeated recursive hashing of shared Final VerdictStatus: ✅ Approve |
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, 19 s compile time in total, 7 of them have a recent master baseline. |
…filter safe under concurrent runs The hash table statistics cache is process-wide, and every run of the test builds the same keys. A run that misses the cache and then finds an equal entry stored by a concurrent run in the meantime logs nothing under its own query_id: `HashJoin` updates the entry without a lookup and skips an equal one. In the flaky check 7 of 300 runs printed 0 for the oracle this way. Run the first query once more after it has finished, when its entry is cached, and count the log rows of both runs. Locally, 64 or 96 concurrent runs on a fresh server: 16 of 160 runs failed before, 0 of 288 after (14 of them logged nothing for the first query and were covered by the second). With `collect_hash_table_stats_during_joins = 0` the oracle still prints 0. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #129184 to 26.10: Hash each node of an ActionsDAG once, fixing a planning hang in the hash table statistics key
…ng a planning hang in the hash table statistics key
…ild loop The conflict resolution of the cherry-pick (#129668) kept the old body of Node::updateHash as the new updateOwnHash, so: - updateOwnHash is defined in ActionsDAG.cpp but not declared in ActionsDAG.h (the declaration came with #126148, which is not on 26.10), and the build fails with "no member named 'updateOwnHash'". - updateOwnHash still ends with the recursion over the children, so each per-node digest re-hashes the whole subtree and the planning hang of #126250 stays. Add the declaration as on master and remove the loop. The tree now equals 26.10 + #129184 with only the #126148 updateOwnHash split, as on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #129184 to 26.10: Hash each node of an ActionsDAG once, fixing a planning hang in the hash table statistics key
…ng a planning hang in the hash table statistics key
…ild loop The conflict resolution of the cherry-pick (#129668) kept the old body of Node::updateHash as the new updateOwnHash, so: - updateOwnHash is defined in ActionsDAG.cpp but not declared in ActionsDAG.h (the declaration came with #126148, which is not on 26.10), and the build fails with "no member named 'updateOwnHash'". - updateOwnHash still ends with the recursion over the children, so each per-node digest re-hashes the whole subtree and the planning hang of #126250 stays. Add the declaration as on master and remove the loop. The tree now equals 26.10 + #129184 with only the #126148 updateOwnHash split, as on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #126250
Related: #126148
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed query planning that never finished for a filter over a subexpression that is reused many times (for example
x + xin 30 nested subqueries) below aJOINor aGROUP BYwith keys, while hash table statistics are collected (the default). Such a query also ignoredKILL QUERYandmax_execution_time.Description
On 26.10.2.103, a
SELECTwith a filter over a subexpression reused at every level of 30 or more nested subqueries (x + x) below aJOINor aGROUP BYwith keys never finishes planning at default settings, and neitherKILL QUERYnormax_execution_timestops it.Since #116781 the hash table statistics key of a join or an aggregation includes
ActionsDAG::getOutputIdentityof each filter below it.ActionsDAG::Node::updateHashhashed a node's own fields and then recursed into every child with no memo, so a node shared by two parents was hashed once per path: a chain ofa_{i+1} = a_i + a_ihas O(depth) nodes and 2^depth paths.ActionsDAG::updateHashalso visited each node once per path and hashed its whole subtree at each visit.Now each node gets a 128-bit digest of its own fields (
updateOwnHash, split out by #126148), its number of children and its children's digests, computed once per node with a per-call memo. Both functions become O(nodes + edges), covering every hash caller (statistics keys, query condition cache, prewhere and top-K keys). Hash values change, equality does not, and every consumer is an in-memory key. Other per-path walks are separate code, not changed here:VirtualColumnUtils::isDeterministic,strictOnRelationsand JIT expression names (#123724).Validation (debug build):
05359_hash_table_stats_key_shared_nodes(aggregation key) and05359_partial_result_shared_nodes_join_filterwith itscollect_hash_table_stats_during_joins = 0workaround removed (join key) time out on base and pass with the fix, 50/50 with randomized settings; both check insystem.text_logthat the query reached the statistics cache. A gtest coversActionsDAG::updateHash, which neither SQL test reaches.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/129184]
Version info
26.11.1.1487-master(included in26.11and later)