Skip to content

Allow to use ON CLUSTER cluster_name in TRUNCATE syntax #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
9 changes: 9 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,11 @@ pub enum Statement {
/// Postgres-specific option
/// [ CASCADE | RESTRICT ]
cascade: Option<TruncateCascadeOption>,
/// ClickHouse-specific option
/// [ ON CLUSTER cluster_name ]
///
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/truncate/)
on_cluster: Option<Ident>,
},
/// ```sql
/// MSCK
Expand Down Expand Up @@ -3293,6 +3298,7 @@ impl fmt::Display for Statement {
only,
identity,
cascade,
on_cluster,
} => {
let table = if *table { "TABLE " } else { "" };
let only = if *only { "ONLY " } else { "" };
Expand Down Expand Up @@ -3321,6 +3327,9 @@ impl fmt::Display for Statement {
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
}
}
if let Some(on_cluster) = on_cluster {
write!(f, " ON CLUSTER {on_cluster}")?;
}
Ok(())
}
Statement::AttachDatabase {
Expand Down
3 changes: 3 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,16 @@ impl<'a> Parser<'a> {
};
};

let on_cluster = self.parse_optional_on_cluster()?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ON CLUSTER is a ClickHouse-specific syntax, but we don't add the dialect guard for the sake of keeping consistent with other places.


Ok(Statement::Truncate {
table_names,
partitions,
table,
only,
identity,
cascade,
on_cluster,
})
}

Expand Down
21 changes: 21 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10792,3 +10792,24 @@ fn test_extract_seconds_single_quote_err() {
"sql parser error: Expected: date/time field, found: 'seconds'"
);
}

#[test]
fn test_truncate_table_with_on_cluster() {
let sql = "TRUNCATE TABLE t ON CLUSTER cluster_name";
match all_dialects().verified_stmt(sql) {
Statement::Truncate { on_cluster, .. } => {
assert_eq!(on_cluster, Some(Ident::new("cluster_name")));
}
_ => panic!("Expected: TRUNCATE TABLE statement"),
}

// Omit ON CLUSTER is allowed
all_dialects().verified_stmt("TRUNCATE TABLE t");

assert_eq!(
ParserError::ParserError("Expected: identifier, found: EOF".to_string()),
all_dialects()
.parse_sql_statements("TRUNCATE TABLE t ON CLUSTER")
.unwrap_err()
);
}
7 changes: 5 additions & 2 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3990,6 +3990,7 @@ fn parse_truncate() {
only: false,
identity: None,
cascade: None,
on_cluster: None,
},
truncate
);
Expand All @@ -4012,7 +4013,8 @@ fn parse_truncate_with_options() {
table: true,
only: true,
identity: Some(TruncateIdentityOption::Restart),
cascade: Some(TruncateCascadeOption::Cascade)
cascade: Some(TruncateCascadeOption::Cascade),
on_cluster: None,
},
truncate
);
Expand Down Expand Up @@ -4043,7 +4045,8 @@ fn parse_truncate_with_table_list() {
table: true,
only: false,
identity: Some(TruncateIdentityOption::Restart),
cascade: Some(TruncateCascadeOption::Cascade)
cascade: Some(TruncateCascadeOption::Cascade),
on_cluster: None,
},
truncate
);
Expand Down
Loading