diff options
Diffstat (limited to 'sqlglot/dialects/redshift.py')
-rw-r--r-- | sqlglot/dialects/redshift.py | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/sqlglot/dialects/redshift.py b/sqlglot/dialects/redshift.py index b70a8a1..04e78a5 100644 --- a/sqlglot/dialects/redshift.py +++ b/sqlglot/dialects/redshift.py @@ -58,6 +58,11 @@ class Redshift(Postgres): "STRTOL": exp.FromBase.from_arg_list, } + NO_PAREN_FUNCTION_PARSERS = { + **Postgres.Parser.NO_PAREN_FUNCTION_PARSERS, + "APPROXIMATE": lambda self: self._parse_approximate_count(), + } + def _parse_table( self, schema: bool = False, @@ -93,11 +98,22 @@ class Redshift(Postgres): return this - def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]: + def _parse_convert( + self, strict: bool, safe: t.Optional[bool] = None + ) -> t.Optional[exp.Expression]: to = self._parse_types() self._match(TokenType.COMMA) this = self._parse_bitwise() - return self.expression(exp.TryCast, this=this, to=to) + return self.expression(exp.TryCast, this=this, to=to, safe=safe) + + def _parse_approximate_count(self) -> t.Optional[exp.ApproxDistinct]: + index = self._index - 1 + func = self._parse_function() + + if isinstance(func, exp.Count) and isinstance(func.this, exp.Distinct): + return self.expression(exp.ApproxDistinct, this=seq_get(func.this.expressions, 0)) + self._retreat(index) + return None class Tokenizer(Postgres.Tokenizer): BIT_STRINGS = [] @@ -144,6 +160,7 @@ class Redshift(Postgres): **Postgres.Generator.TRANSFORMS, exp.Concat: concat_to_dpipe_sql, exp.ConcatWs: concat_ws_to_dpipe_sql, + exp.ApproxDistinct: lambda self, e: f"APPROXIMATE COUNT(DISTINCT {self.sql(e, 'this')})", exp.CurrentTimestamp: lambda self, e: "SYSDATE", exp.DateAdd: lambda self, e: self.func( "DATEADD", exp.var(e.text("unit") or "day"), e.expression, e.this |