Skip to content

Speed up grouped approximate distinct aggregation - #119594

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
raimannma:optimize-uniq-multi-place-prefetch
Sep 13, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
raimannma:optimize-uniq-multi-place-prefetch

Conversation

@raimannma

@raimannma raimannma commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Batch hashes and prefetch destination cells for grouped uniq aggregations.

Related: #117009

Changelog category (leave one):

  • Performance Improvement

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

Speeds up single-argument uniq, uniqCombined, and uniqCombined64 on common scalar types when aggregating many interleaved groups.


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

Version info

  • Merged into: 26.9.1.1337 (included in 26.9 and later)

@clickhouse-gh clickhouse-gh Bot added the comp-aggregate-functions Aggregate function implementations (sum, avg, count, quantile, combinators, etc.). label Sep 12, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Sep 12, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting if it helps...

@clickhouse-gh

clickhouse-gh Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e69adf1]

Summary: ✅


AI Review

Summary

This PR adds specialized multi-place addBatch paths for single-argument uniq, uniqCombined, and uniqCombined64, prefetching target aggregate states and adding focused correctness/performance coverage. I did not find a code-level correctness, compatibility, or test regression in the current tree.

PR Metadata
  • Changelog category: Performance Improvement is correct.
  • Changelog entry: required, but the current text is broader than the implementation. The new fast path is only added in the dedicated single-argument specializations (src/AggregateFunctions/AggregateFunctionUniq.h and src/AggregateFunctions/AggregateFunctionUniqCombined.h); tuple / multi-argument / generic fallback instantiations still use the existing variadic path. Suggested replacement: "Speeds up single-argument uniq, uniqCombined, and uniqCombined64 on common scalar types when aggregating many interleaved groups."
Final Verdict

✅ No blockers in code or test coverage from this review. Tighten the changelog wording before merge so the user-facing promise matches the optimized surface.

LLVM Coverage Report

⚠️ No coverage measurement for commit e69adf1: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_7.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e69adf1b2 with master 3476b12ea (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

12 object files changed (+1.33 MiB total), 0 added.

Object file Master PR Δ
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +142.58 KiB (+5.16%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +141.27 KiB (+5.12%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +141.27 KiB (+5.12%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined2… 2.70 MiB 2.84 MiB +141.27 KiB (+5.12%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +141.27 KiB (+5.11%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +141.27 KiB (+5.11%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +141.25 KiB (+5.11%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +141.23 KiB (+5.11%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqCombined1… 2.70 MiB 2.84 MiB +141.23 KiB (+5.11%)
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniq.cpp.o 10.59 MiB 10.67 MiB +88.00 KiB (+0.81%)

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

19 translation units recompiled, 103 s compile time in total, 19 of them have a recent master baseline.

Job report

@clickhouse-gh

clickhouse-gh Bot commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

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
  • StressHouse run: ec81f817-3f91-4830-ba85-81ea3baa3b43
  • MIRAI run: c25c3567-7f41-4e6e-932f-8f9b89a55863
  • PR check IDs:
    • clickbench_204011_1789235497
    • clickbench_204017_1789235497
    • clickbench_204023_1789235497
    • tpch_adapted_1_official_204034_1789235497
    • tpch_adapted_1_official_204045_1789235497
    • tpch_adapted_1_official_204083_1789235497

@alexey-milovidov

Copy link
Copy Markdown
Member

It did.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 12, 2026
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 13, 2026
Merged via the queue into ClickHouse:master with commit d39373c Sep 13, 2026
175 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 13, 2026
@raimannma
raimannma deleted the optimize-uniq-multi-place-prefetch branch September 13, 2026 05:24
yackushevas pushed a commit to yackushevas/ClickHouse that referenced this pull request Nov 15, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-aggregate-functions Aggregate function implementations (sum, avg, count, quantile, combinators, etc.). external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants