Skip to content

fix: track struct array memory correctly on EmitTo::First in MinMaxStructState (#26891) - #26906

Open
lucasmeyer-dev wants to merge 2 commits into
apache:mainfrom
lucasmeyer-dev:fix/minmax-struct-emit-to-memory-accounting-26891
Open

lucasmeyer-dev wants to merge 2 commits into
apache:mainfrom
lucasmeyer-dev:fix/minmax-struct-emit-to-memory-accounting-26891

Conversation

@lucasmeyer-dev

Copy link
Copy Markdown

Which issue does this PR close?

Closes #26891.

Rationale for this change

When emitting groups via EmitTo::First(n) in MinMaxStructState, memory tracking subtracted s.len() for each emitted group instead of the actual array memory size. Since each group holds a single-row StructArray, total_data_bytes only decreased by 1 byte per group, causing memory usage to remain accounted for after emission.

What changes are included in this PR?

  • In MinMaxStructState::emit_to, sum s.get_array_memory_size() across emitted structs when handling EmitTo::First(n).
  • Added unit test test_min_max_struct_state_emit_to_first_memory_accounting to verify memory reduction on partial emission.

What is the testing strategy for this PR?

  • Unit test test_min_max_struct_state_emit_to_first_memory_accounting in datafusion/functions-aggregate/src/min_max/min_max_struct.rs.
  • Existing aggregate function test suite.

Are there any user-facing changes?

No.

…ructState (apache#26891)

In `MinMaxStructState::emit_to`, emitting groups via `EmitTo::First(n)`
previously computed `first_data_capacity` by summing `s.len()` for each
emitted struct array. Because each group stores a 1-row `StructArray`,
`s.len()` evaluated to 1, causing the byte subtraction from `total_data_bytes`
to only decrease by row counts instead of the actual array memory allocated
by `get_array_memory_size()`.

This caused memory accounting in `MinMaxStructState` to retain phantom bytes
across emitted groups during ordered/streaming aggregations, eventually triggering
`ResourcesExhausted` in memory-bounded execution streams.

Fix this by summing `s.get_array_memory_size()` on the emitted struct arrays,
mirroring the allocation logic in `set_value`, and add a regression unit test
verifying accurate memory decrement and zero residual bytes upon full emission.

Closes apache#26891

Signed-off-by: Lucas Meyer <[email protected]>
@github-actions github-actions Bot added the functions Changes to functions implementation label Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MinMaxStructState::emit_to(EmitTo::First(n)) subtracts row counts from a byte counter

1 participant