You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2026/04/17 14:57:50 UTC

[PR] refactor: Share left-side spill file across partitions on OOM fallback [datafusion]

viirya opened a new pull request, #21699:
URL: https://github.com/apache/datafusion/pull/21699

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #.
   
   ## Rationale for this change
   
   To reduce the redundant re-execution of the left side during OOM fallback.
   
   ## What changes are included in this PR?
   
   Previously when OnceFut failed with OOM, each partition independently re-executed the left child to get its own stream. This was redundant since all partitions need the same left data.
   
   Now the first partition to initiate fallback spills the entire left side to disk via a shared OnceAsync<LeftSpillData>. Other partitions wait on the same future and read from the shared spill file, avoiding redundant re-execution of the left child.
   
   Co-authored-by: Claude Code
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   Unit test
   
   ## Are there any user-facing changes?
   
   No
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] refactor: Share left-side spill file across partitions on OOM fallback [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #21699:
URL: https://github.com/apache/datafusion/pull/21699#discussion_r3104872681


##########
datafusion/physical-plan/src/joins/nested_loop_join.rs:
##########
@@ -863,6 +873,22 @@ enum NLJState {
     EmitLeftUnmatched,
     Done,
 }
+/// Shared data for the left-side spill fallback.
+///
+/// When the in-memory `OnceFut` path fails with OOM, the first partition
+/// spills the entire left side to disk. This struct holds the spill file
+/// reference so other partitions can read from the same file.
+pub(crate) struct LeftSpillData {
+    /// SpillManager used to read the spill file (has the left schema)
+    spill_manager: SpillManager,
+    /// The spill file containing all left-side batches
+    spill_file: RefCountedTempFile,
+    /// Max batch memory size (for read_spill_as_stream)
+    max_batch_memory: usize,

Review Comment:
   Minor cleanup idea: this argument isn’t needed. At the `read_spill_as_stream()` call site, we can pass `None` directly.
   
   I tried to explain the reason in https://github.com/apache/datafusion/pull/21713



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] refactor: Share left-side spill file across partitions on OOM fallback [datafusion]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya merged PR #21699:
URL: https://github.com/apache/datafusion/pull/21699


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] refactor: Share left-side spill file across partitions on OOM fallback [datafusion]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #21699:
URL: https://github.com/apache/datafusion/pull/21699#issuecomment-4275351820

   Thanks @2010YOUY01 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] refactor: Share left-side spill file across partitions on OOM fallback [datafusion]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #21699:
URL: https://github.com/apache/datafusion/pull/21699#discussion_r3105916764


##########
datafusion/physical-plan/src/joins/nested_loop_join.rs:
##########
@@ -863,6 +873,22 @@ enum NLJState {
     EmitLeftUnmatched,
     Done,
 }
+/// Shared data for the left-side spill fallback.
+///
+/// When the in-memory `OnceFut` path fails with OOM, the first partition
+/// spills the entire left side to disk. This struct holds the spill file
+/// reference so other partitions can read from the same file.
+pub(crate) struct LeftSpillData {
+    /// SpillManager used to read the spill file (has the left schema)
+    spill_manager: SpillManager,
+    /// The spill file containing all left-side batches
+    spill_file: RefCountedTempFile,
+    /// Max batch memory size (for read_spill_as_stream)
+    max_batch_memory: usize,

Review Comment:
   Thanks for the approval and the suggestion! You're right, I'll pass None to read_spill_as_stream() and remove max_batch_memory from LeftSpillData. Will update.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] refactor: Share left-side spill file across partitions on OOM fallback [datafusion]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #21699:
URL: https://github.com/apache/datafusion/pull/21699#discussion_r3102668894


##########
datafusion/sqllogictest/test_files/nested_loop_join_spill.slt:
##########
@@ -50,7 +51,7 @@ INNER JOIN generate_series(1, 1) AS t2(v2)
 Plan with Metrics
 01)ProjectionExec: expr=[count(Int64(1))@0 as count(*)], metrics=[<slt:ignore>]
 02)--AggregateExec: mode=Single, gby=[], aggr=[count(Int64(1))], metrics=[<slt:ignore>]
-03)----NestedLoopJoinExec: join_type=Inner, filter=v1@0 + v2@1 > 0, projection=[], metrics=[output_rows=100.0 K, <slt:ignore> spill_count=1, <slt:ignore>]
+03)----NestedLoopJoinExec: join_type=Inner, filter=v1@0 + v2@1 > 0, projection=[], metrics=[output_rows=100.0 K, <slt:ignore> spill_count=2, <slt:ignore>]

Review Comment:
   Now the left side spills during OOM, so spill_count increases from 1 to 2.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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