Apache DataFusion SQL Query Engine
Minor: Encapsulate Aggregate and Window function signatures (#6748)
Andrew Lamb4a0b65d9a1c1
diff --git a/datafusion/expr/src/aggregate_function.rs b/datafusion/expr/src/aggregate_function.rsindex 5b0676a815..add1262237 100644--- a/datafusion/expr/src/aggregate_function.rs+++ b/datafusion/expr/src/aggregate_function.rs@@ -172,58 +172,79 @@ impl FromStr for AggregateFunction {/// Returns the datatype of the aggregate function./// This is used to get the returned data type for aggregate expr.+#[deprecated(+ since = "27.0.0",+ note = "please use `AggregateFunction::return_type` instead"+)]pub fn return_type(fun: &AggregateFunction,input_expr_types: &[DataType],) -> Result<DataType> {- // Note that this function *must* return the same type that the respective physical expression returns- // or the execution panics.+ fun.return_type(input_expr_types)+}- let coerced_data_types = crate::type_coercion::aggregates::coerce_types(- fun,- input_expr_types,- &signature(fun),- )?;+impl AggregateFunction {+ /// Returns the datatype of the aggregate function given its argument types+ ///+ /// This is used to get the returned data type for aggregate expr.+ pub fn return_type(&self, input_expr_types: &[DataType]) -> Result<DataType> {+ // Note that this function *must* return the same type that the respective physical expression returns+ // or the execution panics.- match fun {- AggregateFunction::Count | AggregateFunction::ApproxDistinct => {- Ok(DataType::Int64)- }- AggregateFunction::Max | AggregateFunction::Min => {- // For min and max agg function, the returned type is same as input type.- // The coerced_data_types is same with input_types.- Ok(coerced_data_types[0].clone())- }- AggregateFunction::Sum => sum_return_type(&coerced_data_types[0]),- AggregateFunction::BitAnd- | AggregateFunction::BitOr- | AggregateFunction::BitXor => Ok(coerced_data_types[0].clone()),- AggregateFunction::BoolAnd | AggregateFunction::BoolOr => Ok(DataType::Boolean),- AggregateFunction::Variance => variance_return_type(&coerced_data_types[0]),- AggregateFunction::VariancePop => variance_return_type(&coerced_data_types[0]),- AggregateFunction::Covariance => covariance_return_type(&coerced_data_types[0]),- AggregateFunction::CovariancePop => {- covariance_return_type(&coerced_data_types[0])- }- AggregateFunction::Correlation => correlation_return_type(&coerced_data_types[0]),- AggregateFunction::Stddev => stddev_return_type(&coerced_data_types[0]),- AggregateFunction::StddevPop => stddev_return_type(&coerced_data_types[0]),- AggregateFunction::Avg => avg_return_type(&coerced_data_types[0]),- AggregateFunction::ArrayAgg => Ok(DataType::List(Arc::new(Field::new(- "item",- coerced_data_types[0].clone(),- true,- )))),- AggregateFunction::ApproxPercentileCont => Ok(coerced_data_types[0].clone()),- AggregateFunction::ApproxPercentileContWithWeight => {- Ok(coerced_data_types[0].clone())- }- AggregateFunction::ApproxMedian | AggregateFunction::Median => {- Ok(coerced_data_types[0].clone())- }- AggregateFunction::Grouping => Ok(DataType::Int32),- AggregateFunction::FirstValue | AggregateFunction::LastValue => {- Ok(coerced_data_types[0].clone())+ let coerced_data_types = crate::type_coercion::aggregates::coerce_types(+ self,+ input_expr_types,+ &self.signature(),+ )?;++ match self {+ AggregateFunction::Count | AggregateFunction::ApproxDistinct => {+ Ok(DataType::Int64)+ }+ AggregateFunction::Max | AggregateFunction::Min => {+ // For min and max agg function, the returned type is same as input type.+ // The coerced_data_types is same with input_types.+ Ok(coerced_data_types[0].clone())+ }+ AggregateFunction::Sum => sum_return_type(&coerced_data_types[0]),+ AggregateFunction::BitAnd+ | AggregateFunction::BitOr+ | AggregateFunction::BitXor => Ok(coerced_data_types[0].clone()),+ AggregateFunction::BoolAnd | AggregateFunction::BoolOr => {+ Ok(DataType::Boolean)+ }+ AggregateFunction::Variance => variance_return_type(&coerced_data_types[0]),+ AggregateFunction::VariancePop => {+ variance_return_type(&coerced_data_types[0])+ }+ AggregateFunction::Covariance => {+ covariance_return_type(&coerced_data_types[0])+ }+ AggregateFunction::CovariancePop => {+ covariance_return_type(&coerced_data_types[0])+ }+ AggregateFunction::Correlation => {+ correlation_return_type(&coerced_data_types[0])+ }+ AggregateFunction::Stddev => stddev_return_type(&coerced_data_types[0]),+ AggregateFunction::StddevPop => stddev_return_type(&coerced_data_types[0]),+ AggregateFunction::Avg => avg_return_type(&coerced_data_types[0]),+ AggregateFunction::ArrayAgg => Ok(DataType::List(Arc::new(Field::new(+ "item",+ coerced_data_types[0].clone(),+ true,+ )))),+ AggregateFunction::ApproxPercentileCont => Ok(coerced_data_types[0].clone()),+ AggregateFunction::ApproxPercentileContWithWeight => {+ Ok(coerced_data_types[0].clone())+ }+ AggregateFunction::ApproxMedian | AggregateFunction::Median => {+ Ok(coerced_data_types[0].clone())+ }+ AggregateFunction::Grouping => Ok(DataType::Int32),+ AggregateFunction::FirstValue | AggregateFunction::LastValue => {+ Ok(coerced_data_types[0].clone())+ }}}}@@ -236,79 +257,94 @@ pub fn sum_type_of_avg(input_expr_types: &[DataType]) -> Result<DataType> {let coerced_data_types = crate::type_coercion::aggregates::coerce_types(&fun,input_expr_types,- &signature(&fun),+ &fun.signature(),)?;avg_sum_type(&coerced_data_types[0])}/// the signatures supported by the function `fun`.+#[deprecated(+ since = "27.0.0",+ note = "please use `AggregateFunction::signature` instead"+)]pub fn signature(fun: &AggregateFunction) -> Signature {- // note: the physical expression must accept the type returned by this function or the execution panics.- match fun {- AggregateFunction::Count => Signature::variadic_any(Volatility::Immutable),- AggregateFunction::ApproxDistinct- | AggregateFunction::Grouping- | AggregateFunction::ArrayAgg => Signature::any(1, Volatility::Immutable),- AggregateFunction::Min | AggregateFunction::Max => {- let valid = STRINGS- .iter()- .chain(NUMERICS.iter())- .chain(TIMESTAMPS.iter())- .chain(DATES.iter())- .chain(TIMES.iter())- .cloned()- .collect::<Vec<_>>();- Signature::uniform(1, valid, Volatility::Immutable)- }- AggregateFunction::BitAnd- | AggregateFunction::BitOr- | AggregateFunction::BitXor => {- Signature::uniform(1, INTEGERS.to_vec(), Volatility::Immutable)- }- AggregateFunction::BoolAnd | AggregateFunction::BoolOr => {- Signature::uniform(1, vec![DataType::Boolean], Volatility::Immutable)- }- AggregateFunction::Avg- | AggregateFunction::Sum- | AggregateFunction::Variance- | AggregateFunction::VariancePop- | AggregateFunction::Stddev- | AggregateFunction::StddevPop- | AggregateFunction::Median- | AggregateFunction::ApproxMedian- | AggregateFunction::FirstValue- | AggregateFunction::LastValue => {- Signature::uniform(1, NUMERICS.to_vec(), Volatility::Immutable)- }- AggregateFunction::Covariance | AggregateFunction::CovariancePop => {- Signature::uniform(2, NUMERICS.to_vec(), Volatility::Immutable)- }- AggregateFunction::Correlation => {- Signature::uniform(2, NUMERICS.to_vec(), Volatility::Immutable)- }- AggregateFunction::ApproxPercentileCont => {- // Accept any numeric value paired with a float64 percentile- let with_tdigest_size = NUMERICS.iter().map(|t| {- TypeSignature::Exact(vec![t.clone(), DataType::Float64, t.clone()])- });- Signature::one_of(+ fun.signature()+}++impl AggregateFunction {+ /// the signatures supported by the function `fun`.+ pub fn signature(&self) -> Signature {+ // note: the physical expression must accept the type returned by this function or the execution panics.+ match self {+ AggregateFunction::Count => Signature::variadic_any(Volatility::Immutable),+ AggregateFunction::ApproxDistinct+ | AggregateFunction::Grouping+ | AggregateFunction::ArrayAgg => Signature::any(1, Volatility::Immutable),+ AggregateFunction::Min | AggregateFunction::Max => {+ let valid = STRINGS+ .iter()+ .chain(NUMERICS.iter())+ .chain(TIMESTAMPS.iter())+ .chain(DATES.iter())+ .chain(TIMES.iter())+ .cloned()+ .collect::<Vec<_>>();+ Signature::uniform(1, valid, Volatility::Immutable)+ }+ AggregateFunction::BitAnd+ | AggregateFunction::BitOr+ | AggregateFunction::BitXor => {+ Signature::uniform(1, INTEGERS.to_vec(), Volatility::Immutable)+ }+ AggregateFunction::BoolAnd | AggregateFunction::BoolOr => {+ Signature::uniform(1, vec![DataType::Boolean], Volatility::Immutable)+ }+ AggregateFunction::Avg+ | AggregateFunction::Sum+ | AggregateFunction::Variance+ | AggregateFunction::VariancePop+ | AggregateFunction::Stddev+ | AggregateFunction::StddevPop+ | AggregateFunction::Median+ | AggregateFunction::ApproxMedian+ | AggregateFunction::FirstValue+ | AggregateFunction::LastValue => {+ Signature::uniform(1, NUMERICS.to_vec(), Volatility::Immutable)+ }+ AggregateFunction::Covariance | AggregateFunction::CovariancePop => {+ Signature::uniform(2, NUMERICS.to_vec(), Volatility::Immutable)+ }+ AggregateFunction::Correlation => {+ Signature::uniform(2, NUMERICS.to_vec(), Volatility::Immutable)+ }+ AggregateFunction::ApproxPercentileCont => {+ // Accept any numeric value paired with a float64 percentile+ let with_tdigest_size = NUMERICS.iter().map(|t| {+ TypeSignature::Exact(vec![t.clone(), DataType::Float64, t.clone()])+ });+ Signature::one_of(+ NUMERICS+ .iter()+ .map(|t| TypeSignature::Exact(vec![t.clone(), DataType::Float64]))+ .chain(with_tdigest_size)+ .collect(),+ Volatility::Immutable,+ )+ }+ AggregateFunction::ApproxPercentileContWithWeight => Signature::one_of(+ // Accept any numeric value paired with a float64 percentileNUMERICS.iter()- .map(|t| TypeSignature::Exact(vec![t.clone(), DataType::Float64]))- .chain(with_tdigest_size)+ .map(|t| {+ TypeSignature::Exact(vec![+ t.clone(),+ t.clone(),+ DataType::Float64,+ ])+ }).collect(),Volatility::Immutable,- )+ ),}- AggregateFunction::ApproxPercentileContWithWeight => Signature::one_of(- // Accept any numeric value paired with a float64 percentile- NUMERICS- .iter()- .map(|t| {- TypeSignature::Exact(vec![t.clone(), t.clone(), DataType::Float64])- })- .collect(),- Volatility::Immutable,- ),}}diff --git a/datafusion/expr/src/expr_schema.rs b/datafusion/expr/src/expr_schema.rsindex 5861a76e88..30537d0fdd 100644--- a/datafusion/expr/src/expr_schema.rs+++ b/datafusion/expr/src/expr_schema.rs@@ -22,7 +22,7 @@ use crate::expr::{};use crate::field_util::get_indexed_field;use crate::type_coercion::binary::get_result_type;-use crate::{aggregate_function, window_function, LogicalPlan, Projection, Subquery};+use crate::{LogicalPlan, Projection, Subquery};use arrow::compute::can_cast_types;use arrow::datatypes::DataType;use datafusion_common::{Column, DFField, DFSchema, DataFusionError, ExprSchema, Result};@@ -92,14 +92,14 @@ impl ExprSchemable for Expr {.iter().map(|e| e.get_type(schema)).collect::<Result<Vec<_>>>()?;- window_function::return_type(fun, &data_types)+ fun.return_type(&data_types)}Expr::AggregateFunction(AggregateFunction { fun, args, .. }) => {let data_types = args.iter().map(|e| e.get_type(schema)).collect::<Result<Vec<_>>>()?;- aggregate_function::return_type(fun, &data_types)+ fun.return_type(&data_types)}Expr::AggregateUDF(AggregateUDF { fun, args, .. }) => {let data_types = argsdiff --git a/datafusion/expr/src/type_coercion/aggregates.rs b/datafusion/expr/src/type_coercion/aggregates.rsindex 4f02bf3dfd..939606c04f 100644--- a/datafusion/expr/src/type_coercion/aggregates.rs+++ b/datafusion/expr/src/type_coercion/aggregates.rs@@ -539,7 +539,6 @@ pub fn is_approx_percentile_cont_supported_arg_type(arg_type: &DataType) -> bool#[cfg(test)]mod tests {use super::*;- use crate::aggregate_function;use arrow::datatypes::DataType;#[test]@@ -547,21 +546,21 @@ mod tests {// test input args with error number input typeslet fun = AggregateFunction::Min;let input_types = vec![DataType::Int64, DataType::Int32];- let signature = aggregate_function::signature(&fun);+ let signature = fun.signature();let result = coerce_types(&fun, &input_types, &signature);assert_eq!("Error during planning: The function Min expects 1 arguments, but 2 were provided", result.unwrap_err().to_string());// test input args is invalid data type for sum or avglet fun = AggregateFunction::Sum;let input_types = vec![DataType::Utf8];- let signature = aggregate_function::signature(&fun);+ let signature = fun.signature();let result = coerce_types(&fun, &input_types, &signature);assert_eq!("Error during planning: The function Sum does not support inputs of type Utf8.",result.unwrap_err().to_string());let fun = AggregateFunction::Avg;- let signature = aggregate_function::signature(&fun);+ let signature = fun.signature();let result = coerce_types(&fun, &input_types, &signature);assert_eq!("Error during planning: The function Avg does not support inputs of type Utf8.",@@ -584,7 +583,7 @@ mod tests {];for fun in funs {for input_type in &input_types {- let signature = aggregate_function::signature(&fun);+ let signature = fun.signature();let result = coerce_types(&fun, input_type, &signature);assert_eq!(*input_type, result.unwrap());}@@ -598,7 +597,7 @@ mod tests {];for fun in funs {for input_type in &input_types {- let signature = aggregate_function::signature(&fun);+ let signature = fun.signature();let result = coerce_types(&fun, input_type, &signature);assert_eq!(*input_type, result.unwrap());}@@ -618,8 +617,7 @@ mod tests {vec![DataType::Float64, DataType::Float64],];for input_type in &input_types {- let signature =- aggregate_function::signature(&AggregateFunction::ApproxPercentileCont);+ let signature = AggregateFunction::ApproxPercentileCont.signature();let result = coerce_types(&AggregateFunction::ApproxPercentileCont,input_type,diff --git a/datafusion/expr/src/window_function.rs b/datafusion/expr/src/window_function.rsindex 6f30bff69b..1c847a4541 100644--- a/datafusion/expr/src/window_function.rs+++ b/datafusion/expr/src/window_function.rs@@ -22,9 +22,7 @@use crate::aggregate_function::AggregateFunction;use crate::type_coercion::functions::data_types;-use crate::{- aggregate_function, AggregateUDF, Signature, TypeSignature, Volatility, WindowUDF,-};+use crate::{AggregateUDF, Signature, TypeSignature, Volatility, WindowUDF};use arrow::datatypes::DataType;use datafusion_common::{DataFusionError, Result};use std::sync::Arc;@@ -157,85 +155,117 @@ impl FromStr for BuiltInWindowFunction {}/// Returns the datatype of the window function+#[deprecated(+ since = "27.0.0",+ note = "please use `WindowFunction::return_type` instead"+)]pub fn return_type(fun: &WindowFunction,input_expr_types: &[DataType],) -> Result<DataType> {- match fun {- WindowFunction::AggregateFunction(fun) => {- aggregate_function::return_type(fun, input_expr_types)- }- WindowFunction::BuiltInWindowFunction(fun) => {- return_type_for_built_in(fun, input_expr_types)- }- WindowFunction::AggregateUDF(fun) => {- Ok((*(fun.return_type)(input_expr_types)?).clone())- }- WindowFunction::WindowUDF(fun) => {- Ok((*(fun.return_type)(input_expr_types)?).clone())+ fun.return_type(input_expr_types)+}++impl WindowFunction {+ /// Returns the datatype of the window function+ pub fn return_type(&self, input_expr_types: &[DataType]) -> Result<DataType> {+ match self {+ WindowFunction::AggregateFunction(fun) => fun.return_type(input_expr_types),+ WindowFunction::BuiltInWindowFunction(fun) => {+ fun.return_type(input_expr_types)+ }+ WindowFunction::AggregateUDF(fun) => {+ Ok((*(fun.return_type)(input_expr_types)?).clone())+ }+ WindowFunction::WindowUDF(fun) => {+ Ok((*(fun.return_type)(input_expr_types)?).clone())+ }}}}/// Returns the datatype of the built-in window function-fn return_type_for_built_in(- fun: &BuiltInWindowFunction,- input_expr_types: &[DataType],-) -> Result<DataType> {- // Note that this function *must* return the same type that the respective physical expression returns- // or the execution panics.-- // verify that this is a valid set of data types for this function- data_types(input_expr_types, &signature_for_built_in(fun))?;-- match fun {- BuiltInWindowFunction::RowNumber- | BuiltInWindowFunction::Rank- | BuiltInWindowFunction::DenseRank => Ok(DataType::UInt64),- BuiltInWindowFunction::PercentRank | BuiltInWindowFunction::CumeDist => {- Ok(DataType::Float64)+impl BuiltInWindowFunction {+ pub fn return_type(&self, input_expr_types: &[DataType]) -> Result<DataType> {+ // Note that this function *must* return the same type that the respective physical expression returns+ // or the execution panics.++ // verify that this is a valid set of data types for this function+ data_types(input_expr_types, &self.signature())?;++ match self {+ BuiltInWindowFunction::RowNumber+ | BuiltInWindowFunction::Rank+ | BuiltInWindowFunction::DenseRank => Ok(DataType::UInt64),+ BuiltInWindowFunction::PercentRank | BuiltInWindowFunction::CumeDist => {+ Ok(DataType::Float64)+ }+ BuiltInWindowFunction::Ntile => Ok(DataType::UInt32),+ BuiltInWindowFunction::Lag+ | BuiltInWindowFunction::Lead+ | BuiltInWindowFunction::FirstValue+ | BuiltInWindowFunction::LastValue+ | BuiltInWindowFunction::NthValue => Ok(input_expr_types[0].clone()),}- BuiltInWindowFunction::Ntile => Ok(DataType::UInt32),- BuiltInWindowFunction::Lag- | BuiltInWindowFunction::Lead- | BuiltInWindowFunction::FirstValue- | BuiltInWindowFunction::LastValue- | BuiltInWindowFunction::NthValue => Ok(input_expr_types[0].clone()),}}/// the signatures supported by the function `fun`.+#[deprecated(+ since = "27.0.0",+ note = "please use `WindowFunction::signature` instead"+)]pub fn signature(fun: &WindowFunction) -> Signature {- match fun {- WindowFunction::AggregateFunction(fun) => aggregate_function::signature(fun),- WindowFunction::BuiltInWindowFunction(fun) => signature_for_built_in(fun),- WindowFunction::AggregateUDF(fun) => fun.signature.clone(),- WindowFunction::WindowUDF(fun) => fun.signature.clone(),+ fun.signature()+}++impl WindowFunction {+ /// the signatures supported by the function `fun`.+ pub fn signature(&self) -> Signature {+ match self {+ WindowFunction::AggregateFunction(fun) => fun.signature(),+ WindowFunction::BuiltInWindowFunction(fun) => fun.signature(),+ WindowFunction::AggregateUDF(fun) => fun.signature.clone(),+ WindowFunction::WindowUDF(fun) => fun.signature.clone(),+ }}}/// the signatures supported by the built-in window function `fun`.+#[deprecated(+ since = "27.0.0",+ note = "please use `BuiltInWindowFunction::signature` instead"+)]pub fn signature_for_built_in(fun: &BuiltInWindowFunction) -> Signature {- // note: the physical expression must accept the type returned by this function or the execution panics.- match fun {- BuiltInWindowFunction::RowNumber- | BuiltInWindowFunction::Rank- | BuiltInWindowFunction::DenseRank- | BuiltInWindowFunction::PercentRank- | BuiltInWindowFunction::CumeDist => Signature::any(0, Volatility::Immutable),- BuiltInWindowFunction::Lag | BuiltInWindowFunction::Lead => Signature::one_of(- vec![- TypeSignature::Any(1),- TypeSignature::Any(2),- TypeSignature::Any(3),- ],- Volatility::Immutable,- ),- BuiltInWindowFunction::FirstValue | BuiltInWindowFunction::LastValue => {- Signature::any(1, Volatility::Immutable)+ fun.signature()+}++impl BuiltInWindowFunction {+ /// the signatures supported by the built-in window function `fun`.+ pub fn signature(&self) -> Signature {+ // note: the physical expression must accept the type returned by this function or the execution panics.+ match self {+ BuiltInWindowFunction::RowNumber+ | BuiltInWindowFunction::Rank+ | BuiltInWindowFunction::DenseRank+ | BuiltInWindowFunction::PercentRank+ | BuiltInWindowFunction::CumeDist => Signature::any(0, Volatility::Immutable),+ BuiltInWindowFunction::Lag | BuiltInWindowFunction::Lead => {+ Signature::one_of(+ vec![+ TypeSignature::Any(1),+ TypeSignature::Any(2),+ TypeSignature::Any(3),+ ],+ Volatility::Immutable,+ )+ }+ BuiltInWindowFunction::FirstValue | BuiltInWindowFunction::LastValue => {+ Signature::any(1, Volatility::Immutable)+ }+ BuiltInWindowFunction::Ntile => Signature::any(1, Volatility::Immutable),+ BuiltInWindowFunction::NthValue => Signature::any(2, Volatility::Immutable),}- BuiltInWindowFunction::Ntile => Signature::any(1, Volatility::Immutable),- BuiltInWindowFunction::NthValue => Signature::any(2, Volatility::Immutable),}}@@ -246,10 +276,10 @@ mod tests {#[test]fn test_count_return_type() -> Result<()> {let fun = find_df_window_func("count").unwrap();- let observed = return_type(&fun, &[DataType::Utf8])?;+ let observed = fun.return_type(&[DataType::Utf8])?;assert_eq!(DataType::Int64, observed);- let observed = return_type(&fun, &[DataType::UInt64])?;+ let observed = fun.return_type(&[DataType::UInt64])?;assert_eq!(DataType::Int64, observed);Ok(())@@ -258,10 +288,10 @@ mod tests {#[test]fn test_first_value_return_type() -> Result<()> {let fun = find_df_window_func("first_value").unwrap();- let observed = return_type(&fun, &[DataType::Utf8])?;+ let observed = fun.return_type(&[DataType::Utf8])?;assert_eq!(DataType::Utf8, observed);- let observed = return_type(&fun, &[DataType::UInt64])?;+ let observed = fun.return_type(&[DataType::UInt64])?;assert_eq!(DataType::UInt64, observed);Ok(())@@ -270,10 +300,10 @@ mod tests {#[test]fn test_last_value_return_type() -> Result<()> {let fun = find_df_window_func("last_value").unwrap();- let observed = return_type(&fun, &[DataType::Utf8])?;+ let observed = fun.return_type(&[DataType::Utf8])?;assert_eq!(DataType::Utf8, observed);- let observed = return_type(&fun, &[DataType::Float64])?;+ let observed = fun.return_type(&[DataType::Float64])?;assert_eq!(DataType::Float64, observed);Ok(())@@ -282,10 +312,10 @@ mod tests {#[test]fn test_lead_return_type() -> Result<()> {let fun = find_df_window_func("lead").unwrap();- let observed = return_type(&fun, &[DataType::Utf8])?;+ let observed = fun.return_type(&[DataType::Utf8])?;assert_eq!(DataType::Utf8, observed);- let observed = return_type(&fun, &[DataType::Float64])?;+ let observed = fun.return_type(&[DataType::Float64])?;assert_eq!(DataType::Float64, observed);Ok(())@@ -294,10 +324,10 @@ mod tests {#[test]fn test_lag_return_type() -> Result<()> {let fun = find_df_window_func("lag").unwrap();- let observed = return_type(&fun, &[DataType::Utf8])?;+ let observed = fun.return_type(&[DataType::Utf8])?;assert_eq!(DataType::Utf8, observed);- let observed = return_type(&fun, &[DataType::Float64])?;+ let observed = fun.return_type(&[DataType::Float64])?;assert_eq!(DataType::Float64, observed);Ok(())@@ -306,10 +336,10 @@ mod tests {#[test]fn test_nth_value_return_type() -> Result<()> {let fun = find_df_window_func("nth_value").unwrap();- let observed = return_type(&fun, &[DataType::Utf8, DataType::UInt64])?;+ let observed = fun.return_type(&[DataType::Utf8, DataType::UInt64])?;assert_eq!(DataType::Utf8, observed);- let observed = return_type(&fun, &[DataType::Float64, DataType::UInt64])?;+ let observed = fun.return_type(&[DataType::Float64, DataType::UInt64])?;assert_eq!(DataType::Float64, observed);Ok(())@@ -318,7 +348,7 @@ mod tests {#[test]fn test_percent_rank_return_type() -> Result<()> {let fun = find_df_window_func("percent_rank").unwrap();- let observed = return_type(&fun, &[])?;+ let observed = fun.return_type(&[])?;assert_eq!(DataType::Float64, observed);Ok(())@@ -327,7 +357,7 @@ mod tests {#[test]fn test_cume_dist_return_type() -> Result<()> {let fun = find_df_window_func("cume_dist").unwrap();- let observed = return_type(&fun, &[])?;+ let observed = fun.return_type(&[])?;assert_eq!(DataType::Float64, observed);Ok(())diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs b/datafusion/optimizer/src/analyzer/type_coercion.rsindex a5c8dbe356..8edf734b47 100644--- a/datafusion/optimizer/src/analyzer/type_coercion.rs+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs@@ -41,9 +41,9 @@ use datafusion_expr::type_coercion::other::{use datafusion_expr::type_coercion::{is_datetime, is_numeric, is_utf8_or_large_utf8};use datafusion_expr::utils::from_plan;use datafusion_expr::{- aggregate_function, is_false, is_not_false, is_not_true, is_not_unknown, is_true,- is_unknown, type_coercion, AggregateFunction, BuiltinScalarFunction, Expr,- LogicalPlan, Operator, WindowFrame, WindowFrameBound, WindowFrameUnits,+ is_false, is_not_false, is_not_true, is_not_unknown, is_true, is_unknown,+ type_coercion, AggregateFunction, BuiltinScalarFunction, Expr, LogicalPlan, Operator,+ WindowFrame, WindowFrameBound, WindowFrameUnits,};use datafusion_expr::{ExprSchemable, Signature};@@ -347,7 +347,7 @@ impl TreeNodeRewriter for TypeCoercionRewriter {&fun,&args,&self.schema,- &aggregate_function::signature(&fun),+ &fun.signature(),)?;let expr = Expr::AggregateFunction(expr::AggregateFunction::new(fun, new_expr, distinct, filter, order_by,diff --git a/datafusion/physical-expr/src/aggregate/build_in.rs b/datafusion/physical-expr/src/aggregate/build_in.rsindex 3f778b6f17..4dc7c824ed 100644--- a/datafusion/physical-expr/src/aggregate/build_in.rs+++ b/datafusion/physical-expr/src/aggregate/build_in.rs@@ -29,7 +29,7 @@use crate::{expressions, AggregateExpr, PhysicalExpr, PhysicalSortExpr};use arrow::datatypes::Schema;use datafusion_common::{DataFusionError, Result};-use datafusion_expr::aggregate_function::{return_type, sum_type_of_avg};+use datafusion_expr::aggregate_function::sum_type_of_avg;pub use datafusion_expr::AggregateFunction;use std::sync::Arc;@@ -49,7 +49,7 @@ pub fn create_aggregate_expr(.iter().map(|e| e.data_type(input_schema)).collect::<Result<Vec<_>>>()?;- let rt_type = return_type(fun, &input_phy_types)?;+ let rt_type = fun.return_type(&input_phy_types)?;let ordering_types = ordering_req.iter().map(|e| e.expr.data_type(input_schema))@@ -335,7 +335,7 @@ mod tests {use arrow::datatypes::{DataType, Field};use datafusion_common::ScalarValue;use datafusion_expr::type_coercion::aggregates::NUMERICS;- use datafusion_expr::{aggregate_function, type_coercion, Signature};+ use datafusion_expr::{type_coercion, Signature};#[test]fn test_count_arragg_approx_expr() -> Result<()> {@@ -1035,16 +1035,14 @@ mod tests {#[test]fn test_median() -> Result<()> {- let observed = return_type(&AggregateFunction::ApproxMedian, &[DataType::Utf8]);+ let observed = AggregateFunction::ApproxMedian.return_type(&[DataType::Utf8]);assert!(observed.is_err());- let observed = return_type(&AggregateFunction::ApproxMedian, &[DataType::Int32])?;+ let observed = AggregateFunction::ApproxMedian.return_type(&[DataType::Int32])?;assert_eq!(DataType::Int32, observed);- let observed = return_type(- &AggregateFunction::ApproxMedian,- &[DataType::Decimal128(10, 6)],- );+ let observed =+ AggregateFunction::ApproxMedian.return_type(&[DataType::Decimal128(10, 6)]);assert!(observed.is_err());Ok(())@@ -1052,20 +1050,20 @@ mod tests {#[test]fn test_min_max() -> Result<()> {- let observed = return_type(&AggregateFunction::Min, &[DataType::Utf8])?;+ let observed = AggregateFunction::Min.return_type(&[DataType::Utf8])?;assert_eq!(DataType::Utf8, observed);- let observed = return_type(&AggregateFunction::Max, &[DataType::Int32])?;+ let observed = AggregateFunction::Max.return_type(&[DataType::Int32])?;assert_eq!(DataType::Int32, observed);// test decimal for minlet observed =- return_type(&AggregateFunction::Min, &[DataType::Decimal128(10, 6)])?;+ AggregateFunction::Min.return_type(&[DataType::Decimal128(10, 6)])?;assert_eq!(DataType::Decimal128(10, 6), observed);// test decimal for maxlet observed =- return_type(&AggregateFunction::Max, &[DataType::Decimal128(28, 13)])?;+ AggregateFunction::Max.return_type(&[DataType::Decimal128(28, 13)])?;assert_eq!(DataType::Decimal128(28, 13), observed);Ok(())@@ -1073,24 +1071,24 @@ mod tests {#[test]fn test_sum_return_type() -> Result<()> {- let observed = return_type(&AggregateFunction::Sum, &[DataType::Int32])?;+ let observed = AggregateFunction::Sum.return_type(&[DataType::Int32])?;assert_eq!(DataType::Int64, observed);- let observed = return_type(&AggregateFunction::Sum, &[DataType::UInt8])?;+ let observed = AggregateFunction::Sum.return_type(&[DataType::UInt8])?;assert_eq!(DataType::UInt64, observed);- let observed = return_type(&AggregateFunction::Sum, &[DataType::Float32])?;+ let observed = AggregateFunction::Sum.return_type(&[DataType::Float32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Sum, &[DataType::Float64])?;+ let observed = AggregateFunction::Sum.return_type(&[DataType::Float64])?;assert_eq!(DataType::Float64, observed);let observed =- return_type(&AggregateFunction::Sum, &[DataType::Decimal128(10, 5)])?;+ AggregateFunction::Sum.return_type(&[DataType::Decimal128(10, 5)])?;assert_eq!(DataType::Decimal128(20, 5), observed);let observed =- return_type(&AggregateFunction::Sum, &[DataType::Decimal128(35, 5)])?;+ AggregateFunction::Sum.return_type(&[DataType::Decimal128(35, 5)])?;assert_eq!(DataType::Decimal128(38, 5), observed);Ok(())@@ -1098,73 +1096,73 @@ mod tests {#[test]fn test_sum_no_utf8() {- let observed = return_type(&AggregateFunction::Sum, &[DataType::Utf8]);+ let observed = AggregateFunction::Sum.return_type(&[DataType::Utf8]);assert!(observed.is_err());}#[test]fn test_sum_upcasts() -> Result<()> {- let observed = return_type(&AggregateFunction::Sum, &[DataType::UInt32])?;+ let observed = AggregateFunction::Sum.return_type(&[DataType::UInt32])?;assert_eq!(DataType::UInt64, observed);Ok(())}#[test]fn test_count_return_type() -> Result<()> {- let observed = return_type(&AggregateFunction::Count, &[DataType::Utf8])?;+ let observed = AggregateFunction::Count.return_type(&[DataType::Utf8])?;assert_eq!(DataType::Int64, observed);- let observed = return_type(&AggregateFunction::Count, &[DataType::Int8])?;+ let observed = AggregateFunction::Count.return_type(&[DataType::Int8])?;assert_eq!(DataType::Int64, observed);let observed =- return_type(&AggregateFunction::Count, &[DataType::Decimal128(28, 13)])?;+ AggregateFunction::Count.return_type(&[DataType::Decimal128(28, 13)])?;assert_eq!(DataType::Int64, observed);Ok(())}#[test]fn test_avg_return_type() -> Result<()> {- let observed = return_type(&AggregateFunction::Avg, &[DataType::Float32])?;+ let observed = AggregateFunction::Avg.return_type(&[DataType::Float32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Avg, &[DataType::Float64])?;+ let observed = AggregateFunction::Avg.return_type(&[DataType::Float64])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Avg, &[DataType::Int32])?;+ let observed = AggregateFunction::Avg.return_type(&[DataType::Int32])?;assert_eq!(DataType::Float64, observed);let observed =- return_type(&AggregateFunction::Avg, &[DataType::Decimal128(10, 6)])?;+ AggregateFunction::Avg.return_type(&[DataType::Decimal128(10, 6)])?;assert_eq!(DataType::Decimal128(14, 10), observed);let observed =- return_type(&AggregateFunction::Avg, &[DataType::Decimal128(36, 6)])?;+ AggregateFunction::Avg.return_type(&[DataType::Decimal128(36, 6)])?;assert_eq!(DataType::Decimal128(38, 10), observed);Ok(())}#[test]fn test_avg_no_utf8() {- let observed = return_type(&AggregateFunction::Avg, &[DataType::Utf8]);+ let observed = AggregateFunction::Avg.return_type(&[DataType::Utf8]);assert!(observed.is_err());}#[test]fn test_variance_return_type() -> Result<()> {- let observed = return_type(&AggregateFunction::Variance, &[DataType::Float32])?;+ let observed = AggregateFunction::Variance.return_type(&[DataType::Float32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Variance, &[DataType::Float64])?;+ let observed = AggregateFunction::Variance.return_type(&[DataType::Float64])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Variance, &[DataType::Int32])?;+ let observed = AggregateFunction::Variance.return_type(&[DataType::Int32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Variance, &[DataType::UInt32])?;+ let observed = AggregateFunction::Variance.return_type(&[DataType::UInt32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Variance, &[DataType::Int64])?;+ let observed = AggregateFunction::Variance.return_type(&[DataType::Int64])?;assert_eq!(DataType::Float64, observed);Ok(())@@ -1172,25 +1170,25 @@ mod tests {#[test]fn test_variance_no_utf8() {- let observed = return_type(&AggregateFunction::Variance, &[DataType::Utf8]);+ let observed = AggregateFunction::Variance.return_type(&[DataType::Utf8]);assert!(observed.is_err());}#[test]fn test_stddev_return_type() -> Result<()> {- let observed = return_type(&AggregateFunction::Stddev, &[DataType::Float32])?;+ let observed = AggregateFunction::Stddev.return_type(&[DataType::Float32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Stddev, &[DataType::Float64])?;+ let observed = AggregateFunction::Stddev.return_type(&[DataType::Float64])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Stddev, &[DataType::Int32])?;+ let observed = AggregateFunction::Stddev.return_type(&[DataType::Int32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Stddev, &[DataType::UInt32])?;+ let observed = AggregateFunction::Stddev.return_type(&[DataType::UInt32])?;assert_eq!(DataType::Float64, observed);- let observed = return_type(&AggregateFunction::Stddev, &[DataType::Int64])?;+ let observed = AggregateFunction::Stddev.return_type(&[DataType::Int64])?;assert_eq!(DataType::Float64, observed);Ok(())@@ -1198,7 +1196,7 @@ mod tests {#[test]fn test_stddev_no_utf8() {- let observed = return_type(&AggregateFunction::Stddev, &[DataType::Utf8]);+ let observed = AggregateFunction::Stddev.return_type(&[DataType::Utf8]);assert!(observed.is_err());}@@ -1212,12 +1210,8 @@ mod tests {name: impl Into<String>,) -> Result<Arc<dyn AggregateExpr>> {let name = name.into();- let coerced_phy_exprs = coerce_exprs_for_test(- fun,- input_phy_exprs,- input_schema,- &aggregate_function::signature(fun),- )?;+ let coerced_phy_exprs =+ coerce_exprs_for_test(fun, input_phy_exprs, input_schema, &fun.signature())?;if coerced_phy_exprs.is_empty() {return Err(DataFusionError::Plan(format!("Invalid or wrong number of arguments passed to aggregate: '{name}'",