Skip to content

Optimize aggregate function uniqCombined - #109794

Merged
CurtizJ merged 7 commits into
ClickHouse:masterfrom
CurtizJ:optimize-uniqCombined
Jul 13, 2026
Merged

CurtizJ merged 7 commits into
ClickHouse:masterfrom
CurtizJ:optimize-uniqCombined

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Jul 8, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Optimized aggregate function uniqCombined in case of aggregation without key.

Additional Context

It may be more important in a context of a new uniq statistic (#107863) that uses uniqCombined64(12) as an aggregate function for estimation.

Version info

  • Merged into: 26.7.1.869 (included in 26.7 and later)

CurtizJ and others added 2 commits July 8, 2026 12:45
Add `insertMany` to `CombinedCardinalityEstimator`: it dispatches on the
container type once per batch and runs a tight per-container loop, so the
per-value container dispatch and the non-inlined `insert` call are gone
from the hot path. Container promotions stay exactly size-triggered
because they are observable in results and in serialized states.

Override `addBatchSinglePlace` and `addBatchSinglePlaceNotNull` in
`AggregateFunctionUniqCombined`: rows are hashed a chunk ahead into a
stack buffer, so hash computation pipelines independently of the set
inserts. The String path reads `ColumnString` chars and offsets directly
instead of calling the virtual `getDataAt` per row. Override
`addManyDefaults` to insert the default value once instead of once per
row, which speeds up aggregation of sparse columns.

Aggregation without GROUP BY speeds up by 6-25%, measured both on
synthetic data and on `hits_100m_single` with the query added to the
perf test; results and serialized states are bit-identical to the
previous implementation, and GROUP BY queries are unaffected.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a6d151e]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, parallel, 2/3) FAIL
Server died FAIL cidb IGNORED
03620_json_advanced_shared_data_seek_bug UNKNOWN cidb IGNORED
01711_cte_subquery_fix UNKNOWN cidb IGNORED
00626_replace_partition_from_table UNKNOWN cidb IGNORED
03915_exchange_tables_race UNKNOWN cidb, issue ISSUE EXISTS
03129_low_cardinality_nullable_non_first_primary_key UNKNOWN cidb IGNORED
04202_array_dot_product_combine_overflow UNKNOWN cidb IGNORED
03775_paimon_partition_pruning_storage UNKNOWN cidb IGNORED
Unknown error FAIL cidb IGNORED

AI Review

Summary

This PR adds batched insertion for uniqCombined / uniqCombined64 on the aggregation-without-keys path, keeps the observable promotion behavior in CombinedCardinalityEstimator, and now includes the focused sparse-column regression coverage that was missing earlier. After reviewing the current diff, prior discussion, and the current code paths, I did not find a remaining correctness, compatibility, or testing issue that warrants a new review comment.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 8, 2026
Comment thread tests/performance/uniq_hits_no_key.xml Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR optimizes the batched insertion path of uniqCombined/uniqCombined64 (chunked hashing plus a new insertMany on the cardinality estimator). The two flagged ClickBench improvements, Q10 (-16.3%) and Q11 (-27.1%), are COUNT(DISTINCT UserID) queries that resolve to uniqExact by default and never touch the modified code, and the heavier distinct queries (Q4, Q8, Q9) did not move. Both flagged queries are also historically noisy, so these deltas look like run-to-run variance rather than a real PR effect; both have been downgraded to not sure. No regressions were flagged.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 10 not_sure 178 149 -16.3% <0.0001 aggregation: This PR speeds up uniqCombined's batched insertion, but Q10's COUNT(DISTINCT UserID) resolves to uniqExact by default, an untouched path, so the -16.3% shift on this historically noisy query reads as variance.
⚠️ 11 not_sure 229 167 -27.1% <0.0001 aggregation: Same as Q10: Q11's COUNT(DISTINCT UserID) uses uniqExact, not the uniqCombined path this PR changes, so the -27.1% improvement on this noisy query is not plausibly a PR effect.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: f003b26d-acfb-4c48-aa15-1ecfd4de6438
  • MIRAI run: 701651f9-d8ea-4911-b467-8293c7b370db
  • PR check IDs:
    • clickbench_184734_1783697865
    • clickbench_184747_1783697865
    • clickbench_184753_1783697865
    • tpch_adapted_1_official_184758_1783697865
    • tpch_adapted_1_official_184763_1783697865
    • tpch_adapted_1_official_184772_1783697865

`hashOne` returned `HashValueType`, truncating `CityHash64` to 32 bits
for `String` and `IPv6`, while `Data::Key` is deliberately `UInt64` for
these types: plain `uniqCombined` keeps the full 64-bit hash for them
for compatibility of aggregate function states. Return `Key` instead,
restoring states byte-identical to master; fixes the failures of test
02751_ip_types_aggregate_functions_states.

Suggested in: ClickHouse#109831 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
@novikd novikd self-assigned this Jul 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.90% 77.90% +0.00%

Changed lines: Changed C/C++ lines covered: 86/101 (85.15%) · Uncovered code

Full report · Diff report

@CurtizJ

CurtizJ commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

Performance dashboard: https://performance.ci.clickhouse.com/runs?q=109794. I didn't notice any slowed down queries relevant to changes in this PR.
Screenshot 2026-07-13 at 17 36 56

@CurtizJ
CurtizJ added this pull request to the merge queue Jul 13, 2026
Merged via the queue into ClickHouse:master with commit 6c60943 Jul 13, 2026
174 of 176 checks passed
@CurtizJ
CurtizJ deleted the optimize-uniqCombined branch July 13, 2026 15:57
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 13, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @CurtizJ @novikd — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

pull Bot pushed a commit to CrazyForks/ClickHouse that referenced this pull request Jul 19, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants