Repository navigation
Grouped uniqExact skips a row that repeats the previous row's group and value - #128190
alexey-milovidov merged 3 commits into
Conversation
`uniqExact` / `COUNT(DISTINCT)` with GROUP BY inserted every row into its group's set, also a row whose group and value equal the previous row's, which is common in data sorted by these columns (ClickBench `hits`: 77% of rows repeat the previous (RegionID, UserID)). Inserting a key the set already contains never changes it, so such rows are dropped before the insert (and, for String/IPv6, before hashing). Numeric keys are collected branch-free. Like the keyless last-value cache (`addManyChunked`), the skip is adaptive: when at most half of the rows seen so far in the batch repeat, the rest of the batch goes through the per-row loop, so data without repeats keeps its current speed. Based on the analysis and patch in ClickHouse#128138. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… to uniq_with_key.xml The overflow query of 05472 gets `uniqExact(number)` (no row repeats, so every block reaches the per-row fallback) and `max_block_size = 1024`: blocks that hold two keys take `addBatchWithNonNullPlaces` (the block where k = 1 appears) and `addBatch` with null places (the block where k = 2 is dropped), while a block with one key and `max_threads = 1` would go through `addBatchSinglePlace`. The expected values do not change. `uniq_with_key.xml` gets the cache-resident worst case without repeats (one group per 65536 rows, the value changes every row), so the CI performance comparison watches the fallback on both CI machines. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (head 9777aab): 0 open findingsGate B (codex), 3 runs on the same code; the PR description was corrected between runs.
Final run: 0 findings. |
|
cc @nickitat, could you review this and add the |
|
Workflow [PR], commit [4e0c5f4] Summary: ✅
AI ReviewSummaryPR #128190, reviewed at Missing context / blind spots
Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes5 object files changed (+222.97 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units8 translation units recompiled, 45 s compile time in total, 8 of them have a recent master baseline. |
One of the 50 flaky-check runs timed out at 600 s. The flaky check runs the server under the thread fuzzer, which makes every mutex operation and large allocation expensive, and that run drew `index_granularity = 49` with compact parts and sparse serialization: the INSERT alone took 48 s, and reading the 500000-row table once decompressed ~71000 tiny blocks. The base binary is as slow as this PR on the same settings. Clamp `index_granularity` and `index_granularity_bytes` to their defaults or higher, use 150000 rows, 997-row blocks instead of 97-row ones (still not a multiple of the run lengths, and a block now ends with a partial chunk after full ones) and 10000 rows in the overflow query. The two-level query grouped by `x % 2`, an 8-bit key, which takes `addBatchLookupTable8`, so its sets never reached the new loop. It now reads one stream of 330000 rows with a `UInt32` key, 110000 keys in one group and `max_threads = 2` (with one thread the sets never convert); gdb shows the `UInt64` and the `String` set converting inside `addBatchExact` while repeats are skipped. Replaying the random settings of all 50 CI runs locally under the same thread fuzzer settings, 8 at a time: the old test took up to 417 s (more than 800 s for the failing run), the new one up to 126 s; results match the reference in every run, and mutations M1-M6b still fail the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tring or IPv6 `uniqExact` stores `sipHash128Padded` of each `String` / `FixedString` value (IPv6 as its 16 bytes) and hashed one row at a time. Now a chunk of 256 rows first chooses the rows that get a key exactly as before (NULL, false `-If`, null place and repeated (place, value) rows are skipped), then hashes them with one call of the vectorized functions of ClickHouse#129204, and inserts the keys in the same order. Fewer than 8 chosen rows are hashed one at a time, as those functions would do anyway. The hashes, and so the serialized states, do not change. Builds without AVX2 or NEON hash one row at a time inside the same functions. Two row-list variants are added next to ClickHouse#129204's functions: `sipHash128PaddedStringRows` (`SipHashStrings::hashRows` over a row list) and `sipHash128PaddedFixedStringRows`. Converted call sites, chosen by measurement (ClickHouse#129436 item 5; 1, 16 and all threads, String 0-15 / 16-64 / 0-300 bytes, FixedString(16) / (64), IPv6, 3 ABAB rounds + 5-round re-runs of cells above 1.03), fix / master time: - GROUP BY without consecutive repeats (the ClickHouse#128190 fallback, before: one `add` per row): 0.37-0.66 at 1 thread, 0.57-0.91 at 16 and all threads. - `-If` true on 50-99% of the rows and `Nullable` without GROUP BY: 0.58-0.77 at 1 thread, 0.63-0.92 at 16 and all threads. - `-If` true on 1-10% of the rows without GROUP BY, 1-50% with GROUP BY: 0.77-1.03. - No key, consecutive values rarely repeat (the last-value cache off), String and FixedString: 0.69-1.02. - GROUP BY with repeats (ClickHouse#128190) and no key with repeats (the last-value cache): 0.80-1.06. Left out: - No key without repeats for IPv6: 1.098 on 20M distinct values after a 5-round re-run. - A `UInt8` key (`addBatchLookupTable8` is `final` in the data helper), `Nullable` with GROUP BY and `-If` with a `UInt8` key (combinator code), `-StateIf` (the `-State` inside `-If` adds one row at a time), the radix `uniqExact` (separate change), variadic `uniqExact` (ClickHouse#129435). `sip_hash_128_strings.xml` gets an `IPv6` table and these shapes, including `-If` true on 50% and on 1% of the rows, and no key without repeats. Related: ClickHouse#129436 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #128138
Related: #124126
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
uniqExactandCOUNT(DISTINCT)withGROUP BYare faster on data where consecutive rows often have the same group and value, as in data sorted by these columns: such a row is no longer inserted into the set again. ClickBench Q9 and Q10 are 1.16x and 1.14x faster with 4 threads.Description
Grouped
uniqExactinserted every row into its group's set, also a row whose group and value equal the previous row's. That insert never changes the set, and in sorted data it is common: 77% of the rows of ClickBenchhitsrepeat the previous(RegionID, UserID). Now a 256-row chunk's keys are collected without those rows (branch-free for numbers; a skippedStringorIPv6is not hashed), then inserted. When at most half of the rows seen so far in the block repeated, the rest of the block takes the existing per-row loop, like the keylessaddManyChunked. Without that fallback (the patch in #128138) CPU time grows by up to 25% on repeat-free data whose sets fit in the cache.Xeon 6975P-C, matched ThinLTO builds, 12 rounds, ratio to master:
The drawback cases (unsorted
hits, no repeats with small or large sets, 200K groups in random order, forced two-level aggregation, filtered blocks, 50-67% repeats) are within 1.5% of master; results are identical. Graviton3, 4 threads (#128138): Q9 1.16x, Q10 1.13x faster, repeat-free cases within 1.2%. Not measured on AMD EPYC; the newuniq_with_key.xmlquery adds the worst case to CI perf tests.Only the grouped batch path of single-argument
uniqExact(also with-If) of native numbers,Enum,Date,Date32,DateTime,UUID,IPv4,IPv6,StringandFixedStringchanges; plainuniqExactofDecimal,DateTime64,Nullableor several arguments keeps the per-row loop. Unlike #93268 (reverted in #93294), no state is added and the keyless path is untouched.Analysis and first patch: @ snelheidai, #128138.
Workflow [PR]
Sync PR [sync-upstream/pr/128190]
Version info
26.11.1.927-master(included in26.11and later)