Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4062,7 +4062,7 @@ impl fmt::Display for DropTrigger {
/// A `TRUNCATE` statement.
///
/// ```sql
/// TRUNCATE TABLE table_names [PARTITION (partitions)] [RESTART IDENTITY | CONTINUE IDENTITY] [CASCADE | RESTRICT] [ON CLUSTER cluster_name]
/// TRUNCATE TABLE [IF EXISTS] table_names [PARTITION (partitions)] [RESTART IDENTITY | CONTINUE IDENTITY] [CASCADE | RESTRICT] [ON CLUSTER cluster_name]
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand All @@ -4074,6 +4074,8 @@ pub struct Truncate {
pub partitions: Option<Vec<Expr>>,
/// TABLE - optional keyword
pub table: bool,
/// Snowflake/Redshift-specific option: [ IF EXISTS ]
pub if_exists: bool,
/// Postgres-specific option: [ RESTART IDENTITY | CONTINUE IDENTITY ]
pub identity: Option<super::TruncateIdentityOption>,
/// Postgres-specific option: [ CASCADE | RESTRICT ]
Expand All @@ -4086,10 +4088,11 @@ pub struct Truncate {
impl fmt::Display for Truncate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let table = if self.table { "TABLE " } else { "" };
let if_exists = if self.if_exists { "IF EXISTS " } else { "" };

write!(
f,
"TRUNCATE {table}{table_names}",
"TRUNCATE {table}{if_exists}{table_names}",
table_names = display_comma_separated(&self.table_names)
)?;

Expand Down
2 changes: 2 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ impl<'a> Parser<'a> {
/// Parse `TRUNCATE` statement.
pub fn parse_truncate(&mut self) -> Result<Truncate, ParserError> {
let table = self.parse_keyword(Keyword::TABLE);
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);

let table_names = self
.parse_comma_separated(|p| {
Expand Down Expand Up @@ -1091,6 +1092,7 @@ impl<'a> Parser<'a> {
table_names,
partitions,
table,
if_exists,
identity,
cascade,
on_cluster,
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16773,6 +16773,7 @@ fn parse_truncate_only() {
table_names,
partitions: None,
table: true,
if_exists: false,
identity: None,
cascade: None,
on_cluster: None,
Expand Down
3 changes: 3 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5089,6 +5089,7 @@ fn parse_truncate() {
table_names,
partitions: None,
table: false,
if_exists: false,
identity: None,
cascade: None,
on_cluster: None,
Expand All @@ -5113,6 +5114,7 @@ fn parse_truncate_with_options() {
table_names,
partitions: None,
table: true,
if_exists: false,
identity: Some(TruncateIdentityOption::Restart),
cascade: Some(CascadeOption::Cascade),
on_cluster: None,
Expand Down Expand Up @@ -5146,6 +5148,7 @@ fn parse_truncate_with_table_list() {
table_names,
partitions: None,
table: true,
if_exists: false,
identity: Some(TruncateIdentityOption::Restart),
cascade: Some(CascadeOption::Cascade),
on_cluster: None,
Expand Down
7 changes: 7 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4533,3 +4533,10 @@ fn test_alter_external_table() {
snowflake()
.verified_stmt("ALTER EXTERNAL TABLE IF EXISTS some_table REFRESH 'year=2025/month=12/'");
}

#[test]
fn test_truncate_table_if_exists() {
snowflake().verified_stmt("TRUNCATE TABLE IF EXISTS my_table");
snowflake().verified_stmt("TRUNCATE TABLE my_table");
snowflake().verified_stmt("TRUNCATE IF EXISTS my_table");
}