Skip to content

Faster argMax, argMin, min, max with Adaptive Aggregator - #130269

Open
nihalzp wants to merge 10 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator-combine-arg-min-max
Open

nihalzp wants to merge 10 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator-combine-arg-min-max

Conversation

@nihalzp

@nihalzp nihalzp commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Related: #123531
Related: #128495

Background

The adaptive aggregator (enable_adaptive_aggregator, enabled by default) lets every thread aggregate into its own hash table until the table holds 16,384 keys. Then the table freezes: a row whose key is in the table still updates it, and every other row, a miss, is staged as a record that the merge aggregates later. A record keeps what the merge needs from its row: the key and the arguments of the aggregate functions. The records of a thread go into buffers by the slice of the key hash their key falls in, its partition.

Master combines misses only into count() records, and only within a block. Every other miss stages a record of its own, so a query that keeps one row per key, such as a deduplication by the latest version, stages nearly every row with all its arguments:

SELECT UserID, argMax(URL, EventTime), argMax(Title, EventTime), count()
FROM hits
GROUP BY UserID

Over hits sorted by EventTime, master stages 99.0 million records for its 100.0 million rows at 64 threads and needs 23.0 GiB. In the order of hits it needs more memory than the aggregation without the adaptive aggregator: 12.5 GiB against 11.5 GiB at 16 threads.

What changes

Every thread now remembers the last record it staged into each partition, the partition's tail. A miss whose key equals the key of its partition's tail goes into that record instead of a new one, so the rows of a key that arrive close together end up in one record:

  • a count() record adds the rows of the miss;
  • when every aggregate function selects one row of its group by the same column, such as argMax(x, ts), argMin(x, ts), max(ts) and min(ts), optionally with any and count, the record keeps the best of its rows and the number of rows, which the merge adds to count.

For example, take five missed rows of one block, of two UInt64 keys A and B that fall into different partitions:

row       1      2      3      4      5
key, ts   A, 5   B, 7   A, 9   A, 4   B, 8
Aggregates Master stages This PR stages
count() 4 records: A with 1 row, B with 1, A with 2, B with 1 2 records: A with 3 rows, B with 2
argMax(x, ts), count() 5 records, one per row 2 records: A keeps row 3 (ts 9) and 3 rows, B keeps row 5 (ts 8) and 2 rows

The selection applies when the ordering column is an integer, date, time, Enum, IPv4, Decimal32 or Decimal64 column and no argument is Nullable; other aggregations stage their rows as before. The results stay the same, except which of several rows with an equal ordering value argMax or argMin returns, which parallel aggregation does not define anyway. The new setting adaptive_aggregator_combine_arg_min_max, enabled by default, turns the selections off.

Results

AWS c8a.16xlarge (64 cores, one thread per core), FORMAT Null, 5 runs per query with the average of runs 3 to 5, the binaries alternating run by run, against master.

ClickBench has no query of this kind, so the table runs four with clickhouse-local over hits in two orders: the order of hits, where the rows of a user come in runs, and sorted by EventTime in hits_by_time, where the rows of different users interleave. Peak memory is the peak of the query from --memory-usage.

CREATE TABLE hits_by_time ENGINE = MergeTree ORDER BY (EventTime, WatchID) AS
SELECT UserID, EventTime, URL, Title, RegionID, ResolutionWidth, CounterID, OS, Age, WatchID, SearchPhrase, ClientIP
FROM hits;
OPTIMIZE TABLE hits_by_time FINAL;

The five numbers are RegionID, ResolutionWidth, CounterID, OS and Age.

