Skip to content

Commit f006125

Browse files
Moved Truncate out of the Statement enum
1 parent 67f0bca commit f006125

File tree

6 files changed

+97
-84
lines changed

6 files changed

+97
-84
lines changed

src/ast/ddl.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,3 +3416,74 @@ impl fmt::Display for DropTrigger {
34163416
Ok(())
34173417
}
34183418
}
3419+
3420+
/// A `TRUNCATE` statement.
3421+
///
3422+
/// ```sql
3423+
/// TRUNCATE TABLE table_names [PARTITION (partitions)] [RESTART IDENTITY | CONTINUE IDENTITY] [CASCADE | RESTRICT] [ON CLUSTER cluster_name]
3424+
/// ```
3425+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3426+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3427+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3428+
pub struct Truncate {
3429+
/// Table names to truncate
3430+
pub table_names: Vec<super::TruncateTableTarget>,
3431+
/// Optional partition specification
3432+
pub partitions: Option<Vec<Expr>>,
3433+
/// TABLE - optional keyword
3434+
pub table: bool,
3435+
/// Postgres-specific option: [ RESTART IDENTITY | CONTINUE IDENTITY ]
3436+
pub identity: Option<super::TruncateIdentityOption>,
3437+
/// Postgres-specific option: [ CASCADE | RESTRICT ]
3438+
pub cascade: Option<super::CascadeOption>,
3439+
/// ClickHouse-specific option: [ ON CLUSTER cluster_name ]
3440+
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/truncate/)
3441+
pub on_cluster: Option<Ident>,
3442+
}
3443+
3444+
impl fmt::Display for Truncate {
3445+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3446+
let table = if self.table { "TABLE " } else { "" };
3447+
3448+
write!(
3449+
f,
3450+
"TRUNCATE {table}{table_names}",
3451+
table_names = display_comma_separated(&self.table_names)
3452+
)?;
3453+
3454+
if let Some(identity) = &self.identity {
3455+
match identity {
3456+
super::TruncateIdentityOption::Restart => write!(f, " RESTART IDENTITY")?,
3457+
super::TruncateIdentityOption::Continue => write!(f, " CONTINUE IDENTITY")?,
3458+
}
3459+
}
3460+
if let Some(cascade) = &self.cascade {
3461+
match cascade {
3462+
super::CascadeOption::Cascade => write!(f, " CASCADE")?,
3463+
super::CascadeOption::Restrict => write!(f, " RESTRICT")?,
3464+
}
3465+
}
3466+
3467+
if let Some(ref parts) = &self.partitions {
3468+
if !parts.is_empty() {
3469+
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
3470+
}
3471+
}
3472+
if let Some(on_cluster) = &self.on_cluster {
3473+
write!(f, " ON CLUSTER {on_cluster}")?;
3474+
}
3475+
Ok(())
3476+
}
3477+
}
3478+
3479+
impl Spanned for Truncate {
3480+
fn span(&self) -> Span {
3481+
Span::union_iter(
3482+
self.table_names.iter().map(|i| i.name.span()).chain(
3483+
self.partitions
3484+
.iter()
3485+
.flat_map(|i| i.iter().map(|k| k.span())),
3486+
),
3487+
)
3488+
}
3489+
}

src/ast/mod.rs

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub use self::ddl::{
6969
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
7070
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner, Partition,
7171
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TableConstraint,
72-
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
73-
ViewColumnDef,
72+
TagsColumnOption, Truncate, UserDefinedTypeCompositeAttributeDef,
73+
UserDefinedTypeRepresentation, ViewColumnDef,
7474
};
7575
pub use self::dml::{Delete, Insert};
7676
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -3133,23 +3133,7 @@ pub enum Statement {
31333133
/// TRUNCATE
31343134
/// ```
31353135
/// Truncate (Hive)
3136-
Truncate {
3137-
table_names: Vec<TruncateTableTarget>,
3138-
partitions: Option<Vec<Expr>>,
3139-
/// TABLE - optional keyword;
3140-
table: bool,
3141-
/// Postgres-specific option
3142-
/// [ RESTART IDENTITY | CONTINUE IDENTITY ]
3143-
identity: Option<TruncateIdentityOption>,
3144-
/// Postgres-specific option
3145-
/// [ CASCADE | RESTRICT ]
3146-
cascade: Option<CascadeOption>,
3147-
/// ClickHouse-specific option
3148-
/// [ ON CLUSTER cluster_name ]
3149-
///
3150-
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/truncate/)
3151-
on_cluster: Option<Ident>,
3152-
},
3136+
Truncate(Truncate),
31533137
/// ```sql
31543138
/// MSCK
31553139
/// ```
@@ -4373,6 +4357,12 @@ impl From<Analyze> for Statement {
43734357
}
43744358
}
43754359

