Skip to content

feat(core-spec): add AT TIME ZONE to the expression language - #718

Open
ekut wants to merge 8 commits into
apache:mainfrom
ekut:at-time-zone
Open

ekut wants to merge 8 commits into
apache:mainfrom
ekut:at-time-zone

Conversation

@ekut

@ekut ekut commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Adds AT TIME ZONE with 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:

  • The zone as a string literal with an IANA name.
  • The direction by the kind of the operand. A field follows its datatype, whatever the type of its column. A source column follows its type in the engine, which is the case in a field expression.
  • An error for a Date or Time operand.
  • The operator in the precedence list, left-associative.
  • One result for a wall-clock time that a clock change skips or repeats, the instant under the UTC offset before the change. The table of results has a zone with a 30-minute change and a zone at +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 with TIMESTAMP '...' AT TIME ZONE 'America/New_York' and on ClickHouse with toDateTime('...', 'America/New_York'):

This PR DuckDB 2.0.0 ClickHouse 26.9.5.2
skipped 2026-03-08 02:30:00 07:30:00 UTC 07:30:00 UTC 06:30:00 UTC
repeated 2026-11-01 01:30:00 05:30:00 UTC 06:30:00 UTC 05:30:00 UTC

The 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 in America/New_York, Australia/Lord_Howe, Europe/London and Asia/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 datatype and column disagree

@jbonofre proposed two rules on the thread: a DateTimeTz field over a column without a time zone holds UTC readings, and a DateTime field 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:

  1. Add both rules to this PR.
  2. Take them in a follow-up PR, with the default zone.

I lean to the second, for these reasons:

  • The rules do not reach a field expression. Its operand is a source column, so the same column would read as UTC through the field and as a wall-clock time in a neighbouring field expression.
  • An author can already state the UTC case in the expression: created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York'. The section has it as an example.
  • They are defaults, and they change what existing models return. On DuckDB with the session in America/New_York, a naive 2026-06-01 02:00:00 casts to the date 06-01, and to 05-31 once it is read as a UTC instant.
  • Tools differ today. With no defaultTimezone set, 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

  • Test cases for the results. Syntax cases are in compliance/expressions/; the results are the table in the section.
  • A validator check that the zone is a string literal. It can follow as its own PR.

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 TIMESTAMP and for one cast to TIMESTAMPTZ: CONVERT_TIMEZONE('z', x) for Snowflake, TIMESTAMP(DATETIME(x, 'z')) for BigQuery and FROM_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

ekut and others added 2 commits November 12, 2026 09:24
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>

@orca-review-bot orca-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 411334d.

ekut and others added 2 commits November 16, 2026 06:51
# Conflicts:
#	core-spec/expression_language.md
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ekut
ekut requested a review from orca-review-bot November 19, 2026 00:05
ekut added 2 commits November 20, 2026 06:36
# Conflicts:
#	core-spec/expression_language.md
#	docs/spec_changes.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants