Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2245,6 +2245,10 @@ pub enum TableVersion {
/// Databricks supports this syntax.
/// For example: `SELECT * FROM tbl TIMESTAMP AS OF CURRENT_TIMESTAMP() - INTERVAL 1 HOUR`
TimestampAsOf(Expr),
/// When the table version is defined using `VERSION AS OF`.
/// Databricks supports this syntax.
/// For example: `SELECT * FROM tbl VERSION AS OF 2`
VersionAsOf(Expr),
/// When the table version is defined using a function.
/// For example: `SELECT * FROM tbl AT(TIMESTAMP => '2020-08-14 09:30:00')`
Function(Expr),
Expand All @@ -2255,6 +2259,7 @@ impl Display for TableVersion {
match self {
TableVersion::ForSystemTimeAsOf(e) => write!(f, "FOR SYSTEM_TIME AS OF {e}")?,
TableVersion::TimestampAsOf(e) => write!(f, "TIMESTAMP AS OF {e}")?,
TableVersion::VersionAsOf(e) => write!(f, "VERSION AS OF {e}")?,
TableVersion::Function(func) => write!(f, "{func}")?,
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/dialect/bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Dialect for BigQueryDialect {
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/dialect/databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Dialect for DatabricksDialect {
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ pub trait Dialect: Debug + Any {

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

Expand Down
2 changes: 1 addition & 1 deletion src/dialect/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Dialect for MsSqlDialect {
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl Dialect for SnowflakeDialect {
}

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

Expand Down
5 changes: 4 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15484,7 +15484,7 @@ impl<'a> Parser<'a> {

/// Parses a the timestamp version specifier (i.e. query historical data)
pub fn maybe_parse_table_version(&mut self) -> Result<Option<TableVersion>, ParserError> {
if self.dialect.supports_timestamp_versioning() {
if self.dialect.supports_table_versioning() {
if self.parse_keywords(&[Keyword::FOR, Keyword::SYSTEM_TIME, Keyword::AS, Keyword::OF])
{
let expr = self.parse_expr()?;
Expand All @@ -15496,6 +15496,9 @@ impl<'a> Parser<'a> {
} else if self.parse_keywords(&[Keyword::TIMESTAMP, Keyword::AS, Keyword::OF]) {
let expr = self.parse_expr()?;
return Ok(Some(TableVersion::TimestampAsOf(expr)));
} else if self.parse_keywords(&[Keyword::VERSION, Keyword::AS, Keyword::OF]) {
let expr = Expr::Value(self.parse_number_value()?);
return Ok(Some(TableVersion::VersionAsOf(expr)));
}
}
Ok(None)
Expand Down
11 changes: 9 additions & 2 deletions tests/sqlparser_databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,21 @@ fn data_type_timestamp_ntz() {

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

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

all_dialects_where(|d| d.supports_table_versioning())
.verified_only_select("SELECT 1 FROM t1 VERSION AS OF 1");

assert!(databricks()
.parse_sql_statements("SELECT 1 FROM t1 FOR TIMESTAMP AS OF 'some_timestamp'")
.is_err());

assert!(all_dialects_where(|d| d.supports_table_versioning())
.parse_sql_statements("SELECT 1 FROM t1 VERSION AS OF 1 - 2",)
.is_err())
}