Skip to content

Commit 1c505ce

Browse files
authored
Allow to use ON CLUSTER cluster_name in TRUNCATE syntax (apache#1428)
1 parent 246838a commit 1c505ce

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/ast/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,11 @@ pub enum Statement {
21722172
/// Postgres-specific option
21732173
/// [ CASCADE | RESTRICT ]
21742174
cascade: Option<TruncateCascadeOption>,
2175+
/// ClickHouse-specific option
2176+
/// [ ON CLUSTER cluster_name ]
2177+
///
2178+
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/truncate/)
2179+
on_cluster: Option<Ident>,
21752180
},
21762181
/// ```sql
21772182
/// MSCK
@@ -3293,6 +3298,7 @@ impl fmt::Display for Statement {
32933298
only,
32943299
identity,
32953300
cascade,
3301+
on_cluster,
32963302
} => {
32973303
let table = if *table { "TABLE " } else { "" };
32983304
let only = if *only { "ONLY " } else { "" };
@@ -3321,6 +3327,9 @@ impl fmt::Display for Statement {
33213327
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
33223328
}
33233329
}
3330+
if let Some(on_cluster) = on_cluster {
3331+
write!(f, " ON CLUSTER {on_cluster}")?;
3332+
}
33243333
Ok(())
33253334
}
33263335
Statement::AttachDatabase {

src/parser/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,13 +708,16 @@ impl<'a> Parser<'a> {
708708
};
709709
};
710710

711+
let on_cluster = self.parse_optional_on_cluster()?;
712+
711713
Ok(Statement::Truncate {
712714
table_names,
713715
partitions,
714716
table,
715717
only,
716718
identity,
717719
cascade,
720+
on_cluster,
718721
})
719722
}
720723

tests/sqlparser_common.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10804,3 +10804,24 @@ fn test_extract_seconds_single_quote_err() {
1080410804
"sql parser error: Expected: date/time field, found: 'seconds'"
1080510805
);
1080610806
}
10807+
10808+
#[test]
10809+
fn test_truncate_table_with_on_cluster() {
10810+
let sql = "TRUNCATE TABLE t ON CLUSTER cluster_name";
10811+
match all_dialects().verified_stmt(sql) {
10812+
Statement::Truncate { on_cluster, .. } => {
10813+
assert_eq!(on_cluster, Some(Ident::new("cluster_name")));
10814+
}
10815+
_ => panic!("Expected: TRUNCATE TABLE statement"),
10816+
}
10817+
10818+
// Omit ON CLUSTER is allowed
10819+
all_dialects().verified_stmt("TRUNCATE TABLE t");
10820+
10821+
assert_eq!(
10822+
ParserError::ParserError("Expected: identifier, found: EOF".to_string()),
10823+
all_dialects()
10824+
.parse_sql_statements("TRUNCATE TABLE t ON CLUSTER")
10825+
.unwrap_err()
10826+
);
10827+
}

tests/sqlparser_postgres.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,7 @@ fn parse_truncate() {
40104010
only: false,
40114011
identity: None,
40124012
cascade: None,
4013+
on_cluster: None,
40134014
},
40144015
truncate
40154016
);
@@ -4032,7 +4033,8 @@ fn parse_truncate_with_options() {
40324033
table: true,
40334034
only: true,
40344035
identity: Some(TruncateIdentityOption::Restart),
4035-
cascade: Some(TruncateCascadeOption::Cascade)
4036+
cascade: Some(TruncateCascadeOption::Cascade),
4037+
on_cluster: None,
40364038
},
40374039
truncate
40384040
);
@@ -4063,7 +4065,8 @@ fn parse_truncate_with_table_list() {
40634065
table: true,
40644066
only: false,
40654067
identity: Some(TruncateIdentityOption::Restart),
4066-
cascade: Some(TruncateCascadeOption::Cascade)
4068+
cascade: Some(TruncateCascadeOption::Cascade),
4069+
on_cluster: None,
40674070
},
40684071
truncate
40694072
);

0 commit comments

Comments
 (0)