Skip to content

Count OverflowAny once when external aggregation re-seeds the kept keys - #132141

Open
groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:overflow-any-count-once-on-kept-keys-reseed
Open

groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:overflow-any-count-once-on-kept-keys-reseed

Conversation

@groeneai

@groeneai groeneai commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #131789 (comment)
Related: #126249

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Description

02385_profile_events_overflow fails when the random settings draw group_by_two_level_threshold_bytes = 1 and a small max_bytes_before_external_group_by: OverflowAny grows by 3 instead of 1 (CI report). No issue is open for it.

Since #126249, a max_rows_to_group_by + group_by_overflow_mode = 'any' stream keeps its admitted keys across external-aggregation spills: after each flush reseedKeptKeysAfterSpill merges them back through mergeOnBlock with a cleared no_more_keys. Once the stream has crossed the limit, the seed holds more than max_rows_to_group_by keys, so checkLimits takes the any branch again and counts OverflowAny once per spill. The results are correct; only the profile event over-counts. Master only, not in any release.

mergeOnBlock already recognizes these merges by kept_keys_rebuild_in_progress (set by the re-seed and by the kept-keys rebuild of GROUP BY ... LIMIT) and skips the spill for them. It now also gives checkLimits an already-set flag for them, so the overflow branch is skipped and the memory check still runs. Admission is still decided by the stream's own flag. The chassert in rebuildVariantsToKeptKeys that this flag stays clear is now always true and is removed.

Validation: 05401_external_aggregation_overflow_any_keeps_kept_keys gets one more check (OverflowAny is 1 and the re-seed ran): 50/50 runs fail on master with 3, 50/50 pass with the fix. 02385 with the CI settings forced: 20/20 fail on master, 20/20 pass with the fix.

Reverting #126249 would also stop the failure, but would bring back the wrong results it fixed. The draft #129750 removes the re-seed altogether; this change keeps the counter right until then.

🤖 Generated with Claude Code


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

groeneai and others added 2 commits November 16, 2026 20:57
…keys

Since ClickHouse#126249 a `max_rows_to_group_by` + `group_by_overflow_mode = 'any'`
stream keeps the keys it admitted across external-aggregation spills: every
flush captures them and `reseedKeptKeysAfterSpill` merges them back into the
emptied table through `mergeOnBlock` with a cleared `no_more_keys`.
`mergeOnBlock` then runs `checkLimits` on that cleared flag. Once the stream
has crossed the limit the seed holds more than `max_rows_to_group_by` keys, so
`checkLimits` takes the `any` branch again and increments `OverflowAny` once
per spill; the flag it sets is discarded by the caller. Results are correct,
only the profile event over-counts (`1 + number of spills`), which made
`02385_profile_events_overflow` fail whenever the random settings drew
`group_by_two_level_threshold_bytes = 1` and a small
`max_bytes_before_external_group_by`.

Before ClickHouse#126249 only the kept-keys cutoff of `GROUP BY ... LIMIT` re-seeded,
with a seed of exactly `max_rows_to_group_by` keys, so the branch never fired
there (the deleted `chassert(!reseed_no_more_keys)` encoded that).

`kept_keys_rebuild_in_progress` already marks the merges that only put back
keys the table held (the re-seed and `rebuildVariantsToKeptKeys`), and
`mergeOnBlock` already skips the spill for them. It now also passes
`checkLimits` an already-set local flag for them, which skips the overflow
branch (the only place `OverflowAny` is counted) and keeps the memory check.
Both setters run only in `any` mode, so no `throw`/`break` is skipped. The
stream's own flag still decides admission. The `chassert` that the first
rebuild merge leaves the flag clear is now true by construction; removed.

Reverting ClickHouse#126249 would also stop the failure but brings back the wrong
results it fixed.

05401_external_aggregation_overflow_any_keeps_kept_keys gets one more check:
`OverflowAny` is 1 while the re-seed ran. It fails on master in 50/50
randomized runs (`3`) and passes with the fix.

Related: ClickHouse#131789 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the mergeOnBlock checkLimits comment to one line; the field doc of kept_keys_rebuild_in_progress carries the reason.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 💡 Deleting chassert(!rebuild_no_more_keys) in rebuildVariantsToKeptKeys drops a check DROPPED It can no longer fire: mergeOnBlock now passes a local flag to checkLimits during a rebuild. The seed size is still asserted in buildKeptKeysSeedBlock (chassert(rows_left == 0)).
2 💡 The new local in mergeOnBlock reuses the name rebuild_no_more_keys from rebuildVariantsToKeptKeys AGREE, noted-not-blocking Cosmetic only, no change.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-11:20261116-210100

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new 05401 check fails on master in 50/50 randomized runs (3 instead of 1); 02385 with group_by_two_level_threshold_bytes=1 max_bytes_before_external_group_by=2880163 max_threads=3 max_block_size=56435 forced fails 20/20 with the CI diff (-1 / +3).
b Root cause explained? After a spill, reseedKeptKeysAfterSpill merges the admitted keys back through mergeOnBlock with a cleared no_more_keys. The seed holds more than max_rows_to_group_by keys once the limit was crossed, so checkLimits counts OverflowAny again per spill (1 + spills).
c Fix matches root cause? Yes. Merges that only put back held keys (kept_keys_rebuild_in_progress) give checkLimits an already-set flag, so the overflow is not counted again. The test is not pinned.
d Test intent preserved / new tests added? 05401 extended (additions only): OverflowAny = 1 and AggregationSharedKeptKeysSpillReseeds > 0, so the check fails if the re-seed path does not run. 02385 unchanged.
e Both directions demonstrated? Yes: 05401 master FAIL / fix OK (50/50 each); 02385 forced settings master FAIL / fix OK (20/20 each); clickhouse local repro: delta 3 on master, 1 with the fix, 3 spills and 2 re-seeds on both.
f Fix is general across code paths? Both re-insertion paths (re-seed after a spill, kept-keys rebuild of GROUP BY ... LIMIT) set the same marker and get the same check. Other mergeOnBlock callers never set it and are unchanged. 28-test aggregation/spill regression set green except one that needs the stateful dataset (same on master).
g Fix generalizes across inputs (params/datatypes/wrappers)? The change is in the limit check, independent of key type. Only group_by_overflow_mode = 'any' sets the marker, so throw/break are never skipped. Spill and no-spill, 1-3 threads covered.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change; only the OverflowAny count, and only on master since #126249.
i Invariants and contracts preserved? Admission is still decided by the stream's own flag; the memory check in checkLimits still runs; the spill stays suppressed during these merges; the marker is reset by SCOPE_EXIT on every exit.

Session id: cron:clickhouse-impl-slot-40:20261116-194600

@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [55761f2]

Summary: ❌

job_name test_name status info comment
Fast test (arm_darwin) FAIL
05042_crash_handler_without_global_context FAIL cidb, issue

AI Review

Summary

The change prevents kept-key rebuilds and spill reseeds from counting OverflowAny again while preserving key admission and memory-limit enforcement. No actionable findings. The updated 05401_external_aggregation_overflow_any_keeps_kept_keys regression passed all 50 targeted ASan CI iterations at 49bceb9f19a1, before the latest merge from master. No local tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). labels Nov 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 55761f23e with master e05308f1d (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.

Compile time of recompiled translation units

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

Job report

@PedroTadim PedroTadim self-assigned this Nov 20, 2026
@PedroTadim

Copy link
Copy Markdown
Member

cc @nihalzp @azat

@PedroTadim PedroTadim removed their assignment Nov 23, 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

can be tested Allows running workflows for external contributors comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants