Skip to content

Commit 6060a11

Browse files
authored
Databricks: Support Timetravel With "VERSION AS OF" (apache#2155)
1 parent 46f2234 commit 6060a11

File tree

8 files changed

+23
-8
lines changed

8 files changed

+23
-8
lines changed

src/ast/query.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,10 @@ pub enum TableVersion {
23982398
/// Databricks supports this syntax.
23992399
/// For example: `SELECT * FROM tbl TIMESTAMP AS OF CURRENT_TIMESTAMP() - INTERVAL 1 HOUR`
24002400
TimestampAsOf(Expr),
2401+
/// When the table version is defined using `VERSION AS OF`.
2402+
/// Databricks supports this syntax.
2403+
/// For example: `SELECT * FROM tbl VERSION AS OF 2`
2404+
VersionAsOf(Expr),
24012405
/// When the table version is defined using a function.
24022406
/// For example: `SELECT * FROM tbl AT(TIMESTAMP => '2020-08-14 09:30:00')`
24032407
Function(Expr),
@@ -2408,6 +2412,7 @@ impl Display for TableVersion {
24082412
match self {
24092413
TableVersion::ForSystemTimeAsOf(e) => write!(f, "FOR SYSTEM_TIME AS OF {e}")?,
24102414
TableVersion::TimestampAsOf(e) => write!(f, "TIMESTAMP AS OF {e}")?,
2415+
TableVersion::VersionAsOf(e) => write!(f, "VERSION AS OF {e}")?,
24112416
TableVersion::Function(func) => write!(f, "{func}")?,
24122417
}
24132418
Ok(())

src/dialect/bigquery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Dialect for BigQueryDialect {
136136
}
137137

138138
// See <https://cloud.google.com/bigquery/docs/access-historical-data>
139-
fn supports_timestamp_versioning(&self) -> bool {
139+
fn supports_table_versioning(&self) -> bool {
140140
true
141141
}
142142

src/dialect/databricks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Dialect for DatabricksDialect {
4848
}
4949

5050
/// <https://docs.databricks.com/gcp/en/delta/history#delta-time-travel-syntax>
51-
fn supports_timestamp_versioning(&self) -> bool {
51+
fn supports_table_versioning(&self) -> bool {
5252
true
5353
}
5454

src/dialect/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ pub trait Dialect: Debug + Any {
10721072

10731073
/// Returns true if this dialect supports querying historical table data
10741074
/// by specifying which version of the data to query.
1075-
fn supports_timestamp_versioning(&self) -> bool {
1075+
fn supports_table_versioning(&self) -> bool {
10761076
false
10771077
}
10781078

src/dialect/mssql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Dialect for MsSqlDialect {
107107
}
108108

109109
/// See: <https://learn.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table>
110-
fn supports_timestamp_versioning(&self) -> bool {
110+
fn supports_table_versioning(&self) -> bool {
111111
true
112112
}
113113

src/dialect/snowflake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ impl Dialect for SnowflakeDialect {
540540
}
541541

542542
/// See: <https://docs.snowflake.com/en/sql-reference/constructs/at-before>
543-
fn supports_timestamp_versioning(&self) -> bool {
543+
fn supports_table_versioning(&self) -> bool {
544544
true
545545
}
546546

src/parser/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15731,7 +15731,7 @@ impl<'a> Parser<'a> {
1573115731

1573215732
/// Parses a the timestamp version specifier (i.e. query historical data)
1573315733
pub fn maybe_parse_table_version(&mut self) -> Result<Option<TableVersion>, ParserError> {
15734-
if self.dialect.supports_timestamp_versioning() {
15734+
if self.dialect.supports_table_versioning() {
1573515735
if self.parse_keywords(&[Keyword::FOR, Keyword::SYSTEM_TIME, Keyword::AS, Keyword::OF])
1573615736
{
1573715737
let expr = self.parse_expr()?;
@@ -15743,6 +15743,9 @@ impl<'a> Parser<'a> {
1574315743
} else if self.parse_keywords(&[Keyword::TIMESTAMP, Keyword::AS, Keyword::OF]) {
1574415744
let expr = self.parse_expr()?;
1574515745
return Ok(Some(TableVersion::TimestampAsOf(expr)));
15746+
} else if self.parse_keywords(&[Keyword::VERSION, Keyword::AS, Keyword::OF]) {
15747+
let expr = Expr::Value(self.parse_number_value()?);
15748+
return Ok(Some(TableVersion::VersionAsOf(expr)));
1574615749
}
1574715750
}
1574815751
Ok(None)

tests/sqlparser_databricks.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,21 @@ fn data_type_timestamp_ntz() {
371371

372372
#[test]
373373
fn parse_table_time_travel() {
374-
all_dialects_where(|d| d.supports_timestamp_versioning())
374+
all_dialects_where(|d| d.supports_table_versioning())
375375
.verified_only_select("SELECT 1 FROM t1 TIMESTAMP AS OF '2018-10-18T22:15:12.013Z'");
376376

377-
all_dialects_where(|d| d.supports_timestamp_versioning()).verified_only_select(
377+
all_dialects_where(|d| d.supports_table_versioning()).verified_only_select(
378378
"SELECT 1 FROM t1 TIMESTAMP AS OF CURRENT_TIMESTAMP() - INTERVAL 12 HOURS",
379379
);
380380

381+
all_dialects_where(|d| d.supports_table_versioning())
382+
.verified_only_select("SELECT 1 FROM t1 VERSION AS OF 1");
383+
381384
assert!(databricks()
382385
.parse_sql_statements("SELECT 1 FROM t1 FOR TIMESTAMP AS OF 'some_timestamp'")
383386
.is_err());
387+
388+
assert!(all_dialects_where(|d| d.supports_table_versioning())
389+
.parse_sql_statements("SELECT 1 FROM t1 VERSION AS OF 1 - 2",)
390+
.is_err())
384391
}

0 commit comments

Comments
 (0)