Repository navigation
Speed up aggregation batches with non-null states - #117009
Conversation
|
@alexey-milovidov good to test? This one is tiny unlike the others. |
|
Workflow [PR], commit [e4bbb92] Summary: ✅
AI ReviewSummaryThis PR adds an Final Verdict✅ No blocking or major issues found in the current head of this PR. LLVM Coverage Report |
4e8db4b to
e395857
Compare
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
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/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionsArgMinArgMax… |
15.47 MiB | 15.85 MiB | +388.58 KiB (+2.45%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionAvgWeighted.c… |
12.46 MiB | 12.77 MiB | +318.14 KiB (+2.49%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniq.cpp.o |
10.59 MiB | 10.80 MiB | +222.21 KiB (+2.05%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionSparkbar.cpp.o |
5.03 MiB | 5.18 MiB | +150.85 KiB (+2.93%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionQuantileExact… |
6.48 MiB | 6.62 MiB | +147.49 KiB (+2.22%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionGroupArray.cp… |
4.38 MiB | 4.52 MiB | +141.74 KiB (+3.16%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionDeltaSumTimes… |
3.35 MiB | 3.48 MiB | +134.88 KiB (+3.93%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionCorr.cpp.o |
6.45 MiB | 6.58 MiB | +129.31 KiB (+1.96%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionCovar.cpp.o |
6.28 MiB | 6.40 MiB | +116.57 KiB (+1.81%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionGroupArrayMov… |
5.40 MiB | 5.50 MiB | +107.61 KiB (+1.95%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionQuantile.cpp.o |
3.50 MiB | 3.59 MiB | +97.38 KiB (+2.72%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionGroupUniqArra… |
2.37 MiB | 2.46 MiB | +90.34 KiB (+3.72%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionQuantileDeter… |
2.65 MiB | 2.74 MiB | +88.94 KiB (+3.28%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionGroupNumericI… |
7.19 MiB | 7.28 MiB | +88.06 KiB (+1.20%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionQuantileGK.cp… |
3.14 MiB | 3.21 MiB | +73.98 KiB (+2.30%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionQuantileInter… |
3.30 MiB | 3.37 MiB | +70.22 KiB (+2.08%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/TimeSeries/AggregateFunctionTi… |
4.98 MiB | 5.04 MiB | +64.65 KiB (+1.27%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionAny.cpp.o |
2.39 MiB | 2.45 MiB | +59.55 KiB (+2.43%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionsMinMax.cpp.o |
2.38 MiB | 2.43 MiB | +56.14 KiB (+2.31%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionBitwise.cpp.o |
2.09 MiB | 2.14 MiB | +54.27 KiB (+2.54%) |
716 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
350 translation units recompiled, 2743 s compile time in total, 350 of them have a recent master baseline.
e395857 to
f23a944
Compare
|
Updated to extend this to combinators. It helped |
afe64f2 to
ba0e79f
Compare
|
Rebased some more to hopefully please the CI gods. @cv4g if you could review, that would be greatly appreciated. It seems like a clear and simple win to me. |
ba0e79f to
22999c5
Compare
Aggregations with lots of buckets can get expensive. Here's an excerpt from a profile:
```
ROUTINE ======================== DB::IAggregateFunctionHelper<DB::AggregateFunctionSum<unsigned long, unsigned long, DB::AggregateFunctionSumData<unsigned long>, (DB::AggregateFunctionSumType)0> >::addBatch(unsigned long, unsigned long, char**, unsigned long, DB::IColumn const**, DB::Arena*, long) const in src/AggregateFunctions/IAggregateFunction.h
1.79s 2.32s (flat, cum) 15.07% of Total
. . 549: }
. . 550: }
. . 551: else
. . 552: {
. . 553: for (size_t i = row_begin; i < row_end; ++i)
1.79s 1.79s 554: if (places[i])
. 526.32ms 555: static_cast<const Derived *>(this)->add(places[i] + place_offset, columns, i, arena);
. . 556: }
. . 557: }
. . 558:
. . 559: void serializeBatch(const PaddedPODArray<AggregateDataPtr> & data, size_t start, size_t size, WriteBuffer & buf, std::optional<size_t> version) const final // NOLINT
. . 560: {
```
We can get some of this back by specializing the common non-null version.
For a big query doing a very wide aggregation:
* 1 thread: 77.7s -> 74.3s (both wall and cpu)
* 64 threads: 17.0s -> 16.0s (wall), 99.5s -> 92.4s (cpu)
With a synthetic benchmark:
* Baseline: 1.24s
```
$ ../bench.py --clickhouse=/tmp/clickhouse-baseline --path=$HOME/clickhouse-no-nulls --runs=3 --query="SELECT toUInt16(bitAnd(number, 8191)) AS key, sum(number) FROM numbers(500000000) GROUP BY key FORMAT Null SETTINGS max_threads = 1"
wall: 1.24s, cpu: 1.23s, memory: 0.00GiB
wall: 1.25s, cpu: 1.24s, memory: 0.00GiB
wall: 1.23s, cpu: 1.23s, memory: 0.00GiB
```
* Patched: 1.10s
```
$ ../bench.py --clickhouse=/tmp/clickhouse-no-nulls --path=$HOME/clickhouse-no-nulls --runs=3 --query="SELECT toUInt16(bitAnd(number, 8191)) AS key, sum(number) FROM numbers(500000000) GROUP BY key FORMAT Null SETTINGS max_threads = 1"
wall: 1.10s, cpu: 1.09s, memory: 0.00GiB
wall: 1.10s, cpu: 1.09s, memory: 0.00GiB
wall: 1.11s, cpu: 1.10s, memory: 0.00GiB
```
Move the non-null batch implementation to `IAggregateFunctionHelper` and propagate the guarantee through `AggregateFunctionIf`, `AggregateFunctionOrFill`, and `AggregateFunctionTuple`. Preserve specialized timeseries batching. For `sumOrDefault` over 500 million rows and 8192 groups: 1.64s -> 1.5s. No measurable change was observed for `sumIf`.
22999c5 to
e4bbb92
Compare
|
CI is finally all green ✅ |
The 2048- and 4096-group entries of uniq_multi_place.xml (query indexes 5, 6, 12, 13, 19, 20) take 1.03-2.96 s on MasterCI amd and 1.15-2.58 s on arm; the three 4096-group ones are above report.py's 2 s single-run limit. Each group gets 16384 distinct values, so the time is the groups x 16384 single-threaded state insertions. No existing query number or text changes: the one query (21 entries) is disabled, and the same query over 64 to 1024 groups is appended as 21-35, byte-identical to entries 0-4, 7-11 and 14-18. The batched hash-and-prefetch insert of ClickHouse#119594 that the file measures already shows at 1024 groups. On the 457 master commits of 2026-09-12..24, with that path (before ClickHouse#117009 or after ClickHouse#120578) and without it (between the two), 1024 groups keep 95-128% of the 2048/4096-group speedup on amd, and on arm 91-97% of the 2048-group one and 69-88% of the 4096-group one (bootstrap p5 0.64, arm uniqCombined64). About 1 minute less query time per master perf job on each architecture. Related: ClickHouse#129258 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Antalya 26.8: Backport of ClickHouse#117009 Speed up aggregation batches with non-null states
Aggregations with lots of buckets can get expensive. Here's an excerpt from a profile:
We can get some of this back by specializing the common non-null version.
For a big query doing a very wide aggregation (with improvements from #115925):
Before:
After:
With a synthetic benchmark:
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up aggregation batches with non-null states.
Workflow [PR]
Sync PR [sync-upstream/pr/117009]
Version info
26.9.1.1381(included in26.9and later)