Skip to content

Commit 97f47b5

Browse files
committed
Change on_cluster type from Option<String> to Option<Ident>
1 parent b01e9ec commit 97f47b5

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
@@ -2165,7 +2165,7 @@ pub enum Statement {
21652165
/// ClickHouse dialect supports `ON CLUSTER` clause for ALTER TABLE
21662166
/// For example: `ALTER TABLE table_name ON CLUSTER cluster_name ADD COLUMN c UInt32`
21672167
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/alter/update)
2168-
on_cluster: Option<String>,
2168+
on_cluster: Option<Ident>,
21692169
},
21702170
/// ```sql
21712171
/// ALTER INDEX

src/parser/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5372,14 +5372,9 @@ impl<'a> Parser<'a> {
53725372
}
53735373
}
53745374

5375-
fn parse_optional_on_cluster(&mut self) -> Result<Option<String>, ParserError> {
5375+
fn parse_optional_on_cluster(&mut self) -> Result<Option<Ident>, ParserError> {
53765376
if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) {
5377-
let next_token = self.next_token();
5378-
match next_token.token {
5379-
Token::SingleQuotedString(s) => Ok(Some(format!("'{}'", s))),
5380-
Token::Word(s) => Ok(Some(s.to_string())),
5381-
_ => self.expected("identifier or cluster literal", next_token)?,
5382-
}
5377+
Ok(Some(self.parse_identifier(false)?))
53835378
} else {
53845379
Ok(None)
53855380
}

tests/sqlparser_common.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,7 @@ fn parse_create_table_on_cluster() {
35063506
let sql = "CREATE TABLE t ON CLUSTER '{cluster}' (a INT, b INT)";
35073507
match generic.verified_stmt(sql) {
35083508
Statement::CreateTable(CreateTable { on_cluster, .. }) => {
3509-
assert_eq!(on_cluster.unwrap(), "'{cluster}'".to_string());
3509+
assert_eq!(on_cluster.unwrap().to_string(), "'{cluster}'".to_string());
35103510
}
35113511
_ => unreachable!(),
35123512
}
@@ -3515,7 +3515,7 @@ fn parse_create_table_on_cluster() {
35153515
let sql = "CREATE TABLE t ON CLUSTER my_cluster (a INT, b INT)";
35163516
match generic.verified_stmt(sql) {
35173517
Statement::CreateTable(CreateTable { on_cluster, .. }) => {
3518-
assert_eq!(on_cluster.unwrap(), "my_cluster".to_string());
3518+
assert_eq!(on_cluster.unwrap().to_string(), "my_cluster".to_string());
35193519
}
35203520
_ => unreachable!(),
35213521
}
@@ -3824,13 +3824,22 @@ fn parse_alter_table() {
38243824

38253825
#[test]
38263826
fn test_alter_table_with_on_cluster() {
3827-
let sql = "ALTER TABLE t ON CLUSTER 'cluster' ADD CONSTRAINT bar PRIMARY KEY (baz)";
3828-
match all_dialects().verified_stmt(sql) {
3827+
match all_dialects().verified_stmt("ALTER TABLE t ON CLUSTER 'cluster' ADD CONSTRAINT bar PRIMARY KEY (baz)") {
38293828
Statement::AlterTable {
38303829
name, on_cluster, ..
38313830
} => {
38323831
std::assert_eq!(name.to_string(), "t");
3833-
std::assert_eq!(on_cluster, Some("'cluster'".to_string()));
3832+
std::assert_eq!(on_cluster, Some(Ident::with_quote('\'',"cluster")));
3833+
}
3834+
_ => unreachable!(),
3835+
}
3836+
3837+
match all_dialects().verified_stmt("ALTER TABLE t ON CLUSTER cluster_name ADD CONSTRAINT bar PRIMARY KEY (baz)") {
3838+
Statement::AlterTable {
3839+
name, on_cluster, ..
3840+
} => {
3841+
std::assert_eq!(name.to_string(), "t");
3842+
std::assert_eq!(on_cluster, Some(Ident::new("cluster_name")));
38343843
}
38353844
_ => unreachable!(),
38363845
}
@@ -3839,7 +3848,7 @@ fn test_alter_table_with_on_cluster() {
38393848
.parse_sql_statements("ALTER TABLE t ON CLUSTER 123 ADD CONSTRAINT bar PRIMARY KEY (baz)");
38403849
std::assert_eq!(
38413850
res.unwrap_err(),
3842-
ParserError::ParserError("Expected: identifier or cluster literal, found: 123".to_string())
3851+
ParserError::ParserError("Expected: identifier, found: 123".to_string())
38433852
)
38443853
}
38453854

0 commit comments

Comments
 (0)