99use std:: fmt;
1010use std:: fmt:: { Debug , Formatter } ;
1111use std:: future:: Future ;
12+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
1213use std:: sync:: { Arc , OnceLock } ;
1314
1415use parking_lot:: RwLock ;
@@ -309,9 +310,9 @@ struct ExternalSorter {
309310 /// How much memory to reserve for performing in-memory sort/merges
310311 /// prior to spilling.
311312 sort_spill_reservation_bytes : usize ,
312- /// Other partitions of this sort run alongside this one and share its
313- /// memory pool.
314- has_sibling_partitions : bool ,
313+ /// Partitions of the owning `SortExec`, this one included, still reading
314+ /// their input. Each one will need memory from the pool this sort shares .
315+ partitions_reading_input : Arc < AtomicUsize > ,
315316 /// Shared by the owning SortExec's partitions. Initialized on the runtime
316317 /// that polls the sort, which may differ from the one calling `execute`.
317318 sort_concurrency : Arc < OnceLock < SortConcurrency > > ,
@@ -375,17 +376,25 @@ impl ExternalSorter {
375376 batch_size,
376377 sort_spill_reservation_bytes,
377378 sort_in_place_threshold_bytes,
378- has_sibling_partitions : false ,
379+ partitions_reading_input : Arc :: new ( AtomicUsize :: new ( 1 ) ) ,
379380 sort_concurrency : Arc :: new ( OnceLock :: new ( ) ) ,
380381 } )
381382 }
382383
383- /// Record whether other partitions of this sort share its memory pool.
384- fn with_sibling_partitions ( mut self , has_sibling_partitions : bool ) -> Self {
385- self . has_sibling_partitions = has_sibling_partitions;
384+ /// Share the count of partitions still reading their input with the
385+ /// other partitions of this sort. It must include this partition.
386+ fn with_partitions_reading_input ( mut self , reading : Arc < AtomicUsize > ) -> Self {
387+ self . partitions_reading_input = reading;
386388 self
387389 }
388390
391+ /// Records that this partition has read all its input, and returns whether
392+ /// another partition still reads its own. Executing partitions again
393+ /// without `reset_state` can wrap the count, which only keeps headroom.
394+ fn finish_reading_input ( & self ) -> bool {
395+ self . partitions_reading_input . fetch_sub ( 1 , Ordering :: AcqRel ) > 1
396+ }
397+
389398 /// Appends an unsorted [`RecordBatch`] to `in_mem_batches`
390399 ///
391400 /// Updates memory usage metrics, and possibly triggers spilling to disk
@@ -416,6 +425,7 @@ impl ExternalSorter {
416425 /// 2. A combined streaming merge incorporating both in-memory
417426 /// batches and data from spill files on disk.
418427 async fn sort ( & mut self ) -> Result < SendableRecordBatchStream > {
428+ let other_partitions_reading = self . finish_reading_input ( ) ;
419429 if self . spilled_before ( ) {
420430 // Sort `in_mem_batches` and spill it first. If there are many
421431 // `in_mem_batches` and the memory limit is almost reached, merging
@@ -444,15 +454,17 @@ impl ExternalSorter {
444454 // Other partitions of this sort, still buffering input, share
445455 // the pool with this merge, and may hold nothing at the moment
446456 // it seats its runs. Seating every run the pool admits would
447- // leave them nothing, so keep headroom while they exist or
448- // anyone else holds memory in the pool. A lone partition with
449- // the pool to itself seats every run that fits: the operator
450- // consuming its output reserves nothing before its first
451- // batch, and keeping headroom for it would cost an extra merge
452- // pass whether or not it ever needs the memory.
457+ // leave them nothing, so keep headroom while any of them is
458+ // still reading or anyone else holds memory in the pool;
459+ // partitions already merging hold memory, so the second check
460+ // covers them. A lone partition, or the last one to finish
461+ // reading, with the pool to itself seats every run that fits:
462+ // the operator consuming its output reserves nothing before
463+ // its first batch, and keeping headroom for it would cost an
464+ // extra merge pass whether or not it ever needs the memory.
453465 . with_fan_in_headroom (
454466 Arc :: clone ( & self . runtime . memory_pool ) ,
455- self . has_sibling_partitions ,
467+ other_partitions_reading ,
456468 )
457469 . build ( )
458470 } else {
@@ -1349,6 +1361,10 @@ pub struct SortExec {
13491361 /// Batch-sort limit shared by partitions and clones.
13501362 /// `reset_state` returns a new plan with a fresh limit.
13511363 sort_concurrency : Arc < OnceLock < SortConcurrency > > ,
1364+ /// Partitions still reading their input, shared with their sorters. Set
1365+ /// on the first `execute`, once the input can no longer change.
1366+ /// `reset_state` returns a new plan with a fresh count.
1367+ partitions_reading_input : Arc < OnceLock < Arc < AtomicUsize > > > ,
13521368}
13531369
13541370impl SortExec {
@@ -1369,6 +1385,7 @@ impl SortExec {
13691385 cache : Arc :: new ( cache) ,
13701386 filter : None ,
13711387 sort_concurrency : Arc :: new ( OnceLock :: new ( ) ) ,
1388+ partitions_reading_input : Arc :: new ( OnceLock :: new ( ) ) ,
13721389 }
13731390 }
13741391
@@ -1446,6 +1463,7 @@ impl SortExec {
14461463 cache : Arc :: clone ( & self . cache ) ,
14471464 filter : self . filter . clone ( ) ,
14481465 sort_concurrency : Arc :: clone ( & self . sort_concurrency ) ,
1466+ partitions_reading_input : Arc :: clone ( & self . partitions_reading_input ) ,
14491467 }
14501468 }
14511469
@@ -1776,14 +1794,15 @@ impl ExecutionPlan for SortExec {
17761794 . downcast_ref :: < SortExec > ( )
17771795 . expect ( "rebuilt SortExec with new children" )
17781796 . clone ( ) ;
1779- // Our dynamic filter, execution metrics and sort concurrency are the state
1780- // we need to reset.
1797+ // Our dynamic filter, execution metrics, sort concurrency and count of
1798+ // partitions reading input are the state we need to reset.
17811799 if let Some ( filter) = & new_sort. filter {
17821800 filter. read ( ) . expr ( ) . reset ( ) ;
17831801 new_sort. filter = Some ( new_sort. create_filter ( ) ) ;
17841802 }
17851803 new_sort. metrics_set = ExecutionPlanMetricsSet :: new ( ) ;
17861804 new_sort. sort_concurrency = Arc :: new ( OnceLock :: new ( ) ) ;
1805+ new_sort. partitions_reading_input = Arc :: new ( OnceLock :: new ( ) ) ;
17871806
17881807 Ok ( Arc :: new ( new_sort) )
17891808 }
@@ -1865,9 +1884,13 @@ impl ExecutionPlan for SortExec {
18651884 & self . metrics_set ,
18661885 context. runtime_env ( ) ,
18671886 ) ?
1868- . with_sibling_partitions (
1869- self . input . output_partitioning ( ) . partition_count ( ) > 1 ,
1870- ) ;
1887+ . with_partitions_reading_input ( Arc :: clone (
1888+ self . partitions_reading_input . get_or_init ( || {
1889+ Arc :: new ( AtomicUsize :: new (
1890+ self . input . output_partitioning ( ) . partition_count ( ) ,
1891+ ) )
1892+ } ) ,
1893+ ) ) ;
18711894 sorter. sort_concurrency = Arc :: clone ( & self . sort_concurrency ) ;
18721895 Ok ( Box :: pin ( RecordBatchStreamAdapter :: new (
18731896 self . schema ( ) ,
@@ -2070,6 +2093,7 @@ impl ExecutionPlan for SortExec {
20702093 filter,
20712094 // Execution state, initialized on the runtime polling the decoded plan.
20722095 sort_concurrency : _,
2096+ partitions_reading_input : _,
20732097 } = self ;
20742098 let input = ctx. encode_child ( input) ?;
20752099 let expr = expr
0 commit comments