-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PyCapsule support for Arrow import and export #825
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4a93356
Support Arrow PyCapsule for reading arrow tables and for exporting da…
timsaucer d61621c
Improve flow control on receiving record batches from arrow pycapsule
timsaucer a813dc9
Update code comment
timsaucer 533c915
Add function to project record batch into desired schema
timsaucer d94885c
Validate the pycapsule and get the pointer when successful
timsaucer 932c8c2
Add unit test for export via pycapsule
timsaucer a404a68
Readability
timsaucer 1ed4632
Remove nanoarrow so we don't add another dependency that isn't gainin…
timsaucer d1fe4df
Remove unused import
timsaucer 61473df
Add unit test to check for reading from record batch streams
timsaucer 0bbb293
Update unit test to try a variety of arrow data sources on import
timsaucer 2586b3f
Simplify dataframe creation in unit test
timsaucer 43a869b
Update user documenation to include usage of import and exporting arr…
timsaucer 06e16fb
Rename from_arrow_table to from_arrow since we now support multiple t…
timsaucer afe6420
Add admonition box to warn user
timsaucer a5b4e39
Mark from_arrow_table as deprecated
timsaucer 7cc807d
Clean up error handling
timsaucer babeb77
Marking was not required
timsaucer 955d7d5
Apply linting after rebase
timsaucer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
.. 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. | ||
|
||
Arrow | ||
===== | ||
|
||
DataFusion implements the | ||
`Apache Arrow PyCapsule interface <https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html>`_ | ||
for importing and exporting DataFrames with zero copy. With this feature, any Python | ||
project that implements this interface can share data back and forth with DataFusion | ||
with zero copy. | ||
|
||
We can demonstrate using `pyarrow <https://arrow.apache.org/docs/python/index.html>`_. | ||
|
||
Importing to DataFusion | ||
----------------------- | ||
|
||
Here we will create an Arrow table and import it to DataFusion. | ||
|
||
To import an Arrow table, use :py:func:`datafusion.context.SessionContext.from_arrow`. | ||
This will accept any Python object that implements | ||
`__arrow_c_stream__ <https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html#arrowstream-export>`_ | ||
or `__arrow_c_array__ <https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html#arrowarray-export>`_ | ||
and returns a ``StructArray``. Common pyarrow sources you can use are: | ||
|
||
- `Array <https://arrow.apache.org/docs/python/generated/pyarrow.Array.html>`_ (but it must return a Struct Array) | ||
- `Record Batch <https://arrow.apache.org/docs/python/generated/pyarrow.RecordBatch.html>`_ | ||
- `Record Batch Reader <https://arrow.apache.org/docs/python/generated/pyarrow.RecordBatchReader.html>`_ | ||
- `Table <https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`_ | ||
|
||
.. ipython:: python | ||
|
||
from datafusion import SessionContext | ||
import pyarrow as pa | ||
|
||
data = {"a": [1, 2, 3], "b": [4, 5, 6]} | ||
table = pa.Table.from_pydict(data) | ||
|
||
ctx = SessionContext() | ||
df = ctx.from_arrow(table) | ||
df | ||
|
||
Exporting from DataFusion | ||
------------------------- | ||
|
||
DataFusion DataFrames implement ``__arrow_c_stream__`` PyCapsule interface, so any | ||
Python library that accepts these can import a DataFusion DataFrame directly. | ||
|
||
.. warning:: | ||
It is important to note that this will cause the DataFrame execution to happen, which may be | ||
a time consuming task. That is, you will cause a | ||
:py:func:`datafusion.dataframe.DataFrame.collect` operation call to occur. | ||
|
||
|
||
.. ipython:: python | ||
|
||
df = df.select((col("a") * lit(1.5)).alias("c"), lit("df").alias("d")) | ||
pa.table(df) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,8 @@ IO | |
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
arrow | ||
avro | ||
csv | ||
parquet | ||
json | ||
avro | ||
|
||
parquet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For both they must emit a struct array. Any Arrow array can be passed through an
__arrow_c_stream__
. Canonically, to transfer a DataFrame you have a stream of struct arrays where each one is unpacked to be the columns of a RecordBatch. But it doesn't have to a struct array: you can also transfer aSeries
through an__arrow_c_stream__
, where each batch in the stream iterator is just a primitive array.