westonpace commented on code in PR #12590:
URL: https://github.com/apache/arrow/pull/12590#discussion_r854544306


##########
cpp/src/arrow/python/udf.cc:
##########
@@ -0,0 +1,234 @@
+// 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 CheckOutputType(const DataType& expected, const DataType& actual) {
+  if (!expected.Equals(actual)) {
+    return Status::TypeError("Expected output type, ", expected.name(),
+                             ", but function returned type ", actual.name());
+  }
+  return Status::OK();
+}
+
+// struct PythonUdf {
+//   std::shared_ptr<OwnedRefNoGIL> function;
+//   compute::OutputType output_type;
+
+//   // function needs to be destroyed at process exit
+//   // and Python may no longer be initialized.
+//   ~PythonUdf() {
+//     if (_Py_IsFinalizing()) {
+//       function->detach();
+//     }
+//   }
+
+//   Status operator()(compute::KernelContext* ctx, const compute::ExecBatch& 
batch,
+//                     Datum* out) {
+//     return SafeCallIntoPython([=]() -> Status { return Execute(ctx, batch, 
out); });
+//   }
+
+//   Status Execute(compute::KernelContext* ctx, const compute::ExecBatch& 
batch,
+//                  Datum* out) {
+//     const auto num_args = batch.values.size();
+//     PyObject* arg_tuple = PyTuple_New(num_args);
+//     for (size_t arg_id = 0; arg_id < num_args; arg_id++) {
+//       switch (batch[arg_id].kind()) {
+//         case Datum::SCALAR: {
+//           auto c_data = batch[arg_id].scalar();
+//           PyObject* data = wrap_scalar(c_data);
+//           PyTuple_SetItem(arg_tuple, arg_id, data);
+//           break;
+//         }
+//         case Datum::ARRAY: {
+//           auto c_data = batch[arg_id].make_array();
+//           PyObject* data = wrap_array(c_data);
+//           PyTuple_SetItem(arg_tuple, arg_id, data);
+//           break;
+//         }
+//         default:
+//           auto datum = batch[arg_id];
+//           return Status::NotImplemented(
+//               "User-defined-functions are not supported for the datum kind 
",
+//               datum.ToString(batch[arg_id].kind()));
+//       }
+//     }
+//     PyObject* result;
+//     result = PyObject_CallObject(function->obj(), arg_tuple);
+//     RETURN_NOT_OK(CheckPyError());
+//     if (result == Py_None) {
+//       return Status::Invalid("Output is None, but expected an array");
+//     }
+//     // unwrapping the output for expected output type
+//     if (is_scalar(result)) {
+//       ARROW_ASSIGN_OR_RAISE(auto val, unwrap_scalar(result));
+//       RETURN_NOT_OK(CheckOutputType(*output_type.type(), *val->type));
+//       *out = Datum(val);
+//       return Status::OK();
+//     } else if (is_array(result)) {
+//       ARROW_ASSIGN_OR_RAISE(auto val, unwrap_array(result));
+//       RETURN_NOT_OK(CheckOutputType(*output_type.type(), *val->type()));
+//       *out = Datum(val);
+//       return Status::OK();
+//     } else {
+//       return Status::TypeError("Unexpected output type: ", 
Py_TYPE(result)->tp_name,
+//                                " (expected Scalar or Array)");
+//     }
+//     return Status::OK();
+//   }
+// };
+
+struct PythonUdf {
+  ScalarUdfWrapperCallback cb;
+  std::shared_ptr<OwnedRefNoGIL> function;
+  compute::OutputType output_type;
+
+  // function needs to be destroyed at process exit
+  // and Python may no longer be initialized.
+  ~PythonUdf() {
+    if (_Py_IsFinalizing()) {
+      function->detach();
+    }
+  }

Review Comment:
   We could use `atexit`.  There is `Py_AtExit` but it seems that is called 
after finalization has happened so it is no better than what we are doing here.
   
   The main advantage of using an exit handler would be the case where the user 
is finalizing the interpreter but not ending their program (e.g. maybe they are 
using multiple interpreters or something).  In that case, at finalization, we 
could also remove all python UDFs from the registry (once we have that 
capability).
   
   Maybe this is something we could tackle as part of the unregister PR?  
Otherwise the best we can do is somehow flag the function as finalized so that 
any future call to it would fail with a nice error status.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to