diff options
Diffstat (limited to 'sqlglot/dialects/clickhouse.py')
-rw-r--r-- | sqlglot/dialects/clickhouse.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py index e9d9326..30f728c 100644 --- a/sqlglot/dialects/clickhouse.py +++ b/sqlglot/dialects/clickhouse.py @@ -5,6 +5,7 @@ import typing as t from sqlglot import exp, generator, parser, tokens, transforms from sqlglot.dialects.dialect import ( Dialect, + arg_max_or_min_no_count, inline_array_sql, no_pivot_sql, rename_func, @@ -373,8 +374,11 @@ class ClickHouse(Dialect): exp.Select: transforms.preprocess([transforms.eliminate_qualify]), exp.AnyValue: rename_func("any"), exp.ApproxDistinct: rename_func("uniq"), + exp.ArgMax: arg_max_or_min_no_count("argMax"), + exp.ArgMin: arg_max_or_min_no_count("argMin"), exp.Array: inline_array_sql, exp.CastToStrType: rename_func("CAST"), + exp.CurrentDate: lambda self, e: self.func("CURRENT_DATE"), exp.DateAdd: lambda self, e: self.func( "DATE_ADD", exp.Literal.string(e.text("unit") or "day"), e.expression, e.this ), @@ -418,6 +422,33 @@ class ClickHouse(Dialect): "NAMED COLLECTION", } + def _any_to_has( + self, + expression: exp.EQ | exp.NEQ, + default: t.Callable[[t.Any], str], + prefix: str = "", + ) -> str: + if isinstance(expression.left, exp.Any): + arr = expression.left + this = expression.right + elif isinstance(expression.right, exp.Any): + arr = expression.right + this = expression.left + else: + return default(expression) + return prefix + self.func("has", arr.this.unnest(), this) + + def eq_sql(self, expression: exp.EQ) -> str: + return self._any_to_has(expression, super().eq_sql) + + def neq_sql(self, expression: exp.NEQ) -> str: + return self._any_to_has(expression, super().neq_sql, "NOT ") + + def regexpilike_sql(self, expression: exp.RegexpILike) -> str: + # Manually add a flag to make the search case-insensitive + regex = self.func("CONCAT", "'(?i)'", expression.expression) + return f"match({self.format_args(expression.this, regex)})" + def datatype_sql(self, expression: exp.DataType) -> str: # String is the standard ClickHouse type, every other variant is just an alias. # Additionally, any supplied length parameter will be ignored. |