Skip to content

Radix partitioned uniqExact for all argument types - #127280

Merged
Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:radix-uniq-exact-more-types
Nov 3, 2026
Merged

Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:radix-uniq-exact-more-types

Conversation

@Algunenano

@Algunenano Algunenano commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

The radix partitioned uniqExact handled only some argument types. The others fell back to the ordinary uniqExact, which was much slower and often lost to the count_distinct_optimization rewrite. Now every type is supported:

  • Values of up to 16 bytes, and of exactly 32 bytes, that are unambiguously represented by their bytes are used as keys directly, zero-padded (Decimal*, DateTime64, Time64, Int256/UInt256, FixedString(N ≤ 16)). The 16-byte ones, including IPv6 and FixedString(16), go through a bijection instead of SipHash.
  • Strings of up to 15 bytes are keyed exactly by their bytes and length; longer strings keep the 128-bit SipHash.
  • Other types (Tuple, Array, Map, Variant, JSON, other FixedString widths) use the 128-bit SipHash of updateHashWithValue, as uniqExact does.
  • LowCardinality routes only the distinct dictionary values each block uses.

The local set also takes a batch of keys in chunks of its free room, instead of inserting them one by one when the whole batch might not fit. This speeds up low cardinality inputs of every type.

50M rows, median ms (radix / count_distinct_optimization rewrite / plain uniqExact):

type 10M distinct 1000 distinct
Tuple(UInt64, UInt8) 91 / 367 / 746 48 / 48 / 75
Array(UInt64) 108 / 365 / 699 64 / 62 / 90
Map(UInt64, UInt64) 107 / 416 / 730 –
Decimal32 (1M distinct) 35 / 103 / 687 –
LowCardinality(String) on MergeTree (100k distinct in the first column) 276 / 352 / 343 12 / 13 / 97
String (toString(number % 1000)) – 29 / 22 / 67 (radix on master: 51)

Existing types are unchanged on ClickBench COUNT(DISTINCT) (UserID, WatchID, SearchPhrase, URL, Title, Referer, ClientIP, EventTime). IPv6 and FixedString(16) get 1.2x faster.

At low cardinality on String, the rewrite is still a bit faster (22 ms vs 29). Its GROUP BY keeps short strings inline with a 32-bit CRC32 hash, while radix needs an exact 128-bit key per row.

Related: #123552
Related: #127252

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The radix partitioned uniqExact (and count(DISTINCT)) now supports arguments of every type, including Decimal, DateTime64, big integers, FixedString, Tuple, Array, Map and LowCardinality. It is 2-20x faster than before for these types, and faster for short strings.


Workflow [PR]
Sync PR [sync-upstream/pr/127280]

Version info

  • Merged into: 26.11.1.1243-master (included in 26.11 and later)

Algunenano and others added 3 commits October 21, 2026 22:52
The values of up to 16 and of 32 bytes that are unambiguously represented by their bytes are the keys
themselves, zero-padded (e.g. `Decimal32`, `DateTime64`, `FixedString(12)`, `UInt256`). The 16-byte
values, including `IPv6` and `FixedString(16)`, go through the `mixKey` bijection instead of `SipHash`.
The other values are keyed by the 128-bit `SipHash` of `updateHashWithValue`, as in `uniqExact`
(e.g. `Tuple`, `Array`, `Map`).

The slot hash of the 128-bit keys now multiplies the high half: it keeps the zero padding of a
`FixedString(12)` in its top bits, which put all the keys into the same slots.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each block routes only the distinct dictionary values that its rows use (see
`getMinimalDictionaryEncodedColumn`), so a shared dictionary larger than the block costs nothing
extra. For `LowCardinality(Nullable)` the values are `Nullable` and NULLs are skipped as before.
All the argument types are supported now, so `isRadixUniqExactSupported` is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A string of up to 15 bytes is represented by its bytes and its size, a bijection (see
`packShortString`), then by `mixKey`, instead of the 128-bit `SipHash`, which cost more than half of
the routing of short strings. Their consecutive duplicates are compared inline instead of by `memcmp`.