4360+
impl From<ddl::Truncate> for Statement {
4361+
fn from(truncate: ddl::Truncate) -> Self {
4362+
Statement::Truncate(truncate)
4363+
}
4364+
}
4365+
43764366
/// ```sql
43774367
/// {COPY | REVOKE} CURRENT GRANTS
43784368
/// ```
@@ -4589,45 +4579,7 @@ impl fmt::Display for Statement {
45894579
}
45904580
Ok(())
45914581
}
4592-
Statement::Truncate {
4593-
table_names,
4594-
partitions,
4595-
table,
4596-
identity,
4597-
cascade,
4598-
on_cluster,
4599-
} => {
4600-
let table = if *table { "TABLE " } else { "" };
4601-
4602-
write!(
4603-
f,
4604-
"TRUNCATE {table}{table_names}",
4605-
table_names = display_comma_separated(table_names)
4606-
)?;
4607-
4608-
if let Some(identity) = identity {
4609-
match identity {
4610-
TruncateIdentityOption::Restart => write!(f, " RESTART IDENTITY")?,
4611-
TruncateIdentityOption::Continue => write!(f, " CONTINUE IDENTITY")?,
4612-
}
4613-
}
4614-
if let Some(cascade) = cascade {
4615-
match cascade {
4616-
CascadeOption::Cascade => write!(f, " CASCADE")?,
4617-
CascadeOption::Restrict => write!(f, " RESTRICT")?,
4618-
}
4619-
}
4620-
4621-
if let Some(ref parts) = partitions {
4622-
if !parts.is_empty() {
4623-
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
4624-
}
4625-
}
4626-
if let Some(on_cluster) = on_cluster {
4627-
write!(f, " ON CLUSTER {on_cluster}")?;
4628-
}
4629-
Ok(())
4630-
}
4582+
Statement::Truncate(truncate) => truncate.fmt(f),
46314583
Statement::Case(stmt) => {
46324584
write!(f, "{stmt}")
46334585
}

src/ast/spans.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,7 @@ impl Spanned for Statement {
299299
fn span(&self) -> Span {
300300
match self {
301301
Statement::Analyze(analyze) => analyze.span(),
302-
Statement::Truncate {
303-
table_names,
304-
partitions,
305-
table: _,
306-
identity: _,
307-
cascade: _,
308-
on_cluster: _,
309-
} => union_spans(
310-
table_names
311-
.iter()
312-
.map(|i| i.name.span())
313-
.chain(partitions.iter().flat_map(|i| i.iter().map(|k| k.span()))),
314-
),
302+
Statement::Truncate(truncate) => truncate.span(),
315303
Statement::Msck {
316304
table_name,
317305
repair: _,

src/parser/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,14 +1024,15 @@ impl<'a> Parser<'a> {
10241024

10251025
let on_cluster = self.parse_optional_on_cluster()?;
10261026

1027-
Ok(Statement::Truncate {
1027+
Ok(Truncate {
10281028
table_names,
10291029
partitions,
10301030
table,
10311031
identity,
10321032
cascade,
10331033
on_cluster,
1034-
})
1034+
}
1035+
.into())
10351036
}
10361037

10371038
fn parse_cascade_option(&mut self) -> Option<CascadeOption> {
@@ -1176,7 +1177,8 @@ impl<'a> Parser<'a> {
11761177
cache_metadata,
11771178
noscan,
11781179
compute_statistics,
1179-
}.into())
1180+
}
1181+
.into())
11801182
}
11811183

11821184
/// Parse a new expression including wildcard & qualified wildcard.

tests/sqlparser_common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13343,8 +13343,8 @@ fn test_extract_seconds_single_quote_err() {
1334313343
fn test_truncate_table_with_on_cluster() {
1334413344
let sql = "TRUNCATE TABLE t ON CLUSTER cluster_name";
1334513345
match all_dialects().verified_stmt(sql) {
13346-
Statement::Truncate { on_cluster, .. } => {
13347-
assert_eq!(on_cluster, Some(Ident::new("cluster_name")));
13346+
Statement::Truncate(truncate) => {
13347+
assert_eq!(truncate.on_cluster, Some(Ident::new("cluster_name")));
1334813348
}
1334913349
_ => panic!("Expected: TRUNCATE TABLE statement"),
1335013350
}
@@ -16399,14 +16399,14 @@ fn parse_truncate_only() {
1639916399
];
1640016400

1640116401
assert_eq!(
16402-
Statement::Truncate {
16402+
Statement::Truncate(Truncate {
1640316403
table_names,
1640416404
partitions: None,
1640516405
table: true,
1640616406
identity: None,
1640716407
cascade: None,
1640816408
on_cluster: None,
16409-
},
16409+
}),
1641016410
truncate
1641116411
);
1641216412
}

tests/sqlparser_postgres.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4956,14 +4956,14 @@ fn parse_truncate() {
49564956
only: false,
49574957
}];
49584958
assert_eq!(
4959-
Statement::Truncate {
4959+
Statement::Truncate(Truncate {
49604960
table_names,
49614961
partitions: None,
49624962
table: false,
49634963
identity: None,
49644964
cascade: None,
49654965
on_cluster: None,
4966-
},
4966+
}),
49674967
truncate
49684968
);
49694969
}
@@ -4980,14 +4980,14 @@ fn parse_truncate_with_options() {
49804980
}];
49814981

49824982
assert_eq!(
4983-
Statement::Truncate {
4983+
Statement::Truncate(Truncate {
49844984
table_names,
49854985
partitions: None,
49864986
table: true,
49874987
identity: Some(TruncateIdentityOption::Restart),
49884988
cascade: Some(CascadeOption::Cascade),
49894989
on_cluster: None,
4990-
},
4990+
}),
49914991
truncate
49924992
);
49934993
}
@@ -5013,14 +5013,14 @@ fn parse_truncate_with_table_list() {
50135013
];
50145014

50155015
assert_eq!(
5016-
Statement::Truncate {
5016+
Statement::Truncate(Truncate {
50175017
table_names,
50185018
partitions: None,
50195019
table: true,
50205020
identity: Some(TruncateIdentityOption::Restart),
50215021
cascade: Some(CascadeOption::Cascade),
50225022
on_cluster: None,
5023-
},
5023+
}),
50245024
truncate
50255025
);
50265026
}

0 commit comments

Comments
 (0)