You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++][Python] Move all Python related code into PyArrow #31724
Joris Van den Bossche / @jorisvandenbossche:
Moving to the 10.0.0 milestone. Given that this is a large change, the goal is to merge this as soon as possible after the 9.0.0 release is cut, so we have the full 10.0.0 cycle to test and stabilize the change.
Yue Ni / @niyue:
I have a project relying on arrow/python/pyarrow.h, and I installed arrow from vcpkg https://github.com/microsoft/vcpkg/tree/master/ports/arrow. Previously arrow/python/pyarrow.h is available in include directory after installing arrow, but after upgrading to the latest version of arrow, more specifically commit ec579df, I am not able to find pyarrow.h in include directory of vcpkg.
I think it is probably caused by this change, do we have any recommendation what projects relying on arrow/python/pyarrow.h should do to overcome this issue?
Alenka Frim / @AlenkaF:
Can you check if arrow/python/pyarrow.h is available in arrow/python/pyarrow/include/? And if it is, will you be able to include it from there?
it will still be installed by the python package, only not in the standard "include" directory
Where is the header is expected to be installed? Is it expected to be installed by pyarrow's python wheel to some where and I have to add this path to my compiler's include path?
In my C++ project, I use vcpkg to manage dependency. And one of the module is a python binding for the C++ library, where I use pyarrow's C++ API like arrow::py::wrap_table together with pybind11 to create the python binding. Since I use vcpkg to manage dependency, I expect all C++ dependencies available via vcpkg.
Previously pyarrow C++ is part of vcpkg arrow port, and I can use CMake's find_library(arrow_python) to find the library
and use find_path(arrow/python/pyarrow.h) to find the path to the include directory
What I find for the latest arrow version:
the libarrow_python.a is not built, even if I set ARROW_PYTHON CMake option to ON.
the arrow/python/pyarrow.h cannot be found in include directory after building the C++ library (at least using vcpkg arrow port)
I went through most of the comments in PR for this issue [1] and read this ARROW_PYTHON option issue [2] as well, and the current behavior seems to be the expected behavior. The python directory will NOT be built even if ARROW_PYTHON=ON.
I am not sure what the recommended approach for using pyarrow in C++. According to the document here [3], to make the build automated, it seems these are the steps:
install pyarrow package
launch python, run pyarrow.get_include() to get the include directory
add the include directory to compiler's include search path (probably via CMake)
where is it expected to find the libarrow_python so that CMake can find and use it for link?
build
Is this the recommended approach for doing this? I am not quite sure step #4, any comments on this?
I can think of another approach, which is creating another vcpkg port like arrow_python and build the arrow_python library explicitly, so that projects can use this port for such purpose. Is this a recommended approach after this issue? Thanks.
Yue Ni / @niyue: @kou sorry it is not open source at this moment, but the binding itself is a just very thin wrapper for the C++ library. The code looks like this:
// pyarrow.h is included here
#include <arrow/python/pyarrow.h>
#include <pybind11/pybind11.h>
classSearchContext {
public:
// ...SearchContext() {
// here pyarrow module is initializedautoresult = arrow::py::import_pyarrow();
// ...
}
pybind11::objectsearch(conststring &sql) {
// call the C++ library to run the search and get the result as an arrow tableautotable = get_exec_context()->search(sql);
// use pyarrow's C++ API to wrap the Arrow C++ table as a pyarrow table instancereturn_wrap_table(table);
}
private:
pybind11::object_wrap_table(std::shared_ptr<arrow::Table> table) {
// this is where pyarrow.h is used, the wrap_table API returnpybind11::reinterpret_steal<pybind11::object(arrow::py::wrap_table(table));
}
}
// the above SearchContext class is exposed via pybind11 so that users can use them in Python PYBIND11_MODULE(my_py_binding, m) {
py::class_<SearchContext>(m, "SearchContext")
// ...
.def("search", &SearchContext::search, py::arg("sql") = "");
// ....
}
Here are the materials we referred to when we authored this binding initially (probably two years ago), we don't revisit if there is better approach since it is working for us until now.
auto table = get_exec_context()->search(sql);
auto reader = arrow::tableBatchReader(table);
structArrowArrayStream c_stream;
auto status = arrow::ExportRecordBatchReader(reader, &c_stream); // TODO: Check status// Call the following Python code from pybind11 (sorry. I'm not familiar with Python's C API):// py_reader = pyarrow.ipc.RecordBatchReader._import_from_c(&c_stream);// py_table = pyarrow.Table.from_batches(py_reader);return pybind11::reinterpret_steal<pybind11::object>(py_table);
The C data interface and the C stream interface are included in Apache Arrow C++.
Yue Ni / @niyue: @kou thanks so much. I will give it a try and get back to you later (probably not very soon because I will ask someone else for verifying the python part).
Could you please briefly explain more how this works? I would like to confirm, do you mean the pyarrow.ipc.RecordBatchReader._import_from_c is python code and not C++ code, and we could use pybind11 to execute the pyarrow python code to do this (probably using pybind11's API like https://pybind11.readthedocs.io/en/stable/advanced/embedding.html#executing-python-code)?
Yue Ni / @niyue: @kou I follow your instruction (with some slight modification) to make it compiled and running but ran into some segfault during importingpyarrow module.
1) Since I create a python binding, I use this python binding in my Python code.py::scoped_interpreter guard{} seems not necessary because that will embed another interpreter, otherwise, pybind will report the interpreter is already running error. This is the only thing I revised.
It seems both my python binding and pyarrow uses arrow's code to do some initialization, the arrow::io::internal::GetIOThreadPool for some reason fails to work in this case and caused the segfault.
{}4) Since it is related with ::arrow::internal::GetEnvVar("ARROW_IO_THREADS");{}, I tried setting the ARROW_IO_THREADS env var to a number and gave it another try. And the program still caused a segfault, but reporting a different error (there is no free(): invalid pointer printed this time):
It is different with previously core dump, but I have no idea how this happens now. And the behavior is pretty consistent, if I unset the ARROW_IO_THREADS env var and run the program again, the previous core dump will be generated again.
Summary:
I don't know how to proceed any more. Basically:
I create a C++ library with arrow C++ library
I create a python binding for the C++ library using pybind
Instead of importing pyarrow.h header, I now import <arrow/c/abi.h> and <arrow/c/bridge.h> for bridging
In my python program, I use both my python binding and pyarrow wheel
Could you please shed some light on this issue? Do you think if this kind usage is supported or if it is some bug for arrow project?
Does it mean that you use Apache Arrow C++ from vcpkg and pyarrow wheel from PyPI?
If so, you should not use Apache Arrow C++ from vcpkg. Could you use https://arrow.apache.org/docs/dev/python/integration/extending.html#c-api instead (use pyarrow.get_include()/pyarrow.get_libraries()/pyarrow.get_library_dirs()/arrow/python/pyarrow.h instead of find_package(Arrow)/<arrow/c/abi.h>/<arrow/c/bridge.h>)?
Does it mean that you use Apache Arrow C++ from vcpkg and pyarrow wheel from PyPI?
Almost. I use Apache Arrow C++ from vcpkg (but I don't use the latest version of Arrow in vcpkg, instead, I use a fork of it with some gandiva related modification, and use a custom vcpkg port to manage the arrow dependency).
If so, you should not use Apache Arrow C++ from vcpkg.
Could you briefly explain why this should not be done this way?
Kouhei Sutou / @kou:
Because pyarrow wheel includes pre-built Apache Arrow C++ library. If you use both of Apache Arrow C++ from vcpkg and pyarrow wheel from PyPI, you mix multiple Apache Arrow C++ libraries. It causes unexpected behavior such as a crash.
Yue Ni / @niyue: @kou thanks for the pointer. After some more experiments, I managed to make it to work, the two minor changes are (compiler doesn't help on either of them):
// 1. the cast looks like this:autopy_reader =
py_reader_import_from_c(static_cast<uintptr_t>(reinterpret_cast<int64_t>(&c_stream)));
// 2. no need for `pybind11::reinterpret_steal<pybind11::object>(py_table)`, py_table should be returned directlyreturnpy_table;
If you use both of Apache Arrow C++ from vcpkg and pyarrow wheel from PyPI, you mix multiple Apache Arrow C++ libraries. It causes unexpected behavior such as a crash
Although I managed to make it to work, I am not entirely sure if this is the recommended approach:
Do you think if it helps if I keep pyarrow's version and Arrow C++ library version always consistent (for example, both using 10.0.0)?
If I use the official pyarrow (in Python) wheel and (pyarrow C++ library + Arrow C++ library, both compiled from vcpkg), is it any better than using the C data stream API? You said you mix multiple Apache Arrow C++ libraries and this could cause unexpected behavior, but it seems even if I don't use this approach, as long as I use pyarrow wheel in Python, I may run into such some unexpected problem, is it correct?
Kouhei Sutou / @kou:
I don't recommend the approach because the approach mixes 2 Apache Arrow C++ libraries.
Do you think if it helps if I keep pyarrow's version and Arrow C++ library version always consistent (for example, both using 10.0.0)?
It's better but I don't recommend mixing Apache Arrow C++ library in pyarrow's wheel and Apache Arrow C++ library from vcpkg.
If I use the official pyarrow (in Python) wheel and (pyarrow C++ library + Arrow C++ library, both compiled from vcpkg),
I don't recommend mixing them.
is it any better than using the C data stream API?
You can use simpler API. But it's not the biggest problem. You can use either API you like if you don't mix Apache Arrow C++ libraries.
You said you mix multiple Apache Arrow C++ libraries and this could cause unexpected behavior, but it seems even if I don't use this approach, as long as I use pyarrow wheel in Python, I may run into such some unexpected problem, is it correct?
Sorry. I couldn't understand this. Does it mean that you use only pyarrow wheel? (You don't mix 2 Apache Arrow C++ libraries?)
Activity
asfimport commentedon Jul 8, 2022
Joris Van den Bossche / @jorisvandenbossche:
Moving to the 10.0.0 milestone. Given that this is a large change, the goal is to merge this as soon as possible after the 9.0.0 release is cut, so we have the full 10.0.0 cycle to test and stabilize the change.
asfimport commentedon Aug 26, 2022
Kouhei Sutou / @kou:
Issue resolved by pull request 13311
#13311
asfimport commentedon Oct 5, 2022
Yue Ni / @niyue:
I have a project relying on
arrow/python/pyarrow.h, and I installed arrow from vcpkg https://github.com/microsoft/vcpkg/tree/master/ports/arrow. Previouslyarrow/python/pyarrow.his available inincludedirectory after installing arrow, but after upgrading to the latest version of arrow, more specifically commit ec579df, I am not able to findpyarrow.hinincludedirectory of vcpkg.I think it is probably caused by this change, do we have any recommendation what projects relying on
arrow/python/pyarrow.hshould do to overcome this issue?asfimport commentedon Oct 5, 2022
Alenka Frim / @AlenkaF:
Can you check if
arrow/python/pyarrow.his available inarrow/python/pyarrow/include/? And if it is, will you be able to include it from there?asfimport commentedon Oct 5, 2022
Joris Van den Bossche / @jorisvandenbossche:
The pyarrow.h file will indeed no longer be installed by installing Arrow C++ with vcpkg, but it will still be installed by the python package, only not in the standard "include" directory. For building against pyarrow, you can use
pyarrow.get_include()to get the location of those files, as is documented at https://arrow.apache.org/docs/dev/python/integration/extending.html#c-apiasfimport commentedon Oct 5, 2022
Yue Ni / @niyue:
@AlenkaF @jorisvandenbossche Thanks for the help.
Where is the header is expected to be installed? Is it expected to be installed by pyarrow's python wheel to some where and I have to add this path to my compiler's include path?
In my C++ project, I use vcpkg to manage dependency. And one of the module is a python binding for the C++ library, where I use pyarrow's C++ API like
arrow::py::wrap_tabletogether with pybind11 to create the python binding. Since I use vcpkg to manage dependency, I expect all C++ dependencies available via vcpkg.Previously pyarrow C++ is part of vcpkg arrow port, and I can use CMake's
find_library(arrow_python)to find the libraryand use
find_path(arrow/python/pyarrow.h)to find the path to the include directoryWhat I find for the latest arrow version:
the
libarrow_python.ais not built, even if I set ARROW_PYTHON CMake option toON.the
arrow/python/pyarrow.hcannot be found inincludedirectory after building the C++ library (at least using vcpkg arrow port)I went through most of the comments in PR for this issue [1] and read this ARROW_PYTHON option issue [2] as well, and the current behavior seems to be the expected behavior. The
pythondirectory will NOT be built even if ARROW_PYTHON=ON.I am not sure what the recommended approach for using pyarrow in C++. According to the document here [3], to make the build automated, it seems these are the steps:
install pyarrow package
launch python, run
pyarrow.get_include()to get theincludedirectoryadd the
includedirectory to compiler's include search path (probably via CMake)where is it expected to find the
libarrow_pythonso that CMake can find and use it for link?build
Is this the recommended approach for doing this? I am not quite sure step #4, any comments on this?
I can think of another approach, which is creating another vcpkg port like
arrow_pythonand build thearrow_pythonlibrary explicitly, so that projects can use this port for such purpose. Is this a recommended approach after this issue? Thanks.[1] #13311
[2]https://issues.apache.org/jira/browse/ARROW-17868
[3]Using pyarrow from C++ and Cython Code, https://arrow.apache.org/docs/dev/python/integration/extending.html#c-api
asfimport commentedon Oct 5, 2022
Kouhei Sutou / @kou:
Is your project an open source project? If so, could you share your code? I want to see an use case for
pyarrow.h.asfimport commentedon Oct 6, 2022
Yue Ni / @niyue:
@kou sorry it is not open source at this moment, but the binding itself is a just very thin wrapper for the C++ library. The code looks like this:
Here are the materials we referred to when we authored this binding initially (probably two years ago), we don't revisit if there is better approach since it is working for us until now.
[1] https://arrow.apache.org/docs/python/integration/extending.html#_CPPv4N5arrow5arrow2py10wrap_tableERKNSt10shared_ptrI5TableEE
[2] https://stackoverflow.com/questions/57863751/how-to-convert-pyarrow-table-to-arrow-table-when-interfacing-between-pyarrow-in
asfimport commentedon Oct 6, 2022
Alenka Frim / @AlenkaF:
I have created a new Jira issue that will add extra info on the Apache Arrow documentation on how to use functions mentioned above to find the header files and the libraries for PyArrow C++ API: https://issues.apache.org/jira/browse/ARROW-17950
asfimport commentedon Oct 6, 2022
Kouhei Sutou / @kou:
@niyue Thanks. How about using the C data interface / the C stream interface instead of
arrow/python/pyarrow.h?The C data interface and the C stream interface are included in Apache Arrow C++.
asfimport commentedon Oct 7, 2022
Yue Ni / @niyue:
@kou thanks so much. I will give it a try and get back to you later (probably not very soon because I will ask someone else for verifying the python part).
asfimport commentedon Oct 7, 2022
Yue Ni / @niyue:
@kou
Could you please briefly explain more how this works? I would like to confirm, do you mean the
pyarrow.ipc.RecordBatchReader._import_from_cis python code and not C++ code, and we could use pybind11 to execute the pyarrow python code to do this (probably using pybind11's API like https://pybind11.readthedocs.io/en/stable/advanced/embedding.html#executing-python-code)?asfimport commentedon Oct 7, 2022
Kouhei Sutou / @kou:
Yes,
pyarrow.ipc.RecordBatchReader._import_from_cis a Python code.Thanks for sharing the pybind11's API document. It seems that we can write the code like the following:
{ py::scoped_interpreter guard{}; py::module_ pyarrow = py::module_::import("pyarrow"); // We can use auto py::object py_reader_import_from_c = pyarrow.attr("ipc").attr("RecordBatchReader").attr("_import_from_c") // uintptr_t isn't listed here: https://pybind11.readthedocs.io/en/stable/advanced/cast/overview.html#list-of-all-builtin-conversions py::object py_reader = py_reader_import_from_c(static_cast<uint64_t>(static_cast<uintptr_t>(&c_stream))); py::object py_table = pyarrow.attr("Table").attr("from_batches")(py_reader); return pybind11::reinterpret_steal<pybind11::object>(py_table); }asfimport commentedon Nov 10, 2022
Yue Ni / @niyue:
@kou I follow your instruction (with some slight modification) to make it compiled and running but ran into some segfault during importing
pyarrowmodule.1) Since I create a python binding, I use this python binding in my Python code.
py::scoped_interpreter guard{}seems not necessary because that will embed another interpreter, otherwise, pybind will reportthe interpreter is already runningerror. This is the only thing I revised.2) In my python code:
Some error like below is reported:
The line 65 of
pyarrow/\_\_init\_\_.pyis:{}3) a core dump was generated{}, and I use gdb to briefly view the core dump, it looks like this:
It seems both my python binding and pyarrow uses arrow's code to do some initialization, the
arrow::io::internal::GetIOThreadPoolfor some reason fails to work in this case and caused the segfault.{}4) Since it is related with
::arrow::internal::GetEnvVar("ARROW_IO_THREADS");{}, I tried setting theARROW_IO_THREADSenv var to a number and gave it another try. And the program still caused a segfault, but reporting a different error (there is nofree(): invalid pointerprinted this time):{}5) I used gdb to view the core dump again{}, and it reported error like below:
It is different with previously core dump, but I have no idea how this happens now. And the behavior is pretty consistent, if I unset the
ARROW_IO_THREADSenv var and run the program again, the previous core dump will be generated again.Summary:
I don't know how to proceed any more. Basically:
I create a C++ library with arrow C++ library
I create a python binding for the C++ library using pybind
Instead of importing pyarrow.h header, I now import <arrow/c/abi.h> and <arrow/c/bridge.h> for bridging
In my python program, I use both my python binding and pyarrow wheel
Could you please shed some light on this issue? Do you think if this kind usage is supported or if it is some bug for arrow project?
asfimport commentedon Nov 11, 2022
Kouhei Sutou / @kou:
Does it mean that you use Apache Arrow C++ from vcpkg and pyarrow wheel from PyPI?
If so, you should not use Apache Arrow C++ from vcpkg. Could you use https://arrow.apache.org/docs/dev/python/integration/extending.html#c-api instead (use
pyarrow.get_include()/pyarrow.get_libraries()/pyarrow.get_library_dirs()/arrow/python/pyarrow.hinstead offind_package(Arrow)/<arrow/c/abi.h>/<arrow/c/bridge.h>)?asfimport commentedon Nov 11, 2022
Yue Ni / @niyue:
Almost. I use Apache Arrow C++ from vcpkg (but I don't use the latest version of Arrow in vcpkg, instead, I use a fork of it with some gandiva related modification, and use a custom vcpkg port to manage the arrow dependency).
Could you briefly explain why this should not be done this way?
asfimport commentedon Nov 11, 2022
Kouhei Sutou / @kou:
Because pyarrow wheel includes pre-built Apache Arrow C++ library. If you use both of Apache Arrow C++ from vcpkg and pyarrow wheel from PyPI, you mix multiple Apache Arrow C++ libraries. It causes unexpected behavior such as a crash.
asfimport commentedon Nov 20, 2022
Yue Ni / @niyue:
@kou thanks for the pointer. After some more experiments, I managed to make it to work, the two minor changes are (compiler doesn't help on either of them):
Do you think if it helps if I keep pyarrow's version and Arrow C++ library version always consistent (for example, both using 10.0.0)?
If I use the official pyarrow (in Python) wheel and (pyarrow C++ library + Arrow C++ library, both compiled from vcpkg), is it any better than using the C data stream API? You said
you mix multiple Apache Arrow C++ librariesand this could cause unexpected behavior, but it seems even if I don't use this approach, as long as I use pyarrow wheel in Python, I may run into such some unexpected problem, is it correct?asfimport commentedon Nov 22, 2022
Kouhei Sutou / @kou:
I don't recommend the approach because the approach mixes 2 Apache Arrow C++ libraries.
It's better but I don't recommend mixing Apache Arrow C++ library in pyarrow's wheel and Apache Arrow C++ library from vcpkg.
I don't recommend mixing them.
You can use simpler API. But it's not the biggest problem. You can use either API you like if you don't mix Apache Arrow C++ libraries.
Sorry. I couldn't understand this. Does it mean that you use only pyarrow wheel? (You don't mix 2 Apache Arrow C++ libraries?)