This is an automated email from the ASF dual-hosted git repository.

iffyio pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 1e54a34a Parse MySQL `ALTER TABLE DROP FOREIGN KEY` syntax (#1762)
1e54a34a is described below

commit 1e54a34acdea192c3d67330e604e0bf9ce8bf866
Author: Michael Victor Zink <[email protected]>
AuthorDate: Tue Mar 11 23:34:18 2025 -0700

    Parse MySQL `ALTER TABLE DROP FOREIGN KEY` syntax (#1762)
---
 src/ast/ddl.rs           | 13 ++++++++++++-
 src/ast/spans.rs         |  1 +
 src/parser/mod.rs        |  7 ++++---
 tests/sqlparser_mysql.rs | 10 ++++++++++
 4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs
index 61963143..99d8521c 100644
--- a/src/ast/ddl.rs
+++ b/src/ast/ddl.rs
@@ -151,8 +151,18 @@ pub enum AlterTableOperation {
     },
     /// `DROP PRIMARY KEY`
     ///
-    /// Note: this is a MySQL-specific operation.
+    /// Note: this is a [MySQL]-specific operation.
+    ///
+    /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
     DropPrimaryKey,
+    /// `DROP FOREIGN KEY <fk_symbol>`
+    ///
+    /// Note: this is a [MySQL]-specific operation.
+    ///
+    /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
+    DropForeignKey {
+        name: Ident,
+    },
     /// `ENABLE ALWAYS RULE rewrite_rule_name`
     ///
     /// Note: this is a PostgreSQL-specific operation.
@@ -530,6 +540,7 @@ impl fmt::Display for AlterTableOperation {
                 )
             }
             AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY 
KEY"),
+            AlterTableOperation::DropForeignKey { name } => write!(f, "DROP 
FOREIGN KEY {name}"),
             AlterTableOperation::DropColumn {
                 column_name,
                 if_exists,
diff --git a/src/ast/spans.rs b/src/ast/spans.rs
index 0a64fb8e..62ca9dc0 100644
--- a/src/ast/spans.rs
+++ b/src/ast/spans.rs
@@ -998,6 +998,7 @@ impl Spanned for AlterTableOperation {
                 .span()
                 .union_opt(&with_name.as_ref().map(|n| n.span)),
             AlterTableOperation::DropPrimaryKey => Span::empty(),
+            AlterTableOperation::DropForeignKey { name } => name.span,
             AlterTableOperation::EnableAlwaysRule { name } => name.span,
             AlterTableOperation::EnableAlwaysTrigger { name } => name.span,
             AlterTableOperation::EnableReplicaRule { name } => name.span,
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index b3441538..2b95d674 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -7998,10 +7998,11 @@ impl<'a> Parser<'a> {
                     name,
                     drop_behavior,
                 }
-            } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY])
-                && dialect_of!(self is MySqlDialect | GenericDialect)
-            {
+            } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
                 AlterTableOperation::DropPrimaryKey
+            } else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::KEY]) {
+                let name = self.parse_identifier()?;
+                AlterTableOperation::DropForeignKey { name }
             } else if self.parse_keyword(Keyword::PROJECTION)
                 && dialect_of!(self is ClickHouseDialect|GenericDialect)
             {
diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs
index 8d89ce4e..560ea9da 100644
--- a/tests/sqlparser_mysql.rs
+++ b/tests/sqlparser_mysql.rs
@@ -2273,6 +2273,16 @@ fn parse_alter_table_drop_primary_key() {
     );
 }
 
+#[test]
+fn parse_alter_table_drop_foreign_key() {
+    assert_matches!(
+        alter_table_op(
+            mysql_and_generic().verified_stmt("ALTER TABLE tab DROP FOREIGN 
KEY foo_ibfk_1")
+        ),
+        AlterTableOperation::DropForeignKey { name } if name.value == 
"foo_ibfk_1"
+    );
+}
+
 #[test]
 fn parse_alter_table_change_column() {
     let expected_name = ObjectName::from(vec![Ident::new("orders")]);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to