Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions datafusion/functions-aggregate/src/array_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ use arrow::compute::{filter, SortOptions};
use arrow::datatypes::{DataType, Field, Fields};

use datafusion_common::cast::as_list_array;
use datafusion_common::utils::{get_row_at_idx, SingleRowListArrayBuilder};
use datafusion_common::utils::{compare_rows, get_row_at_idx, SingleRowListArrayBuilder};
use datafusion_common::{exec_err, ScalarValue};
use datafusion_common::{internal_err, Result};
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
use datafusion_expr::utils::format_state_name;
use datafusion_expr::utils::{format_state_name, AggregateOrderSensitivity};
use datafusion_expr::{Accumulator, Signature, Volatility};
use datafusion_expr::{AggregateUDFImpl, Documentation};
use datafusion_functions_aggregate_common::merge_arrays::merge_ordered_arrays;
Expand All @@ -37,7 +37,7 @@ use datafusion_macros::user_doc;
use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
use std::cmp::Ordering;
use std::collections::{HashSet, VecDeque};
use std::mem::{size_of, size_of_val};
use std::mem::{size_of, size_of_val, take};
use std::sync::Arc;

make_udaf_expr_and_func!(
Expand Down Expand Up @@ -75,12 +75,14 @@ This aggregation function can only mix DISTINCT and ORDER BY if the ordering exp
/// ARRAY_AGG aggregate expression
pub struct ArrayAgg {
signature: Signature,
is_input_pre_ordered: bool,
}

impl Default for ArrayAgg {
fn default() -> Self {
Self {
signature: Signature::any(1, Volatility::Immutable),
is_input_pre_ordered: false,
}
}
}
Expand Down Expand Up @@ -140,6 +142,20 @@ impl AggregateUDFImpl for ArrayAgg {
Ok(fields)
}

fn order_sensitivity(&self) -> AggregateOrderSensitivity {
AggregateOrderSensitivity::Beneficial
}

fn with_beneficial_ordering(
self: Arc<Self>,
beneficial_ordering: bool,
) -> Result<Option<Arc<dyn AggregateUDFImpl>>> {
Ok(Some(Arc::new(Self {
signature: self.signature.clone(),
is_input_pre_ordered: beneficial_ordering,
})))
}

fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
let data_type = acc_args.exprs[0].data_type(acc_args.schema)?;
let ignore_nulls =
Expand Down Expand Up @@ -195,6 +211,7 @@ impl AggregateUDFImpl for ArrayAgg {
&data_type,
&ordering_dtypes,
acc_args.ordering_req.clone(),
self.is_input_pre_ordered,
acc_args.is_reversed,
ignore_nulls,
)
Expand Down Expand Up @@ -506,6 +523,8 @@ pub(crate) struct OrderSensitiveArrayAggAccumulator {
datatypes: Vec<DataType>,
/// Stores the ordering requirement of the `Accumulator`.
ordering_req: LexOrdering,
/// Whether the input is known to be pre-ordered
is_input_pre_ordered: bool,
/// Whether the aggregation is running in reverse.
reverse: bool,
/// Whether the aggregation should ignore null values.
Expand All @@ -519,6 +538,7 @@ impl OrderSensitiveArrayAggAccumulator {
datatype: &DataType,
ordering_dtypes: &[DataType],
ordering_req: LexOrdering,
is_input_pre_ordered: bool,
reverse: bool,
ignore_nulls: bool,
) -> Result<Self> {
Expand All @@ -529,10 +549,33 @@ impl OrderSensitiveArrayAggAccumulator {
ordering_values: vec![],
datatypes,
ordering_req,
is_input_pre_ordered,
reverse,
ignore_nulls,
})
}

fn sort(&mut self) {
let sort_options = self
.ordering_req
.iter()
.map(|sort_expr| sort_expr.options)
.collect::<Vec<_>>();
let mut values = take(&mut self.values)
.into_iter()
.zip(take(&mut self.ordering_values))
.collect::<Vec<_>>();
let mut delayed_cmp_err = Ok(());
values.sort_by(|(_, left_ordering), (_, right_ordering)| {
compare_rows(left_ordering, right_ordering, &sort_options).unwrap_or_else(
|err| {
delayed_cmp_err = Err(err);
Ordering::Equal
},
)
});
(self.values, self.ordering_values) = values.into_iter().unzip();
}
}

