Skip to content

Commit 2cf33de

Browse files
authored
Fixed truncate table if exists for snowflake (#2166)
1 parent 6060a11 commit 2cf33de

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

src/ast/ddl.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,7 +4062,7 @@ impl fmt::Display for DropTrigger {
40624062
/// A `TRUNCATE` statement.
40634063
///
40644064
/// ```sql
4065-
/// TRUNCATE TABLE table_names [PARTITION (partitions)] [RESTART IDENTITY | CONTINUE IDENTITY] [CASCADE | RESTRICT] [ON CLUSTER cluster_name]
4065+
/// TRUNCATE TABLE [IF EXISTS] table_names [PARTITION (partitions)] [RESTART IDENTITY | CONTINUE IDENTITY] [CASCADE | RESTRICT] [ON CLUSTER cluster_name]
40664066
/// ```
40674067
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
40684068
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -4074,6 +4074,8 @@ pub struct Truncate {
40744074
pub partitions: Option<Vec<Expr>>,
40754075
/// TABLE - optional keyword
40764076
pub table: bool,
4077+
/// Snowflake/Redshift-specific option: [ IF EXISTS ]
4078+
pub if_exists: bool,
40774079
/// Postgres-specific option: [ RESTART IDENTITY | CONTINUE IDENTITY ]
40784080
pub identity: Option<super::TruncateIdentityOption>,
40794081
/// Postgres-specific option: [ CASCADE | RESTRICT ]
@@ -4086,10 +4088,11 @@ pub struct Truncate {
40864088
impl fmt::Display for Truncate {
40874089
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40884090
let table = if self.table { "TABLE " } else { "" };
4091+
let if_exists = if self.if_exists { "IF EXISTS " } else { "" };
40894092

40904093
write!(
40914094
f,
4092-
"TRUNCATE {table}{table_names}",
4095+
"TRUNCATE {table}{if_exists}{table_names}",
40934096
table_names = display_comma_separated(&self.table_names)
40944097
)?;
40954098

src/parser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ impl<'a> Parser<'a> {
10541054
/// Parse `TRUNCATE` statement.
10551055
pub fn parse_truncate(&mut self) -> Result<Truncate, ParserError> {
10561056
let table = self.parse_keyword(Keyword::TABLE);
1057+
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
10571058

10581059
let table_names = self
10591060
.parse_comma_separated(|p| {
@@ -1091,6 +1092,7 @@ impl<'a> Parser<'a> {
10911092
table_names,
10921093
partitions,
10931094
table,
1095+
if_exists,
10941096
identity,
10951097
cascade,
10961098
on_cluster,

tests/sqlparser_common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16773,6 +16773,7 @@ fn parse_truncate_only() {
1677316773
table_names,
1677416774
partitions: None,
1677516775
table: true,
16776+
if_exists: false,
1677616777
identity: None,
1677716778
cascade: None,
1677816779
on_cluster: None,

tests/sqlparser_postgres.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,6 +5089,7 @@ fn parse_truncate() {
50895089
table_names,
50905090
partitions: None,
50915091
table: false,
5092+
if_exists: false,
50925093
identity: None,
50935094
cascade: None,
50945095
on_cluster: None,
@@ -5113,6 +5114,7 @@ fn parse_truncate_with_options() {
51135114
table_names,
51145115
partitions: None,
51155116
table: true,
5117+
if_exists: false,
51165118
identity: Some(TruncateIdentityOption::Restart),
51175119
cascade: Some(CascadeOption::Cascade),
51185120
on_cluster: None,
@@ -5146,6 +5148,7 @@ fn parse_truncate_with_table_list() {
51465148
table_names,
51475149
partitions: None,
51485150
table: true,
5151+
if_exists: false,
51495152
identity: Some(TruncateIdentityOption::Restart),
51505153
cascade: Some(CascadeOption::Cascade),
51515154
on_cluster: None,

tests/sqlparser_snowflake.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,3 +4533,10 @@ fn test_alter_external_table() {
45334533
snowflake()
45344534
.verified_stmt("ALTER EXTERNAL TABLE IF EXISTS some_table REFRESH 'year=2025/month=12/'");
45354535
}
4536+
4537+
#[test]
4538+
fn test_truncate_table_if_exists() {
4539+
snowflake().verified_stmt("TRUNCATE TABLE IF EXISTS my_table");
4540+
snowflake().verified_stmt("TRUNCATE TABLE my_table");
4541+
snowflake().verified_stmt("TRUNCATE IF EXISTS my_table");
4542+
}

0 commit comments

Comments
 (0)