Skip to content

Commit

Permalink
Support DROP TEMPORARY TABLE, MySQL syntax (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
liadgiladi authored Aug 7, 2023
1 parent 10a6ec5 commit eb4be98
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,8 @@ pub enum Statement {
/// Hive allows you specify whether the table's stored data will be
/// deleted along with the dropped table
purge: bool,
/// MySQL-specific "TEMPORARY" keyword
temporary: bool,
},
/// DROP Function
DropFunction {
Expand Down Expand Up @@ -2572,9 +2574,11 @@ impl fmt::Display for Statement {
cascade,
restrict,
purge,
temporary,
} => write!(
f,
"DROP {}{} {}{}{}{}",
"DROP {}{}{} {}{}{}{}",
if *temporary { "TEMPORARY " } else { "" },
object_type,
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(names),
Expand Down
5 changes: 5 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3127,6 +3127,10 @@ impl<'a> Parser<'a> {
}

pub fn parse_drop(&mut self) -> Result<Statement, ParserError> {
// MySQL dialect supports `TEMPORARY`
let temporary = dialect_of!(self is MySqlDialect | GenericDialect)
&& self.parse_keyword(Keyword::TEMPORARY);

let object_type = if self.parse_keyword(Keyword::TABLE) {
ObjectType::Table
} else if self.parse_keyword(Keyword::VIEW) {
Expand Down Expand Up @@ -3169,6 +3173,7 @@ impl<'a> Parser<'a> {
cascade,
restrict,
purge,
temporary,
})
}

Expand Down
4 changes: 4 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5432,6 +5432,7 @@ fn parse_drop_table() {
names,
cascade,
purge: _,
temporary,
..
} => {
assert!(!if_exists);
Expand All @@ -5441,6 +5442,7 @@ fn parse_drop_table() {
names.iter().map(ToString::to_string).collect::<Vec<_>>()
);
assert!(!cascade);
assert!(!temporary);
}
_ => unreachable!(),
}
Expand All @@ -5453,6 +5455,7 @@ fn parse_drop_table() {
names,
cascade,
purge: _,
temporary,
..
} => {
assert!(if_exists);
Expand All @@ -5462,6 +5465,7 @@ fn parse_drop_table() {
names.iter().map(ToString::to_string).collect::<Vec<_>>()
);
assert!(cascade);
assert!(!temporary);
}
_ => unreachable!(),
}
Expand Down
26 changes: 26 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,3 +1597,29 @@ fn parse_string_introducers() {
fn parse_div_infix() {
mysql().verified_stmt(r#"SELECT 5 DIV 2"#);
}

#[test]
fn parse_drop_temporary_table() {
let sql = "DROP TEMPORARY TABLE foo";
match mysql().verified_stmt(sql) {
Statement::Drop {
object_type,
if_exists,
names,
cascade,
purge: _,
temporary,
..
} => {
assert!(!if_exists);
assert_eq!(ObjectType::Table, object_type);
assert_eq!(
vec!["foo"],
names.iter().map(ToString::to_string).collect::<Vec<_>>()
);
assert!(!cascade);
assert!(temporary);
}
_ => unreachable!(),
}
}

0 comments on commit eb4be98

Please sign in to comment.