-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtypes.py
More file actions
358 lines (274 loc) · 10.1 KB
/
types.py
File metadata and controls
358 lines (274 loc) · 10.1 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
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
# Copyright 2021-present MongoDB, Inc.
#
# Licensed 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.
import enum
from datetime import datetime
import bson
import numpy as np
import pyarrow as pa
try:
import polars as pl
from pymongoarrow.polars_types import PolarsBinary, PolarsCode, PolarsDecimal128, PolarsObjectId
except ImportError:
pl = None
import pyarrow.types as _atypes
from bson import Binary, Code, Decimal128, Int64, ObjectId
from pyarrow import DataType as _ArrowDataType
from pyarrow import (
ExtensionScalar,
ExtensionType,
binary,
bool_,
float64,
int64,
list_,
null,
string,
struct,
timestamp,
)
from pymongoarrow.pandas_types import (
PandasBinary,
PandasCode,
PandasDecimal128,
PandasObjectId,
)
class _BsonArrowTypes(enum.Enum):
datetime = ord(bson.BSONDAT)
double = ord(bson.BSONNUM)
int32 = ord(bson.BSONINT)
int64 = ord(bson.BSONLON)
objectid = ord(bson.BSONOID)
string = ord(bson.BSONSTR)
bool = ord(bson.BSONBOO)
decimal128 = ord(bson.BSONDEC)
document = ord(bson.BSONOBJ)
array = ord(bson.BSONARR)
binary = ord(bson.BSONBIN)
code = ord(bson.BSONCOD)
# Keep in sync with constants in lib.pyx
date32 = 100
date64 = 101
null = 102
# Custom Extension Types.
# See https://arrow.apache.org/docs/python/extending_types.html#defining-extension-types-user-defined-types
# for details.
class BSONExtensionScalar(ExtensionScalar):
def as_py(self, *args, **kwargs):
if self.value is None:
return None
return self._bson_class(self.value.as_py(*args, **kwargs))
class ObjectIdScalar(BSONExtensionScalar):
_bson_class = ObjectId
class ObjectIdType(ExtensionType):
_type_marker = _BsonArrowTypes.objectid
def __init__(self):
super().__init__(binary(12), "pymongoarrow.objectid")
def __reduce__(self):
return ObjectIdType, ()
def __arrow_ext_scalar_class__(self):
return ObjectIdScalar
def to_pandas_dtype(self):
return PandasObjectId()
def __arrow_ext_serialize__(self):
return b""
@classmethod
def __arrow_ext_deserialize__(self, storage_type, serialized):
return ObjectIdType()
class Decimal128Scalar(ExtensionScalar):
def as_py(self, *args, **kwargs):
if self.value is None:
return None
return Decimal128.from_bid(self.value.as_py(*args, **kwargs))
class Decimal128Type(ExtensionType):
_type_marker = _BsonArrowTypes.decimal128
def __init__(self):
super().__init__(binary(16), "pymongoarrow.decimal128")
def __reduce__(self):
return Decimal128Type, ()
def __arrow_ext_scalar_class__(self):
return Decimal128Scalar
def to_pandas_dtype(self):
return PandasDecimal128()
def __arrow_ext_serialize__(self):
return b""
@classmethod
def __arrow_ext_deserialize__(self, storage_type, serialized):
return Decimal128Type()
class BinaryScalar(ExtensionScalar):
def as_py(self, *args, **kwargs):
value = self.value
if value is None:
return None
return Binary(self.value.as_py(*args, **kwargs), self.type.subtype)
class BinaryType(ExtensionType):
_type_marker = _BsonArrowTypes.binary
def __init__(self, subtype):
self._subtype = subtype
super().__init__(binary(), "pymongoarrow.binary")
@property
def subtype(self):
return self._subtype
def __reduce__(self):
return BinaryType, (self._subtype,)
def __arrow_ext_scalar_class__(self):
return BinaryScalar
def to_pandas_dtype(self):
return PandasBinary(self.subtype)
def __arrow_ext_serialize__(self):
if isinstance(self.subtype, bool):
# Previously serialized "True"/"False". Going forward, normalize to 1/0.
subtype_str = "1" if self.subtype else "0"
else:
subtype_str = str(self.subtype)
return f"subtype={subtype_str}".encode()
@classmethod
def __arrow_ext_deserialize__(cls, storage_type, serialized):
serialized = serialized.decode()
assert serialized.startswith("subtype=") # noqa: S101
subtype = int(serialized.split("=")[1])
return BinaryType(subtype)
class CodeScalar(BSONExtensionScalar):
_bson_class = Code
class CodeType(ExtensionType):
_type_marker = _BsonArrowTypes.code
def __init__(self):
super().__init__(string(), "pymongoarrow.code")
def __reduce__(self):
return CodeType, ()
def __arrow_ext_scalar_class__(self):
return CodeScalar
def to_pandas_dtype(self):
return PandasCode()
def __arrow_ext_serialize__(self):
return b""
@classmethod
def __arrow_ext_deserialize__(self, storage_type, serialized):
return CodeType()
# Register all of the extension types.
for dtype in [ObjectIdType, CodeType, Decimal128Type]:
pa.register_extension_type(dtype())
pa.register_extension_type(BinaryType(0))
if pl and hasattr(pl, "register_extension_type"):
pl.register_extension_type("pymongoarrow.objectid", PolarsObjectId)
pl.register_extension_type("pymongoarrow.code", PolarsCode)
pl.register_extension_type("pymongoarrow.decimal128", PolarsDecimal128)
pl.register_extension_type("pymongoarrow.binary", PolarsBinary)
# Internal Type Handling.
def _is_objectid(obj):
type_marker = getattr(obj, "_type_marker", "")
return type_marker == ObjectIdType._type_marker
def _is_decimal128(obj):
type_marker = getattr(obj, "_type_marker", "")
return type_marker == Decimal128Type._type_marker
def _is_binary(obj):
type_marker = getattr(obj, "_type_marker", "")
return type_marker == BinaryType._type_marker
def _is_code(obj):
type_marker = getattr(obj, "_type_marker", "")
return type_marker == CodeType._type_marker
_TYPE_NORMALIZER_FACTORY = {
Int64: lambda _: int64(),
float: lambda _: float64(),
int: lambda _: int64(),
# Note: we cannot infer a timezone form a raw datetime class,
# if a timezone is preferred then a timestamp with tz information
# must be used directly.
datetime: lambda _: timestamp("ms"),
ObjectId: lambda _: ObjectIdType(),
Decimal128: lambda _: Decimal128Type(),
str: lambda _: string(),
bool: lambda _: bool_(),
Binary: BinaryType,
Code: lambda _: CodeType(),
}
_TYPE_CHECKER_TO_NUMPY = {
_atypes.is_int32: np.int32,
_atypes.is_int64: np.int64,
_atypes.is_float64: np.float64,
_atypes.is_timestamp: "datetime64[ms]",
_is_objectid: object,
_atypes.is_string: np.str_,
_atypes.is_boolean: np.bool_,
}
def get_numpy_type(type):
for checker, comp_type in _TYPE_CHECKER_TO_NUMPY.items():
if checker(type):
return comp_type
return None
_TYPE_CHECKER_TO_INTERNAL_TYPE = {
_atypes.is_int32: _BsonArrowTypes.int32.value,
_atypes.is_int64: _BsonArrowTypes.int64.value,
_atypes.is_float64: _BsonArrowTypes.double.value,
_atypes.is_timestamp: _BsonArrowTypes.datetime.value,
_atypes.is_null: _BsonArrowTypes.null.value,
_is_objectid: _BsonArrowTypes.objectid.value,
_is_decimal128: _BsonArrowTypes.decimal128.value,
_is_binary: _BsonArrowTypes.binary.value,
_is_code: _BsonArrowTypes.code.value,
_atypes.is_string: _BsonArrowTypes.string.value,
_atypes.is_boolean: _BsonArrowTypes.bool.value,
_atypes.is_struct: _BsonArrowTypes.document.value,
_atypes.is_list: _BsonArrowTypes.array.value,
_atypes.is_date32: _BsonArrowTypes.date32.value,
_atypes.is_date64: _BsonArrowTypes.date64.value,
_atypes.is_large_string: _BsonArrowTypes.string.value,
_atypes.is_large_list: _BsonArrowTypes.array.value,
_atypes.is_decimal128: _BsonArrowTypes.decimal128.value,
}
def _is_typeid_supported(typeid):
return typeid in _TYPE_NORMALIZER_FACTORY or typeid is None
def _normalize_typeid(typeid, field_name):
if isinstance(typeid, _ArrowDataType):
return typeid
if isinstance(typeid, dict):
fields = []
for sub_field_name, sub_typeid in typeid.items():
fields.append((sub_field_name, _normalize_typeid(sub_typeid, sub_field_name)))
return struct(fields)
if isinstance(typeid, list):
if len(typeid) != 1:
msg = f"list field in schema must contain exactly one element, not {len(typeid)}"
raise ValueError(msg)
return list_(_normalize_typeid(typeid[0], "0"))
if _is_typeid_supported(typeid):
if typeid is None: # noqa: SIM108
normalizer = lambda _: null() # noqa: E731
else:
normalizer = _TYPE_NORMALIZER_FACTORY[typeid]
return normalizer(typeid)
msg = f"Unsupported type identifier {typeid} for field {field_name}"
raise ValueError(msg)
def _get_internal_typemap(typemap):
internal_typemap = {}
for fname, ftype in typemap.items():
for checker, internal_id in _TYPE_CHECKER_TO_INTERNAL_TYPE.items():
if checker(ftype):
internal_typemap[fname] = (internal_id, ftype)
break
if fname not in internal_typemap:
msg = f'Unsupported data type in schema for field "{fname}" of type "{ftype}"'
raise ValueError(msg)
return internal_typemap
def _in_type_map(t):
if isinstance(t, np.dtype):
try:
t = pa.from_numpy_dtype(t)
except pa.lib.ArrowNotImplementedError:
return False
return any(checker(t) for checker in _TYPE_CHECKER_TO_INTERNAL_TYPE)
def _validate_schema(schema):
for i in schema:
if not _in_type_map(i):
msg = f'Unsupported data type "{i}" in schema'
raise ValueError(msg)