Skip to content

Commit f2c9d5b

Browse files
committed
bench(functions-aggregate): add coalesce-peers first_value comparison
Adds coalesce_comparison_bench to the first_last benchmark: N independent primitive first_value accumulators (the pre-rewrite plan) vs one struct-valued accumulator carrying the same N columns (the post-rewrite plan produced by the CoalesceFirstLast optimizer rule). The struct path uses the native nested GroupsAccumulator from apache#23628. Runs as 'first_value coalesce_peers(i64,utf8,f64) separate x3' vs '... coalesced struct' so the head-to-head win is visible in the same run. Part of apache#23600.
1 parent 5f0ba13 commit f2c9d5b

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

‎datafusion/functions-aggregate/benches/first_last.rs‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,103 @@ fn create_list_of_struct_array(n: usize, list_len: usize, null_density: f32) ->
452452
))
453453
}
454454

455+
/// Head-to-head for the coalesce-peers rewrite: N independent primitive
456+
/// `first_value` accumulators (the pre-rewrite plan) vs one struct-valued
457+
/// accumulator carrying the same N columns (the post-rewrite plan). Uses the
458+
/// same worst-case ordering as [`update_bench`] so every row forces an
459+
/// ordering comparison in every accumulator.
460+
#[expect(clippy::needless_pass_by_value)]
461+
fn coalesce_comparison_bench(
462+
c: &mut Criterion,
463+
name: &str,
464+
column_values: Vec<ArrayRef>,
465+
struct_values: ArrayRef,
466+
ord: ArrayRef,
467+
num_groups: usize,
468+
) {
469+
let n = ord.len();
470+
let group_indices: Vec<usize> = (0..n).map(|i| i % num_groups).collect();
471+
let worst_ord: ArrayRef = Arc::new(Int64Array::from(vec![i64::MAX; n]));
472+
473+
// Pre-rewrite: one accumulator per column.
474+
c.bench_function(&format!("{name} separate x{}", column_values.len()), |b| {
475+
b.iter_batched(
476+
|| {
477+
column_values
478+
.iter()
479+
.map(|values| {
480+
let mut acc = prepare_typed_groups_accumulator(
481+
true,
482+
values.data_type().clone(),
483+
);
484+
acc.update_batch(
485+
&[Arc::clone(values), Arc::clone(&worst_ord)],
486+
&group_indices,
487+
None,
488+
num_groups,
489+
)
490+
.unwrap();
491+
acc
492+
})
493+
.collect::<Vec<_>>()
494+
},
495+
|mut accumulators| {
496+
for _ in 0..100 {
497+
for (acc, values) in accumulators.iter_mut().zip(&column_values) {
498+
#[expect(clippy::unit_arg)]
499+
black_box(
500+
acc.update_batch(
501+
&[Arc::clone(values), Arc::clone(&ord)],
502+
&group_indices,
503+
None,
504+
num_groups,
505+
)
506+
.unwrap(),
507+
);
508+
}
509+
}
510+
},
511+
BatchSize::SmallInput,
512+
)
513+
});
514+
515+
// Post-rewrite: a single struct-valued accumulator.
516+
c.bench_function(&format!("{name} coalesced struct"), |b| {
517+
b.iter_batched(
518+
|| {
519+
let mut acc = prepare_typed_groups_accumulator(
520+
true,
521+
struct_values.data_type().clone(),
522+
);
523+
acc.update_batch(
524+
&[Arc::clone(&struct_values), Arc::clone(&worst_ord)],
525+
&group_indices,
526+
None,
527+
num_groups,
528+
)
529+
.unwrap();
530+
acc
531+
},
532+
|mut accumulator| {
533+
for _ in 0..100 {
534+
#[expect(clippy::unit_arg)]
535+
black_box(
536+
accumulator
537+
.update_batch(
538+
&[Arc::clone(&struct_values), Arc::clone(&ord)],
539+
&group_indices,
540+
None,
541+
num_groups,
542+
)
543+
.unwrap(),
544+
);
545+
}
546+
},
547+
BatchSize::SmallInput,
548+
)
549+
});
550+
}
551+
455552
fn first_last_nested_benchmark(c: &mut Criterion) {
456553
const N: usize = 65536;
457554
const NUM_GROUPS: usize = 1024;
@@ -510,6 +607,19 @@ fn first_last_nested_benchmark(c: &mut Criterion) {
510607
);
511608
}
512609
}
610+
// Coalesce-peers head-to-head on null-free columns.
611+
let a = Arc::new(create_primitive_array::<Int64Type>(N, 0.0)) as ArrayRef;
612+
let b = Arc::new(create_string_array_with_len::<i32>(N, 0.0, 16)) as ArrayRef;
613+
let d = Arc::new(create_primitive_array::<Float64Type>(N, 0.0)) as ArrayRef;
614+
let struct_values = create_struct_array(N, 0.0);
615+
coalesce_comparison_bench(
616+
c,
617+
"first_value coalesce_peers(i64,utf8,f64)",
618+
vec![a, b, d],
619+
struct_values,
620+
ord,
621+
NUM_GROUPS,
622+
);
513623
}
514624

515625
fn first_last_benchmark(c: &mut Criterion) {

0 commit comments

Comments
 (0)