Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
70 changes: 63 additions & 7 deletions datafusion/functions-aggregate/src/array_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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;

use arrow::array::{
Expand All @@ -31,14 +31,17 @@ use arrow::datatypes::{DataType, Field, FieldRef, Fields};

use datafusion_common::cast::as_list_array;
use datafusion_common::scalar::copy_array_data;
use datafusion_common::utils::{get_row_at_idx, SingleRowListArrayBuilder};
use datafusion_common::utils::{
compare_rows, get_row_at_idx, take_function_args, SingleRowListArrayBuilder,
};
use datafusion_common::{exec_err, internal_err, Result, ScalarValue};
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
use datafusion_expr::utils::format_state_name;
use datafusion_expr::{
Accumulator, AggregateUDFImpl, Documentation, Signature, Volatility,
};
use datafusion_functions_aggregate_common::merge_arrays::merge_ordered_arrays;
use datafusion_functions_aggregate_common::order::AggregateOrderSensitivity;
use datafusion_functions_aggregate_common::utils::ordering_fields;
use datafusion_macros::user_doc;
use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
Expand Down Expand Up @@ -78,12 +81,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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this new field should trigger adding equals/hash_value implementations.
Being fixed in #17065

}

impl Default for ArrayAgg {
fn default() -> Self {
Self {
signature: Signature::any(1, Volatility::Immutable),
is_input_pre_ordered: false,
}
}
}
Expand Down Expand Up @@ -144,6 +149,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 @@ -196,6 +215,7 @@ impl AggregateUDFImpl for ArrayAgg {
&data_type,
&ordering_dtypes,
ordering,
self.is_input_pre_ordered,
acc_args.is_reversed,
ignore_nulls,
)
Expand Down Expand Up @@ -512,6 +532,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 @@ -525,6 +547,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 @@ -535,11 +558,34 @@ 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();
}

fn evaluate_orderings(&self) -> Result<ScalarValue> {
let fields = ordering_fields(&self.ordering_req, &self.datatypes[1..]);

Expand Down Expand Up @@ -610,9 +656,8 @@ impl Accumulator for OrderSensitiveArrayAggAccumulator {
// inside `ARRAY_AGG` list, we will receive an `Array` that stores values
// received from its ordering requirement expression. (This information
// is necessary for during merging).
let [array_agg_values, agg_orderings, ..] = &states else {
return exec_err!("State should have two elements");
};
let [array_agg_values, agg_orderings] =
take_function_args("OrderSensitiveArrayAggAccumulator::merge_batch", states)?;
let Some(agg_orderings) = agg_orderings.as_list_opt::<i32>() else {
return exec_err!("Expects to receive a list array");
};
Expand All @@ -623,8 +668,11 @@ impl Accumulator for OrderSensitiveArrayAggAccumulator {
let mut partition_ordering_values = vec![];

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

// Convert array to Scalars to sort them easily. Convert back to array at evaluation.
let array_agg_res = ScalarValue::convert_array_to_scalar_vec(array_agg_values)?;
Expand Down Expand Up @@ -673,13 +721,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
40 changes: 2 additions & 38 deletions datafusion/physical-expr/src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl AggregateExprBuilder {

let return_field = fun.return_field(&input_exprs_fields)?;
let is_nullable = fun.is_nullable();
// TODO rename AggregateExprBuilder::alias to name
let name = match alias {
None => {
return internal_err!(
Expand Down Expand Up @@ -575,18 +576,10 @@ impl AggregateFunctionExpr {
ReversedUDAF::NotSupported => None,
ReversedUDAF::Identical => Some(self.clone()),
ReversedUDAF::Reversed(reverse_udf) => {
let mut name = self.name().to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think removing this may have other unintended effects. I will request some more eyes on this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I agree this code was deliberate & nice. I hope we don't parse those names though.
If there is a better solution to agg reverse causing failures (#16625 (comment)), let me know. I can also drop this fix, I don't like it too.

Alternatively to the fix, I can block reversing for beneficial functions and thus hide the problem for now. Would it be preferred for this PR?

// If the function is changed, we need to reverse order_by clause as well
// i.e. First(a order by b asc null first) -> Last(a order by b desc null last)
if self.fun().name() != reverse_udf.name() {
replace_order_by_clause(&mut name);
}
replace_fn_name_clause(&mut name, self.fun.name(), reverse_udf.name());

AggregateExprBuilder::new(reverse_udf, self.args.to_vec())
.order_by(self.order_bys.iter().map(|e| e.reverse()).collect())
.schema(Arc::new(self.schema.clone()))
.alias(name)
.alias(self.name())
.with_ignore_nulls(self.ignore_nulls)
.with_distinct(self.is_distinct)
.with_reversed(!self.is_reversed)
Expand Down Expand Up @@ -684,32 +677,3 @@ impl PartialEq for AggregateFunctionExpr {
.all(|(this_arg, other_arg)| this_arg.eq(other_arg))
}
}

fn replace_order_by_clause(order_by: &mut String) {
let suffixes = [
(" DESC NULLS FIRST]", " ASC NULLS LAST]"),
(" ASC NULLS FIRST]", " DESC NULLS LAST]"),
(" DESC NULLS LAST]", " ASC NULLS FIRST]"),
(" ASC NULLS LAST]", " DESC NULLS FIRST]"),
];

if let Some(start) = order_by.find("ORDER BY [") {
if let Some(end) = order_by[start..].find(']') {
let order_by_start = start + 9;
let order_by_end = start + end;

let column_order = &order_by[order_by_start..=order_by_end];
for (suffix, replacement) in suffixes {
if column_order.ends_with(suffix) {
let new_order = column_order.replace(suffix, replacement);
order_by.replace_range(order_by_start..=order_by_end, &new_order);
break;
}
}
}
}
}

fn replace_fn_name_clause(aggr_name: &mut String, fn_name_old: &str, fn_name_new: &str) {
*aggr_name = aggr_name.replace(fn_name_old, fn_name_new);
}
Loading