Repository navigation
Pack nullable fixed-width aggregation keys once per block - #126098
Conversation
`HashMethodKeysFixed` batch-packs a block's keys only when `usePreparedKeys` holds, which excludes every key set with a Nullable column, so `GROUP BY` / `DISTINCT` over nullable fixed-width keys (`nullable_keys128` / `nullable_keys256`, e.g. TPC-DS q65) builds the NULL bitmap and packs the key row by row. For a row without NULLs the nullable per-row layout is a zero bitmap followed by the keys in clause order, which a strided batch copy produces. Nullable states now batch-pack that layout (`packFixedBatchOrdered`) and re-pack with the per-row code only the rows that have a non-zero null-map byte; null maps that are all zero over the range are skipped with `memoryIsZero`. A row whose null-map bytes are all zero gets no bit from `createBitmap` whether a set byte is tested for `== 1` or for non-zero, so the result is the per-row key either way. The only allocation stays `prepared_keys`, so `estimatePreparedKeysMemory` (now true for these key sets) stays exact. Nullable states also get a sub-range variant (`SubRangeStateOf`), so aggregation in order, DISTINCT in order and the set skip index pack only the run they are built for, with the same 256-row threshold as the non-nullable keys (ClickHouse#120259). Without it, a whole-block pack per run of the sorting prefix made `GROUP BY a, nb` in order 0.045 s -> 17.5 s on 131072 rows with unique `a`. The range selection is shared by both batching branches (`rowsToBatchPack`). Non-nullable key sets keep their path: `keys256` is shared with the hash join, and its measured gain was small. Release A/B vs master (4 threads, 9 interleaved runs, paired geometric mean): nullable pair 60M groups 0.80x, 7.7K groups 0.39x; group_by_fixed_keys.xml a,d 0.80x, e,i 0.88x, e,f,i 0.74x; (Nullable(Int64), Int64) 40M rows with 50% NULL 0.74x, all NULL 0.89x; DISTINCT e,i 0.42x; non-nullable controls 0.96x-1.05x. In order (1 thread): unique-`a` arms 0.99x-1.03x, runs of 300 0.40x-0.87x, runs of 65536 0.76x. Based on the patch in issue ClickHouse#120100. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Whether a nullable key set is batch-packed, and the per-row fallback for a row outside a batched sub-range, are not observable from SQL. Extend `gtest_columns_hashing_prepared_keys.cpp` with a `Nullable(UInt32), UInt64` keys128 set (NULL-free and NULL-bearing halves) and check, against the per-row `packFixed` with a bitmap computed from the null map: the whole block and an explicit whole-block range are batched and carry the NULL bit in the array; long sub-ranges with and without NULLs are batched over their own rows and rows outside them fall back to per-row packing; the sub-range threshold holds on both sides. Also cover `nullable_keys256` in the `estimatePreparedKeysMemory` oracle and sort the `Columns/` includes in `HashMethod.h`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The three nullable cases in gtest_columns_hashing_prepared_keys.cpp built only a keys128 state, so nothing pinned that a nullable_keys256 set is batch-packed: if useOrderedPreparedKeys declined UInt256, every such set would fall back to per-row packing with all tests green (the allocation estimate case shares the same gate, and the SQL tests check results only). Each case now runs for a keys128 set and for a keys256 set whose fixture gains a third UInt64 column (20 key bytes: too wide for keys128 with its 2-byte bitmap, fits keys256 with its 4-byte one), against the per-row packFixed<Key> oracle. A build whose useOrderedPreparedKeys also requires sizeof(Key) <= 16 fails all three with the keys256 trace only; a build that skips the NULL repack fails all three for both widths. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the rowsToBatchPack doc comment to one line. 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; 2 findings over 3 rounds, the last round clean) plus my own review (4 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261015-183700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261015-150706 |
|
Workflow [PR], commit [a3881cf] Summary: ✅
AI ReviewSummaryPR #126098 batch-packs nullable fixed-width keys for Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings to post. |
|
cc @nihalzp @nickitat, could you review this? Nullable fixed-width key sets ( |
48752e8
Closes: #120100
Related: #120259
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
GROUP BYandDISTINCTover several fixed-width keys, some of themNullable, are faster: the keys of a block are packed at once instead of row by row. Such queries take 12-26% less time on typical nullable key sets and up to 2.5x less with few distinct groups. Closes #120100.Description
HashMethodKeysFixedbatch-packs the keys of a block only whenusePreparedKeysholds, which excludes every key set with aNullablecolumn, sonullable_keys128/nullable_keys256build the NULL bitmap and pack the key row by row.For a row without NULLs, the per-row nullable layout is a zero bitmap followed by the keys in clause order, which a strided batch copy produces (
packFixedBatchOrdered). Only rows with a non-zero null-map byte are packed again by the per-row code, and a null map that is all zero over the range is skipped (memoryIsZero). The output side reads the clause order it always read. Nullable states also get a sub-range variant, so aggregation in order,DISTINCTin order and the set index pack only their run, with the 256-row threshold from #120259; the whole-block-only patch in the issue tookGROUP BY a, nbin order from 0.045 s to 17.5 s with uniquea.estimatePreparedKeysMemorycovers these key sets. Non-nullable key sets are unchanged:keys256is shared with the hash join, and its gain was small.Measured (release, paired geometric mean against master, 9 interleaved runs, 4 threads): nullable pair with 7.7K groups 0.39x, with 60M groups 0.80x;
group_by_fixed_keys.xmla, d0.80x,e, i0.88x,e, f, i0.74x;(Nullable(Int64), Int64)with 50% / 100% NULL 0.74x / 0.89x;DISTINCT e, i0.42x; non-nullable controls 0.96x-1.05x. In order (1 thread): uniquea0.97x-1.03x, runs of 300 rows 0.40x-0.87x, runs of 65536 rows 0.76x. Results match master (05401); the unit test pins what SQL cannot observe: that nullable sets batch, the 256-row threshold and the per-row fallback outside a batched run. Based on the patch in the issue.Workflow [PR]
Sync PR [sync-upstream/pr/126098]
Version info
26.11.1.23-master(included in26.11and later)