From 349936285bc12fbb39f1c1ecfae6480f86c9ae71 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Sun, 24 Sep 2023 06:10:21 -0400 Subject: [PATCH] feat(clickhouse): add isnan and startswith renamings --- sqlglot/dialects/clickhouse.py | 2 ++ tests/dialects/test_clickhouse.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py index 744608101b..14f953b614 100644 --- a/sqlglot/dialects/clickhouse.py +++ b/sqlglot/dialects/clickhouse.py @@ -359,12 +359,14 @@ class Generator(generator.Generator): "DATE_DIFF", exp.Literal.string(e.text("unit") or "day"), e.expression, e.this ), exp.Final: lambda self, e: f"{self.sql(e, 'this')} FINAL", + exp.IsNan: rename_func("isNaN"), exp.Map: lambda self, e: _lower_func(var_map_sql(self, e)), exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}", exp.Pivot: no_pivot_sql, exp.Quantile: lambda self, e: self.func("quantile", e.args.get("quantile")) + f"({self.sql(e, 'this')})", exp.RegexpLike: lambda self, e: f"match({self.format_args(e.this, e.expression)})", + exp.StartsWith: rename_func("startsWith"), exp.StrPosition: lambda self, e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})", exp.VarMap: lambda self, e: _lower_func(var_map_sql(self, e)), exp.Xor: lambda self, e: self.func("xor", e.this, e.expression, *e.expressions), diff --git a/tests/dialects/test_clickhouse.py b/tests/dialects/test_clickhouse.py index 40a270ed7e..da07eb76ef 100644 --- a/tests/dialects/test_clickhouse.py +++ b/tests/dialects/test_clickhouse.py @@ -225,6 +225,18 @@ def test_clickhouse(self): "SELECT s, arr_external FROM arrays_test ARRAY JOIN [1, 2, 3] AS arr_external" ) + self.validate_identity("SELECT isNaN(x)") + self.validate_all( + "SELECT IS_NAN(x), ISNAN(x)", + write={"clickhouse": "SELECT isNaN(x), isNaN(x)"}, + ) + + self.validate_identity("SELECT startsWith('a', 'b')") + self.validate_all( + "SELECT STARTS_WITH('a', 'b'), STARTSWITH('a', 'b')", + write={"clickhouse": "SELECT startsWith('a', 'b'), startsWith('a', 'b')"}, + ) + def test_cte(self): self.validate_identity("WITH 'x' AS foo SELECT foo") self.validate_identity("WITH SUM(bytes) AS foo SELECT foo FROM system.parts")