Skip to content

Commit 64b3953

Browse files
committed
fix some lints and tests, add purge on tables ad proper formatting for INSERT INTO TABLE
1 parent 88d7b69 commit 64b3953

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
@@ -547,6 +549,9 @@ pub enum Statement {
547549
/// Whether `CASCADE` was specified. This will be `false` when
548550
/// `RESTRICT` or no drop behavior at all was specified.
549551
cascade: bool,
552+
/// Hive allows you specify whether the table's stored data will be
553+
/// deleted along with the dropped table
554+
purge: bool,
550555
},
551556
/// SET <variable>
552557
///
@@ -680,16 +685,14 @@ impl fmt::Display for Statement {
680685
partitioned,
681686
columns,
682687
source,
688+
table,
683689
} => {
684690
write!(
685691
f,
686-
"INSERT {act} {table_name} ",
692+
"INSERT {act}{tbl} {table_name} ",
687693
table_name = table_name,
688-
act = if *overwrite {
689-
"OVERWRITE TABLE"
690-
} else {
691-
"INTO"
692-
}
694+
act = if *overwrite { "OVERWRITE" } else { "INTO" },
695+
tbl = if *table { " TABLE" } else { "" }
693696
)?;
694697
if !columns.is_empty() {
695698
write!(f, "({}) ", display_comma_separated(columns))?;
@@ -975,13 +978,15 @@ impl fmt::Display for Statement {
975978
if_exists,
976979
names,
977980
cascade,
981+
purge,
978982
} => write!(
979983
f,
980-
"DROP {}{} {}{}",
984+
"DROP {}{} {}{}{}",
981985
object_type,
982986
if *if_exists { " IF EXISTS" } else { "" },
983987
display_comma_separated(names),
984988
if *cascade { " CASCADE" } else { "" },
989+
if *purge { " PURGE" } else { "" }
985990
),
986991
Statement::SetVariable {
987992
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
@@ -1250,6 +1250,7 @@ impl Parser {
12501250
let names = self.parse_comma_separated(Parser::parse_object_name)?;
12511251
let cascade = self.parse_keyword(Keyword::CASCADE);
12521252
let restrict = self.parse_keyword(Keyword::RESTRICT);
1253+
let purge = self.parse_keyword(Keyword::PURGE);
12531254
if cascade && restrict {
12541255
return parser_err!("Cannot specify both CASCADE and RESTRICT in DROP");
12551256
}
@@ -1258,6 +1259,7 @@ impl Parser {
12581259
if_exists,
12591260
names,
12601261
cascade,
1262+
purge,
12611263
})
12621264
}
12631265

@@ -2135,11 +2137,11 @@ impl Parser {
21352137
});
21362138
}
21372139
} else if variable.value == "TRANSACTION" && modifier.is_none() {
2138-
return Ok(Statement::SetTransaction {
2140+
Ok(Statement::SetTransaction {
21392141
modes: self.parse_transaction_modes()?,
2140-
});
2142+
})
21412143
} else {
2142-
return self.expected("equals sign or TO", self.peek_token());
2144+
self.expected("equals sign or TO", self.peek_token())
21432145
}
21442146
}
21452147

@@ -2375,13 +2377,9 @@ impl Parser {
23752377
/// Parse an INSERT statement
23762378
pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
23772379
let action = self.expect_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE])?;
2378-
let overwrite = if action == Keyword::OVERWRITE {
2379-
true
2380-
} else {
2381-
false
2382-
};
2380+
let overwrite = action == Keyword::OVERWRITE;
23832381
// Hive lets you put table here regardless
2384-
self.parse_keyword(Keyword::TABLE);
2382+
let table = self.parse_keyword(Keyword::TABLE);
23852383
let table_name = self.parse_object_name()?;
23862384
let columns = self.parse_parenthesized_column_list(Optional)?;
23872385

@@ -2400,6 +2398,7 @@ impl Parser {
24002398
partitioned,
24012399
columns,
24022400
source,
2401+
table,
24032402
})
24042403
}
24052404

tests/sqlparser_common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,7 @@ fn parse_drop_table() {
27152715
if_exists,
27162716
names,
27172717
cascade,
2718+
purge: _,
27182719
} => {
27192720
assert_eq!(false, if_exists);
27202721
assert_eq!(ObjectType::Table, object_type);
@@ -2734,6 +2735,7 @@ fn parse_drop_table() {
27342735
if_exists,
27352736
names,
27362737
cascade,
2738+
purge: _,
27372739
} => {
27382740
assert_eq!(true, if_exists);
27392741
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)