Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Date_add throws or produces wrong results when the result would be nonexistent time in the time zone #11845

Closed

Conversation

kevinwilfong
Copy link
Contributor

Summary:
Date_add is computed on top of Unix timestamps (millis since epoch). When the unit being added is at least a
day, this calculation is done in the "local" time zone (the session time zone in the case of Timestamps and the
time zone associated with the value in the case of TImestampWithTimeZones). This local time is then converted
back to a system time.

When this calculation is done in a "local" time zone, the resulting local millis since epoch may not exist, e.g. in
U.S. time zones that respect daylight savings time, it may fall in the hour 2:00-3:00 am on the day when the
clocks move forward. In this case, date_add will throw an exception when this is done with Timestamps, and
return the next valid time (e.g. 3 am in the example above) when this is done with TimestampWithTimeZones.

To fix this I added a function correct_nonexistent_time to TimeZoneMap which can take a local timestamp and
adjust it by the difference between the to time zones (e.g. moves it forward an hour in the case of the example
above) and returns a timestamp that exists. This gets us behavior that is consistent with Presto Java.

Differential Revision: D67154813

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 12, 2024
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D67154813

Copy link

netlify bot commented Dec 12, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit a095fa5
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/675b3b6d578df60008c7054e

…be nonexistent time in the time zone (facebookincubator#11845)

Summary:

Date_add is computed on top of Unix timestamps (millis since epoch). When the unit being added is at least a
day, this calculation is done in the "local" time zone (the session time zone in the case of Timestamps and the
time zone associated with the value in the case of TImestampWithTimeZones). This local time is then converted
back to a system time.

When this calculation is done in a "local" time zone, the resulting local millis since epoch may not exist, e.g. in
U.S. time zones that respect daylight savings time, it may fall in the hour 2:00-3:00 am on the day when the
clocks move forward. In this case, date_add will throw an exception when this is done with Timestamps, and
return the next valid time (e.g. 3 am in the example above) when this is done with TimestampWithTimeZones.

To fix this I added a function correct_nonexistent_time to TimeZoneMap which can take a local timestamp and
adjust it by the difference between the to time zones (e.g. moves it forward an hour in the case of the example
above) and returns a timestamp that exists. This gets us behavior that is consistent with Presto Java.

Differential Revision: D67154813
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D67154813

@HeidiHan0000 HeidiHan0000 self-requested a review December 13, 2024 18:12
@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 42bd38a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants