Skip to content
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

Support ALTER TABLE DROP PRIMARY KEY #682

Merged
merged 3 commits into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/ddl.rs
Expand Up @@ -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<Expr>,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/parser.rs
Expand Up @@ -3136,6 +3136,9 @@ impl<'a> Parser<'a> {
name,
cascade,
}
} else if self.parse_keyword(Keyword::PRIMARY) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ding-young ALTER TABLE tb DROP PRIMARY isn't allowed and, therefore, shouldn't be acceptable.

The let _ in the next line is permissive over this.

Also, I think this is a MySQL-specific statement. Could you please add the validation to accept only MySQL and GenericDialect, please?

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]);
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlparser_common.rs
Expand Up @@ -2778,6 +2778,19 @@ 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");
Expand Down