Skip to content

Commit 7338ce9

Browse files
committed
Add support for dropping multiple columns in Snowflake
1 parent f32a41a commit 7338ce9

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
@@ -137,10 +137,10 @@ pub enum AlterTableOperation {
137137
name: Ident,
138138
drop_behavior: Option<DropBehavior>,
139139
},
140-
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
140+
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ , <column_name>, ... ] [ CASCADE ]`
141141
DropColumn {
142142
has_column_keyword: bool,
143-
column_name: Ident,
143+
column_names: Vec<Ident>,
144144
if_exists: bool,
145145
drop_behavior: Option<DropBehavior>,
146146
},
@@ -615,15 +615,15 @@ impl fmt::Display for AlterTableOperation {
615615
AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
616616
AlterTableOperation::DropColumn {
617617
has_column_keyword,
618-
column_name,
618+
column_names: column_name,
619619
if_exists,
620620
drop_behavior,
621621
} => write!(
622622
f,
623623
"DROP {}{}{}{}",
624624
if *has_column_keyword { "COLUMN " } else { "" },
625625
if *if_exists { "IF EXISTS " } else { "" },
626-
column_name,
626+
display_comma_separated(column_name),
627627
match drop_behavior {
628628
None => "",
629629
Some(DropBehavior::Restrict) => " RESTRICT",

src/ast/spans.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,10 +1107,10 @@ impl Spanned for AlterTableOperation {
11071107
} => name.span,
11081108
AlterTableOperation::DropColumn {
11091109
has_column_keyword: _,
1110-
column_name,
1110+
column_names,
11111111
if_exists: _,
11121112
drop_behavior: _,
1113-
} => column_name.span,
1113+
} => union_spans(column_names.iter().map(|i| i.span)),
11141114
AlterTableOperation::AttachPartition { partition } => partition.span(),
11151115
AlterTableOperation::DetachPartition { partition } => partition.span(),
11161116
AlterTableOperation::FreezePartition {

src/dialect/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,11 @@ pub trait Dialect: Debug + Any {
10601060
fn supports_space_separated_column_options(&self) -> bool {
10611061
false
10621062
}
1063+
1064+
/// Returns true if the dialect supports `ALTER TABLE tbl DROP COLUMN c1, ..., cn`
1065+
fn supports_comma_separated_drop_column_list(&self) -> bool {
1066+
false
1067+
}
10631068
}
10641069

10651070
/// 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
@@ -8657,11 +8657,15 @@ impl<'a> Parser<'a> {
86578657
} else {
86588658
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
86598659
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8660-
let column_name = self.parse_identifier()?;
8660+
let column_names = if self.dialect.supports_comma_separated_drop_column_list() {
8661+
self.parse_comma_separated(Parser::parse_identifier)?
8662+
} else {
8663+
vec![self.parse_identifier()?]
8664+
};
86618665
let drop_behavior = self.parse_optional_drop_behavior();
86628666
AlterTableOperation::DropColumn {
86638667
has_column_keyword,
8664-
column_name,
8668+
column_names,
86658669
if_exists,
86668670
drop_behavior,
86678671
}

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)