diff --git a/src/ast/dml.rs b/src/ast/dml.rs index 0036be07f..aad7d2e22 100644 --- a/src/ast/dml.rs +++ b/src/ast/dml.rs @@ -126,7 +126,7 @@ pub struct CreateTable { pub on_commit: Option, /// ClickHouse "ON CLUSTER" clause: /// - pub on_cluster: Option, + pub on_cluster: Option, /// ClickHouse "PRIMARY KEY " clause. /// pub primary_key: Option>, diff --git a/src/ast/helpers/stmt_create_table.rs b/src/ast/helpers/stmt_create_table.rs index 92c75e6a4..19efaeece 100644 --- a/src/ast/helpers/stmt_create_table.rs +++ b/src/ast/helpers/stmt_create_table.rs @@ -73,7 +73,7 @@ pub struct CreateTableBuilder { pub default_charset: Option, pub collation: Option, pub on_commit: Option, - pub on_cluster: Option, + pub on_cluster: Option, pub primary_key: Option>, pub order_by: Option>, pub partition_by: Option>, @@ -261,7 +261,7 @@ impl CreateTableBuilder { self } - pub fn on_cluster(mut self, on_cluster: Option) -> Self { + pub fn on_cluster(mut self, on_cluster: Option) -> Self { self.on_cluster = on_cluster; self } diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 6aa9752f2..ed2d7d8f9 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -2165,7 +2165,7 @@ pub enum Statement { /// ClickHouse dialect supports `ON CLUSTER` clause for ALTER TABLE /// For example: `ALTER TABLE table_name ON CLUSTER cluster_name ADD COLUMN c UInt32` /// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/alter/update) - on_cluster: Option, + on_cluster: Option, }, /// ```sql /// ALTER INDEX diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 33645ce0e..4d3ae8de9 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -5372,14 +5372,9 @@ impl<'a> Parser<'a> { } } - fn parse_optional_on_cluster(&mut self) -> Result, ParserError> { + fn parse_optional_on_cluster(&mut self) -> Result, ParserError> { if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) { - let next_token = self.next_token(); - match next_token.token { - Token::SingleQuotedString(s) => Ok(Some(format!("'{}'", s))), - Token::Word(s) => Ok(Some(s.to_string())), - _ => self.expected("identifier or cluster literal", next_token)?, - } + Ok(Some(self.parse_identifier(false)?)) } else { Ok(None) } diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 700ff8ba1..b90167fb6 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -3506,7 +3506,7 @@ fn parse_create_table_on_cluster() { let sql = "CREATE TABLE t ON CLUSTER '{cluster}' (a INT, b INT)"; match generic.verified_stmt(sql) { Statement::CreateTable(CreateTable { on_cluster, .. }) => { - assert_eq!(on_cluster.unwrap(), "'{cluster}'".to_string()); + assert_eq!(on_cluster.unwrap().to_string(), "'{cluster}'".to_string()); } _ => unreachable!(), } @@ -3515,7 +3515,7 @@ fn parse_create_table_on_cluster() { let sql = "CREATE TABLE t ON CLUSTER my_cluster (a INT, b INT)"; match generic.verified_stmt(sql) { Statement::CreateTable(CreateTable { on_cluster, .. }) => { - assert_eq!(on_cluster.unwrap(), "my_cluster".to_string()); + assert_eq!(on_cluster.unwrap().to_string(), "my_cluster".to_string()); } _ => unreachable!(), } @@ -3824,13 +3824,26 @@ fn parse_alter_table() { #[test] fn test_alter_table_with_on_cluster() { - let sql = "ALTER TABLE t ON CLUSTER 'cluster' ADD CONSTRAINT bar PRIMARY KEY (baz)"; - match all_dialects().verified_stmt(sql) { + match all_dialects() + .verified_stmt("ALTER TABLE t ON CLUSTER 'cluster' ADD CONSTRAINT bar PRIMARY KEY (baz)") + { + Statement::AlterTable { + name, on_cluster, .. + } => { + std::assert_eq!(name.to_string(), "t"); + std::assert_eq!(on_cluster, Some(Ident::with_quote('\'', "cluster"))); + } + _ => unreachable!(), + } + + match all_dialects() + .verified_stmt("ALTER TABLE t ON CLUSTER cluster_name ADD CONSTRAINT bar PRIMARY KEY (baz)") + { Statement::AlterTable { name, on_cluster, .. } => { std::assert_eq!(name.to_string(), "t"); - std::assert_eq!(on_cluster, Some("'cluster'".to_string())); + std::assert_eq!(on_cluster, Some(Ident::new("cluster_name"))); } _ => unreachable!(), } @@ -3839,7 +3852,7 @@ fn test_alter_table_with_on_cluster() { .parse_sql_statements("ALTER TABLE t ON CLUSTER 123 ADD CONSTRAINT bar PRIMARY KEY (baz)"); std::assert_eq!( res.unwrap_err(), - ParserError::ParserError("Expected: identifier or cluster literal, found: 123".to_string()) + ParserError::ParserError("Expected: identifier, found: 123".to_string()) ) }