Apache DataFusion SQL Query Engine
perf: avoid processing discarded rows for negative `nth_value` (#24453)
linfeng5fd5d51c6553
diff --git a/datafusion/functions-aggregate/Cargo.toml b/datafusion/functions-aggregate/Cargo.tomlindex 5abea16e2c..ae6bf70088 100644--- a/datafusion/functions-aggregate/Cargo.toml+++ b/datafusion/functions-aggregate/Cargo.toml@@ -92,6 +92,10 @@ harness = falsename = "median"harness = false+[[bench]]+name = "nth_value"+harness = false+[[bench]]name = "percentile_cont"harness = falsediff --git a/datafusion/functions-aggregate/benches/nth_value.rs b/datafusion/functions-aggregate/benches/nth_value.rsnew file mode 100644index 0000000000..a0cff5397b--- /dev/null+++ b/datafusion/functions-aggregate/benches/nth_value.rs@@ -0,0 +1,102 @@+// 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, Int64Array};+use arrow::compute::SortOptions;+use arrow::datatypes::DataType;+use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main};+use datafusion_expr::Accumulator;+use datafusion_functions_aggregate::nth_value::{+ NthValueAccumulator, TrivialNthValueAccumulator,+};+use datafusion_physical_expr::expressions::Column;+use datafusion_physical_expr::{LexOrdering, PhysicalSortExpr};++const N_VALUES: [i64; 6] = [-100, -10, -1, 1, 10, 100];+const BATCH_LEN: usize = 8192;+const UPDATES_PER_ITER: usize = 10;++fn int64_array(values: impl IntoIterator<Item = i64>) -> ArrayRef {+ Arc::new(Int64Array::from_iter_values(values))+}++fn trivial_accumulator(n: i64) -> TrivialNthValueAccumulator {+ TrivialNthValueAccumulator::try_new(n, &DataType::Int64).unwrap()+}++fn ordered_accumulator(n: i64) -> NthValueAccumulator {+ let ordering = LexOrdering::new(vec![PhysicalSortExpr::new(+ Arc::new(Column::new("ordering", 0)),+ SortOptions::default(),+ )])+ .unwrap();+ NthValueAccumulator::try_new(n, &DataType::Int64, &[DataType::Int64], ordering)+ .unwrap()+}++fn nth_value_benchmark(c: &mut Criterion) {+ let values = int64_array(0..BATCH_LEN as i64);+ let mut group = c.benchmark_group("nth_value");++ for n in N_VALUES {+ let n_values = int64_array(std::iter::repeat_n(n, BATCH_LEN));+ let trivial_batch = vec![Arc::clone(&values), Arc::clone(&n_values)];+ group.bench_function(BenchmarkId::new("trivial", format!("n={n}")), |b| {+ b.iter_batched(+ || trivial_accumulator(n),+ |mut accumulator| {+ for _ in 0..UPDATES_PER_ITER {+ accumulator.update_batch(&trivial_batch).unwrap();+ }+ black_box(accumulator.evaluate().unwrap());+ },+ BatchSize::SmallInput,+ )+ });++ let ordered_batches = (0..UPDATES_PER_ITER)+ .map(|batch_idx| {+ let start = (batch_idx * BATCH_LEN) as i64;+ vec![+ Arc::clone(&values),+ Arc::clone(&n_values),+ int64_array(start..start + BATCH_LEN as i64),+ ]+ })+ .collect::<Vec<_>>();+ group.bench_function(BenchmarkId::new("ordered", format!("n={n}")), |b| {+ b.iter_batched(+ || ordered_accumulator(n),+ |mut accumulator| {+ for batch in &ordered_batches {+ accumulator.update_batch(batch).unwrap();+ }+ black_box(accumulator.evaluate().unwrap());+ },+ BatchSize::SmallInput,+ )+ });+ }++ group.finish();+}++criterion_group!(benches, nth_value_benchmark);+criterion_main!(benches);diff --git a/datafusion/functions-aggregate/src/nth_value.rs b/datafusion/functions-aggregate/src/nth_value.rsindex bddc46e27e..32d9d3057c 100644--- a/datafusion/functions-aggregate/src/nth_value.rs+++ b/datafusion/functions-aggregate/src/nth_value.rs@@ -20,6 +20,7 @@use std::collections::VecDeque;use std::mem::{size_of, size_of_val};+use std::ops::Range;use std::sync::Arc;use arrow::array::{ArrayRef, AsArray, StructArray, new_empty_array};@@ -214,20 +215,13 @@ impl TrivialNthValueAccumulator {})}- /// Updates state, with the `values`. Fetch contains missing number of entries for state to be complete- /// None represents all of the new `values` need to be added to the state.+ /// Updates state with rows in `range` from `values`.fn append_new_data(&mut self,values: &[ArrayRef],- fetch: Option<usize>,+ range: Range<usize>,) -> Result<()> {- let n_row = values[0].len();- let n_to_add = if let Some(fetch) = fetch {- std::cmp::min(fetch, n_row)- } else {- n_row- };- for index in 0..n_to_add {+ for index in range {let mut row = get_row_at_idx(values, index)?;self.values.push_back(row.swap_remove(0));// At index 1, we have n index argument, which is constant.@@ -246,14 +240,18 @@ impl Accumulator for TrivialNthValueAccumulator {if from_start {// direction is from startlet n_remaining = n_required.saturating_sub(self.values.len());- self.append_new_data(values, Some(n_remaining))?;+ let batch_len = values[0].len();+ self.append_new_data(values, 0..n_remaining.min(batch_len))?;} else {// direction is from end- self.append_new_data(values, None)?;- let start_offset = self.values.len().saturating_sub(n_required);+ let batch_len = values[0].len();+ let new_count = n_required.min(batch_len);+ let retain_count = n_required - new_count;+ let start_offset = self.values.len().saturating_sub(retain_count);if start_offset > 0 {self.values.drain(0..start_offset);}+ self.append_new_data(values, batch_len - new_count..batch_len)?;}}Ok(())@@ -387,20 +385,13 @@ impl NthValueAccumulator {))}- /// Updates state, with the `values`. Fetch contains missing number of entries for state to be complete- /// None represents all of the new `values` need to be added to the state.+ /// Updates state with rows in `range` from `values`.fn append_new_data(&mut self,values: &[ArrayRef],- fetch: Option<usize>,+ range: Range<usize>,) -> Result<()> {- let n_row = values[0].len();- let n_to_add = if let Some(fetch) = fetch {- std::cmp::min(fetch, n_row)- } else {- n_row- };- for index in 0..n_to_add {+ for index in range {let row = get_row_at_idx(values, index)?;self.values.push_back(row[0].clone());// At index 1, we have n index argument.@@ -424,15 +415,19 @@ impl Accumulator for NthValueAccumulator {if from_start {// direction is from startlet n_remaining = n_required.saturating_sub(self.values.len());- self.append_new_data(values, Some(n_remaining))?;+ let batch_len = values[0].len();+ self.append_new_data(values, 0..n_remaining.min(batch_len))?;} else {// direction is from end- self.append_new_data(values, None)?;- let start_offset = self.values.len().saturating_sub(n_required);+ let batch_len = values[0].len();+ let new_count = n_required.min(batch_len);+ let retain_count = n_required - new_count;+ let start_offset = self.values.len().saturating_sub(retain_count);if start_offset > 0 {self.values.drain(0..start_offset);self.ordering_values.drain(0..start_offset);}+ self.append_new_data(values, batch_len - new_count..batch_len)?;}Ok(())diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.sltindex 460d4cd2ff..70f080aed5 100644--- a/datafusion/sqllogictest/test_files/aggregate.slt+++ b/datafusion/sqllogictest/test_files/aggregate.slt@@ -7237,6 +7237,44 @@ from aggregate_test_100;statement okset datafusion.execution.batch_size = 8192;+# Test negative nth_value update_batch() with batches smaller than, equal to,+# and larger than N for both order-insensitive and order-sensitive accumulators.+statement ok+set datafusion.execution.target_partitions = 1;++statement ok+set datafusion.execution.batch_size = 1;++query II+select nth_value(i, -3), nth_value(i, -3 order by i)+from generate_series(1, 4) t(i);+----+2 2++statement ok+set datafusion.execution.batch_size = 3;++query II+select nth_value(i, -3), nth_value(i, -3 order by i)+from generate_series(1, 6) t(i);+----+4 4++statement ok+set datafusion.execution.batch_size = 4;++query II+select nth_value(i, -3), nth_value(i, -3 order by i)+from generate_series(1, 8) t(i);+----+6 6++statement ok+set datafusion.execution.batch_size = 8192;++statement ok+set datafusion.execution.target_partitions = 4;+# regr_*() testing retract_batch() from RegrAccumulator's internal implementationquery RRIRRRRRR