Shape16 threads64 threads
TimePeak memoryTimePeak memory
GROUP BY UserID: argMax of 5 numbers by EventTime, hits482.0 → 237.3 ms, 2.03×2,926 → 972 MiB, −67%271.0 → 169.3 ms, 1.60×4,168 → 1,298 MiB, −69%
GROUP BY UserID: argMax of 5 numbers by EventTime, hits_by_time559.0 → 506.0 ms, 1.10×2,899 → 2,233 MiB, −23%293.3 → 251.0 ms, 1.17×4,386 → 2,849 MiB, −35%
GROUP BY UserID: argMin(URL, EventTime), min(EventTime), hits678.3 → 444.7 ms, 1.53×6,173 → 2,311 MiB, −63%388.3 → 310.0 ms, 1.25×7,850 → 3,609 MiB, −54%
GROUP BY UserID: argMin(URL, EventTime), min(EventTime), hits_by_time960.0 → 967.3 ms, 0.99×7,215 → 6,867 MiB, −5%661.0 → 522.7 ms, 1.26×13,451 → 9,393 MiB, −30%
GROUP BY UserID: argMax(URL, EventTime), argMax(Title, EventTime), count, hits1,321.7 → 840.7 ms, 1.57×12,797 → 4,815 MiB, −62%648.0 → 585.7 ms, 1.11×19,393 → 8,151 MiB, −58%
GROUP BY UserID: argMax(URL, EventTime), argMax(Title, EventTime), count, hits_by_time1,943.0 → 2,016.3 ms, 0.96×14,960 → 14,711 MiB, −2%1,308.7 → 1,044.0 ms, 1.25×23,539 → 17,084 MiB, −27%
GROUP BY UserID: count, hits_by_time167.7 → 169.0 ms, 0.99×1,520 → 1,141 MiB, −25%136.0 → 124.3 ms, 1.09×2,077 → 1,492 MiB, −28%
Geomean1.26×−41%1.24×−46%

On hits, 78% of the missed rows combine, and the selections need 54-69% less memory than master. On hits_by_time, rows of other users often come between two rows of a user, so 26-28% of the missed rows combine at 16 threads and 33-37% at 64 threads.

Changelog category (leave one):

  • Performance Improvement

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

GROUP BY queries that pick one row per group, such as SELECT id, argMax(name, updated_at), argMax(status, updated_at), max(updated_at) FROM t GROUP BY id, which keeps the latest name and status of every id, use less memory and run faster with the adaptive aggregator (enable_adaptive_aggregator, enabled by default). When every aggregate function is argMax, argMin, max or min by the same integer, date or time column, optionally with any and count, the aggregator now keeps only the best of the rows of a group that arrive close together, instead of a copy of each of them until the end of the aggregation. On six such queries over the ClickBench hits table, the geometric mean speedup is 1.31x at 16 threads and 1.27x at 64 threads, up to 2.03x, and peak memory drops by 43% and 48%. The optimization is controlled by the new setting adaptive_aggregator_combine_arg_min_max, enabled by default.


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

@clickhouse-gh

clickhouse-gh Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fd0fe72]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted) ERROR
OOM in dmesg ERROR cidb
Build profile diff ERROR

AI Review

Summary

Reviewed PR #130269 at fd0fe7234c1965bdba545b8312da0593b558e97b. The change combines adaptive aggregation misses into partition tails, retaining the best row for compatible argMin, argMax, min, and max aggregations and preserving count through record multiplicities. No actionable blockers or major findings were identified.

Consumers checked: appendDelayedRecords writes multiplicities and handles reordered sparse source rows; recordsWritten rebinds tails to owned records; accountStagedBatch accounts for replacements; drainAdaptivePartition and executeStagedArgumentRecords restore multiplicities for memory and spill replay. AdaptiveStagedRecordsSource uses the same replay before external merging. Count-first top-K retains its existing record-count contract because row selection is disabled for that path.

All five added stateless tests (05565–05569) passed with output exactly matching their references using the Fast Test binary for the reviewed commit. The PR supplies before/after performance measurements and a focused performance test. No prior review threads or author replies required reconciliation, and no new inline comments were posted.

Final Verdict

✅ Approve

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant