Repository navigation
Conversation
The aggregate peak memory test in datafusion-physical-plan needs a counting global allocator. The only one lives in datafusion-sqllogictest, which other crates cannot depend on. This commit moves it, unchanged, to `datafusion_execution::memory_pool::counting_allocator`, next to the drift tracker that consumes its count. The module is `#[doc(hidden)]` and is marked for internal use only, per the API health policy. This is a mechanical move. sqllogictest re-exports the same items. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A test that checks a memory reservation against real allocations needs the peak of one thread, exact to the byte. The process-wide count flushes per thread only every 256 KB, and tests that run in parallel mix in it. This commit adds an exact per-thread live count and peak, and `ThreadPeak` to read them. A realloc raises the peak as a copy, because the new block is live before the old block is freed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stream that reserves a batch's peak memory before it aggregates needs the evaluated group keys first, so that it can bound the growth from the keys. `aggregate_batch` evaluates and aggregates in one call, so a stream has no point in between. This commit replaces `aggregate_batch` on the four hash tables with `aggregate_evaluated`, and makes the streams call `evaluate_batch` first. It changes no behavior. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A growing hashbrown table or `Vec` allocates its new storage before it frees the old storage, so for a moment both are live. A stream that reserves only after `GroupValues::intern` never shows this peak to the memory pool. This commit adds `hash_table_growth` and `vec_growth`, which simulate the growth steps and return the largest number of bytes above the current allocation. It adds `GroupValues::max_intern_growth` and `GroupValues::max_intern_growth_for`, with implementations for the primitive, row and column group values. The primitive implementation counts only the keys that the table does not store yet, so a batch of existing keys reserves no growth. The tests check each bound against the measured peak. Nothing calls the new methods yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hash aggregate streams call `intern` and then `try_resize`. When the group hash table grows, it holds the old and the new table at the same time, so the memory pool never sees the peak. With 8 partitions and a 2 GB fair pool, the heap peak was 1.9 times the tracked peak. Each stream now evaluates a batch, reserves `max_evaluated_growth` above the table's size, and only then aggregates. When the reservation fails, the stream does what it does after a batch today: Final and Single spill, and Partial and PartialReduce emit early. The spill headroom (one `u32` per new group) is part of the reservation. The new test runs 4 batches of distinct keys in Single and Partial modes. It checks the per-thread heap peak against the pool's peak reservation. Without this fix it fails with a heap peak of 2004319 bytes against 1572976 reserved bytes. The call-sequence snapshots change because of this commit. The next commit updates them on their own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The streams now reserve a batch's growth before they aggregate it. So the recorded sequences gain a `try_grow` before each batch, and a refusal can come one call earlier. `spills_twice_then_replays` needs a limit of 40000 bytes instead of 39000 to still spill twice. This is a mechanical snapshot update. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
adriangb
force-pushed
the
agg-reserve-before-group-growth
branch
from
November 23, 2026 15:12
3f9552a to
085314e
Compare
…reserve-before-group-growth # Conflicts: # datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs # datafusion/physical-plan/src/aggregates/group_values/row.rs # datafusion/physical-plan/src/aggregates/group_values/single_group_by/primitive.rs
# Conflicts: # datafusion/physical-plan/src/aggregates/aggregate_hash_table/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
A high-cardinality
GROUP BYcan use far more memory than its memory limit, and under a hard memory cap the process gets OOM-killed.The hash aggregate streams reserve memory for their group table only after they aggregate a batch. When a batch makes the group hash table grow, hashbrown allocates the new table before it frees the old one, so the heap briefly holds both. The pool never sees that peak. The stream spills afterwards, but the memory is already allocated. With several partitions growing at once, the process goes well past the pool limit.
MRE in stock
datafusion-cli(Linux, 8 partitions,--memory-limit 2g,FairSpillPool):sudo systemd-run --scope -p MemoryMax=1200M -p MemorySwapMax=0 --uid=$(id -u) \ /usr/bin/time -v datafusion-cli --memory-limit 2g --mem-pool-type fair -q -f q1.sqlMemoryMaxmainWith a counting allocator, the heap peak of this query on
mainis about 1.9× the pool's tracked peak (1016 MB against 530 MB). With this PR, it is about 0.95×.What changes are included in this PR?
Read the PR commit by commit. Each commit builds and passes tests on its own, except commit 5. Commit 5 fails the call-sequence snapshots, and commit 6 updates them.
e3980c8moveCountingAllocatorfrom sqllogictest todatafusion_execution::memory_pool::counting_allocator#[doc(hidden)]and for internal use only.a01c6a8addThreadPeak, an exact per-thread peak, to that allocator62d8dbbsplitaggregate_batchintoevaluate_batchandaggregate_evaluatedab4ae31GroupValues::max_intern_growthandmax_intern_growth_for, and the growth bounds ingrowth.rs9bbfe97the Partial, PartialReduce, Final and Single streams reservememory_size() + growthbefore each batch085314eupdate theaggregate_memory_call_sequencesnapshotsIn commit 5, a stream that the pool refuses spills or emits early first. After the batch, it shrinks the reservation to the actual size, as before. If spilling is disabled, a refused pre-check does not fail the query. The check after the batch decides.
Review tip: start with
group_values/growth.rs, which has the model and its ASCII diagram, thenAggregateHashTable::max_evaluated_growth, then the four stream call sites.Not covered:
GroupsAccumulatordoes not report its capacity.GroupValuesRowsandGroupValuesColumn. Only their hash tables are bounded.GroupedHashAggregateStream.What is the testing strategy for this PR?
growth.rsunit tests compare the bound with the measured old + new allocation sizes while a table and aVecgrow. The bound is exact where hashbrown uses SSE2 groups, and at most 16 bytes high elsewhere.intern_growth_for_existing_keys_is_zerofills a table to capacity. Existing keys give a bound of 0, and one new key gives exactly one doubling.reservation_covers_group_table_growthcounts heap per thread and checks that the pool peak covers the doubling. Onmainit fails: heap peak 2004319 bytes against a reserved peak of 1572976 bytes.aggregate_memory_call_sequencesnapshots gain the pre-batchtry_growandshrinkcalls.spills_twice_then_replaysneeds 40 KB instead of 39 KB to keep its two spills.Performance: no criterion benches yet. In the worst case, where every batch sits within one batch of a table doubling (1 partition, 29.4 M groups), wall time is 4.1 s against 3.7 to 3.8 s on
main. On the MRE above, wall time is unchanged. I will run the aggregate benches before this leaves draft.Are there any user-facing changes?
Queries with a high-cardinality
GROUP BYstay closer to the memory limit. They may spill slightly earlier.GroupValuesis a public trait. It gainsmax_intern_growthandmax_intern_growth_for, both with default implementations, so this is not a breaking change.🤖 Generated with Claude Code