@@ -125,14 +125,19 @@ struct Scratch {
125125 /// appearance in the batch
126126 groups_with_rows : Vec < usize > ,
127127
128- /// `offsets[i]` is the index into the reordered rows where the rows for
128+ /// `offsets[i]` is the index into `batch_indices` where the rows for
129129 /// `groups_with_rows[i]` start
130130 offsets : Vec < usize > ,
131+
132+ /// Indices into the batch rows, with the rows of each group contiguous
133+ batch_indices : Vec < u32 > ,
131134}
132135
133136impl Scratch {
134137 fn allocated_size ( & self ) -> usize {
135- self . groups_with_rows . allocated_size ( ) + self . offsets . allocated_size ( )
138+ self . groups_with_rows . allocated_size ( )
139+ + self . offsets . allocated_size ( )
140+ + self . batch_indices . allocated_size ( )
136141 }
137142}
138143
@@ -290,6 +295,7 @@ impl GroupsAccumulatorAdapter {
290295 let Scratch {
291296 groups_with_rows,
292297 offsets,
298+ batch_indices,
293299 } = scratch;
294300
295301 // groups_with_rows holds a list of group indexes that have any rows
@@ -320,7 +326,14 @@ impl GroupsAccumulatorAdapter {
320326 self . add_allocation ( indices_allocation_delta) ;
321327
322328 // batch_indices holds indices into values, each group is contiguous
323- let mut batch_indices = Vec :: with_capacity ( group_indices. len ( ) ) ;
329+ batch_indices. clear ( ) ;
330+ batch_indices
331+ . try_reserve ( group_indices. len ( ) )
332+ . map_err ( |e| {
333+ arrow_datafusion_err ! ( arrow:: error:: ArrowError :: MemoryError (
334+ e. to_string( )
335+ ) )
336+ } ) ?;
324337
325338 // offsets[i] is index into batch_indices where the rows for
326339 // groups_with_rows[i] start
@@ -334,13 +347,20 @@ impl GroupsAccumulatorAdapter {
334347 offset_so_far += indices. len ( ) ;
335348 offsets. push ( offset_so_far) ;
336349 }
337- let batch_indices = batch_indices. into ( ) ;
350+ // Move the buffer into an array without a copy, for the take kernels
351+ let indices_array =
352+ PrimitiveArray :: < UInt32Type > :: from ( std:: mem:: take ( batch_indices) ) ;
338353
339354 // reorder the values and opt_filter by batch_indices so that
340355 // all values for each group are contiguous, then invoke the
341356 // accumulator once per group with values
342- let values = take_arrays ( values, & batch_indices, None ) ?;
343- let opt_filter = get_filter_at_indices ( opt_filter, & batch_indices) ?;
357+ let values = take_arrays ( values, & indices_array, None ) ?;
358+ let opt_filter = get_filter_at_indices ( opt_filter, & indices_array) ?;
359+
360+ // The take kernels only borrow the indices, so the array holds the
361+ // only reference to the buffer and we can get it back without a copy
362+ let ( _, indices_buffer, _) = indices_array. into_parts ( ) ;
363+ * batch_indices = indices_buffer. into_inner ( ) . into_vec ( ) . unwrap_or_default ( ) ;
344364
345365 let grouped_update_metric = self . grouped_metric ( ) ;
346366
@@ -888,6 +908,8 @@ mod tests {
888908 let retained_indices = adapter. states [ 0 ] . indices . allocated_size ( ) ;
889909 assert ! ( retained_indices > 0 ) ;
890910 assert ! ( adapter. states[ 0 ] . indices. is_empty( ) ) ;
911+ // The indices buffer comes back from the take kernels without a copy
912+ assert ! ( adapter. scratch. batch_indices. capacity( ) >= 4 ) ;
891913 let retained_scratch = adapter. scratch . allocated_size ( ) ;
892914 assert ! ( retained_scratch > 0 ) ;
893915 assert_eq ! (
0 commit comments