Repository navigation
Keep the hash table statistics of a capped GROUP BY apart from the uncapped aggregation - #126374
Conversation
…capped one The trivial GROUP BY LIMIT optimization pushes LIMIT into the aggregation as `max_rows_to_group_by = LIMIT + OFFSET` with `group_by_overflow_mode = 'any'`, and a user can set the same cap with `any` or `break`. The aggregation's statistics key did not include that cap, so a capped run and the same uncapped aggregation shared one entry: the capped run preallocated the uncapped table size, and its small sizes replaced the entry, so the next uncapped run preallocated nothing. An aggregation over a capped one (for example a capped subquery) shares the same problem one level up, since the inner step is hashed without its cap. Mix the cap and the overflow mode of every `AggregatingStep` in the aggregation's subtree, the step itself included, into the key stamped on the step, whenever any of them can stop admitting keys (mode is not `throw`). Uncapped steps contribute zeros, so an outer cap over an uncapped inner and an uncapped outer over a capped inner stay apart, and a plan without any such cap keeps exactly the previous key. Only the stamped key changes; the node hashes returned by `calculateHashTableCacheKeys` (used by automatic parallel replicas and join keys) stay as they are. A `throw` cap either throws or keeps every group, so it keeps sharing the uncapped entry. Closes ClickHouse#122608 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the mixGroupByCapsIntoKey comment to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 4 findings over 3 rounds) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261016-223300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261016-222200 |
|
cc @KochetovNicolai, could you review this? The trivial |
|
Workflow [PR], commit [844b176] Summary: ✅
AI ReviewSummaryThis PR separates hash-table statistics for capped and uncapped Final VerdictStatus: ✅ Approve |
The trivial `GROUP BY ... LIMIT` optimization and `max_rows_to_group_by` with `group_by_overflow_mode = 'any'` can produce the same `(cap, mode)` pair, but they retain very different tables. The planner cutoff (`shared_kept_keys_for_overflow_any`) rebuilds the tables to exactly the kept keys, while the per-stream `any` cap checks the limit after a block and keeps every key of that block. With one statistics entry for both, `GROUP BY k LIMIT 10` preallocated the tables of the settings cap and then overwrote the entry with tiny sizes, so the next settings-capped run preallocated nothing (measured with 4 streams: 262144 preallocated for the `LIMIT 10` run, then 0 for the `any` run; after this change 0 and 262144). Mix the flag into the key with each cap. Reported by the AI review on the PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #126374 to 26.10: Keep the hash table statistics of a capped `GROUP BY` apart from the uncapped aggregation
… `GROUP BY` apart from the uncapped aggregation
Closes: #122608
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
GROUP BYwhose groups are capped, by the trivialGROUP BY ... LIMIToptimization or bymax_rows_to_group_bywithgroup_by_overflow_mode'any'or'break', now keeps its own hash table size statistics, and so does an aggregation over such a capped one. It no longer preallocates the hash table of the same aggregation without the cap, and the next uncapped run still preallocates for all its groups. Closes #122608.Description
The trivial
GROUP BY ... LIMIToptimization moves theLIMITinto the aggregation asmax_rows_to_group_by = LIMIT + OFFSETwithgroup_by_overflow_mode = 'any', but the hash table statistics key of the aggregation did not include that cap. A capped run and the same uncapped aggregation shared one entry: the capped run preallocated the full table, then replaced the entry with its own small sizes (sum_of_sizes=50forLIMIT 10over 1M keys), so the next uncapped run preallocated nothing. Results are not affected.setAggregationHashTableCacheKeysnow mixesmax_rows_to_group_by, the overflow mode and whether it is theLIMITcutoff (which keeps exactly the cap, not whole blocks) of the aggregation and of every aggregation below it into the key stamped on theAggregatingStepwhen any of them can stop admitting keys (anyorbreak); without such a cap the key is unchanged. The node hashes ofcalculateHashTableCacheKeys, which automatic parallel replicas and join keys use, do not change. Athrowcap keeps sharing the uncapped entry: such a run either throws or keeps every group.Release build, 40M rows over 8M
Nullable(UInt64)keys,count(),max_threads = 16,GROUP BY k LIMIT 5000alternating withGROUP BY k:LIMIT 5000run: 159-161 ms and 1.44 GiB peak memory before, 94-108 ms and 30 MiB after;The new test asserts
AggregationPreallocatedElementsInHashTablesfor the planner cutoff with twoLIMITvalues, the settings pass of aggregate-free projections,anyandbreakcaps, a settings cap equal to aLIMIT, an outer aggregation over a capped subquery, cold and warm, and athrowcontrol. It fails on master and passes 90 randomized runs; 285 related tests show no new failures.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126374]
Version info
26.11.1.127-master(included in26.11and later)