Skip to content

Commit 12c0878

Browse files
authored
Add drop behavior to DROP PRIMARY/FOREIGN KEY (apache#2002)
1 parent b660a3b commit 12c0878

File tree

5 files changed

+67
-34
lines changed

5 files changed

+67
-34
lines changed

src/ast/ddl.rs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,18 @@ pub enum AlterTableOperation {
200200
},
201201
/// `DROP PRIMARY KEY`
202202
///
203-
/// Note: this is a [MySQL]-specific operation.
204-
///
205-
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
206-
DropPrimaryKey,
203+
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html)
204+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/constraints-drop)
205+
DropPrimaryKey {
206+
drop_behavior: Option<DropBehavior>,
207+
},
207208
/// `DROP FOREIGN KEY <fk_symbol>`
208209
///
209-
/// Note: this is a [MySQL]-specific operation.
210-
///
211-
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
210+
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html)
211+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/constraints-drop)
212212
DropForeignKey {
213213
name: Ident,
214+
drop_behavior: Option<DropBehavior>,
214215
},
215216
/// `DROP INDEX <index_name>`
216217
///
@@ -648,36 +649,51 @@ impl fmt::Display for AlterTableOperation {
648649
} => {
649650
write!(
650651
f,
651-
"DROP CONSTRAINT {}{}{}",
652+
"DROP CONSTRAINT {}{}",
652653
if *if_exists { "IF EXISTS " } else { "" },
653-
name,
654-
match drop_behavior {
655-
None => "",
656-
Some(DropBehavior::Restrict) => " RESTRICT",
657-
Some(DropBehavior::Cascade) => " CASCADE",
658-
}
659-
)
654+
name
655+
)?;
656+
if let Some(drop_behavior) = drop_behavior {
657+
write!(f, " {drop_behavior}")?;
658+
}
659+
Ok(())
660+
}
661+
AlterTableOperation::DropPrimaryKey { drop_behavior } => {
662+
write!(f, "DROP PRIMARY KEY")?;
663+
if let Some(drop_behavior) = drop_behavior {
664+
write!(f, " {drop_behavior}")?;
665+
}
666+
Ok(())
667+
}
668+
AlterTableOperation::DropForeignKey {
669+
name,
670+
drop_behavior,
671+
} => {
672+
write!(f, "DROP FOREIGN KEY {name}")?;
673+
if let Some(drop_behavior) = drop_behavior {
674+
write!(f, " {drop_behavior}")?;
675+
}
676+
Ok(())
660677
}
661-
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
662-
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
663678
AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
664679
AlterTableOperation::DropColumn {
665680
has_column_keyword,
666681
column_names: column_name,
667682
if_exists,
668683
drop_behavior,
669-
} => write!(
670-
f,
671-
"DROP {}{}{}{}",
672-
if *has_column_keyword { "COLUMN " } else { "" },
673-
if *if_exists { "IF EXISTS " } else { "" },
674-
display_comma_separated(column_name),
675-
match drop_behavior {
676-
None => "",
677-
Some(DropBehavior::Restrict) => " RESTRICT",
678-
Some(DropBehavior::Cascade) => " CASCADE",
684+
} => {
685+
write!(
686+
f,
687+
"DROP {}{}{}",
688+
if *has_column_keyword { "COLUMN " } else { "" },
689+
if *if_exists { "IF EXISTS " } else { "" },
690+
display_comma_separated(column_name),
691+
)?;
692+
if let Some(drop_behavior) = drop_behavior {
693+
write!(f, " {drop_behavior}")?;
679694
}
680-
),
695+
Ok(())
696+
}
681697
AlterTableOperation::AttachPartition { partition } => {
682698
write!(f, "ATTACH {partition}")
683699
}

src/ast/spans.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,8 @@ impl Spanned for AlterTableOperation {
11501150
} => partition
11511151
.span()
11521152
.union_opt(&with_name.as_ref().map(|n| n.span)),
1153-
AlterTableOperation::DropPrimaryKey => Span::empty(),
1154-
AlterTableOperation::DropForeignKey { name } => name.span,
1153+
AlterTableOperation::DropPrimaryKey { .. } => Span::empty(),
1154+
AlterTableOperation::DropForeignKey { name, .. } => name.span,
11551155
AlterTableOperation::DropIndex { name } => name.span,
11561156
AlterTableOperation::EnableAlwaysRule { name } => name.span,
11571157
AlterTableOperation::EnableAlwaysTrigger { name } => name.span,

src/parser/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8930,10 +8930,15 @@ impl<'a> Parser<'a> {
89308930
drop_behavior,
89318931
}
89328932
} else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
8933-
AlterTableOperation::DropPrimaryKey
8933+
let drop_behavior = self.parse_optional_drop_behavior();
8934+
AlterTableOperation::DropPrimaryKey { drop_behavior }
89348935
} else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::KEY]) {
89358936
let name = self.parse_identifier()?;
8936-
AlterTableOperation::DropForeignKey { name }
8937+
let drop_behavior = self.parse_optional_drop_behavior();
8938+
AlterTableOperation::DropForeignKey {
8939+
name,
8940+
drop_behavior,
8941+
}
89378942
} else if self.parse_keyword(Keyword::INDEX) {
89388943
let name = self.parse_identifier()?;
89398944
AlterTableOperation::DropIndex { name }

tests/sqlparser_mysql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,9 @@ fn parse_alter_table_add_columns() {
27522752
fn parse_alter_table_drop_primary_key() {
27532753
assert_matches!(
27542754
alter_table_op(mysql_and_generic().verified_stmt("ALTER TABLE tab DROP PRIMARY KEY")),
2755-
AlterTableOperation::DropPrimaryKey
2755+
AlterTableOperation::DropPrimaryKey {
2756+
drop_behavior: None
2757+
}
27562758
);
27572759
}
27582760

@@ -2762,7 +2764,7 @@ fn parse_alter_table_drop_foreign_key() {
27622764
alter_table_op(
27632765
mysql_and_generic().verified_stmt("ALTER TABLE tab DROP FOREIGN KEY foo_ibfk_1")
27642766
),
2765-
AlterTableOperation::DropForeignKey { name } if name.value == "foo_ibfk_1"
2767+
AlterTableOperation::DropForeignKey { name, .. } if name.value == "foo_ibfk_1"
27662768
);
27672769
}
27682770

tests/sqlparser_snowflake.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4571,3 +4571,13 @@ fn test_create_database() {
45714571
.to_string();
45724572
assert!(err.contains("Expected"), "Unexpected error: {err}");
45734573
}
4574+
4575+
#[test]
4576+
fn test_drop_constraints() {
4577+
snowflake().verified_stmt("ALTER TABLE tbl DROP PRIMARY KEY");
4578+
snowflake().verified_stmt("ALTER TABLE tbl DROP FOREIGN KEY k1");
4579+
snowflake().verified_stmt("ALTER TABLE tbl DROP CONSTRAINT c1");
4580+
snowflake().verified_stmt("ALTER TABLE tbl DROP PRIMARY KEY CASCADE");
4581+
snowflake().verified_stmt("ALTER TABLE tbl DROP FOREIGN KEY k1 RESTRICT");
4582+
snowflake().verified_stmt("ALTER TABLE tbl DROP CONSTRAINT c1 CASCADE");
4583+
}

0 commit comments

Comments
 (0)