Apache DataFusion SQL Query Engine
bench: first_last remove noisy benchmarks, add update_batch (#21487)
theirix2818abb8a10b
diff --git a/datafusion/functions-aggregate/benches/first_last.rs b/datafusion/functions-aggregate/benches/first_last.rsindex 6070470138..1d18e1c7dc 100644--- a/datafusion/functions-aggregate/benches/first_last.rs+++ b/datafusion/functions-aggregate/benches/first_last.rs@@ -15,22 +15,24 @@// specific language governing permissions and limitations// under the License.-use std::hint::black_box;-use std::sync::Arc;--use arrow::array::{ArrayRef, BooleanArray};+use arrow::array::{ArrayRef, BooleanArray, Int64Array};use arrow::compute::SortOptions;use arrow::datatypes::{DataType, Field, Int64Type, Schema};use arrow::util::bench_util::{create_boolean_array, create_primitive_array};+use datafusion_common::instant::Instant;+use std::hint::black_box;+use std::sync::Arc;use datafusion_expr::{Accumulator, AggregateUDFImpl, EmitTo, GroupsAccumulator, function::AccumulatorArgs,};-use datafusion_functions_aggregate::first_last::{FirstValue, LastValue};+use datafusion_functions_aggregate::first_last::{+ FirstValue, LastValue, TrivialFirstValueAccumulator, TrivialLastValueAccumulator,+};use datafusion_physical_expr::PhysicalSortExpr;use datafusion_physical_expr::expressions::col;-use criterion::{Criterion, criterion_group, criterion_main};+use criterion::{BatchSize, Criterion, criterion_group, criterion_main};fn prepare_groups_accumulator(is_first: bool) -> Box<dyn GroupsAccumulator> {let schema = Arc::new(Schema::new(vec![@@ -72,92 +74,120 @@ fn prepare_groups_accumulator(is_first: bool) -> Box<dyn GroupsAccumulator> {}}-fn prepare_accumulator(is_first: bool) -> Box<dyn Accumulator> {- let schema = Arc::new(Schema::new(vec![- Field::new("value", DataType::Int64, true),- Field::new("ord", DataType::Int64, true),- ]));-- let order_expr = col("ord", &schema).unwrap();- let sort_expr = PhysicalSortExpr {- expr: order_expr,- options: SortOptions::default(),- };-- let value_field: Arc<Field> = Field::new("value", DataType::Int64, true).into();- let accumulator_args = AccumulatorArgs {- return_field: Arc::clone(&value_field),- schema: &schema,- expr_fields: &[value_field],- ignore_nulls: false,- order_bys: std::slice::from_ref(&sort_expr),- is_reversed: false,- name: if is_first {- "FIRST_VALUE(value ORDER BY ord)"- } else {- "LAST_VALUE(value ORDER BY ord)"- },- is_distinct: false,- exprs: &[col("value", &schema).unwrap()],- };-+fn create_trivial_accumulator(+ is_first: bool,+ ignore_nulls: bool,+) -> Box<dyn Accumulator> {if is_first {- FirstValue::new().accumulator(accumulator_args).unwrap()+ Box::new(+ TrivialFirstValueAccumulator::try_new(&DataType::Int64, ignore_nulls)+ .unwrap(),+ )} else {- LastValue::new().accumulator(accumulator_args).unwrap()+ Box::new(+ TrivialLastValueAccumulator::try_new(&DataType::Int64, ignore_nulls).unwrap(),+ )}}#[expect(clippy::needless_pass_by_value)]-fn convert_to_state_bench(+#[expect(clippy::too_many_arguments)]+fn evaluate_bench(c: &mut Criterion,is_first: bool,+ emit_to: EmitTo,name: &str,values: ArrayRef,+ ord: ArrayRef,opt_filter: Option<&BooleanArray>,+ num_groups: usize,) {+ let n = values.len();+ let group_indices: Vec<usize> = (0..n).map(|i| i % num_groups).collect();+c.bench_function(name, |b| {- b.iter(|| {- let accumulator = prepare_groups_accumulator(is_first);- black_box(+ b.iter_batched(+ || {+ let mut accumulator = prepare_groups_accumulator(is_first);accumulator- .convert_to_state(std::slice::from_ref(&values), opt_filter)- .unwrap(),- )- })+ .update_batch(+ &[Arc::clone(&values), Arc::clone(&ord)],+ &group_indices,+ opt_filter,+ num_groups,+ )+ .unwrap();+ accumulator+ },+ |mut accumulator| {+ black_box(accumulator.evaluate(emit_to).unwrap());+ },+ BatchSize::SmallInput,+ )});}#[expect(clippy::needless_pass_by_value)]-fn evaluate_accumulator_bench(+fn update_bench(c: &mut Criterion,is_first: bool,name: &str,values: ArrayRef,ord: ArrayRef,+ opt_filter: Option<&BooleanArray>,+ num_groups: usize,) {+ let n = values.len();+ let group_indices: Vec<usize> = (0..n).map(|i| i % num_groups).collect();++ // Initialize with worst-case ordering so update_batch forces rows comparison for all groups.+ let worst_ord: ArrayRef = Arc::new(Int64Array::from(vec![+ if is_first {+ i64::MAX+ } else {+ i64::MIN+ };+ n+ ]));+c.bench_function(name, |b| {b.iter_batched(|| {- // setup, not timed- let mut accumulator = prepare_accumulator(is_first);+ let mut accumulator = prepare_groups_accumulator(is_first);accumulator- .update_batch(&[Arc::clone(&values), Arc::clone(&ord)])+ .update_batch(+ &[Arc::clone(&values), Arc::clone(&worst_ord)],+ &group_indices,+ None, // no filter: ensure all groups are initialised+ num_groups,+ ).unwrap();accumulator},- |mut accumulator| black_box(accumulator.evaluate().unwrap()),- criterion::BatchSize::SmallInput,+ |mut accumulator| {+ for _ in 0..100 {+ #[expect(clippy::unit_arg)]+ black_box(+ accumulator+ .update_batch(+ &[Arc::clone(&values), Arc::clone(&ord)],+ &group_indices,+ opt_filter,+ num_groups,+ )+ .unwrap(),+ );+ }+ },+ BatchSize::SmallInput,)});}#[expect(clippy::needless_pass_by_value)]-#[expect(clippy::too_many_arguments)]-fn evaluate_bench(+fn merge_bench(c: &mut Criterion,is_first: bool,- emit_to: EmitTo,name: &str,values: ArrayRef,ord: ArrayRef,@@ -166,15 +196,26 @@ fn evaluate_bench() {let n = values.len();let group_indices: Vec<usize> = (0..n).map(|i| i % num_groups).collect();+ let is_set: ArrayRef = Arc::new(BooleanArray::from(vec![true; n]));++ // Initialize with worst-case ordering so update_batch forces rows comparison for all groups.+ let worst_ord: ArrayRef = Arc::new(Int64Array::from(vec![+ if is_first {+ i64::MAX+ } else {+ i64::MIN+ };+ n+ ]));c.bench_function(name, |b| {b.iter_batched(|| {- // setup, not timed+ // Prebuild accumulatorlet mut accumulator = prepare_groups_accumulator(is_first);accumulator.update_batch(- &[Arc::clone(&values), Arc::clone(&ord)],+ &[Arc::clone(&values), Arc::clone(&worst_ord)],&group_indices,opt_filter,num_groups,@@ -182,12 +223,54 @@ fn evaluate_bench(.unwrap();accumulator},- |mut accumulator| black_box(accumulator.evaluate(emit_to).unwrap()),- criterion::BatchSize::SmallInput,+ |mut accumulator| {+ for _ in 0..100 {+ #[expect(clippy::unit_arg)]+ black_box(+ accumulator+ .merge_batch(+ &[+ Arc::clone(&values),+ Arc::clone(&ord),+ Arc::clone(&is_set),+ ],+ &group_indices,+ opt_filter,+ num_groups,+ )+ .unwrap(),+ );+ }+ },+ BatchSize::SmallInput,)});}+#[expect(clippy::needless_pass_by_value)]+fn trivial_update_bench(+ c: &mut Criterion,+ is_first: bool,+ ignore_nulls: bool,+ name: &str,+ values: ArrayRef,+) {+ c.bench_function(name, |b| {+ b.iter_custom(|iters| {+ // The bench is way too fast, so apply scaling factor+ let mut accumulators: Vec<Box<dyn Accumulator>> = (0..iters * 100)+ .map(|_| create_trivial_accumulator(is_first, ignore_nulls))+ .collect();+ let start = Instant::now();+ for acc in &mut accumulators {+ #[expect(clippy::unit_arg)]+ black_box(acc.update_batch(&[Arc::clone(&values)]).unwrap());+ }+ start.elapsed()+ })+ });+}+fn first_last_benchmark(c: &mut Criterion) {const N: usize = 65536;const NUM_GROUPS: usize = 1024;@@ -208,27 +291,10 @@ fn first_last_benchmark(c: &mut Criterion) {let ord = Arc::new(create_primitive_array::<Int64Type>(N, null_density))as ArrayRef;- evaluate_accumulator_bench(- c,- is_first,- &format!("{fn_name} evaluate_accumulator_bench nulls={pct}%"),- values.clone(),- ord.clone(),- );-for with_filter in [false, true] {let filter = create_boolean_array(N, 0.0, 0.5);let opt_filter = if with_filter { Some(&filter) } else { None };- convert_to_state_bench(- c,- is_first,- &format!(- "{fn_name} convert_to_state nulls={pct}%, filter={with_filter}"- ),- values.clone(),- opt_filter,- );evaluate_bench(c,is_first,@@ -253,6 +319,37 @@ fn first_last_benchmark(c: &mut Criterion) {opt_filter,NUM_GROUPS,);++ update_bench(+ c,+ is_first,+ &format!("{fn_name} update_bench nulls={pct}%, filter={with_filter}"),+ values.clone(),+ ord.clone(),+ opt_filter,+ NUM_GROUPS,+ );+ merge_bench(+ c,+ is_first,+ &format!("{fn_name} merge_bench nulls={pct}%, filter={with_filter}"),+ values.clone(),+ ord.clone(),+ opt_filter,+ NUM_GROUPS,+ );+ }++ for ignore_nulls in [false, true] {+ trivial_update_bench(+ c,+ is_first,+ ignore_nulls,+ &format!(+ "{fn_name} trivial_update_bench nulls={pct}%, ignore_nulls={ignore_nulls}"+ ),+ values.clone(),+ );}}}