martin-g commented on code in PR #19460:
URL: https://github.com/apache/datafusion/pull/19460#discussion_r2642248390


##########
datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt:
##########
@@ -0,0 +1,27 @@
+# time + interval → time
+# Add an interval to a time
+# time '01:00' + interval '3 hours' → 04:00:00
+
+query ?
+SELECT '01:00'::time + interval '3 hours'

Review Comment:
   Please add cases for time+interval > 24 hours, e.g. SELECT '22:00'::time + 
interval '3 hours'



##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1810,7 +1886,19 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: 
&DataType) -> Option<DataTyp
         (Interval(_) | Duration(_), Interval(_) | Duration(_)) => {
             Some(Interval(MonthDayNano))
         }
+        (Date32, Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | 
UInt64)
+        | (Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64, 
Date32) => {
+            Some(Date32)
+        }
+        (Date64, Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | 
UInt64)
+        | (Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64, 
Date64) => {
+            Some(Date32)

Review Comment:
   ```suggestion
               Some(Date64)
   ```



##########
datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt:
##########
@@ -0,0 +1,27 @@
+# time + interval → time
+# Add an interval to a time
+# time '01:00' + interval '3 hours' → 04:00:00
+
+query ?
+SELECT '01:00'::time + interval '3 hours'
+----
+4 hours
+
+query T
+SELECT arrow_typeof('01:00'::time + interval '3 hours')
+----
+Interval(MonthDayNano)
+
+# time - interval → time
+# Subtract an interval from a time
+# time '05:00' - interval '2 hours' → 03:00:00
+
+query ?
+SELECT '05:00'::time - interval '2 hours'
+----
+3 hours

Review Comment:
   The comment above says `time - interval → time` but actually the result is 
of type `interval`, not `time`



##########
datafusion/sqllogictest/test_files/datetime/arith_interval_double.slt:
##########
@@ -0,0 +1,31 @@
+# interval * double precision → interval

Review Comment:
   Maybe add a comment that those are not supported yet ?!
   There are only error checks below.



##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -290,17 +290,211 @@ impl<'a> TypeCoercionRewriter<'a> {
         right: Expr,
         right_schema: &DFSchema,
     ) -> Result<(Expr, Expr)> {
-        let (left_type, right_type) = BinaryTypeCoercer::new(
-            &left.get_type(left_schema)?,
+        let left_data_type = left.get_type(left_schema)?;
+        let right_data_type = right.get_type(right_schema)?;
+        let (left_type, right_type) =
+            BinaryTypeCoercer::new(&left_data_type, &op, &right_data_type)
+                .get_input_types()?;
+        let left_cast_ok = can_cast_types(&left_data_type, &left_type);
+        let right_cast_ok = can_cast_types(&right_data_type, &right_type);
+
+        // handle special cases for
+        // * Date +/- int => Date
+        // * Date + time => Timestamp
+        let left_expr = if !left_cast_ok {
+            Self::coerce_date_time_math_op(
+                left,
+                &op,
+                &left_data_type,
+                &left_type,
+                &right_type,
+            )?
+        } else {
+            left.cast_to(&left_type, left_schema)?
+        };
+
+        let right_expr = if !right_cast_ok {
+            Self::coerce_date_time_math_op(
+                right,
+                &op,
+                &right_data_type,
+                &right_type,
+                &left_type,
+            )?
+        } else {
+            right.cast_to(&right_type, right_schema)?
+        };
+
+        Ok((left_expr, right_expr))
+    }
+
+    fn coerce_date_time_math_op(
+        expr: Expr,
+        op: &Operator,
+        left_current_type: &DataType,
+        left_target_type: &DataType,
+        right_target_type: &DataType,
+    ) -> Result<Expr, DataFusionError> {
+        use DataType::*;
+
+        let e = match (
             &op,
-            &right.get_type(right_schema)?,
-        )
-        .get_input_types()?;
+            &left_current_type,
+            &left_target_type,
+            &right_target_type,
+        ) {
+            // int +/- date => date
+            (
+                Operator::Plus | Operator::Minus,
+                Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | 
UInt64,
+                Interval(IntervalUnit::MonthDayNano),
+                Date32 | Date64,
+            ) => {
+                // cast to i64 first
+                let expr = if *left_current_type == Int64 {
+                    expr
+                } else {
+                    Expr::Cast(Cast::new(Box::new(expr), Int64))
+                };
+                // next, multiply by 86400 to get seconds
+                let expr = Expr::BinaryExpr(BinaryExpr::new(
+                    Box::new(expr),
+                    Operator::Multiply,
+                    Box::new(Expr::Literal(ScalarValue::Int64(Some(86400)), 
None)),
+                ));
+                // cast to duration
+                let expr =
+                    Expr::Cast(Cast::new(Box::new(expr), 
Duration(TimeUnit::Second)));
+
+                // finally cast to interval
+                Expr::Cast(Cast::new(
+                    Box::new(expr),
+                    Interval(IntervalUnit::MonthDayNano),
+                ))
+            }
+            // These might seem to be a bit convoluted, however for arrow to 
do date + time arithmetic
+            // date must be cast to Timestamp(Nanosecond) and time cast to 
Duration(Nanosecond)
+            // (they must be the same timeunit).
+            //
+            // For Time32/64 we first need to cast to an Int64, convert that 
to nanoseconds based
+            // on the time unit, then cast that to duration.
+            //
+            // Time + date -> timestamp or
+            (
+                Operator::Plus | Operator::Minus,
+                Time32(_) | Time64(_),
+                Duration(TimeUnit::Nanosecond),
+                Timestamp(TimeUnit::Nanosecond, None),
+            ) => {
+                let expr = match left_current_type {
+                    Time32(TimeUnit::Second) => {
+                        let ex = Expr::Cast(Cast::new(Box::new(expr), Int64));
+                        Expr::BinaryExpr(BinaryExpr::new(
+                            Box::new(ex),
+                            Operator::Multiply,
+                            Box::new(Expr::Literal(
+                                ScalarValue::Int64(Some(1_000_000_000)),
+                                None,
+                            )),
+                        ))
+                    }
+                    Time32(TimeUnit::Millisecond) => {
+                        let ex = Expr::Cast(Cast::new(Box::new(expr), Int64));
+                        Expr::BinaryExpr(BinaryExpr::new(
+                            Box::new(ex),
+                            Operator::Multiply,
+                            Box::new(Expr::Literal(
+                                ScalarValue::Int64(Some(1_000_000)),
+                                None,
+                            )),
+                        ))
+                    }
+                    Time64(TimeUnit::Microsecond) => {
+                        let ex = Expr::Cast(Cast::new(Box::new(expr), Int64));
+                        Expr::BinaryExpr(BinaryExpr::new(
+                            Box::new(ex),
+                            Operator::Multiply,
+                            Box::new(Expr::Literal(
+                                ScalarValue::Int64(Some(1_000)),
+                                None,
+                            )),
+                        ))
+                    }
+                    Time64(TimeUnit::Nanosecond) => {
+                        Expr::Cast(Cast::new(Box::new(expr), Int64))
+                    }
+                    _ => unreachable!(),

Review Comment:
   ```suggestion
                       _ => internal_err!("..."),
   ```
   in case a new time unit is ever added to the enum



##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -290,17 +290,211 @@ impl<'a> TypeCoercionRewriter<'a> {
         right: Expr,
         right_schema: &DFSchema,
     ) -> Result<(Expr, Expr)> {
-        let (left_type, right_type) = BinaryTypeCoercer::new(
-            &left.get_type(left_schema)?,
+        let left_data_type = left.get_type(left_schema)?;
+        let right_data_type = right.get_type(right_schema)?;
+        let (left_type, right_type) =
+            BinaryTypeCoercer::new(&left_data_type, &op, &right_data_type)
+                .get_input_types()?;
+        let left_cast_ok = can_cast_types(&left_data_type, &left_type);
+        let right_cast_ok = can_cast_types(&right_data_type, &right_type);
+
+        // handle special cases for
+        // * Date +/- int => Date
+        // * Date + time => Timestamp
+        let left_expr = if !left_cast_ok {
+            Self::coerce_date_time_math_op(
+                left,
+                &op,
+                &left_data_type,
+                &left_type,
+                &right_type,
+            )?
+        } else {
+            left.cast_to(&left_type, left_schema)?
+        };
+
+        let right_expr = if !right_cast_ok {
+            Self::coerce_date_time_math_op(
+                right,
+                &op,
+                &right_data_type,
+                &right_type,
+                &left_type,
+            )?
+        } else {
+            right.cast_to(&right_type, right_schema)?
+        };
+
+        Ok((left_expr, right_expr))
+    }
+
+    fn coerce_date_time_math_op(
+        expr: Expr,
+        op: &Operator,
+        left_current_type: &DataType,
+        left_target_type: &DataType,
+        right_target_type: &DataType,
+    ) -> Result<Expr, DataFusionError> {
+        use DataType::*;
+
+        let e = match (
             &op,
-            &right.get_type(right_schema)?,
-        )
-        .get_input_types()?;
+            &left_current_type,
+            &left_target_type,
+            &right_target_type,
+        ) {
+            // int +/- date => date
+            (
+                Operator::Plus | Operator::Minus,
+                Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | 
UInt64,
+                Interval(IntervalUnit::MonthDayNano),
+                Date32 | Date64,
+            ) => {
+                // cast to i64 first
+                let expr = if *left_current_type == Int64 {
+                    expr
+                } else {
+                    Expr::Cast(Cast::new(Box::new(expr), Int64))
+                };
+                // next, multiply by 86400 to get seconds
+                let expr = Expr::BinaryExpr(BinaryExpr::new(
+                    Box::new(expr),
+                    Operator::Multiply,
+                    Box::new(Expr::Literal(ScalarValue::Int64(Some(86400)), 
None)),

Review Comment:
   consider extracting a constant, e.g. `const SECONDS_PER_DAY: ... `



##########
datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt:
##########
@@ -0,0 +1,27 @@
+# time + interval → time
+# Add an interval to a time
+# time '01:00' + interval '3 hours' → 04:00:00
+
+query ?
+SELECT '01:00'::time + interval '3 hours'
+----
+4 hours
+
+query T
+SELECT arrow_typeof('01:00'::time + interval '3 hours')
+----
+Interval(MonthDayNano)
+
+# time - interval → time
+# Subtract an interval from a time
+# time '05:00' - interval '2 hours' → 03:00:00
+
+query ?
+SELECT '05:00'::time - interval '2 hours'

Review Comment:
   Please add cases for time-interval < 0 hours, e.g. SELECT '02:00'::time - 
interval '3 hours'



##########
datafusion/sqllogictest/test_files/datetime/arith_timestamp_duration.slt:
##########
@@ -0,0 +1,147 @@
+# timestamp + duration → timestamp
+# Add an duration to a timestamp
+# timestamp '2001-09-28 01:00' + arrow_cast(12345000000000, 
'Duration(Nanosecond)' → 2001-09-29 00:00:00

Review Comment:
   ```suggestion
   # timestamp '2001-09-28 01:00' + arrow_cast(12345000000000, 
'Duration(Nanosecond)') → 2001-09-29 00:00:00
   ```



##########
datafusion/sqllogictest/test_files/datetime/arith_date_interval.slt:
##########
@@ -0,0 +1,27 @@
+# date + interval → timestamp
+# Add an interval to a date
+# date '2001-09-28' + interval '1 hour' → 2001-09-28 01:00:00
+
+query D
+SELECT '2001-09-28'::date + interval '1 hour'
+----
+2001-09-28

Review Comment:
   can this print the time part too ?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to