Skip to content

Commit e934a7f

Browse files
committed
Keep the COLUMN keyword only if it exists when dropping the column
1 parent 3017265 commit e934a7f

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

src/ast/ddl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub enum AlterTableOperation {
139139
},
140140
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
141141
DropColumn {
142+
has_column_keyword: bool,
142143
column_name: Ident,
143144
if_exists: bool,
144145
drop_behavior: Option<DropBehavior>,
@@ -606,12 +607,14 @@ impl fmt::Display for AlterTableOperation {
606607
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
607608
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
608609
AlterTableOperation::DropColumn {
610+
has_column_keyword,
609611
column_name,
610612
if_exists,
611613
drop_behavior,
612614
} => write!(
613615
f,
614-
"DROP COLUMN {}{}{}",
616+
"DROP {}{}{}{}",
617+
if *has_column_keyword { "COLUMN " } else { "" },
615618
if *if_exists { "IF EXISTS " } else { "" },
616619
column_name,
617620
match drop_behavior {

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ impl Spanned for AlterTableOperation {
10901090
drop_behavior: _,
10911091
} => name.span,
10921092
AlterTableOperation::DropColumn {
1093+
has_column_keyword: _,
10931094
column_name,
10941095
if_exists: _,
10951096
drop_behavior: _,

src/parser/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8608,11 +8608,12 @@ impl<'a> Parser<'a> {
86088608
} else if self.parse_keywords(&[Keyword::CLUSTERING, Keyword::KEY]) {
86098609
AlterTableOperation::DropClusteringKey
86108610
} else {
8611-
let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8611+
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
86128612
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
86138613
let column_name = self.parse_identifier()?;
86148614
let drop_behavior = self.parse_optional_drop_behavior();
86158615
AlterTableOperation::DropColumn {
8616+
has_column_keyword,
86168617
column_name,
86178618
if_exists,
86188619
drop_behavior,

tests/sqlparser_common.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4926,17 +4926,18 @@ fn parse_alter_table_drop_column() {
49264926
check_one("DROP COLUMN IF EXISTS is_active CASCADE");
49274927
check_one("DROP COLUMN IF EXISTS is_active RESTRICT");
49284928
one_statement_parses_to(
4929-
"ALTER TABLE tab DROP IF EXISTS is_active CASCADE",
4929+
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
49304930
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
49314931
);
49324932
one_statement_parses_to(
49334933
"ALTER TABLE tab DROP is_active CASCADE",
4934-
"ALTER TABLE tab DROP COLUMN is_active CASCADE",
4934+
"ALTER TABLE tab DROP is_active CASCADE",
49354935
);
49364936

49374937
fn check_one(constraint_text: &str) {
49384938
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
49394939
AlterTableOperation::DropColumn {
4940+
has_column_keyword: true,
49404941
column_name,
49414942
if_exists,
49424943
drop_behavior,

tests/sqlparser_mysql.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,7 @@ fn parse_alter_table_with_algorithm() {
28012801
operations,
28022802
vec![
28032803
AlterTableOperation::DropColumn {
2804+
has_column_keyword: true,
28042805
column_name: Ident::new("password_digest"),
28052806
if_exists: false,
28062807
drop_behavior: None,
@@ -2848,6 +2849,7 @@ fn parse_alter_table_with_lock() {
28482849
operations,
28492850
vec![
28502851
AlterTableOperation::DropColumn {
2852+
has_column_keyword: true,
28512853
column_name: Ident::new("password_digest"),
28522854
if_exists: false,
28532855
drop_behavior: None,

0 commit comments

Comments
 (0)