Support spilling for WindowAggExec#22947
Draft
wirybeaver wants to merge 1 commit into
Draft
Conversation
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?
Closes #22946.
Rationale for this change
WindowAggExeccurrently buffers the entire child input in memory, concatenates all buffered batches after the child stream is exhausted, then evaluates every window partition and emits a single output batch. This means memory usage scales with the full input size, not only with the largest window partition. Large inputs can therefore exhaust the memory pool even when individual partitions are small and DataFusion is configured with spill storage.This PR changes
WindowAggExecto process completed window partitions incrementally and adds spill support for the active partition when it cannot fit in the memory reservation.Design overview
The implementation keeps the operator's sorted-input contract: rows are grouped into logical window partitions using the existing partition-boundary evaluation. The execution model changes from whole-input buffering to partition-at-a-time buffering. Each completed partition is either retained in memory or moved to a spill file if the memory reservation cannot grow, then evaluated and emitted independently.
Memory reservations are released after each completed partition is evaluated. For spilled partitions, the operator still materializes the complete partition before evaluating window expressions; spilling is used to keep buffered input out of memory while waiting for the partition boundary.
Tradeoff and alternatives
The previous whole-input model can be faster for small or moderate inputs when memory is sufficient: it concatenates once, evaluates over larger contiguous batches, and emits fewer output batches. The partition-at-a-time model is more robust for large or skewed inputs and memory-limited execution, but it may add per-partition state management, repeated aggregation calls, and more output batches.
If preserving the current fully in-memory behavior is preferred, an alternative design is to introduce a separate operator such as
SpillingWindowAggExec. The planner could choose the existing in-memoryWindowAggExecfor small/fast-path workloads and chooseSpillingWindowAggExecwhen spill is enabled or when the query is expected to run under memory pressure.Another possible direction is integrating spill or more streaming evaluation into
BoundedWindowAggExecfor bounded frames. I am open to that direction, but it seems like a separate design problem:BoundedWindowAggExecis already optimized around stateful in-memory partition buffers and pruning, so adding disk-backed state there likely needs a focused API/design rather than being folded into this PR.What changes are included in this PR?
This PR adds spill support to
WindowAggExecwhile changing the buffering and emission model from whole-input to completed-partition execution:self.batchesbuffer with active and completed partition state.MemoryConsumerfor eachWindowAggStreampartition.SpillManagerwhen memory reservation growth fails and a disk manager is configured.SpillMetrics.Are these changes tested?
Yes. Added focused unit coverage for:
Locally verified after rebasing onto
apache/datafusion/main: