Skip to content

Keep the COLUMN keyword only if it exists when dropping the column #1862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub enum AlterTableOperation {
},
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
DropColumn {
has_column_keyword: bool,
column_name: Ident,
if_exists: bool,
drop_behavior: Option<DropBehavior>,
Expand Down Expand Up @@ -606,12 +607,14 @@ impl fmt::Display for AlterTableOperation {
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
AlterTableOperation::DropColumn {
has_column_keyword,
column_name,
if_exists,
drop_behavior,
} => write!(
f,
"DROP COLUMN {}{}{}",
"DROP {}{}{}{}",
if *has_column_keyword { "COLUMN " } else { "" },
if *if_exists { "IF EXISTS " } else { "" },
column_name,
match drop_behavior {
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ impl Spanned for AlterTableOperation {
drop_behavior: _,
} => name.span,
AlterTableOperation::DropColumn {
has_column_keyword: _,
column_name,
if_exists: _,
drop_behavior: _,
Expand Down
3 changes: 2 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8608,11 +8608,12 @@ impl<'a> Parser<'a> {
} else if self.parse_keywords(&[Keyword::CLUSTERING, Keyword::KEY]) {
AlterTableOperation::DropClusteringKey
} else {
let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let column_name = self.parse_identifier()?;
let drop_behavior = self.parse_optional_drop_behavior();
AlterTableOperation::DropColumn {
has_column_keyword,
column_name,
if_exists,
drop_behavior,
Expand Down
5 changes: 3 additions & 2 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4926,17 +4926,18 @@ fn parse_alter_table_drop_column() {
check_one("DROP COLUMN IF EXISTS is_active CASCADE");
check_one("DROP COLUMN IF EXISTS is_active RESTRICT");
one_statement_parses_to(
"ALTER TABLE tab DROP IF EXISTS is_active CASCADE",
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
);
one_statement_parses_to(
"ALTER TABLE tab DROP is_active CASCADE",
"ALTER TABLE tab DROP COLUMN is_active CASCADE",
"ALTER TABLE tab DROP is_active CASCADE",
);

fn check_one(constraint_text: &str) {
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
AlterTableOperation::DropColumn {
has_column_keyword: true,
column_name,
if_exists,
drop_behavior,
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,7 @@ fn parse_alter_table_with_algorithm() {
operations,
vec![
AlterTableOperation::DropColumn {
has_column_keyword: true,
column_name: Ident::new("password_digest"),
if_exists: false,
drop_behavior: None,
Expand Down Expand Up @@ -2848,6 +2849,7 @@ fn parse_alter_table_with_lock() {
operations,
vec![
AlterTableOperation::DropColumn {
has_column_keyword: true,
column_name: Ident::new("password_digest"),
if_exists: false,
drop_behavior: None,
Expand Down
Loading