Skip to content

Commit c698391

Browse files
authored
Add the alter table ON COMMIT option to Snowflake (apache#1606)
1 parent 7867ba3 commit c698391

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/dialect/snowflake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ pub fn parse_create_table(
377377
parser.expect_token(&Token::RParen)?;
378378
builder = builder.with_tags(Some(tags));
379379
}
380+
Keyword::ON if parser.parse_keyword(Keyword::COMMIT) => {
381+
let on_commit = Some(parser.parse_create_table_on_commit()?);
382+
builder = builder.on_commit(on_commit);
383+
}
380384
_ => {
381385
return parser.expected("end of statement", next_token);
382386
}

src/parser/mod.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6155,22 +6155,11 @@ impl<'a> Parser<'a> {
61556155
None
61566156
};
61576157

6158-
let on_commit: Option<OnCommit> =
6159-
if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT, Keyword::DELETE, Keyword::ROWS])
6160-
{
6161-
Some(OnCommit::DeleteRows)
6162-
} else if self.parse_keywords(&[
6163-
Keyword::ON,
6164-
Keyword::COMMIT,
6165-
Keyword::PRESERVE,
6166-
Keyword::ROWS,
6167-
]) {
6168-
Some(OnCommit::PreserveRows)
6169-
} else if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT, Keyword::DROP]) {
6170-
Some(OnCommit::Drop)
6171-
} else {
6172-
None
6173-
};
6158+
let on_commit = if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT]) {
6159+
Some(self.parse_create_table_on_commit()?)
6160+
} else {
6161+
None
6162+
};
61746163

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

@@ -6226,6 +6215,21 @@ impl<'a> Parser<'a> {
62266215
.build())
62276216
}
62286217

6218+
pub(crate) fn parse_create_table_on_commit(&mut self) -> Result<OnCommit, ParserError> {
6219+
if self.parse_keywords(&[Keyword::DELETE, Keyword::ROWS]) {
6220+
Ok(OnCommit::DeleteRows)
6221+
} else if self.parse_keywords(&[Keyword::PRESERVE, Keyword::ROWS]) {
6222+
Ok(OnCommit::PreserveRows)
6223+
} else if self.parse_keywords(&[Keyword::DROP]) {
6224+
Ok(OnCommit::Drop)
6225+
} else {
6226+
parser_err!(
6227+
"Expecting DELETE ROWS, PRESERVE ROWS or DROP",
6228+
self.peek_token()
6229+
)
6230+
}
6231+
}
6232+
62296233
/// Parse configuration like partitioning, clustering information during the table creation.
62306234
///
62316235
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_2)

tests/sqlparser_snowflake.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ fn test_snowflake_create_table_column_comment() {
355355
}
356356
}
357357

358+
#[test]
359+
fn test_snowflake_create_table_on_commit() {
360+
snowflake().verified_stmt(
361+
r#"CREATE LOCAL TEMPORARY TABLE "AAA"."foo" ("bar" INTEGER) ON COMMIT PRESERVE ROWS"#,
362+
);
363+
snowflake().verified_stmt(r#"CREATE TABLE "AAA"."foo" ("bar" INTEGER) ON COMMIT DELETE ROWS"#);
364+
snowflake().verified_stmt(r#"CREATE TABLE "AAA"."foo" ("bar" INTEGER) ON COMMIT DROP"#);
365+
}
366+
358367
#[test]
359368
fn test_snowflake_create_local_table() {
360369
match snowflake().verified_stmt("CREATE TABLE my_table (a INT)") {

0 commit comments

Comments
 (0)