Skip to content

Commit

Permalink
feat(clickhouse): add support for ALTER TABLE REPLACE PARTITION state…
Browse files Browse the repository at this point in the history
…ment (#3441)

* feat(clickhouse): add support for ALTER TABLE REPLACE PARTITION statement

* feat(clickhouse): add support for ALTER TABLE REPLACE PARTITION statement

Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>

* feat(clickhouse): add support for replace partition expression

* feat(clickhouse): add support for ALTER TABLE REPLACE PARTITION statement

---------

Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
  • Loading branch information
GaliFFun and georgesittas committed May 9, 2024
1 parent 5cfb29c commit 07badc9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ class Parser(parser.Parser):
"CODEC": lambda self: self._parse_compress(),
}

ALTER_PARSERS = {
**parser.Parser.ALTER_PARSERS,
"REPLACE": lambda self: self._parse_alter_table_replace(),
}

SCHEMA_UNNAMED_CONSTRAINTS = {
*parser.Parser.SCHEMA_UNNAMED_CONSTRAINTS,
"INDEX",
Expand Down Expand Up @@ -593,6 +598,16 @@ def _parse_partition(self) -> t.Optional[exp.Partition]:

return self.expression(exp.Partition, expressions=expressions)

def _parse_alter_table_replace(self) -> t.Optional[exp.Expression]:
partition = self._parse_partition()

if not partition or not self._match(TokenType.FROM):
return None

return self.expression(
exp.ReplacePartition, expression=partition, source=self._parse_table_parts()
)

class Generator(generator.Generator):
QUERY_HINTS = False
STRUCT_DELIMITER = ("(", ")")
Expand Down Expand Up @@ -849,3 +864,8 @@ def partition_sql(self, expression: exp.Partition) -> str:

def partitionid_sql(self, expression: exp.PartitionId) -> str:
return f"ID {self.sql(expression.this)}"

def replacepartition_sql(self, expression: exp.ReplacePartition) -> str:
return (
f"REPLACE {self.sql(expression.expression)} FROM {self.sql(expression, 'source')}"
)
5 changes: 5 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4191,6 +4191,11 @@ class DropPartition(Expression):
arg_types = {"expressions": True, "exists": False}


# https://clickhouse.com/docs/en/sql-reference/statements/alter/partition#replace-partition
class ReplacePartition(Expression):
arg_types = {"expression": True, "source": True}


# Binary expressions like (ADD a b)
class Binary(Condition):
arg_types = {"this": True, "expression": True}
Expand Down
7 changes: 7 additions & 0 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ def test_clickhouse(self):
)
self.validate_identity("ALTER TABLE visits DROP PARTITION ID '201901'")

self.validate_identity("ALTER TABLE visits REPLACE PARTITION 201901 FROM visits_tmp")
self.validate_identity("ALTER TABLE visits REPLACE PARTITION ALL FROM visits_tmp")
self.validate_identity(
"ALTER TABLE visits REPLACE PARTITION tuple(toYYYYMM(toDate('2019-01-25'))) FROM visits_tmp"
)
self.validate_identity("ALTER TABLE visits REPLACE PARTITION ID '201901' FROM visits_tmp")

def test_cte(self):
self.validate_identity("WITH 'x' AS foo SELECT foo")
self.validate_identity("WITH ['c'] AS field_names SELECT field_names")
Expand Down

0 comments on commit 07badc9

Please sign in to comment.