Repository navigation
Faster GROUP BY of fixed-width keys and one String - #129180
Conversation
…eshold of hashing states
…d_string` aggregation methods
…_string_keys_in_aggregation`
…us-string # Conflicts: # src/Core/ProtocolDefines.h
|
Workflow [PR], commit [dcde2f6] Summary: ✅
AI ReviewSummaryReviewed #129180 at Findings❌ Blockers
Missing context / blind spots
Tests
Final VerdictStatus: ❌ Block. Minimum required action: bound and validate staged-record decoding at the spill-consumption boundary, with focused malformed-record coverage. This preserves the previously dismissed concern in the summary only; no new inline review or thread reopening is required. |
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
Build profile diff (arm_release)Comparing Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Interpreters/Aggregator.cpp.o |
29.41 MiB | 33.63 MiB | +4.22 MiB (+14.36%) |
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/Merge.cpp.o |
10.51 MiB | 12.46 MiB | +1.94 MiB (+18.48%) |
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/Producer.cpp.o |
2.09 MiB | 2.50 MiB | +413.10 KiB (+19.28%) |
src/CMakeFiles/dbms.dir/Interpreters/AggregatorThresholdTopK.cpp.o |
1.45 MiB | 1.69 MiB | +246.56 KiB (+16.57%) |
src/CMakeFiles/dbms.dir/Interpreters/AggregatedDataVariants.cpp.o |
824.33 KiB | 980.44 KiB | +156.11 KiB (+18.94%) |
src/CMakeFiles/dbms.dir/Processors/Transforms/LimitByTransform.cpp.o |
1.45 MiB | 1.58 MiB | +131.56 KiB (+8.84%) |
src/CMakeFiles/dbms.dir/Processors/Transforms/NegativeLimitByTransform.cpp.o |
1.10 MiB | 1.21 MiB | +108.79 KiB (+9.65%) |
src/CMakeFiles/dbms.dir/Interpreters/AggregationMethod.cpp.o |
539.41 KiB | 628.41 KiB | +89.00 KiB (+16.50%) |
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/AdaptiveAggregation.cpp.o |
288.30 KiB | 318.18 KiB | +29.88 KiB (+10.36%) |
711 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
908 translation units recompiled, 9791 s compile time in total, 908 of them have a recent master baseline.
# Conflicts: # docs/resources/develop-contribute/native-protocol/protocol.mdx # src/Client/HedgedConnections.cpp # src/Client/MultiplexedConnections.cpp # src/Common/tests/gtest_packed_string_ref.cpp
…plus-string # Conflicts: # src/Core/ProtocolDefines.h
…getHash` returns
…string_keys_in_aggregation`
…plus-string # Conflicts: # src/Interpreters/AdaptiveAggregation/Merge.cpp # src/Interpreters/Aggregator.cpp # src/Planner/Planner.cpp # src/Processors/QueryPlan/QueryPlanSerializationSettings.cpp
…those of the string table
…xed width and a `String` added
…plus-string # Conflicts: # src/Core/ProtocolDefines.h # src/Interpreters/tests/gtest_aggregator_adaptive_merge.cpp
…ngle-level merge, so `merge_cell` keeps master's call sites
…f fixed-width keys and a `String`
…ys and a `String`
…p` and `Cube` plan steps when it is off or cannot apply
|
@groeneai Open an issue for this pre-existing leak, with a reproducer, and say in it whether it reproduces on master and which releases contain it. |
|
Opened #132439 for the leak. It reproduces on master and on the latest release of every supported branch (26.10.5.57, 26.9.31.21, 26.8.39.19, 26.3.66.4), and on every older release I tested back to 23.8: about 260 MiB per query on 26.6 and later, about 560 MiB on 26.3 and older. Only |
5703367
…String keys The merge of master brought ClickHouse#129180's shortcut into the frozen loop of argument records: a key of fixed-width values and an empty String that repeats the last missed key is staged with that key's hash, without being hashed or probed. The shortcut staged the row before the repeat check, so with the `keysN_packed_string` methods the repeats of such a key were staged even when every aggregate ignores repeated values. The repeat check is now a lambda that both paths call. A row on the shortcut takes the last missed key's hash, so the check compares it with the previous row exactly as on the probed path. The test gains a `GROUP BY k, e` case whose String key is empty in the first half of each key: results equal `enable_adaptive_aggregator = 0`, and it stages less than half the records of a `sum` query. Measured on the debug build: 40768 staged records with the change, 120736 without it, 200704 for `sum`; the binary without the change fails only that check (`1 0`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #110573
Related: #112089
Background
GROUP BYof fixed-width keys and oneStringkey is common: an ID or a date with a name, a phrase or a URL. ClickBench Q16 is one:Master aggregates such keys with
prealloc_serialized. It serializes the key columns of every row into one byte string, the bytes ofUserIDfollowed by the length and the bytes ofSearchPhrase, and hashes that string. The hash table stores a reference to the bytes of each key, copied into an arena, and compares keys byte by byte. Every row pays for serializing and hashing its whole key, and every key is a reference to bytes elsewhere in memory, even when its string is empty.A lone
Stringkey has a better method since #110573:key_packed_stringkeeps the string as a 16-byte packed reference, with short strings inline and the hash computed once. The new methods extend it to fixed-width keys and oneString.The key
keys32_packed_string,keys64_packed_stringandkeys128_packed_stringapply when the keys are one non-nullableStringand fixed-width values of 1, 2, 4, 8 or 16 bytes, at most 16 bytes in total. The key has two parts:UInt32,UInt64orUInt128as thekeys32,keys64andkeys128methods pack them.UInt32, a 16-byte reference after them would leave 4 bytes of padding, so the key takesUnhashedPackedStringRefthere: a 16-byte reference with 4-byte alignment and no hash, which keeps strings of up to 15 bytes inline, while the key keeps its hash in the 4 bytes that would be padding. Both keys take 24 bytes.Keys with an empty string
An empty string is the usual form of a missing value. In
hits, 86.8% of the rows have an emptySearchPhrase, and Q16 has 13.4 million groups with an empty phrase next to 10.7 million with another.Like
StringHashTable, the hash table of the new methods (KeysAndPackedStringHashMap) splits the keys by their string:keys32,keys64andkeys128. Its cells are as narrow as theirs, 16 bytes forUserIDagainst 32 for a key with a string.The adaptive aggregator stages the keys that miss a frozen table in the same spirit: a key with an empty string takes a record as narrow as that of a fixed-width key, without the string and its hash.
Without the adaptive aggregator, a repeated query preallocates its hash tables by the statistics of its earlier runs (
collect_hash_table_stats_during_aggregation). One number of keys cannot size both tables of the new methods, so the statistics also record how many keys had an empty string, and the preallocation splits by that share.Results
ClickBench
hits(100M rows) on AWSc8a.16xlarge(64 cores, one thread per core),clickhouse-local,FORMAT Null. The baseline is the same build withenable_fixed_and_packed_string_keys_in_aggregation = 0, which aggregates these keys withprealloc_serialized, as master does. Peak memory is the peak of the query from--memory-usage.GROUP BY MobilePhone, MobilePhoneModel:count(DISTINCT UserID), whereMobilePhoneModel <> ''GROUP BY SearchEngineID, SearchPhrase:count, whereSearchPhrase <> ''GROUP BY UserID, SearchPhrase:countGROUP BY UserID, minute(EventTime), SearchPhrase:countQ11 and Q14 run
keys32_packed_string, Q16keys64_packed_stringand Q18keys128_packed_string. Q11 has 302 groups and spends its time in the states ofcount(DISTINCT UserID), and Q14 has no empty phrases, so they gain little.ClickBench has no
GROUP BYof a narrow key and a short string, whose stringskeys32_packed_stringkeeps inline. AUInt32and aStringof 12 to 15 bytes, 20M rows, with 200 thousand groups ink, sand 2 million ink2, s2:GROUP BY k, s:countGROUP BY k2, s2:countGROUP BY k2, s2:count,enable_adaptive_aggregator = 0Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
GROUP BYof fixed-width keys and oneStringkey, such asGROUP BY UserID, minute(EventTime), SearchPhrase, uses a new hash table. It keeps strings of up to 11 bytes inside the key, or of up to 15 bytes when the fixed-width keys fit in 4 bytes, computes the hash of a key once, and stores keys with an empty string as narrow fixed-width keys. On four ClickBench queries that group by such keys, the geometric mean speedup is 1.19x at 16 threads and 1.13x at 64 threads, up to 1.43x, and peak memory drops by 18% and 12%. The new settingenable_fixed_and_packed_string_keys_in_aggregationis enabled by default; disabling it restores the previous hash table.Workflow [PR]
Sync PR [sync-upstream/pr/129180]
Version info
26.11.1.2684-master(included in26.11and later)