paleolimbot commented on code in PR #13397: URL: https://github.com/apache/arrow/pull/13397#discussion_r901502171
########## r/src/compute.cpp: ########## @@ -574,3 +576,90 @@ SEXP compute__CallFunction(std::string func_name, cpp11::list args, cpp11::list std::vector<std::string> compute__GetFunctionNames() { return arrow::compute::GetFunctionRegistry()->GetFunctionNames(); } + +class RScalarUDFCallable : public arrow::compute::ArrayKernelExec { + public: + RScalarUDFCallable(const std::shared_ptr<arrow::Schema>& input_types, + const std::shared_ptr<arrow::DataType>& output_type, cpp11::sexp fun) + : input_types_(input_types), output_type_(output_type), fun_(fun) {} + + arrow::Status operator()(arrow::compute::KernelContext* context, + const arrow::compute::ExecSpan& span, + arrow::compute::ExecResult* result) { + std::vector<std::shared_ptr<arrow::Array>> array_args; + for (int64_t i = 0; i < span.num_values(); i++) { + const arrow::compute::ExecValue& v = span[i]; + if (v.is_array()) { + array_args.push_back(v.array.ToArray()); + } else if (v.is_scalar()) { + auto array = ValueOrStop(arrow::MakeArrayFromScalar(*v.scalar, span.length)); + array_args.push_back(array); + } + } + + auto batch = arrow::RecordBatch::Make(input_types_, span.length, array_args); + + auto fun_result = SafeCallIntoR<std::shared_ptr<arrow::Array>>([&]() { + cpp11::sexp batch_sexp = cpp11::to_r6<arrow::RecordBatch>(batch); + cpp11::sexp batch_length_sexp = cpp11::as_sexp(span.length); + + cpp11::writable::list udf_context = {batch_length_sexp}; + udf_context.names() = {"batch_length"}; + + cpp11::sexp fun_result_sexp = fun_(udf_context, batch_sexp); + if (!Rf_inherits(fun_result_sexp, "Array")) { + cpp11::stop("arrow_scalar_function must return an Array"); + } + + return cpp11::as_cpp<std::shared_ptr<arrow::Array>>(fun_result_sexp); + }); + + if (!fun_result.ok()) { + return fun_result.status(); + } + + result->value.emplace<std::shared_ptr<arrow::ArrayData>>( + fun_result.ValueUnsafe()->data()); + return arrow::Status::OK(); Review Comment: @vibhatha Running this line causes later code to segfault...I don't *think* that my Array is the problem but I think that I might have the wrong incantation here with respect to how to put the result into the `arrow::compute::ExecResult* result`. -- 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