Skip to content

Add the alter table ON COMMIT option to Snowflake #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2024
Merged
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
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
Loading