Skip to content

Commit 22eb8ff

Browse files
fix: spill final hash aggregate output when the pool refuses it (#26734)
## Which issue does this PR close? - Closes #26701. ## Rationale for this change `FinalHashAggregateStream` fails with `ResourcesExhausted` when the materialized output is bigger than the table it came from (e.g. `string_agg`) and the pool won't grow the reservation. The other streams carry on. ## What changes are included in this PR? When that resize is refused and temp files are enabled, the stream writes the materialized output to one spill file, drops it, frees the reservation and streams the batches back. Without temp files it still errors, now with a message saying it couldn't spill the output. ## What is the testing strategy for this PR? Updated the `Final` case in `output_materialization_refused` (from #26562): it now spills once and returns all 256 rows in 4 batches. Aggregate unit tests and the aggregate slt files pass. ## Are there any user-facing changes? No, queries that hit this now succeed instead of erroring. --- This was AI-assisted, I've reviewed the changes myself.
1 parent 70c2e4c commit 22eb8ff

3 files changed

Lines changed: 83 additions & 12 deletions

File tree

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,17 @@ impl FinalHashAggregateStream {
619619
.await?;
620620
self.close_input();
621621

622-
match spill_context.filter(|s| s.has_spills()) {
622+
match spill_context {
623623
// - If spilled before, perform merging spill runs
624-
Some(spill_context) => {
624+
Some(spill_context) if spill_context.has_spills() => {
625625
self.produce_output_from_spills(hash_table, spill_context, emitter)
626626
.await?
627627
}
628628
// Either all the input fit in memory or hit soft group limit with no spilling
629-
None => self.produce_output_from_memory(hash_table, emitter).await?,
629+
spill_context => {
630+
self.produce_output_from_memory(hash_table, spill_context, emitter)
631+
.await?
632+
}
630633
}
631634

632635
Ok(())
@@ -806,6 +809,7 @@ impl FinalHashAggregateStream {
806809
async fn produce_output_from_memory(
807810
&mut self,
808811
mut hash_table: AggregateHashTable<FinalMarker>,
812+
spill_context: Option<Box<AggregateSpill>>,
809813
mut emitter: TryEmitter<RecordBatch, DataFusionError>,
810814
) -> Result<()> {
811815
let elapsed_compute = self.baseline_metrics.elapsed_compute().clone();
@@ -825,7 +829,33 @@ impl FinalHashAggregateStream {
825829
// The table hands over its groups as they are materialized and
826830
// reports a size of 0 once it reaches `Done`, so this releases the
827831
// reservation before the final batch goes downstream.
828-
self.reservation.try_resize(hash_table.memory_size())?;
832+
match self.reservation.try_resize(hash_table.memory_size()) {
833+
Ok(()) => {}
834+
// The materialized output is bigger than the table was. Write
835+
// it to disk and stream it back instead of failing the query.
836+
Err(e @ DataFusionError::ResourcesExhausted(_)) => {
837+
let spill_context = spill_context.ok_or_else(|| e.context(
838+
"Final hash aggregate cannot spill its output because temporary files are not enabled in the DiskManager",
839+
))?;
840+
let remaining =
841+
std::iter::from_fn(|| hash_table.next_output_batch().transpose());
842+
let mut output = spill_context.spill_output(
843+
Arc::clone(&self.schema),
844+
std::iter::once(Ok(batch)).chain(remaining),
845+
)?;
846+
drop(hash_table);
847+
self.reservation.try_resize(0)?;
848+
timer.done();
849+
850+
while let Some(batch) = output.next().await.transpose()? {
851+
emitter
852+
.emit(batch.record_output(&self.baseline_metrics))
853+
.await;
854+
}
855+
return Ok(());
856+
}
857+
Err(e) => return Err(e),
858+
}
829859

830860
timer.done();
831861
emitter

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,27 @@ impl AggregateSpill {
236236
Ok(())
237237
}
238238

239+
/// Writes already evaluated output batches to one spill file and returns a
240+
/// stream that reads them back.
241+
pub(super) fn spill_output(
242+
&self,
243+
schema: SchemaRef,
244+
batches: impl Iterator<Item = Result<RecordBatch>>,
245+
) -> Result<SendableRecordBatchStream> {
246+
let spill_manager = SpillManager::new(
247+
self.context.runtime_env(),
248+
self.spill_manager.metrics.clone(),
249+
schema,
250+
)
251+
.with_compression_type(self.spill_manager.compression);
252+
let spill_file = spill_manager
253+
.spill_record_batch_iter_and_return_max_batch_memory(batches, self.label)?;
254+
let Some((file, max_record_batch_memory)) = spill_file else {
255+
return internal_err!("{}: produced an empty output spill", self.label);
256+
};
257+
spill_manager.read_spill_as_stream(file, Some(max_record_batch_memory))
258+
}
259+
239260
/// Merges every sorted run, and does the aggregate evaluation with
240261
/// [`ClusteredFinalAggregateStream`].
241262
pub(super) fn into_replay_stream(

‎datafusion/physical-plan/tests/cases/aggregate_memory_call_sequence.rs‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ struct Run {
167167
clustering: GroupClusteringMode,
168168
metrics: MetricsSet,
169169
output_batch_rows: Vec<usize>,
170+
output: Vec<RecordBatch>,
170171
/// The most bytes the aggregate held at once.
171172
peak: usize,
172173
}
@@ -326,7 +327,7 @@ impl Case {
326327
),
327328
Err(e) => format!("Err({})", e.strip_backtrace()),
328329
};
329-
drop(result);
330+
let output = result.unwrap_or_default();
330331
drop(context);
331332
assert_eq!(pool.reserved(), 0, "every reservation is released");
332333

@@ -335,6 +336,7 @@ impl Case {
335336
clustering: aggregate.group_clustering_mode().clone(),
336337
metrics: aggregate.metrics().unwrap(),
337338
output_batch_rows,
339+
output,
338340
peak: pool.peak.load(Ordering::Relaxed),
339341
})
340342
}
@@ -578,11 +580,10 @@ async fn without_spilling_returns_refusal() -> Result<()> {
578580

579581
/// The table fits, but growing the reservation to the materialized output
580582
/// (`try_resize(memory_size())` on the first of several output batches) is
581-
/// refused. The streams disagree: Final returns the error, Single hands the
582-
/// output off unreserved (`reserved = false`, no further calls), and Partial
583-
/// and PartialReduce ignore it (and retry on every batch). Final is the only
584-
/// one that fails the query, although the output is already materialized,
585-
/// which #26701 reports. This pins each behavior as it is.
583+
/// refused. The streams disagree: Final spills the output and reads it back
584+
/// (#26701), Single hands the output off unreserved (`reserved = false`, no
585+
/// further calls), and Partial and PartialReduce ignore it (and retry on every
586+
/// batch). This pins each behavior as it is.
586587
///
587588
/// Built-in aggregates materialize at most a little more than their accounted
588589
/// state (`string_agg` does, see #26701), so this uses [`PaddedOutputUdaf`],
@@ -597,17 +598,36 @@ async fn output_materialization_refused() -> Result<()> {
597598
};
598599

599600
let run = padded(AggregateMode::Final).run().await?;
600-
assert_eq!(run.spill_count(), 0);
601+
assert_eq!(run.spill_count(), 1);
602+
assert_eq!(run.output_batch_rows, vec![64; 4]);
601603
assert_snapshot!(run.snapshot, @r"
602604
FinalHashAggregateStream[0] register can_spill=true
603605
FinalHashAggregateStream[0] try_grow [bytes]
604606
FinalHashAggregateStream[0] try_grow [bytes]
605607
FinalHashAggregateStream[0] try_grow [bytes] -> refused
606608
FinalHashAggregateStream[0] shrink [bytes]
607609
FinalHashAggregateStream[0] unregister
608-
=> Err(Resources exhausted: Failed to allocate additional [bytes] for FinalHashAggregateStream[0] with [bytes] already allocated for this reservation - [bytes] remain available for the total memory pool: greedy(used: [bytes], pool_size: [bytes]))
610+
=> Ok(256 rows in 4 batches)
609611
");
610612

613+
let unlimited = Case {
614+
memory_limit: usize::MAX,
615+
..padded(AggregateMode::Final)
616+
}
617+
.run()
618+
.await?;
619+
assert_eq!(unlimited.spill_count(), 0);
620+
assert_eq!(run.output, unlimited.output);
621+
622+
let run = Case {
623+
spill_enabled: false,
624+
..padded(AggregateMode::Final)
625+
}
626+
.run()
627+
.await?;
628+
assert_eq!(run.spill_count(), 0);
629+
assert!(run.snapshot.contains("cannot spill its output"));
630+
611631
let run = padded(AggregateMode::Single).run().await?;
612632
assert_eq!(run.spill_count(), 0);
613633
assert_eq!(run.output_batch_rows, vec![64; 4]);

0 commit comments

Comments
 (0)