Jump to content

Time travel (SQL)

From Open Lakehouse Wiki
Revision as of 00:53, 25 November 2026 by Open Lakehouse Wiki sync (talk | contribs) (Sync from open-lakehouse/wiki@7c9abaa)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Time travel (SQL)
Also known as AS OF query, temporal query, snapshot query
Field Table formats and SQL
Key parts VERSION AS OF, TIMESTAMP AS OF, table versions or snapshots, retention
Open source implementations Apache Spark with Delta Lake, Apache Iceberg and Apache Hudi; Trino, Snowflake, BigQuery
Related Open table format, Data Source V2, Catalog plugin API (Spark)
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (SqlBaseParser.g4, SQLConf.scala, TableCatalog.java, TimeTravelSpec.scala, DataFrameReader.scala, docs/sql-migration-guide.md), the Spark 3.3.0, 3.4.0, 4.0.0 and 4.3.0 release notes, Delta Lake batch reads and writes docs, Apache Iceberg 1.12.0 Spark queries and configuration docs, Apache Hudi 1.2.1 SQL queries and quick start docs, Trino 484 Iceberg and Delta Lake connector docs, the Snowflake AT/BEFORE reference and the BigQuery query syntax and time travel docs.

Time travel in SQL is a query that reads a table as it was at an earlier version or point in time, written in Apache Spark SQL as SELECT ... FROM t VERSION AS OF v or SELECT ... FROM t TIMESTAMP AS OF ts.[1] Spark parses the clause, and the table format resolves it: Delta Lake maps the version to a commit in its transaction log, and Apache Iceberg maps it to a snapshot ID, branch or tag. Time travel is used to reproduce reports and models, audit changes and recover from bad writes, and it works only while the old data and metadata are still retained.[2]

What is the syntax in Apache Spark?

The Spark SQL grammar accepts four spellings after a table name: VERSION AS OF and FOR SYSTEM_VERSION AS OF take an integer or string literal; TIMESTAMP AS OF and FOR SYSTEM_TIME AS OF take an expression.[1] The timestamp expression must be deterministic and evaluable once at analysis time, and a query cannot carry both a version and a timestamp.[3]

Spark 3.3.0 added the AS OF syntax (SPARK-37219), and Spark 3.4.0 allowed a scalar subquery in the timestamp (SPARK-39306).[4][5] Spark 4.0.0 added time travel options to the spark.read API (SPARK-45575); the option names are versionAsOf and timestampAsOf by default, configurable with spark.sql.timeTravelVersionKey and spark.sql.timeTravelTimestampKey.[6][7]

Spark 4.3.0 added an @ suffix on table names (SPARK-57421, SPARK-58012): name@v123 reads version 123, and name@20240101000000000 (format yyyyMMddHHmmssSSS, in the session time zone) reads the table at that timestamp to millisecond precision. The suffix is controlled by spark.sql.timeTravel.atSyntax.enabled, default true.[8][7] Also since 4.3, Spark rejects these time travel options on catalog-backed Data Source V2 writes with UNSUPPORTED_FEATURE.TIME_TRAVEL, because a write must target the current table state.[9]

How does a connector implement time travel?

For catalog tables, Spark passes the parsed clause to the catalog plugin. TableCatalog has loadTable(ident, String version) and loadTable(ident, long timestamp), where the timestamp is in microseconds since the Unix epoch. Their default implementations throw a "no such table" error, so a catalog that does not override them does not support time travel.[10]

Example

Spark SQL against a Delta Lake or Iceberg table named sales.orders:[2][11][7]

-- Delta Lake: commit version from DESCRIBE HISTORY
SELECT * FROM sales.orders VERSION AS OF 12;
SELECT * FROM sales.orders TIMESTAMP AS OF '2026-11-01 00:00:00';

-- Iceberg: snapshot ID, branch or tag name
SELECT * FROM lake.sales.orders VERSION AS OF 10963874102873;
SELECT * FROM lake.sales.orders VERSION AS OF 'audit-branch';

-- Spark 4.3 @ suffix (for catalogs that implement time travel)
SELECT * FROM sales.orders@v12;

The same reads with the DataFrame API, where spark.read.table passes its options to the relation that Spark resolves:[12][6]

