Skip to content

Commit d2466af

Browse files
authored
Add support for dropping multiple columns in Snowflake (#1918)
1 parent b0bcc46 commit d2466af

File tree

7 files changed

+28
-12
lines changed

7 files changed

+28
-12
lines changed

src/ast/ddl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ pub enum AlterTableOperation {
140140
name: Ident,
141141
drop_behavior: Option<DropBehavior>,
142142
},
143-
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
143+
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ , <column_name>, ... ] [ CASCADE ]`
144144
DropColumn {
145145
has_column_keyword: bool,
146-
column_name: Ident,
146+
column_names: Vec<Ident>,
147147
if_exists: bool,
148148
drop_behavior: Option<DropBehavior>,
149149
},
@@ -631,15 +631,15 @@ impl fmt::Display for AlterTableOperation {
631631
AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
632632
AlterTableOperation::DropColumn {
633633
has_column_keyword,
634-
column_name,
634+
column_names: column_name,
635635
if_exists,
636636
drop_behavior,
637637
} => write!(
638638
f,
639639
"DROP {}{}{}{}",
640640
if *has_column_keyword { "COLUMN " } else { "" },
641641
if *if_exists { "IF EXISTS " } else { "" },
642-
column_name,
642+
display_comma_separated(column_name),
643643
match drop_behavior {
644644
None => "",
645645
Some(DropBehavior::Restrict) => " RESTRICT",

src/ast/spans.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,10 +1112,10 @@ impl Spanned for AlterTableOperation {
11121112
} => name.span,
11131113
AlterTableOperation::DropColumn {
11141114
has_column_keyword: _,
1115-
column_name,
1115+
column_names,
11161116
if_exists: _,
11171117
drop_behavior: _,
1118-
} => column_name.span,
1118+
} => union_spans(column_names.iter().map(|i| i.span)),
11191119
AlterTableOperation::AttachPartition { partition } => partition.span(),
11201120
AlterTableOperation::DetachPartition { partition } => partition.span(),
11211121
AlterTableOperation::FreezePartition {

src/dialect/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,11 @@ pub trait Dialect: Debug + Any {
10711071
fn supports_alter_column_type_using(&self) -> bool {
10721072
false
10731073
}
1074+
1075+
/// Returns true if the dialect supports `ALTER TABLE tbl DROP COLUMN c1, ..., cn`
1076+
fn supports_comma_separated_drop_column_list(&self) -> bool {
1077+
false
1078+
}
10741079
}
10751080

10761081
/// This represents the operators for which precedence must be defined

src/dialect/snowflake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ impl Dialect for SnowflakeDialect {
364364
fn supports_space_separated_column_options(&self) -> bool {
365365
true
366366
}
367+
368+
fn supports_comma_separated_drop_column_list(&self) -> bool {
369+
true
370+
}
367371
}
368372

369373
fn parse_file_staging_command(kw: Keyword, parser: &mut Parser) -> Result<Statement, ParserError> {

src/parser/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8675,11 +8675,15 @@ impl<'a> Parser<'a> {
86758675
} else {
86768676
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
86778677
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8678-
let column_name = self.parse_identifier()?;
8678+
let column_names = if self.dialect.supports_comma_separated_drop_column_list() {
8679+
self.parse_comma_separated(Parser::parse_identifier)?
8680+
} else {
8681+
vec![self.parse_identifier()?]
8682+
};
86798683
let drop_behavior = self.parse_optional_drop_behavior();
86808684
AlterTableOperation::DropColumn {
86818685
has_column_keyword,
8682-
column_name,
8686+
column_names,
86838687
if_exists,
86848688
drop_behavior,
86858689
}

tests/sqlparser_common.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4996,15 +4996,18 @@ fn parse_alter_table_drop_column() {
49964996
"ALTER TABLE tab DROP is_active CASCADE",
49974997
);
49984998

4999+
let dialects = all_dialects_where(|d| d.supports_comma_separated_drop_column_list());
5000+
dialects.verified_stmt("ALTER TABLE tbl DROP COLUMN c1, c2, c3");
5001+
49995002
fn check_one(constraint_text: &str) {
50005003
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
50015004
AlterTableOperation::DropColumn {
50025005
has_column_keyword: true,
5003-
column_name,
5006+
column_names,
50045007
if_exists,
50055008
drop_behavior,
50065009
} => {
5007-
assert_eq!("is_active", column_name.to_string());
5010+
assert_eq!("is_active", column_names.first().unwrap().to_string());
50085011
assert!(if_exists);
50095012
match drop_behavior {
50105013
None => assert!(constraint_text.ends_with(" is_active")),

tests/sqlparser_mysql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,7 +2876,7 @@ fn parse_alter_table_with_algorithm() {
28762876
vec![
28772877
AlterTableOperation::DropColumn {
28782878
has_column_keyword: true,
2879-
column_name: Ident::new("password_digest"),
2879+
column_names: vec![Ident::new("password_digest")],
28802880
if_exists: false,
28812881
drop_behavior: None,
28822882
},
@@ -2924,7 +2924,7 @@ fn parse_alter_table_with_lock() {
29242924
vec![
29252925
AlterTableOperation::DropColumn {
29262926
has_column_keyword: true,
2927-
column_name: Ident::new("password_digest"),
2927+
column_names: vec![Ident::new("password_digest")],
29282928
if_exists: false,
29292929
drop_behavior: None,
29302930
},

0 commit comments

Comments
 (0)