This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 5bd2593c52 test: add Between UT for type_coercion (#5929)
5bd2593c52 is described below

commit 5bd2593c52d5d3c6f09af4380ec40aa531d3166f
Author: jakevin <[email protected]>
AuthorDate: Wed Apr 12 03:24:42 2023 +0800

    test: add Between UT for type_coercion (#5929)
---
 datafusion/optimizer/src/analyzer/type_coercion.rs | 59 ++++++++++++++++++++--
 1 file changed, 54 insertions(+), 5 deletions(-)

diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs 
b/datafusion/optimizer/src/analyzer/type_coercion.rs
index 6e11907cd1..11228ee8ec 100644
--- a/datafusion/optimizer/src/analyzer/type_coercion.rs
+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs
@@ -729,13 +729,14 @@ mod test {
     use arrow::datatypes::{DataType, TimeUnit};
 
     use datafusion_common::tree_node::TreeNode;
+    use datafusion_common::ScalarValue::Utf8;
     use datafusion_common::{DFField, DFSchema, DFSchemaRef, Result, 
ScalarValue};
     use datafusion_expr::expr::{self, Like};
     use datafusion_expr::{
         cast, col, concat, concat_ws, create_udaf, is_true,
-        AccumulatorFunctionImplementation, AggregateFunction, AggregateUDF, 
BinaryExpr,
-        BuiltinScalarFunction, Case, ColumnarValue, ExprSchemable, Filter, 
Operator,
-        StateTypeFunction, Subquery,
+        AccumulatorFunctionImplementation, AggregateFunction, AggregateUDF, 
Between,
+        BinaryExpr, BuiltinScalarFunction, Case, Cast, ColumnarValue, 
ExprSchemable,
+        Filter, Operator, StateTypeFunction, Subquery,
     };
     use datafusion_expr::{
         lit,
@@ -1007,8 +1008,56 @@ mod test {
         let expected =
             "Projection: CAST(a AS Decimal128(24, 4)) IN ([CAST(Int32(1) AS 
Decimal128(24, 4)), CAST(Int8(4) AS Decimal128(24, 4)), CAST(Int64(8) AS 
Decimal128(24, 4))]) AS a IN (Map { iter: Iter([Int32(1), Int8(4), Int64(8)]) 
})\
              \n  EmptyRelation";
-        assert_analyzed_plan_eq(Arc::new(TypeCoercion::new()), &plan, 
expected)?;
-        Ok(())
+        assert_analyzed_plan_eq(Arc::new(TypeCoercion::new()), &plan, expected)
+    }
+
+    #[test]
+    fn between_case() -> Result<()> {
+        let expr = Expr::Between(Between::new(
+            Box::new(col("a")),
+            false,
+            Box::new(Expr::Literal(Utf8(Some("2002-05-08".to_string())))),
+            // (cast('2002-05-08' as date) + interval '1 months')
+            Box::new(Expr::BinaryExpr(BinaryExpr {
+                left: Box::new(Expr::Cast(Cast {
+                    expr: 
Box::new(Expr::Literal(Utf8(Some("2002-05-08".to_string())))),
+                    data_type: DataType::Date32,
+                })),
+                op: Operator::Plus,
+                right: 
Box::new(Expr::Literal(ScalarValue::IntervalYearMonth(Some(1)))),
+            })),
+        ));
+        let empty = empty_with_type(DataType::Utf8);
+        let plan = LogicalPlan::Filter(Filter::try_new(expr, empty)?);
+        let expected =
+            "Filter: a BETWEEN Utf8(\"2002-05-08\") AND 
CAST(CAST(Utf8(\"2002-05-08\") AS Date32) + IntervalYearMonth(\"1\") AS Utf8)\
+            \n  EmptyRelation";
+        assert_analyzed_plan_eq(Arc::new(TypeCoercion::new()), &plan, expected)
+    }
+
+    #[test]
+    fn between_infer_cheap_type() -> Result<()> {
+        let expr = Expr::Between(Between::new(
+            Box::new(col("a")),
+            false,
+            // (cast('2002-05-08' as date) + interval '1 months')
+            Box::new(Expr::BinaryExpr(BinaryExpr {
+                left: Box::new(Expr::Cast(Cast {
+                    expr: 
Box::new(Expr::Literal(Utf8(Some("2002-05-08".to_string())))),
+                    data_type: DataType::Date32,
+                })),
+                op: Operator::Plus,
+                right: 
Box::new(Expr::Literal(ScalarValue::IntervalYearMonth(Some(1)))),
+            })),
+            Box::new(Expr::Literal(Utf8(Some("2002-12-08".to_string())))),
+        ));
+        let empty = empty_with_type(DataType::Utf8);
+        let plan = LogicalPlan::Filter(Filter::try_new(expr, empty)?);
+        // TODO: we should cast col(a).
+        let expected =
+            "Filter: CAST(a AS Date32) BETWEEN CAST(Utf8(\"2002-05-08\") AS 
Date32) + IntervalYearMonth(\"1\") AND CAST(Utf8(\"2002-12-08\") AS Date32)\
+            \n  EmptyRelation";
+        assert_analyzed_plan_eq(Arc::new(TypeCoercion::new()), &plan, expected)
     }
 
     #[test]

Reply via email to