Skip to content

Commit 4af0adb

Browse files
support snowflake alter table swap with
Signed-off-by: Pawel Leszczynski <[email protected]>
1 parent 1cf913e commit 4af0adb

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/ast/ddl.rs

+7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ pub enum AlterTableOperation {
9696
column_name: Ident,
9797
op: AlterColumnOperation,
9898
},
99+
/// 'SWAP WITH <table_name>'
100+
///
101+
/// Note: this is Snowflake specific https://docs.snowflake.com/en/sql-reference/sql/alter-table
102+
SwapWith { table_name: ObjectName },
99103
}
100104

101105
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -203,6 +207,9 @@ impl fmt::Display for AlterTableOperation {
203207
AlterTableOperation::RenameConstraint { old_name, new_name } => {
204208
write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}")
205209
}
210+
AlterTableOperation::SwapWith { table_name} => {
211+
write!(f, "SWAP WITH {table_name}")
212+
}
206213
}
207214
}
208215
}

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ define_keywords!(
545545
SUM,
546546
SUPER,
547547
SUPERUSER,
548+
SWAP,
548549
SYMMETRIC,
549550
SYNC,
550551
SYSTEM,

src/parser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3891,9 +3891,13 @@ impl<'a> Parser<'a> {
38913891
);
38923892
};
38933893
AlterTableOperation::AlterColumn { column_name, op }
3894+
} else if self.parse_keyword(Keyword::SWAP) {
3895+
self.expect_keyword(Keyword::WITH)?;
3896+
let table_name = self.parse_object_name()?;
3897+
AlterTableOperation::SwapWith { table_name }
38943898
} else {
38953899
return self.expected(
3896-
"ADD, RENAME, PARTITION or DROP after ALTER TABLE",
3900+
"ADD, RENAME, PARTITION, SWAP or DROP after ALTER TABLE",
38973901
self.peek_token(),
38983902
);
38993903
};

tests/sqlparser_snowflake.rs

+18
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,21 @@ fn test_select_wildcard_with_exclude_and_rename() {
493493
"sql parser error: Expected end of statement, found: EXCLUDE"
494494
);
495495
}
496+
497+
#[test]
498+
fn test_alter_table_swap_with() {
499+
let sql = "ALTER TABLE tab1 SWAP WITH tab2";
500+
match snowflake_and_generic().verified_stmt(sql) {
501+
Statement::AlterTable {
502+
name,
503+
operation:
504+
AlterTableOperation::SwapWith {
505+
table_name
506+
},
507+
} => {
508+
assert_eq!("tab1", name.to_string());
509+
assert_eq!("tab2", table_name.to_string());
510+
}
511+
_ => unreachable!(),
512+
};
513+
}

0 commit comments

Comments
 (0)