1818use std:: hint:: black_box;
1919use std:: sync:: Arc ;
2020
21- use arrow:: array:: Int64Array ;
2221use arrow:: array:: {
23- Array , ArrayRef , ArrowPrimitiveType , AsArray , ListArray , NullBufferBuilder ,
24- StringArray ,
22+ Array , ArrayRef , ArrowPrimitiveType , AsArray , Int64Array , ListArray ,
23+ NullBufferBuilder , StringArray ,
2524} ;
2625use arrow:: datatypes:: { DataType , Field , FieldRef , Int64Type , Schema } ;
27- use criterion:: { Criterion , criterion_group, criterion_main} ;
26+ use criterion:: { BatchSize , Criterion , criterion_group, criterion_main} ;
2827use datafusion_expr:: function:: AccumulatorArgs ;
2928use datafusion_expr:: { Accumulator , AggregateUDFImpl } ;
3029use datafusion_functions_aggregate:: array_agg:: {
@@ -294,8 +293,7 @@ fn distinct_array_agg_benchmark(c: &mut Criterion) {
294293}
295294
296295/// Precomputes the schema, physical expressions, sort expression, and aggregate
297- /// metadata so each benchmark iteration measures only accumulator creation,
298- /// `update_batch()`, and `evaluate()`.
296+ /// metadata so each benchmark iteration can focus on accumulator operations.
299297struct OrderedArrayAggBenchFixture {
300298 schema : Schema ,
301299 value_expr : Arc < dyn PhysicalExpr > ,
@@ -306,9 +304,9 @@ struct OrderedArrayAggBenchFixture {
306304}
307305
308306impl OrderedArrayAggBenchFixture {
309- fn new ( input_preordered : bool ) -> Self {
307+ fn new ( value_type : & DataType , input_preordered : bool ) -> Self {
310308 let schema = Schema :: new ( vec ! [
311- Field :: new( "value" , DataType :: Int64 , false ) ,
309+ Field :: new( "value" , value_type . clone ( ) , false ) ,
312310 Field :: new( "ordering" , DataType :: Int64 , false ) ,
313311 ] ) ;
314312
@@ -332,7 +330,7 @@ impl OrderedArrayAggBenchFixture {
332330
333331 let return_field = Field :: new (
334332 "array_agg" ,
335- DataType :: List ( Field :: new_list_field ( DataType :: Int64 , true ) . into ( ) ) ,
333+ DataType :: List ( Field :: new_list_field ( value_type . clone ( ) , true ) . into ( ) ) ,
336334 true ,
337335 )
338336 . into ( ) ;
@@ -398,11 +396,12 @@ fn create_ordered_array_agg_batches(
398396fn ordered_array_agg_bench (
399397 c : & mut Criterion ,
400398 name : & str ,
399+ value_type : & DataType ,
401400 batches : & [ [ ArrayRef ; 2 ] ] ,
402401 input_preordered : bool ,
403402) {
404403 c. bench_function ( name, |b| {
405- let fixture = OrderedArrayAggBenchFixture :: new ( input_preordered) ;
404+ let fixture = OrderedArrayAggBenchFixture :: new ( value_type , input_preordered) ;
406405 b. iter ( || {
407406 let mut accumulator = fixture. create_accumulator ( ) ;
408407
@@ -428,6 +427,7 @@ fn ordered_array_agg_benchmark(c: &mut Criterion) {
428427 "ordered_array_agg i64 ordered input, \
429428 {rows_per_batch} rows per update_batch"
430429 ) ,
430+ & DataType :: Int64 ,
431431 & ordered_batches,
432432 true ,
433433 ) ;
@@ -438,15 +438,83 @@ fn ordered_array_agg_benchmark(c: &mut Criterion) {
438438 "ordered_array_agg i64 random input, \
439439 {rows_per_batch} rows per update_batch"
440440 ) ,
441+ & DataType :: Int64 ,
441442 & shuffled_batches,
442443 false ,
443444 ) ;
444445 }
445446}
447+
448+ /// Creates preordered batches with a 4 KiB Utf8 payload per row.
449+ fn create_wide_utf8_batches ( rows_per_batch : usize ) -> Vec < [ ArrayRef ; 2 ] > {
450+ assert ! (
451+ rows_per_batch > 0 ,
452+ "rows_per_batch must be greater than zero"
453+ ) ;
454+
455+ let values = vec ! [ "x" . repeat( 4 * 1_024 ) ; ORDERED_ARRAY_AGG_ROWS ] ;
456+
457+ values
458+ . chunks ( rows_per_batch)
459+ . enumerate ( )
460+ . map ( |( idx, batch_values) | {
461+ let payload =
462+ Arc :: new ( StringArray :: from_iter_values ( batch_values. iter ( ) ) ) as ArrayRef ;
463+ let start = idx * rows_per_batch;
464+ let ordering_values = Arc :: new ( Int64Array :: from (
465+ ( start..start + batch_values. len ( ) )
466+ . map ( |row| i64:: try_from ( row) . unwrap ( ) )
467+ . collect :: < Vec < _ > > ( ) ,
468+ ) ) as ArrayRef ;
469+
470+ [ payload, ordering_values]
471+ } )
472+ . collect ( )
473+ }
474+
475+ fn ordered_array_agg_update_only_bench (
476+ c : & mut Criterion ,
477+ name : & str ,
478+ value_type : & DataType ,
479+ batches : & [ [ ArrayRef ; 2 ] ] ,
480+ input_preordered : bool ,
481+ ) {
482+ let fixture = OrderedArrayAggBenchFixture :: new ( value_type, input_preordered) ;
483+
484+ c. bench_function ( name, |b| {
485+ b. iter_batched (
486+ || fixture. create_accumulator ( ) ,
487+ |mut accumulator| {
488+ for batch in batches {
489+ accumulator
490+ . update_batch ( batch)
491+ . expect ( "update_batch should succeed" ) ;
492+ }
493+
494+ // Return the accumulator so Criterion drops it after timing the updates.
495+ accumulator
496+ } ,
497+ BatchSize :: PerIteration ,
498+ ) ;
499+ } ) ;
500+ }
501+
502+ fn ordered_array_agg_wide_utf8_benchmark ( c : & mut Criterion ) {
503+ let ordered_batches = create_wide_utf8_batches ( 1 ) ;
504+ ordered_array_agg_update_only_bench (
505+ c,
506+ "ordered_array_agg utf8 4 KiB, 1 row per update_batch" ,
507+ & DataType :: Utf8 ,
508+ & ordered_batches,
509+ true ,
510+ ) ;
511+ }
512+
446513criterion_group ! (
447514 benches,
448515 array_agg_benchmark,
449516 distinct_array_agg_benchmark,
450- ordered_array_agg_benchmark
517+ ordered_array_agg_benchmark,
518+ ordered_array_agg_wide_utf8_benchmark
451519) ;
452520criterion_main ! ( benches) ;
0 commit comments