@@ -49,6 +49,7 @@ use arrow::compute::{BatchCoalescer, SortOptions, filter_record_batch};
4949use arrow:: datatypes:: { DataType , Field , Schema } ;
5050use arrow_ord:: sort:: SortColumn ;
5151use arrow_schema:: SchemaRef ;
52+ use bytes:: Bytes ;
5253use datafusion_common:: JoinType :: * ;
5354use datafusion_common:: {
5455 JoinSide , internal_err,
@@ -59,9 +60,12 @@ use datafusion_common::{
5960} ;
6061use datafusion_common_runtime:: JoinSet ;
6162use datafusion_execution:: config:: SessionConfig ;
62- use datafusion_execution:: disk_manager:: { DiskManagerBuilder , DiskManagerMode } ;
63+ use datafusion_execution:: disk_manager:: {
64+ DiskManager , DiskManagerBuilder , DiskManagerMode ,
65+ } ;
6366use datafusion_execution:: memory_pool:: MemoryConsumer ;
6467use datafusion_execution:: runtime_env:: RuntimeEnvBuilder ;
68+ use datafusion_execution:: spill_file:: { SpillFile , SpillWriter , TempFileFactory } ;
6569use datafusion_execution:: { SendableRecordBatchStream , TaskContext } ;
6670use datafusion_expr:: Operator ;
6771use datafusion_physical_expr:: expressions:: BinaryExpr ;
@@ -70,6 +74,7 @@ use datafusion_physical_expr_common::physical_expr::PhysicalExprRef;
7074use futures:: { Stream , StreamExt } ;
7175use insta:: assert_snapshot;
7276use itertools:: Itertools ;
77+ use std:: collections:: VecDeque ;
7378
7479fn build_table (
7580 a : ( & str , & Vec < i32 > ) ,
@@ -5065,3 +5070,302 @@ async fn spill_read_back_single_source() -> Result<()> {
50655070
50665071 Ok ( ( ) )
50675072}
5073+
5074+ /// Small chunk size so even tiny test spill files are split into several
5075+ /// pieces, forcing multiple genuine suspend/resume cycles instead of one.
5076+ const PENDING_CHUNK_SIZE : usize = 16 ;
5077+
5078+ /// Splits real spill bytes into fixed-size chunks and yields `Poll::Pending`
5079+ /// before every chunk
5080+ struct PendingChunkedStream {
5081+ chunks : VecDeque < Bytes > ,
5082+ yield_pending : bool ,
5083+ }
5084+
5085+ impl PendingChunkedStream {
5086+ fn new ( bytes : Bytes ) -> Self {
5087+ let mut chunks = VecDeque :: new ( ) ;
5088+ if bytes. is_empty ( ) {
5089+ chunks. push_back ( bytes) ;
5090+ } else {
5091+ let mut remaining = bytes;
5092+ while !remaining. is_empty ( ) {
5093+ let take = PENDING_CHUNK_SIZE . min ( remaining. len ( ) ) ;
5094+ chunks. push_back ( remaining. split_to ( take) ) ;
5095+ }
5096+ }
5097+ Self {
5098+ chunks,
5099+ yield_pending : true ,
5100+ }
5101+ }
5102+ }
5103+
5104+ impl Stream for PendingChunkedStream {
5105+ type Item = Result < Bytes > ;
5106+
5107+ fn poll_next (
5108+ mut self : Pin < & mut Self > ,
5109+ cx : & mut Context < ' _ > ,
5110+ ) -> Poll < Option < Self :: Item > > {
5111+ if self . yield_pending {
5112+ self . yield_pending = false ;
5113+ cx. waker ( ) . wake_by_ref ( ) ;
5114+ return Poll :: Pending ;
5115+ }
5116+ // Pending before every subsequent chunk as well.
5117+ self . yield_pending = true ;
5118+ match self . chunks . pop_front ( ) {
5119+ Some ( chunk) => Poll :: Ready ( Some ( Ok ( chunk) ) ) ,
5120+ None => Poll :: Ready ( None ) ,
5121+ }
5122+ }
5123+ }
5124+
5125+ /// A `SpillFile` that delegates everything to a real local spill file,
5126+ /// except `read_stream`, which is forced through `PendingChunkedStream`.
5127+ struct PendingSpillFile {
5128+ inner : Arc < dyn SpillFile > ,
5129+ }
5130+
5131+ impl SpillFile for PendingSpillFile {
5132+ fn path ( & self ) -> Option < & std:: path:: Path > {
5133+ self . inner . path ( )
5134+ }
5135+
5136+ fn size ( & self ) -> Option < u64 > {
5137+ self . inner . size ( )
5138+ }
5139+
5140+ fn read_stream ( & self ) -> Result < Pin < Box < dyn Stream < Item = Result < Bytes > > + Send > > > {
5141+ let path = self
5142+ . inner
5143+ . path ( )
5144+ . expect ( "PendingSpillFile only wraps local files" )
5145+ . to_owned ( ) ;
5146+
5147+ let stream = futures:: stream:: once ( async move {
5148+ tokio:: fs:: read ( & path)
5149+ . await
5150+ . map ( Bytes :: from)
5151+ . map_err ( datafusion_common:: DataFusionError :: IoError )
5152+ } )
5153+ . flat_map (
5154+ |read_result| -> Pin < Box < dyn Stream < Item = Result < Bytes > > + Send > > {
5155+ match read_result {
5156+ Ok ( bytes) => Box :: pin ( PendingChunkedStream :: new ( bytes) ) ,
5157+ Err ( e) => Box :: pin ( futures:: stream:: once ( async move { Err ( e) } ) ) ,
5158+ }
5159+ } ,
5160+ ) ;
5161+
5162+ Ok ( Box :: pin ( stream) )
5163+ }
5164+
5165+ fn open_writer ( & self ) -> Result < Box < dyn SpillWriter > > {
5166+ self . inner . open_writer ( )
5167+ }
5168+ }
5169+
5170+ /// Wraps the default `OsTmpDirectory` factory so every spill file it
5171+ /// creates is a [`PendingSpillFile`].
5172+ struct PendingTempFileFactory {
5173+ inner : Arc < DiskManager > ,
5174+ }
5175+
5176+ impl TempFileFactory for PendingTempFileFactory {
5177+ fn create_temp_file ( & self , description : & str ) -> Result < Arc < dyn SpillFile > > {
5178+ Ok ( Arc :: new ( PendingSpillFile {
5179+ inner : self . inner . create_tmp_file ( description) ?,
5180+ } ) )
5181+ }
5182+ }
5183+
5184+ fn pending_disk_manager_builder ( ) -> DiskManagerBuilder {
5185+ let inner = Arc :: new (
5186+ DiskManagerBuilder :: default ( )
5187+ . with_mode ( DiskManagerMode :: OsTmpDirectory )
5188+ . build ( )
5189+ . unwrap ( ) ,
5190+ ) ;
5191+ DiskManagerBuilder :: default ( ) . with_mode ( DiskManagerMode :: Custom ( Arc :: new (
5192+ PendingTempFileFactory { inner } ,
5193+ ) ) )
5194+ }
5195+
5196+ /// Materializing-side (Inner/Left/Right/Full) coverage: identical to
5197+ /// `overallocation_multi_batch_spill`, but every spill read goes through
5198+ /// `PendingSpillFile`, so `poll_spilled_batches` must actually hit and
5199+ /// recover from `Poll::Pending` mid-read.
5200+ #[ tokio:: test]
5201+ async fn materializing_spill_pending_stream ( ) -> Result < ( ) > {
5202+ let left_batch_1 = build_table_i32 (
5203+ ( "a1" , & vec ! [ 0 , 1 ] ) ,
5204+ ( "b1" , & vec ! [ 1 , 1 ] ) ,
5205+ ( "c1" , & vec ! [ 4 , 5 ] ) ,
5206+ ) ;
5207+ let left_batch_2 = build_table_i32 (
5208+ ( "a1" , & vec ! [ 2 , 3 ] ) ,
5209+ ( "b1" , & vec ! [ 1 , 1 ] ) ,
5210+ ( "c1" , & vec ! [ 6 , 7 ] ) ,
5211+ ) ;
5212+ let right_batch_1 = build_table_i32 (
5213+ ( "a2" , & vec ! [ 0 , 10 ] ) ,
5214+ ( "b2" , & vec ! [ 1 , 1 ] ) ,
5215+ ( "c2" , & vec ! [ 50 , 60 ] ) ,
5216+ ) ;
5217+ let right_batch_2 = build_table_i32 (
5218+ ( "a2" , & vec ! [ 20 , 30 ] ) ,
5219+ ( "b2" , & vec ! [ 1 , 1 ] ) ,
5220+ ( "c2" , & vec ! [ 70 , 80 ] ) ,
5221+ ) ;
5222+ let left = build_table_from_batches ( vec ! [ left_batch_1, left_batch_2] ) ;
5223+ let right = build_table_from_batches ( vec ! [ right_batch_1, right_batch_2] ) ;
5224+ let on = vec ! [ (
5225+ Arc :: new( Column :: new_with_schema( "b1" , & left. schema( ) ) ?) as _,
5226+ Arc :: new( Column :: new_with_schema( "b2" , & right. schema( ) ) ?) as _,
5227+ ) ] ;
5228+ let sort_options = vec ! [ SortOptions :: default ( ) ; on. len( ) ] ;
5229+
5230+ let runtime = RuntimeEnvBuilder :: new ( )
5231+ . with_memory_limit ( 500 , 1.0 )
5232+ . with_disk_manager_builder ( pending_disk_manager_builder ( ) )
5233+ . build_arc ( ) ?;
5234+
5235+ for join_type in [ Inner , Left , Right , Full ] {
5236+ let task_ctx =
5237+ Arc :: new ( TaskContext :: default ( ) . with_runtime ( Arc :: clone ( & runtime) ) ) ;
5238+ let join = join_with_options (
5239+ Arc :: clone ( & left) ,
5240+ Arc :: clone ( & right) ,
5241+ on. clone ( ) ,
5242+ join_type,
5243+ sort_options. clone ( ) ,
5244+ NullEquality :: NullEqualsNothing ,
5245+ ) ?;
5246+ let stream = join. execute ( 0 , task_ctx) ?;
5247+ let spilled_result = common:: collect ( stream) . await . unwrap ( ) ;
5248+
5249+ let metrics = join. metrics ( ) . unwrap ( ) ;
5250+ assert ! (
5251+ metrics. spill_count( ) . unwrap( ) > 0 ,
5252+ "expected spill_count > 0 for {join_type:?}"
5253+ ) ;
5254+
5255+ // Compare against a no-spill run to make sure the Pending
5256+ // re-entry path didn't corrupt or drop any data.
5257+ let task_ctx_no_spill = Arc :: new ( TaskContext :: default ( ) ) ;
5258+ let join_no_spill = join_with_options (
5259+ Arc :: clone ( & left) ,
5260+ Arc :: clone ( & right) ,
5261+ on. clone ( ) ,
5262+ join_type,
5263+ sort_options. clone ( ) ,
5264+ NullEquality :: NullEqualsNothing ,
5265+ ) ?;
5266+ let stream = join_no_spill. execute ( 0 , task_ctx_no_spill) ?;
5267+ let no_spill_result = common:: collect ( stream) . await . unwrap ( ) ;
5268+
5269+ assert_eq ! (
5270+ spilled_result, no_spill_result,
5271+ "Pending-forced spill read produced different results for {join_type:?}"
5272+ ) ;
5273+ }
5274+
5275+ Ok ( ( ) )
5276+ }
5277+
5278+ /// Bitwise-side (Semi/Anti) coverage: identical to `bitwise_spill_with_filter`,
5279+ /// but every spill read goes through `PendingSpillFile`, forcing
5280+ /// `process_key_match_with_filter`'s spilled-batch loop to actually hit and
5281+ /// resume from `Poll::Pending`.
5282+ #[ tokio:: test]
5283+ async fn bitwise_spill_pending_stream ( ) -> Result < ( ) > {
5284+ let left = build_table (
5285+ ( "a1" , & vec ! [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ,
5286+ ( "b1" , & vec ! [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ,
5287+ ( "c1" , & vec ! [ 4 , 5 , 6 , 7 , 8 , 9 ] ) ,
5288+ ) ;
5289+ let right = build_table (
5290+ ( "a2" , & vec ! [ 10 , 20 , 30 , 40 , 50 ] ) ,
5291+ ( "b1" , & vec ! [ 1 , 3 , 4 , 6 , 8 ] ) ,
5292+ ( "c2" , & vec ! [ 50 , 60 , 70 , 80 , 90 ] ) ,
5293+ ) ;
5294+ let on = vec ! [ (
5295+ Arc :: new( Column :: new_with_schema( "b1" , & left. schema( ) ) ?) as _,
5296+ Arc :: new( Column :: new_with_schema( "b1" , & right. schema( ) ) ?) as _,
5297+ ) ] ;
5298+ let sort_options = vec ! [ SortOptions :: default ( ) ; on. len( ) ] ;
5299+
5300+ // c1 < c2 is always true for matching keys — same filter as
5301+ // bitwise_spill_with_filter, so the inner key group is buffered
5302+ // (and spilled) rather than short-circuited.
5303+ let filter = JoinFilter :: new (
5304+ Arc :: new ( BinaryExpr :: new (
5305+ Arc :: new ( Column :: new ( "c1" , 0 ) ) ,
5306+ Operator :: Lt ,
5307+ Arc :: new ( Column :: new ( "c2" , 1 ) ) ,
5308+ ) ) ,
5309+ vec ! [
5310+ ColumnIndex {
5311+ index: 2 ,
5312+ side: JoinSide :: Left ,
5313+ } ,
5314+ ColumnIndex {
5315+ index: 2 ,
5316+ side: JoinSide :: Right ,
5317+ } ,
5318+ ] ,
5319+ Arc :: new ( Schema :: new ( vec ! [
5320+ Field :: new( "c1" , DataType :: Int32 , false ) ,
5321+ Field :: new( "c2" , DataType :: Int32 , false ) ,
5322+ ] ) ) ,
5323+ ) ;
5324+
5325+ let runtime = RuntimeEnvBuilder :: new ( )
5326+ . with_memory_limit ( 100 , 1.0 )
5327+ . with_disk_manager_builder ( pending_disk_manager_builder ( ) )
5328+ . build_arc ( ) ?;
5329+
5330+ for join_type in [ LeftSemi , LeftAnti , RightSemi , RightAnti ] {
5331+ let task_ctx =
5332+ Arc :: new ( TaskContext :: default ( ) . with_runtime ( Arc :: clone ( & runtime) ) ) ;
5333+ let join = SortMergeJoinExec :: try_new (
5334+ Arc :: clone ( & left) ,
5335+ Arc :: clone ( & right) ,
5336+ on. clone ( ) ,
5337+ Some ( filter. clone ( ) ) ,
5338+ join_type,
5339+ sort_options. clone ( ) ,
5340+ NullEquality :: NullEqualsNothing ,
5341+ ) ?;
5342+ let stream = join. execute ( 0 , task_ctx) ?;
5343+ let spilled_result = common:: collect ( stream) . await . unwrap ( ) ;
5344+
5345+ let metrics = join. metrics ( ) . unwrap ( ) ;
5346+ assert ! (
5347+ metrics. spill_count( ) . unwrap( ) > 0 ,
5348+ "expected spill_count > 0 for {join_type:?}"
5349+ ) ;
5350+
5351+ let task_ctx_no_spill = Arc :: new ( TaskContext :: default ( ) ) ;
5352+ let join_no_spill = SortMergeJoinExec :: try_new (
5353+ Arc :: clone ( & left) ,
5354+ Arc :: clone ( & right) ,
5355+ on. clone ( ) ,
5356+ Some ( filter. clone ( ) ) ,
5357+ join_type,
5358+ sort_options. clone ( ) ,
5359+ NullEquality :: NullEqualsNothing ,
5360+ ) ?;
5361+ let stream = join_no_spill. execute ( 0 , task_ctx_no_spill) ?;
5362+ let no_spill_result = common:: collect ( stream) . await . unwrap ( ) ;
5363+
5364+ assert_eq ! (
5365+ spilled_result, no_spill_result,
5366+ "Pending-forced spill read produced different results for {join_type:?}"
5367+ ) ;
5368+ }
5369+
5370+ Ok ( ( ) )
5371+ }
0 commit comments