@@ -900,3 +900,286 @@ impl RecordBatchStream for OrderedFinalAggregateStream {
900900 Arc :: clone ( & self . schema )
901901 }
902902}
903+
904+ #[ cfg( test) ]
905+ mod tests {
906+ use super :: * ;
907+ use crate :: ExecutionPlan ;
908+ use crate :: aggregates:: PhysicalGroupBy ;
909+ use crate :: common:: collect;
910+ use crate :: stream:: RecordBatchStreamAdapter ;
911+ use crate :: test:: TestMemoryExec ;
912+ use arrow:: array:: { Int64Array , StringViewArray } ;
913+ use arrow:: datatypes:: { DataType , Field , Schema } ;
914+ use datafusion_execution:: config:: SessionConfig ;
915+ use datafusion_execution:: memory_pool:: { GreedyMemoryPool , MemoryPool } ;
916+ use datafusion_execution:: runtime_env:: RuntimeEnvBuilder ;
917+ use datafusion_functions_aggregate:: { min_max:: min_udaf, sum:: sum_udaf} ;
918+ use datafusion_physical_expr:: aggregate:: AggregateExprBuilder ;
919+ use datafusion_physical_expr:: expressions:: col;
920+ use futures:: FutureExt ;
921+ use futures:: channel:: mpsc;
922+ use std:: collections:: BTreeMap ;
923+
924+ #[ derive( Clone , Copy ) ]
925+ enum Finish {
926+ Collect ,
927+ DropDuringMerge ,
928+ DropDuringReplay ,
929+ InputError ,
930+ }
931+
932+ #[ tokio:: test]
933+ async fn spill_replay_with_another_ordered_partition ( ) -> Result < ( ) > {
934+ for input_batches in [ 28 , 36 , 55 , 63 ] {
935+ run_shared_pool_case ( input_batches, 600 * 1024 , Finish :: Collect ) . await ?;
936+ }
937+ // The same input also produces the reference results without spilling.
938+ run_shared_pool_case ( 63 , 10 * 1024 * 1024 , Finish :: Collect ) . await
939+ }
940+
941+ #[ tokio:: test]
942+ async fn spill_replay_releases_memory_on_drop ( ) -> Result < ( ) > {
943+ run_shared_pool_case ( 36 , 600 * 1024 , Finish :: DropDuringMerge ) . await ?;
944+ run_shared_pool_case ( 36 , 600 * 1024 , Finish :: DropDuringReplay ) . await
945+ }
946+
947+ #[ tokio:: test]
948+ async fn ordered_spill_releases_memory_on_input_error ( ) -> Result < ( ) > {
949+ run_shared_pool_case ( 36 , 600 * 1024 , Finish :: InputError ) . await
950+ }
951+
952+ async fn run_shared_pool_case (
953+ input_batches : i64 ,
954+ limit : usize ,
955+ finish : Finish ,
956+ ) -> Result < ( ) > {
957+ let schema = Arc :: new ( Schema :: new ( vec ! [
958+ Field :: new( "a" , DataType :: Int64 , false ) ,
959+ Field :: new( "b" , DataType :: Int64 , false ) ,
960+ Field :: new( "v" , DataType :: Int64 , false ) ,
961+ Field :: new( "s" , DataType :: Utf8View , false ) ,
962+ ] ) ) ;
963+ let groups = PhysicalGroupBy :: new_single ( vec ! [
964+ ( col( "a" , & schema) ?, "a" . into( ) ) ,
965+ ( col( "b" , & schema) ?, "b" . into( ) ) ,
966+ ] ) ;
967+ let expressions = vec ! [
968+ Arc :: new(
969+ AggregateExprBuilder :: new( sum_udaf( ) , vec![ col( "v" , & schema) ?] )
970+ . schema( Arc :: clone( & schema) )
971+ . alias( "sum" )
972+ . build( ) ?,
973+ ) ,
974+ Arc :: new(
975+ AggregateExprBuilder :: new( min_udaf( ) , vec![ col( "s" , & schema) ?] )
976+ . schema( Arc :: clone( & schema) )
977+ . alias( "min" )
978+ . build( ) ?,
979+ ) ,
980+ ] ;
981+ let empty = TestMemoryExec :: try_new_exec ( & [ vec ! [ ] ] , Arc :: clone ( & schema) , None ) ?;
982+ let partial = AggregateExec :: try_new (
983+ AggregateMode :: Partial ,
984+ groups. clone ( ) ,
985+ expressions. clone ( ) ,
986+ vec ! [ None ; 2 ] ,
987+ empty,
988+ Arc :: clone ( & schema) ,
989+ ) ?;
990+ let partial_schema = partial. schema ( ) ;
991+ let ordering =
992+ LexOrdering :: new ( [ PhysicalSortExpr :: new_default ( col ( "a" , & partial_schema) ?) ] )
993+ . unwrap ( ) ;
994+ let input = TestMemoryExec :: try_new (
995+ & [ vec ! [ ] , vec ! [ ] ] ,
996+ Arc :: clone ( & partial_schema) ,
997+ None ,
998+ ) ?
999+ . try_with_sort_information ( vec ! [ ordering] ) ?;
1000+ let aggregate = AggregateExec :: try_new (
1001+ AggregateMode :: FinalPartitioned ,
1002+ groups. as_final ( ) ,
1003+ expressions,
1004+ vec ! [ None ; 2 ] ,
1005+ Arc :: new ( TestMemoryExec :: update_cache ( & Arc :: new ( input) ) ) ,
1006+ schema,
1007+ ) ?;
1008+ assert_eq ! (
1009+ aggregate. input_order_mode( ) ,
1010+ & InputOrderMode :: PartiallySorted ( vec![ 0 ] )
1011+ ) ;
1012+
1013+ let pool: Arc < dyn MemoryPool > = Arc :: new ( GreedyMemoryPool :: new ( limit) ) ;
1014+ let context = Arc :: new (
1015+ TaskContext :: default ( )
1016+ . with_session_config (
1017+ SessionConfig :: new ( ) . with_batch_size ( 128 ) . set_bool (
1018+ "datafusion.execution.enable_migration_aggregate" ,
1019+ true ,
1020+ ) ,
1021+ )
1022+ . with_runtime (
1023+ RuntimeEnvBuilder :: new ( )
1024+ . with_max_spill_merge_fan_in ( 2 )
1025+ . with_memory_pool ( Arc :: clone ( & pool) )
1026+ . build_arc ( ) ?,
1027+ ) ,
1028+ ) ;
1029+ let mut streams = vec ! [ ] ;
1030+ let mut senders = vec ! [ ] ;
1031+ for partition in 0 ..2 {
1032+ let ( sender, receiver) = mpsc:: unbounded ( ) ;
1033+ let input = Box :: pin ( RecordBatchStreamAdapter :: new (
1034+ Arc :: clone ( & partial_schema) ,
1035+ receiver,
1036+ ) ) ;
1037+ let stream = OrderedFinalAggregateStream :: new_with_input (
1038+ & aggregate,
1039+ & context,
1040+ partition,
1041+ input,
1042+ aggregate. input_order_mode ( ) ,
1043+ ) ?;
1044+ senders. push ( sender) ;
1045+ streams. push ( stream) ;
1046+ }
1047+ let mut expected = BTreeMap :: new ( ) ;
1048+ let mut make_batch = |partition : i64 , start : i64 | {
1049+ for value in start..start + 128 {
1050+ let entry = expected
1051+ . entry ( (
1052+ partition,
1053+ if partition == 0 && value % 128 == 0 {
1054+ 0
1055+ } else {
1056+ value
1057+ } ,
1058+ ) )
1059+ . or_insert_with ( || ( 0 , ( value % 2 ) . to_string ( ) ) ) ;
1060+ entry. 0 += value * 2 ;
1061+ }
1062+ RecordBatch :: try_new (
1063+ Arc :: clone ( & partial_schema) ,
1064+ vec ! [
1065+ Arc :: new( Int64Array :: from( vec![ partition; 128 ] ) ) ,
1066+ Arc :: new( Int64Array :: from_iter_values( ( start..start + 128 ) . map(
1067+ |value| {
1068+ if partition == 0 && value % 128 == 0 {
1069+ 0
1070+ } else {
1071+ value
1072+ }
1073+ } ,
1074+ ) ) ) ,
1075+ Arc :: new( Int64Array :: from_iter_values(
1076+ ( start..start + 128 ) . map( |v| v * 2 ) ,
1077+ ) ) ,
1078+ Arc :: new( StringViewArray :: from_iter_values(
1079+ ( start..start + 128 ) . map( |v| if v % 2 == 0 { "0" } else { "1" } ) ,
1080+ ) ) ,
1081+ ] ,
1082+ )
1083+ . unwrap ( )
1084+ } ;
1085+ // Keep partition 1's incomplete ordered run live while partition 0 spills
1086+ // and replays. Channel inputs return Pending after each supplied batch,
1087+ // making this interleaving independent of task scheduling.
1088+ for batch in 0 ..55 {
1089+ senders[ 1 ]
1090+ . unbounded_send ( Ok ( make_batch ( 1 , batch * 128 ) ) )
1091+ . unwrap ( ) ;
1092+ assert ! ( streams[ 1 ] . next( ) . now_or_never( ) . is_none( ) ) ;
1093+ }
1094+ let held = streams[ 1 ] . reservation . size ( ) ;
1095+ assert ! ( held > 500 * 1024 ) ;
1096+ for batch in 0 ..input_batches {
1097+ // Repeated keys cross spill runs, so replay must merge their sums.
1098+ senders[ 0 ]
1099+ . unbounded_send ( Ok ( make_batch ( 0 , batch * 128 ) ) )
1100+ . unwrap ( ) ;
1101+ assert ! ( streams[ 0 ] . next( ) . now_or_never( ) . is_none( ) ) ;
1102+ }
1103+ if limit == 600 * 1024 {
1104+ assert ! ( aggregate. metrics( ) . unwrap( ) . spill_count( ) . unwrap( ) > 0 ) ;
1105+ }
1106+ let mut first = streams. remove ( 0 ) ;
1107+ match finish {
1108+ Finish :: Collect => {
1109+ senders[ 0 ] . close_channel ( ) ;
1110+ let mut output = collect ( Box :: pin ( first) ) . await ?;
1111+ assert_eq ! ( pool. reserved( ) , held) ;
1112+ senders[ 1 ] . close_channel ( ) ;
1113+ output. extend ( collect ( Box :: pin ( streams. remove ( 0 ) ) ) . await ?) ;
1114+ let mut actual = BTreeMap :: new ( ) ;
1115+ for batch in output {
1116+ let a = batch
1117+ . column ( 0 )
1118+ . as_any ( )
1119+ . downcast_ref :: < Int64Array > ( )
1120+ . unwrap ( ) ;
1121+ let b = batch
1122+ . column ( 1 )
1123+ . as_any ( )
1124+ . downcast_ref :: < Int64Array > ( )
1125+ . unwrap ( ) ;
1126+ let sum = batch
1127+ . column ( 2 )
1128+ . as_any ( )
1129+ . downcast_ref :: < Int64Array > ( )
1130+ . unwrap ( ) ;
1131+ let min = batch
1132+ . column ( 3 )
1133+ . as_any ( )
1134+ . downcast_ref :: < StringViewArray > ( )
1135+ . unwrap ( ) ;
1136+ for row in 0 ..batch. num_rows ( ) {
1137+ assert ! (
1138+ actual
1139+ . insert(
1140+ ( a. value( row) , b. value( row) ) ,
1141+ ( sum. value( row) , min. value( row) . to_string( ) )
1142+ )
1143+ . is_none( )
1144+ ) ;
1145+ }
1146+ }
1147+ assert_eq ! ( actual, expected) ;
1148+ assert_eq ! (
1149+ aggregate. metrics( ) . unwrap( ) . spill_count( ) . unwrap( ) > 0 ,
1150+ limit == 600 * 1024
1151+ ) ;
1152+ }
1153+ Finish :: DropDuringMerge => {
1154+ senders[ 0 ] . close_channel ( ) ;
1155+ let _ = first. next ( ) . now_or_never ( ) ;
1156+ assert ! ( matches!(
1157+ first. state. as_ref( ) ,
1158+ Some ( OrderedFinalAggregateState :: MergingSpills { .. } )
1159+ ) ) ;
1160+ drop ( first) ;
1161+ }
1162+ Finish :: DropDuringReplay => {
1163+ senders[ 0 ] . close_channel ( ) ;
1164+ first. next ( ) . await . unwrap ( ) ?;
1165+ assert ! ( pool. reserved( ) > held) ;
1166+ drop ( first) ;
1167+ assert_eq ! ( pool. reserved( ) , held) ;
1168+ }
1169+ Finish :: InputError => {
1170+ senders[ 0 ]
1171+ . unbounded_send ( datafusion_common:: exec_err!(
1172+ "injected input failure"
1173+ ) )
1174+ . unwrap ( ) ;
1175+ let error = first. next ( ) . await . unwrap ( ) . unwrap_err ( ) ;
1176+ assert ! ( error. to_string( ) . contains( "injected input failure" ) ) ;
1177+ assert_eq ! ( pool. reserved( ) , held) ;
1178+ drop ( first) ;
1179+ }
1180+ }
1181+ drop ( streams) ;
1182+ assert_eq ! ( pool. reserved( ) , 0 ) ;
1183+ Ok ( ( ) )
1184+ }
1185+ }
0 commit comments