@@ -32,6 +32,7 @@ use super::{
3232 DisplayAs , ExecutionPlanProperties , RecordBatchStream , SendableRecordBatchStream ,
3333} ;
3434use crate :: aggregates:: {
35+ PartitionRun , append_subpartition_column, partition_runs_from_column,
3536 relative_partition, relative_partition_schema, with_relative_partition,
3637} ;
3738use crate :: coalesce:: LimitedBatchCoalescer ;
@@ -347,20 +348,21 @@ impl PartitionedLimitedBatchCoalescer {
347348 }
348349
349350 fn push_batch ( & mut self , batch : RecordBatch ) -> Result < ( ) > {
350- let relative_partition =
351- relative_partition ( batch. schema_ref ( ) ) ?. ok_or_else ( || {
352- DataFusionError :: Internal (
353- "Missing relative partition metadata for partitioned batch coalescing"
354- . to_string ( ) ,
355- )
356- } ) ?;
351+ let ( batch, runs) = partition_runs_from_column ( batch) ?;
357352 let num_relative_partitions = self . coalescers . len ( ) ;
358- let coalescer = self . coalescers . get_mut ( relative_partition) . ok_or_else ( || {
359- DataFusionError :: Internal ( format ! (
360- "Relative partition {relative_partition} exceeds configured partition count {num_relative_partitions}"
361- ) )
362- } ) ?;
363- coalescer. push_batch ( batch) ?;
353+ let mut offset = 0 ;
354+ for run in runs {
355+ let relative_partition = run. relative_partition ;
356+ let coalescer = self . coalescers . get_mut ( relative_partition) . ok_or_else ( || {
357+ DataFusionError :: Internal ( format ! (
358+ "Relative partition {relative_partition} exceeds configured partition count {num_relative_partitions}"
359+ ) )
360+ } ) ?;
361+ let run_batch =
362+ with_relative_partition ( batch. slice ( offset, run. len ) , relative_partition) ;
363+ coalescer. push_batch ( run_batch) ?;
364+ offset += run. len ;
365+ }
364366 Ok ( ( ) )
365367 }
366368
@@ -1113,12 +1115,89 @@ impl BatchPartitioner {
11131115
11141116 partition_timer. done ( ) ;
11151117
1116- Self :: partition_grouped_take ( batch, indices, timer, |aggr_partition| {
1117- (
1118+ Self :: partition_hash_aggregate_grouped_take (
1119+ batch,
1120+ indices,
1121+ timer,
1122+ max_aggr_partition_factor,
1123+ )
1124+ }
1125+
1126+ fn partition_hash_aggregate_grouped_take (
1127+ batch : & RecordBatch ,
1128+ indices : & mut [ Vec < u32 > ] ,
1129+ timer : & metrics:: Time ,
1130+ max_aggr_partition_factor : usize ,
1131+ ) -> Result < Vec < Result < ( usize , usize , RecordBatch ) > > > {
1132+ let mut output_ranges = Vec :: with_capacity ( indices. len ( ) ) ;
1133+ let mut reordered_indices = Vec :: with_capacity ( batch. num_rows ( ) ) ;
1134+
1135+ for ( aggr_partition, p_indices) in indices. iter_mut ( ) . enumerate ( ) {
1136+ if p_indices. is_empty ( ) {
1137+ continue ;
1138+ }
1139+
1140+ let start = reordered_indices. len ( ) ;
1141+ reordered_indices. extend_from_slice ( p_indices) ;
1142+ output_ranges. push ( (
11181143 aggr_partition / max_aggr_partition_factor,
1119- aggr_partition % max_aggr_partition_factor,
1120- )
1121- } )
1144+ PartitionRun :: new (
1145+ aggr_partition % max_aggr_partition_factor,
1146+ p_indices. len ( ) ,
1147+ ) ?,
1148+ start,
1149+ ) ) ;
1150+ p_indices. clear ( ) ;
1151+ }
1152+
1153+ if reordered_indices. is_empty ( ) {
1154+ return Ok ( vec ! [ ] ) ;
1155+ }
1156+
1157+ let batches = {
1158+ let _timer = timer. timer ( ) ;
1159+ let indices_array: PrimitiveArray < UInt32Type > = reordered_indices. into ( ) ;
1160+ let columns = take_arrays ( batch. columns ( ) , & indices_array, None ) ?;
1161+
1162+ let mut options = RecordBatchOptions :: new ( ) ;
1163+ options = options. with_row_count ( Some ( indices_array. len ( ) ) ) ;
1164+ let reordered_batch =
1165+ RecordBatch :: try_new_with_options ( batch. schema ( ) , columns, & options) ?;
1166+
1167+ let mut output_batches = Vec :: new ( ) ;
1168+ let mut current_output_partition = None ;
1169+ let mut current_start = 0 ;
1170+ let mut current_len = 0 ;
1171+ let mut current_runs = Vec :: new ( ) ;
1172+
1173+ for ( output_partition, run, start) in output_ranges {
1174+ if current_output_partition != Some ( output_partition) {
1175+ if let Some ( partition) = current_output_partition {
1176+ let batch = reordered_batch. slice ( current_start, current_len) ;
1177+ let batch = append_subpartition_column ( & batch, & current_runs) ?;
1178+ output_batches. push ( Ok ( ( partition, 0 , batch) ) ) ;
1179+ }
1180+
1181+ current_output_partition = Some ( output_partition) ;
1182+ current_start = start;
1183+ current_len = 0 ;
1184+ current_runs. clear ( ) ;
1185+ }
1186+
1187+ current_len += run. len ;
1188+ current_runs. push ( run) ;
1189+ }
1190+
1191+ if let Some ( partition) = current_output_partition {
1192+ let batch = reordered_batch. slice ( current_start, current_len) ;
1193+ let batch = append_subpartition_column ( & batch, & current_runs) ?;
1194+ output_batches. push ( Ok ( ( partition, 0 , batch) ) ) ;
1195+ }
1196+
1197+ output_batches
1198+ } ;
1199+
1200+ Ok ( batches)
11221201 }
11231202
11241203 fn partition_grouped_take (
@@ -2023,12 +2102,7 @@ impl RepartitionExec {
20232102 }
20242103
20252104 for res in partitioner. partition_iter_with_relative_partition ( batch) ? {
2026- let ( partition, relative_partition, batch) = res?;
2027- let batch = if max_aggr_partition_factor > 1 {
2028- with_relative_partition ( batch, relative_partition)
2029- } else {
2030- batch
2031- } ;
2105+ let ( partition, _relative_partition, batch) = res?;
20322106
20332107 let timer = metrics. send_time [ partition] . timer ( ) ;
20342108 // if there is still a receiver, send to it
@@ -2382,18 +2456,20 @@ mod tests {
23822456 fn partitioned_coalescer_keeps_relative_partitions_separate ( ) -> Result < ( ) > {
23832457 let schema =
23842458 Arc :: new ( Schema :: new ( vec ! [ Field :: new( "c0" , DataType :: UInt32 , false ) ] ) ) ;
2385- let make_batch = |relative_partition , values : Vec < u32 > | {
2459+ let make_batch = |values : Vec < u32 > , runs : & [ PartitionRun ] | {
23862460 let batch = RecordBatch :: try_new (
23872461 Arc :: clone ( & schema) ,
23882462 vec ! [ Arc :: new( UInt32Array :: from( values) ) ] ,
23892463 ) ?;
2390- Ok :: < _ , DataFusionError > ( with_relative_partition ( batch, relative_partition ) )
2464+ append_subpartition_column ( & batch, runs )
23912465 } ;
23922466 let mut coalescer = PartitionedLimitedBatchCoalescer :: new ( & schema, 4 , 2 ) ;
23932467
2394- coalescer. push_batch ( make_batch ( 0 , vec ! [ 0 , 1 ] ) ?) ?;
2395- coalescer. push_batch ( make_batch ( 1 , vec ! [ 10 , 11 ] ) ?) ?;
2396- coalescer. push_batch ( make_batch ( 0 , vec ! [ 2 , 3 ] ) ?) ?;
2468+ coalescer. push_batch ( make_batch (
2469+ vec ! [ 0 , 1 , 10 , 11 ] ,
2470+ & [ PartitionRun :: new ( 0 , 2 ) ?, PartitionRun :: new ( 1 , 2 ) ?] ,
2471+ ) ?) ?;
2472+ coalescer. push_batch ( make_batch ( vec ! [ 2 , 3 ] , & [ PartitionRun :: new ( 0 , 2 ) ?] ) ?) ?;
23972473
23982474 let partition_zero = coalescer. next_completed_batch ( ) . unwrap ( ) ;
23992475 assert_eq ! ( partition_zero. num_rows( ) , 4 ) ;
@@ -2409,10 +2485,9 @@ mod tests {
24092485 }
24102486
24112487 // Covers hash aggregate partitioning mapping multiple aggregate partitions
2412- // back to one output partition while returning each relative partition as
2413- // its own batch.
2488+ // back to one output partition through an internal subpartition column.
24142489 #[ test]
2415- fn test_hash_aggregate_partitioner_returns_relative_partitions ( ) -> Result < ( ) > {
2490+ fn test_hash_aggregate_partitioner_appends_subpartition_column ( ) -> Result < ( ) > {
24162491 let schema =
24172492 Arc :: new ( Schema :: new ( vec ! [ Field :: new( "c0" , DataType :: UInt32 , false ) ] ) ) ;
24182493 let batch = RecordBatch :: try_new (
@@ -2432,10 +2507,21 @@ mod tests {
24322507 for result in partitioner. partition_iter_with_relative_partition ( batch) ? {
24332508 let ( partition, relative_partition, batch) = result?;
24342509 assert ! ( partition < 2 ) ;
2435- assert ! ( relative_partition < 4 ) ;
2436- assert_eq ! ( batch. num_columns( ) , 1 ) ;
2510+ assert_eq ! ( relative_partition, 0 ) ;
2511+ assert_eq ! ( batch. num_columns( ) , 2 ) ;
24372512 num_rows += batch. num_rows ( ) ;
2438- seen[ partition] [ relative_partition] = true ;
2513+
2514+ let subpartitions = batch
2515+ . column ( 1 )
2516+ . as_any ( )
2517+ . downcast_ref :: < UInt32Array > ( )
2518+ . expect ( "subpartition column should be UInt32" ) ;
2519+ for value in subpartitions. iter ( ) {
2520+ let value =
2521+ value. expect ( "subpartition value should not be null" ) as usize ;
2522+ assert ! ( value < 4 ) ;
2523+ seen[ partition] [ value] = true ;
2524+ }
24392525 }
24402526
24412527 assert_eq ! ( num_rows, 128 ) ;
0 commit comments