2025-02-13 14:53:05 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-13 21:52:55 +01:00
|
|
|
import typing as t
|
|
|
|
|
2025-02-13 06:15:54 +01:00
|
|
|
from sqlglot import exp
|
2025-02-13 15:52:09 +01:00
|
|
|
from sqlglot.dialects.dialect import (
|
|
|
|
approx_count_distinct_sql,
|
|
|
|
arrow_json_extract_sql,
|
2025-02-13 21:28:36 +01:00
|
|
|
build_timestamp_trunc,
|
2025-02-13 15:52:09 +01:00
|
|
|
rename_func,
|
2025-02-13 21:30:28 +01:00
|
|
|
unit_to_str,
|
2025-02-13 21:52:55 +01:00
|
|
|
inline_array_sql,
|
2025-02-13 15:52:09 +01:00
|
|
|
)
|
2025-02-13 06:15:54 +01:00
|
|
|
from sqlglot.dialects.mysql import MySQL
|
2025-02-13 15:46:19 +01:00
|
|
|
from sqlglot.helper import seq_get
|
2025-02-13 06:15:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class StarRocks(MySQL):
|
2025-02-13 15:57:23 +01:00
|
|
|
class Parser(MySQL.Parser):
|
2025-02-13 15:46:19 +01:00
|
|
|
FUNCTIONS = {
|
|
|
|
**MySQL.Parser.FUNCTIONS,
|
2025-02-13 21:28:36 +01:00
|
|
|
"DATE_TRUNC": build_timestamp_trunc,
|
2025-02-13 20:51:40 +01:00
|
|
|
"DATEDIFF": lambda args: exp.DateDiff(
|
|
|
|
this=seq_get(args, 0), expression=seq_get(args, 1), unit=exp.Literal.string("DAY")
|
|
|
|
),
|
|
|
|
"DATE_DIFF": lambda args: exp.DateDiff(
|
|
|
|
this=seq_get(args, 1), expression=seq_get(args, 2), unit=seq_get(args, 0)
|
|
|
|
),
|
|
|
|
"REGEXP": exp.RegexpLike.from_arg_list,
|
2025-02-13 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
2025-02-13 21:52:55 +01:00
|
|
|
def _parse_unnest(self, with_alias: bool = True) -> t.Optional[exp.Unnest]:
|
|
|
|
unnest = super()._parse_unnest(with_alias=with_alias)
|
|
|
|
|
|
|
|
if unnest:
|
|
|
|
alias = unnest.args.get("alias")
|
|
|
|
|
|
|
|
if alias and not alias.args.get("columns"):
|
|
|
|
# Starrocks defaults to naming the UNNEST column as "unnest"
|
|
|
|
# if it's not otherwise specified
|
|
|
|
alias.set("columns", [exp.to_identifier("unnest")])
|
|
|
|
|
|
|
|
return unnest
|
|
|
|
|
2025-02-13 15:57:23 +01:00
|
|
|
class Generator(MySQL.Generator):
|
2025-02-13 20:46:55 +01:00
|
|
|
CAST_MAPPING = {}
|
|
|
|
|
2025-02-13 06:15:54 +01:00
|
|
|
TYPE_MAPPING = {
|
2025-02-13 15:57:23 +01:00
|
|
|
**MySQL.Generator.TYPE_MAPPING,
|
2025-02-13 06:15:54 +01:00
|
|
|
exp.DataType.Type.TEXT: "STRING",
|
|
|
|
exp.DataType.Type.TIMESTAMP: "DATETIME",
|
|
|
|
exp.DataType.Type.TIMESTAMPTZ: "DATETIME",
|
|
|
|
}
|
2025-02-13 14:40:43 +01:00
|
|
|
|
|
|
|
TRANSFORMS = {
|
2025-02-13 15:57:23 +01:00
|
|
|
**MySQL.Generator.TRANSFORMS,
|
2025-02-13 21:52:55 +01:00
|
|
|
exp.Array: inline_array_sql,
|
2025-02-13 15:52:09 +01:00
|
|
|
exp.ApproxDistinct: approx_count_distinct_sql,
|
2025-02-13 20:51:40 +01:00
|
|
|
exp.DateDiff: lambda self, e: self.func(
|
2025-02-13 21:30:28 +01:00
|
|
|
"DATE_DIFF", unit_to_str(e), e.this, e.expression
|
2025-02-13 20:51:40 +01:00
|
|
|
),
|
2025-02-13 14:50:31 +01:00
|
|
|
exp.JSONExtractScalar: arrow_json_extract_sql,
|
|
|
|
exp.JSONExtract: arrow_json_extract_sql,
|
2025-02-13 15:53:39 +01:00
|
|
|
exp.RegexpLike: rename_func("REGEXP"),
|
2025-02-13 21:28:36 +01:00
|
|
|
exp.StrToUnix: lambda self, e: self.func("UNIX_TIMESTAMP", e.this, self.format_time(e)),
|
2025-02-13 21:30:28 +01:00
|
|
|
exp.TimestampTrunc: lambda self, e: self.func("DATE_TRUNC", unit_to_str(e), e.this),
|
2025-02-13 14:40:43 +01:00
|
|
|
exp.TimeStrToDate: rename_func("TO_DATE"),
|
2025-02-13 21:28:36 +01:00
|
|
|
exp.UnixToStr: lambda self, e: self.func("FROM_UNIXTIME", e.this, self.format_time(e)),
|
2025-02-13 14:40:43 +01:00
|
|
|
exp.UnixToTime: rename_func("FROM_UNIXTIME"),
|
|
|
|
}
|
2025-02-13 15:57:23 +01:00
|
|
|
|
2025-02-13 14:53:05 +01:00
|
|
|
TRANSFORMS.pop(exp.DateTrunc)
|