Skip to content

Use higher quality hash for nullable fixed-width keys in external aggregation - #114210

Merged
nickitat merged 4 commits into
masterfrom
nullable-keys-better-hash-external-merge
Aug 17, 2026
Merged

nickitat merged 4 commits into
masterfrom
nullable-keys-better-hash-external-merge

Conversation

@nickitat

@nickitat nickitat commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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 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.

nickitat and others added 3 commits August 10, 2026 13:56
…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
@clickhouse-gh

clickhouse-gh Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b92c187]

Summary: ✅


AI Review

Summary

This PR adds *_hash64 external-merge variants for nullable fixed-width aggregation methods and updates the affected stateless tests. The main idea is correct, but the change still leaves one supported nullable fixed-width shape on the old 32-bit hash path, so the collision-driven slowdown is not fully eliminated.

Findings

❌ Blockers

  • [src/Interpreters/Aggregator.cpp:4040] mergeBlocks remaps nullable_key64 / nullable_keys128 / nullable_keys256, but it still skips low_cardinality_key64. chooseMethod selects low_cardinality_key64 for LowCardinality(Nullable(UInt64)) in src/Interpreters/AggregatedDataVariants.cpp:328-341, and that variant is still backed by AggregatedDataWithNullableUInt64Key in src/Interpreters/AggregatedDataVariants.h:132. Numeric LowCardinality dictionaries do not provide a precomputed saved_hash (ReverseIndex::use_saved_hash = !is_numeric_column in src/Columns/ReverseIndex.h:323-352), so the external-merge path continues to hash those keys with HashCRC32. That leaves the same > 4B-key collision blowup in place for a supported nullable fixed-width key shape. Suggested fix: add a low_cardinality_key64_hash64 variant/wrapper, remap it here alongside nullable_key64, and extend the new stateless coverage to a spilled LowCardinality(Nullable(UInt64)) case.
Final Verdict

Request changes.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.10% +0.10%

Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 10, 2026
`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
Comment thread src/Interpreters/Aggregator.cpp Resolved
@nihalzp nihalzp self-assigned this Aug 13, 2026
@nickitat
nickitat enabled auto-merge August 17, 2026 11:45
@nickitat
nickitat added this pull request to the merge queue Aug 17, 2026
Merged via the queue into master with commit 12e64d3 Aug 17, 2026
354 of 356 checks passed
@nickitat
nickitat deleted the nullable-keys-better-hash-external-merge branch August 17, 2026 19:52
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants