Skip to content

Commit 5481918

Browse files
support snowflake alter table swap with (#825)
Signed-off-by: Pawel Leszczynski <[email protected]>
1 parent 9ea396d commit 5481918

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-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
@@ -3909,9 +3909,13 @@ impl<'a> Parser<'a> {
39093909
);
39103910
};
39113911
AlterTableOperation::AlterColumn { column_name, op }
3912+
} else if self.parse_keyword(Keyword::SWAP) {
3913+
self.expect_keyword(Keyword::WITH)?;
3914+
let table_name = self.parse_object_name()?;
3915+
AlterTableOperation::SwapWith { table_name }
39123916
} else {
39133917
return self.expected(
3914-
"ADD, RENAME, PARTITION or DROP after ALTER TABLE",
3918+
"ADD, RENAME, PARTITION, SWAP or DROP after ALTER TABLE",
39153919
self.peek_token(),
39163920
);
39173921
};

tests/sqlparser_snowflake.rs

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

0 commit comments

Comments
 (0)