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 2021/06/29 14:56:22 UTC

[GitHub] [arrow] bkietz opened a new pull request #10619: Sketch of a general purpose cython trampolining utility

bkietz opened a new pull request #10619:
URL: https://github.com/apache/arrow/pull/10619


   


-- 
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



[GitHub] [arrow] bkietz closed pull request #10619: ARROW-13226: [Python] Add a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
bkietz closed pull request #10619:
URL: https://github.com/apache/arrow/pull/10619


   


-- 
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



[GitHub] [arrow] bkietz commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
bkietz commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r661714951



##########
File path: python/pyarrow/tests/test_dataset.py
##########
@@ -3095,10 +3095,17 @@ def test_write_dataset_use_threads(tempdir):
         pa.schema([("part", pa.string())]), flavor="hive")
 
     target1 = tempdir / 'partitioned1'
+    paths_written = []
+
+    def writer_pre_finish(writer):
+        pass
+        # paths_written.append(writer.path)

Review comment:
       unfortunately there's a segfault I can't track down for this non-example usage




-- 
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



[GitHub] [arrow] github-actions[bot] commented on pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#issuecomment-870673659


   <!--
     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.
   -->
   
   Thanks for opening a pull request!
   
   If this is not a [minor PR](https://github.com/apache/arrow/blob/master/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on JIRA? https://issues.apache.org/jira/browse/ARROW
   
   Opening JIRAs ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
   
   Then could you also rename pull request title in the following format?
   
       ARROW-${JIRA_ID}: [${COMPONENT}] ${SUMMARY}
   
   or
   
       MINOR: [${COMPONENT}] ${SUMMARY}
   
   See also:
   
     * [Other pull requests](https://github.com/apache/arrow/pulls/)
     * [Contribution Guidelines - How to contribute patches](https://arrow.apache.org/docs/developers/contributing.html#how-to-contribute-patches)
   


-- 
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



[GitHub] [arrow] bkietz commented on pull request #10619: ARROW-13226: [Python] Add a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
bkietz commented on pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#issuecomment-871718386


   @westonpace alright I've opened a JIRA just for BindFunction and I'll strip the dataset:: specifics


-- 
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



[GitHub] [arrow] github-actions[bot] removed a comment on pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
github-actions[bot] removed a comment on pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#issuecomment-870673659


   <!--
     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.
   -->
   
   Thanks for opening a pull request!
   
   If this is not a [minor PR](https://github.com/apache/arrow/blob/master/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on JIRA? https://issues.apache.org/jira/browse/ARROW
   
   Opening JIRAs ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
   
   Then could you also rename pull request title in the following format?
   
       ARROW-${JIRA_ID}: [${COMPONENT}] ${SUMMARY}
   
   or
   
       MINOR: [${COMPONENT}] ${SUMMARY}
   
   See also:
   
     * [Other pull requests](https://github.com/apache/arrow/pulls/)
     * [Contribution Guidelines - How to contribute patches](https://arrow.apache.org/docs/developers/contributing.html#how-to-contribute-patches)
   


-- 
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



[GitHub] [arrow] pitrou commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
pitrou commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r660776807



##########
File path: cpp/src/arrow/python/common.h
##########
@@ -185,6 +185,62 @@ class ARROW_PYTHON_EXPORT OwnedRefNoGIL : public OwnedRef {
   }
 };
 
+template <typename Fn>
+struct BoundFunction;
+
+template <typename... Args>
+struct BoundFunction<void(PyObject*, Args...)> {
+  // We bind `cdef void fn(object, ...)` to get a `Status(...)`
+  // where the Status contains any Python error raised by `fn`
+  using Unbound = void(PyObject*, Args...);
+  using Bound = Status(Args...);
+
+  BoundFunction(Unbound* unbound, PyObject* bound_arg)
+      : bound_arg_(bound_arg), unbound_(unbound) {}
+
+  Status Invoke(Args... args) const {
+    unbound_(bound_arg_.obj(), std::forward<Args>(args)...);
+    return CheckPyError();
+  }
+
+  Unbound* unbound_;
+  OwnedRefNoGIL bound_arg_;
+};
+
+template <typename Return, typename... Args>
+struct BoundFunction<Return(PyObject*, Args...)> {
+  // We bind `cdef Return fn(object, ...)` to get a `Result<Return>(...)`
+  // where the Result contains any Python error raised by `fn` or the
+  // return value from `fn`.
+  using Unbound = Return(PyObject*, Args...);
+  using Bound = Result<Return>(Args...);
+
+  BoundFunction(Unbound* unbound, PyObject* bound_arg)
+      : bound_arg_(bound_arg), unbound_(unbound) {}
+
+  Result<Return> Invoke(Args... args) const {
+    Return ret = unbound_(bound_arg_.obj(), std::forward<Args>(args)...);
+    RETURN_NOT_OK(CheckPyError());
+    return ret;
+  }
+
+  Unbound* unbound_;
+  OwnedRefNoGIL bound_arg_;
+};
+
+template <typename OutFn, typename Return, typename... Args>
+std::function<OutFn> BindFunction(Return (*unbound)(PyObject*, Args...),
+                                  PyObject* bound_arg) {
+  using Fn = BoundFunction<Return(PyObject*, Args...)>;
+
+  static_assert(std::is_same<typename Fn::Bound, OutFn>::value,
+                "requested bound function of unsupported type");
+
+  Py_XINCREF(bound_arg);
+  auto bound_fn = std::make_shared<Fn>(unbound, bound_arg);
+  return [bound_fn](Args... args) { return bound_fn->Invoke(args...); };

Review comment:
       Should use `std::forward<Args>`?




-- 
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



[GitHub] [arrow] bkietz commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
bkietz commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r660788491



##########
File path: cpp/src/arrow/python/common.h
##########
@@ -185,6 +185,62 @@ class ARROW_PYTHON_EXPORT OwnedRefNoGIL : public OwnedRef {
   }
 };
 
+template <typename Fn>
+struct BoundFunction;
+
+template <typename... Args>
+struct BoundFunction<void(PyObject*, Args...)> {
+  // We bind `cdef void fn(object, ...)` to get a `Status(...)`
+  // where the Status contains any Python error raised by `fn`
+  using Unbound = void(PyObject*, Args...);
+  using Bound = Status(Args...);
+
+  BoundFunction(Unbound* unbound, PyObject* bound_arg)
+      : bound_arg_(bound_arg), unbound_(unbound) {}
+
+  Status Invoke(Args... args) const {
+    unbound_(bound_arg_.obj(), std::forward<Args>(args)...);
+    return CheckPyError();
+  }

Review comment:
       SafeCallIntoPython is awkward in the presence of void return, I'll just use PyAcquireGIL

##########
File path: python/pyarrow/tests/test_dataset.py
##########
@@ -3397,3 +3404,13 @@ def test_dataset_null_to_dictionary_cast(tempdir, dataset_reader):
     )
     table = dataset_reader.to_table(fsds)
     assert table.schema == schema
+
+
+def test_visit_strings_adhoc():
+    import pyarrow._dataset as _ds
+
+    strings = ['a', 'b', 'c']
+    visited = []
+    _ds._visit_strings(strings, visited.append)

Review comment:
       will do

##########
File path: cpp/src/arrow/python/common.h
##########
@@ -185,6 +185,62 @@ class ARROW_PYTHON_EXPORT OwnedRefNoGIL : public OwnedRef {
   }
 };
 
+template <typename Fn>
+struct BoundFunction;
+
+template <typename... Args>
+struct BoundFunction<void(PyObject*, Args...)> {
+  // We bind `cdef void fn(object, ...)` to get a `Status(...)`
+  // where the Status contains any Python error raised by `fn`
+  using Unbound = void(PyObject*, Args...);
+  using Bound = Status(Args...);
+
+  BoundFunction(Unbound* unbound, PyObject* bound_arg)
+      : bound_arg_(bound_arg), unbound_(unbound) {}
+
+  Status Invoke(Args... args) const {
+    unbound_(bound_arg_.obj(), std::forward<Args>(args)...);
+    return CheckPyError();
+  }
+
+  Unbound* unbound_;
+  OwnedRefNoGIL bound_arg_;
+};
+
+template <typename Return, typename... Args>
+struct BoundFunction<Return(PyObject*, Args...)> {
+  // We bind `cdef Return fn(object, ...)` to get a `Result<Return>(...)`
+  // where the Result contains any Python error raised by `fn` or the
+  // return value from `fn`.
+  using Unbound = Return(PyObject*, Args...);
+  using Bound = Result<Return>(Args...);
+
+  BoundFunction(Unbound* unbound, PyObject* bound_arg)
+      : bound_arg_(bound_arg), unbound_(unbound) {}
+
+  Result<Return> Invoke(Args... args) const {
+    Return ret = unbound_(bound_arg_.obj(), std::forward<Args>(args)...);
+    RETURN_NOT_OK(CheckPyError());
+    return ret;
+  }
+
+  Unbound* unbound_;
+  OwnedRefNoGIL bound_arg_;
+};
+
+template <typename OutFn, typename Return, typename... Args>
+std::function<OutFn> BindFunction(Return (*unbound)(PyObject*, Args...),
+                                  PyObject* bound_arg) {
+  using Fn = BoundFunction<Return(PyObject*, Args...)>;
+
+  static_assert(std::is_same<typename Fn::Bound, OutFn>::value,
+                "requested bound function of unsupported type");
+
+  Py_XINCREF(bound_arg);
+  auto bound_fn = std::make_shared<Fn>(unbound, bound_arg);
+  return [bound_fn](Args... args) { return bound_fn->Invoke(args...); };

Review comment:
       will do




-- 
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



[GitHub] [arrow] westonpace commented on pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
westonpace commented on pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#issuecomment-871655477


   I've used this as part of #10628 which uses it to hook metadata_collector into the file visitor.  If we want to merge this separately maybe reduce the scope to be just BindFunction?


-- 
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



[GitHub] [arrow] pitrou commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
pitrou commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r660774414



##########
File path: python/pyarrow/tests/test_dataset.py
##########
@@ -3397,3 +3404,13 @@ def test_dataset_null_to_dictionary_cast(tempdir, dataset_reader):
     )
     table = dataset_reader.to_table(fsds)
     assert table.schema == schema
+
+
+def test_visit_strings_adhoc():
+    import pyarrow._dataset as _ds
+
+    strings = ['a', 'b', 'c']
+    visited = []
+    _ds._visit_strings(strings, visited.append)

Review comment:
       Also test a failing callback?




-- 
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



[GitHub] [arrow] pitrou commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
pitrou commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r660775729



##########
File path: cpp/src/arrow/python/common.h
##########
@@ -185,6 +185,62 @@ class ARROW_PYTHON_EXPORT OwnedRefNoGIL : public OwnedRef {
   }
 };
 
+template <typename Fn>
+struct BoundFunction;
+
+template <typename... Args>
+struct BoundFunction<void(PyObject*, Args...)> {
+  // We bind `cdef void fn(object, ...)` to get a `Status(...)`
+  // where the Status contains any Python error raised by `fn`
+  using Unbound = void(PyObject*, Args...);
+  using Bound = Status(Args...);
+
+  BoundFunction(Unbound* unbound, PyObject* bound_arg)
+      : bound_arg_(bound_arg), unbound_(unbound) {}
+
+  Status Invoke(Args... args) const {
+    unbound_(bound_arg_.obj(), std::forward<Args>(args)...);
+    return CheckPyError();
+  }

Review comment:
       If this is meant to be called from arbitrary C++ code, it should probably take the GIL before anything else. Perhaps reuse `SafeCallIntoPython`?




-- 
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



[GitHub] [arrow] github-actions[bot] commented on pull request #10619: ARROW-13226: [Python] Add a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#issuecomment-871716353


   https://issues.apache.org/jira/browse/ARROW-13226


-- 
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



[GitHub] [arrow] bkietz commented on pull request #10619: ARROW-13226: [Python] Add a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
bkietz commented on pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#issuecomment-872179888


   +1, merging


-- 
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



[GitHub] [arrow] bkietz commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
bkietz commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r660788491



##########
File path: cpp/src/arrow/python/common.h
##########
@@ -185,6 +185,62 @@ class ARROW_PYTHON_EXPORT OwnedRefNoGIL : public OwnedRef {
   }
 };
 
+template <typename Fn>
+struct BoundFunction;
+
+template <typename... Args>
+struct BoundFunction<void(PyObject*, Args...)> {
+  // We bind `cdef void fn(object, ...)` to get a `Status(...)`
+  // where the Status contains any Python error raised by `fn`
+  using Unbound = void(PyObject*, Args...);
+  using Bound = Status(Args...);
+
+  BoundFunction(Unbound* unbound, PyObject* bound_arg)
+      : bound_arg_(bound_arg), unbound_(unbound) {}
+
+  Status Invoke(Args... args) const {
+    unbound_(bound_arg_.obj(), std::forward<Args>(args)...);
+    return CheckPyError();
+  }

Review comment:
       `SafeCallIntoPython` assumes that we're returning a `Result<T>` or `Status`, which `unbound_` doesn't (it returns `T` or `void`).
   
   Additionally, gcc4.8 [cannot pass a parameter pack into a lambda](https://wandbox.org/permlink/SoKWKBMJCWDVuyjZ)
   
   I'll use PyAcquireGIL




-- 
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



[GitHub] [arrow] lidavidm commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
lidavidm commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r660841726



##########
File path: python/pyarrow/tests/test_dataset.py
##########
@@ -3095,10 +3095,17 @@ def test_write_dataset_use_threads(tempdir):
         pa.schema([("part", pa.string())]), flavor="hive")
 
     target1 = tempdir / 'partitioned1'
+    paths_written = []
+
+    def writer_pre_finish(writer):
+        pass
+        # paths_written.append(writer.path)

Review comment:
       Was this meant to remain commented out?




-- 
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



[GitHub] [arrow] westonpace commented on a change in pull request #10619: Sketch of a general purpose cython trampolining utility

Posted by GitBox <gi...@apache.org>.
westonpace commented on a change in pull request #10619:
URL: https://github.com/apache/arrow/pull/10619#discussion_r661738910



##########
File path: python/pyarrow/tests/test_dataset.py
##########
@@ -3095,10 +3095,17 @@ def test_write_dataset_use_threads(tempdir):
         pa.schema([("part", pa.string())]), flavor="hive")
 
     target1 = tempdir / 'partitioned1'
+    paths_written = []
+
+    def writer_pre_finish(writer):
+        pass
+        # paths_written.append(writer.path)

Review comment:
       FYI, I merged this in and modified this part somewhat.  It's working for me at #10628 




-- 
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