From aa21a47006fde6124794850f40d5a94a6e7547f6 Mon Sep 17 00:00:00 2001 From: Maciej Obuchowski Date: Thu, 18 May 2023 15:28:28 +0200 Subject: [PATCH] truncate: table as optional keyword Signed-off-by: Maciej Obuchowski --- src/ast/mod.rs | 6 +++++- src/parser.rs | 3 ++- tests/sqlparser_postgres.rs | 13 +++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index e26cb0dfc..e7006859e 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1142,6 +1142,8 @@ pub enum Statement { #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))] table_name: ObjectName, partitions: Option>, + /// TABLE - optional keyword; + table: bool, }, /// Msck (Hive) Msck { @@ -1844,8 +1846,10 @@ impl fmt::Display for Statement { Statement::Truncate { table_name, partitions, + table, } => { - write!(f, "TRUNCATE TABLE {table_name}")?; + let table = if *table { "TABLE " } else { "" }; + write!(f, "TRUNCATE {table}{table_name}")?; if let Some(ref parts) = partitions { if !parts.is_empty() { write!(f, " PARTITION ({})", display_comma_separated(parts))?; diff --git a/src/parser.rs b/src/parser.rs index af577ce5d..74dfea725 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -473,7 +473,7 @@ impl<'a> Parser<'a> { } pub fn parse_truncate(&mut self) -> Result { - self.expect_keyword(Keyword::TABLE)?; + let table = self.parse_keyword(Keyword::TABLE); let table_name = self.parse_object_name()?; let mut partitions = None; if self.parse_keyword(Keyword::PARTITION) { @@ -484,6 +484,7 @@ impl<'a> Parser<'a> { Ok(Statement::Truncate { table_name, partitions, + table, }) } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 9de8e0eb4..5abf17233 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2950,3 +2950,16 @@ fn parse_select_group_by_cube() { select.group_by ); } + +#[test] +fn parse_truncate() { + let truncate = pg_and_generic().verified_stmt("TRUNCATE db.table_name"); + assert_eq!( + Statement::Truncate { + table_name: ObjectName(vec![Ident::new("db"), Ident::new("table_name")]), + partitions: None, + table: false + }, + truncate + ); +}