forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension_type.cc
More file actions
207 lines (179 loc) · 6.49 KB
/
Copy pathextension_type.cc
File metadata and controls
207 lines (179 loc) · 6.49 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
// 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.
#include <memory>
#include <utility>
#include "arrow/python/extension_type.h"
#include "arrow/python/helpers.h"
#include "arrow/python/pyarrow.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/logging.h"
namespace arrow {
using internal::checked_cast;
namespace py {
namespace {
// Serialize a Python ExtensionType instance
Status SerializeExtInstance(PyObject* type_instance, std::string* out) {
OwnedRef res(
cpp_PyObject_CallMethod(type_instance, "__arrow_ext_serialize__", nullptr));
if (!res) {
return ConvertPyError();
}
if (!PyBytes_Check(res.obj())) {
return Status::TypeError(
"__arrow_ext_serialize__ should return bytes object, "
"got ",
internal::PyObject_StdStringRepr(res.obj()));
}
*out = internal::PyBytes_AsStdString(res.obj());
return Status::OK();
}
// Deserialize a Python ExtensionType instance
PyObject* DeserializeExtInstance(PyObject* type_class,
std::shared_ptr<DataType> storage_type,
const std::string& serialized_data) {
OwnedRef storage_ref(wrap_data_type(storage_type));
if (!storage_ref) {
return nullptr;
}
OwnedRef data_ref(PyBytes_FromStringAndSize(
serialized_data.data(), static_cast<Py_ssize_t>(serialized_data.size())));
if (!data_ref) {
return nullptr;
}
return cpp_PyObject_CallMethod(type_class, "__arrow_ext_deserialize__", "OO",
storage_ref.obj(), data_ref.obj());
}
} // namespace
static const char* kExtensionName = "arrow.py_extension_type";
PyExtensionType::PyExtensionType(std::shared_ptr<DataType> storage_type, PyObject* typ,
PyObject* inst)
: ExtensionType(storage_type),
extension_name_(kExtensionName),
type_class_(typ),
type_instance_(inst) {}
PyExtensionType::PyExtensionType(std::shared_ptr<DataType> storage_type,
std::string extension_name, PyObject* typ,
PyObject* inst)
: ExtensionType(storage_type),
extension_name_(std::move(extension_name)),
type_class_(typ),
type_instance_(inst) {}
bool PyExtensionType::ExtensionEquals(const ExtensionType& other) const {
PyAcquireGIL lock;
if (other.extension_name() != extension_name()) {
return false;
}
const auto& other_ext = checked_cast<const PyExtensionType&>(other);
int res = -1;
if (!type_instance_) {
if (other_ext.type_instance_) {
return false;
}
// Compare Python types
res = PyObject_RichCompareBool(type_class_.obj(), other_ext.type_class_.obj(), Py_EQ);
} else {
if (!other_ext.type_instance_) {
return false;
}
// Compare Python instances
OwnedRef left(GetInstance());
OwnedRef right(other_ext.GetInstance());
if (!left || !right) {
goto error;
}
res = PyObject_RichCompareBool(left.obj(), right.obj(), Py_EQ);
}
if (res == -1) {
goto error;
}
return res == 1;
error:
// Cannot propagate error
PyErr_WriteUnraisable(nullptr);
return false;
}
std::shared_ptr<Array> PyExtensionType::MakeArray(std::shared_ptr<ArrayData> data) const {
DCHECK_EQ(data->type->id(), Type::EXTENSION);
return std::make_shared<ExtensionArray>(data);
}
std::string PyExtensionType::Serialize() const {
DCHECK(type_instance_);
return serialized_;
}
Status PyExtensionType::Deserialize(std::shared_ptr<DataType> storage_type,
const std::string& serialized_data,
std::shared_ptr<DataType>* out) const {
PyAcquireGIL lock;
if (import_pyarrow()) {
return ConvertPyError();
}
OwnedRef res(DeserializeExtInstance(type_class_.obj(), storage_type, serialized_data));
if (!res) {
return ConvertPyError();
}
return unwrap_data_type(res.obj(), out);
}
PyObject* PyExtensionType::GetInstance() const {
if (!type_instance_) {
PyErr_SetString(PyExc_TypeError, "Not an instance");
return nullptr;
}
DCHECK(PyWeakref_CheckRef(type_instance_.obj()));
PyObject* inst = PyWeakref_GET_OBJECT(type_instance_.obj());
if (inst != Py_None) {
// Cached instance still alive
Py_INCREF(inst);
return inst;
} else {
// Must reconstruct from serialized form
// XXX cache again?
return DeserializeExtInstance(type_class_.obj(), storage_type_, serialized_);
}
}
Status PyExtensionType::SetInstance(PyObject* inst) const {
// Check we have the right type
PyObject* typ = reinterpret_cast<PyObject*>(Py_TYPE(inst));
if (typ != type_class_.obj()) {
return Status::TypeError("Unexpected Python ExtensionType class ",
internal::PyObject_StdStringRepr(typ), " expected ",
internal::PyObject_StdStringRepr(type_class_.obj()));
}
PyObject* wr = PyWeakref_NewRef(inst, nullptr);
if (wr == NULL) {
return ConvertPyError();
}
type_instance_.reset(wr);
return SerializeExtInstance(inst, &serialized_);
}
Status PyExtensionType::FromClass(const std::shared_ptr<DataType> storage_type,
const std::string extension_name, PyObject* typ,
std::shared_ptr<ExtensionType>* out) {
Py_INCREF(typ);
out->reset(new PyExtensionType(storage_type, std::move(extension_name), typ));
return Status::OK();
}
Status RegisterPyExtensionType(const std::shared_ptr<DataType>& type) {
DCHECK_EQ(type->id(), Type::EXTENSION);
auto ext_type = std::dynamic_pointer_cast<ExtensionType>(type);
return RegisterExtensionType(ext_type);
}
Status UnregisterPyExtensionType(const std::string& type_name) {
return UnregisterExtensionType(type_name);
}
std::string PyExtensionName() { return kExtensionName; }
} // namespace py
} // namespace arrow