From b128031907bd4f1434c10fd8627d168de5bc42c2 Mon Sep 17 00:00:00 2001 From: Seoyoung Lee Date: Fri, 21 Oct 2022 12:37:52 +0900 Subject: [PATCH 1/3] parse alter table drop primary key --- src/ast/ddl.rs | 5 +++++ src/parser.rs | 3 +++ tests/sqlparser_common.rs | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index cae0f597b..c758b4f55 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -44,6 +44,10 @@ pub enum AlterTableOperation { if_exists: bool, cascade: bool, }, + /// `DROP PRIMARY KEY` + /// + /// Note: this is a MySQL-specific operation. + DropPrimaryKey, /// `RENAME TO PARTITION (partition=val)` RenamePartitions { old_partitions: Vec, @@ -124,6 +128,7 @@ impl fmt::Display for AlterTableOperation { if *cascade { " CASCADE" } else { "" }, ) } + AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"), AlterTableOperation::DropColumn { column_name, if_exists, diff --git a/src/parser.rs b/src/parser.rs index 108427889..d69dc3552 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3136,6 +3136,9 @@ impl<'a> Parser<'a> { name, cascade, } + } else if self.parse_keyword(Keyword::PRIMARY) { + let _ = self.parse_keyword(Keyword::KEY); + AlterTableOperation::DropPrimaryKey } else { let _ = self.parse_keyword(Keyword::COLUMN); let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]); diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index bae310ef0..3ee4539ed 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2778,6 +2778,18 @@ fn parse_alter_table_drop_constraint() { ); } +#[test] +fn parse_alter_table_drop_primary_key() { + match verified_stmt("ALTER TABLE tab DROP PRIMARY KEY") { + Statement::AlterTable { + name, + operation: + AlterTableOperation::DropPrimaryKey, + } => { assert_eq!("tab", name.to_string()); } + _ => unreachable!(), + } +} + #[test] fn parse_bad_constraint() { let res = parse_sql_statements("ALTER TABLE tab ADD"); From 6e0616ad82ac693dc8de3de7d2311f3b57143c68 Mon Sep 17 00:00:00 2001 From: Seoyoung Lee Date: Fri, 21 Oct 2022 12:46:01 +0900 Subject: [PATCH 2/3] cargo nightly fmt --- tests/sqlparser_common.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 3ee4539ed..07acc0ef2 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2783,9 +2783,10 @@ fn parse_alter_table_drop_primary_key() { match verified_stmt("ALTER TABLE tab DROP PRIMARY KEY") { Statement::AlterTable { name, - operation: - AlterTableOperation::DropPrimaryKey, - } => { assert_eq!("tab", name.to_string()); } + operation: AlterTableOperation::DropPrimaryKey, + } => { + assert_eq!("tab", name.to_string()); + } _ => unreachable!(), } } From 11cab1f194bf4bb0f32efd0e7ee6bafd2bd9bea3 Mon Sep 17 00:00:00 2001 From: Seoyoung Lee Date: Tue, 1 Nov 2022 05:14:51 +0900 Subject: [PATCH 3/3] add Dialect validation --- src/parser.rs | 5 +++-- tests/sqlparser_common.rs | 13 ------------- tests/sqlparser_mysql.rs | 13 +++++++++++++ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index d69dc3552..d1c2911fc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3136,8 +3136,9 @@ impl<'a> Parser<'a> { name, cascade, } - } else if self.parse_keyword(Keyword::PRIMARY) { - let _ = self.parse_keyword(Keyword::KEY); + } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) + && dialect_of!(self is MySqlDialect | GenericDialect) + { AlterTableOperation::DropPrimaryKey } else { let _ = self.parse_keyword(Keyword::COLUMN); diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 07acc0ef2..bae310ef0 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2778,19 +2778,6 @@ fn parse_alter_table_drop_constraint() { ); } -#[test] -fn parse_alter_table_drop_primary_key() { - match verified_stmt("ALTER TABLE tab DROP PRIMARY KEY") { - Statement::AlterTable { - name, - operation: AlterTableOperation::DropPrimaryKey, - } => { - assert_eq!("tab", name.to_string()); - } - _ => unreachable!(), - } -} - #[test] fn parse_bad_constraint() { let res = parse_sql_statements("ALTER TABLE tab ADD"); diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 8c42c715b..52c0205d6 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -875,6 +875,19 @@ fn parse_update_with_joins() { } } +#[test] +fn parse_alter_table_drop_primary_key() { + match mysql_and_generic().verified_stmt("ALTER TABLE tab DROP PRIMARY KEY") { + Statement::AlterTable { + name, + operation: AlterTableOperation::DropPrimaryKey, + } => { + assert_eq!("tab", name.to_string()); + } + _ => unreachable!(), + } +} + #[test] fn parse_alter_table_change_column() { let expected_name = ObjectName(vec![Ident::new("orders")]);