Skip to content

Commit 310553f

Browse files
yoavcloudayman-sigma
authored andcommitted
Add support for dropping multiple columns in Snowflake (apache#1918)
1 parent 852b4e4 commit 310553f

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
@@ -1076,6 +1076,11 @@ pub trait Dialect: Debug + Any {
10761076
fn supports_alter_column_type_using(&self) -> bool {
10771077
false
10781078
}
1079+
1080+
/// Returns true if the dialect supports `ALTER TABLE tbl DROP COLUMN c1, ..., cn`
1081+
fn supports_comma_separated_drop_column_list(&self) -> bool {
1082+
false
1083+
}
10791084
}
10801085

10811086
/// 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
@@ -8706,11 +8706,15 @@ impl<'a> Parser<'a> {
87068706
} else {
87078707
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
87088708
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8709-
let column_name = self.parse_identifier()?;
8709+
let column_names = if self.dialect.supports_comma_separated_drop_column_list() {
8710+
self.parse_comma_separated(Parser::parse_identifier)?
8711+
} else {
8712+
vec![self.parse_identifier()?]
8713+
};
87108714
let drop_behavior = self.parse_optional_drop_behavior();
87118715
AlterTableOperation::DropColumn {
87128716
has_column_keyword,
8713-
column_name,
8717+
column_names,
87148718
if_exists,
87158719
drop_behavior,
87168720
}

tests/sqlparser_common.rs

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

5000+
let dialects = all_dialects_where(|d| d.supports_comma_separated_drop_column_list());
5001+
dialects.verified_stmt("ALTER TABLE tbl DROP COLUMN c1, c2, c3");
5002+
50005003
fn check_one(constraint_text: &str) {
50015004
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
50025005
AlterTableOperation::DropColumn {
50035006
has_column_keyword: true,
5004-
column_name,
5007+
column_names,
50055008
if_exists,
50065009
drop_behavior,
50075010
} => {
5008-
assert_eq!("is_active", column_name.to_string());
5011+
assert_eq!("is_active", column_names.first().unwrap().to_string());
50095012
assert!(if_exists);
50105013
match drop_behavior {
50115014
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)