lidavidm commented on code in PR #12590: URL: https://github.com/apache/arrow/pull/12590#discussion_r843952853
########## cpp/src/arrow/python/udf.cc: ########## @@ -0,0 +1,162 @@ +// 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 <cstddef> +#include <memory> +#include <sstream> + +#include "arrow/compute/function.h" +#include "arrow/python/common.h" + +namespace arrow { + +namespace py { + +Status VerifyArrayInput(const compute::ExecBatch& batch) { + for (auto value : batch.values) { + if (!value.is_array()) { + return Status::Invalid("Expected array input, but got ", value.type()); + } + } + return Status::OK(); +} + +Status VerifyScalarInput(const compute::ExecBatch& batch) { + for (auto value : batch.values) { + if (!value.is_scalar()) { + return Status::Invalid("Expected scalar input, but got ", value.type()); + } + } + return Status::OK(); +} + +Status VerifyArityAndInput(compute::Arity arity, const compute::ExecBatch& batch) { + if (!arity.is_varargs) { + bool match = static_cast<uint64_t>(arity.num_args) == batch.values.size(); + if (!match) { + return Status::Invalid( + "Function Arity and Input data shape doesn't match, expected ", arity.num_args, + ", got ", batch.values.size()); + } + } else { + bool match = static_cast<uint64_t>(arity.num_args) <= batch.values.size(); + if (!match) { + return Status::Invalid("Required minimum number of arguments", arity.num_args, + " in VarArgs function is not met.", ", Received ", + batch.values.size()); + } + } + return Status::OK(); +} + +Status ExecFunctionScalar(const compute::ExecBatch& batch, PyObject* function, + const compute::Arity& arity, Datum* out) { + // num_args for arity varargs is arity.num_args, and for other arities, + // it is equal to the number of values in the batch + int64_t num_args = + arity.is_varargs ? static_cast<int64_t>(batch.values.size()) : arity.num_args; + PyObject* arg_tuple = PyTuple_New(num_args); + for (int arg_id = 0; arg_id < num_args; arg_id++) { + if (!batch[arg_id].is_scalar()) { + return Status::Invalid("Input type and data type doesn't match"); + } + auto c_data = batch[arg_id].scalar(); + PyObject* data = wrap_scalar(c_data); + PyTuple_SetItem(arg_tuple, arg_id, data); + } + PyObject* result = PyObject_CallObject(function, arg_tuple); Review Comment: At the very least, use CheckPyError. And at least some code prefers SafeCallIntoPython over explicitly manipulating the GIL. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org