Skip to content

Commit e4591d2

Browse files
committed
Add support for dropping multiple columns in Snowflake
1 parent 9020385 commit e4591d2

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
@@ -1111,10 +1111,10 @@ impl Spanned for AlterTableOperation {
11111111
} => name.span,
11121112
AlterTableOperation::DropColumn {
11131113
has_column_keyword: _,
1114-
column_name,
1114+
column_names,
11151115
if_exists: _,
11161116
drop_behavior: _,
1117-
} => column_name.span,
1117+
} => union_spans(column_names.iter().map(|i| i.span)),
11181118
AlterTableOperation::AttachPartition { partition } => partition.span(),
11191119
AlterTableOperation::DetachPartition { partition } => partition.span(),
11201120
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
@@ -8673,11 +8673,15 @@ impl<'a> Parser<'a> {
86738673
} else {
86748674
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
86758675
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8676-
let column_name = self.parse_identifier()?;
8676+
let column_names = if self.dialect.supports_comma_separated_drop_column_list() {
8677+
self.parse_comma_separated(Parser::parse_identifier)?
8678+
} else {
8679+
vec![self.parse_identifier()?]
8680+
};
86778681
let drop_behavior = self.parse_optional_drop_behavior();
86788682
AlterTableOperation::DropColumn {
86798683
has_column_keyword,
8680-
column_name,
8684+
column_names,
86818685
if_exists,
86828686
drop_behavior,
86838687
}

tests/sqlparser_common.rs

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

4982+
let dialects = all_dialects_where(|d| d.supports_comma_separated_drop_column_list());
4983+
dialects.verified_stmt("ALTER TABLE tbl DROP COLUMN c1, c2, c3");
4984+
49824985
fn check_one(constraint_text: &str) {
49834986
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
49844987
AlterTableOperation::DropColumn {
49854988
has_column_keyword: true,
4986-
column_name,
4989+
column_names,
49874990
if_exists,
49884991
drop_behavior,
49894992
} => {
4990-
assert_eq!("is_active", column_name.to_string());
4993+
assert_eq!("is_active", column_names.first().unwrap().to_string());
49914994
assert!(if_exists);
49924995
match drop_behavior {
49934996
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)