Repository navigation
Expand file tree
/
Copy pathspark2.py
More file actions
275 lines (229 loc) · 10.6 KB
/
Copy pathspark2.py
File metadata and controls
275 lines (229 loc) · 10.6 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from __future__ import annotations
from sqlglot import exp, transforms
from sqlglot.dialects.dialect import (
bracket_to_element_at_sql,
is_parse_json,
rename_func,
unit_to_str,
)
from sqlglot.generators.hive import HIVE_DATE_FORMAT, HiveGenerator, HIVE_TS_OR_DS_EXPRESSIONS
from sqlglot.transforms import (
preprocess,
remove_unique_constraints,
ctas_with_tmp_tables_to_create_tmp_view,
move_schema_columns_to_partitioned_by,
)
def _json_format_sql(self: Spark2Generator, expression: exp.JSONFormat) -> str:
this = expression.this
if is_parse_json(this):
if this.this.is_string:
# Since FROM_JSON requires a nested type, we always wrap the json string with
# an array to ensure that "naked" strings like "'a'" will be handled correctly
wrapped_json = exp.Literal.string(f"[{this.this.name}]")
from_json = self.func(
"FROM_JSON", wrapped_json, self.func("SCHEMA_OF_JSON", wrapped_json)
)
to_json = self.func("TO_JSON", from_json)
# This strips the [, ] delimiters of the dummy array printed by TO_JSON
return self.func("REGEXP_EXTRACT", to_json, "'^.(.*).$'", "1")
return self.sql(this)
return self.func("TO_JSON", this, expression.args.get("options"))
def _map_sql(self: Spark2Generator, expression: exp.Map) -> str:
keys = expression.args.get("keys")
values = expression.args.get("values")
if not keys or not values:
return self.func("MAP")
return self.func("MAP_FROM_ARRAYS", keys, values)
def _str_to_date(self: Spark2Generator, expression: exp.StrToDate) -> str:
time_format = self.format_time(expression)
if time_format == HIVE_DATE_FORMAT:
return self.func("TO_DATE", expression.this)
return self.func("TO_DATE", expression.this, time_format)
def _unix_to_time_sql(self: Spark2Generator, expression: exp.UnixToTime) -> str:
scale = expression.args.get("scale")
timestamp = expression.this
if scale is None:
return self.sql(exp.cast(exp.func("from_unixtime", timestamp), exp.DType.TIMESTAMP))
if scale == exp.UnixToTime.SECONDS:
return self.func("TIMESTAMP_SECONDS", timestamp)
if scale == exp.UnixToTime.MILLIS:
return self.func("TIMESTAMP_MILLIS", timestamp)
if scale == exp.UnixToTime.MICROS:
return self.func("TIMESTAMP_MICROS", timestamp)
unix_seconds = exp.Div(this=timestamp, expression=exp.func("POW", 10, scale))
return self.func("TIMESTAMP_SECONDS", unix_seconds)
def _unalias_pivot(expression: exp.Expr) -> exp.Expr:
"""
Spark doesn't allow PIVOT aliases, so we need to remove them and possibly wrap a
pivoted source in a subquery with the same alias to preserve the query's semantics.
Example:
>>> from sqlglot import parse_one
>>> expr = parse_one("SELECT piv.x FROM tbl PIVOT (SUM(a) FOR b IN ('x')) piv")
>>> print(_unalias_pivot(expr).sql(dialect="spark"))
SELECT piv.x FROM (SELECT * FROM tbl PIVOT(SUM(a) FOR b IN ('x'))) AS piv
"""
if isinstance(expression, exp.From) and expression.this.args.get("pivots"):
pivot = expression.this.args["pivots"][0]
if pivot.alias:
alias = pivot.args["alias"].pop()
return exp.From(
this=expression.this.replace(
exp.select("*")
.from_(expression.this.copy(), copy=False)
.subquery(alias=alias, copy=False)
)
)
return expression
def _unqualify_pivot_columns(expression: exp.Expr) -> exp.Expr:
"""
Spark doesn't allow the column referenced in the PIVOT's field to be qualified,
so we need to unqualify it.
Example:
>>> from sqlglot import parse_one
>>> expr = parse_one("SELECT * FROM tbl PIVOT (SUM(tbl.sales) FOR tbl.quarter IN ('Q1', 'Q2'))")
>>> print(_unqualify_pivot_columns(expr).sql(dialect="spark"))
SELECT * FROM tbl PIVOT(SUM(tbl.sales) FOR quarter IN ('Q1', 'Q2'))
"""
if isinstance(expression, exp.Pivot):
expression.set(
"fields", [transforms.unqualify_columns(field) for field in expression.fields]
)
return expression
def temporary_storage_provider(expression: exp.Expr) -> exp.Expr:
# spark2, spark, Databricks require a storage provider for temporary tables
provider = exp.FileFormatProperty(this=exp.Literal.string("parquet"))
expression.args["properties"].append("expressions", provider)
return expression
class Spark2Generator(HiveGenerator):
QUERY_HINTS = True
NVL2_SUPPORTED = True
CAN_IMPLEMENT_ARRAY_ANY = True
ALTER_SET_TYPE = "TYPE"
PARSE_JSON_NAME: str | None = None
PROPERTIES_LOCATION = {
**HiveGenerator.PROPERTIES_LOCATION,
exp.EngineProperty: exp.Properties.Location.UNSUPPORTED,
exp.AutoIncrementProperty: exp.Properties.Location.UNSUPPORTED,
exp.CharacterSetProperty: exp.Properties.Location.UNSUPPORTED,
exp.CollateProperty: exp.Properties.Location.UNSUPPORTED,
}
TS_OR_DS_EXPRESSIONS = (
*HIVE_TS_OR_DS_EXPRESSIONS,
exp.DayOfMonth,
exp.DayOfWeek,
exp.DayOfYear,
exp.WeekOfYear,
)
TRANSFORMS = {
k: v
for k, v in {
**HiveGenerator.TRANSFORMS,
exp.ApproxDistinct: rename_func("APPROX_COUNT_DISTINCT"),
exp.ArraySum: lambda self, e: (
f"AGGREGATE({self.sql(e, 'this')}, 0, (acc, x) -> acc + x, acc -> acc)"
),
exp.ArrayToString: rename_func("ARRAY_JOIN"),
exp.ArraySlice: rename_func("SLICE"),
exp.AtTimeZone: lambda self, e: self.func(
"FROM_UTC_TIMESTAMP", e.this, e.args.get("zone")
),
exp.BitwiseLeftShift: rename_func("SHIFTLEFT"),
exp.BitwiseRightShift: rename_func("SHIFTRIGHT"),
exp.Create: preprocess(
[
remove_unique_constraints,
lambda e: ctas_with_tmp_tables_to_create_tmp_view(
e, temporary_storage_provider
),
move_schema_columns_to_partitioned_by,
]
),
exp.DateFromParts: rename_func("MAKE_DATE"),
exp.DateTrunc: lambda self, e: self.func("TRUNC", e.this, unit_to_str(e)),
exp.DayOfMonth: rename_func("DAYOFMONTH"),
exp.DayOfWeek: rename_func("DAYOFWEEK"),
# (DAY_OF_WEEK(datetime) % 7) + 1 is equivalent to DAYOFWEEK_ISO(datetime)
exp.DayOfWeekIso: lambda self, e: f"(({self.func('DAYOFWEEK', e.this)} % 7) + 1)",
exp.DayOfYear: rename_func("DAYOFYEAR"),
exp.Format: rename_func("FORMAT_STRING"),
exp.From: transforms.preprocess([_unalias_pivot]),
exp.FromTimeZone: lambda self, e: self.func(
"TO_UTC_TIMESTAMP", e.this, e.args.get("zone")
),
exp.JSONFormat: _json_format_sql,
exp.LogicalAnd: rename_func("BOOL_AND"),
exp.LogicalOr: rename_func("BOOL_OR"),
exp.Map: _map_sql,
exp.Pivot: transforms.preprocess([_unqualify_pivot_columns]),
exp.Reduce: rename_func("AGGREGATE"),
exp.RegexpReplace: lambda self, e: self.func(
"REGEXP_REPLACE",
e.this,
e.expression,
e.args["replacement"],
e.args.get("position"),
),
exp.Select: transforms.preprocess(
[
transforms.eliminate_qualify,
transforms.eliminate_distinct_on,
transforms.unnest_to_explode,
transforms.any_to_exists,
]
),
exp.SHA2Digest: lambda self, e: self.func(
"SHA2", e.this, e.args.get("length") or exp.Literal.number(256)
),
exp.StrToDate: _str_to_date,
exp.StrToTime: lambda self, e: self.func("TO_TIMESTAMP", e.this, self.format_time(e)),
exp.TimestampTrunc: lambda self, e: self.func("DATE_TRUNC", unit_to_str(e), e.this),
exp.UnixToTime: _unix_to_time_sql,
exp.VariancePop: rename_func("VAR_POP"),
exp.WeekOfYear: rename_func("WEEKOFYEAR"),
exp.WithinGroup: transforms.preprocess(
[transforms.remove_within_group_for_percentiles]
),
exp.ArraySort: None,
exp.ILike: None,
exp.Left: None,
exp.MonthsBetween: None,
exp.Right: None,
}.items()
if v is not None
}
WRAP_DERIVED_VALUES = False
CREATE_FUNCTION_RETURN_AS = False
def struct_sql(self, expression: exp.Struct) -> str:
from sqlglot.generator import Generator
return Generator.struct_sql(self, expression)
def cast_sql(self, expression: exp.Cast, safe_prefix: str | None = None) -> str:
arg = expression.this
is_json_extract = isinstance(
arg, (exp.JSONExtract, exp.JSONExtractScalar)
) and not arg.args.get("variant_extract")
# We can't use a non-nested type (eg. STRING) as a schema
if expression.to.args.get("nested") and (is_parse_json(arg) or is_json_extract):
schema = f"'{self.sql(expression, 'to')}'"
return self.func("FROM_JSON", arg if is_json_extract else arg.this, schema)
if is_parse_json(expression):
return self.func("TO_JSON", arg)
return super(HiveGenerator, self).cast_sql(expression, safe_prefix=safe_prefix)
def fileformatproperty_sql(self, expression: exp.FileFormatProperty) -> str:
if expression.args.get("hive_format"):
return super().fileformatproperty_sql(expression)
return f"USING {expression.name.upper()}"
def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
this = self.sql(expression, "this")
new_name = self.sql(expression, "rename_to") or this
comment = self.sql(expression, "comment")
if new_name == this:
if comment:
return f"ALTER COLUMN {this} COMMENT {comment}"
return super(HiveGenerator, self).altercolumn_sql(expression)
return f"RENAME COLUMN {this} TO {new_name}"
def renamecolumn_sql(self, expression: exp.RenameColumn) -> str:
return super(HiveGenerator, self).renamecolumn_sql(expression)
def bracket_sql(self, expression: exp.Bracket) -> str:
if expression.args.get("safe") is False:
return bracket_to_element_at_sql(self, expression)
return super().bracket_sql(expression)