Skip to content

Commit 80d47ee

Browse files
dmzmkiffyio
andauthored
Adds support for mysql's drop index (#1864)
Co-authored-by: Ifeanyi Ubah <[email protected]>
1 parent a8bde39 commit 80d47ee

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

src/ast/mod.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,9 @@ pub enum Statement {
34053405
purge: bool,
34063406
/// MySQL-specific "TEMPORARY" keyword
34073407
temporary: bool,
3408+
/// MySQL-specific drop index syntax, which requires table specification
3409+
/// See <https://dev.mysql.com/doc/refman/8.4/en/drop-index.html>
3410+
table: Option<ObjectName>,
34083411
},
34093412
/// ```sql
34103413
/// DROP FUNCTION
@@ -5242,17 +5245,24 @@ impl fmt::Display for Statement {
52425245
restrict,
52435246
purge,
52445247
temporary,
5245-
} => write!(
5246-
f,
5247-
"DROP {}{}{} {}{}{}{}",
5248-
if *temporary { "TEMPORARY " } else { "" },
5249-
object_type,
5250-
if *if_exists { " IF EXISTS" } else { "" },
5251-
display_comma_separated(names),
5252-
if *cascade { " CASCADE" } else { "" },
5253-
if *restrict { " RESTRICT" } else { "" },
5254-
if *purge { " PURGE" } else { "" }
5255-
),
5248+
table,
5249+
} => {
5250+
write!(
5251+
f,
5252+
"DROP {}{}{} {}{}{}{}",
5253+
if *temporary { "TEMPORARY " } else { "" },
5254+
object_type,
5255+
if *if_exists { " IF EXISTS" } else { "" },
5256+
display_comma_separated(names),
5257+
if *cascade { " CASCADE" } else { "" },
5258+
if *restrict { " RESTRICT" } else { "" },
5259+
if *purge { " PURGE" } else { "" },
5260+
)?;
5261+
if let Some(table_name) = table.as_ref() {
5262+
write!(f, " ON {}", table_name)?;
5263+
};
5264+
Ok(())
5265+
}
52565266
Statement::DropFunction {
52575267
if_exists,
52585268
func_desc,

src/parser/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6255,6 +6255,11 @@ impl<'a> Parser<'a> {
62556255
loc
62566256
);
62576257
}
6258+
let table = if self.parse_keyword(Keyword::ON) {
6259+
Some(self.parse_object_name(false)?)
6260+
} else {
6261+
None
6262+
};
62586263
Ok(Statement::Drop {
62596264
object_type,
62606265
if_exists,
@@ -6263,6 +6268,7 @@ impl<'a> Parser<'a> {
62636268
restrict,
62646269
purge,
62656270
temporary,
6271+
table,
62666272
})
62676273
}
62686274

tests/sqlparser_mysql.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3987,3 +3987,34 @@ fn parse_straight_join() {
39873987
mysql()
39883988
.verified_stmt("SELECT a.*, b.* FROM table_a STRAIGHT_JOIN table_b AS b ON a.b_id = b.id");
39893989
}
3990+
3991+
#[test]
3992+
fn parse_drop_index() {
3993+
let sql = "DROP INDEX idx_name ON table_name";
3994+
match mysql().verified_stmt(sql) {
3995+
Statement::Drop {
3996+
object_type,
3997+
if_exists,
3998+
names,
3999+
cascade,
4000+
restrict,
4001+
purge,
4002+
temporary,
4003+
table,
4004+
} => {
4005+
assert!(!if_exists);
4006+
assert_eq!(ObjectType::Index, object_type);
4007+
assert_eq!(
4008+
vec!["idx_name"],
4009+
names.iter().map(ToString::to_string).collect::<Vec<_>>()
4010+
);
4011+
assert!(!cascade);
4012+
assert!(!restrict);
4013+
assert!(!purge);
4014+
assert!(!temporary);
4015+
assert!(table.is_some());
4016+
assert_eq!("table_name", table.unwrap().to_string());
4017+
}
4018+
_ => unreachable!(),
4019+
}
4020+
}

0 commit comments

Comments
 (0)