appletreeisyellow commented on code in PR #9068:
URL: https://github.com/apache/arrow-datafusion/pull/9068#discussion_r1471364271
##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -564,8 +564,34 @@ fn _date_trunc_coarse_with_tz(
granularity: &str,
value: Option<DateTime<Tz>>,
) -> Result<Option<i64>> {
- let value = _date_trunc_coarse::<DateTime<Tz>>(granularity, value)?;
- Ok(value.and_then(|value| value.timestamp_nanos_opt()))
+ if let Some(value) = value {
+ let local = value.naive_local();
+ let truncated = _date_trunc_coarse::<NaiveDateTime>(granularity,
Some(local))?;
+ let truncated = truncated.map(|truncated| {
+ match truncated.and_local_timezone(value.timezone()) {
+ LocalResult::None => {
+ // It is impossible to truncate from a time that does
exist into one that doesn't.
+ panic!("date_trunc produced impossible time")
+ }
+ LocalResult::Single(datetime) => datetime,
+ LocalResult::Ambiguous(datetime1, datetime2) => {
+ // Because we are truncating from an equally or more
specific time
+ // the original time must have been within the ambiguous
local time
+ // period. Therefore the offset of one of these times
should match the
+ // offset of the original time.
+ if datetime1.offset().fix() == value.offset().fix() {
+ datetime1
+ } else {
+ datetime2
+ }
Review Comment:
Nice!
##########
datafusion/sqllogictest/test_files/timestamps.slt:
##########
@@ -1262,6 +1262,89 @@ SELECT DATE_TRUNC('day', arrow_cast(TIMESTAMP
'2023-08-03 14:38:50Z', 'Timestamp
2023-08-03T14:38:50
2023-08-03T14:38:50
+# date_trunc with data with timezones
+statement ok
+CREATE TABLE timestamp_strings(ts varchar)
+AS VALUES
+('2024-10-27 00:00:00'),
+('2024-10-27 00:30:00'),
+('2024-10-27 01:30:00'),
+('2024-10-27 02:00:00'), -- Daylight Savings Time happens here in Berlin
+('2024-10-27 02:30:00'),
+('2024-10-27 03:00:00'),
+('2024-10-27 03:30:00')
+;
+
+statement ok
+create view timestamp_utc as
+select
+ arrow_cast(ts, 'Timestamp(Nanosecond, Some("UTC"))') as ts
+from timestamp_strings;
+
+statement ok
+create view timestamp_berlin as
+select
+ arrow_cast(ts, 'Timestamp(Nanosecond, Some("Europe/Berlin"))') as ts
+from timestamp_utc; -- have to convert to utc prior to converting to berlin
+
+query PT
Review Comment:
@alamb What does `query PT` mean? Same question for `query PPPP`
--
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]