Max Gekk created SPARK-62368:
--------------------------------
Summary: date_trunc to WEEK, MONTH, QUARTER or YEAR returns the
later of two midnights when the truncated day starts inside a DST overlap
Key: SPARK-62368
URL: https://issues.apache.org/jira/browse/SPARK-62368
Project: Spark
Issue Type: Bug
Components: SQL
Affects Versions: 5.0.0
Reporter: Max Gekk
SPARK-56769 added a fast path to DateTimeUtils.truncTimestamp for the units
WEEK, MONTH, QUARTER and YEAR. It truncates in the zone offset of the input and
accepts the result when the offset at the result equals the offset at the
input; otherwise it calls truncTimestampSlow. The pull request says that the
output is identical to the slow path in all cases.
The guard misses one case. When the truncated local midnight is inside a DST
overlap, that midnight occurs twice. The slow path (daysToMicros, which is
LocalDate.atStartOfDay) returns the first occurrence. For an input after the
overlap the fast path returns the second occurrence, and the guard passes,
because the offset there is the input's offset.
Example: America/Havana on 1 November 2020. At 01:00 the clocks went back to
00:00, so 00:00 occurred at 04:00 UTC (offset -04:00) and again at 05:00 UTC
(offset -05:00).
{code:sql}
SET spark.sql.session.timeZone=America/Havana;
SELECT ts, date_trunc('MONTH', ts) AS month_start,
unix_seconds(date_trunc('MONTH', ts)) AS month_start_epoch
FROM VALUES (timestamp_seconds(1604205000)), (timestamp_seconds(1605891600)) AS
t(ts);
{code}
Actual:
{code}
|ts |month_start |month_start_epoch|
|2020-11-01 00:30:00|2020-11-01 00:00:00|1604203200 |
|2020-11-20 12:00:00|2020-11-01 00:00:00|1604206800 |
{code}
Expected: 1604203200 in both rows, which is what truncTimestampSlow returns.
So two instants of one month truncate to two different instants that print the
same. GROUP BY date_trunc('MONTH', ts) returns two groups for that month:
{code}
|month_start |cnt|
|2020-11-01 00:00:00|1 |
|2020-11-01 00:00:00|1 |
{code}
At the level of the function:
{code:scala}
val zid = ZoneId.of("America/Havana")
// 2020-11-20T17:00:00Z
truncTimestamp(1605891600000000L, TRUNC_TO_MONTH, zid)
// returns 1604206800000000 (2020-11-01T05:00:00Z); the slow path returns
1604203200000000
{code}
The fast path is in the 4.3.0 release, in branch-4.4, branch-4.x and master.
The output above is from a build whose truncTimestamp and truncTimestampSlow
are text-equal to master's.
Scope: with tzdata 2026f, from 1970 to 2100 there are 23 cases of a zone and a
day where a repeated midnight starts a week, a month, a quarter or a year.
Examples: America/Havana and Cuba on 2015-11-01 and 2020-11-01;
America/St_Johns and America/Goose_Bay on 2009-11-01; America/Managua on
2006-10-01; Asia/Gaza and Asia/Hebron on 2004-10-01; Asia/Jerusalem in the
weeks of 2001-09-24 and 2002-10-07; Europe/Rome on 1972-10-01 and 1978-10-01.
DAY is not affected: its slow path is ZonedDateTime.truncatedTo(DAYS), which
keeps the offset as the fast path does.
Possible fix: for the date-level units, also fall back to the slow path when
the truncated local time has two valid offsets
(ZoneRules.getValidOffsets(local).size() == 2), and add the Havana case to
DateTimeUtilsSuite.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]