Skip to content

Commit 4f49e3e

Browse files
marvinlanhenkealamb
authored andcommitted
Consider timezones with UTC and +00:00 to be the same (apache#10960)
* feat: add temporal_coercion check * fix: add return stmt * chore: add slts * fix: remove println * Update datafusion/expr/src/type_coercion/binary.rs --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent d4228fe commit 4f49e3e

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

datafusion/expr/src/type_coercion/binary.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,12 +1050,16 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataTyp
10501050
}
10511051
(Timestamp(lhs_unit, lhs_tz), Timestamp(rhs_unit, rhs_tz)) => {
10521052
let tz = match (lhs_tz, rhs_tz) {
1053-
// can't cast across timezones
10541053
(Some(lhs_tz), Some(rhs_tz)) => {
1055-
if lhs_tz != rhs_tz {
1056-
return None;
1057-
} else {
1058-
Some(lhs_tz.clone())
1054+
match (lhs_tz.as_ref(), rhs_tz.as_ref()) {
1055+
// UTC and "+00:00" are the same by definition. Most other timezones
1056+
// do not have a 1-1 mapping between timezone and an offset from UTC
1057+
("UTC", "+00:00") | ("+00:00", "UTC") => Some(lhs_tz.clone()),
1058+
(lhs, rhs) if lhs == rhs => Some(lhs_tz.clone()),
1059+
// can't cast across timezones
1060+
_ => {
1061+
return None;
1062+
}
10591063
}
10601064
}
10611065
(Some(lhs_tz), None) => Some(lhs_tz.clone()),

datafusion/sqllogictest/test_files/timestamps.slt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,3 +2801,26 @@ query B
28012801
select current_time = current_time;
28022802
----
28032803
true
2804+
2805+
# Test temporal coercion for UTC
2806+
query ?
2807+
select arrow_cast('2024-06-17T11:00:00', 'Timestamp(Nanosecond, Some("UTC"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("UTC"))');
2808+
----
2809+
0 days -1 hours 0 mins 0.000000 secs
2810+
2811+
query ?
2812+
select arrow_cast('2024-06-17T13:00:00', 'Timestamp(Nanosecond, Some("+00:00"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("UTC"))');
2813+
----
2814+
0 days 1 hours 0 mins 0.000000 secs
2815+
2816+
query ?
2817+
select arrow_cast('2024-06-17T13:00:00', 'Timestamp(Nanosecond, Some("UTC"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("+00:00"))');
2818+
----
2819+
0 days 1 hours 0 mins 0.000000 secs
2820+
2821+
# not supported: coercion across timezones
2822+
query error
2823+
select arrow_cast('2024-06-17T13:00:00', 'Timestamp(Nanosecond, Some("UTC"))') - arrow_cast('2024-06-17T12:00:00', 'Timestamp(Microsecond, Some("+01:00"))');
2824+
2825+
query error
2826+
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"))');

0 commit comments

Comments
 (0)