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


##########
datafusion/optimizer/src/type_coercion.rs:
##########
@@ -54,17 +54,19 @@ impl OptimizerRule for TypeCoercion {
             .map(|p| self.optimize(p, optimizer_config))
             .collect::<Result<Vec<_>>>()?;
 
-        let schema = match new_inputs.len() {
-            1 => new_inputs[0].schema().clone(),
-            2 => DFSchemaRef::new(build_join_schema(
-                new_inputs[0].schema(),
-                new_inputs[1].schema(),
-                &JoinType::Inner,
-            )?),
-            _ => DFSchemaRef::new(DFSchema::empty()),
-        };
+        // get schema representing all available input fields. This is used 
for data type

Review Comment:
   👍 



##########
datafusion/optimizer/src/type_coercion.rs:
##########
@@ -87,14 +89,51 @@ impl ExprRewriter for TypeCoercionRewriter {
 
     fn mutate(&mut self, expr: Expr) -> Result<Expr> {
         match expr {
-            Expr::BinaryExpr { left, op, right } => {
+            Expr::BinaryExpr {
+                ref left,
+                op,
+                ref right,
+            } => {
                 let left_type = left.get_type(&self.schema)?;
                 let right_type = right.get_type(&self.schema)?;
-                let coerced_type = coerce_types(&left_type, &op, &right_type)?;
-                Ok(Expr::BinaryExpr {
-                    left: Box::new(left.cast_to(&coerced_type, &self.schema)?),
-                    op,
-                    right: Box::new(right.cast_to(&coerced_type, 
&self.schema)?),
+                match (&left_type, &right_type) {
+                    (
+                        DataType::Date32 | DataType::Date64 | 
DataType::Timestamp(_, _),
+                        &DataType::Interval(_),
+                    ) => {
+                        // Arrow `can_cast_types` says we cannot cast an 
Interval to
+                        // Date32/Date64/Timestamp, which contradicts 
DataFusion's `coerce_types`
+                        Ok(expr.clone())
+                    }
+                    _ => {
+                        let coerced_type = coerce_types(&left_type, &op, 
&right_type)?;
+                        Ok(Expr::BinaryExpr {
+                            left: Box::new(
+                                left.clone().cast_to(&coerced_type, 
&self.schema)?,
+                            ),
+                            op,
+                            right: Box::new(
+                                right.clone().cast_to(&coerced_type, 
&self.schema)?,
+                            ),
+                        })
+                    }
+                }
+            }
+            Expr::Between {
+                expr,
+                negated,
+                low,
+                high,
+            } => {
+                let expr_type = expr.get_type(&self.schema)?;
+                let low_type = low.get_type(&self.schema)?;
+                let coerced_type = comparison_coercion(&expr_type, &low_type)
+                    .ok_or_else(|| DataFusionError::Internal("".to_string()))?;

Review Comment:
   Why the empty error message? That seems like it may be confusing



##########
datafusion/expr/src/binary_rule.rs:
##########
@@ -516,6 +516,8 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: 
&DataType) -> Option<DataTyp
     use arrow::datatypes::DataType::*;
     use arrow::datatypes::TimeUnit;
     match (lhs_type, rhs_type) {
+        (Date64, Date32) => Some(Date64),

Review Comment:
   🤔  I think coercing from Date32 -> Date64 is probably fine, but going the 
other way loses precision, right?



##########
datafusion/optimizer/src/type_coercion.rs:
##########
@@ -244,6 +283,34 @@ mod test {
         Ok(())
     }
 
+    #[test]
+    fn binary_op_date32_add_interval() -> Result<()> {
+        //CAST(Utf8("1998-03-18") AS Date32) + IntervalDayTime("386547056640")

Review Comment:
   ❤️ 



##########
datafusion/optimizer/src/type_coercion.rs:
##########
@@ -87,14 +89,51 @@ impl ExprRewriter for TypeCoercionRewriter {
 
     fn mutate(&mut self, expr: Expr) -> Result<Expr> {
         match expr {
-            Expr::BinaryExpr { left, op, right } => {
+            Expr::BinaryExpr {
+                ref left,
+                op,
+                ref right,
+            } => {
                 let left_type = left.get_type(&self.schema)?;
                 let right_type = right.get_type(&self.schema)?;
-                let coerced_type = coerce_types(&left_type, &op, &right_type)?;
-                Ok(Expr::BinaryExpr {
-                    left: Box::new(left.cast_to(&coerced_type, &self.schema)?),
-                    op,
-                    right: Box::new(right.cast_to(&coerced_type, 
&self.schema)?),
+                match (&left_type, &right_type) {
+                    (
+                        DataType::Date32 | DataType::Date64 | 
DataType::Timestamp(_, _),
+                        &DataType::Interval(_),
+                    ) => {
+                        // Arrow `can_cast_types` says we cannot cast an 
Interval to

Review Comment:
   I don't think it makes sense to cast an interval to a specific point in time 
-- aka I think DataFusion's coerce_types is incorrect



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to