Skip to content

Commit e60e9f8

Browse files
yoavcloudVedin
authored andcommitted
Add the alter table ON COMMIT option to Snowflake (apache#1606)
1 parent 43b5d9c commit e60e9f8

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/ast/dml.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ use sqlparser_derive::{Visit, VisitMut};
3232
pub use super::ddl::{ColumnDef, TableConstraint};
3333

3434
use super::{
35-
display_comma_separated, display_separated, query::InputFormatClause, Assignment, ClusteredBy,
35+
display_comma_separated, display_separated, ClusteredBy,
3636
CommentDef, Expr, FileFormat, FromTable, HiveDistributionStyle, HiveFormat, HiveIOFormat,
3737
HiveRowFormat, Ident, InsertAliases, MysqlInsertPriority, ObjectName, OnCommit, OnInsert,
38-
OneOrManyWithParens, OrderByExpr, Query, RowAccessPolicy, SelectItem, Setting, SqlOption,
39-
SqliteOnConflict, StorageSerializationPolicy, TableEngine, TableObject, TableWithJoins, Tag,
38+
OneOrManyWithParens, OrderByExpr, Query, RowAccessPolicy, SelectItem, SqlOption,
39+
SqliteOnConflict, StorageSerializationPolicy, TableEngine, TableWithJoins, Tag,
4040
WrappedCollection,
4141
};
4242

src/parser/mod.rs

+20-16
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

+9
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)