From d21ec2c0012442d4d11ba117e4be12c968e0e41d Mon Sep 17 00:00:00 2001 From: marvinlanhenke Date: Tue, 18 Jun 2024 18:33:07 +0200 Subject: [PATCH 1/5] feat: add temporal_coercion check --- datafusion/expr/src/type_coercion/binary.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs index 615bb3ac568c2..c30247f127da8 100644 --- a/datafusion/expr/src/type_coercion/binary.rs +++ b/datafusion/expr/src/type_coercion/binary.rs @@ -1050,12 +1050,12 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option { let tz = match (lhs_tz, rhs_tz) { - // can't cast across timezones (Some(lhs_tz), Some(rhs_tz)) => { - if lhs_tz != rhs_tz { - return None; - } else { - Some(lhs_tz.clone()) + match (lhs_tz.as_ref(), rhs_tz.as_ref()) { + ("UTC", "+00:00") | ("+00:00", "UTC") => Some(lhs_tz.clone()), + (lhs, rhs) if lhs == rhs => Some(lhs_tz.clone()), + // can't cast across timezones + _ => None, } } (Some(lhs_tz), None) => Some(lhs_tz.clone()), From 93b9258fa2bd7e883dbb1397e0ca122999037f3a Mon Sep 17 00:00:00 2001 From: marvinlanhenke Date: Tue, 18 Jun 2024 20:50:32 +0200 Subject: [PATCH 2/5] fix: add return stmt --- datafusion/expr/src/type_coercion/binary.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs index c30247f127da8..f6e1587e6a153 100644 --- a/datafusion/expr/src/type_coercion/binary.rs +++ b/datafusion/expr/src/type_coercion/binary.rs @@ -1033,6 +1033,8 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option Some(Interval(MonthDayNano)), (Date64, Date32) | (Date32, Date64) => Some(Date64), @@ -1055,7 +1057,9 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option Some(lhs_tz.clone()), (lhs, rhs) if lhs == rhs => Some(lhs_tz.clone()), // can't cast across timezones - _ => None, + _ => { + return None; + } } } (Some(lhs_tz), None) => Some(lhs_tz.clone()), From c1bb60da44dd2c7623d151ebd280ac7c7737905e Mon Sep 17 00:00:00 2001 From: marvinlanhenke Date: Tue, 18 Jun 2024 20:51:37 +0200 Subject: [PATCH 3/5] chore: add slts --- .../sqllogictest/test_files/timestamps.slt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/datafusion/sqllogictest/test_files/timestamps.slt b/datafusion/sqllogictest/test_files/timestamps.slt index 7d5d601bbfdd3..96d846d449e1e 100644 --- a/datafusion/sqllogictest/test_files/timestamps.slt +++ b/datafusion/sqllogictest/test_files/timestamps.slt @@ -2801,3 +2801,26 @@ query B select current_time = current_time; ---- true + +# Test temporal coercion for UTC +query ? +select arrow_cast('2024-06-17T11:00:00', 'Timestamp(Nanosecond, Some("UTC"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("UTC"))'); +---- +0 days -1 hours 0 mins 0.000000 secs + +query ? +select arrow_cast('2024-06-17T13:00:00', 'Timestamp(Nanosecond, Some("+00:00"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("UTC"))'); +---- +0 days 1 hours 0 mins 0.000000 secs + +query ? +select arrow_cast('2024-06-17T13:00:00', 'Timestamp(Nanosecond, Some("UTC"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("+00:00"))'); +---- +0 days 1 hours 0 mins 0.000000 secs + +# not supported: coercion across timezones +query error +select arrow_cast('2024-06-17T13:00:00', 'Timestamp(Nanosecond, Some("UTC"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("+01:00"))'); + +query error +select arrow_cast('2024-06-17T13:00:00', 'Timestamp(Nanosecond, Some("+00:00"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("+01:00"))'); From fb57495aa1b977ca4d45fbff617e1221e3e761ac Mon Sep 17 00:00:00 2001 From: marvinlanhenke Date: Tue, 18 Jun 2024 20:58:43 +0200 Subject: [PATCH 4/5] fix: remove println --- datafusion/expr/src/type_coercion/binary.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs index f6e1587e6a153..d93b362c6bfe1 100644 --- a/datafusion/expr/src/type_coercion/binary.rs +++ b/datafusion/expr/src/type_coercion/binary.rs @@ -1033,8 +1033,6 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option Some(Interval(MonthDayNano)), (Date64, Date32) | (Date32, Date64) => Some(Date64), From c3169e57556ec7c5f281421b8725954a6231ae9c Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Thu, 20 Jun 2024 11:42:51 -0400 Subject: [PATCH 5/5] Update datafusion/expr/src/type_coercion/binary.rs --- datafusion/expr/src/type_coercion/binary.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs index d93b362c6bfe1..ea9d0c2fe72ec 100644 --- a/datafusion/expr/src/type_coercion/binary.rs +++ b/datafusion/expr/src/type_coercion/binary.rs @@ -1052,6 +1052,8 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option { match (lhs_tz.as_ref(), rhs_tz.as_ref()) { + // UTC and "+00:00" are the same by definition. Most other timezones + // do not have a 1-1 mapping between timezone and an offset from UTC ("UTC", "+00:00") | ("+00:00", "UTC") => Some(lhs_tz.clone()), (lhs, rhs) if lhs == rhs => Some(lhs_tz.clone()), // can't cast across timezones