sqlglot-odps 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlglot_odps/__init__.py +21 -0
- sqlglot_odps/dialect.py +42 -0
- sqlglot_odps/generator.py +381 -0
- sqlglot_odps/parser.py +738 -0
- sqlglot_odps/py.typed +0 -0
- sqlglot_odps/statements.py +86 -0
- sqlglot_odps-0.1.0.dist-info/METADATA +185 -0
- sqlglot_odps-0.1.0.dist-info/RECORD +11 -0
- sqlglot_odps-0.1.0.dist-info/WHEEL +4 -0
- sqlglot_odps-0.1.0.dist-info/entry_points.txt +2 -0
- sqlglot_odps-0.1.0.dist-info/licenses/LICENSE +21 -0
sqlglot_odps/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from sqlglot_odps.dialect import Odps
|
|
2
|
+
from sqlglot_odps.generator import OdpsGenerator
|
|
3
|
+
from sqlglot_odps.parser import OdpsParser
|
|
4
|
+
from sqlglot_odps.statements import (
|
|
5
|
+
StatementKind,
|
|
6
|
+
classify,
|
|
7
|
+
classify_script,
|
|
8
|
+
parse_script,
|
|
9
|
+
statement_kind,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"Odps",
|
|
14
|
+
"OdpsParser",
|
|
15
|
+
"OdpsGenerator",
|
|
16
|
+
"StatementKind",
|
|
17
|
+
"classify",
|
|
18
|
+
"classify_script",
|
|
19
|
+
"parse_script",
|
|
20
|
+
"statement_kind",
|
|
21
|
+
]
|
sqlglot_odps/dialect.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from sqlglot.dialects.hive import Hive
|
|
4
|
+
from sqlglot.tokens import TokenType
|
|
5
|
+
|
|
6
|
+
from sqlglot_odps.generator import OdpsGenerator
|
|
7
|
+
from sqlglot_odps.parser import OdpsParser
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Odps(Hive):
|
|
11
|
+
ALIAS_POST_VERSION = False
|
|
12
|
+
|
|
13
|
+
TIME_MAPPING = {
|
|
14
|
+
"yyyy": "%Y",
|
|
15
|
+
"yy": "%y",
|
|
16
|
+
"mm": "%m",
|
|
17
|
+
"dd": "%d",
|
|
18
|
+
"hh": "%H",
|
|
19
|
+
"mi": "%M",
|
|
20
|
+
"ss": "%S",
|
|
21
|
+
"ff3": "%f",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
DATE_FORMAT = "'yyyy-mm-dd'"
|
|
25
|
+
TIME_FORMAT = "'yyyy-mm-dd hh:mi:ss'"
|
|
26
|
+
|
|
27
|
+
class Tokenizer(Hive.Tokenizer):
|
|
28
|
+
COMMANDS = Hive.Tokenizer.COMMANDS - {TokenType.SHOW}
|
|
29
|
+
KEYWORDS = {
|
|
30
|
+
**Hive.Tokenizer.KEYWORDS,
|
|
31
|
+
"EXPLAIN": TokenType.DESCRIBE,
|
|
32
|
+
"LIFECYCLE": TokenType.KEY,
|
|
33
|
+
"OPTION": TokenType.OPTION,
|
|
34
|
+
"ZORDER BY": TokenType.SORT_BY,
|
|
35
|
+
"TIMESTAMP AS OF": TokenType.FOR,
|
|
36
|
+
"TIMESTAMP BETWEEN": TokenType.FOR,
|
|
37
|
+
"VERSION AS OF": TokenType.FOR,
|
|
38
|
+
"VERSION BETWEEN": TokenType.FOR,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
Parser = OdpsParser
|
|
42
|
+
Generator = OdpsGenerator
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from functools import partial
|
|
4
|
+
|
|
5
|
+
from sqlglot import exp
|
|
6
|
+
from sqlglot.dialects.dialect import rename_func, unit_to_str
|
|
7
|
+
from sqlglot.generators.hive import HiveGenerator
|
|
8
|
+
from sqlglot.transforms import (
|
|
9
|
+
any_to_exists,
|
|
10
|
+
eliminate_distinct_on,
|
|
11
|
+
move_schema_columns_to_partitioned_by,
|
|
12
|
+
preprocess,
|
|
13
|
+
remove_unique_constraints,
|
|
14
|
+
unnest_to_explode,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
_AUTO_PARTITION_TYPES = (
|
|
18
|
+
exp.DateTrunc,
|
|
19
|
+
exp.TimestampTrunc,
|
|
20
|
+
exp.DatetimeTrunc,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _move_schema_columns_to_partitioned_by(expression: exp.Expr) -> exp.Expr:
|
|
25
|
+
"""Like the Hive transform, but skip AUTO PARTITIONED BY (where this is a DateTrunc/Alias)."""
|
|
26
|
+
assert isinstance(expression, exp.Create)
|
|
27
|
+
prop = expression.find(exp.PartitionedByProperty)
|
|
28
|
+
if prop and prop.args.get("auto"):
|
|
29
|
+
return expression
|
|
30
|
+
return move_schema_columns_to_partitioned_by(expression)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class OdpsGenerator(HiveGenerator):
|
|
34
|
+
TYPE_MAPPING = {
|
|
35
|
+
**HiveGenerator.TYPE_MAPPING,
|
|
36
|
+
exp.DType.DATETIME: "DATETIME",
|
|
37
|
+
exp.DType.VARCHAR: "STRING",
|
|
38
|
+
exp.DType.CHAR: "STRING",
|
|
39
|
+
exp.DType.TEXT: "STRING",
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
TRANSFORMS = {
|
|
43
|
+
**HiveGenerator.TRANSFORMS,
|
|
44
|
+
exp.Select: preprocess(
|
|
45
|
+
[
|
|
46
|
+
eliminate_distinct_on,
|
|
47
|
+
partial(unnest_to_explode, unnest_using_arrays_zip=False),
|
|
48
|
+
any_to_exists,
|
|
49
|
+
]
|
|
50
|
+
),
|
|
51
|
+
exp.Create: preprocess(
|
|
52
|
+
[
|
|
53
|
+
remove_unique_constraints,
|
|
54
|
+
_move_schema_columns_to_partitioned_by,
|
|
55
|
+
]
|
|
56
|
+
),
|
|
57
|
+
exp.PartitionedByProperty: lambda self, e: self._partitioned_by_sql(e),
|
|
58
|
+
# Date/time transforms
|
|
59
|
+
exp.TsOrDsAdd: lambda self, e: self._dateadd_sql(e),
|
|
60
|
+
exp.DateAdd: lambda self, e: self._dateadd_sql(e),
|
|
61
|
+
exp.TimestampAdd: lambda self, e: self._dateadd_sql(e),
|
|
62
|
+
exp.DatetimeAdd: lambda self, e: self._dateadd_sql(e),
|
|
63
|
+
exp.DateSub: lambda self, e: self._dateadd_sql(e),
|
|
64
|
+
exp.DateDiff: lambda self, e: self._datediff_sql(e),
|
|
65
|
+
exp.DateTrunc: lambda self, e: self._datetrunc_sql(e),
|
|
66
|
+
exp.TimestampTrunc: lambda self, e: self._datetrunc_sql(e),
|
|
67
|
+
exp.DatetimeTrunc: lambda self, e: self._datetrunc_sql(e),
|
|
68
|
+
exp.CurrentTimestamp: lambda self, e: "GETDATE()",
|
|
69
|
+
exp.CurrentDatetime: lambda self, e: "NOW()",
|
|
70
|
+
# String transforms
|
|
71
|
+
exp.Lower: rename_func("TOLOWER"),
|
|
72
|
+
exp.Upper: rename_func("TOUPPER"),
|
|
73
|
+
# JSON / misc
|
|
74
|
+
exp.ParseJSON: rename_func("FROM_JSON"),
|
|
75
|
+
exp.CurrentUser: lambda self, e: "GET_USER_ID()",
|
|
76
|
+
exp.UnixMillis: rename_func("TO_MILLIS"),
|
|
77
|
+
# Aggregate
|
|
78
|
+
exp.ApproxDistinct: rename_func("APPROX_DISTINCT"),
|
|
79
|
+
exp.ArgMax: lambda self, e: self.func("ARG_MAX", e.this, e.expression),
|
|
80
|
+
exp.ArgMin: lambda self, e: self.func("ARG_MIN", e.this, e.expression),
|
|
81
|
+
exp.LogicalAnd: rename_func("BOOL_AND"),
|
|
82
|
+
exp.LogicalOr: rename_func("BOOL_OR"),
|
|
83
|
+
# Statistical aggregate fixes (Hive emits wrong names)
|
|
84
|
+
exp.Space: rename_func("SPACE"),
|
|
85
|
+
exp.VariancePop: rename_func("VAR_POP"),
|
|
86
|
+
exp.Variance: rename_func("VAR_SAMP"),
|
|
87
|
+
# Numeric truncation: TRUNC(n, d)
|
|
88
|
+
exp.Trunc: lambda self, e: self.func("TRUNC", e.this, e.args.get("decimals")),
|
|
89
|
+
# String position: MaxCompute uses INSTR(str, substr), not LOCATE(substr, str)
|
|
90
|
+
exp.StrPosition: lambda self, e: self.func(
|
|
91
|
+
"INSTR", e.this, e.args.get("substr"), e.args.get("position")
|
|
92
|
+
),
|
|
93
|
+
# TO_DATE(str, fmt) returns DATETIME — modeled as StrToTime; emit TO_DATE in MaxCompute
|
|
94
|
+
exp.StrToTime: lambda self, e: self.func(
|
|
95
|
+
"TO_DATE", e.this, e.args.get("format")
|
|
96
|
+
),
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
def _dateadd_sql(
|
|
100
|
+
self,
|
|
101
|
+
expression: (
|
|
102
|
+
exp.TsOrDsAdd
|
|
103
|
+
| exp.DateAdd
|
|
104
|
+
| exp.DateSub
|
|
105
|
+
| exp.TimestampAdd
|
|
106
|
+
| exp.DatetimeAdd
|
|
107
|
+
),
|
|
108
|
+
) -> str:
|
|
109
|
+
unit = unit_to_str(expression) if expression.args.get("unit") else "'DAY'"
|
|
110
|
+
delta = expression.expression
|
|
111
|
+
if isinstance(expression, exp.DateSub):
|
|
112
|
+
# DateSub magnitude is positive; negate it so DATEADD subtracts.
|
|
113
|
+
# Some dialects (e.g. BigQuery) store the magnitude as a string
|
|
114
|
+
# literal — normalize to a number first so we emit -3 not -'3'.
|
|
115
|
+
if isinstance(delta, exp.Literal) and delta.is_string:
|
|
116
|
+
delta = exp.Literal.number(delta.this)
|
|
117
|
+
delta = exp.Neg(this=delta)
|
|
118
|
+
return self.func("DATEADD", expression.this, delta, unit)
|
|
119
|
+
|
|
120
|
+
def _datediff_sql(self, expression: exp.DateDiff) -> str:
|
|
121
|
+
unit = unit_to_str(expression) if expression.args.get("unit") else None
|
|
122
|
+
return self.func("DATEDIFF", expression.this, expression.expression, unit)
|
|
123
|
+
|
|
124
|
+
def _datetrunc_sql(
|
|
125
|
+
self, expression: exp.DateTrunc | exp.TimestampTrunc | exp.DatetimeTrunc
|
|
126
|
+
) -> str:
|
|
127
|
+
unit = expression.args.get("unit")
|
|
128
|
+
# WeekStart units must be emitted as 'week(day)' string literals.
|
|
129
|
+
# unit_to_str returns the raw node name which would produce DATETRUNC(dt, WEEK(MONDAY))
|
|
130
|
+
# — invalid MaxCompute SQL. Reconstruct the canonical 'week(day)' form instead.
|
|
131
|
+
if isinstance(unit, exp.WeekStart):
|
|
132
|
+
day = unit.this.name.lower() if unit.args.get("this") else "monday"
|
|
133
|
+
unit_sql = exp.Literal.string(f"week({day})")
|
|
134
|
+
else:
|
|
135
|
+
unit_sql = unit_to_str(expression)
|
|
136
|
+
return self.func("DATETRUNC", expression.this, unit_sql)
|
|
137
|
+
|
|
138
|
+
def groupconcat_sql(self, expression: exp.GroupConcat) -> str:
|
|
139
|
+
sep = expression.args.get("separator") or exp.Literal.string(",")
|
|
140
|
+
return self.func("WM_CONCAT", sep, expression.this)
|
|
141
|
+
|
|
142
|
+
def tochar_sql(self, expression: exp.ToChar) -> str:
|
|
143
|
+
return self.func("TO_CHAR", expression.this, expression.args.get("format"))
|
|
144
|
+
|
|
145
|
+
def substring_sql(self, expression: exp.Substring) -> str:
|
|
146
|
+
return self.func(
|
|
147
|
+
"SUBSTR",
|
|
148
|
+
expression.this,
|
|
149
|
+
expression.args.get("start"),
|
|
150
|
+
expression.args.get("length"),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
def extract_sql(self, expression: exp.Extract) -> str:
|
|
154
|
+
unit = expression.this
|
|
155
|
+
return self.func(
|
|
156
|
+
"DATEPART", expression.expression, exp.Literal.string(unit.name)
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
def mod_sql(self, expression: exp.Mod) -> str:
|
|
160
|
+
# Reverse the WEEKDAY parser transform: (DAYOFWEEK(x) + 5) % 7 → WEEKDAY(x)
|
|
161
|
+
rhs = expression.expression
|
|
162
|
+
lhs = expression.this
|
|
163
|
+
if (
|
|
164
|
+
isinstance(rhs, exp.Literal)
|
|
165
|
+
and rhs.this == "7"
|
|
166
|
+
and isinstance(lhs, exp.Paren)
|
|
167
|
+
and isinstance(lhs.this, exp.Add)
|
|
168
|
+
and isinstance(lhs.this.this, exp.DayOfWeek)
|
|
169
|
+
and isinstance(lhs.this.expression, exp.Literal)
|
|
170
|
+
and lhs.this.expression.this == "5"
|
|
171
|
+
):
|
|
172
|
+
return self.func("WEEKDAY", lhs.this.this.this)
|
|
173
|
+
return super().mod_sql(expression)
|
|
174
|
+
|
|
175
|
+
def _partitioned_by_sql(self, expression: exp.PartitionedByProperty) -> str:
|
|
176
|
+
inner = expression.this
|
|
177
|
+
if expression.args.get("auto"):
|
|
178
|
+
value = inner.this if isinstance(inner, exp.Alias) else inner
|
|
179
|
+
if isinstance(value, _AUTO_PARTITION_TYPES):
|
|
180
|
+
unit = value.args.get("unit")
|
|
181
|
+
trunc_sql = self.func(
|
|
182
|
+
"TRUNC_TIME", value.this, exp.Literal.string(unit.name.lower())
|
|
183
|
+
)
|
|
184
|
+
alias = (
|
|
185
|
+
f" AS {self.sql(inner, 'alias')}"
|
|
186
|
+
if isinstance(inner, exp.Alias)
|
|
187
|
+
else ""
|
|
188
|
+
)
|
|
189
|
+
return f"AUTO PARTITIONED BY ({trunc_sql}{alias})"
|
|
190
|
+
return f"AUTO PARTITIONED BY ({self.sql(inner)})"
|
|
191
|
+
return f"PARTITIONED BY {self.sql(inner)}"
|
|
192
|
+
|
|
193
|
+
def block_sql(self, expression: exp.Block) -> str:
|
|
194
|
+
sql = super().block_sql(expression)
|
|
195
|
+
return sql if not sql or sql.endswith(";") else f"{sql};"
|
|
196
|
+
|
|
197
|
+
def select_sql(self, expression: exp.Select) -> str:
|
|
198
|
+
if not expression.args.get("read"):
|
|
199
|
+
return super().select_sql(expression)
|
|
200
|
+
table = expression.args["from_"].this
|
|
201
|
+
columns = (
|
|
202
|
+
""
|
|
203
|
+
if expression.is_star
|
|
204
|
+
else f" ({self.expressions(expression, flat=True)})"
|
|
205
|
+
)
|
|
206
|
+
partition = self.sql(table, "partition")
|
|
207
|
+
partition = f" {partition}" if partition else ""
|
|
208
|
+
limit = expression.args.get("limit")
|
|
209
|
+
count = f" {self.sql(limit, 'expression')}" if limit else ""
|
|
210
|
+
return f"READ {self.table_parts(table)}{columns}{partition}{count}"
|
|
211
|
+
|
|
212
|
+
def export_sql(self, expression: exp.Export) -> str:
|
|
213
|
+
if expression.args.get("transfer") != "UNLOAD":
|
|
214
|
+
return super().export_sql(expression)
|
|
215
|
+
source = self.sql(expression, "this")
|
|
216
|
+
direction = "OVERWRITE" if expression.args.get("overwrite") else "INTO"
|
|
217
|
+
destination = self.expressions(expression, key="options", sep=" ", flat=True)
|
|
218
|
+
return f"UNLOAD FROM {source} {direction} {destination}"
|
|
219
|
+
|
|
220
|
+
def propertyeq_sql(self, expression: exp.PropertyEQ) -> str:
|
|
221
|
+
if isinstance(expression.this, exp.Parameter) and expression.this.args.get(
|
|
222
|
+
"native"
|
|
223
|
+
):
|
|
224
|
+
return self.binary(expression, ":=")
|
|
225
|
+
return super().propertyeq_sql(expression)
|
|
226
|
+
|
|
227
|
+
def create_sql(self, expression: exp.Create) -> str:
|
|
228
|
+
sql = super().create_sql(expression)
|
|
229
|
+
if resources := expression.args.get("resources"):
|
|
230
|
+
resources_sql = ", ".join(
|
|
231
|
+
f"{self.sql(resource, 'this')} {self.sql(resource, 'value')}".lstrip()
|
|
232
|
+
for resource in resources
|
|
233
|
+
)
|
|
234
|
+
return f"{sql} USING {resources_sql}"
|
|
235
|
+
return sql
|
|
236
|
+
|
|
237
|
+
def version_sql(self, expression: exp.Version) -> str:
|
|
238
|
+
if expression.args.get("kind") == "BETWEEN":
|
|
239
|
+
start, end = expression.expression.expressions
|
|
240
|
+
return f"{expression.name} BETWEEN {self.sql(start)} AND {self.sql(end)}"
|
|
241
|
+
return super().version_sql(expression)
|
|
242
|
+
|
|
243
|
+
def offset_limit_modifiers(self, expression, fetch, limit):
|
|
244
|
+
modifiers = super().offset_limit_modifiers(expression, fetch, limit)
|
|
245
|
+
if not (zorder := expression.args.get("zorder")):
|
|
246
|
+
return modifiers
|
|
247
|
+
columns = self.expressions(zorder, flat=True)
|
|
248
|
+
columns = f"({columns})" if zorder.args.get("wrapped") else columns
|
|
249
|
+
return (
|
|
250
|
+
self.seg("ZORDER BY") + f" {columns}",
|
|
251
|
+
*modifiers,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
def show_sql(self, expression: exp.Show) -> str:
|
|
255
|
+
kind = self.sql(expression, "this")
|
|
256
|
+
target = self.sql(expression, "target")
|
|
257
|
+
scope = self.sql(expression, "scope")
|
|
258
|
+
like = self.sql(expression, "like")
|
|
259
|
+
property_name = self.sql(expression, "property_name")
|
|
260
|
+
target = f" {target}" if target else ""
|
|
261
|
+
scope = f" FROM {scope}" if scope else ""
|
|
262
|
+
like = f" LIKE {like}" if like else ""
|
|
263
|
+
property_name = f" ({property_name})" if property_name else ""
|
|
264
|
+
return f"SHOW {kind}{target}{scope}{like}{property_name}"
|
|
265
|
+
|
|
266
|
+
def describe_sql(self, expression: exp.Describe) -> str:
|
|
267
|
+
if expression.args.get("kind") == "EXPLAIN":
|
|
268
|
+
return f"EXPLAIN {self.sql(expression, 'this')}"
|
|
269
|
+
if expression.args.get("kind"):
|
|
270
|
+
kind = self.sql(expression, "kind").upper()
|
|
271
|
+
return (
|
|
272
|
+
super()
|
|
273
|
+
.describe_sql(expression)
|
|
274
|
+
.replace("DESCRIBE", f"DESCRIBE {kind}", 1)
|
|
275
|
+
)
|
|
276
|
+
return super().describe_sql(expression)
|
|
277
|
+
|
|
278
|
+
def parameter_sql(self, expression: exp.Parameter) -> str:
|
|
279
|
+
if expression.args.get("native"):
|
|
280
|
+
return f"@{self.sql(expression, 'this')}"
|
|
281
|
+
return super().parameter_sql(expression)
|
|
282
|
+
|
|
283
|
+
def struct_sql(self, expression: exp.Struct) -> str:
|
|
284
|
+
values = [
|
|
285
|
+
f"{self.sql(value, 'expression')} AS {self.sql(value, 'this')}"
|
|
286
|
+
if isinstance(value, exp.PropertyEQ)
|
|
287
|
+
else self.sql(value)
|
|
288
|
+
for value in expression.expressions
|
|
289
|
+
]
|
|
290
|
+
return self.func("STRUCT", *values)
|
|
291
|
+
|
|
292
|
+
def update_sql(self, expression: exp.Update) -> str:
|
|
293
|
+
if expression.args.get("evolve"):
|
|
294
|
+
target = self.sql(expression, "this")
|
|
295
|
+
assignments = self.expressions(expression, flat=True)
|
|
296
|
+
source = self.sql(expression, "from_")
|
|
297
|
+
where = self.sql(expression, "where")
|
|
298
|
+
return self.prepend_ctes(
|
|
299
|
+
expression,
|
|
300
|
+
f"UPDATE {target} EVOLVE COLUMNS SET {assignments}{source}{where}",
|
|
301
|
+
)
|
|
302
|
+
return super().update_sql(expression)
|
|
303
|
+
|
|
304
|
+
def truncatetable_sql(self, expression: exp.TruncateTable) -> str:
|
|
305
|
+
sql = super().truncatetable_sql(expression)
|
|
306
|
+
partitions = self.expressions(expression, key="partitions", flat=True)
|
|
307
|
+
columns = self.expressions(expression, key="columns", flat=True)
|
|
308
|
+
if partitions:
|
|
309
|
+
sql += f" {partitions}"
|
|
310
|
+
if columns:
|
|
311
|
+
sql += f" COLUMNS ({columns})"
|
|
312
|
+
return sql
|
|
313
|
+
|
|
314
|
+
def addpartition_sql(self, expression: exp.AddPartition) -> str:
|
|
315
|
+
sql = super().addpartition_sql(expression)
|
|
316
|
+
for extra in expression.args.get("additional_partitions") or []:
|
|
317
|
+
location = self.sql(extra, "location")
|
|
318
|
+
sql += f" {self.sql(extra, 'this')}"
|
|
319
|
+
if location:
|
|
320
|
+
sql += f" {location}"
|
|
321
|
+
return sql
|
|
322
|
+
|
|
323
|
+
def insert_sql(self, expression: exp.Insert) -> str:
|
|
324
|
+
if expression.args.get("exists") and expression.args.get("overwrite"):
|
|
325
|
+
hint = self.sql(expression, "hint")
|
|
326
|
+
target = self.sql(expression, "this")
|
|
327
|
+
stored = self.sql(expression, "stored")
|
|
328
|
+
stored = f" {stored}" if stored else ""
|
|
329
|
+
kind = "" if isinstance(expression.this, exp.Directory) else " TABLE"
|
|
330
|
+
body = self.sql(expression, "expression")
|
|
331
|
+
return self.prepend_ctes(
|
|
332
|
+
expression,
|
|
333
|
+
f"INSERT{hint} OVERWRITE{kind} {target}{stored} IF NOT EXISTS{self.sep()}{body}",
|
|
334
|
+
)
|
|
335
|
+
return super().insert_sql(expression)
|
|
336
|
+
|
|
337
|
+
def multitableinserts_sql(self, expression: exp.MultitableInserts) -> str:
|
|
338
|
+
if expression.args.get("kind") == "FROM":
|
|
339
|
+
source = self.sql(expression, "source")
|
|
340
|
+
branches = self.expressions(expression, sep=self.sep())
|
|
341
|
+
return self.prepend_ctes(expression, f"{source}{self.sep()}{branches}")
|
|
342
|
+
return super().multitableinserts_sql(expression)
|
|
343
|
+
|
|
344
|
+
def clusteredbyproperty_sql(self, expression: exp.ClusteredByProperty) -> str:
|
|
345
|
+
kind = "RANGE CLUSTERED BY" if expression.args.get("range") else "CLUSTERED BY"
|
|
346
|
+
auto = " AUTO" if expression.args.get("auto") else ""
|
|
347
|
+
columns = self.expressions(expression, flat=True)
|
|
348
|
+
sorted_by = self.expressions(expression, key="sorted_by", flat=True)
|
|
349
|
+
buckets = self.sql(expression, "buckets")
|
|
350
|
+
columns = f" ({columns})" if columns else ""
|
|
351
|
+
sorted_by = f" SORTED BY ({sorted_by})" if sorted_by else ""
|
|
352
|
+
buckets = f" INTO {buckets} BUCKETS" if buckets else ""
|
|
353
|
+
return f"{kind}{auto}{columns}{sorted_by}{buckets}"
|
|
354
|
+
|
|
355
|
+
def datatype_sql(self, expression: exp.DataType) -> str:
|
|
356
|
+
# VARCHAR and CHAR map to STRING in MaxCompute, with no length parameters
|
|
357
|
+
if expression.this in (exp.DType.VARCHAR, exp.DType.CHAR):
|
|
358
|
+
return self.TYPE_MAPPING.get(
|
|
359
|
+
expression.this, super().datatype_sql(expression)
|
|
360
|
+
)
|
|
361
|
+
return super().datatype_sql(expression)
|
|
362
|
+
|
|
363
|
+
def properties_sql(self, expression: exp.Properties) -> str:
|
|
364
|
+
# Var-keyed exp.Property instances (e.g. LIFECYCLE 30) render as bare
|
|
365
|
+
# KEY value after the schema. String-keyed ones stay in TBLPROPERTIES.
|
|
366
|
+
var_keyed = [
|
|
367
|
+
p
|
|
368
|
+
for p in expression.expressions
|
|
369
|
+
if type(p) is exp.Property and isinstance(p.this, exp.Var)
|
|
370
|
+
]
|
|
371
|
+
other = [p for p in expression.expressions if p not in var_keyed]
|
|
372
|
+
|
|
373
|
+
other_node = exp.Properties(expressions=other)
|
|
374
|
+
other_node.parent = expression.parent
|
|
375
|
+
base_sql = super().properties_sql(other_node) if other else ""
|
|
376
|
+
|
|
377
|
+
bare_sql = " ".join(f"{p.name} {self.sql(p, 'value')}" for p in var_keyed)
|
|
378
|
+
|
|
379
|
+
if base_sql and bare_sql:
|
|
380
|
+
return f"{base_sql} {bare_sql}"
|
|
381
|
+
return base_sql or bare_sql
|