paleolimbot commented on code in PR #13397: URL: https://github.com/apache/arrow/pull/13397#discussion_r918308915
########## r/src/compute.cpp: ########## @@ -574,3 +576,169 @@ SEXP compute__CallFunction(std::string func_name, cpp11::list args, cpp11::list std::vector<std::string> compute__GetFunctionNames() { return arrow::compute::GetFunctionRegistry()->GetFunctionNames(); } + +class RScalarUDFKernelState : public arrow::compute::KernelState { + public: + RScalarUDFKernelState(cpp11::sexp exec_func, cpp11::sexp resolver) + : exec_func_(exec_func), resolver_(resolver) {} + + cpp11::function exec_func_; + cpp11::function resolver_; +}; + +arrow::Result<arrow::TypeHolder> ResolveScalarUDFOutputType( + arrow::compute::KernelContext* context, + const std::vector<arrow::TypeHolder>& input_types) { + return SafeCallIntoR<arrow::TypeHolder>( + [&]() -> arrow::TypeHolder { + auto kernel = + reinterpret_cast<const arrow::compute::ScalarKernel*>(context->kernel()); + auto state = std::dynamic_pointer_cast<RScalarUDFKernelState>(kernel->data); + + cpp11::writable::list input_types_sexp(input_types.size()); + for (size_t i = 0; i < input_types.size(); i++) { + input_types_sexp[i] = + cpp11::to_r6<arrow::DataType>(input_types[i].GetSharedPtr()); + } + + cpp11::sexp output_type_sexp = state->resolver_(input_types_sexp); + if (!Rf_inherits(output_type_sexp, "DataType")) { + cpp11::stop("arrow_scalar_function resolver must return a DataType"); Review Comment: I updated this - a user might get this error if the scalar function author provided an R function as a type resolver and it didn't return the right thing (more likely a scalar function author would get the error when writing tests which hopefully they do if they're using that feature). -- 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