21
21
from pyarrow import DataType as _ArrowDataType
22
22
from pyarrow import (
23
23
ExtensionScalar ,
24
- PyExtensionType ,
24
+ ExtensionType ,
25
25
binary ,
26
26
bool_ ,
27
27
float64 ,
@@ -71,11 +71,11 @@ class ObjectIdScalar(BSONExtensionScalar):
71
71
_bson_class = ObjectId
72
72
73
73
74
- class ObjectIdType (PyExtensionType ):
74
+ class ObjectIdType (ExtensionType ):
75
75
_type_marker = _BsonArrowTypes .objectid
76
76
77
77
def __init__ (self ):
78
- super ().__init__ (binary (12 ))
78
+ super ().__init__ (binary (12 ), "pymongoarrow.objectid" )
79
79
80
80
def __reduce__ (self ):
81
81
return ObjectIdType , ()
@@ -86,6 +86,13 @@ def __arrow_ext_scalar_class__(self):
86
86
def to_pandas_dtype (self ):
87
87
return PandasObjectId ()
88
88
89
+ def __arrow_ext_serialize__ (self ):
90
+ return b""
91
+
92
+ @classmethod
93
+ def __arrow_ext_deserialize__ (self , storage_type , serialized ):
94
+ return ObjectIdType ()
95
+
89
96
90
97
class Decimal128Scalar (ExtensionScalar ):
91
98
def as_py (self ):
@@ -94,11 +101,11 @@ def as_py(self):
94
101
return Decimal128 .from_bid (self .value .as_py ())
95
102
96
103
97
- class Decimal128Type (PyExtensionType ):
104
+ class Decimal128Type (ExtensionType ):
98
105
_type_marker = _BsonArrowTypes .decimal128
99
106
100
107
def __init__ (self ):
101
- super ().__init__ (binary (16 ))
108
+ super ().__init__ (binary (16 ), "pymongoarrow.decimal128" )
102
109
103
110
def __reduce__ (self ):
104
111
return Decimal128Type , ()
@@ -109,6 +116,13 @@ def __arrow_ext_scalar_class__(self):
109
116
def to_pandas_dtype (self ):
110
117
return PandasDecimal128 ()
111
118
119
+ def __arrow_ext_serialize__ (self ):
120
+ return b""
121
+
122
+ @classmethod
123
+ def __arrow_ext_deserialize__ (self , storage_type , serialized ):
124
+ return Decimal128Type ()
125
+
112
126
113
127
class BinaryScalar (ExtensionScalar ):
114
128
def as_py (self ):
@@ -118,12 +132,12 @@ def as_py(self):
118
132
return Binary (self .value .as_py (), self .type .subtype )
119
133
120
134
121
- class BinaryType (PyExtensionType ):
135
+ class BinaryType (ExtensionType ):
122
136
_type_marker = _BsonArrowTypes .binary
123
137
124
138
def __init__ (self , subtype ):
125
139
self ._subtype = subtype
126
- super ().__init__ (binary ())
140
+ super ().__init__ (binary (), "pymongoarrow.binary" )
127
141
128
142
@property
129
143
def subtype (self ):
@@ -138,16 +152,26 @@ def __arrow_ext_scalar_class__(self):
138
152
def to_pandas_dtype (self ):
139
153
return PandasBinary (self .subtype )
140
154
155
+ def __arrow_ext_serialize__ (self ):
156
+ return f"subtype={ self .subtype } " .encode ()
157
+
158
+ @classmethod
159
+ def __arrow_ext_deserialize__ (cls , storage_type , serialized ):
160
+ serialized = serialized .decode ()
161
+ assert serialized .startswith ("subtype=" ) # noqa: S101
162
+ subtype = int (serialized .split ("=" )[1 ])
163
+ return BinaryType (subtype )
164
+
141
165
142
166
class CodeScalar (BSONExtensionScalar ):
143
167
_bson_class = Code
144
168
145
169
146
- class CodeType (PyExtensionType ):
170
+ class CodeType (ExtensionType ):
147
171
_type_marker = _BsonArrowTypes .code
148
172
149
173
def __init__ (self ):
150
- super ().__init__ (string ())
174
+ super ().__init__ (string (), "pymongoarrow.code" )
151
175
152
176
def __reduce__ (self ):
153
177
return CodeType , ()
@@ -158,6 +182,18 @@ def __arrow_ext_scalar_class__(self):
158
182
def to_pandas_dtype (self ):
159
183
return PandasCode ()
160
184
185
+ def __arrow_ext_serialize__ (self ):
186
+ return b""
187
+
188
+ @classmethod
189
+ def __arrow_ext_deserialize__ (self , storage_type , serialized ):
190
+ return CodeType ()
191
+
192
+
193
+ # Register all of the extension types.
194
+ for dtype in [ObjectIdType , CodeType , Decimal128Type ]:
195
+ pa .register_extension_type (dtype ())
196
+ pa .register_extension_type (BinaryType (0 ))
161
197
162
198
# Internal Type Handling.
163
199
0 commit comments