summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/clickhouse.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-10-26 17:21:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-10-26 17:21:50 +0000
commit6c774776db5e016d597e582c7006ba8d27006f9d (patch)
tree8a65b7a9938002f9b152d9a6dfd150f15e402a6b /sqlglot/dialects/clickhouse.py
parentAdding upstream version 18.13.0. (diff)
downloadsqlglot-6c774776db5e016d597e582c7006ba8d27006f9d.tar.xz
sqlglot-6c774776db5e016d597e582c7006ba8d27006f9d.zip
Adding upstream version 18.17.0.upstream/18.17.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/clickhouse.py')
-rw-r--r--sqlglot/dialects/clickhouse.py31
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.