forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudf.cc
More file actions
301 lines (273 loc) · 11.5 KB
/
Copy pathudf.cc
File metadata and controls
301 lines (273 loc) · 11.5 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
// 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 "arrow/python/udf.h"
#include "arrow/compute/function.h"
#include "arrow/compute/kernel.h"
#include "arrow/python/common.h"
#include "arrow/util/checked_cast.h"
namespace arrow {
namespace py {
namespace {
struct PythonUdfKernelState : public compute::KernelState {
explicit PythonUdfKernelState(std::shared_ptr<OwnedRefNoGIL> function)
: function(function) {
Py_INCREF(function->obj());
}
// function needs to be destroyed at process exit
// and Python may no longer be initialized.
~PythonUdfKernelState() {
if (_Py_IsFinalizing()) {
function->detach();
}
}
std::shared_ptr<OwnedRefNoGIL> function;
};
struct PythonUdfKernelInit {
explicit PythonUdfKernelInit(std::shared_ptr<OwnedRefNoGIL> function)
: function(function) {
Py_INCREF(function->obj());
}
// function needs to be destroyed at process exit
// and Python may no longer be initialized.
~PythonUdfKernelInit() {
if (_Py_IsFinalizing()) {
function->detach();
}
}
Result<std::unique_ptr<compute::KernelState>> operator()(
compute::KernelContext*, const compute::KernelInitArgs&) {
return std::make_unique<PythonUdfKernelState>(function);
}
std::shared_ptr<OwnedRefNoGIL> function;
};
struct PythonTableUdfKernelInit {
PythonTableUdfKernelInit(std::shared_ptr<OwnedRefNoGIL> function_maker,
UdfWrapperCallback cb)
: function_maker(function_maker), cb(cb) {
Py_INCREF(function_maker->obj());
}
// function needs to be destroyed at process exit
// and Python may no longer be initialized.
~PythonTableUdfKernelInit() {
if (_Py_IsFinalizing()) {
function_maker->detach();
}
}
Result<std::unique_ptr<compute::KernelState>> operator()(
compute::KernelContext* ctx, const compute::KernelInitArgs&) {
ScalarUdfContext scalar_udf_context{ctx->memory_pool(), /*batch_length=*/0};
std::unique_ptr<OwnedRefNoGIL> function;
RETURN_NOT_OK(SafeCallIntoPython([this, &scalar_udf_context, &function] {
OwnedRef empty_tuple(PyTuple_New(0));
function = std::make_unique<OwnedRefNoGIL>(
cb(function_maker->obj(), scalar_udf_context, empty_tuple.obj()));
RETURN_NOT_OK(CheckPyError());
return Status::OK();
}));
if (!PyCallable_Check(function->obj())) {
return Status::TypeError("Expected a callable Python object.");
}
return std::make_unique<PythonUdfKernelState>(
std::move(function));
}
std::shared_ptr<OwnedRefNoGIL> function_maker;
UdfWrapperCallback cb;
};
struct PythonUdf : public PythonUdfKernelState {
PythonUdf(std::shared_ptr<OwnedRefNoGIL> function, UdfWrapperCallback cb,
std::vector<TypeHolder> input_types, compute::OutputType output_type)
: PythonUdfKernelState(function),
cb(cb),
input_types(input_types),
output_type(output_type) {}
UdfWrapperCallback cb;
std::vector<TypeHolder> input_types;
compute::OutputType output_type;
TypeHolder resolved_type;
Result<TypeHolder> ResolveType(compute::KernelContext* ctx,
const std::vector<TypeHolder>& types) {
if (input_types == types) {
if (!resolved_type) {
ARROW_ASSIGN_OR_RAISE(resolved_type, output_type.Resolve(ctx, input_types));
}
return resolved_type;
}
return output_type.Resolve(ctx, types);
}
Status Exec(compute::KernelContext* ctx, const compute::ExecSpan& batch,
compute::ExecResult* out) {
auto state = arrow::internal::checked_cast<PythonUdfKernelState*>(ctx->state());
std::shared_ptr<OwnedRefNoGIL>& function = state->function;
const int num_args = batch.num_values();
ScalarUdfContext scalar_udf_context{ctx->memory_pool(), batch.length};
OwnedRef arg_tuple(PyTuple_New(num_args));
RETURN_NOT_OK(CheckPyError());
for (int arg_id = 0; arg_id < num_args; arg_id++) {
if (batch[arg_id].is_scalar()) {
std::shared_ptr<Scalar> c_data = batch[arg_id].scalar->GetSharedPtr();
PyObject* data = wrap_scalar(c_data);
PyTuple_SetItem(arg_tuple.obj(), arg_id, data);
} else {
std::shared_ptr<Array> c_data = batch[arg_id].array.ToArray();
PyObject* data = wrap_array(c_data);
PyTuple_SetItem(arg_tuple.obj(), arg_id, data);
}
}
OwnedRef result(cb(function->obj(), scalar_udf_context, arg_tuple.obj()));
RETURN_NOT_OK(CheckPyError());
// unwrapping the output for expected output type
if (is_array(result.obj())) {
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Array> val, unwrap_array(result.obj()));
ARROW_ASSIGN_OR_RAISE(TypeHolder type, ResolveType(ctx, batch.GetTypes()));
if (type.type == NULLPTR) {
return Status::TypeError("expected output datatype is null");
}
if (*type.type != *val->type()) {
return Status::TypeError("Expected output datatype ", type.type->ToString(),
", but function returned datatype ",
val->type()->ToString());
}
out->value = std::move(val->data());
return Status::OK();
} else {
return Status::TypeError("Unexpected output type: ", Py_TYPE(result.obj())->tp_name,
" (expected Array)");
}
return Status::OK();
}
};
Status PythonUdfExec(compute::KernelContext* ctx, const compute::ExecSpan& batch,
compute::ExecResult* out) {
auto udf = static_cast<PythonUdf*>(ctx->kernel()->data.get());
return SafeCallIntoPython([&]() -> Status { return udf->Exec(ctx, batch, out); });
}
Status RegisterUdf(PyObject* user_function, compute::KernelInit kernel_init,
UdfWrapperCallback wrapper, const UdfOptions& options,
compute::FunctionRegistry* registry) {
if (!PyCallable_Check(user_function)) {
return Status::TypeError("Expected a callable Python object.");
}
auto scalar_func = std::make_shared<compute::ScalarFunction>(
options.func_name, options.arity, options.func_doc);
Py_INCREF(user_function);
std::vector<compute::InputType> input_types;
for (const auto& in_dtype : options.input_types) {
input_types.emplace_back(in_dtype);
}
compute::OutputType output_type(options.output_type);
auto udf_data = std::make_shared<PythonUdf>(
std::make_shared<OwnedRefNoGIL>(user_function), wrapper,
TypeHolder::FromTypes(options.input_types), options.output_type);
compute::ScalarKernel kernel(
compute::KernelSignature::Make(std::move(input_types), std::move(output_type),
options.arity.is_varargs),
PythonUdfExec, kernel_init);
kernel.data = std::move(udf_data);
kernel.mem_allocation = compute::MemAllocation::NO_PREALLOCATE;
kernel.null_handling = compute::NullHandling::COMPUTED_NO_PREALLOCATE;
RETURN_NOT_OK(scalar_func->AddKernel(std::move(kernel)));
if (registry == NULLPTR) {
registry = compute::GetFunctionRegistry();
}
RETURN_NOT_OK(registry->AddFunction(std::move(scalar_func)));
return Status::OK();
}
} // namespace
Status RegisterScalarFunction(PyObject* user_function, UdfWrapperCallback wrapper,
const UdfOptions& options,
compute::FunctionRegistry* registry) {
return RegisterUdf(
user_function,
PythonUdfKernelInit{std::make_shared<OwnedRefNoGIL>(user_function)}, wrapper,
options, registry);
}
Status RegisterTabularFunction(PyObject* user_function, UdfWrapperCallback wrapper,
const UdfOptions& options,
compute::FunctionRegistry* registry) {
if (options.arity.num_args != 0 || options.arity.is_varargs) {
return Status::NotImplemented("tabular function of non-null arity");
}
if (options.output_type->id() != Type::type::STRUCT) {
return Status::Invalid("tabular function with non-struct output");
}
return RegisterUdf(
user_function,
PythonTableUdfKernelInit{std::make_shared<OwnedRefNoGIL>(user_function), wrapper},
wrapper, options, registry);
}
Result<std::shared_ptr<RecordBatchReader>> CallTabularFunction(
const std::string& func_name, const std::vector<Datum>& args,
compute::FunctionRegistry* registry) {
if (args.size() != 0) {
return Status::NotImplemented("non-empty arguments to tabular function");
}
if (registry == NULLPTR) {
registry = compute::GetFunctionRegistry();
}
ARROW_ASSIGN_OR_RAISE(auto func, registry->GetFunction(func_name));
if (func->kind() != compute::Function::SCALAR) {
return Status::Invalid("tabular function of non-scalar kind");
}
auto arity = func->arity();
if (arity.num_args != 0 || arity.is_varargs) {
return Status::NotImplemented("tabular function of non-null arity");
}
auto kernels =
arrow::internal::checked_pointer_cast<compute::ScalarFunction>(func)->kernels();
if (kernels.size() != 1) {
return Status::NotImplemented("tabular function with non-single kernel");
}
const compute::ScalarKernel* kernel = kernels[0];
auto out_type = kernel->signature->out_type();
if (out_type.kind() != compute::OutputType::FIXED) {
return Status::Invalid("tabular kernel of non-fixed kind");
}
auto datatype = out_type.type();
if (datatype->id() != Type::type::STRUCT) {
return Status::Invalid("tabular kernel with non-struct output");
}
auto struct_type = arrow::internal::checked_cast<StructType*>(datatype.get());
auto schema = ::arrow::schema(struct_type->fields());
std::vector<TypeHolder> in_types;
ARROW_ASSIGN_OR_RAISE(auto func_exec,
GetFunctionExecutor(func_name, in_types, NULLPTR, registry));
auto next_func =
[schema,
func_exec = std::move(func_exec)]() -> Result<std::shared_ptr<RecordBatch>> {
std::vector<Datum> args;
// passed_length of -1 or 0 with args.size() of 0 leads to an empty ExecSpanIterator
// in exec.cc and to never invoking the source function, so 1 is passed instead
// TODO: GH-33612: Support batch size in user-defined tabular functions
ARROW_ASSIGN_OR_RAISE(auto datum, func_exec->Execute(args, /*passed_length=*/1));
if (!datum.is_array()) {
return Status::Invalid("UDF result of non-array kind");
}
std::shared_ptr<Array> array = datum.make_array();
if (array->length() == 0) {
return IterationTraits<std::shared_ptr<RecordBatch>>::End();
}
ARROW_ASSIGN_OR_RAISE(auto batch, RecordBatch::FromStructArray(std::move(array)));
if (!schema->Equals(batch->schema())) {
return Status::Invalid("UDF result with shape not conforming to schema");
}
return std::move(batch);
};
return RecordBatchReader::MakeFromIterator(MakeFunctionIterator(std::move(next_func)),
schema);
}
} // namespace py
} // namespace arrow