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
4 changes: 4 additions & 0 deletions datafusion/ffi/src/udaf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ impl AggregateUDFImpl for ForeignAggregateUDF {
pub enum FFI_AggregateOrderSensitivity {
Insensitive,
HardRequirement,
SoftRequirement,
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically speaking this is an FFI API change -- I am not sure what implication that has (note this would not be released until DataFusion 50 anyways).

cc @timsaucer -- I wonder if we should gather up the FFI breaking changes into their own PR / more carefully schedule such breakages

Copy link
Member Author

Choose a reason for hiding this comment

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

This new thing doesn't need to be supported in the FFI.
However, i didn't know how to avoid adding this.
When looking at impl From<AggregateOrderSensitivity> for FFI_AggregateOrderSensitivity i am under impression that this particular part of FFI API is tightly coupled with the datafusion core, so in this particular place it cannot deliver API stability without inhibiting datafusion core progress. The necessary solution might be replacing this From with TryFrom, same with (impl From<FFI_AggregateOrderSensitivity> for AggregateOrderSensitivity).

My understanding is that, by tightly coupling AggregateOrderSensitivity and FFI_AggregateOrderSensitivity code author chose to let these enums naturally evolve over time, considering this not a breaking change, or an acceptable breaking change.

Beneficial,
}

Expand All @@ -569,6 +570,7 @@ impl From<FFI_AggregateOrderSensitivity> for AggregateOrderSensitivity {
match value {
FFI_AggregateOrderSensitivity::Insensitive => Self::Insensitive,
FFI_AggregateOrderSensitivity::HardRequirement => Self::HardRequirement,
FFI_AggregateOrderSensitivity::SoftRequirement => Self::SoftRequirement,
FFI_AggregateOrderSensitivity::Beneficial => Self::Beneficial,
}
}
Expand All @@ -579,6 +581,7 @@ impl From<AggregateOrderSensitivity> for FFI_AggregateOrderSensitivity {
match value {
AggregateOrderSensitivity::Insensitive => Self::Insensitive,
AggregateOrderSensitivity::HardRequirement => Self::HardRequirement,
AggregateOrderSensitivity::SoftRequirement => Self::SoftRequirement,
AggregateOrderSensitivity::Beneficial => Self::Beneficial,
}
}
Expand Down Expand Up @@ -720,6 +723,7 @@ mod tests {
fn test_round_trip_all_order_sensitivities() {
test_round_trip_order_sensitivity(AggregateOrderSensitivity::Insensitive);
test_round_trip_order_sensitivity(AggregateOrderSensitivity::HardRequirement);
test_round_trip_order_sensitivity(AggregateOrderSensitivity::SoftRequirement);
test_round_trip_order_sensitivity(AggregateOrderSensitivity::Beneficial);
}
}
15 changes: 13 additions & 2 deletions datafusion/functions-aggregate-common/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ pub enum AggregateOrderSensitivity {
/// Ordering at the input is not important for the result of the aggregator.
Insensitive,
/// Indicates that the aggregate expression has a hard requirement on ordering.
/// The aggregator can not produce a correct result unless its ordering
/// The aggregator cannot produce a correct result unless its ordering
/// requirement is satisfied.
HardRequirement,
/// Indicates that the aggregator is more efficient when the input is ordered
/// but can still produce its result correctly regardless of the input ordering.
/// This is similar to, but stronger than, [`Self::Beneficial`].
///
/// Similarly to [`Self::HardRequirement`], when possible DataFusion will insert
/// a `SortExec`, to reorder the input to match the SoftRequirement. However,
/// when such a `SortExec` cannot be inserted, (for example, due to conflicting
/// [`Self::HardRequirement`] with other ordered aggregates in the query),
/// the aggregate function will still execute, without the preferred order, unlike
/// with [`Self::HardRequirement`]
SoftRequirement,
/// Indicates that ordering is beneficial for the aggregate expression in terms
/// of evaluation efficiency. The aggregator can produce its result efficiently
/// when its required ordering is satisfied; however, it can still produce the
Expand All @@ -38,7 +49,7 @@ impl AggregateOrderSensitivity {
}

pub fn is_beneficial(&self) -> bool {
self.eq(&AggregateOrderSensitivity::Beneficial)
matches!(self, Self::SoftRequirement | Self::Beneficial)
}

pub fn hard_requires(&self) -> bool {
Expand Down
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::SoftRequirement
}

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 @@ -518,6 +538,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 @@ -531,6 +553,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 @@ -541,11 +564,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 @@ -616,9 +662,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 @@ -629,8 +674,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 @@ -679,13 +727,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
Loading