Skip to content

Commit e3b1045

Browse files
committed
Clamp to the sort boundary when using partial group ordering
Closes #20445
1 parent 6301344 commit e3b1045

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

datafusion/physical-plan/src/aggregates/row_hash.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,19 @@ impl GroupedHashAggregateStream {
10361036
self.group_values.len()
10371037
};
10381038

1039-
if let Some(batch) = self.emit(EmitTo::First(n), false)? {
1039+
// Clamp to the sort boundary when using partial group ordering,
1040+
// otherwise remove_groups panics (#20445).
1041+
let n = match &self.group_ordering {
1042+
GroupOrdering::None => n,
1043+
_ => match self.group_ordering.emit_to() {
1044+
Some(EmitTo::First(max)) => n.min(max),
1045+
_ => 0,
1046+
},
1047+
};
1048+
1049+
if n > 0
1050+
&& let Some(batch) = self.emit(EmitTo::First(n), false)?
1051+
{
10401052
Ok(Some(ExecutionState::ProducingOutput(batch)))
10411053
} else {
10421054
Err(oom)

0 commit comments

Comments
 (0)