You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "rkrishn7 (via GitHub)" <gi...@apache.org> on 2026/08/24 02:23:00 UTC
[I] Add arbitrary group retention for grouped aggregation state [datafusion]
rkrishn7 opened a new issue, #24603:
URL: https://github.com/apache/datafusion/issues/24603
### Is your feature request related to a problem or challenge?
Long-lived aggregation state needs to delete groups that are no longer active. A streaming engine may evict keys after a TTL or watermark while keeping every other group available for future updates.
`EmitTo::First(n)` can remove only a prefix, and it emits values as part of the operation. Arbitrary cleanup currently requires draining all `G` group keys and accumulator states, selecting the `K` survivors, rebuilding the group interner, and merging every survivor back into each accumulator. This is expensive and temporarily materializes the complete state.
For example, expiring groups `{1, 4, 7}` from ten live groups cannot be expressed without rebuilding all remaining groups.
### Describe the solution you'd like
Add `retain_groups` to both state-owning traits:
```rust
pub trait GroupValues: Send {
fn retain_groups(&mut self, retained_group_indices: &[usize]) -> Result<()>;
}
pub trait GroupsAccumulator: Send + Any {
fn retain_groups(
&mut self,
retained_group_indices: &[usize],
total_num_groups: usize,
) -> Result<()>;
}
```
The input order defines the new dense group IDs. Retaining `[5, 2]` maps old group `5` to new group `0` and old group `2` to new group `1`. Indices must be unique and in bounds. An empty selection clears the state; retaining every group in order is a no-op.
The operation destroys the complement and returns no output. A caller applies the same selection to `GroupValues` and every accumulator before using the new IDs. This keeps retention separate from `EmitTo`, whose contract combines output with prefix removal.
Universal defaults make the API usable by custom implementations:
- `GroupsAccumulator` can call `state(EmitTo::All)`, use Arrow `take`, and merge the retained state under new group IDs.
- `GroupValues` can materialize the selected keys using the preserving-read API, clear its lookup state, and re-intern the survivors. An emit-and-take fallback can support implementations without preserving reads.
This work should follow the implementation of #24602 so this fallback can avoid materializing discarded keys when possible.
Built-in implementations can likely do better most of the time. Vector and bitmap-backed accumulators can move or copy only retained entries. Hash tables and collision lists can remap IDs in place or rebuild at retained size. Variable-width and collection accumulators can drop unreferenced buffers and batches. Optimized implementations should update memory accounting and release excess capacity, although exact capacity is not part of the logical API contract.
### Describe alternatives you've considered
- Extending `EmitTo` with arbitrary indices would make its removal and output semantics harder to reason about. Retention does not need to produce aggregate results.
- A free list or tombstone design would avoid immediate compaction, but it would give up dense IDs and require changes throughout vector-backed accumulators. Block-based state may make some removals cheaper in the future, but it does not define how arbitrary survivors are remapped.
### Additional context
- #24602
--
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: github-unsubscribe@datafusion.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] Add arbitrary group retention for grouped aggregation state [datafusion]
Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on issue #24603:
URL: https://github.com/apache/datafusion/issues/24603#issuecomment-5565002223
> Long-lived aggregation state needs to delete groups that are no longer active. A streaming engine may evict keys after a TTL or watermark while keeping every other group available for future updates.
I think the current groups accumulator physical design is only optimized for throughput, so it's really hard to make eviction efficient. Reusing the `GroupsAccumulator` s can make both batched and streaming execution harder to evolve. The existing `Accumulator` interface seems even more suitable than `GroupsAccumulator` to extend for streaming use cases.
I'm wondering whether you've considered extending `Accumulator`, or adding a parallel accumulator implementation optimized for streaming use cases, and what the blockers are.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] Add arbitrary group retention for grouped aggregation state [datafusion]
Posted by "rkrishn7 (via GitHub) via github" <gi...@datafusion.apache.org>.
rkrishn7 closed issue #24603: Add arbitrary group retention for grouped aggregation state
URL: https://github.com/apache/datafusion/issues/24603
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] Add arbitrary group retention for grouped aggregation state [datafusion]
Posted by "zhuqi-lucas (via GitHub)" <gi...@apache.org>.
zhuqi-lucas commented on issue #24603:
URL: https://github.com/apache/datafusion/issues/24603#issuecomment-5390376902
The prefix-only limitation of `EmitTo::First(n)` is real, so +1 on separating retention from emission.
One question on the contract: `retain_groups` has to apply the *same* permutation to `GroupValues` and to every accumulator for the new dense IDs to line up, and it returns `Result`. What's the contract if accumulator `k` fails after `0..k-1` have already remapped? At that point `GroupValues` and the accumulators disagree on what a group ID means, and there's no way to roll back.
Worth pinning down one way or the other — either document it as poisoning (caller must discard the whole aggregation state on `Err`), or make it two-phase so validation/allocation can fail before anything is mutated. Silent divergence across accumulators would be nasty to debug.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] Add arbitrary group retention for grouped aggregation state [datafusion]
Posted by "rkrishn7 (via GitHub) via github" <gi...@datafusion.apache.org>.
rkrishn7 commented on issue #24603:
URL: https://github.com/apache/datafusion/issues/24603#issuecomment-6699745307
> I think the current groups accumulator physical design is only optimized for throughput, so it's really hard to make eviction efficient. Reusing the `GroupsAccumulator` s can make both batched and streaming execution harder to evolve.
@2010YOUY01 I think you're right that it's quite hard to make eviction efficient. It might be possible with primitive state that gets stored in something like a Vec/bitmap. But it gets harder with cases like variable-width state (e.g. values addressed by offsets into a shared buffer).
> The existing Accumulator interface seems even more suitable than GroupsAccumulator to extend for streaming use cases.
Unfortunately we'd lose most of the advantages of `GroupsAccumulator` that way. For high-volume, append-only streams, throughput and memory per group matter as much as in batch execution, and the overhead of one `Accumulator` per group adds up as the number of groups grows. We saw this in #27150 which replaces the per-group `Accumulator`s behind `GroupsAccumulatorAdapter` with a native `GroupsAccumulator`.
However, per-group removal may be the wrong primitive to expose, given how many implementations lay out their state for throughput. TTL (which prompted this issue) can be handled in other ways. Though, I still think streaming execution benefits a lot from `GroupsAccumulator`, so I don't want to give up on it entirely.
Will think a bit more if there's a narrower API we could expose that's helpful here.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] Add arbitrary group retention for grouped aggregation state [datafusion]
Posted by "rkrishn7 (via GitHub)" <gi...@apache.org>.
rkrishn7 commented on issue #24603:
URL: https://github.com/apache/datafusion/issues/24603#issuecomment-5389989224
take
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] Add arbitrary group retention for grouped aggregation state [datafusion]
Posted by "rkrishn7 (via GitHub) via github" <gi...@datafusion.apache.org>.
rkrishn7 commented on issue #24603:
URL: https://github.com/apache/datafusion/issues/24603#issuecomment-6937276868
Going to close this. Agree with @2010YOUY01 that eviction is probably not worth trying to implement on these types. I'm tracking an alternative, potentially more generally useful extension in #27498
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] Add arbitrary group retention for grouped aggregation state [datafusion]
Posted by "rkrishn7 (via GitHub) via github" <gi...@datafusion.apache.org>.
rkrishn7 closed issue #24603: Add arbitrary group retention for grouped aggregation state
URL: https://github.com/apache/datafusion/issues/24603
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org