Skip to content

Apache DataFusion SQL Query Engine

perf: Faster grouped `first_value` / `last_value` with a single primitive ORDER BY key (#27303)

Pierre Lacave76287b2b6cbf
diff --git a/datafusion/functions-aggregate/src/first_last.rs b/datafusion/functions-aggregate/src/first_last.rs
index 8b5b34dea2..eaf0533204 100644
--- a/datafusion/functions-aggregate/src/first_last.rs
+++ b/datafusion/functions-aggregate/src/first_last.rs
@@ -9,18 +9,21 @@ use std::hash::Hash;
use std::mem::size_of_val;
use std::sync::Arc;
-use arrow::array::{Array, ArrayRef, AsArray, BooleanArray, BooleanBufferBuilder};
-use arrow::buffer::BooleanBuffer;
+use arrow::array::{
+ Array, ArrayRef, ArrowPrimitiveType, AsArray, BooleanArray, BooleanBufferBuilder,
+ PrimitiveArray, downcast_primitive,
+};
+use arrow::buffer::{BooleanBuffer, NullBuffer};
use arrow::compute::{self, LexicographicalComparator, SortColumn, SortOptions};
use arrow::datatypes::{
- DataType, Date32Type, Date64Type, Decimal32Type, Decimal64Type, Decimal128Type,
- Decimal256Type, Field, FieldRef, Float16Type, Float32Type, Float64Type, Int8Type,
- Int16Type, Int32Type, Int64Type, Time32MillisecondType, Time32SecondType,
- Time64MicrosecondType, Time64NanosecondType, TimeUnit, TimestampMicrosecondType,
- TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType, UInt8Type,
- UInt16Type, UInt32Type, UInt64Type,
+ ArrowNativeTypeOp, DataType, Date32Type, Date64Type, Decimal32Type, Decimal64Type,
+ Decimal128Type, Decimal256Type, Field, FieldRef, Float16Type, Float32Type,
+ Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, Time32MillisecondType,
+ Time32SecondType, Time64MicrosecondType, Time64NanosecondType, TimeUnit,
+ TimestampMicrosecondType, TimestampMillisecondType, TimestampNanosecondType,
+ TimestampSecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
};
-use datafusion_common::cast::as_boolean_array;
+use datafusion_common::cast::{as_boolean_array, as_primitive_array};
use datafusion_common::utils::{compare_rows, extract_row_at_idx_to_buf, get_row_at_idx};
use datafusion_common::{
DataFusionError, Result, ScalarValue, arrow_datafusion_err, internal_err,
@@ -34,6 +37,9 @@ use datafusion_expr::{
GroupsAccumulator, ReversedUDAF, Signature, SortExpr, Volatility,
udaf_return_field_preserving_arg_metadata,
};
+use datafusion_functions_aggregate_common::aggregate::groups_accumulator::nulls::{
+ filter_to_nulls, filtered_null_mask,
+};
use datafusion_functions_aggregate_common::utils::get_sort_options;
use datafusion_macros::user_doc;
use datafusion_physical_expr_common::sort_expr::LexOrdering;
@@ -86,6 +92,26 @@ fn create_groups_accumulator_helper<S: ValueState + 'static>(
.map(|e| e.expr.data_type(args.schema))
.collect::<Result<Vec<_>>>()?;
+ // A single ORDER BY expression of primitive type gets typed orderings
+ if let ([sort_expr], [ordering_dtype]) = (&*ordering, ordering_dtypes.as_slice()) {
+ macro_rules! primitive_key_accumulator {
+ ($t:ty) => {
+ return Ok(Box::new(FirstLastPrimitiveKeyGroupsAccumulator::<$t>::new(
+ Box::new(state),
+ sort_expr.options,
+ ordering_dtype.clone(),
+ args.ignore_nulls,
+ is_first,
+ is_input_pre_ordered,
+ )))
+ };
+ }
+ downcast_primitive! {
+ ordering_dtype => (primitive_key_accumulator),
+ _ => {}
+ }
+ }
+
Ok(Box::new(FirstLastGroupsAccumulator::try_new(
state,
ordering,
@@ -900,12 +926,289 @@ impl<S: ValueState + 'static> GroupsAccumulator for FirstLastGroupsAccumulator<S
values: &[ArrayRef],
opt_filter: Option<&BooleanArray>,
) -> Result<Vec<ArrayRef>> {
- let filter = opt_filter
- .cloned()
- .unwrap_or_else(|| BooleanArray::from(vec![true; values[0].len()]));
- let mut result = values.to_vec();
- result.push(Arc::new(filter));
- Ok(result)
+ Ok(ordered_convert_to_state(values, opt_filter))
+ }
+}
+
+/// State `[value, orderings.., is_set]` with one row per input row, where
+/// `is_set` is the FILTER clause, including its nulls.
+fn ordered_convert_to_state(
+ values: &[ArrayRef],
+ opt_filter: Option<&BooleanArray>,
+) -> Vec<ArrayRef> {
+ let filter = opt_filter
+ .cloned()
+ .unwrap_or_else(|| BooleanArray::from(vec![true; values[0].len()]));
+ let mut result = values.to_vec();
+ result.push(Arc::new(filter));
+ result
+}
+
+/// [`FirstLastGroupsAccumulator`] for a single ORDER BY expression of primitive
+/// type `K`, which keeps the orderings as `K::Native` and compares them directly.
+///
+/// It picks the same rows: strict comparisons keep the first of tied rows, and
+/// pre-ordered input keeps the first (FIRST_VALUE) or last (LAST_VALUE) row.
+struct FirstLastPrimitiveKeyGroupsAccumulator<K: ArrowPrimitiveType> {
+ /// Value of each group's winner
+ state: Box<dyn ValueState>,
+ /// Ordering of each group's winner, null where `ordering_nulls` is unset
+ orderings: Vec<K::Native>,
+ ordering_nulls: BooleanBufferBuilder,
+ /// Whether each group has a winner
+ is_sets: BooleanBufferBuilder,
+ ordering_dtype: DataType,
+ /// Sort options under which the winner sorts first: the ORDER BY options
+ /// for FIRST_VALUE, reversed for LAST_VALUE
+ winner_options: SortOptions,
+ pick_first_in_group: bool,
+ ignore_nulls: bool,
+ is_input_pre_ordered: bool,
+ /// `(group_idx, row)` of the latest winning row of each group the current
+ /// batch updated
+ updates: Vec<(usize, usize)>,
+ /// Groups with an entry in `updates`, all-false between batches
+ seen: BooleanBufferBuilder,
+ /// Position in `updates` of each group in `seen`
+ update_pos: Vec<usize>,
+}
+
+impl<K: ArrowPrimitiveType> FirstLastPrimitiveKeyGroupsAccumulator<K> {
+ fn new(
+ state: Box<dyn ValueState>,
+ options: SortOptions,
+ ordering_dtype: DataType,
+ ignore_nulls: bool,
+ pick_first_in_group: bool,
+ is_input_pre_ordered: bool,
+ ) -> Self {
+ Self {
+ state,
+ orderings: Vec::new(),
+ ordering_nulls: BooleanBufferBuilder::new(0),
+ is_sets: BooleanBufferBuilder::new(0),
+ ordering_dtype,
+ winner_options: if pick_first_in_group {
+ options
+ } else {
+ !options
+ },
+ pick_first_in_group,
+ ignore_nulls,
+ is_input_pre_ordered,
+ updates: Vec::new(),
+ seen: BooleanBufferBuilder::new(0),
+ update_pos: Vec::new(),
+ }
+ }
+
+ fn resize(&mut self, total_num_groups: usize) {
+ self.state.resize(total_num_groups);
+ self.orderings.resize(total_num_groups, K::default_value());
+ self.ordering_nulls.resize(total_num_groups);
+ self.is_sets.resize(total_num_groups);
+ self.seen.resize(total_num_groups);
+ self.update_pos.resize(total_num_groups, 0);
+ }
+
+ /// Rows that pass `filter` and, with IGNORE NULLS, have a non-null value;
+ /// `None` when all rows do.
+ fn qualifying_rows(
+ &self,
+ filter: Option<&BooleanArray>,
+ vals: &ArrayRef,
+ ) -> Option<NullBuffer> {
+ let rows = if self.ignore_nulls {
+ filtered_null_mask(filter, vals.as_ref())
+ } else {
+ filter.map(filter_to_nulls)
+ };
+ rows.filter(|rows| rows.null_count() > 0)
+ }
+
+ /// Whether `ordering` (`None` if null) sorts strictly before the ordering
+ /// of the winner of group `group_idx`, which must be set.
+ #[inline]
+ fn sorts_before_winner(&self, ordering: Option<K::Native>, group_idx: usize) -> bool {
+ let winner = self
+ .ordering_nulls
+ .get_bit(group_idx)
+ .then(|| self.orderings[group_idx]);
+ match (ordering, winner) {
+ (Some(ordering), Some(winner)) => {
+ let cmp = ordering.compare(winner);
+ if self.winner_options.descending {
+ cmp.is_gt()
+ } else {
+ cmp.is_lt()
+ }
+ }
+ (None, Some(_)) => self.winner_options.nulls_first,
+ (Some(_), None) => !self.winner_options.nulls_first,
+ (None, None) => false,
+ }
+ }
+
+ /// Makes each of `rows` the winner of its group if it beats the current
+ /// winner, and records it in `updates`, once per group. Without `compare`,
+ /// FIRST_VALUE keeps the first row of each group and LAST_VALUE takes every
+ /// row.
+ fn find_winners(
+ &mut self,
+ orderings: &PrimitiveArray<K>,
+ group_indices: &[usize],
+ rows: impl Iterator<Item = usize>,
+ compare: bool,
+ updates: &mut Vec<(usize, usize)>,
+ ) {
+ for row in rows {
+ let group_idx = group_indices[row];
+ let ordering = orderings.is_valid(row).then(|| orderings.value(row));
+ let is_winner = !self.is_sets.get_bit(group_idx)
+ || if compare {
+ self.sorts_before_winner(ordering, group_idx)
+ } else {
+ !self.pick_first_in_group
+ };
+ if is_winner {
+ self.is_sets.set_bit(group_idx, true);
+ self.orderings[group_idx] = ordering.unwrap_or_default();
+ self.ordering_nulls.set_bit(group_idx, ordering.is_some());
+ if self.seen.get_bit(group_idx) {
+ updates[self.update_pos[group_idx]].1 = row;
+ } else {
+ self.seen.set_bit(group_idx, true);
+ self.update_pos[group_idx] = updates.len();
+ updates.push((group_idx, row));
+ }
+ }
+ }
+ }
+
+ /// Updates the winners with the `rows` of a batch (all rows if `None`).
+ fn update_winners(
+ &mut self,
+ vals: &ArrayRef,
+ orderings: &ArrayRef,
+ group_indices: &[usize],
+ rows: Option<NullBuffer>,
+ compare: bool,
+ ) -> Result<()> {
+ let orderings = as_primitive_array::<K>(orderings)?;
+ // Push to a local: pushing through `self` makes the loop reload its fields.
+ let mut updates = std::mem::take(&mut self.updates);
+ // Sized once per batch: at most one update per group the batch touches
+ let max_updates = group_indices.len().min(self.is_sets.len());
+ updates.try_reserve(max_updates).map_err(|e| {
+ arrow_datafusion_err!(arrow::error::ArrowError::MemoryError(e.to_string()))
+ })?;
+ match rows {
+ Some(rows) => self.find_winners(
+ orderings,
+ group_indices,
+ rows.valid_indices(),
+ compare,
+ &mut updates,
+ ),
+ None => {
+ let rows = 0..group_indices.len();
+ self.find_winners(orderings, group_indices, rows, compare, &mut updates)
+ }
+ }
+ for &(group_idx, _) in updates.iter() {
+ self.seen.set_bit(group_idx, false);
+ }
+ let result = self.state.update_rows(vals, &updates);
+ updates.clear();
+ self.updates = updates;
+ result
+ }
+
+ fn take(&mut self, emit_to: EmitTo) -> Result<(ArrayRef, ArrayRef, BooleanBuffer)> {
+ let vals = self.state.take(emit_to)?;
+ let orderings = PrimitiveArray::<K>::new(
+ emit_to.take_needed(&mut self.orderings).into(),
+ Some(NullBuffer::new(state::take_need(
+ &mut self.ordering_nulls,
+ emit_to,
+ ))),
+ )
+ .with_data_type(self.ordering_dtype.clone());
+ let is_sets = state::take_need(&mut self.is_sets, emit_to);
+ Ok((vals, Arc::new(orderings), is_sets))
+ }
+}
+
+impl<K: ArrowPrimitiveType> GroupsAccumulator
+ for FirstLastPrimitiveKeyGroupsAccumulator<K>
+{
+ fn update_batch(
+ &mut self,
+ values: &[ArrayRef],
+ group_indices: &[usize],
+ opt_filter: Option<&BooleanArray>,
+ total_num_groups: usize,
+ ) -> Result<()> {
+ let [vals, orderings] = values else {
+ return internal_err!("Expected a value and an ordering column");
+ };
+ self.resize(total_num_groups);
+ let rows = self.qualifying_rows(opt_filter, vals);
+ let compare = !self.is_input_pre_ordered;
+ self.update_winners(vals, orderings, group_indices, rows, compare)
+ }
+
+ fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+ Ok(self.take(emit_to)?.0)
+ }
+
+ fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
+ let (vals, orderings, is_sets) = self.take(emit_to)?;
+ Ok(vec![
+ vals,
+ orderings,
+ Arc::new(BooleanArray::new(is_sets, None)),
+ ])
+ }
+
+ fn merge_batch(
+ &mut self,
+ values: &[ArrayRef],
+ group_indices: &[usize],
+ total_num_groups: usize,
+ ) -> Result<()> {
+ let [vals, orderings, is_set] = values else {
+ return internal_err!("Expected value, ordering and is_set state columns");
+ };
+ self.resize(total_num_groups);
+ let rows = self.qualifying_rows(Some(as_boolean_array(is_set)?), vals);
+ self.update_winners(vals, orderings, group_indices, rows, true)
+ }
+
+ fn convert_to_state(
+ &self,
+ values: &[ArrayRef],
+ opt_filter: Option<&BooleanArray>,
+ ) -> Result<Vec<ArrayRef>> {
+ Ok(ordered_convert_to_state(values, opt_filter))
+ }
+
+ fn size(&self) -> usize {
+ self.state.size()
+ + self.orderings.capacity() * size_of::<K::Native>()
+ + (self.ordering_nulls.capacity()
+ + self.is_sets.capacity()
+ + self.seen.capacity())
+ / 8
+ + self.updates.capacity() * size_of::<(usize, usize)>()
+ + self.update_pos.capacity() * size_of::<usize>()
+ }
+
+ fn shrink_to_fit(&mut self) {
+ // `resize` sizes `seen` and `update_pos` again before the next batch
+ self.updates = Vec::new();
+ self.seen = BooleanBufferBuilder::new(0);
+ self.update_pos = Vec::new();
}
}
@@ -3401,4 +3704,126 @@ mod tests {
Ok(())
}
+
+ /// A single primitive ORDER BY expression reaches the typed accumulator,
+ /// other orderings the generic one.
+ #[test]
+ fn primitive_key_routing() -> Result<()> {
+ let ts = DataType::Timestamp(TimeUnit::Nanosecond, Some("+05:00".into()));
+ let schema = Schema::new(vec![
+ Field::new("v", DataType::Utf8, true),
+ Field::new("k", ts, true),
+ Field::new("s", DataType::Utf8, true),
+ ]);
+ let exprs = [col("v", &schema)?];
+ let expr_fields = [Arc::new(schema.field(0).clone())];
+ let is_typed = |udaf: &dyn AggregateUDFImpl, order_by: &[&str]| -> Result<bool> {
+ let order_bys = order_by
+ .iter()
+ .map(|name| Ok(PhysicalSortExpr::new_default(col(name, &schema)?)))
+ .collect::<Result<Vec<_>>>()?;
+ let args = || AccumulatorArgs {
+ return_field: Arc::clone(&expr_fields[0]),
+ schema: &schema,
+ expr_fields: &expr_fields,
+ ignore_nulls: false,
+ order_bys: &order_bys,
+ is_reversed: false,
+ name: "acc",
+ is_distinct: false,
+ exprs: &exprs,
+ };
+ assert!(udaf.groups_accumulator_supported(args()));
+ let acc = udaf.create_groups_accumulator(args())?;
+ let acc: &dyn std::any::Any = acc.as_ref();
+ let typed = acc
+ .is::<FirstLastPrimitiveKeyGroupsAccumulator<TimestampNanosecondType>>();
+ assert_ne!(
+ typed,
+ acc.is::<FirstLastGroupsAccumulator<BytesValueState>>()
+ );
+ Ok(typed)
+ };
+
+ assert!(is_typed(&FirstValue::new(), &["k"])?);
+ assert!(is_typed(&LastValue::new(), &["k"])?);
+ assert!(!is_typed(&FirstValue::new(), &["k", "s"])?);
+ assert!(!is_typed(&LastValue::new(), &["s"])?);
+ Ok(())
+ }
+
+ /// On tied keys the typed accumulator picks the same rows as the generic one,
+ /// within a batch, across batches and when merging.
+ #[test]
+ fn primitive_key_ties_match_generic() -> Result<()> {
+ let schema = Schema::new(vec![Field::new("k", DataType::Int64, true)]);
+ let sort = PhysicalSortExpr::new_default(col("k", &schema)?);
+ let ordering = LexOrdering::new(vec![sort]).unwrap();
+ let accs = |pick_first, pre_ordered| -> Result<[Box<dyn GroupsAccumulator>; 2]> {
+ let state = || PrimitiveValueState::<Int64Type>::new(DataType::Int64);
+ Ok([
+ Box::new(FirstLastGroupsAccumulator::try_new(
+ state(),
+ ordering.clone(),
+ false,
+ &[DataType::Int64],
+ pick_first,
+ pre_ordered,
+ )?),
+ Box::new(FirstLastPrimitiveKeyGroupsAccumulator::<Int64Type>::new(
+ Box::new(state()),
+ SortOptions::default(),
+ DataType::Int64,
+ false,
+ pick_first,
+ pre_ordered,
+ )),
+ ])
+ };
+ // Two groups whose rows all have key 7, over two batches.
+ let batch = |vals: Vec<i64>| -> Vec<ArrayRef> {
+ let keys = Int64Array::from(vec![7; vals.len()]);
+ vec![Arc::new(Int64Array::from(vals)), Arc::new(keys)]
+ };
+ let batches: [(Vec<ArrayRef>, &[usize]); 2] = [
+ (batch(vec![1, 2, 3, 4]), &[0, 1, 0, 1]),
+ (batch(vec![5, 6]), &[0, 1]),
+ ];
+
+ for pick_first in [true, false] {
+ for pre_ordered in [false, true] {
+ // Comparisons are strict, so the first row seen wins; pre-ordered
+ // LAST_VALUE takes the last row.
+ let expected = if pre_ordered && !pick_first {
+ vec![Some(5), Some(6)]
+ } else {
+ vec![Some(1), Some(2)]
+ };
+ for mut acc in accs(pick_first, pre_ordered)? {
+ for (cols, groups) in &batches {
+ acc.update_batch(cols, groups, None, 2)?;
+ }
+ assert_eq!(int64_values(&acc.evaluate(EmitTo::All)?), expected);
+ }
+
+ // Merging one state per batch keeps the first state's rows.
+ for mut acc in accs(pick_first, pre_ordered)? {
+ for (cols, groups) in &batches {
+ let [mut partial, _] = accs(pick_first, pre_ordered)?;
+ partial.update_batch(cols, groups, None, 2)?;
+ acc.merge_batch(&partial.state(EmitTo::All)?, &[0, 1], 2)?;
+ }
+ let first_batch_rows = match (pre_ordered, pick_first) {
+ (true, false) => vec![Some(3), Some(4)],
+ _ => vec![Some(1), Some(2)],
+ };
+ assert_eq!(
+ int64_values(&acc.evaluate(EmitTo::All)?),
+ first_batch_rows
+ );
+ }
+ }
+ }
+ Ok(())
+ }
}
diff --git a/datafusion/functions-aggregate/src/first_last/state.rs b/datafusion/functions-aggregate/src/first_last/state.rs
index 52d752525e..c4bda2c1be 100644
--- a/datafusion/functions-aggregate/src/first_last/state.rs
+++ b/datafusion/functions-aggregate/src/first_last/state.rs
@@ -35,6 +35,16 @@ pub(crate) trait ValueState: Send + Sync {
/// group by t.id1, t.v1;
/// ```
fn update(&mut self, group_idx: usize, array: &ArrayRef, idx: usize) -> Result<()>;
+ /// Calls [`Self::update`] for each `(group_idx, idx)` in order.
+ fn update_rows(
+ &mut self,
+ array: &ArrayRef,
+ updates: &[(usize, usize)],
+ ) -> Result<()> {
+ updates
+ .iter()
+ .try_for_each(|&(group_idx, idx)| self.update(group_idx, array, idx))
+ }
/// Takes the accumulated state and returns it as an [`ArrayRef`], respecting the `emit_to` strategy.
fn take(&mut self, emit_to: EmitTo) -> Result<ArrayRef>;
/// Returns the estimated memory size of the state in bytes.
@@ -71,6 +81,19 @@ impl<T: ArrowPrimitiveType + Send> ValueState for PrimitiveValueState<T> {
Ok(())
}
+ fn update_rows(
+ &mut self,
+ array: &ArrayRef,
+ updates: &[(usize, usize)],
+ ) -> Result<()> {
+ let array = array.as_primitive::<T>();
+ for &(group_idx, idx) in updates {
+ self.vals[group_idx] = array.value(idx);
+ self.nulls.set_bit(group_idx, array.is_valid(idx));
+ }
+ Ok(())
+ }
+
fn take(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
let values = emit_to.take_needed(&mut self.vals);
let null_buf = NullBuffer::new(take_need(&mut self.nulls, emit_to));
diff --git a/datafusion/physical-plan/src/aggregates/clustered_partial_stream.rs b/datafusion/physical-plan/src/aggregates/clustered_partial_stream.rs
index 29a2e25a34..f96250e1b2 100644
--- a/datafusion/physical-plan/src/aggregates/clustered_partial_stream.rs
+++ b/datafusion/physical-plan/src/aggregates/clustered_partial_stream.rs
@@ -606,9 +606,10 @@ mod tests {
}
/// Like [`completed_groups_release_accumulator_scratch`], with the native
- /// `FIRST_VALUE(v ORDER BY v)` accumulator. It keeps a bitmap of the
- /// groups each batch updated, which grows to one bit per group (16 KB for
- /// 131072 groups), so it must release that bitmap too.
+ /// `FIRST_VALUE` accumulators: `ORDER BY v` runs the single primitive key
+ /// one, `ORDER BY v, k` the generic one. Each keeps per-group scratch for
+ /// the groups a batch updated (16 KB for 131072 groups), so it must
+ /// release it too.
///
/// Skipped under `force_hash_collisions`, where the 131,072 colliding group
/// keys make the aggregate quadratic and the test take many minutes.
@@ -618,18 +619,26 @@ mod tests {
use crate::aggregates::clustered_test_util::aggregate_expr_over_sorted_input;
use datafusion_functions_aggregate::first_last::first_value_udaf;
- let plan = aggregate_expr_over_sorted_input(
- AggregateMode::Partial,
- &["k"],
- vec![vec![(0..131072).collect()]],
- first_value_udaf(),
- |builder, schema| {
- Ok(builder
- .order_by(vec![PhysicalSortExpr::new_default(col("v", schema)?)]))
- },
- )?;
- assert_eq!(plan.group_clustering_mode(), &GroupClusteringMode::Full);
- assert_output_unchanged_under_limit(&plan, 16 * 1024).await
+ for order_by in [&["v"][..], &["v", "k"]] {
+ let plan = aggregate_expr_over_sorted_input(
+ AggregateMode::Partial,
+ &["k"],
+ vec![vec![(0..131072).collect()]],
+ first_value_udaf(),
+ |builder, schema| {
+ let order_by = order_by
+ .iter()
+ .map(|name| Ok(PhysicalSortExpr::new_default(col(name, schema)?)))
+ .collect::<Result<Vec<_>>>()?;
+ Ok(builder.order_by(order_by))
+ },
+ )?;
+ assert_eq!(plan.group_clustering_mode(), &GroupClusteringMode::Full);
+ assert_output_unchanged_under_limit(&plan, 16 * 1024)
+ .await
+ .map_err(|e| e.context(format!("ORDER BY {order_by:?}")))?;
+ }
+ Ok(())
}
/// With partial group clustering, the stream emits every state under
diff --git a/datafusion/sqllogictest/test_files/first_last_primitive_key.slt b/datafusion/sqllogictest/test_files/first_last_primitive_key.slt
new file mode 100644
index 0000000000..328c3eb12e
--- /dev/null
+++ b/datafusion/sqllogictest/test_files/first_last_primitive_key.slt
@@ -0,0 +1,185 @@
+# SPDX-License-Identifier: Apache-2.0
+# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
+# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
+
+# Grouped first_value / last_value take a typed accumulator for a single
+# primitive ORDER BY column, and the generic one for more columns. Each query
+# checks that `ORDER BY k` returns the same rows as `ORDER BY k, u`: `u` is
+# unique and no group has two rows with the same `k`, so `u` never decides.
+
+# Small batches spread each group over several batches and partitions
+statement ok
+set datafusion.execution.batch_size = 16;
+
+# Group `g` has 1 + g % 8 rows. Its j-th row has `p = j * 3 % 8`, unique in
+# the group, and every key `k_*` increases with `p`. In every third group, `p`
+# and the keys are NULL in the first row. Values are NULL in every fifth row.
+# Groups 0 to 210 arrive interleaved, the others one after another.
+statement ok
+CREATE TABLE src AS
+SELECT
+ g, u, p,
+ arrow_cast(p * 30 - 100, 'Int8') AS k_i8,
+ arrow_cast(p, 'UInt64') * arrow_cast(2600000000000000000, 'UInt64') AS k_u64,
+ arrow_cast(f, 'Float16') AS k_f16,
+ f AS k_f64,
+ arrow_cast(p * 25 - 87, 'Decimal128(10, 2)') AS k_dec128,
+ arrow_cast(p * 25 - 87, 'Decimal256(40, 2)') AS k_dec256,
+ arrow_cast(p * 1000000000000 - 3000000000000, 'Timestamp(Nanosecond, Some("+05:00"))') AS k_ts,
+ arrow_cast(arrow_cast(p - 3, 'Utf8') || ' days', 'Interval(MonthDayNano)') AS k_interval,
+ CASE WHEN u % 5 = 2 THEN NULL ELSE u END AS v_i64,
+ CASE WHEN u % 5 = 2 THEN NULL ELSE 'v' || u END AS v_str,
+ CASE WHEN u % 5 = 2 THEN NULL ELSE named_struct('u', u) END AS v_struct
+FROM (
+ SELECT g, u, p,
+ CASE p
+ WHEN 0 THEN -CAST('NaN' AS DOUBLE)
+ WHEN 1 THEN -CAST('inf' AS DOUBLE)
+ WHEN 2 THEN -1.5
+ WHEN 3 THEN -0.0
+ WHEN 4 THEN 0.0
+ WHEN 5 THEN 1.5
+ WHEN 6 THEN CAST('inf' AS DOUBLE)
+ WHEN 7 THEN CAST('NaN' AS DOUBLE)
+ END AS f
+ FROM (
+ SELECT g, u, CASE WHEN j = 0 AND g % 3 = 1 THEN NULL ELSE j * 3 % 8 END AS p
+ FROM (
+ SELECT value AS u,
+ CASE WHEN value < 1688 THEN value % 211 ELSE 211 + (value - 1688) / 8 END AS g,
+ CASE WHEN value < 1688 THEN value / 211 ELSE (value - 1688) % 8 END AS j
+ FROM range(3376)
+ )
+ WHERE j <= g % 8
+ )
+);
+
+query III
+SELECT count(*), count(DISTINCT g), count(p) FROM src;
+----
+1893 422 1752
+
+# Group 215 has the floats -NaN, -inf, -1.5, -0.0, 0.0, 1.5, inf and NaN, in
+# that order. Group 7 has a NULL key, which sorts last.
+query IIIII
+SELECT g, first_value(p ORDER BY k_f64), last_value(p ORDER BY k_f64),
+ first_value(p ORDER BY k_f64) FILTER (WHERE p >= 3),
+ last_value(p ORDER BY k_f64) FILTER (WHERE p <= 4)
+FROM src WHERE g IN (7, 215) GROUP BY g ORDER BY g;
+----
+7 1 NULL 3 4
+215 0 7 3 4
+
+# Key types with their own comparison or data type
+query I
+SELECT count(*) FROM (
+ SELECT g,
+ first_value(u ORDER BY k_i8), last_value(u ORDER BY k_i8),
+ first_value(u ORDER BY k_u64), last_value(u ORDER BY k_u64),
+ first_value(u ORDER BY k_f16), last_value(u ORDER BY k_f16),
+ first_value(u ORDER BY k_f64), last_value(u ORDER BY k_f64),
+ first_value(u ORDER BY k_dec128), last_value(u ORDER BY k_dec128),
+ first_value(u ORDER BY k_dec256), last_value(u ORDER BY k_dec256),
+ first_value(u ORDER BY k_ts), last_value(u ORDER BY k_ts),
+ first_value(u ORDER BY k_interval), last_value(u ORDER BY k_interval)
+ FROM src GROUP BY g
+ EXCEPT
+ SELECT g,
+ first_value(u ORDER BY k_i8, u), last_value(u ORDER BY k_i8, u),
+ first_value(u ORDER BY k_u64, u), last_value(u ORDER BY k_u64, u),
+ first_value(u ORDER BY k_f16, u), last_value(u ORDER BY k_f16, u),
+ first_value(u ORDER BY k_f64, u), last_value(u ORDER BY k_f64, u),
+ first_value(u ORDER BY k_dec128, u), last_value(u ORDER BY k_dec128, u),
+ first_value(u ORDER BY k_dec256, u), last_value(u ORDER BY k_dec256, u),
+ first_value(u ORDER BY k_ts, u), last_value(u ORDER BY k_ts, u),
+ first_value(u ORDER BY k_interval, u), last_value(u ORDER BY k_interval, u)
+ FROM src GROUP BY g
+);
+----
+0
+
+# Every ASC / DESC and NULLS FIRST / LAST combination
+query I
+SELECT count(*) FROM (
+ SELECT g,
+ first_value(u ORDER BY k_f64 ASC NULLS FIRST),
+ first_value(u ORDER BY k_f64 ASC NULLS LAST),
+ first_value(u ORDER BY k_f64 DESC NULLS FIRST),
+ first_value(u ORDER BY k_f64 DESC NULLS LAST),
+ last_value(u ORDER BY k_f64 ASC NULLS FIRST),
+ last_value(u ORDER BY k_f64 ASC NULLS LAST),
+ last_value(u ORDER BY k_f64 DESC NULLS FIRST),
+ last_value(u ORDER BY k_f64 DESC NULLS LAST)
+ FROM src GROUP BY g
+ EXCEPT
+ SELECT g,
+ first_value(u ORDER BY k_f64 ASC NULLS FIRST, u),
+ first_value(u ORDER BY k_f64 ASC NULLS LAST, u),
+ first_value(u ORDER BY k_f64 DESC NULLS FIRST, u),
+ first_value(u ORDER BY k_f64 DESC NULLS LAST, u),
+ last_value(u ORDER BY k_f64 ASC NULLS FIRST, u),
+ last_value(u ORDER BY k_f64 ASC NULLS LAST, u),
+ last_value(u ORDER BY k_f64 DESC NULLS FIRST, u),
+ last_value(u ORDER BY k_f64 DESC NULLS LAST, u)
+ FROM src GROUP BY g
+);
+----
+0
+
+# Primitive, string and struct values, with NULL values, IGNORE NULLS and a
+# FILTER that is NULL for some rows
+statement ok
+CREATE VIEW value_diff AS
+SELECT count(*) AS mismatches FROM (
+ SELECT g,
+ first_value(v_i64 ORDER BY k_i8), last_value(v_i64 ORDER BY k_i8) IGNORE NULLS,
+ first_value(v_str ORDER BY k_i8 DESC) IGNORE NULLS, last_value(v_str ORDER BY k_i8),
+ first_value(v_struct ORDER BY k_i8) IGNORE NULLS,
+ last_value(v_struct ORDER BY k_i8 NULLS FIRST),
+ first_value(v_i64 ORDER BY k_i8) FILTER (WHERE nullif(u % 7, 3) > 1),
+ last_value(v_str ORDER BY k_i8) FILTER (WHERE nullif(u % 7, 3) > 1),
+ first_value(v_struct ORDER BY k_i8 DESC) FILTER (WHERE nullif(u % 7, 3) > 1)
+ FROM src GROUP BY g
+ EXCEPT
+ SELECT g,
+ first_value(v_i64 ORDER BY k_i8, u), last_value(v_i64 ORDER BY k_i8, u) IGNORE NULLS,
+ first_value(v_str ORDER BY k_i8 DESC, u) IGNORE NULLS, last_value(v_str ORDER BY k_i8, u),
+ first_value(v_struct ORDER BY k_i8, u) IGNORE NULLS,
+ last_value(v_struct ORDER BY k_i8 NULLS FIRST, u),
+ first_value(v_i64 ORDER BY k_i8, u) FILTER (WHERE nullif(u % 7, 3) > 1),
+ last_value(v_str ORDER BY k_i8, u) FILTER (WHERE nullif(u % 7, 3) > 1),
+ first_value(v_struct ORDER BY k_i8 DESC, u) FILTER (WHERE nullif(u % 7, 3) > 1)
+ FROM src GROUP BY g
+);
+
+query I
+SELECT mismatches FROM value_diff;
+----
+0
+
+# Again with partial aggregation skipped, which merges `convert_to_state` rows
+statement ok
+set datafusion.execution.skip_partial_aggregation_probe_rows_threshold = 0;
+
+statement ok
+set datafusion.execution.skip_partial_aggregation_probe_ratio_threshold = 0.0;
+
+query I
+SELECT mismatches FROM value_diff;
+----
+0
+
+statement ok
+reset datafusion.execution.skip_partial_aggregation_probe_ratio_threshold;
+
+statement ok
+reset datafusion.execution.skip_partial_aggregation_probe_rows_threshold;
+
+statement ok
+reset datafusion.execution.batch_size;
+
+statement ok
+DROP VIEW value_diff;
+
+statement ok
+DROP TABLE src;