summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/tableau.py
blob: d5fba178ba026bc5f532714431f9c24a142d29db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import annotations

from sqlglot import exp, generator, parser, transforms
from sqlglot.dialects.dialect import Dialect


class Tableau(Dialect):
    class Generator(generator.Generator):
        JOIN_HINTS = False
        TABLE_HINTS = False

        TRANSFORMS = {
            **generator.Generator.TRANSFORMS,
            exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
        }

        PROPERTIES_LOCATION = {
            **generator.Generator.PROPERTIES_LOCATION,
            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
        }

        def if_sql(self, expression: exp.If) -> str:
            this = self.sql(expression, "this")
            true = self.sql(expression, "true")
            false = self.sql(expression, "false")
            return f"IF {this} THEN {true} ELSE {false} END"

        def coalesce_sql(self, expression: exp.Coalesce) -> str:
            return f"IFNULL({self.sql(expression, 'this')}, {self.expressions(expression)})"

        def count_sql(self, expression: exp.Count) -> str:
            this = expression.this
            if isinstance(this, exp.Distinct):
                return f"COUNTD({self.expressions(this, flat=True)})"
            return f"COUNT({self.sql(expression, 'this')})"

    class Parser(parser.Parser):
        FUNCTIONS = {
            **parser.Parser.FUNCTIONS,
            "COUNTD": lambda args: exp.Count(this=exp.Distinct(expressions=args)),
        }