Skip to content

Commit 61bc01c

Browse files
authored
fix(clickhouse): allow string literal for clickhouse ON CLUSTER clause (#4971)
1 parent 06f4417 commit 61bc01c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

sqlglot/dialects/clickhouse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def _parse_primary_key(
837837
def _parse_on_property(self) -> t.Optional[exp.Expression]:
838838
index = self._index
839839
if self._match_text_seq("CLUSTER"):
840-
this = self._parse_id_var()
840+
this = self._parse_string() or self._parse_id_var()
841841
if this:
842842
return self.expression(exp.OnCluster, this=this)
843843
else:

tests/dialects/test_clickhouse.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ def test_clickhouse(self):
105105
self.validate_identity("SELECT * FROM table LIMIT 1 BY a, b")
106106
self.validate_identity("SELECT * FROM table LIMIT 2 OFFSET 1 BY a, b")
107107
self.validate_identity("TRUNCATE TABLE t1 ON CLUSTER test_cluster")
108+
self.validate_identity("TRUNCATE TABLE t1 ON CLUSTER '{cluster}'")
108109
self.validate_identity("TRUNCATE DATABASE db")
109110
self.validate_identity("TRUNCATE DATABASE db ON CLUSTER test_cluster")
111+
self.validate_identity("TRUNCATE DATABASE db ON CLUSTER '{cluster}'")
110112
self.validate_identity(
111113
"SELECT DATE_BIN(toDateTime('2023-01-01 14:45:00'), INTERVAL '1' MINUTE, toDateTime('2023-01-01 14:35:30'), 'UTC')",
112114
)
@@ -155,12 +157,21 @@ def test_clickhouse(self):
155157
self.validate_identity(
156158
"CREATE TABLE test ON CLUSTER default (id UInt8) ENGINE=AggregatingMergeTree() ORDER BY tuple()"
157159
)
160+
self.validate_identity(
161+
"CREATE TABLE test ON CLUSTER '{cluster}' (id UInt8) ENGINE=AggregatingMergeTree() ORDER BY tuple()"
162+
)
158163
self.validate_identity(
159164
"CREATE MATERIALIZED VIEW test_view ON CLUSTER cl1 (id UInt8) ENGINE=AggregatingMergeTree() ORDER BY tuple() AS SELECT * FROM test_data"
160165
)
166+
self.validate_identity(
167+
"CREATE MATERIALIZED VIEW test_view ON CLUSTER '{cluster}' (id UInt8) ENGINE=AggregatingMergeTree() ORDER BY tuple() AS SELECT * FROM test_data"
168+
)
161169
self.validate_identity(
162170
"CREATE MATERIALIZED VIEW test_view ON CLUSTER cl1 TO table1 AS SELECT * FROM test_data"
163171
)
172+
self.validate_identity(
173+
"CREATE MATERIALIZED VIEW test_view ON CLUSTER '{cluster}' TO table1 AS SELECT * FROM test_data"
174+
)
164175
self.validate_identity(
165176
"CREATE MATERIALIZED VIEW test_view TO db.table1 (id UInt8) AS SELECT * FROM test_data"
166177
)
@@ -547,7 +558,9 @@ def test_clickhouse(self):
547558
)
548559
self.validate_identity("ALTER TABLE visits REPLACE PARTITION ID '201901' FROM visits_tmp")
549560
self.validate_identity("ALTER TABLE visits ON CLUSTER test_cluster DROP COLUMN col1")
561+
self.validate_identity("ALTER TABLE visits ON CLUSTER '{cluster}' DROP COLUMN col1")
550562
self.validate_identity("DELETE FROM tbl ON CLUSTER test_cluster WHERE date = '2019-01-01'")
563+
self.validate_identity("DELETE FROM tbl ON CLUSTER '{cluster}' WHERE date = '2019-01-01'")
551564

552565
self.assertIsInstance(
553566
parse_one("Tuple(select Int64)", into=exp.DataType, read="clickhouse"), exp.DataType
@@ -1182,6 +1195,7 @@ def test_drop_on_cluster(self):
11821195
for creatable in ("DATABASE", "TABLE", "VIEW", "DICTIONARY", "FUNCTION"):
11831196
with self.subTest(f"Test DROP {creatable} ON CLUSTER"):
11841197
self.validate_identity(f"DROP {creatable} test ON CLUSTER test_cluster")
1198+
self.validate_identity(f"DROP {creatable} test ON CLUSTER '{{cluster}}'")
11851199

11861200
def test_datetime_funcs(self):
11871201
# Each datetime func has an alias that is roundtripped to the original name e.g. (DATE_SUB, DATESUB) -> DATE_SUB

0 commit comments

Comments
 (0)