Skip to content

Commit faa418a

Browse files
fix: emit completed groups when fully sorted final and single aggregates run out of memory
When a memory reservation is refused and the stream cannot spill, `ClusteredFinalAggregateStream` and `ClusteredSingleAggregateStream` now emit the completed groups and keep only the active group, returning the error only when no group is complete. This mirrors the fix for the partial stream. Closes apache#26628.
1 parent 836b4dd commit faa418a

2 files changed

Lines changed: 190 additions & 8 deletions

File tree

‎datafusion/physical-plan/src/aggregates/clustered_final_stream.rs‎

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ impl ClusteredFinalAggregateStream {
261261
/// 1. Start.
262262
/// 2. Merge one input batch:
263263
/// - If memory fits and no groups are complete, continue reading input.
264-
/// - If OOM, spill.
264+
/// - If OOM, spill. Without spilling, emit completed groups through [3],
265+
/// and fail only when no group is complete.
265266
/// 3. Prepare output:
266267
/// - Before any spill, a prefix of groups is complete: materialize the
267268
/// entire prefix once, retaining the input and active groups to resume
@@ -340,7 +341,15 @@ impl Aggregating {
340341
Ok(()) => {}
341342
Err(oom @ DataFusionError::ResourcesExhausted(_)) => {
342343
let Some(spill_context) = self.spill_context.as_mut() else {
343-
return Err(oom);
344+
// Without spilling, emitting completed groups is the only
345+
// way to release memory. Fail only when none is complete.
346+
let batch =
347+
self.table.take_completed_result_batch()?.ok_or(oom)?;
348+
timer.done();
349+
return Ok(Some(ExecutionStage::Outputting(Outputting {
350+
batch,
351+
resume: Some(self),
352+
})));
344353
};
345354
if self.table.is_empty() {
346355
return Err(oom);
@@ -464,8 +473,11 @@ mod tests {
464473
use crate::test::TestMemoryExec;
465474
use arrow::array::{Int64Array, StringViewArray};
466475
use arrow::datatypes::{DataType, Field, Schema};
476+
use datafusion_common::test_util::batches_to_string;
467477
use datafusion_execution::config::SessionConfig;
468-
use datafusion_execution::memory_pool::{GreedyMemoryPool, MemoryPool};
478+
use datafusion_execution::memory_pool::{
479+
GreedyMemoryPool, MemoryPool, UnboundedMemoryPool,
480+
};
469481
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
470482
use datafusion_functions_aggregate::{min_max::min_udaf, sum::sum_udaf};
471483
use datafusion_physical_expr::PhysicalSortExpr;
@@ -732,4 +744,87 @@ mod tests {
732744
assert_eq!(pool.reserved(), 0);
733745
Ok(())
734746
}
747+
748+
/// Runs a final `SUM(k) GROUP BY k` over two partial-state batches sorted on
749+
/// `k` that share a boundary key. Returns the output and the most memory the
750+
/// stream held while handing off output.
751+
async fn run_fully_sorted(
752+
pool: Arc<dyn MemoryPool>,
753+
) -> Result<(Vec<RecordBatch>, usize)> {
754+
let schema = Arc::new(Schema::new(vec![Field::new("k", DataType::Int64, false)]));
755+
let groups = PhysicalGroupBy::new_single(vec![(col("k", &schema)?, "k".into())]);
756+
let expressions = vec![Arc::new(
757+
AggregateExprBuilder::new(sum_udaf(), vec![col("k", &schema)?])
758+
.schema(Arc::clone(&schema))
759+
.alias("sum")
760+
.build()?,
761+
)];
762+
let empty = TestMemoryExec::try_new_exec(&[vec![]], Arc::clone(&schema), None)?;
763+
let partial = AggregateExec::try_new(
764+
AggregateMode::Partial,
765+
groups.clone(),
766+
expressions.clone(),
767+
vec![None],
768+
empty,
769+
Arc::clone(&schema),
770+
)?;
771+
let partial_schema = partial.schema();
772+
let batches = [0..=256, 256..=512]
773+
.into_iter()
774+
.map(|keys| {
775+
let k = Int64Array::from_iter_values(keys);
776+
RecordBatch::try_new(
777+
Arc::clone(&partial_schema),
778+
vec![Arc::new(k.clone()), Arc::new(k)],
779+
)
780+
})
781+
.collect::<Result<Vec<_>, _>>()?;
782+
let ordering =
783+
LexOrdering::new([PhysicalSortExpr::new_default(col("k", &partial_schema)?)])
784+
.unwrap();
785+
let input =
786+
TestMemoryExec::try_new(&[batches], Arc::clone(&partial_schema), None)?
787+
.try_with_sort_information(vec![ordering])?;
788+
let aggregate = AggregateExec::try_new(
789+
AggregateMode::Final,
790+
groups.as_final(),
791+
expressions,
792+
vec![None],
793+
Arc::new(TestMemoryExec::update_cache(&Arc::new(input))),
794+
schema,
795+
)?;
796+
assert!(matches!(
797+
aggregate.group_clustering_mode(),
798+
GroupClusteringMode::Full
799+
));
800+
let context = Arc::new(
801+
TaskContext::default().with_runtime(
802+
RuntimeEnvBuilder::new()
803+
.with_memory_pool(Arc::clone(&pool))
804+
.build_arc()?,
805+
),
806+
);
807+
let mut stream =
808+
ClusteredFinalAggregateStream::new(&aggregate, &context, 0)?.into_stream();
809+
// Each output batch is a single slice, so the stream releases it before
810+
// handing it off and holds only the table.
811+
let mut held = pool.reserved();
812+
let mut output = vec![];
813+
while let Some(batch) = stream.next().await.transpose()? {
814+
held = held.max(pool.reserved());
815+
output.push(batch);
816+
}
817+
Ok((output, held))
818+
}
819+
820+
/// The limit fits the table with only the active group, but not the table
821+
/// before the completed groups are emitted. The result must not change.
822+
#[tokio::test]
823+
async fn completed_groups_under_memory_pressure() -> Result<()> {
824+
let (expected, held) =
825+
run_fully_sorted(Arc::new(UnboundedMemoryPool::default())).await?;
826+
let (actual, _) = run_fully_sorted(Arc::new(GreedyMemoryPool::new(held))).await?;
827+
assert_eq!(batches_to_string(&actual), batches_to_string(&expected));
828+
Ok(())
829+
}
735830
}

‎datafusion/physical-plan/src/aggregates/clustered_single_stream.rs‎

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ use crate::{RecordBatchStream, SendableRecordBatchStream};
8282
/// only the current group remains active after completed groups are emitted, and
8383
/// memory usage does not grow with the total number of groups.
8484
///
85-
/// If a memory reservation nevertheless fails, the stream returns the error
86-
/// directly, indicating an unexpected behavior.
85+
/// If the reservation after an input batch fails, `ClusteredSingleAggregateStream`
86+
/// still emits the completed groups, and returns the error only when no group is
87+
/// complete.
8788
///
8889
/// ## Partial group clustering
8990
///
@@ -335,7 +336,27 @@ impl ClusteredSingleAggregateStream {
335336
let Some(spill_context) = spill_context else {
336337
// `None` means spilling is not supported, see comments
337338
// at `ClusteredSingleAggregateState` for details.
338-
return Self::break_with_err(e);
339+
// Emitting completed groups is then the only way to
340+
// release memory. Fail only when none is complete.
341+
let timer = elapsed_compute.timer();
342+
let result = table.take_completed_result_batch();
343+
timer.done();
344+
return match result {
345+
Ok(Some(batch)) => {
346+
let table_memory =
347+
Self::reservation_size_for_table(&table, None);
348+
self.start_outputting(
349+
batch,
350+
table_memory,
351+
ClusteredSingleAggregateState::ReadingInput {
352+
table,
353+
spill_context: None,
354+
},
355+
)
356+
}
357+
Ok(None) => Self::break_with_err(e),
358+
Err(e) => Self::break_with_err(e),
359+
};
339360
};
340361
if table.is_empty() {
341362
return Self::break_with_internal_err(
@@ -766,10 +787,10 @@ mod tests {
766787
use crate::test::TestMemoryExec;
767788
use arrow::array::Int64Array;
768789
use arrow::datatypes::{DataType, Field, Schema};
769-
use datafusion_common::test_util::batches_to_sort_string;
790+
use datafusion_common::test_util::{batches_to_sort_string, batches_to_string};
770791
use datafusion_execution::config::SessionConfig;
771792
use datafusion_execution::memory_pool::{
772-
GreedyMemoryPool, MemoryPool, PeakRecordingPool,
793+
GreedyMemoryPool, MemoryPool, PeakRecordingPool, UnboundedMemoryPool,
773794
};
774795
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
775796
use datafusion_functions_aggregate::sum::sum_udaf;
@@ -851,4 +872,70 @@ mod tests {
851872
);
852873
Ok(())
853874
}
875+
876+
/// Runs a single `SUM(k) GROUP BY k` over two batches sorted on `k` that
877+
/// share a boundary key. Returns the output and the most memory the stream
878+
/// held while handing off output.
879+
async fn run_fully_sorted(
880+
pool: Arc<dyn MemoryPool>,
881+
) -> Result<(Vec<RecordBatch>, usize)> {
882+
let schema = Arc::new(Schema::new(vec![Field::new("k", DataType::Int64, false)]));
883+
let batches = [0..=256, 256..=512]
884+
.into_iter()
885+
.map(|keys| {
886+
let k = Int64Array::from_iter_values(keys);
887+
RecordBatch::try_new(Arc::clone(&schema), vec![Arc::new(k)])
888+
})
889+
.collect::<Result<Vec<_>, _>>()?;
890+
let input = TestMemoryExec::try_new(&[batches], Arc::clone(&schema), None)?
891+
.try_with_sort_information(vec![
892+
LexOrdering::new([PhysicalSortExpr::new_default(col("k", &schema)?)])
893+
.unwrap(),
894+
])?;
895+
let aggregate = AggregateExec::try_new(
896+
AggregateMode::Single,
897+
PhysicalGroupBy::new_single(vec![(col("k", &schema)?, "k".into())]),
898+
vec![Arc::new(
899+
AggregateExprBuilder::new(sum_udaf(), vec![col("k", &schema)?])
900+
.schema(Arc::clone(&schema))
901+
.alias("sum")
902+
.build()?,
903+
)],
904+
vec![None],
905+
Arc::new(TestMemoryExec::update_cache(&Arc::new(input))),
906+
Arc::clone(&schema),
907+
)?;
908+
assert!(matches!(
909+
aggregate.group_clustering_mode,
910+
GroupClusteringMode::Full
911+
));
912+
let context = Arc::new(
913+
TaskContext::default().with_runtime(
914+
RuntimeEnvBuilder::new()
915+
.with_memory_pool(Arc::clone(&pool))
916+
.build_arc()?,
917+
),
918+
);
919+
let mut stream = ClusteredSingleAggregateStream::new(&aggregate, &context, 0)?;
920+
// Each output batch is a single slice, so the stream releases it before
921+
// handing it off and holds only the table.
922+
let mut held = pool.reserved();
923+
let mut output = vec![];
924+
while let Some(batch) = stream.next().await.transpose()? {
925+
held = held.max(pool.reserved());
926+
output.push(batch);
927+
}
928+
Ok((output, held))
929+
}
930+
931+
/// The limit fits the table with only the active group, but not the table
932+
/// before the completed groups are emitted. The result must not change.
933+
#[tokio::test]
934+
async fn fully_sorted_completed_groups_under_memory_pressure() -> Result<()> {
935+
let (expected, held) =
936+
run_fully_sorted(Arc::new(UnboundedMemoryPool::default())).await?;
937+
let (actual, _) = run_fully_sorted(Arc::new(GreedyMemoryPool::new(held))).await?;
938+
assert_eq!(batches_to_string(&actual), batches_to_string(&expected));
939+
Ok(())
940+
}
854941
}

0 commit comments

Comments
 (0)