Skip to content

Commit 0a6e8f0

Browse files
committed
Make COLUMN optional and fix whitespace handling in fmt::Display implementation
1 parent 01136f0 commit 0a6e8f0

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/ast/ddl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum AlterTableOperation {
2727
AddColumn { column_def: ColumnDef },
2828
/// TODO: implement `DROP CONSTRAINT <name>`
2929
DropConstraint { name: Ident },
30-
/// `DROP COLUMN [ IF EXISTS ] <column> [ CASCADE ]`
30+
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
3131
DropColumn {
3232
column_name: Ident,
3333
if_exists: bool,
@@ -56,10 +56,10 @@ impl fmt::Display for AlterTableOperation {
5656
cascade,
5757
} => write!(
5858
f,
59-
"DROP COLUMN {}{} {}",
60-
if *if_exists { "IF EXISTS " } else { " " },
59+
"DROP COLUMN {}{}{}",
60+
if *if_exists { "IF EXISTS " } else { "" },
6161
column_name,
62-
if *cascade { "CASCADE" } else { "" }
62+
if *cascade { " CASCADE" } else { "" }
6363
),
6464
AlterTableOperation::RenameColumn {
6565
old_column_name,

src/parser.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,17 +1341,14 @@ impl Parser {
13411341
}
13421342
}
13431343
} else if self.parse_keyword(Keyword::DROP) {
1344-
if self.parse_keyword(Keyword::COLUMN) {
1345-
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
1346-
let column_name = self.parse_identifier()?;
1347-
let cascade = self.parse_keyword(Keyword::CASCADE);
1348-
AlterTableOperation::DropColumn {
1349-
column_name,
1350-
if_exists,
1351-
cascade,
1352-
}
1353-
} else {
1354-
return self.expected("a column in ALTER TABLE .. DROP", self.peek_token());
1344+
let _ = self.parse_keyword(Keyword::COLUMN);
1345+
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
1346+
let column_name = self.parse_identifier()?;
1347+
let cascade = self.parse_keyword(Keyword::CASCADE);
1348+
AlterTableOperation::DropColumn {
1349+
column_name,
1350+
if_exists,
1351+
cascade,
13551352
}
13561353
} else {
13571354
return self.expected("ADD, RENAME, or DROP after ALTER TABLE", self.peek_token());

tests/sqlparser_common.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,14 @@ fn parse_alter_table_constraints() {
13911391
#[test]
13921392
fn parse_alter_table_drop_column() {
13931393
check_one("DROP COLUMN IF EXISTS is_active CASCADE");
1394+
one_statement_parses_to(
1395+
"ALTER TABLE tab DROP IF EXISTS is_active CASCADE",
1396+
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
1397+
);
1398+
one_statement_parses_to(
1399+
"ALTER TABLE tab DROP is_active CASCADE",
1400+
"ALTER TABLE tab DROP COLUMN is_active CASCADE",
1401+
);
13941402

13951403
fn check_one(constraint_text: &str) {
13961404
match verified_stmt(&format!("ALTER TABLE tab {}", constraint_text)) {

0 commit comments

Comments
 (0)