2025-02-13 14:52:26 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-13 21:25:55 +01:00
|
|
|
from sqlglot import exp, generator, parser, tokens, transforms
|
2025-02-13 16:00:14 +01:00
|
|
|
from sqlglot.dialects.dialect import Dialect, rename_func
|
2025-02-13 06:15:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Tableau(Dialect):
|
2025-02-13 21:25:55 +01:00
|
|
|
class Tokenizer(tokens.Tokenizer):
|
|
|
|
IDENTIFIERS = [("[", "]")]
|
|
|
|
QUOTES = ["'", '"']
|
|
|
|
|
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 20:42:40 +01:00
|
|
|
QUERY_HINTS = False
|
2025-02-13 15:51:35 +01:00
|
|
|
|
2025-02-13 06:15:54 +01:00
|
|
|
TRANSFORMS = {
|
2025-02-13 15:56:32 +01:00
|
|
|
**generator.Generator.TRANSFORMS,
|
2025-02-13 16:00:14 +01:00
|
|
|
exp.Coalesce: rename_func("IFNULL"),
|
2025-02-13 15:52:54 +01:00
|
|
|
exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
|
2025-02-13 06:15:54 +01:00
|
|
|
}
|
|
|
|
|
2025-02-13 15:51:35 +01:00
|
|
|
PROPERTIES_LOCATION = {
|
2025-02-13 15:56:32 +01:00
|
|
|
**generator.Generator.PROPERTIES_LOCATION,
|
2025-02-13 15:51:35 +01:00
|
|
|
exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
|
|
|
|
}
|
|
|
|
|
2025-02-13 15:56:32 +01:00
|
|
|
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 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')})"
|
|
|
|
|
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:56:32 +01:00
|
|
|
**parser.Parser.FUNCTIONS,
|
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
|
|
|
}
|
2025-02-13 21:19:36 +01:00
|
|
|
NO_PAREN_IF_COMMANDS = False
|