[[arrow::export]]
| 735 | |
| 736 | // [[arrow::export]] |
| 737 | void RegisterScalarUDF(std::string name, cpp11::list func_sexp) { |
| 738 | cpp11::list in_type_r(func_sexp["in_type"]); |
| 739 | cpp11::list out_type_r(func_sexp["out_type"]); |
| 740 | R_xlen_t n_kernels = in_type_r.size(); |
| 741 | |
| 742 | if (n_kernels == 0) { |
| 743 | cpp11::stop("Can't register user-defined function with zero kernels"); |
| 744 | } |
| 745 | |
| 746 | // Compute the Arity from the list of input kernels. We don't currently handle |
| 747 | // variable numbers of arguments in a user-defined function. |
| 748 | int n_args = cpp11::as_cpp<std::shared_ptr<arrow::Schema>>(in_type_r[0])->num_fields(); |
| 749 | for (R_xlen_t i = 1; i < n_kernels; i++) { |
| 750 | auto in_types = cpp11::as_cpp<std::shared_ptr<arrow::Schema>>(in_type_r[i]); |
| 751 | if (in_types->num_fields() != n_args) { |
| 752 | cpp11::stop( |
| 753 | "Kernels for user-defined function must accept the same number of arguments"); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | arrow::compute::Arity arity(n_args, false); |
| 758 | |
| 759 | // The function documentation isn't currently accessible from R but is required |
| 760 | // for the C++ function constructor. |
| 761 | std::vector<std::string> dummy_argument_names(n_args); |
| 762 | for (int64_t i = 0; i < n_args; i++) { |
| 763 | dummy_argument_names[i] = "arg"; |
| 764 | } |
| 765 | const arrow::compute::FunctionDoc dummy_function_doc{ |
| 766 | "A user-defined R function", "returns something", std::move(dummy_argument_names)}; |
| 767 | |
| 768 | auto func = |
| 769 | std::make_shared<arrow::compute::ScalarFunction>(name, arity, dummy_function_doc); |
| 770 | |
| 771 | for (R_xlen_t i = 0; i < n_kernels; i++) { |
| 772 | auto in_types = cpp11::as_cpp<std::shared_ptr<arrow::Schema>>(in_type_r[i]); |
| 773 | cpp11::sexp out_type_func = out_type_r[i]; |
| 774 | |
| 775 | std::vector<arrow::compute::InputType> compute_in_types(in_types->num_fields()); |
| 776 | for (int j = 0; j < in_types->num_fields(); j++) { |
| 777 | compute_in_types[j] = arrow::compute::InputType(in_types->field(j)->type()); |
| 778 | } |
| 779 | |
| 780 | arrow::compute::OutputType out_type((&ResolveScalarUDFOutputType)); |
| 781 | |
| 782 | auto signature = std::make_shared<arrow::compute::KernelSignature>( |
| 783 | std::move(compute_in_types), std::move(out_type), true); |
| 784 | arrow::compute::ScalarKernel kernel(signature, &CallRScalarUDF); |
| 785 | kernel.mem_allocation = arrow::compute::MemAllocation::NO_PREALLOCATE; |
| 786 | kernel.null_handling = arrow::compute::NullHandling::COMPUTED_NO_PREALLOCATE; |
| 787 | kernel.data = |
| 788 | std::make_shared<RScalarUDFKernelState>(func_sexp["wrapper_fun"], out_type_func); |
| 789 | |
| 790 | StopIfNotOk(func->AddKernel(std::move(kernel))); |
| 791 | } |
| 792 | |
| 793 | auto registry = arrow::compute::GetFunctionRegistry(); |
| 794 | StopIfNotOk(registry->AddFunction(std::move(func), true)); |
no test coverage detected