You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/06/22 17:52:44 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6748: Minor: Encapsulate `return_type` and `signature` in `AggregateFunction` and `WindowFunction`

alamb commented on code in PR #6748:
URL: https://github.com/apache/arrow-datafusion/pull/6748#discussion_r1238857253


##########
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 {

Review Comment:
   This code is now indented one level more



##########
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 {

Review Comment:
   This code is now indented one level more -- there are no logical changes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org