Repository navigation
Keep same-type GROUP BY expressions apart in the hash table statistics key - #126148
Conversation
A row- and layout-preserving `ExpressionStep` that is not a pure permutation contributed nothing to the hash-table statistics key, and since ClickHouse#116781 a key column above it is identified by its header position only. So `GROUP BY a + 1` and `GROUP BY intDiv(a, 1000000)` over the same `UInt64` column shared one entry, and the 2-group aggregation preallocated the 2M cells recorded by the other query. Through a subquery (`SELECT k FROM (SELECT intDiv(a, N) AS k FROM t) GROUP BY k`) the key column is named `__table1.k` for every expression, so that shape already collided on 26.8 and 26.9. Such a step now contributes `ActionsDAG::getComputedOutputsHash`: what each output computes, with `ALIAS` nodes looked through and an `INPUT` identified by its position in the step's input header, the way `writeCacheKeyColumnName` identifies a column. The hash does not change when expression merging absorbs an in-order rename into the step, which automatic parallel replicas needs: the single-replica plan folds the rename into the projection and the parallel-replicas plan keeps them apart. Serializing the DAG instead writes the absorbed `ALIAS` nodes, and `SELECT repeat(a, 100) AS a, b` at default merging then stops collecting statistics again, the regression adcb292 removed. `Node::updateHash` is split into the node's own content (`updateOwnHash`) and the recursion into its children; existing hashes are byte-identical. A lambda's captured names are still hashed by name, so a lambda that captures a column is not rename-invariant; at worst automatic parallel replicas does not match such a shape, the fail-closed direction. Closes ClickHouse#125826 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) plus my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261015-223400 |
|
Workflow [PR], commit [ae00155] Summary: ❌
AI ReviewSummaryReviewed #126148 at Final VerdictStatus: ✅ Approve |
|
cc @nickitat @KochetovNicolai, could you review this? A row- and layout-preserving |
A build-independent cache key drops every constant's name, and with `with_variable_size_constant_values = false` it also dropped the value of any constant without a fixed size. A string or array literal then had no identity left in the key, so `GROUP BY trimBoth(s, 'x')` and `GROUP BY trimBoth(s, '0123456789')` shared one hash-table statistics entry, and so did `formatDateTime(d, '%Y')` and `formatDateTime(d, '%F %T')`, whose step is keyed by its serialized form. 26.8 and 26.9 keep them apart: there the constant's name, which spells a literal's value, was still in the key. `isConstantValueInCacheKey` now decides for both `updateHash` and `serialize` with `for_cache_key`: a value without a fixed size is kept when it is at most 1 KiB, which covers ordinary literals and costs nothing next to the rest of the key. Larger values are still left out, so a heavy folded scalar subquery is not hashed on every execution, and so is any value holding an aggregate function state, found by walking the column rather than the type, since a `Dynamic` value hides it from the type and `byteSize` does not count a state's foreign arenas. Sets and lambdas keep their current handling. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing commit See the job log for details. |
CI finish ledger - 45002a3Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261016-093007 |
|
Measured conclusion for the
There is nothing to fix in this PR, and I am not pushing again just to change the layout. How the gate should treat such single-build flips is the open question at #122350 (comment). |
|
@groeneai the new test is too slow for the flaky check and fails the
The other failures look unrelated: the On the overlap with #127882: both PRs change the same branch of |
The test ran up to 153 s in the public ASan/TSan flaky checks and exceeded the 180 s limit in `Stateless tests (amd_asan_ubsan, flaky check, s3 storage, meta in keeper)` of the private sync PR ClickHouse/clickhouse-private#81821 (https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/praktika.html?PR=81821&sha=45002a340199a6cbe78206b82a3893656ad0dbd7&name_0=PR). The tables now have 600e3 rows instead of 1.2e6, which still keeps every recorded entry above the 500e3 groups under which `getSizeHint` does not preallocate. Each shape records one entry with one group per row and checks the colliding expression with a single-group key such as `intDiv(a, 1000000)`, the reproducer of the issue, so the number of aggregations with many groups drops from about 17 to 7. The `Nullable` cases, the `formatDateTime` literal pair and the accepted 1 KiB collision are dropped, as they exercise the same key paths as the remaining cases. The table of the automatic parallel replicas line has 1e5 rows instead of 1e6. Every check of a first run of a different expression still reads the entry of the query before it on master. Related: ClickHouse#125826
|
Update: I made the test faster myself in ae73339, so please disregard my request to @groeneai above. The tables have 600e3 rows instead of 1.2e6, which keeps every recorded entry above the 500e3 threshold of On the master base binary (7055af3) the new version still fails on all four bug lines ( |
`formatDateTime(d, '%Y')` after `formatDateTime(d, '%F %T')` is the only case in the test where the computing step changes the column type, so the step is keyed by `ActionsDAG::serialize` with `for_cache_key` and not by `getComputedOutputsHash`. Without it, reverting the short-literal change in `serialize` alone (keeping it in `updateOwnHash`) leaves the test green; with it, `q05401_string_format_year_1` reads 600000. It costs one aggregation with 600e3 groups and one with one group. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. I checked ae73339 on a local debug build of this PR's source: it passes 50/50 under I pushed one commit on top of it, ae00155, which brings back the |
CI finish ledger - ae00155Every 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:20261027-223005 |
d1c8ad7
…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]>
…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: #125826
Closes: #125825
Related: #116781
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
GROUP BYkeys computed by different expressions of the same type, for exampleintDiv(a, 1000000)anda + 1inSELECT k FROM (SELECT ... AS k FROM t) GROUP BY k, no longer share one hash table size statistics entry, so an aggregation preallocates its hash table for its own number of groups instead of for another query's. Closes #125826.Description
Two
GROUP BYqueries whose key expressions keep the column type shared a statistics entry: afterSELECT a + 1 AS k, count() FROM t GROUP BY k(2M groups),SELECT intDiv(a, 1000000) AS k, count() FROM t GROUP BY kpreallocates 2M cells (64.14 MiB instead of 172 KiB on a debug build) and fails undermax_memory_usage = 40000000. Results are not affected. On master both spellings collide (since #116781); on 26.8 and 26.9 only through a subquery, whose key is named__table1.kfor every expression.calculateHashFromStepreturns 0 for a row- and layout-preservingExpressionStepthat is not a pure permutation, so a computing step leaves no trace in the key. Such a step now contributes a hash of what it computes (ActionsDAG::getComputedOutputsHash):ALIASnodes are looked through, an input is identified by its header position, as inwriteCacheKeyColumnName, and other nodes are hashed as in the build-independentupdateHash. The hash does not change when expression merging folds a rename into the step, which automatic parallel replicas needs; hashing the serialized DAG instead makesSELECT repeat(a, 100) AS a, bstop collecting statistics at default merging (the regression adcb292 fixed).Since #116781 a build-independent key also lost string and array literals: the constant's name, which spells its value, is dropped, and a value without a fixed size was left out. So
trimBoth(s, 'x')andtrimBoth(s, '0123456789'), orformatDateTime(d, '%Y')andformatDateTime(d, '%F %T'), shared an entry on master (not on 26.8 and 26.9).updateHashandserializewithfor_cache_keynow keep such a value up to 1 KiB; larger values and aggregate states are still left out.The new test fails on master (five lines) and passes 50/50 randomized; its parallel replicas line fails with the serialized-DAG variant. 232 related tests, including the stateful
test.hitsones, show no new failures.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126148]
Version info
26.11.1.1290-master(included in26.11and later)