Apache DataFusion SQL Query Engine
refactor: use get_result_type to replace binary_operator_data_type. (#6241)
jakevind3c76c4a9add
diff --git a/datafusion/expr/src/expr_schema.rs b/datafusion/expr/src/expr_schema.rsindex 4e08860cce..ba37cf6d45 100644--- a/datafusion/expr/src/expr_schema.rs+++ b/datafusion/expr/src/expr_schema.rs@@ -20,7 +20,7 @@ use crate::expr::{AggregateFunction, BinaryExpr, Cast, GetIndexedField, Sort, TryCast, WindowFunction,};use crate::field_util::get_indexed_field;-use crate::type_coercion::binary::binary_operator_data_type;+use crate::type_coercion::binary::get_result_type;use crate::type_coercion::other::get_coerce_type_for_case_expression;use crate::{aggregate_function, function, window_function, LogicalPlan, Projection, Subquery,@@ -149,11 +149,7 @@ impl ExprSchemable for Expr {ref left,ref right,ref op,- }) => binary_operator_data_type(- &left.get_type(schema)?,- op,- &right.get_type(schema)?,- ),+ }) => get_result_type(&left.get_type(schema)?, op, &right.get_type(schema)?),Expr::Like { .. } | Expr::ILike { .. } | Expr::SimilarTo { .. } => {Ok(DataType::Boolean)}diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rsindex e4d09aba01..006b56c7fe 100644--- a/datafusion/expr/src/type_coercion/binary.rs+++ b/datafusion/expr/src/type_coercion/binary.rs@@ -30,88 +30,27 @@ use crate::type_coercion::{};use crate::Operator;-/// Returns the return type of a binary operator or an error when the binary operator cannot-/// perform the computation between the argument's types, even after type coercion.-///-/// This function makes some assumptions about the underlying available computations.-pub fn binary_operator_data_type(+/// returns the resulting type of a binary expression evaluating the `op` with the left and right hand types+pub fn get_result_type(lhs_type: &DataType,op: &Operator,rhs_type: &DataType,) -> Result<DataType> {- let result_type = if !any_decimal(lhs_type, rhs_type) {- // validate that it is possible to perform the operation on incoming types.- // (or the return datatype cannot be inferred)- get_result_type(lhs_type, op, rhs_type)?- } else {+ if op.is_numerical_operators() && any_decimal(lhs_type, rhs_type) {let (coerced_lhs_type, coerced_rhs_type) =math_decimal_coercion(lhs_type, rhs_type);- let lhs_type = if let Some(lhs_type) = coerced_lhs_type {- lhs_type- } else {- lhs_type.clone()- };- let rhs_type = if let Some(rhs_type) = coerced_rhs_type {- rhs_type- } else {- rhs_type.clone()- };-- match op {- Operator::Plus- | Operator::Minus- | Operator::Divide- | Operator::Multiply- | Operator::Modulo => decimal_op_mathematics_type(op, &lhs_type, &rhs_type)- .or_else(|| coerce_types(&lhs_type, op, &rhs_type).ok())- .ok_or_else(|| {- DataFusionError::Internal(format!(- "Could not get return type for {op:?} between {lhs_type:?} and {rhs_type:?}"- ))- })?,- _ => coerce_types(&lhs_type, op, &rhs_type)?,- }- };- match op {- // operators that return a boolean- Operator::Eq- | Operator::NotEq- | Operator::And- | Operator::Or- | Operator::Lt- | Operator::Gt- | Operator::GtEq- | Operator::LtEq- | Operator::RegexMatch- | Operator::RegexIMatch- | Operator::RegexNotMatch- | Operator::RegexNotIMatch- | Operator::IsDistinctFrom- | Operator::IsNotDistinctFrom => Ok(DataType::Boolean),- // bitwise operations return the common coerced type- Operator::BitwiseAnd- | Operator::BitwiseOr- | Operator::BitwiseXor- | Operator::BitwiseShiftLeft- | Operator::BitwiseShiftRight => Ok(result_type),- // math operations return the same value as the common coerced type- Operator::Plus- | Operator::Minus- | Operator::Divide- | Operator::Multiply- | Operator::Modulo => Ok(result_type),- // string operations return the same values as the common coerced type- Operator::StringConcat => Ok(result_type),- }-}+ let lhs_type = coerced_lhs_type.unwrap_or(lhs_type.clone());+ let rhs_type = coerced_rhs_type.unwrap_or(rhs_type.clone());-/// returns the resulting type of a binary expression evaluating the `op` with the left and right hand types-pub fn get_result_type(- lhs_type: &DataType,- op: &Operator,- rhs_type: &DataType,-) -> Result<DataType> {+ if op.is_numerical_operators() {+ if let Some(result_type) =+ decimal_op_mathematics_type(op, &lhs_type, &rhs_type)+ {+ return Ok(result_type);+ }+ }+ }let result = match op {Operator::And| Operator::Or@@ -121,6 +60,10 @@ pub fn get_result_type(| Operator::Gt| Operator::GtEq| Operator::LtEq+ | Operator::RegexMatch+ | Operator::RegexIMatch+ | Operator::RegexNotMatch+ | Operator::RegexNotIMatch| Operator::IsDistinctFrom| Operator::IsNotDistinctFrom => Some(DataType::Boolean),Operator::Plus | Operator::Minus@@ -131,29 +74,23 @@ pub fn get_result_type({temporal_add_sub_coercion(lhs_type, rhs_type, op)}+ // following same with `coerce_types`Operator::BitwiseAnd| Operator::BitwiseOr| Operator::BitwiseXor| Operator::BitwiseShiftRight- | Operator::BitwiseShiftLeft- | Operator::Plus+ | Operator::BitwiseShiftLeft => bitwise_coercion(lhs_type, rhs_type),+ Operator::Plus| Operator::Minus| Operator::Modulo| Operator::Divide- | Operator::Multiply- | Operator::RegexMatch- | Operator::RegexIMatch- | Operator::RegexNotMatch- | Operator::RegexNotIMatch- | Operator::StringConcat => coerce_types(lhs_type, op, rhs_type).ok(),+ | Operator::Multiply => mathematics_numerical_coercion(lhs_type, rhs_type),+ Operator::StringConcat => string_concat_coercion(lhs_type, rhs_type),};- match result {- None => Err(DataFusionError::Plan(format!(- "Unsupported argument types. Can not evaluate {lhs_type:?} {op} {rhs_type:?}"- ))),- Some(t) => Ok(t),- }+ result.ok_or(DataFusionError::Plan(format!(+ "Unsupported argument types. Can not evaluate {lhs_type:?} {op} {rhs_type:?}"+ )))}/// Coercion rules for all binary operators. Returns the 'coerce_types'diff --git a/datafusion/physical-expr/src/expressions/binary.rs b/datafusion/physical-expr/src/expressions/binary.rsindex 5ca7b9ddf2..d99c147202 100644--- a/datafusion/physical-expr/src/expressions/binary.rs+++ b/datafusion/physical-expr/src/expressions/binary.rs@@ -98,7 +98,7 @@ use datafusion_common::scalar::*;use datafusion_common::ScalarValue;use datafusion_common::{DataFusionError, Result};use datafusion_expr::type_coercion::binary::{- binary_operator_data_type, coercion_decimal_mathematics_type,+ coercion_decimal_mathematics_type, get_result_type,};use datafusion_expr::{ColumnarValue, Operator};@@ -651,7 +651,7 @@ impl PhysicalExpr for BinaryExpr {}fn data_type(&self, input_schema: &Schema) -> Result<DataType> {- binary_operator_data_type(+ get_result_type(&self.left.data_type(input_schema)?,&self.op,&self.right.data_type(input_schema)?,