adriangb opened a new pull request, #27645:
URL: https://github.com/apache/datafusion/pull/27645
## Which issue does this PR close?
- No issue is filed yet. I can file one before this leaves draft.
## Rationale for this change
A high-cardinality `GROUP BY` can 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`):
```sql
SET datafusion.execution.target_partitions = 8;
SELECT count(*) FROM (SELECT value % 20000000 AS k, count(*) AS c
FROM generate_series(1, 40000000) GROUP BY k);
```
```bash
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.sql
```
| `MemoryMax` | `main` | this PR |
|---|---|---|
| none | OK, max RSS 1465 MB | OK, max RSS 1067 MB |
| 1300M | killed (SIGKILL) | OK |
| 1200M | killed (SIGKILL) | OK |
| 1100M | killed (SIGKILL) | OK |
With a counting allocator, the heap peak of this query on `main` is 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?
The PR has two commits, and each one builds and passes tests on its own.
1. [`38a5c47`](https://github.com/adriangb/datafusion/commit/38a5c4729):
`GroupValues::max_intern_growth` and `max_intern_growth_for` return an upper
bound on the bytes above `size()` that interning may hold at its peak. Both the
old and the new storage count while a buffer grows. The bound comes from the
current capacity, so it is 0 when the batch fits. `GroupValuesPrimitive` also
counts only the keys that are not stored yet. So a full table that sees only
existing keys reserves nothing. Nothing calls these methods yet.
2. [`191bc17`](https://github.com/adriangb/datafusion/commit/191bc179e): the
Partial, PartialReduce, Final and Single streams evaluate a batch, reserve
`memory_size() + growth`, and then aggregate it. If the pool refuses, they
spill or emit early first. After the batch, they shrink 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, then `AggregateHashTable::max_batch_growth`, then the four
stream call sites.
**Not covered:**
- Accumulator growth: `GroupsAccumulator` does not report its capacity.
- The value buffers of `GroupValuesRows` and `GroupValuesColumn`. Only their
hash tables are bounded.
- The clustered streams.
- The legacy `GroupedHashAggregateStream`.
## What is the testing strategy for this PR?
- `growth.rs` unit tests compare the bound with the measured old + new
allocation sizes while a table and a `Vec` grow. The bound is exact where
hashbrown uses SSE2 groups, and at most 16 bytes high elsewhere.
- `intern_growth_for_existing_keys_is_zero` fills a table to capacity.
Existing keys give a bound of 0, and one new key gives exactly one doubling.
- The new integration test `reservation_covers_group_table_growth` counts
heap per thread and checks that the pool peak covers the doubling. On `main` it
fails: heap peak 2004319 bytes against a reserved peak of 1572976 bytes.
- The `aggregate_memory_call_sequence` snapshots gain the pre-batch
`try_grow` and `shrink` calls. `spills_twice_then_replays` needs 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 BY` stay closer to the memory limit.
They may spill slightly earlier.
`GroupValues` is a public trait. It gains `max_intern_growth` and
`max_intern_growth_for`, both with default implementations, so this is not a
breaking change.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]