You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/10/09 01:49:29 UTC

(datafusion) branch main updated: fix: apply `spill_compression` to `RepartitionExec` spill files (#26142)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 048fc192a9 fix: apply `spill_compression` to `RepartitionExec` spill files (#26142)
048fc192a9 is described below

commit 048fc192a9b0366ac3c61e44482bbfa60450b169
Author: Adrian Garcia Badaracco <17...@users.noreply.github.com>
AuthorDate: Fri Oct 9 01:38:38 2026 +0000

    fix: apply `spill_compression` to `RepartitionExec` spill files (#26142)
    
    ## Which issue does this PR close?
    
    - Closes #26141.
    
    ## Rationale for this change
    
    `RepartitionExec` does not apply
    `datafusion.execution.spill_compression`, so its spill files are always
    uncompressed. All other spilling operators (sort, aggregate, sort-merge
    join, nested loop join) apply the setting. A user who sets a codec to
    decrease spill disk usage gets no decrease for the data that
    `RepartitionExec` spills.
    
    ## What changes are included in this PR?
    
    - `RepartitionExec::execute` now builds its `SpillManager` with
    `.with_compression_type(context.session_config().spill_compression())`,
    the same as the other operators.
    - No change is necessary on the read side. Spill files use the Arrow IPC
    Stream format, and the reader gets the codec from the stream.
    - I checked the other non-test `SpillManager::new` call sites.
    `RepartitionExec` was the only one that did not pass the setting.
    
    ## What is the testing strategy for this PR?
    
    The new test `repartition_spill_honors_spill_compression` runs a
    spilling `RepartitionExec` on constant (highly compressible) data with
    `uncompressed`, `lz4_frame` and `zstd`. It asserts that the same rows
    spill, that the data reads back intact, and that `spilled_bytes` with a
    codec is less than half of the uncompressed value.
    
    The test fails on `main` (`679264 vs 679264 bytes`) and passes with the
    fix:
    
    | `spill_compression` | `spilled_bytes` on `main` | `spilled_bytes` with
    this PR |
    | --- | --- | --- |
    | `uncompressed` | 679,264 | 679,264 |
    | `lz4_frame` | 679,264 | 7,744 |
    | `zstd` | 679,264 | 5,024 |
    
    ## Are there any user-facing changes?
    
    Yes. Spill files from `RepartitionExec` now use the codec from
    `datafusion.execution.spill_compression`. The default (`uncompressed`)
    does not change. There are no API changes.
---
 datafusion/physical-plan/src/repartition/mod.rs | 78 ++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)

diff --git a/datafusion/physical-plan/src/repartition/mod.rs b/datafusion/physical-plan/src/repartition/mod.rs
index 51ce1345fc..629846b3de 100644
--- a/datafusion/physical-plan/src/repartition/mod.rs
+++ b/datafusion/physical-plan/src/repartition/mod.rs
@@ -1758,7 +1758,8 @@ impl ExecutionPlan for RepartitionExec {
             Arc::clone(&context.runtime_env()),
             spill_metrics,
             input.schema(),
-        );
+        )
+        .with_compression_type(context.session_config().spill_compression());
 
         // Get existing ordering to use for merging
         let sort_exprs = self.sort_exprs().cloned();
@@ -2656,6 +2657,7 @@ mod tests {
     use arrow::datatypes::{DataType, Field, Schema, TimeUnit};
     use datafusion_common::ScalarValue;
     use datafusion_common::cast::{as_string_array, as_uint32_array};
+    use datafusion_common::config::SpillCompression;
     use datafusion_common::exec_err;
     use datafusion_common::test_util::batches_to_sort_string;
     use datafusion_common_runtime::JoinSet;
@@ -4589,6 +4591,80 @@ mod tests {
         Ok(())
     }
 
+    /// Runs a spilling `RepartitionExec` with the given spill compression and
+    /// returns `(spilled_rows, spilled_bytes)`.
+    async fn repartition_spill_with_compression(
+        spill_compression: SpillCompression,
+    ) -> Result<(usize, usize)> {
+        // Highly compressible input: 20 batches of 8192 identical values
+        let schema = test_schema(false);
+        let batch = RecordBatch::try_new(
+            Arc::clone(&schema),
+            vec![Arc::new(UInt32Array::from(vec![42; 8192]))],
+        )?;
+        let num_batches = 20;
+        let input_partitions = vec![vec![batch; num_batches]];
+
+        // Tight memory limit to force every batch to spill
+        let runtime = RuntimeEnvBuilder::default()
+            .with_memory_limit(1, 1.0)
+            .build_arc()?;
+        let session_config =
+            SessionConfig::new().with_spill_compression(spill_compression);
+        let task_ctx = Arc::new(
+            TaskContext::default()
+                .with_runtime(runtime)
+                .with_session_config(session_config),
+        );
+
+        let exec =
+            TestMemoryExec::try_new_exec(&input_partitions, Arc::clone(&schema), None)?;
+        let exec = RepartitionExec::try_new(exec, Partitioning::RoundRobinBatch(4))?;
+
+        let mut total_rows = 0;
+        for i in 0..exec.partitioning().partition_count() {
+            let mut stream = exec.execute(i, Arc::clone(&task_ctx))?;
+            while let Some(result) = stream.next().await {
+                let batch = result?;
+                // Spilled data must read back intact regardless of the codec
+                let values = as_uint32_array(batch.column(0))?;
+                assert!(values.iter().all(|v| v == Some(42)));
+                total_rows += batch.num_rows();
+            }
+        }
+        assert_eq!(total_rows, num_batches * 8192);
+
+        let metrics = exec.metrics().unwrap();
+        Ok((
+            metrics.spilled_rows().unwrap(),
+            metrics.spilled_bytes().unwrap(),
+        ))
+    }
+
+    #[tokio::test]
+    async fn repartition_spill_honors_spill_compression() -> Result<()> {
+        let (uncompressed_rows, uncompressed_bytes) =
+            repartition_spill_with_compression(SpillCompression::Uncompressed).await?;
+        assert!(uncompressed_rows > 0, "Expected spilling to occur");
+
+        for spill_compression in [SpillCompression::Lz4Frame, SpillCompression::Zstd] {
+            let (rows, bytes) =
+                repartition_spill_with_compression(spill_compression).await?;
+            assert_eq!(
+                rows, uncompressed_rows,
+                "Expected the same rows to spill with {spill_compression}"
+            );
+            // The input is constant, so any codec shrinks it by far more than 2x
+            assert!(
+                bytes * 2 < uncompressed_bytes,
+                "Expected {spill_compression} spill files to be much smaller than \
+                 uncompressed ones: {bytes} vs {uncompressed_bytes} bytes"
+            );
+        }
+
+        Ok(())
+    }
+
     #[tokio::test]
     async fn oom() -> Result<()> {
         use datafusion_execution::disk_manager::{DiskManagerBuilder, DiskManagerMode};


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org