Skip to content

Commit

Permalink
Add the alter table ON COMMIT option to Snowflake (apache#1606)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavcloud authored Dec 17, 2024
1 parent 7867ba3 commit c698391
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ pub fn parse_create_table(
parser.expect_token(&Token::RParen)?;
builder = builder.with_tags(Some(tags));
}
Keyword::ON if parser.parse_keyword(Keyword::COMMIT) => {
let on_commit = Some(parser.parse_create_table_on_commit()?);
builder = builder.on_commit(on_commit);
}
_ => {
return parser.expected("end of statement", next_token);
}
Expand Down
36 changes: 20 additions & 16 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6155,22 +6155,11 @@ impl<'a> Parser<'a> {
None
};

let on_commit: Option<OnCommit> =
if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT, Keyword::DELETE, Keyword::ROWS])
{
Some(OnCommit::DeleteRows)
} else if self.parse_keywords(&[
Keyword::ON,
Keyword::COMMIT,
Keyword::PRESERVE,
Keyword::ROWS,
]) {
Some(OnCommit::PreserveRows)
} else if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT, Keyword::DROP]) {
Some(OnCommit::Drop)
} else {
None
};
let on_commit = if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT]) {
Some(self.parse_create_table_on_commit()?)
} else {
None
};

let strict = self.parse_keyword(Keyword::STRICT);

Expand Down Expand Up @@ -6226,6 +6215,21 @@ impl<'a> Parser<'a> {
.build())
}

pub(crate) fn parse_create_table_on_commit(&mut self) -> Result<OnCommit, ParserError> {
if self.parse_keywords(&[Keyword::DELETE, Keyword::ROWS]) {
Ok(OnCommit::DeleteRows)
} else if self.parse_keywords(&[Keyword::PRESERVE, Keyword::ROWS]) {
Ok(OnCommit::PreserveRows)
} else if self.parse_keywords(&[Keyword::DROP]) {
Ok(OnCommit::Drop)
} else {
parser_err!(
"Expecting DELETE ROWS, PRESERVE ROWS or DROP",
self.peek_token()
)
}
}

/// Parse configuration like partitioning, clustering information during the table creation.
///
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_2)
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ fn test_snowflake_create_table_column_comment() {
}
}

#[test]
fn test_snowflake_create_table_on_commit() {
snowflake().verified_stmt(
r#"CREATE LOCAL TEMPORARY TABLE "AAA"."foo" ("bar" INTEGER) ON COMMIT PRESERVE ROWS"#,
);
snowflake().verified_stmt(r#"CREATE TABLE "AAA"."foo" ("bar" INTEGER) ON COMMIT DELETE ROWS"#);
snowflake().verified_stmt(r#"CREATE TABLE "AAA"."foo" ("bar" INTEGER) ON COMMIT DROP"#);
}

#[test]
fn test_snowflake_create_local_table() {
match snowflake().verified_stmt("CREATE TABLE my_table (a INT)") {
Expand Down

0 comments on commit c698391

Please sign in to comment.