@@ -245,8 +245,7 @@ async fn assert_released(pool: &AdjustablePool, runtime: &RuntimeEnv) {
245245 assert_eq ! ( state. unchecked_over_limit, 0 ) ;
246246}
247247
248- #[ tokio:: test]
249- async fn test_spill_preserves_merge_workspace_after_limit_decreases ( ) -> Result < ( ) > {
248+ async fn check_spill_preserves_merge_workspace_after_limit_decreases ( ) -> Result < ( ) > {
250249 let fixture = fixture ( ) ?;
251250 let pool = AdjustablePool :: new ( fixture. capacity ) ;
252251 let ( mut sorter, runtime) = sorter ( & fixture, Arc :: clone ( & pool) ) ?;
@@ -279,6 +278,17 @@ async fn test_spill_preserves_merge_workspace_after_limit_decreases() -> Result<
279278 Ok ( ( ) )
280279}
281280
281+ #[ tokio:: test]
282+ async fn test_spill_preserves_merge_workspace_after_limit_decreases ( ) -> Result < ( ) > {
283+ check_spill_preserves_merge_workspace_after_limit_decreases ( ) . await
284+ }
285+
286+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
287+ async fn test_spill_preserves_merge_workspace_after_limit_decreases_multithreaded ( )
288+ -> Result < ( ) > {
289+ check_spill_preserves_merge_workspace_after_limit_decreases ( ) . await
290+ }
291+
282292#[ tokio:: test]
283293async fn test_spill_workspace_does_not_hide_insufficient_memory ( ) -> Result < ( ) > {
284294 let fixture = fixture ( ) ?;
@@ -533,6 +543,155 @@ async fn test_chunked_string_view_spill_can_use_workspace() -> Result<()> {
533543 check_chunked_string_view_workspace ( true ) . await
534544}
535545
546+ /// Prepared runs retain their workspace loans until merging starts. Looking
547+ /// ahead during the merge returns enough workspace for its batch and cursors.
548+ async fn check_sort_preparation_workspace ( consume_output : bool ) -> Result < ( ) > {
549+ let batch_size = 1024 ;
550+ let values: ArrayRef = Arc :: new ( StringViewArray :: from_iter_values (
551+ ( 0 ..4096 )
552+ . rev ( )
553+ . map ( |i| format ! ( "row-{i:08}-{}" , "x" . repeat( 87 ) ) ) ,
554+ ) ) ;
555+ let batch = RecordBatch :: try_from_iter ( [ ( "key" , values) ] ) ?;
556+ let ordering: LexOrdering = [ PhysicalSortExpr :: new_default ( Arc :: new ( Column :: new (
557+ "key" , 0 ,
558+ ) ) ) ]
559+ . into ( ) ;
560+ let input_bytes = get_reserved_bytes_for_record_batch ( & batch) ?;
561+ let sorted_batches = sort_batch_chunked ( & batch, & ordering, batch_size) ?;
562+ // Each StringView output chunk retains the shared string buffers.
563+ // Summing the per-batch estimates counts those buffers once per chunk.
564+ let sorted_bytes = sorted_batches
565+ . iter ( )
566+ . map ( get_record_batch_memory_size)
567+ . sum :: < usize > ( ) ;
568+ let loan_per_run = sorted_bytes - input_bytes;
569+ let first_bytes = get_record_batch_memory_size ( & sorted_batches[ 0 ] ) ;
570+ assert ! ( loan_per_run > first_bytes) ;
571+ // Both full loans fit before merging; only one extra batch fits beside them.
572+ let workspace = 2 * loan_per_run + first_bytes;
573+ let runtime = RuntimeEnvBuilder :: new ( )
574+ . with_memory_pool ( Arc :: new ( GreedyMemoryPool :: new ( 2 * input_bytes + workspace) ) )
575+ . build_arc ( ) ?;
576+ let mut sorter = ExternalSorter :: new (
577+ 0 ,
578+ batch. schema ( ) ,
579+ ordering. clone ( ) ,
580+ batch_size,
581+ workspace,
582+ 0 ,
583+ SpillCompression :: Uncompressed ,
584+ & ExecutionPlanMetricsSet :: new ( ) ,
585+ Arc :: clone ( & runtime) ,
586+ ) ?;
587+ sorter. insert_batch ( batch. clone ( ) ) . await ?;
588+ sorter. insert_batch ( batch. clone ( ) ) . await ?;
589+ sorter. merge_reservation . free ( ) ;
590+ let stream = sorter. in_mem_sort_stream ( false , false ) . await ?;
591+ // Verify every sort task exits before merging. Tokio can update its task
592+ // count shortly after the join completes.
593+ tokio:: time:: timeout ( Duration :: from_secs ( 5 ) , async {
594+ while tokio:: runtime:: Handle :: current ( )
595+ . metrics ( )
596+ . num_alive_tasks ( )
597+ != 0
598+ {
599+ tokio:: task:: yield_now ( ) . await ;
600+ }
601+ } )
602+ . await
603+ . expect ( "sort jobs must finish before merging" ) ;
604+ // Temporarily borrow the unused workspace: both runs hold their full loans,
605+ // leaving only the extra batch. Dropping the temporary returns it immediately.
606+ assert_eq ! ( sorter. merge_pool. borrow( usize :: MAX ) . size( ) , first_bytes) ;
607+ if consume_output {
608+ let output: Vec < RecordBatch > = stream. try_collect ( ) . await ?;
609+ let expected = sort_batch (
610+ & concat_batches ( & batch. schema ( ) , & [ batch. clone ( ) , batch. clone ( ) ] ) ?,
611+ & ordering,
612+ None ,
613+ ) ?;
614+ assert_eq ! ( concat_batches( & batch. schema( ) , & output) ?, expected) ;
615+ } else {
616+ drop ( stream) ;
617+ }
618+ drop ( sorter) ;
619+ assert_eq ! ( runtime. memory_pool. reserved( ) , 0 ) ;
620+ Ok ( ( ) )
621+ }
622+
623+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
624+ async fn test_sort_preparation_retains_workspace_until_drop ( ) -> Result < ( ) > {
625+ check_sort_preparation_workspace ( false ) . await
626+ }
627+
628+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
629+ async fn test_sort_merge_lookahead_returns_cursor_workspace ( ) -> Result < ( ) > {
630+ check_sort_preparation_workspace ( true ) . await
631+ }
632+
633+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
634+ async fn test_sort_preparation_keeps_output_reserved ( ) -> Result < ( ) > {
635+ let parent = RecordBatch :: try_from_iter ( [ (
636+ "key" ,
637+ Arc :: new ( Int64Array :: from_iter_values ( ( 0 ..8 * 4096 ) . rev ( ) ) ) as ArrayRef ,
638+ ) ] ) ?;
639+ // All runs share one input buffer, which must be reserved only once.
640+ let batches = ( 0 ..8 )
641+ . map ( |run| parent. slice ( run * 4096 , 4096 ) )
642+ . collect :: < Vec < _ > > ( ) ;
643+ let ordering: LexOrdering = [ PhysicalSortExpr :: new_default ( Arc :: new ( Column :: new (
644+ "key" , 0 ,
645+ ) ) ) ]
646+ . into ( ) ;
647+ let input_bytes = get_reserved_bytes_for_record_batch ( & parent) ?;
648+ let sorted_bytes =
649+ get_record_batch_memory_size ( & sort_batch ( & batches[ 0 ] , & ordering, None ) ?)
650+ * batches. len ( ) ;
651+ // Reserve the merge's cursor memory alongside the sorted batches.
652+ let merge_workspace = sorted_bytes;
653+ let capacity = input_bytes + merge_workspace;
654+ let runtime = RuntimeEnvBuilder :: new ( )
655+ . with_memory_pool ( Arc :: new ( GreedyMemoryPool :: new ( capacity) ) )
656+ . build_arc ( ) ?;
657+ let mut sorter = ExternalSorter :: new (
658+ 0 ,
659+ batches[ 0 ] . schema ( ) ,
660+ ordering. clone ( ) ,
661+ 8192 ,
662+ merge_workspace,
663+ 0 ,
664+ SpillCompression :: Uncompressed ,
665+ & ExecutionPlanMetricsSet :: new ( ) ,
666+ Arc :: clone ( & runtime) ,
667+ ) ?;
668+ for batch in & batches {
669+ sorter. insert_batch ( batch. clone ( ) ) . await ?;
670+ }
671+ sorter. merge_reservation . free ( ) ;
672+ let stream = sorter. in_mem_sort_stream ( false , false ) . await ?;
673+ assert_eq ! (
674+ runtime. memory_pool. reserved( ) ,
675+ sorted_bytes + merge_workspace
676+ ) ;
677+
678+ // Another consumer can take the released sort scratch space, while prepared
679+ // batches and merge workspace remain reserved for this sort.
680+ let other = MemoryConsumer :: new ( "other" ) . register ( & runtime. memory_pool ) ;
681+ other. try_grow ( capacity - runtime. memory_pool . reserved ( ) ) ?;
682+ let output: Vec < RecordBatch > = stream. try_collect ( ) . await ?;
683+ let expected = sort_batch (
684+ & concat_batches ( & batches[ 0 ] . schema ( ) , & batches) ?,
685+ & ordering,
686+ None ,
687+ ) ?;
688+ assert_eq ! ( concat_batches( & batches[ 0 ] . schema( ) , & output) ?, expected) ;
689+ drop ( sorter) ;
690+ drop ( other) ;
691+ assert_eq ! ( runtime. memory_pool. reserved( ) , 0 ) ;
692+ Ok ( ( ) )
693+ }
694+
536695#[ tokio:: test]
537696async fn test_single_batch_spill_preserves_workspace_after_limit_decreases ( ) -> Result < ( ) >
538697{
0 commit comments