|
18 | 18 | //! Optimizer rule for type validation and coercion |
19 | 19 |
|
20 | 20 | use crate::{OptimizerConfig, OptimizerRule}; |
21 | | -use arrow::datatypes::DataType; |
22 | 21 | use datafusion_common::{DFSchema, DFSchemaRef, Result}; |
23 | 22 | use datafusion_expr::binary_rule::coerce_types; |
24 | 23 | use datafusion_expr::expr_rewriter::{ExprRewritable, ExprRewriter, RewriteRecursion}; |
@@ -86,37 +85,18 @@ impl ExprRewriter for TypeCoercionRewriter { |
86 | 85 | } |
87 | 86 |
|
88 | 87 | fn mutate(&mut self, expr: Expr) -> Result<Expr> { |
89 | | - match &expr { |
| 88 | + match expr { |
90 | 89 | Expr::BinaryExpr { left, op, right } => { |
91 | 90 | let left_type = left.get_type(&self.schema)?; |
92 | 91 | let right_type = right.get_type(&self.schema)?; |
93 | | - match right_type { |
94 | | - DataType::Interval(_) => { |
95 | | - // we don't want to cast intervals because that breaks |
96 | | - // the logic in the physical planner |
97 | | - Ok(expr) |
98 | | - } |
99 | | - _ => { |
100 | | - let coerced_type = coerce_types(&left_type, op, &right_type)?; |
101 | | - let left = left.clone().cast_to(&coerced_type, &self.schema)?; |
102 | | - let right = right.clone().cast_to(&coerced_type, &self.schema)?; |
103 | | - match (&left, &right) { |
104 | | - (Expr::Cast { .. }, _) | (_, Expr::Cast { .. }) => { |
105 | | - Ok(Expr::BinaryExpr { |
106 | | - left: Box::new(left), |
107 | | - op: *op, |
108 | | - right: Box::new(right), |
109 | | - }) |
110 | | - } |
111 | | - _ => { |
112 | | - // no cast was added so we return the original expression |
113 | | - Ok(expr) |
114 | | - } |
115 | | - } |
116 | | - } |
117 | | - } |
| 92 | + let coerced_type = coerce_types(&left_type, &op, &right_type)?; |
| 93 | + Ok(Expr::BinaryExpr { |
| 94 | + left: Box::new(left.cast_to(&coerced_type, &self.schema)?), |
| 95 | + op, |
| 96 | + right: Box::new(right.cast_to(&coerced_type, &self.schema)?), |
| 97 | + }) |
118 | 98 | } |
119 | | - _ => Ok(expr), |
| 99 | + expr => Ok(expr), |
120 | 100 | } |
121 | 101 | } |
122 | 102 | } |
|
0 commit comments