Skip to content

Add support for dropping multiple columns in Snowflake #1918

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

Merged
merged 1 commit into from
Jul 5, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ pub enum AlterTableOperation {
name: Ident,
drop_behavior: Option<DropBehavior>,
},
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ , <column_name>, ... ] [ CASCADE ]`
DropColumn {
has_column_keyword: bool,
column_name: Ident,
column_names: Vec<Ident>,
if_exists: bool,
drop_behavior: Option<DropBehavior>,
},
Expand Down Expand Up @@ -631,15 +631,15 @@ impl fmt::Display for AlterTableOperation {
AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
AlterTableOperation::DropColumn {
has_column_keyword,
column_name,
column_names: column_name,
if_exists,
drop_behavior,
} => write!(
f,
"DROP {}{}{}{}",
if *has_column_keyword { "COLUMN " } else { "" },
if *if_exists { "IF EXISTS " } else { "" },
column_name,
display_comma_separated(column_name),
match drop_behavior {
None => "",
Some(DropBehavior::Restrict) => " RESTRICT",
Expand Down
4 changes: 2 additions & 2 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,10 +1111,10 @@ impl Spanned for AlterTableOperation {
} => name.span,
AlterTableOperation::DropColumn {
has_column_keyword: _,
column_name,
column_names,
if_exists: _,
drop_behavior: _,
} => column_name.span,
} => union_spans(column_names.iter().map(|i| i.span)),
AlterTableOperation::AttachPartition { partition } => partition.span(),
AlterTableOperation::DetachPartition { partition } => partition.span(),
AlterTableOperation::FreezePartition {
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,11 @@ pub trait Dialect: Debug + Any {
fn supports_alter_column_type_using(&self) -> bool {
false
}

/// Returns true if the dialect supports `ALTER TABLE tbl DROP COLUMN c1, ..., cn`
fn supports_comma_separated_drop_column_list(&self) -> bool {
false
}
}

/// This represents the operators for which precedence must be defined
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ impl Dialect for SnowflakeDialect {
fn supports_space_separated_column_options(&self) -> bool {
true
}

fn supports_comma_separated_drop_column_list(&self) -> bool {
true
}
}

fn parse_file_staging_command(kw: Keyword, parser: &mut Parser) -> Result<Statement, ParserError> {
Expand Down
8 changes: 6 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8673,11 +8673,15 @@ impl<'a> Parser<'a> {
} else {
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let column_name = self.parse_identifier()?;
let column_names = if self.dialect.supports_comma_separated_drop_column_list() {
self.parse_comma_separated(Parser::parse_identifier)?
} else {
vec![self.parse_identifier()?]
};
let drop_behavior = self.parse_optional_drop_behavior();
AlterTableOperation::DropColumn {
has_column_keyword,
column_name,
column_names,
if_exists,
drop_behavior,
}
Expand Down
7 changes: 5 additions & 2 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4979,15 +4979,18 @@ fn parse_alter_table_drop_column() {
"ALTER TABLE tab DROP is_active CASCADE",
);

let dialects = all_dialects_where(|d| d.supports_comma_separated_drop_column_list());
dialects.verified_stmt("ALTER TABLE tbl DROP COLUMN c1, c2, c3");

fn check_one(constraint_text: &str) {
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
AlterTableOperation::DropColumn {
has_column_keyword: true,
column_name,
column_names,
if_exists,
drop_behavior,
} => {
assert_eq!("is_active", column_name.to_string());
assert_eq!("is_active", column_names.first().unwrap().to_string());
assert!(if_exists);
match drop_behavior {
None => assert!(constraint_text.ends_with(" is_active")),
Expand Down
4 changes: 2 additions & 2 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2876,7 +2876,7 @@ fn parse_alter_table_with_algorithm() {
vec![
AlterTableOperation::DropColumn {
has_column_keyword: true,
column_name: Ident::new("password_digest"),
column_names: vec![Ident::new("password_digest")],
if_exists: false,
drop_behavior: None,
},
Expand Down Expand Up @@ -2924,7 +2924,7 @@ fn parse_alter_table_with_lock() {
vec![
AlterTableOperation::DropColumn {
has_column_keyword: true,
column_name: Ident::new("password_digest"),
column_names: vec![Ident::new("password_digest")],
if_exists: false,
drop_behavior: None,
},
Expand Down