Skip to content

Commit

Permalink
support snowflake alter table swap with
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Leszczynski <leszczynski.pawel@gmail.com>
  • Loading branch information
pawel-big-lebowski committed Mar 7, 2023
1 parent 1cf913e commit cc487de
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ pub enum AlterTableOperation {
column_name: Ident,
op: AlterColumnOperation,
},
/// 'SWAP WITH <table_name>'
///
/// Note: this is Snowflake specific <https://docs.snowflake.com/en/sql-reference/sql/alter-table>
SwapWith { table_name: ObjectName },
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -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}")
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ define_keywords!(
SUM,
SUPER,
SUPERUSER,
SWAP,
SYMMETRIC,
SYNC,
SYSTEM,
Expand Down
6 changes: 5 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
};
Expand Down
15 changes: 15 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
};
}

0 comments on commit cc487de

Please sign in to comment.