-
-
Notifications
You must be signed in to change notification settings - Fork 749
Add custom serialization support for pyarrow #2115
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c0f842d
Update .gitignore
6b6af6c
Add custom serialization support for pyarrow
be5ce71
Add some serialization tests for arrow
9870b07
yield scatter
mrocklin 911f71a
Fix tests
0c4da1d
Cleanup imports
af4eb31
remove to_pybytes in arrow serialization
mrocklin 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,3 +6,5 @@ docs/build | |
| continuous_integration/hdfs-initialized | ||
| .cache | ||
| .#* | ||
| .idea/ | ||
| .pytest_cache/ | ||
This file contains hidden or 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 hidden or 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,53 @@ | ||
| from __future__ import print_function, division, absolute_import | ||
|
|
||
| from .serialize import register_serialization | ||
|
|
||
|
|
||
| def serialize_batch(batch): | ||
| import pyarrow as pa | ||
| sink = pa.BufferOutputStream() | ||
| writer = pa.RecordBatchStreamWriter(sink, batch.schema) | ||
| writer.write_batch(batch) | ||
| writer.close() | ||
| buf = sink.get_result() | ||
| header = {} | ||
| frames = [buf] | ||
| return header, frames | ||
|
|
||
|
|
||
| def deserialize_batch(header, frames): | ||
| import pyarrow as pa | ||
| blob = frames[0] | ||
| reader = pa.RecordBatchStreamReader(pa.BufferReader(blob)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opened ARROW-2859 to see if we can get rid of this |
||
| return reader.read_next_batch() | ||
|
|
||
|
|
||
| def serialize_table(tbl): | ||
| import pyarrow as pa | ||
| sink = pa.BufferOutputStream() | ||
| writer = pa.RecordBatchStreamWriter(sink, tbl.schema) | ||
| writer.write_table(tbl) | ||
| writer.close() | ||
| buf = sink.get_result() | ||
| header = {} | ||
| frames = [buf] | ||
| return header, frames | ||
|
|
||
|
|
||
| def deserialize_table(header, frames): | ||
| import pyarrow as pa | ||
| blob = frames[0] | ||
| reader = pa.RecordBatchStreamReader(pa.BufferReader(blob)) | ||
| return reader.read_all() | ||
|
|
||
|
|
||
| register_serialization( | ||
| 'pyarrow.lib.RecordBatch', | ||
| serialize_batch, | ||
| deserialize_batch | ||
| ) | ||
| register_serialization( | ||
| 'pyarrow.lib.Table', | ||
| serialize_table, | ||
| deserialize_table | ||
| ) | ||
This file contains hidden or 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,44 @@ | ||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| pa = pytest.importorskip('pyarrow') | ||
|
|
||
| from distributed.utils_test import gen_cluster | ||
| from distributed.protocol import deserialize, serialize | ||
| from distributed.protocol.serialize import class_serializers, typename | ||
|
|
||
|
|
||
| df = pd.DataFrame({'A': list('abc'), 'B': [1,2,3]}) | ||
| tbl = pa.Table.from_pandas(df, preserve_index=False) | ||
| batch = pa.RecordBatch.from_pandas(df, preserve_index=False) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('obj', [batch, tbl], ids=["RecordBatch", "Table"]) | ||
| def test_roundtrip(obj): | ||
| # Test that the serialize/deserialize functions actually | ||
| # work independent of distributed | ||
| header, frames = serialize(obj) | ||
| new_obj = deserialize(header, frames) | ||
| assert obj.equals(new_obj) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('obj', [batch, tbl], ids=["RecordBatch", "Table"]) | ||
| def test_typename(obj): | ||
| # The typename used to register the custom serialization is hardcoded | ||
| # ensure that the typename hasn't changed | ||
| assert typename(type(obj)) in class_serializers | ||
|
|
||
|
|
||
| def echo(arg): | ||
| return arg | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('obj', [batch, tbl], ids=["RecordBatch", "Table"]) | ||
| def test_scatter(obj): | ||
| @gen_cluster(client=True) | ||
| def run_test(client, scheduler, worker1, worker2): | ||
| obj_fut = yield client.scatter(obj) | ||
| fut = client.submit(echo, obj_fut) | ||
| result = yield fut | ||
| assert obj.equals(result) | ||
| run_test() |
This file contains hidden or 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
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.
One improvement on the arrow side would be if
RecordBatchStreamWriterwas a context manager as that would avoid the need for an explicit close.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.
A fair point. ARROW-2863