The local set takes the keys of a batch in chunks of its room: only the new keys take room, so with
frequent keys, it had no room for the whole batch, and the keys were inserted one by one.

`toString(number % 1000)` over 50M rows: 48 ms -> 29 ms (the `count_distinct_optimization` rewrite
takes 22 ms). `toUInt32(number % 1000)`: 10 ms -> 8 ms. The ClickBench `COUNT(DISTINCT)` queries are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ea4cfa3]

Summary: ✅


AI Review

Summary

This PR extends radix partitioned uniqExact to all argument types, deduplicates used LowCardinality dictionary values, and improves short-string keys and local batch insertion. No unresolved findings remain in the current code; the three earlier findings are addressed. The added regression tests passed in Fast test, and the PR provides focused performance measurements.

Missing context / blind spots
  • ⚠️ The current CI run still has broader build, sanitizer, and performance jobs pending. Their completed reports would close this validation gap. No local build or test run was performed.
Final Verdict

Status: ✅ Approve. No new inline findings.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 21, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). label Oct 21, 2026
… ones

The packed short strings and the `SipHash` of the longer strings shared the `UInt128` set without a
discriminator, and `mixKey` is easy to invert, so a long string could have the key of a 15-byte string
(e.g. `long-string-example-494` and `unhex('D7A0004D354CD8BEB38E8080053CB7')` were counted once).
The longer strings now set the top bit of their hash before `mixKey`, which the packed short strings
never have.

Also reduce the data of the tests 10x: `05447_radix_uniq_exact_low_cardinality` ran longer than
180 s in the flaky check with ASan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ea4cfa3ed with master 0fc3c8038 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

6 object files changed (+93.83 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Transforms/RadixUniqExactTransform.cpp.o 440.59 KiB 534.74 KiB +94.15 KiB (+21.37%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

9 translation units recompiled, 33 s compile time in total, 9 of them have a recent master baseline.

Job report

Algunenano and others added 3 commits October 22, 2026 22:19
The keys of the strings of 16 bytes or more went through `mixKey`, two more `intHash64` per row, to keep them apart
from the keys of the short strings. The first round of `mixKey` now leaves the top bit alone, so the keys of the short
strings keep it clear, and the `SipHash` of the longer strings only sets it. 20-byte strings are 1.15x faster in one
thread; the long strings are marked unlikely, so that the loop of the short strings keeps its registers.

The `MergeTree` queries of `05447_radix_uniq_exact_low_cardinality` computed two aggregates, which the radix
partitioning does not handle, so they are split and their pipeline is checked. The performance test covers the long
strings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… test, and parallel replicas in `05447`

The performance test had only the types that the radix partitioning handled before, so the comparison showed nothing
of the new ones. It now has `Tuple`, `Array`, `Decimal128`, short strings and `LowCardinality(String)` from a
`MergeTree` table, with fewer queries on the old types (8 in total).

With parallel replicas, the replicas aggregate the table of `05447_radix_uniq_exact_low_cardinality` and their states
are merged, which does not use the radix partitioning, so its check of the pipeline failed. The test disables them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nihalzp nihalzp self-assigned this Nov 2, 2026
@clickhouse-gh clickhouse-gh Bot added the can be tested Allows running workflows for external contributors label Nov 2, 2026
@Algunenano
Algunenano enabled auto-merge November 3, 2026 14:40
@Algunenano
Algunenano added this pull request to the merge queue Nov 3, 2026
Merged via the queue into ClickHouse:master with commit 0fce803 Nov 3, 2026
366 of 367 checks passed
@Algunenano
Algunenano deleted the radix-uniq-exact-more-types branch November 3, 2026 19:16
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). pr-performance Pull request with some performance 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