Repository navigation
Conversation
AT TIME ZONE with an IANA zone name converts an instant to the wall-clock time of a zone and back. The operator is RECOMMENDED: an implementation that supports it gives one result for wall-clock times that a clock change skips or repeats, and one that does not reports an error. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…tures Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
orca-review-bot
left a comment
There was a problem hiding this comment.
Nice, tight PR. I checked a few things locally: the grammar tests pass with the README's uv command (11 passed), ruff check and format are clean, and all five rows of the skipped/repeated table match Python zoneinfo with fold=0.
On the open question, I'd go with option 2 and take @jbonofre's mismatch defaults in the follow-up with the default zone. They change existing results and don't reach field expressions.
Unanchored: can we open a follow-up issue for result fixtures (the DST rows and the Date/Time error cases) and link it here? Syntax fixtures alone won't catch an engine that skips the rewrite.
Two spec questions are inline.
🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.
| | Operand | Instant or wall-clock time | | ||
| | :---- | :---- | | ||
| | A reference to a field | As the field's `datatype` declares: `DateTimeTz` is an instant, `DateTime` is a wall-clock time. The physical type of the column behind the field does not decide it | | ||
| | A reference to a source column | As the column's type in the engine. A type whose values are instants is an instant, whether or not its name mentions a time zone. A type whose values are a date and time that name no instant is a wall-clock time | |
There was a problem hiding this comment.
What does an implementation do when it doesn't know the engine type of a source column? For example, a converter emitting SQL without a catalog. The direction, and so whether the skipped/repeated rewrite applies, depends on that type. Line 442 already says "reports the error where it knows the type" for Date/Time. Can we add a matching sentence for direction, e.g. that it reports an error, or that the result is implementation-defined?
🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.
There was a problem hiding this comment.
The section has no rule for that case, and it needs one. Proposed text, after the sentence about a source column of a date or time-of-day type:
"An implementation that does not know the type of a source column MAY leave the direction to the native conversion of its engine, and the result for a skipped or repeated time is then implementation-defined. An implementation that does not do so reports the error."
With an error alone, a converter without a catalog would reject AT TIME ZONE on every source column. That includes a column of instants, where the conversion has one result. I'll add the text once a reviewer agrees with it.
| CAST(created_at_utc AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York' AS DATE) | ||
| ``` | ||
|
|
||
| An implementation that supports `AT TIME ZONE` MUST follow the rules of this section. An implementation that does not support it MUST report an error for an expression that uses it. It MUST NOT evaluate the expression without the conversion. An implementation MAY support only the conversion of an instant to a wall-clock time. It then reports the same error for the other direction. |
There was a problem hiding this comment.
Does an instant → wall-clock-only implementation also have to reject the operands whose direction is undefined (line 444: a CAST to TIMESTAMP, a field with no datatype)? As written it might evaluate those as instants. Can we spell out either way in this paragraph?
🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.
There was a problem hiding this comment.
No. For those operands "this version does not define the direction of the conversion, and a portable expression does not use one". That holds for every implementation, whichever directions it supports, so I'd leave the paragraph as it is.
| assert kind(add.valueExpr(1)) == "MultiplicativeValue" | ||
|
|
||
|
|
||
| def test_at_time_zone_binds_tighter_than_addition(ossie): |
There was a problem hiding this comment.
nit: can we add a -x AT TIME ZONE 'UTC' case here? It would pin unary binding tighter than AT, as in the spec's precedence list.
🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.
# Conflicts: # core-spec/expression_language.md
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
# Conflicts: # docs/spec_changes.md
# Conflicts: # core-spec/expression_language.md # docs/spec_changes.md
Adds
AT TIME ZONEwith a time zone name to the expression language. This is step 1 of the plan on dev@ [1] that followed #710: the Layer 1 primitives that @christianeu-db suggested landing first. The default zone is step 2 and is not in this PR.x AT TIME ZONE 'America/New_York'converts an instant (DateTimeTz) to the wall-clock time (DateTime) of the zone, and a wall-clock time to the instant it names there. From the thread, the section has:datatype, whatever the type of its column. A source column follows its type in the engine, which is the case in a field expression.DateorTimeoperand.+05:30.The level is RECOMMENDED. An implementation that supports the operator follows the section. One that does not reports an error and does not evaluate the expression without the conversion. That is the rule for a consumer with no zone conversion, which the thread asked about [2].
Engines differ on skipped and repeated times, so an implementation rewrites where its engine gives another result. Measured for
America/New_York, on DuckDB withTIMESTAMP '...' AT TIME ZONE 'America/New_York'and on ClickHouse withtoDateTime('...', 'America/New_York'):2026-03-08 02:30:0007:30:00UTC07:30:00UTC06:30:00UTC2026-11-01 01:30:0005:30:00UTC06:30:00UTC05:30:00UTCThe section describes a rewrite built from the engine's own conversion. It matched Python
zoneinfo(fold=0) on both engines for 13 wall-clock times inAmerica/New_York,Australia/Lord_Howe,Europe/LondonandAsia/Kolkata. Rows for other engines are welcome.Standard SQL: the SQL:2003 grammar, which the expression language builds on, has
AT TIME ZONE <interval primary>, a displacement, and no time zone names. The SQL:2011 and SQL:2016 grammars are the same there. A displacement cannot follow daylight saving time, so the operand is a zone name, as in PostgreSQL and DuckDB. The Standards Reference section notes the departure.Open question: a field whose
datatypeand column disagree@jbonofre proposed two rules on the thread: a
DateTimeTzfield over a column without a time zone holds UTC readings, and aDateTimefield over a column with a zone is its wall clock in UTC. As submitted, the section leaves both cases implementation-defined. I see two ways to settle them:I lean to the second, for these reasons:
created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York'. The section has it as an example.America/New_York, a naive2026-06-01 02:00:00casts to the date06-01, and to05-31once it is read as a UTC instant.defaultTimezoneset, OrionBelt leaves a naive column as stored and reads it in the model zone, per @ralfbecher in Model time zone: which day CURRENT_DATE and the date of an instant mean #710. ClickHouse has no date and time type without a zone, so the second rule would apply to every wall-clock column there and shift it by the offset of the column.If reviewers prefer the first way, I'll add the rules here.
Not in this PR
compliance/expressions/; the results are the table in the section.Impact on implementations: the change is additive, and no existing expression changes meaning. An implementation that translates through SQLGlot has to pick the direction itself. SQLGlot 30.31.0 generates the same call for an operand cast to
TIMESTAMPand for one cast toTIMESTAMPTZ:CONVERT_TIMEZONE('z', x)for Snowflake,TIMESTAMP(DATETIME(x, 'z'))for BigQuery andFROM_UTC_TIMESTAMP(x, 'z')for Databricks. For MySQL it drops the conversion with a warning.This is a specification change, so it takes the discussion period and the vote on dev@ that CONTRIBUTING describes.
[1] https://lists.apache.org/thread/flrwck9jj39rv2soxx1m4fxcpmtktxhf
[2] https://lists.apache.org/thread/4pwjk2kwphyglw2zy8hv4s41s0clhrzv