alamb commented on code in PR #14302: URL: https://github.com/apache/datafusion/pull/14302#discussion_r1929738234
########## datafusion/functions-aggregate/benches/array_agg.rs: ########## @@ -0,0 +1,186 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::sync::Arc; + +use arrow::array::{Array, ArrayRef, ArrowPrimitiveType, AsArray, ListArray}; +use arrow::datatypes::Int64Type; +use arrow::util::bench_util::create_primitive_array; +use arrow_schema::Field; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use datafusion_expr::Accumulator; +use datafusion_functions_aggregate::array_agg::ArrayAggAccumulator; + +use arrow::util::test_util::seedable_rng; +use arrow_buffer::{NullBufferBuilder, OffsetBuffer}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; + +fn merge_batch_bench(c: &mut Criterion, name: &str, values: ArrayRef) { + let list_item_data_type = values.as_list::<i32>().values().data_type().clone(); + c.bench_function(name, |b| { + b.iter(|| { + #[allow(clippy::unit_arg)] + black_box( + ArrayAggAccumulator::try_new(&list_item_data_type) + .unwrap() + .merge_batch(&[values.clone()]) + .unwrap(), + ) + }) + }); +} + +/// Create List array with the given item data type, null density, null locations and non zero length lists density +/// Creates an random (but fixed-seeded) array of a given size and null density +pub fn create_list_array<T>( + size: usize, + null_density: f32, + non_zero_length_lists_probability: f32, +) -> ListArray +where + T: ArrowPrimitiveType, + Standard: Distribution<T::Native>, +{ + let mut nulls_builder = NullBufferBuilder::new(size); + let mut rng = seedable_rng(); + + let offsets = OffsetBuffer::from_lengths((0..size).map(|_| { + let is_null = rng.gen::<f32>() < null_density; + + let mut length = rng.gen_range(1..10); + + if is_null { + nulls_builder.append_null(); + + if rng.gen::<f32>() > non_zero_length_lists_probability { + length = 0; + } + } else { + nulls_builder.append_non_null(); + } + + length + })); + + let length = *offsets.last().unwrap() as usize; + + let values = create_primitive_array::<T>(length, 0.0); + + let field = Field::new_list_field(T::DATA_TYPE, true); + + ListArray::new( + Arc::new(field), + offsets, + Arc::new(values), + nulls_builder.finish(), + ) +} + +fn array_agg_benchmark(c: &mut Criterion) { + let values = Arc::new(create_list_array::<Int64Type>(8192, 0.0, 0.0)) as ArrayRef; + merge_batch_bench(c, "array_agg i64 merge_batch no nulls", values); + + let values = Arc::new(create_list_array::<Int64Type>(8192, 1.0, 0.0)) as ArrayRef; + merge_batch_bench( + c, + "array_agg i64 merge_batch all nulls, 100% of nulls point to a zero length array", + values, + ); + + let values = Arc::new(create_list_array::<Int64Type>(8192, 1.0, 0.1)) as ArrayRef; + merge_batch_bench( + c, + "array_agg i64 merge_batch all nulls, 90% of nulls point to a zero length array", Review Comment: Thanks -- that makes a lot more sense to me and I found it easier to understand. 🙏 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org