Skip to content

Add support for mysql's drop index (ALTER TABLE table_a DROP INDEX idx_a) #1865

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ pub enum AlterTableOperation {
DropForeignKey {
name: Ident,
},
/// `DROP INDEX <index_name>`
///
/// Note: this is a [MySQL]-specific operation.
///
Comment on lines +192 to +193
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Note: this is a [MySQL]-specific operation.
///

I think we can skip the note, since we're already linking to the mysql docs below

/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
DropIndex {
name: Ident,
},
/// `ENABLE ALWAYS RULE rewrite_rule_name`
///
/// Note: this is a PostgreSQL-specific operation.
Expand Down Expand Up @@ -606,6 +614,7 @@ impl fmt::Display for AlterTableOperation {
}
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
AlterTableOperation::DropColumn {
has_column_keyword,
column_name,
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ impl Spanned for AlterTableOperation {
.union_opt(&with_name.as_ref().map(|n| n.span)),
AlterTableOperation::DropPrimaryKey => Span::empty(),
AlterTableOperation::DropForeignKey { name } => name.span,
AlterTableOperation::DropIndex { name } => name.span,
AlterTableOperation::EnableAlwaysRule { name } => name.span,
AlterTableOperation::EnableAlwaysTrigger { name } => name.span,
AlterTableOperation::EnableReplicaRule { name } => name.span,
Expand Down
3 changes: 3 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8611,6 +8611,9 @@ impl<'a> Parser<'a> {
} else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::KEY]) {
let name = self.parse_identifier()?;
AlterTableOperation::DropForeignKey { name }
} else if self.parse_keyword(Keyword::INDEX) {
let name = self.parse_identifier()?;
AlterTableOperation::DropIndex { name }
} else if self.parse_keyword(Keyword::PROJECTION)
&& dialect_of!(self is ClickHouseDialect|GenericDialect)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9132,7 +9132,9 @@ fn test_create_index_with_with_clause() {
#[test]
fn parse_drop_index() {
let sql = "DROP INDEX idx_a";
match verified_stmt(sql) {
// MySql dialect doesn't support `DROP INDEX idx_a`,you need to specify a specific table, please refer:
// [MySql](https://dev.mysql.com/doc/refman/8.4/en/drop-index.html)
match all_dialects_except(|d| d.is::<MySqlDialect>()).verified_stmt(sql) {
Comment on lines +9135 to +9137
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure I followed the intent here, from the code in this PR the parser doesnt seem to have started rejecting any DROP INDEX statement variants specifically for mysql, so that I imagined these should still have been passing for mysql?

Statement::Drop {
names, object_type, ..
} => {
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4018,3 +4018,13 @@ fn parse_drop_index() {
_ => unreachable!(),
}
}

#[test]
fn parse_alter_table_drop_index() {
assert_matches!(
alter_table_op(
mysql_and_generic().verified_stmt("ALTER TABLE tab DROP INDEX idx_index")
),
AlterTableOperation::DropIndex { name } if name.value == "idx_index"
);
}