Skip to content

[Bug] [v52 regression] Panic in GroupOrderingPartial::remove_groups when Partial aggregate with PartiallySorted hits memory pressure #20445

@jackkleeman

Description

@jackkleeman

Describe the bug

Partial aggregation with PartiallySorted ordering panics when memory pressure triggers the EmitEarly OOM path:

assertion failed: *current_sort >= n
at aggregates/order/partial.rs:174

#19287 changed Partial aggregation to always use OutOfMemoryMode::EmitEarly:

(AggregateMode::Partial, _) => OutOfMemoryMode::EmitEarly,

The EmitEarly handler calls emit(EmitTo::First(n)) with n based on batch size, ignoring the sort boundary tracked by GroupOrderingPartial. Previously, emit_early_if_necessary() had a guard:

&& matches!(self.group_ordering, GroupOrdering::None)

This guard was lost in the refactor.

Trigger conditions

  1. Partial aggregate with PartiallySorted ordering (e.g. GROUP BY a, b where input is sorted on a)
  2. Memory pressure triggers EmitEarly before a sort key boundary is reached
  3. Many groups accumulate with the same sort key value (so current_sort stays at 0 while n grows)

To Reproduce

A unit test is included in the linked PR #20446

Expected behavior

OOM, but not a assertion panic

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions