From cc487debec424e4e8ff9d393028ae8c6eb6430fb Mon Sep 17 00:00:00 2001 From: Pawel Leszczynski Date: Mon, 6 Mar 2023 11:21:44 +0100 Subject: [PATCH] support snowflake alter table swap with Signed-off-by: Pawel Leszczynski --- src/ast/ddl.rs | 7 +++++++ src/keywords.rs | 1 + src/parser.rs | 6 +++++- tests/sqlparser_snowflake.rs | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 31e944b6b..3edabd7f8 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -96,6 +96,10 @@ pub enum AlterTableOperation { column_name: Ident, op: AlterColumnOperation, }, + /// 'SWAP WITH ' + /// + /// Note: this is Snowflake specific + SwapWith { table_name: ObjectName }, } #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] @@ -203,6 +207,9 @@ impl fmt::Display for AlterTableOperation { AlterTableOperation::RenameConstraint { old_name, new_name } => { write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}") } + AlterTableOperation::SwapWith { table_name } => { + write!(f, "SWAP WITH {table_name}") + } } } } diff --git a/src/keywords.rs b/src/keywords.rs index af741aeef..20832bff2 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -545,6 +545,7 @@ define_keywords!( SUM, SUPER, SUPERUSER, + SWAP, SYMMETRIC, SYNC, SYSTEM, diff --git a/src/parser.rs b/src/parser.rs index 526cf58d4..fc84be728 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3891,9 +3891,13 @@ impl<'a> Parser<'a> { ); }; AlterTableOperation::AlterColumn { column_name, op } + } else if self.parse_keyword(Keyword::SWAP) { + self.expect_keyword(Keyword::WITH)?; + let table_name = self.parse_object_name()?; + AlterTableOperation::SwapWith { table_name } } else { return self.expected( - "ADD, RENAME, PARTITION or DROP after ALTER TABLE", + "ADD, RENAME, PARTITION, SWAP or DROP after ALTER TABLE", self.peek_token(), ); }; diff --git a/tests/sqlparser_snowflake.rs b/tests/sqlparser_snowflake.rs index eeaebf118..5f5169733 100644 --- a/tests/sqlparser_snowflake.rs +++ b/tests/sqlparser_snowflake.rs @@ -493,3 +493,18 @@ fn test_select_wildcard_with_exclude_and_rename() { "sql parser error: Expected end of statement, found: EXCLUDE" ); } + +#[test] +fn test_alter_table_swap_with() { + let sql = "ALTER TABLE tab1 SWAP WITH tab2"; + match snowflake_and_generic().verified_stmt(sql) { + Statement::AlterTable { + name, + operation: AlterTableOperation::SwapWith { table_name }, + } => { + assert_eq!("tab1", name.to_string()); + assert_eq!("tab2", table_name.to_string()); + } + _ => unreachable!(), + }; +}