2025-02-13 14:52:26 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from sqlglot import exp, generator, parser
|
2025-02-13 06:15:54 +01:00
|
|
|
from sqlglot.dialects.dialect import Dialect
|
|
|
|
|
|
|
|
|
|
|
|
def _if_sql(self, expression):
|
|
|
|
return f"IF {self.sql(expression, 'this')} THEN {self.sql(expression, 'true')} ELSE {self.sql(expression, 'false')} END"
|
|
|
|
|
|
|
|
|
|
|
|
def _coalesce_sql(self, expression):
|
|
|
|
return f"IFNULL({self.sql(expression, 'this')}, {self.expressions(expression)})"
|
|
|
|
|
|
|
|
|
|
|
|
def _count_sql(self, expression):
|
|
|
|
this = expression.this
|
|
|
|
if isinstance(this, exp.Distinct):
|
2025-02-13 14:46:14 +01:00
|
|
|
return f"COUNTD({self.expressions(this, flat=True)})"
|
2025-02-13 06:15:54 +01:00
|
|
|
return f"COUNT({self.sql(expression, 'this')})"
|
|
|
|
|
|
|
|
|
|
|
|
class Tableau(Dialect):
|
2025-02-13 14:52:26 +01:00
|
|
|
class Generator(generator.Generator):
|
2025-02-13 15:51:35 +01:00
|
|
|
JOIN_HINTS = False
|
|
|
|
TABLE_HINTS = False
|
|
|
|
|
2025-02-13 06:15:54 +01:00
|
|
|
TRANSFORMS = {
|
2025-02-13 14:52:26 +01:00
|
|
|
**generator.Generator.TRANSFORMS, # type: ignore
|
2025-02-13 06:15:54 +01:00
|
|
|
exp.If: _if_sql,
|
|
|
|
exp.Coalesce: _coalesce_sql,
|
|
|
|
exp.Count: _count_sql,
|
|
|
|
}
|
|
|
|
|
2025-02-13 15:51:35 +01:00
|
|
|
PROPERTIES_LOCATION = {
|
|
|
|
**generator.Generator.PROPERTIES_LOCATION, # type: ignore
|
|
|
|
exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
|
|
|
|
}
|
|
|
|
|
2025-02-13 14:52:26 +01:00
|
|
|
class Parser(parser.Parser):
|
2025-02-13 06:15:54 +01:00
|
|
|
FUNCTIONS = {
|
2025-02-13 15:01:11 +01:00
|
|
|
**parser.Parser.FUNCTIONS, # type: ignore
|
2025-02-13 14:46:14 +01:00
|
|
|
"COUNTD": lambda args: exp.Count(this=exp.Distinct(expressions=args)),
|
2025-02-13 06:15:54 +01:00
|
|
|
}
|