Skip to content

Commit b46c5b3

Browse files
GaliFFunRustem Galiev
and
Rustem Galiev
authored
feat(clickhouse): add support for partition expression (#3428)
* feat(clickhouse): add support for partition expression * feat(clickhouse): add support for partition expression --------- Co-authored-by: Rustem Galiev <[email protected]>
1 parent 80670bb commit b46c5b3

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

sqlglot/dialects/clickhouse.py

+21
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,21 @@ def _parse_index_constraint(
578578
granularity=granularity,
579579
)
580580

581+
def _parse_partition(self) -> t.Optional[exp.Partition]:
582+
# https://clickhouse.com/docs/en/sql-reference/statements/alter/partition#how-to-set-partition-expression
583+
if not self._match(TokenType.PARTITION):
584+
return None
585+
586+
if self._match_text_seq("ID"):
587+
# Corresponds to the PARTITION ID <string_value> syntax
588+
expressions: t.List[exp.Expression] = [
589+
self.expression(exp.PartitionId, this=self._parse_string())
590+
]
591+
else:
592+
expressions = self._parse_expressions()
593+
594+
return self.expression(exp.Partition, expressions=expressions)
595+
581596
class Generator(generator.Generator):
582597
QUERY_HINTS = False
583598
STRUCT_DELIMITER = ("(", ")")
@@ -828,3 +843,9 @@ def indexcolumnconstraint_sql(self, expression: exp.IndexColumnConstraint) -> st
828843
granularity = f" GRANULARITY {granularity}" if granularity else ""
829844

830845
return f"INDEX{this}{expr}{index_type}{granularity}"
846+
847+
def partition_sql(self, expression: exp.Partition) -> str:
848+
return f"PARTITION {self.expressions(expression, flat=True)}"
849+
850+
def partitionid_sql(self, expression: exp.PartitionId) -> str:
851+
return f"ID {self.sql(expression.this)}"

sqlglot/expressions.py

+5
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,11 @@ class PartitionRange(Expression):
21772177
arg_types = {"this": True, "expression": True}
21782178

21792179

2180+
# https://clickhouse.com/docs/en/sql-reference/statements/alter/partition#how-to-set-partition-expression
2181+
class PartitionId(Expression):
2182+
pass
2183+
2184+
21802185
class Fetch(Expression):
21812186
arg_types = {
21822187
"direction": False,

tests/dialects/test_clickhouse.py

+7
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ def test_clickhouse(self):
425425
},
426426
)
427427

428+
self.validate_identity("ALTER TABLE visits DROP PARTITION 201901")
429+
self.validate_identity("ALTER TABLE visits DROP PARTITION ALL")
430+
self.validate_identity(
431+
"ALTER TABLE visits DROP PARTITION tuple(toYYYYMM(toDate('2019-01-25')))"
432+
)
433+
self.validate_identity("ALTER TABLE visits DROP PARTITION ID '201901'")
434+
428435
def test_cte(self):
429436
self.validate_identity("WITH 'x' AS foo SELECT foo")
430437
self.validate_identity("WITH ['c'] AS field_names SELECT field_names")

0 commit comments

Comments
 (0)