Skip to content

Commit 3be19c7

Browse files
authored
truncate: table as optional keyword (#883)
Signed-off-by: Maciej Obuchowski <[email protected]>
1 parent 097e7ad commit 3be19c7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/ast/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,8 @@ pub enum Statement {
11421142
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
11431143
table_name: ObjectName,
11441144
partitions: Option<Vec<Expr>>,
1145+
/// TABLE - optional keyword;
1146+
table: bool,
11451147
},
11461148
/// Msck (Hive)
11471149
Msck {
@@ -1844,8 +1846,10 @@ impl fmt::Display for Statement {
18441846
Statement::Truncate {
18451847
table_name,
18461848
partitions,
1849+
table,
18471850
} => {
1848-
write!(f, "TRUNCATE TABLE {table_name}")?;
1851+
let table = if *table { "TABLE " } else { "" };
1852+
write!(f, "TRUNCATE {table}{table_name}")?;
18491853
if let Some(ref parts) = partitions {
18501854
if !parts.is_empty() {
18511855
write!(f, " PARTITION ({})", display_comma_separated(parts))?;

src/parser.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<'a> Parser<'a> {
473473
}
474474

475475
pub fn parse_truncate(&mut self) -> Result<Statement, ParserError> {
476-
self.expect_keyword(Keyword::TABLE)?;
476+
let table = self.parse_keyword(Keyword::TABLE);
477477
let table_name = self.parse_object_name()?;
478478
let mut partitions = None;
479479
if self.parse_keyword(Keyword::PARTITION) {
@@ -484,6 +484,7 @@ impl<'a> Parser<'a> {
484484
Ok(Statement::Truncate {
485485
table_name,
486486
partitions,
487+
table,
487488
})
488489
}
489490

tests/sqlparser_postgres.rs

+13
Original file line numberDiff line numberDiff line change
@@ -2950,3 +2950,16 @@ fn parse_select_group_by_cube() {
29502950
select.group_by
29512951
);
29522952
}
2953+
2954+
#[test]
2955+
fn parse_truncate() {
2956+
let truncate = pg_and_generic().verified_stmt("TRUNCATE db.table_name");
2957+
assert_eq!(
2958+
Statement::Truncate {
2959+
table_name: ObjectName(vec![Ident::new("db"), Ident::new("table_name")]),
2960+
partitions: None,
2961+
table: false
2962+
},
2963+
truncate
2964+
);
2965+
}

0 commit comments

Comments
 (0)