You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/06/20 10:17:19 UTC

[GitHub] [arrow] paleolimbot commented on a diff in pull request #13397: ARROW-16444: [R] Implement user-defined scalar functions in R bindings

paleolimbot commented on code in PR #13397:
URL: https://github.com/apache/arrow/pull/13397#discussion_r901497172


##########
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();
+  }
+
+ private:
+  std::shared_ptr<arrow::Schema> input_types_;
+  std::shared_ptr<arrow::DataType> output_type_;
+  cpp11::function fun_;
+};
+
+// [[arrow::export]]
+void RegisterScalarUDF(std::string name, cpp11::sexp fun) {
+  const arrow::compute::FunctionDoc dummy_function_doc{
+      "A user-defined R function", "returns something", {"..."}};
+
+  auto func = std::make_shared<arrow::compute::ScalarFunction>(
+      name, arrow::compute::Arity::VarArgs(), dummy_function_doc);

Review Comment:
   I'm not aware of any way to access the value of function documentation in the R bindings, so it would be a little weird to document it in the query engine itself (more likely would be that the package registering the function would provide their own R wrapper and document that).



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org