Skip to content

Commit

Permalink
feat(7849): coerce TIMESTAMP to TIMESTAMPTZ (#7850)
Browse files Browse the repository at this point in the history
* feat(7849): coerce TIMESTAMP to TIMESTAMPTZ

Add coercion rules to support coercion from timestamp types without
a timezone to a timestamp with a timezone. Like with the coercion
of strings or numeric constants, when a function requires the
TIMEZONE_WILDCARD placeholder timezone "+00" will be used as the
timezone offset.

* review comments

Add additional SQL logic test suggested in review.

* more review suggestions

Add a positive test case for TIMESTAMPTZ and TIMESTAMP comparison.
  • Loading branch information
mhilton authored Oct 18, 2023
1 parent 7acd883 commit 14a8681
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn coerced_from<'a>(
Timestamp(_, Some(from_tz)) => {
Some(Timestamp(unit.clone(), Some(from_tz.clone())))
}
Null | Date32 | Utf8 | LargeUtf8 => {
Null | Date32 | Utf8 | LargeUtf8 | Timestamp(_, None) => {
// In the absence of any other information assume the time zone is "+00" (UTC).
Some(Timestamp(unit.clone(), Some("+00".into())))
}
Expand All @@ -238,7 +238,7 @@ fn coerced_from<'a>(
Timestamp(_, Some(_))
if matches!(
type_from,
Null | Timestamp(_, Some(_)) | Date32 | Utf8 | LargeUtf8
Null | Timestamp(_, _) | Date32 | Utf8 | LargeUtf8
) =>
{
Some(type_into.clone())
Expand Down
18 changes: 18 additions & 0 deletions datafusion/sqllogictest/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,12 @@ SELECT date_bin('1 day', TIMESTAMPTZ '2022-01-01 20:10:00Z', TIMESTAMPTZ '2020-0
----
2022-01-02T00:00:00+07:00

# coerce TIMESTAMP to TIMESTAMPTZ
query P
SELECT date_bin('1 day', TIMESTAMPTZ '2022-01-01 20:10:00Z', TIMESTAMP '2020-01-01')
----
2022-01-01T07:00:00+07:00

# postgresql: 1
query R
SELECT date_part('hour', TIMESTAMPTZ '2000-01-01T01:01:01') as part
Expand Down Expand Up @@ -1758,3 +1764,15 @@ query T
SELECT arrow_typeof(date_bin(INTERVAL '1 day', time, '1970-01-01T00:00:00+05:00')) FROM foo LIMIT 1
----
Timestamp(Nanosecond, Some("+05:00"))


# timestamp comparison with and without timezone
query B
SELECT TIMESTAMPTZ '2022-01-01 20:10:00Z' = TIMESTAMP '2020-01-01'
----
false

query B
SELECT TIMESTAMPTZ '2020-01-01 00:00:00Z' = TIMESTAMP '2020-01-01'
----
true

0 comments on commit 14a8681

Please sign in to comment.