Skip to content

Commit

Permalink
fix(clickhouse): fix incorrect array param generation for clickhouse …
Browse files Browse the repository at this point in the history
…quantiles (#2311)

* fix(clickhouse): fix incorrect array param generation for clickhouse quantiles

* chore: `_quantile` -> `_quantile_sql`

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

* chore: omit `get` cal because `quantile` is a required argument

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

---------

Co-authored-by: Jo <46752250+GeorgeSittas@users.noreply.github.com>
  • Loading branch information
cpcloud and georgesittas authored Sep 25, 2023
1 parent 3cb3131 commit c51ecb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def _lower_func(sql: str) -> str:
return sql[:index].lower() + sql[index:]


def _quantile_sql(self, e):
quantile = e.args["quantile"]
args = f"({self.sql(e, 'this')})"
if isinstance(quantile, exp.Array):
func = self.func("quantiles", *quantile)
else:
func = self.func("quantile", quantile)
return func + args


class ClickHouse(Dialect):
NORMALIZE_FUNCTIONS: bool | str = False
NULL_ORDERING = "nulls_are_last"
Expand Down Expand Up @@ -363,8 +373,7 @@ class Generator(generator.Generator):
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.Quantile: _quantile_sql,
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'))})",
Expand Down
15 changes: 15 additions & 0 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ def test_clickhouse(self):
self.validate_identity(
"SELECT s, arr_external FROM arrays_test ARRAY JOIN [1, 2, 3] AS arr_external"
)
self.validate_all(
"SELECT quantile(0.5)(a)",
read={"duckdb": "SELECT quantile(a, 0.5)"},
write={"clickhouse": "SELECT quantile(0.5)(a)"},
)
self.validate_all(
"SELECT quantiles(0.5, 0.4)(a)",
read={"duckdb": "SELECT quantile(a, [0.5, 0.4])"},
write={"clickhouse": "SELECT quantiles(0.5, 0.4)(a)"},
)
self.validate_all(
"SELECT quantiles(0.5)(a)",
read={"duckdb": "SELECT quantile(a, [0.5])"},
write={"clickhouse": "SELECT quantiles(0.5)(a)"},
)

self.validate_identity("SELECT isNaN(x)")
self.validate_all(
Expand Down

0 comments on commit c51ecb1

Please sign in to comment.