Skip to content

Commit

Permalink
Consider timezones with UTC and +00:00 to be the same (apache#10960)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and appletreeisyellow committed Jun 24, 2024
1 parent b26f680 commit 21c0f6e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
14 changes: 9 additions & 5 deletions datafusion/expr/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
23 changes: 23 additions & 0 deletions datafusion/sqllogictest/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))');

0 comments on commit 21c0f6e

Please sign in to comment.