From d04679b43bb35573f33db0588e070399bd64b51f Mon Sep 17 00:00:00 2001 From: jackwener Date: Thu, 1 Jun 2023 14:14:24 +0800 Subject: [PATCH] feat: type coercion support date - date --- .../core/tests/sqllogictests/test_files/dates.slt | 2 +- datafusion/expr/src/type_coercion/binary.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/datafusion/core/tests/sqllogictests/test_files/dates.slt b/datafusion/core/tests/sqllogictests/test_files/dates.slt index bfeb92fd59603..403403eef2ee7 100644 --- a/datafusion/core/tests/sqllogictests/test_files/dates.slt +++ b/datafusion/core/tests/sqllogictests/test_files/dates.slt @@ -90,7 +90,7 @@ select i_item_desc from test where d3_date > now() + '5 days'; # DATE minus DATE -query error DataFusion error: Error during planning: Unsupported argument types\. Can not evaluate Date32 \- Date32 +query error DataFusion error: Optimizer rule 'simplify_expressions' failed\ncaused by\nArrow error: Cast error: Cannot perform arithmetic operation between array of type Date32 and array of type Date32 SELECT DATE '2023-04-09' - DATE '2023-04-02'; # DATE minus Timestamp diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs index 86c5c15f14465..b668ea81ec5a1 100644 --- a/datafusion/expr/src/type_coercion/binary.rs +++ b/datafusion/expr/src/type_coercion/binary.rs @@ -55,8 +55,7 @@ fn mathematics_temporal_result_type( (Interval(_), Date64) => Some(rhs_type.clone()), (Date64, Interval(_)) => Some(lhs_type.clone()), // interval +/- - (Interval(YearMonth), Interval(YearMonth)) => Some(Interval(YearMonth)), - (Interval(DayTime), Interval(DayTime)) => Some(Interval(DayTime)), + (Interval(l), Interval(h)) if l == h => Some(lhs_type.clone()), (Interval(_), Interval(_)) => Some(Interval(MonthDayNano)), // timestamp - timestamp (Timestamp(Second, _), Timestamp(Second, _)) @@ -68,7 +67,10 @@ fn mathematics_temporal_result_type( Some(Interval(MonthDayNano)) } (Timestamp(_, _), Timestamp(_, _)) => None, - // TODO: date minus date + // date - date + (Date32, Date32) => Some(Interval(DayTime)), + (Date64, Date64) => Some(Interval(MonthDayNano)), + (Date32, Date64) | (Date64, Date32) => Some(Interval(MonthDayNano)), // date - timestamp, timestamp - date (Date32, Timestamp(_, _)) | (Timestamp(_, _), Date32) @@ -738,6 +740,8 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option Some(Interval(YearMonth)), (Interval(DayTime), Interval(DayTime)) => Some(Interval(DayTime)), (Interval(_), Interval(_)) => Some(Interval(MonthDayNano)), + (Date32, Date32) => Some(Date32), + (Date64, Date64) => Some(Date64), (Date64, Date32) | (Date32, Date64) => Some(Date64), (Utf8, Date32) | (Date32, Utf8) => Some(Date32), (Utf8, Date64) | (Date64, Utf8) => Some(Date64),