Skip to content

Parse FOR TIMESTAMP AS OF and FOR VERSION AS OF table versions - #2486

Open
rexminnis wants to merge 2 commits into
apache:mainfrom
rexminnis:trino-time-travel
Open

Parse FOR TIMESTAMP AS OF and FOR VERSION AS OF table versions#2486
rexminnis wants to merge 2 commits into
apache:mainfrom
rexminnis:trino-time-travel

Conversation

@rexminnis

Copy link
Copy Markdown

Stacked on #2485; only the last commit is new here, and I will rebase once that one lands.

Trino spells time travel on Iceberg and Delta tables with a leading FOR:

SELECT * FROM t FOR TIMESTAMP AS OF TIMESTAMP '2026-01-01 00:00:00 UTC'
SELECT * FROM t FOR VERSION AS OF 8954597067493422955
SELECT * FROM t FOR VERSION AS OF 'my-branch'

https://trino.io/docs/current/connector/iceberg.html#time-travel-queries

The parser already accepts the Databricks forms without FOR behind supports_table_versioning. This adds TableVersion::ForTimestampAsOf and TableVersion::ForVersionAsOf with their own Display, behind a new Dialect::supports_for_table_version hook that TrinoDialect opts into, so a statement round-trips in the spelling its engine accepts. VERSION AS OF keeps its numeric-only argument; the FOR form takes an expression because Iceberg accepts a branch or tag name there. Databricks keeps rejecting the FOR forms, as its existing test asserts.

One pre-existing issue noticed on the way, left for a separate change: an alias after a version clause parses, but the canonical form prints the alias first (t AS a FOR VERSION AS OF 1), which no engine accepts.

Trino (the SQL engine that descends from Presto) follows the standard
closely, and most of what it needs is already an opt-in hook: double-quoted
identifiers only, FILTER on aggregates, GROUP BY expressions and grouping
sets, lambdas, MATCH_RECOGNIZE, table versioning, parenthesized EXPLAIN
options, named table-function arguments with =>, and COMMENT ON. The test
corpus covers the constructs a Trino user writes daily. Not covered yet:
the FOR prefix on TIMESTAMP/VERSION AS OF (time travel), ROW types with
named fields, and inline WITH FUNCTION routines.
Trino spells time travel on Iceberg and Delta tables with a leading FOR:
FOR TIMESTAMP AS OF <expr> and FOR VERSION AS OF <snapshot id | branch>.
The parser already accepts the Databricks forms without FOR behind
supports_table_versioning; these two variants join them behind a new
supports_for_table_version hook that TrinoDialect opts into, with their
own Display, so a statement round-trips in the spelling its engine
accepts. Databricks keeps rejecting the FOR forms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant