-
Notifications
You must be signed in to change notification settings - Fork 28.1k
/
types.py
361 lines (335 loc) · 13.1 KB
/
types.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pyspark.sql.connect.utils import check_dependencies
check_dependencies(__name__, __file__)
import json
import pyarrow as pa
from typing import Optional
from pyspark.sql.types import (
DataType,
ByteType,
ShortType,
IntegerType,
FloatType,
DateType,
TimestampType,
TimestampNTZType,
DayTimeIntervalType,
MapType,
StringType,
CharType,
VarcharType,
StructType,
StructField,
ArrayType,
DoubleType,
LongType,
DecimalType,
BinaryType,
BooleanType,
NullType,
UserDefinedType,
)
import pyspark.sql.connect.proto as pb2
from pyspark.sql.utils import is_remote
JVM_BYTE_MIN: int = -(1 << 7)
JVM_BYTE_MAX: int = (1 << 7) - 1
JVM_SHORT_MIN: int = -(1 << 15)
JVM_SHORT_MAX: int = (1 << 15) - 1
JVM_INT_MIN: int = -(1 << 31)
JVM_INT_MAX: int = (1 << 31) - 1
JVM_LONG_MIN: int = -(1 << 63)
JVM_LONG_MAX: int = (1 << 63) - 1
def pyspark_types_to_proto_types(data_type: DataType) -> pb2.DataType:
ret = pb2.DataType()
if isinstance(data_type, NullType):
ret.null.CopyFrom(pb2.DataType.NULL())
elif isinstance(data_type, StringType):
ret.string.CopyFrom(pb2.DataType.String())
elif isinstance(data_type, BooleanType):
ret.boolean.CopyFrom(pb2.DataType.Boolean())
elif isinstance(data_type, BinaryType):
ret.binary.CopyFrom(pb2.DataType.Binary())
elif isinstance(data_type, ByteType):
ret.byte.CopyFrom(pb2.DataType.Byte())
elif isinstance(data_type, ShortType):
ret.short.CopyFrom(pb2.DataType.Short())
elif isinstance(data_type, IntegerType):
ret.integer.CopyFrom(pb2.DataType.Integer())
elif isinstance(data_type, LongType):
ret.long.CopyFrom(pb2.DataType.Long())
elif isinstance(data_type, FloatType):
ret.float.CopyFrom(pb2.DataType.Float())
elif isinstance(data_type, DoubleType):
ret.double.CopyFrom(pb2.DataType.Double())
elif isinstance(data_type, DecimalType):
ret.decimal.scale = data_type.scale
ret.decimal.precision = data_type.precision
elif isinstance(data_type, DateType):
ret.date.CopyFrom(pb2.DataType.Date())
elif isinstance(data_type, TimestampType):
ret.timestamp.CopyFrom(pb2.DataType.Timestamp())
elif isinstance(data_type, TimestampNTZType):
ret.timestamp_ntz.CopyFrom(pb2.DataType.TimestampNTZ())
elif isinstance(data_type, DayTimeIntervalType):
ret.day_time_interval.start_field = data_type.startField
ret.day_time_interval.end_field = data_type.endField
elif isinstance(data_type, StructType):
for field in data_type.fields:
struct_field = pb2.DataType.StructField()
struct_field.name = field.name
struct_field.data_type.CopyFrom(pyspark_types_to_proto_types(field.dataType))
struct_field.nullable = field.nullable
if field.metadata is not None and len(field.metadata) > 0:
struct_field.metadata = json.dumps(field.metadata)
ret.struct.fields.append(struct_field)
elif isinstance(data_type, MapType):
ret.map.key_type.CopyFrom(pyspark_types_to_proto_types(data_type.keyType))
ret.map.value_type.CopyFrom(pyspark_types_to_proto_types(data_type.valueType))
ret.map.value_contains_null = data_type.valueContainsNull
elif isinstance(data_type, ArrayType):
ret.array.element_type.CopyFrom(pyspark_types_to_proto_types(data_type.elementType))
ret.array.contains_null = data_type.containsNull
elif isinstance(data_type, UserDefinedType):
json_value = data_type.jsonValue()
ret.udt.type = "udt"
if "class" in json_value:
# Scala/Java UDT
ret.udt.jvm_class = json_value["class"]
else:
# Python UDT
ret.udt.serialized_python_class = json_value["serializedClass"]
ret.udt.python_class = json_value["pyClass"]
ret.udt.sql_type.CopyFrom(pyspark_types_to_proto_types(data_type.sqlType()))
else:
raise Exception(f"Unsupported data type {data_type}")
return ret
def proto_schema_to_pyspark_data_type(schema: pb2.DataType) -> DataType:
if schema.HasField("null"):
return NullType()
elif schema.HasField("boolean"):
return BooleanType()
elif schema.HasField("binary"):
return BinaryType()
elif schema.HasField("byte"):
return ByteType()
elif schema.HasField("short"):
return ShortType()
elif schema.HasField("integer"):
return IntegerType()
elif schema.HasField("long"):
return LongType()
elif schema.HasField("float"):
return FloatType()
elif schema.HasField("double"):
return DoubleType()
elif schema.HasField("decimal"):
p = schema.decimal.precision if schema.decimal.HasField("precision") else 10
s = schema.decimal.scale if schema.decimal.HasField("scale") else 0
return DecimalType(precision=p, scale=s)
elif schema.HasField("string"):
return StringType()
elif schema.HasField("char"):
return CharType(schema.char.length)
elif schema.HasField("var_char"):
return VarcharType(schema.var_char.length)
elif schema.HasField("date"):
return DateType()
elif schema.HasField("timestamp"):
return TimestampType()
elif schema.HasField("timestamp_ntz"):
return TimestampNTZType()
elif schema.HasField("day_time_interval"):
start: Optional[int] = (
schema.day_time_interval.start_field
if schema.day_time_interval.HasField("start_field")
else None
)
end: Optional[int] = (
schema.day_time_interval.end_field
if schema.day_time_interval.HasField("end_field")
else None
)
return DayTimeIntervalType(startField=start, endField=end)
elif schema.HasField("array"):
return ArrayType(
proto_schema_to_pyspark_data_type(schema.array.element_type),
schema.array.contains_null,
)
elif schema.HasField("struct"):
fields = []
for f in schema.struct.fields:
if f.HasField("metadata"):
metadata = json.loads(f.metadata)
else:
metadata = None
fields.append(
StructField(
f.name, proto_schema_to_pyspark_data_type(f.data_type), f.nullable, metadata
)
)
return StructType(fields)
elif schema.HasField("map"):
return MapType(
proto_schema_to_pyspark_data_type(schema.map.key_type),
proto_schema_to_pyspark_data_type(schema.map.value_type),
schema.map.value_contains_null,
)
elif schema.HasField("udt"):
assert schema.udt.type == "udt"
json_value = {}
if schema.udt.HasField("python_class"):
json_value["pyClass"] = schema.udt.python_class
if schema.udt.HasField("serialized_python_class"):
json_value["serializedClass"] = schema.udt.serialized_python_class
return UserDefinedType.fromJson(json_value)
else:
raise Exception(f"Unsupported data type {schema}")
def to_arrow_type(dt: DataType) -> "pa.DataType":
"""
Convert Spark data type to pyarrow type.
This function refers to 'pyspark.sql.pandas.types.to_arrow_type' but relax the restriction,
e.g. it supports nested StructType.
"""
if type(dt) == BooleanType:
arrow_type = pa.bool_()
elif type(dt) == ByteType:
arrow_type = pa.int8()
elif type(dt) == ShortType:
arrow_type = pa.int16()
elif type(dt) == IntegerType:
arrow_type = pa.int32()
elif type(dt) == LongType:
arrow_type = pa.int64()
elif type(dt) == FloatType:
arrow_type = pa.float32()
elif type(dt) == DoubleType:
arrow_type = pa.float64()
elif type(dt) == DecimalType:
arrow_type = pa.decimal128(dt.precision, dt.scale)
elif type(dt) == StringType:
arrow_type = pa.string()
elif type(dt) == BinaryType:
arrow_type = pa.binary()
elif type(dt) == DateType:
arrow_type = pa.date32()
elif type(dt) == TimestampType:
# Timestamps should be in UTC, JVM Arrow timestamps require a timezone to be read
arrow_type = pa.timestamp("us", tz="UTC")
elif type(dt) == TimestampNTZType:
arrow_type = pa.timestamp("us", tz=None)
elif type(dt) == DayTimeIntervalType:
arrow_type = pa.duration("us")
elif type(dt) == ArrayType:
arrow_type = pa.list_(to_arrow_type(dt.elementType))
elif type(dt) == MapType:
arrow_type = pa.map_(to_arrow_type(dt.keyType), to_arrow_type(dt.valueType))
elif type(dt) == StructType:
fields = [
pa.field(field.name, to_arrow_type(field.dataType), nullable=field.nullable)
for field in dt
]
arrow_type = pa.struct(fields)
elif type(dt) == NullType:
arrow_type = pa.null()
else:
raise TypeError("Unsupported type in conversion to Arrow: " + str(dt))
return arrow_type
def to_arrow_schema(schema: StructType) -> "pa.Schema":
"""Convert a schema from Spark to Arrow"""
fields = [
pa.field(field.name, to_arrow_type(field.dataType), nullable=field.nullable)
for field in schema
]
return pa.schema(fields)
def from_arrow_type(at: "pa.DataType", prefer_timestamp_ntz: bool = False) -> DataType:
"""Convert pyarrow type to Spark data type.
This function refers to 'pyspark.sql.pandas.types.from_arrow_type' but relax the restriction,
e.g. it supports nested StructType, Array of TimestampType. However, Arrow DictionaryType is
not allowed.
"""
import pyarrow.types as types
spark_type: DataType
if types.is_boolean(at):
spark_type = BooleanType()
elif types.is_int8(at):
spark_type = ByteType()
elif types.is_int16(at):
spark_type = ShortType()
elif types.is_int32(at):
spark_type = IntegerType()
elif types.is_int64(at):
spark_type = LongType()
elif types.is_float32(at):
spark_type = FloatType()
elif types.is_float64(at):
spark_type = DoubleType()
elif types.is_decimal(at):
spark_type = DecimalType(precision=at.precision, scale=at.scale)
elif types.is_string(at):
spark_type = StringType()
elif types.is_binary(at):
spark_type = BinaryType()
elif types.is_date32(at):
spark_type = DateType()
elif types.is_timestamp(at) and prefer_timestamp_ntz and at.tz is None:
spark_type = TimestampNTZType()
elif types.is_timestamp(at):
spark_type = TimestampType()
elif types.is_duration(at):
spark_type = DayTimeIntervalType()
elif types.is_list(at):
spark_type = ArrayType(from_arrow_type(at.value_type))
elif types.is_map(at):
spark_type = MapType(from_arrow_type(at.key_type), from_arrow_type(at.item_type))
elif types.is_struct(at):
return StructType(
[
StructField(field.name, from_arrow_type(field.type), nullable=field.nullable)
for field in at
]
)
elif types.is_null(at):
spark_type = NullType()
else:
raise TypeError("Unsupported type in conversion from Arrow: " + str(at))
return spark_type
def from_arrow_schema(arrow_schema: "pa.Schema") -> StructType:
"""Convert schema from Arrow to Spark."""
return StructType(
[
StructField(field.name, from_arrow_type(field.type), nullable=field.nullable)
for field in arrow_schema
]
)
def parse_data_type(data_type: str) -> DataType:
# Currently we don't have a way to have a current Spark session in Spark Connect, and
# pyspark.sql.SparkSession has a centralized logic to control the session creation.
# So uses pyspark.sql.SparkSession for now. Should replace this to using the current
# Spark session for Spark Connect in the future.
from pyspark.sql import SparkSession as PySparkSession
assert is_remote()
return_type_schema = (
PySparkSession.builder.getOrCreate().createDataFrame(data=[], schema=data_type).schema
)
with_col_name = " " in data_type.strip()
if len(return_type_schema.fields) == 1 and not with_col_name:
# To match pyspark.sql.types._parse_datatype_string
return_type = return_type_schema.fields[0].dataType
else:
return_type = return_type_schema
return return_type