diff --git a/datafusion/physical-plan/src/repartition/mod.rs b/datafusion/physical-plan/src/repartition/mod.rs index 129154c24e9fa..a9014dc92b213 100644 --- a/datafusion/physical-plan/src/repartition/mod.rs +++ b/datafusion/physical-plan/src/repartition/mod.rs @@ -439,7 +439,7 @@ impl RepartitionExecState { preserve_order: bool, name: &str, context: &Arc, - spill_manager: SpillManager, + make_spill_manager: impl FnOnce() -> SpillManager, coalescer_batch_size: usize, ) -> Result<&mut ConsumingInputStreamsState> { let streams_and_metrics = match self { @@ -467,7 +467,7 @@ impl RepartitionExecState { let num_output_partitions = partitioning.partition_count(); let coalesce_batches = !preserve_order && !input.boundedness().is_unbounded(); - let spill_manager = Arc::new(spill_manager); + let spill_manager = Arc::new(make_spill_manager()); let (txs, rxs) = if preserve_order { // Create partition-aware channels with one channel per (input, output) pair @@ -1750,8 +1750,6 @@ impl ExecutionPlan for RepartitionExec { partition ); - let spill_metrics = SpillMetrics::new(&self.metrics, partition); - let input = Arc::clone(&self.input); let partitioning = self.partitioning().clone(); let metrics = self.metrics.clone(); @@ -1763,12 +1761,14 @@ impl ExecutionPlan for RepartitionExec { .batch_size .unwrap_or_else(|| context.session_config().batch_size()); - let spill_manager = SpillManager::new( - Arc::clone(&context.runtime_env()), - spill_metrics, - input.schema(), - ) - .with_compression_type(context.session_config().spill_compression()); + let spill_manager = preserve_order.then(|| { + SpillManager::new( + Arc::clone(&context.runtime_env()), + SpillMetrics::new(&metrics, partition), + input.schema(), + ) + .with_compression_type(context.session_config().spill_compression()) + }); // Get existing ordering to use for merging let sort_exprs = self.sort_exprs().cloned(); @@ -1797,7 +1797,18 @@ impl ExecutionPlan for RepartitionExec { preserve_order, &name, &context, - spill_manager.clone(), + || { + spill_manager.clone().unwrap_or_else(|| { + SpillManager::new( + Arc::clone(&context.runtime_env()), + SpillMetrics::new(&metrics, partition), + input.schema(), + ) + .with_compression_type( + context.session_config().spill_compression(), + ) + }) + }, coalescer_batch_size, )?; @@ -1869,7 +1880,9 @@ impl ExecutionPlan for RepartitionExec { .with_batch_size(context.session_config().batch_size()) .with_fetch(fetch) .with_reservation(merge_reservation) - .with_spill_manager(spill_manager) + .with_spill_manager( + spill_manager.expect("preserve-order spill manager must exist"), + ) .build() } else { // Non-preserve-order case: single input stream, so use the first spill reader @@ -4551,6 +4564,44 @@ mod tests { Ok(()) } + #[tokio::test] + async fn unordered_repartition_registers_spill_metrics_once() -> Result<()> { + let exec = RepartitionExec::try_new( + Arc::new(EmptyExec::new(Arc::new(Schema::empty()))), + Partitioning::RoundRobinBatch(2), + )?; + let task_ctx = Arc::new(TaskContext::default()); + let output_stream0 = exec.execute(0, Arc::clone(&task_ctx))?; + let output_stream1 = exec.execute(1, Arc::clone(&task_ctx))?; + + let metrics_before_poll = exec.metrics().unwrap(); + assert_eq!(metrics_before_poll.spill_count(), None); + + assert!(crate::common::collect(output_stream0).await?.is_empty()); + assert!(crate::common::collect(output_stream1).await?.is_empty()); + + assert_eq!(metrics_before_poll.spill_count(), None); + let metrics = exec.metrics().unwrap(); + assert_eq!( + metrics + .iter() + .filter(|metric| { + matches!( + metric.value().name(), + "spill_count" | "spilled_bytes" | "spilled_rows" + ) + }) + .count(), + 3 + ); + assert_eq!(metrics.for_partition(0).spill_count(), Some(0)); + assert_eq!(metrics.for_partition(1).spill_count(), None); + assert_eq!(metrics.spilled_bytes(), Some(0)); + assert_eq!(metrics.spilled_rows(), Some(0)); + + Ok(()) + } + #[tokio::test] async fn repartition_with_spilling() -> Result<()> { // Test that repartition successfully spills to disk when memory is constrained