Skip to content

Commit 5a78d6b

Browse files
committed
Change on_cluster type from Option<String> to Option<Ident>
1 parent 2b43a96 commit 5a78d6b

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

src/ast/dml.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub struct CreateTable {
126126
pub on_commit: Option<OnCommit>,
127127
/// ClickHouse "ON CLUSTER" clause:
128128
/// <https://clickhouse.com/docs/en/sql-reference/distributed-ddl/>
129-
pub on_cluster: Option<String>,
129+
pub on_cluster: Option<Ident>,
130130
/// ClickHouse "PRIMARY KEY " clause.
131131
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/>
132132
pub primary_key: Option<Box<Expr>>,

src/ast/helpers/stmt_create_table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct CreateTableBuilder {
7373
pub default_charset: Option<String>,
7474
pub collation: Option<String>,
7575
pub on_commit: Option<OnCommit>,
76-
pub on_cluster: Option<String>,
76+
pub on_cluster: Option<Ident>,
7777
pub primary_key: Option<Box<Expr>>,
7878
pub order_by: Option<OneOrManyWithParens<Expr>>,
7979
pub partition_by: Option<Box<Expr>>,
@@ -261,7 +261,7 @@ impl CreateTableBuilder {
261261
self
262262
}
263263

264-
pub fn on_cluster(mut self, on_cluster: Option<String>) -> Self {
264+
pub fn on_cluster(mut self, on_cluster: Option<Ident>) -> Self {
265265
self.on_cluster = on_cluster;
266266
self
267267
}

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,7 @@ pub enum Statement {
21222122
/// ClickHouse dialect supports `ON CLUSTER` clause for ALTER TABLE
21232123
/// For example: `ALTER TABLE table_name ON CLUSTER cluster_name ADD COLUMN c UInt32`
21242124
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/alter/update)
2125-
on_cluster: Option<String>,
2125+
on_cluster: Option<Ident>,
21262126
},
21272127
/// ```sql
21282128
/// ALTER INDEX

src/parser/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5313,14 +5313,9 @@ impl<'a> Parser<'a> {
53135313
}
53145314
}
53155315

5316-
fn parse_optional_on_cluster(&mut self) -> Result<Option<String>, ParserError> {
5316+
fn parse_optional_on_cluster(&mut self) -> Result<Option<Ident>, ParserError> {
53175317
if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) {
5318-
let next_token = self.next_token();
5319-
match next_token.token {
5320-
Token::SingleQuotedString(s) => Ok(Some(format!("'{}'", s))),
5321-
Token::Word(s) => Ok(Some(s.to_string())),
5322-
_ => self.expected("identifier or cluster literal", next_token)?,
5323-
}
5318+
Ok(Some(self.parse_identifier(false)?))
53245319
} else {
53255320
Ok(None)
53265321
}

tests/sqlparser_common.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3495,7 +3495,7 @@ fn parse_create_table_on_cluster() {
34953495
let sql = "CREATE TABLE t ON CLUSTER '{cluster}' (a INT, b INT)";
34963496
match generic.verified_stmt(sql) {
34973497
Statement::CreateTable(CreateTable { on_cluster, .. }) => {
3498-
assert_eq!(on_cluster.unwrap(), "'{cluster}'".to_string());
3498+
assert_eq!(on_cluster.unwrap().to_string(), "'{cluster}'".to_string());
34993499
}
35003500
_ => unreachable!(),
35013501
}
@@ -3504,7 +3504,7 @@ fn parse_create_table_on_cluster() {
35043504
let sql = "CREATE TABLE t ON CLUSTER my_cluster (a INT, b INT)";
35053505
match generic.verified_stmt(sql) {
35063506
Statement::CreateTable(CreateTable { on_cluster, .. }) => {
3507-
assert_eq!(on_cluster.unwrap(), "my_cluster".to_string());
3507+
assert_eq!(on_cluster.unwrap().to_string(), "my_cluster".to_string());
35083508
}
35093509
_ => unreachable!(),
35103510
}
@@ -3813,13 +3813,22 @@ fn parse_alter_table() {
38133813

38143814
#[test]
38153815
fn test_alter_table_with_on_cluster() {
3816-
let sql = "ALTER TABLE t ON CLUSTER 'cluster' ADD CONSTRAINT bar PRIMARY KEY (baz)";
3817-
match all_dialects().verified_stmt(sql) {
3816+
match all_dialects().verified_stmt("ALTER TABLE t ON CLUSTER 'cluster' ADD CONSTRAINT bar PRIMARY KEY (baz)") {
38183817
Statement::AlterTable {
38193818
name, on_cluster, ..
38203819
} => {
38213820
std::assert_eq!(name.to_string(), "t");
3822-
std::assert_eq!(on_cluster, Some("'cluster'".to_string()));
3821+
std::assert_eq!(on_cluster, Some(Ident::with_quote('\'',"cluster")));
3822+
}
3823+
_ => unreachable!(),
3824+
}
3825+
3826+
match all_dialects().verified_stmt("ALTER TABLE t ON CLUSTER cluster_name ADD CONSTRAINT bar PRIMARY KEY (baz)") {
3827+
Statement::AlterTable {
3828+
name, on_cluster, ..
3829+
} => {
3830+
std::assert_eq!(name.to_string(), "t");
3831+
std::assert_eq!(on_cluster, Some(Ident::new("cluster_name")));
38233832
}
38243833
_ => unreachable!(),
38253834
}
@@ -3828,7 +3837,7 @@ fn test_alter_table_with_on_cluster() {
38283837
.parse_sql_statements("ALTER TABLE t ON CLUSTER 123 ADD CONSTRAINT bar PRIMARY KEY (baz)");
38293838
std::assert_eq!(
38303839
res.unwrap_err(),
3831-
ParserError::ParserError("Expected: identifier or cluster literal, found: 123".to_string())
3840+
ParserError::ParserError("Expected: identifier, found: 123".to_string())
38323841
)
38333842
}
38343843

0 commit comments

Comments
 (0)