Skip to content

Apache DataFusion SQL Query Engine

Consider timezones with `UTC` and `+00:00` to be the same (#10960)

Marvin Lanhenke4a0c7f35a02e
diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs
index 615bb3ac56..ea9d0c2fe7 100644
--- a/datafusion/expr/src/type_coercion/binary.rs
+++ b/datafusion/expr/src/type_coercion/binary.rs
@@ -1050,12 +1050,16 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataTyp
}
(Timestamp(lhs_unit, lhs_tz), Timestamp(rhs_unit, rhs_tz)) => {
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 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
+ _ => {
+ return None;
+ }
}
}
(Some(lhs_tz), None) => Some(lhs_tz.clone()),
diff --git a/datafusion/sqllogictest/test_files/timestamps.slt b/datafusion/sqllogictest/test_files/timestamps.slt
index 7d5d601bbf..96d846d449 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"))');