Skip to content

Commit 2ea1d84

Browse files
committed
fix some lints and tests, add purge on tables ad proper formatting for INSERT INTO TABLE
1 parent ac6ae2f commit 2ea1d84

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

src/ast/mod.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ pub enum Statement {
461461
source: Box<Query>,
462462
/// partitioned insert (Hive)
463463
partitioned: Option<Vec<Expr>>,
464+
/// whether the insert has the table keyword (Hive)
465+
table: bool,
464466
},
465467
Copy {
466468
/// TABLE
@@ -545,6 +547,9 @@ pub enum Statement {
545547
/// Whether `CASCADE` was specified. This will be `false` when
546548
/// `RESTRICT` or no drop behavior at all was specified.
547549
cascade: bool,
550+
/// Hive allows you specify whether the table's stored data will be
551+
/// deleted along with the dropped table
552+
purge: bool,
548553
},
549554
/// SET <variable>
550555
///
@@ -678,16 +683,14 @@ impl fmt::Display for Statement {
678683
partitioned,
679684
columns,
680685
source,
686+
table,
681687
} => {
682688
write!(
683689
f,
684-
"INSERT {act} {table_name} ",
690+
"INSERT {act}{tbl} {table_name} ",
685691
table_name = table_name,
686-
act = if *overwrite {
687-
"OVERWRITE TABLE"
688-
} else {
689-
"INTO"
690-
}
692+
act = if *overwrite { "OVERWRITE" } else { "INTO" },
693+
tbl = if *table { " TABLE" } else { "" }
691694
)?;
692695
if !columns.is_empty() {
693696
write!(f, "({}) ", display_comma_separated(columns))?;
@@ -965,13 +968,15 @@ impl fmt::Display for Statement {
965968
if_exists,
966969
names,
967970
cascade,
971+
purge,
968972
} => write!(
969973
f,
970-
"DROP {}{} {}{}",
974+
"DROP {}{} {}{}{}",
971975
object_type,
972976
if *if_exists { " IF EXISTS" } else { "" },
973977
display_comma_separated(names),
974978
if *cascade { " CASCADE" } else { "" },
979+
if *purge { " PURGE" } else { "" }
975980
),
976981
Statement::SetVariable {
977982
local,

src/ast/query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl fmt::Display for Query {
5757

5858
/// A node in a tree, representing a "query body" expression, roughly:
5959
/// `SELECT ... [ {UNION|EXCEPT|INTERSECT} SELECT ...]`
60+
#[allow(clippy::large_enum_variant)]
6061
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6162
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6263
pub enum SetExpr {

src/dialect/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ define_keywords!(
336336
PREPARE,
337337
PRIMARY,
338338
PROCEDURE,
339+
PURGE,
339340
RANGE,
340341
RANK,
341342
RCFILE,

src/parser.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@ impl Parser {
12401240
let names = self.parse_comma_separated(Parser::parse_object_name)?;
12411241
let cascade = self.parse_keyword(Keyword::CASCADE);
12421242
let restrict = self.parse_keyword(Keyword::RESTRICT);
1243+
let purge = self.parse_keyword(Keyword::PURGE);
12431244
if cascade && restrict {
12441245
return parser_err!("Cannot specify both CASCADE and RESTRICT in DROP");
12451246
}
@@ -1248,6 +1249,7 @@ impl Parser {
12481249
if_exists,
12491250
names,
12501251
cascade,
1252+
purge,
12511253
})
12521254
}
12531255

@@ -2124,11 +2126,11 @@ impl Parser {
21242126
});
21252127
}
21262128
} else if variable.value == "TRANSACTION" && modifier.is_none() {
2127-
return Ok(Statement::SetTransaction {
2129+
Ok(Statement::SetTransaction {
21282130
modes: self.parse_transaction_modes()?,
2129-
});
2131+
})
21302132
} else {
2131-
return self.expected("equals sign or TO", self.peek_token());
2133+
self.expected("equals sign or TO", self.peek_token())
21322134
}
21332135
}
21342136

@@ -2364,13 +2366,9 @@ impl Parser {
23642366
/// Parse an INSERT statement
23652367
pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
23662368
let action = self.expect_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE])?;
2367-
let overwrite = if action == Keyword::OVERWRITE {
2368-
true
2369-
} else {
2370-
false
2371-
};
2369+
let overwrite = action == Keyword::OVERWRITE;
23722370
// Hive lets you put table here regardless
2373-
self.parse_keyword(Keyword::TABLE);
2371+
let table = self.parse_keyword(Keyword::TABLE);
23742372
let table_name = self.parse_object_name()?;
23752373
let columns = self.parse_parenthesized_column_list(Optional)?;
23762374

@@ -2389,6 +2387,7 @@ impl Parser {
23892387
partitioned,
23902388
columns,
23912389
source,
2390+
table,
23922391
})
23932392
}
23942393

tests/sqlparser_common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,7 @@ fn parse_drop_table() {
25782578
if_exists,
25792579
names,
25802580
cascade,
2581+
purge: _,
25812582
} => {
25822583
assert_eq!(false, if_exists);
25832584
assert_eq!(ObjectType::Table, object_type);
@@ -2597,6 +2598,7 @@ fn parse_drop_table() {
25972598
if_exists,
25982599
names,
25992600
cascade,
2601+
purge: _,
26002602
} => {
26012603
assert_eq!(true, if_exists);
26022604
assert_eq!(ObjectType::Table, object_type);

tests/sqlparser_hive.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ fn parse_with_cte() {
6969
hive().verified_stmt(with);
7070
}
7171

72+
#[test]
73+
fn drop_table_purge() {
74+
let purge = "DROP TABLE db.table_name PURGE";
75+
hive().verified_stmt(purge);
76+
}
77+
7278
fn hive() -> TestedDialects {
7379
TestedDialects {
7480
dialects: vec![Box::new(HiveDialect {})],

0 commit comments

Comments
 (0)