UBarney commented on code in PR #21383:
URL: https://github.com/apache/datafusion/pull/21383#discussion_r3037731579
##########
datafusion/functions-aggregate/src/first_last.rs:
##########
@@ -831,6 +830,8 @@ pub struct FirstValueAccumulator {
orderings: Vec<ScalarValue>,
// Stores the applicable ordering requirement.
ordering_req: LexOrdering,
+ // derived from `ordering_req`.
+ sort_options: Vec<SortOptions>,
Review Comment:
Since these changes are for performance optimization, should we add some
benchmarks for `FirstValueAccumulator` to quantify the improvement and prevent
future regressions?
##########
datafusion/functions-aggregate/benches/first_last.rs:
##########
@@ -0,0 +1,194 @@
+// 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::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, BooleanArray};
+use arrow::compute::SortOptions;
+use arrow::datatypes::{DataType, Field, Int64Type, Schema};
+use arrow::util::bench_util::{create_boolean_array, create_primitive_array};
+
+use datafusion_expr::{
+ AggregateUDFImpl, EmitTo, GroupsAccumulator, function::AccumulatorArgs,
+};
+use datafusion_functions_aggregate::first_last::{FirstValue, LastValue};
+use datafusion_physical_expr::PhysicalSortExpr;
+use datafusion_physical_expr::expressions::col;
+
+use criterion::{Criterion, criterion_group, criterion_main};
+
+fn prepare_groups_accumulator(is_first: bool) -> Box<dyn GroupsAccumulator> {
+ 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()],
+ };
+
+ if is_first {
+ FirstValue::new()
+ .create_groups_accumulator(accumulator_args)
+ .unwrap()
+ } else {
+ LastValue::new()
+ .create_groups_accumulator(accumulator_args)
+ .unwrap()
+ }
+}
+
+#[expect(clippy::needless_pass_by_value)]
+fn convert_to_state_bench(
+ c: &mut Criterion,
+ is_first: bool,
+ name: &str,
+ values: ArrayRef,
+ opt_filter: Option<&BooleanArray>,
+) {
+ c.bench_function(name, |b| {
+ b.iter(|| {
+ let accumulator = prepare_groups_accumulator(is_first);
+ black_box(
+ accumulator
+ .convert_to_state(std::slice::from_ref(&values),
opt_filter)
Review Comment:
Why are we benchmarking `convert_to_state` here? Since its implementation
hasn't changed in this PR, I'm curious about the motivation.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]