andygrove commented on code in PR #407:
URL: https://github.com/apache/datafusion-spark/pull/407#discussion_r4711204687
##########
crates/spark/src/function/datetime/to_utc_timestamp.rs:
##########
@@ -32,17 +29,21 @@ use datafusion_macros::user_doc;
description = "Treats `ts` as a wall-clock time in time zone `tz` and
returns the UTC wall-clock time. \
This is the inverse of `from_utc_timestamp`. DataFusion has no
built-in equivalent. A string `ts` is cast to a timestamp, \
so a string with an offset is first converted to UTC. The result has
the same type as `ts`, including any time zone \
- it carries. Returns NULL if either argument is NULL. Unlike
`from_utc_timestamp`, `tz` is parsed by Arrow, \
- so it accepts region IDs and `+HH:MM` offsets but not all of Spark's
spellings (such as `GMT+1` or `PST`). \
- An unknown zone is an error. Within a few hours of a daylight saving
time change, the offset can be taken \
- from the wrong side of the change, so the result can differ from Spark
by the size of the change \
- ([#110](https://github.com/apache/datafusion-spark/issues/110)).",
+ it carries. Returns NULL if either argument is NULL. `tz` accepts the
same spellings as `from_utc_timestamp`: \
+ region IDs, offsets such as `+01:00`, `GMT+1`, `UTC+1:2` and
`UT-010203`, Java short IDs such as `PST`, and legacy \
+ `SystemV/` IDs. An offset beyond 18 hours or an unknown zone is an
error. A time skipped when clocks go forward \
+ moves forward by the length of the gap, and a time that happens twice
when clocks go back takes the earlier offset. \
+ Differences from Spark: `tz` is only parsed for rows where both
arguments are non-NULL, so an invalid constant `tz` \
Review Comment:
`from_utc_timestamp`'s doc also lists "conversion is limited to Chrono's
calendar range" as a difference, and the same applies here. `local_to_utc`
errors for region zones past about year 262143, while Spark 4.2.0 returns
`+270000-01-01 05:00:00` for `to_utc_timestamp(CAST('+270000-01-01 00:00:00' AS
TIMESTAMP_NTZ), 'America/New_York')`. Could you add that item to this list so
it matches `from_utc_timestamp`? You'd need to run
`dev/update_function_docs.sh` again afterwards.
##########
crates/sqllogictest/test_files/datetime/to_utc_timestamp.slt:
##########
@@ -142,16 +142,69 @@ SELECT to_utc_timestamp('2020-11-04T14:06:40'::timestamp,
'America/New_York'::st
----
2020-11-04T19:06:40
-# Near a daylight saving time change the offset is taken from the wrong side
of the change.
-# Spark returns 2024-03-10T07:30:00 and 2024-11-03T07:30:00
(https://github.com/apache/datafusion-spark/issues/110).
-# xfail spark: to_utc_timestamp takes the offset from the wrong side of a DST
change (https://github.com/apache/datafusion-spark/issues/110)
+# Near a daylight saving time change the offset comes from the local time. A
time in the gap
+# moves forward by the length of the gap, and a time in the overlap takes the
earlier offset.
query P
SELECT to_utc_timestamp('2024-03-10T03:30:00'::timestamp, 'America/New_York');
----
-2024-03-10T08:30:00
+2024-03-10T07:30:00
-# xfail spark: to_utc_timestamp takes the offset from the wrong side of a DST
change (https://github.com/apache/datafusion-spark/issues/110)
query P
SELECT to_utc_timestamp('2024-11-03T02:30:00'::timestamp, 'America/New_York');
----
-2024-11-03T06:30:00
+2024-11-03T07:30:00
+
+query P
+SELECT to_utc_timestamp('2024-03-10T05:00:00'::timestamp,
'America/Los_Angeles'::string);
+----
+2024-03-10T12:00:00
+
+query P
+SELECT to_utc_timestamp(column1, column2)
+FROM VALUES
+('2024-03-10T01:59:59'::timestamp, 'America/New_York'::string),
+('2024-03-10T02:30:00'::timestamp, 'America/New_York'::string),
+('2024-03-10T03:00:00'::timestamp, 'America/New_York'::string),
+('2024-11-03T00:59:59'::timestamp, 'America/New_York'::string),
+('2024-11-03T01:30:00'::timestamp, 'America/New_York'::string),
+('2024-11-03T02:00:00'::timestamp, 'America/New_York'::string);
+----
+2024-03-10T06:59:59
+2024-03-10T07:30:00
+2024-03-10T07:00:00
+2024-11-03T04:59:59
+2024-11-03T05:30:00
+2024-11-03T07:00:00
+
+# Spark timezone spellings, including second-resolution offsets.
+query P
+SELECT to_utc_timestamp('2024-01-15T12:00:00'::timestamp, 'GMT+1'::string);
+----
+2024-01-15T11:00:00
+
+query P
+SELECT to_utc_timestamp(column1, column2)
+FROM VALUES
+('2024-07-01T00:00:00.123456'::timestamp, 'PST'::string),
+('2024-07-01T00:00:00.123456'::timestamp, 'GMT+1'::string),
+('2024-07-01T00:00:00.123456'::timestamp, 'UTC+1:2'::string),
+('2024-07-01T00:00:00.123456'::timestamp, 'UT-010203'::string),
+('2024-04-15T00:00:00.123456'::timestamp, 'SystemV/PST8PDT'::string),
+('2024-07-01T00:00:00.123456'::timestamp, 'SystemV/HST10'::string),
+(NULL::timestamp, '+19:00'::string);
+----
+2024-07-01T07:00:00.123456
+2024-06-30T23:00:00.123456
+2024-06-30T22:58:00.123456
+2024-07-01T01:02:03.123456
+2024-04-15T08:00:00.123456
+2024-07-01T10:00:00.123456
+NULL
Review Comment:
Could we add a few records for the `local_to_utc` paths this function now
uses? The `SystemV` rows are on dates without a transition, but `local_to_utc`
has separate gap and overlap logic for `SystemV`. The old code also matched on
the time unit, while the new path relies on `units_per_second` and
`div_euclid`, and every record here is in microseconds. These pass on this
branch and match Spark 4.2.0:
```
query P
SELECT to_utc_timestamp(column1, column2)
FROM VALUES
(CAST('1969-12-31T23:59:59.5' AS TIMESTAMP), CAST('America/New_York' AS
STRING)),
(CAST('2024-11-03T01:30:00' AS TIMESTAMP), CAST('SystemV/EST5EDT' AS
STRING)),
(CAST('2024-03-10T02:30:00' AS TIMESTAMP), CAST('SystemV/EST5EDT' AS
STRING));
----
1970-01-01T04:59:59.500
2024-11-03T06:30:00
2024-03-10T07:30:00
```
It would also be good to have a `Timestamp(Second, None)` and a
`Timestamp(Nanosecond, None)` input in the overlap. For example,
`arrow_cast(CAST('2024-11-03T01:30:00' AS TIMESTAMP), 'Timestamp(Second,
None)')` with `America/New_York` should give `2024-11-03T05:30:00`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]