Repository navigation
Restore the batched prefetch path for uniq and uniqCombined in GROUP BY - #120578
Conversation
a183c77 added addBatchUniq (hash a 32-row chunk, prefetch the destination cells, then insert) and wired it into AggregateFunctionUniq::addBatch, for AggregateFunctionUniqUniquesHashSetData only, and AggregateFunctionUniqCombined::addBatch. ace35a5 then added a second batch entry point, IAggregateFunction::addBatchWithNonNullPlaces, and made Aggregator::addBatch prefer it whenever all_places_are_non_null, which is set to !top_k, i.e. for the ordinary GROUP BY. The interface default forwards to addBatch, but IAggregateFunctionHelper overrides the same virtual at IAggregateFunction.h:574 with its own per-row add() loop, and that override is what dispatch resolves to for every class deriving through IAggregateFunctionDataHelper. Neither uniq class overrode it, so their addBatch, and with it addBatchUniq, became unreachable on the ordinary GROUP BY path. The two commits crossed without a rebase: a183c77 is an ancestor of ace35a5 but not of its recorded base, so that branch never saw the two new overriders. It did carry the companion override to all four overriders that existed on its base. Of the six addBatch overriders in src/, those four already have the companion (-If, -OrFill, -Tuple, the timeseries base); these two did not. Verified under gdb on two builds that a breakpoint inside addBatchUniq is not reached on an ordinary GROUP BY before this change and is reached after, with an ORDER BY ... LIMIT arm that reaches it on both to show the probe can fire. Measured on two binaries from the same cmake config differing only by this patch, arms interleaved base/fix three times, 15 samples per shape, min-of-15, with max_threads=1 and the two-level thresholds and compiled expressions off: 512 groups, 8.4M rows: uniq +23.0% uniqCombined +11.2% uniqCombined64 +17.9% 512 groups, 33.5M rows: uniq +34.1% uniqCombined +34.5% uniqCombined64 +39.4% 4096 groups, 67M rows: uniq +42.1% uniqCombined +34.2% uniqCombined64 +46.5% uniqHLL12 delegates to the helper on both arms and serves as the control: it moves at most 1.5%. At 64 groups nothing moves on any function, which is expected when the states fit in cache and there is nothing to prefetch. The if constexpr in AggregateFunctionUniq is required. An unconditional delegation to addBatch would route uniqHLL12 and uniqExact into the helper's addBatch, which re-tests places[i] per row, and would take the non-null loop away from them. No new test: the change is behaviour-preserving, so a functional test cannot distinguish the two loops. tests/performance/uniq_multi_place.xml, shipped with a183c77 as this guard, is what can, and 05183_uniq_multi_place_prefetch is the correctness oracle, green here including 50 runs under randomized settings.
Restate the if constexpr comment as a bare invariant about the base helper: drop the half that the identical if constexpr in addBatch above already shows, and the relative-speed rationale, which is in the commit message.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex): 0 findings, over both changed
Checked and found clean, for the record: the new overrides forward the same arguments to the same Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-48:20260917-111200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260917-092200 |
|
Workflow [PR], commit [4a5c467] Summary: ✅
AI ReviewSummaryThis PR restores the intended Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 4a5c467.
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes11 object files changed (+151.62 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units18 translation units recompiled, 104 s compile time in total, 18 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=20 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
CI finish ledger - 4a5c467Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260917-163222 |
df28fe7
|
Fixing PR for test_max_bytes_ratio_before_external_distinct[user] created: #120751 |
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]>
Closes: #120531
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Restored the batched hash-and-prefetch insert path for
uniq,uniqCombinedanduniqCombined64in ordinaryGROUP BYqueries, where they had silently fallen back to a per-row loop and were up to 46% slower. Closes #120531.Description
a183c7755806eaddedaddBatchUniq(hash a chunk, prefetch the cells, then insert) and wired it into both uniq classes'addBatch.ace35a5495f43then added a second entry point,addBatchWithNonNullPlaces, and madeAggregator::addBatchprefer it wheneverall_places_are_non_null, which is!top_k, the ordinaryGROUP BY. The interface default forwards toaddBatch, butIAggregateFunctionHelperoverrides that virtual (IAggregateFunction.h:574) with a per-rowadd()loop, and that is what dispatch resolves to for classes deriving throughIAggregateFunctionDataHelper. Neither uniq class overrode it, soaddBatchUniqbecame unreachable there. The two commits crossed without a rebase, so the later branch carried the override only to the four overriders on its base.This adds the missing override to both, delegating to their existing
addBatch. Theif constexprkeepsuniqHLL12anduniqExacton the base's non-null loop, faster for them.Under gdb the path is not reached on an ordinary
GROUP BYbefore the change and is reached after; a top-k arm shows the probe fires. Magnitude, two binaries from one cmake config differing only by this patch, interleaved, min-of-15, as the extra time master takes over this build:uniquniqCombineduniqCombined64uniqHLL12, unaffected by design, is the control: at most 1.5%.No new test: the change is behaviour-preserving, so
tests/performance/uniq_multi_place.xml(shipped witha183c7755806eas this guard) is what can distinguish the loops, and should report them as faster here.05183_uniq_multi_place_prefetchis the correctness oracle, green including 50 randomized runs.Inverting the
Aggregatordefault so functions opt in would make this omission impossible; I kept the change minimal but can do that instead.26.9also contains the dispatch.Workflow [PR]
Sync PR [sync-upstream/pr/120578]
Version info
26.10.1.52(included in26.10and later)