Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datafusion-functions-aggregate): add support for lists and other nested types in min and max #13991

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
37 changes: 33 additions & 4 deletions datafusion/functions-aggregate/src/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! [`Min`] and [`MinAccumulator`] accumulator for the `min` function

mod min_max_bytes;
mod min_max_generic;

use arrow::array::{
ArrayRef, BinaryArray, BinaryViewArray, BooleanArray, Date32Array, Date64Array,
Expand Down Expand Up @@ -54,6 +55,10 @@ use arrow::datatypes::{
};

use crate::min_max::min_max_bytes::MinMaxBytesAccumulator;
use crate::min_max::min_max_generic::{
GenericMaxAccumulator, GenericMinAccumulator, GenericSlidingMaxAccumulator,
GenericSlidingMinAccumulator,
};
use datafusion_common::ScalarValue;
use datafusion_expr::{
function::AccumulatorArgs, Accumulator, AggregateUDFImpl, Documentation, Signature,
Expand Down Expand Up @@ -230,7 +235,13 @@ impl AggregateUDFImpl for Max {
}

fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(MaxAccumulator::try_new(acc_args.return_type)?))
if acc_args.return_type.is_nested() {
Ok(Box::new(GenericMaxAccumulator::try_new(
acc_args.return_type,
)?))
Comment on lines +239 to +241
Copy link
Contributor

Choose a reason for hiding this comment

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

If you wanted to make this less verbose you could probably do something like

Suggested change
Ok(Box::new(GenericMaxAccumulator::try_new(
acc_args.return_type,
)?))
GenericMaxAccumulator::try_new(
acc_args.return_type,
).map(Box::new)

} else {
Ok(Box::new(MaxAccumulator::try_new(acc_args.return_type)?))
Copy link
Contributor

Choose a reason for hiding this comment

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

The pattern used in other accumulators is slightly different I think -- It would have a function on MaxAccumulator that explicitly declared the types it supported and then fallback to the GenericMaxAccumulator if not.

For example, something like

if MaxAccumulator::supports_type(&acc_args.return_type) {
   Ok(Box::new(MaxAccumulator::try_new(acc_args.return_type)?))
} else {
   Ok(Box::new(GenericMaxAccumulator::try_new(
                acc_args.return_type,
            )?))
}

This is a minor detail and not required, I just wanted to point it out

}
}

fn aliases(&self) -> &[String] {
Expand Down Expand Up @@ -337,7 +348,13 @@ impl AggregateUDFImpl for Max {
&self,
args: AccumulatorArgs,
) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(SlidingMaxAccumulator::try_new(args.return_type)?))
if args.return_type.is_nested() {
Ok(Box::new(GenericSlidingMaxAccumulator::try_new(
args.return_type,
)?))
} else {
Ok(Box::new(SlidingMaxAccumulator::try_new(args.return_type)?))
}
}

fn is_descending(&self) -> Option<bool> {
Expand Down Expand Up @@ -1051,7 +1068,13 @@ impl AggregateUDFImpl for Min {
}

fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(MinAccumulator::try_new(acc_args.return_type)?))
if acc_args.return_type.is_nested() {
Ok(Box::new(GenericMinAccumulator::try_new(
acc_args.return_type,
)?))
} else {
Ok(Box::new(MinAccumulator::try_new(acc_args.return_type)?))
}
}

fn aliases(&self) -> &[String] {
Expand Down Expand Up @@ -1158,7 +1181,13 @@ impl AggregateUDFImpl for Min {
&self,
args: AccumulatorArgs,
) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(SlidingMinAccumulator::try_new(args.return_type)?))
if args.return_type.is_nested() {
Ok(Box::new(GenericSlidingMinAccumulator::try_new(
args.return_type,
)?))
} else {
Ok(Box::new(SlidingMinAccumulator::try_new(args.return_type)?))
}
}

fn is_descending(&self) -> Option<bool> {
Expand Down
Loading
Loading