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
36 changes: 35 additions & 1 deletion datafusion/functions-aggregate/src/first_last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ use datafusion_macros::user_doc;
use datafusion_physical_expr_common::sort_expr::LexOrdering;

mod state;
mod unordered_struct;

use state::{BytesValueState, GenericValueState, PrimitiveValueState, ValueState};
use unordered_struct::UnorderedStructFirstValueGroupsAccumulator;

fn flat_view_struct(data_type: &DataType) -> bool {
let DataType::Struct(fields) = data_type else {
return false;
};
!fields.is_empty()
&& fields.iter().all(|field| {
matches!(field.data_type(), DataType::BinaryView | DataType::Utf8View)
})
}

create_func!(FirstValue, first_value_udaf);
create_func!(LastValue, last_value_udaf);
Expand Down Expand Up @@ -339,13 +351,20 @@ impl AggregateUDFImpl for FirstValue {
}

fn groups_accumulator_supported(&self, args: AccumulatorArgs) -> bool {
groups_accumulator_supported(&args)
(args.order_bys.is_empty() && flat_view_struct(args.return_field.data_type()))
|| groups_accumulator_supported(&args)
}

fn create_groups_accumulator(
&self,
args: AccumulatorArgs,
) -> Result<Box<dyn GroupsAccumulator>> {
if args.order_bys.is_empty() && flat_view_struct(args.return_field.data_type()) {
return Ok(Box::new(UnorderedStructFirstValueGroupsAccumulator::new(
args.return_field.data_type().clone(),
args.ignore_nulls,
)));
}
create_groups_accumulator(&args, true, self.name())
}

Expand Down Expand Up @@ -1449,6 +1468,21 @@ mod tests {

use super::*;

#[test]
fn unordered_struct_fast_path_only_accepts_flat_view_fields() {
let view = Arc::new(Field::new("value", DataType::BinaryView, false));
let nested = Arc::new(Field::new(
"values",
DataType::List(Arc::clone(&view)),
false,
));
assert!(flat_view_struct(&DataType::Struct(vec![view].into())));
assert!(!flat_view_struct(&DataType::Struct(vec![nested].into())));
assert!(!flat_view_struct(&DataType::Struct(
vec![Arc::new(Field::new("value", DataType::Int64, false))].into()
)));
}

#[test]
fn test_first_last_value_value() -> Result<()> {
let mut first_accumulator =
Expand Down
Loading
Loading