impl Accumulator for OrderSensitiveArrayAggAccumulator {
Expand Down Expand Up @@ -586,6 +629,9 @@ impl Accumulator for OrderSensitiveArrayAggAccumulator {
let mut partition_ordering_values = vec![];

// Existing values should be merged also.
if !self.is_input_pre_ordered {
self.sort();
}
partition_values.push(self.values.clone().into());
partition_ordering_values.push(self.ordering_values.clone().into());

Expand Down Expand Up @@ -636,13 +682,21 @@ impl Accumulator for OrderSensitiveArrayAggAccumulator {
}

fn state(&mut self) -> Result<Vec<ScalarValue>> {
if !self.is_input_pre_ordered {
self.sort();
}

let mut result = vec![self.evaluate()?];
result.push(self.evaluate_orderings()?);

Ok(result)
}

fn evaluate(&mut self) -> Result<ScalarValue> {
if !self.is_input_pre_ordered {
self.sort();
}

if self.values.is_empty() {
return Ok(ScalarValue::new_null_list(
self.datatypes[0].clone(),
Expand Down
19 changes: 11 additions & 8 deletions datafusion/physical-plan/src/aggregates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ mod tests {
use datafusion_execution::config::SessionConfig;
use datafusion_execution::memory_pool::FairSpillPool;
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
use datafusion_functions_aggregate::array_agg::array_agg_udaf;
use datafusion_expr::test::function_stub::max_udaf;
use datafusion_functions_aggregate::average::avg_udaf;
use datafusion_functions_aggregate::count::count_udaf;
use datafusion_functions_aggregate::first_last::{first_value_udaf, last_value_udaf};
Expand Down Expand Up @@ -2333,13 +2333,16 @@ mod tests {
.into_iter()
.map(|order_by_expr| {
let ordering_req = order_by_expr.unwrap_or_default();
AggregateExprBuilder::new(array_agg_udaf(), vec![Arc::clone(col_a)])
.alias("a")
.order_by(LexOrdering::new(ordering_req.to_vec()))
.schema(Arc::clone(&test_schema))
.build()
.map(Arc::new)
.unwrap()
AggregateExprBuilder::new(
max_udaf(), // any UDAF not using Beneficial order sensitivity
vec![Arc::clone(col_a)],
)
.alias("a")
.order_by(LexOrdering::new(ordering_req.to_vec()))
.schema(Arc::clone(&test_schema))
.build()
.map(Arc::new)
.unwrap()
})
.collect::<Vec<_>>();
let group_by = PhysicalGroupBy::new_single(vec![]);
Expand Down
5 changes: 2 additions & 3 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,8 @@ physical_plan
01)AggregateExec: mode=Final, gby=[], aggr=[array_agg(agg_order.c1) ORDER BY [agg_order.c2 DESC NULLS FIRST, agg_order.c3 ASC NULLS LAST]]
02)--CoalescePartitionsExec
03)----AggregateExec: mode=Partial, gby=[], aggr=[array_agg(agg_order.c1) ORDER BY [agg_order.c2 DESC NULLS FIRST, agg_order.c3 ASC NULLS LAST]]
04)------SortExec: expr=[c2@1 DESC, c3@2 ASC NULLS LAST], preserve_partitioning=[true]
05)--------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
06)----------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/aggregate_agg_multi_order.csv]]}, projection=[c1, c2, c3], has_header=true
04)------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
05)--------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/aggregate_agg_multi_order.csv]]}, projection=[c1, c2, c3], has_header=true

# test array_agg_order with list data type
statement ok
Expand Down
Loading
Loading