Apache DataFusion SQL Query Engine
Pass `return_type` to `AccumulatorFunctionImplementation ` for user defined aggregates (#3428)
Andrew Lamb447141cb8878
diff --git a/datafusion-examples/examples/simple_udaf.rs b/datafusion-examples/examples/simple_udaf.rsindex 378d2548ef..0afb572719 100644--- a/datafusion-examples/examples/simple_udaf.rs+++ b/datafusion-examples/examples/simple_udaf.rs@@ -169,7 +169,7 @@ async fn main() -> Result<()> {Arc::new(DataType::Float64),Volatility::Immutable,// This is the accumulator factory; DataFusion uses it to create new accumulators.- Arc::new(|| Ok(Box::new(GeometricMean::new()))),+ Arc::new(|_| Ok(Box::new(GeometricMean::new()))),// This is the description of the state. `state()` must match the types here.Arc::new(vec![DataType::Float64, DataType::UInt32]),);diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rsindex aaed710dff..64f75dfa24 100644--- a/datafusion/core/src/execution/context.rs+++ b/datafusion/core/src/execution/context.rs@@ -2017,7 +2017,7 @@ mod tests {DataType::Float64,Arc::new(DataType::Float64),Volatility::Immutable,- Arc::new(|| Ok(Box::new(AvgAccumulator::try_new(&DataType::Float64)?))),+ Arc::new(|_| Ok(Box::new(AvgAccumulator::try_new(&DataType::Float64)?))),Arc::new(vec![DataType::UInt64, DataType::Float64]),);diff --git a/datafusion/core/src/physical_plan/udaf.rs b/datafusion/core/src/physical_plan/udaf.rsindex 71e7e06575..e017bb5ad6 100644--- a/datafusion/core/src/physical_plan/udaf.rs+++ b/datafusion/core/src/physical_plan/udaf.rs@@ -64,6 +64,7 @@ pub fn create_aggregate_expr(pub struct AggregateFunctionExpr {fun: AggregateUDF,args: Vec<Arc<dyn PhysicalExpr>>,+ /// Output / return type of this aggregatedata_type: DataType,name: String,}@@ -99,7 +100,7 @@ impl AggregateExpr for AggregateFunctionExpr {}fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> {- (self.fun.accumulator)()+ (self.fun.accumulator)(&self.data_type)}fn name(&self) -> &str {diff --git a/datafusion/core/tests/sql/udf.rs b/datafusion/core/tests/sql/udf.rsindex 705f0abe1f..9d13d5f593 100644--- a/datafusion/core/tests/sql/udf.rs+++ b/datafusion/core/tests/sql/udf.rs@@ -166,7 +166,7 @@ async fn simple_udaf() -> Result<()> {DataType::Float64,Arc::new(DataType::Float64),Volatility::Immutable,- Arc::new(|| Ok(Box::new(AvgAccumulator::try_new(&DataType::Float64)?))),+ Arc::new(|_| Ok(Box::new(AvgAccumulator::try_new(&DataType::Float64)?))),Arc::new(vec![DataType::UInt64, DataType::Float64]),);diff --git a/datafusion/core/tests/user_defined_aggregates.rs b/datafusion/core/tests/user_defined_aggregates.rsindex 33e5d0a0d8..fe84a7e8d8 100644--- a/datafusion/core/tests/user_defined_aggregates.rs+++ b/datafusion/core/tests/user_defined_aggregates.rs@@ -114,7 +114,7 @@ fn register_aggregate(ctx: &mut SessionContext) {let signatures = vec![TypeSignature::Exact(FirstSelector::input_datatypes())];let accumulator: AccumulatorFunctionImplementation =- Arc::new(|| Ok(Box::new(FirstSelector::new())));+ Arc::new(|_| Ok(Box::new(FirstSelector::new())));let volatility = Volatility::Immutable;diff --git a/datafusion/expr/src/function.rs b/datafusion/expr/src/function.rsindex b8b17bf400..973ea8f83f 100644--- a/datafusion/expr/src/function.rs+++ b/datafusion/expr/src/function.rs@@ -39,15 +39,16 @@ use std::sync::Arc;pub type ScalarFunctionImplementation =Arc<dyn Fn(&[ColumnarValue]) -> Result<ColumnarValue> + Send + Sync>;-/// A function's return type+/// Factory that returns the functions's return type given the input argument typespub type ReturnTypeFunction =Arc<dyn Fn(&[DataType]) -> Result<Arc<DataType>> + Send + Sync>;-/// the implementation of an aggregate function+/// Factory that returns an accumulator for the given aggregate, given+/// its return datatype.pub type AccumulatorFunctionImplementation =- Arc<dyn Fn() -> Result<Box<dyn Accumulator>> + Send + Sync>;+ Arc<dyn Fn(&DataType) -> Result<Box<dyn Accumulator>> + Send + Sync>;-/// This signature corresponds to which types an aggregator serializes+/// Factory that returns the types used by an aggregator to serialize/// its state, given its return datatype.pub type StateTypeFunction =Arc<dyn Fn(&DataType) -> Result<Arc<Vec<DataType>>> + Send + Sync>;diff --git a/datafusion/proto/src/lib.rs b/datafusion/proto/src/lib.rsindex f0da137936..8e9475329c 100644--- a/datafusion/proto/src/lib.rs+++ b/datafusion/proto/src/lib.rs@@ -1089,7 +1089,7 @@ mod roundtrip_tests {Arc::new(DataType::Float64),Volatility::Immutable,// This is the accumulator factory; DataFusion uses it to create new accumulators.- Arc::new(|| Ok(Box::new(Dummy {}))),+ Arc::new(|_| Ok(Box::new(Dummy {}))),// This is the description of the state. `state()` must match the types here.Arc::new(vec![DataType::Float64, DataType::UInt32]),);