Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support snowflake alter table swap with #825

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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!(),
};
}