Fix inconsistent TIMESTAMP WITH TIME ZONE semantics
Context
Presto has inconsistent semantics for TIMESTAMP WITH TIME ZONE (TSwTZ). Comparison is by instant which is correct, but field functions read each value's stored per-value zone, so two equal values can produce different results.
For a deterministic function f the expectation is: a == b ⇒ f(a) == f(b) should hold
But for TSwTZ:
4 PM EST == 1 PM PST (same instant)
hour(4 PM EST) != hour(1 PM PST) => 4 != 1
Previous discussion on the topic: prestodb/presto#23252.
Proposal
Two changes, both gated by a session property (legacy_timestamp_with_timezone), default = today's behavior:
- Field/render functions ignore the stored zone and use the session time zone — so a == b ⇒ f(a) == f(b) for
TIMESTAMP WITH TIME ZONE. Comparison is already instant-based and stays unchanged. - AT TIME ZONE on a
TIMESTAMP WITH TIME ZONEreturns a plainTIMESTAMPso an explicit zone request is frozen into a wall clock instead of riding as a zone, that field functions later override.
| Behavior | legacy=true (today) | legacy=false |
|---|---|---|
Field functions (hour, date_format, …) on TSwTZ |
Read each value's stored zone | Use the session zone |
TSwTZ AT TIME ZONE 'z' |
returns TIMESTAMP WITH TIME ZONE |
Returns TIMESTAMP (zone frozen to wall clock) |
| Comparison / joins / GROUP BY | By Instant | By Instant (unchanged) |
AT TIME ZONE Update
Presto today always returns a value with a time zone. Under the new behavior the only change is for input TIMESTAMP WITH TIME ZONE input: which will return a TIMESTAMP without the zone (the zone is dropped after rendering the wall clock in z). All other signatures are unchanged.
| Input | Output (legacy=true) | Output (legacy=false) |
|---|---|---|
TIMESTAMP |
TIMESTAMP WITH TIME ZONE |
TIMESTAMP WITH TIME ZONE |
TIMESTAMP WITH TIME ZONE |
TIMESTAMP WITH TIME ZONE |
TIMESTAMP |
TIME |
- | - |
TIME WITH TIMEZONE |
TIME WITH TIMEZONE |
TIME WITH TIMEZONE |
This also aligns with Postgres, reaching the same consistent TIMESTAMP WITH TIME ZONE behavior in a different way: Postgres doesn't store the zone, the Presto proposal is to ignore it.
Example
TIMESTAMP input - the session zone determines the answer
-- session = America/New_York SELECT TIMESTAMP '2024-06-15 12:00:00' AT TIME ZONE 'Asia/Tokyo'; → 12:00 read as NY (−4) = 16:00Z → rendered in Tokyo = 2024-06-16 01:00, tag Asia/Tokyo
Similarly, -- session = America/Los_Angeles SELECT TIMESTAMP '2024-06-15 12:00:00' AT TIME ZONE 'Asia/Tokyo'; -- 2024-06-16 04:00:00 Asia/Tokyo (12:00 PDT = 19:00Z)
AT TIME ZONE creates an instant using the session zone. This signature keeps returning TSwTZ as an instant is produced, it returns a type representing an instant.
For TSwTZ input - the session zone is irrelevant
-- session = America/New_York SELECT TIMESTAMP '2024-06-15 12:00:00 America/Los_Angeles' AT TIME ZONE 'Asia/Tokyo'; -- 2024-06-16 04:00:00 Asia/Tokyo -- identical under ANY session zone
The instant is already fixed at 19:00Z, the zone argument chooses how it's rendered. hour() shows why changes 1 and 2 have to be apply together. Change 1 fixes correctness (restoring a = b ⇒ f(a) = f(b)). Change 2 preserves the ability to ask for "the hour in Tokyo"
SELECT hour(TIMESTAMP '2024-06-15 12:00:00 America/Los_Angeles' AT TIME ZONE 'Asia/Tokyo');
| Case | Instant | Zone Stored | hour() reads | Result |
|---|---|---|---|---|
| Today | 19:00Z | Asia/Tokyo | the tag, Tokyo | 4 |
| Change 1 Only | 19:00Z | Asia/Tokyo | the session, New York | 15 |
| Change 1 and 2 only | - | None: TS 04:00 | wall clock, 04:00 | 4 (same as today) |
Source: prestodb/presto