df_v12 = spark.read.option("versionAsOf", 12).table("sales.orders")
df_nov = spark.read.option("timestampAsOf", "2026-11-01 00:00:00").table("sales.orders")

How do table formats compare?

Delta Lake Apache Iceberg Apache Hudi
What a version is A commit version in the transaction log, listed by DESCRIBE HISTORY[2] A snapshot ID, or a branch or tag name[11] A commit time (instant)[13]
Spark SQL syntax VERSION AS OF, TIMESTAMP AS OF[2] VERSION AS OF, TIMESTAMP AS OF, FOR SYSTEM_VERSION AS OF, FOR SYSTEM_TIME AS OF[11] TIMESTAMP AS OF[14]
Timestamp forms Any expression castable to a timestamp, such as '2018-10-18' or current_timestamp() - interval 12 hours; no subqueries[2] Timestamp string, or Unix time in seconds in SQL[11] yyyy-MM-dd HH:mm:ss.SSS, yyyy-MM-dd or yyyyMMddHHmmssSSS[14]
DataFrame options versionAsOf, timestampAsOf[2] snapshot-id, as-of-timestamp (milliseconds), branch, tag[11] as.of.instant[13]
What limits how far back delta.logRetentionDuration (default 30 days) and VACUUM, governed by delta.deletedFileRetentionDuration (default 7 days)[2] Snapshot expiration; history.expire.max-snapshot-age-ms defaults to 5 days and history.expire.min-snapshots-to-keep to 1[15] Not described on the cited Hudi pages

Delta Lake documents that, by default, a table can be read up to 30 days back unless VACUUM has run or the retention properties were changed. Because version timestamps come from log file timestamps, time travel by timestamp can break if the table directory is copied to a new location, while time travel by version is unaffected.[2]

Iceberg branches and tags give names to snapshots. A query by snapshot ID or timestamp uses the schema of that snapshot, a query on a branch uses the table's current schema, and a query on a tag uses the schema of the tagged snapshot.[11] Iceberg snapshots remain until they are expired, so expiration settings, not a fixed window, decide what can be read.[16]

Time travel in other engines

Engine Syntax
Trino 484 (Iceberg and Delta Lake connectors) FOR VERSION AS OF and FOR TIMESTAMP AS OF; on Iceberg, the version can be a branch or tag name[17][18]
Snowflake AT or BEFORE in the FROM clause, for example AT (TIMESTAMP => ...)[19]
BigQuery FOR SYSTEM_TIME AS OF; the time travel window is seven days by default and can be set between two and seven days[20][21]

See also

References

  1. ↑ 1.0 1.1 SqlBaseParser.g4 (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 Table batch reads and writes. Delta Lake (Linux Foundation).Retrieved 2026-11-23.
  3. ↑ TimeTravelSpec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ Spark Release 3.3.0. Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ Spark Release 3.4.0. Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ 6.0 6.1 Spark Release 4.0.0. Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ 7.0 7.1 7.2 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  8. ↑ Spark Release 4.3.0. Apache Software Foundation.Retrieved 2026-11-23.
  9. ↑ Migration Guide: SQL, Datasets and DataFrame (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  10. ↑ TableCatalog.java (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  11. ↑ 11.0 11.1 11.2 11.3 11.4 11.5 Spark Queries (Apache Iceberg 1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
  12. ↑ DataFrameReader.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  13. ↑ 13.0 13.1 Spark Quick Start (Apache Hudi 1.2.1). Apache Software Foundation.Retrieved 2026-11-23.
  14. ↑ 14.0 14.1 SQL Queries (Apache Hudi 1.2.1). Apache Software Foundation.Retrieved 2026-11-23.
  15. ↑ Configuration (Apache Iceberg 1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
  16. ↑ Maintenance (Apache Iceberg 1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
  17. ↑ Iceberg connector (Trino 484). Trino Software Foundation.Retrieved 2026-11-23.
  18. ↑ Delta Lake connector (Trino 484). Trino Software Foundation.Retrieved 2026-11-23.
  19. ↑ AT, BEFORE. Snowflake.Retrieved 2026-11-23.
  20. ↑ Query syntax (GoogleSQL for BigQuery). Google Cloud.Retrieved 2026-11-23.
  21. ↑ Data retention with time travel and fail-safe. Google Cloud.Retrieved 2026-11-23.