Repository navigation
Use higher quality hash for nullable fixed-width keys in external aggregation - #114210
Merged
Merged
Conversation
…ge aggregation External aggregation merges partitions whose combined key count can far exceed 4 billion, which is why `mergeBlocks` re-aggregates the spilled stream with a method hashing over more than 32 bits rather than `HashCRC32`. `key64`, `keys128`, `keys256` and the serialized methods all have such a counterpart and are remapped to it; the nullable fixed-width methods never got one, so a `GROUP BY` on a nullable key that spills is still re-aggregated with a 32-bit hash and takes the collision blowup the remap exists to avoid. Add `nullable_key64_hash64`, `nullable_keys128_hash64` and `nullable_keys256_hash64`, and remap to them. The packed forms need no new data type: a nullable packed key carries its null map inside the key, so they reuse `AggregatedDataWithKeys128Hash64` / `…256Hash64` with `has_nullable_keys`. Only the single nullable key needs one, mirroring how `nullable_key64` wraps the `HashCRC32` map in `AggregationDataWithNullKey`. `nullable_key32` is deliberately left out, matching the existing choice not to remap `key32`: a 32-bit key space cannot exceed 4 billion distinct values. The remap makes those three methods reachable for the first time, so the test covers each of them end to end. Note the key widths it uses are what selects the method - a nullable key is packed together with its null map, so two `UInt32` keys fit in 128 bits while two `UInt64` keys need 256. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LJmHFQH2TPX9kHnwtWZe7f
The comment read as though each nullable key carries its own null map. It does not: `chooseAggregationMethod` reserves a fixed `KeysNullMap` bitmap inside the packed key - 2 bytes of the 16 for `keys128`, 4 of the 32 for `keys256` - so the 128-bit form has 14 bytes left for keys. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LJmHFQH2TPX9kHnwtWZe7f
How `chooseAggregationMethod` sizes a nullable packed key is not what this test is about, and the note was long enough to bury what is. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LJmHFQH2TPX9kHnwtWZe7f
Contributor
|
Workflow [PR], commit [b92c187] Summary: ✅
AI ReviewSummaryThis PR adds Findings❌ Blockers
Final VerdictRequest changes. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code |
`Aggregator::mergeBlocks` remaps a method to its `*_hash64` counterpart, and this change adds the nullable fixed-width methods to that remap. Cube and Rollup merge their grouping sets through `mergeBlocks`, so a `WITH CUBE` on nullable keys is now re-aggregated with `UInt256Hash` instead of `UInt256HashCRC32` and its rows come out of the table in a different order. The query in question has no `ORDER BY`, so its order was never specified - the reference simply recorded whichever order the old hash produced. Master already does this to the non-nullable methods, `key64` / `keys128` / `keys256` being in the same remap, so `WITH CUBE` on non-nullable keys has always gone through hash64; this only makes nullable keys behave the same way. Only the one query moves, by one row. The new order is stable over 10 runs, and no other test in the tree fails on a result mismatch. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114210&sha=22be91abb1d706e5b3c1d1c4ce745f3a87769b21&name_0=PR Related: #114210 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LJmHFQH2TPX9kHnwtWZe7f
nihalzp
approved these changes
Aug 16, 2026
nickitat
enabled auto-merge
August 17, 2026 11:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
64-bit hash function is used now for external nullable fixed-width aggregation methods (avoids collision disaster for very high cardinality aggregation).
External aggregation merges partitions whose combined key count can far exceed 4 billion, which is why
mergeBlocksre-aggregates the spilled stream with a method hashing over more than 32 bits rather thanHashCRC32.key64,keys128,keys256and the serialized methods all have such a counterpart and are remapped to it; the nullable fixed-width methods never got one, so aGROUP BYon a nullable key that spills is still re-aggregated with a 32-bit hash and takes the collision blowup the remap exists to avoid.Add
nullable_key64_hash64,nullable_keys128_hash64andnullable_keys256_hash64, and remap to them. The packed forms need no new data type: a nullable packed key carries its null map inside the key, so they reuseAggregatedDataWithKeys128Hash64/…256Hash64withhas_nullable_keys. Only the single nullable key needs one, mirroring hownullable_key64wraps theHashCRC32map inAggregationDataWithNullKey.nullable_key32is deliberately left out, matching the existing choice not to remapkey32: a 32-bit key space cannot exceed 4 billion distinct values.