-
Notifications
You must be signed in to change notification settings - Fork 155
Allow pickling PyExpr #1517
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
Open
ntjohnson1
wants to merge
9
commits into
apache:main
Choose a base branch
from
rerun-io:nick/pickle_expr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Allow pickling PyExpr #1517
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
23543fc
feat: allow replacing the global SessionContext
ntjohnson1 5796b53
feat: pickle/dill support for Expr
ntjohnson1 07ffa21
Add multiprocessing test since that was motivating factor
ntjohnson1 106ea3c
ci: cap test-matrix at 30 minutes to prevent 6-hour hangs
ntjohnson1 bdeedd7
test: move pickle/multiprocessing helpers into a non-test module
ntjohnson1 31db513
Fix multiprocessing table name collision
ntjohnson1 6662c0f
Diagnosing on CI since I can't repro locally even on my linux box
ntjohnson1 0c77277
Local dev adds .pyth file that ci installation from wheel doesn't hav…
ntjohnson1 f3bc684
Discovery of the tests module is very strange. Proving that is the ro…
ntjohnson1 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
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
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
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
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
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,85 @@ | ||
| # 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. | ||
|
|
||
| """Helpers for :mod:`test_pickle_multiprocessing`. | ||
|
|
||
| Spawn workers re-import the module that defines a pickled function by the | ||
| function's ``__module__`` attribute. Pytest's ``--import-mode=importlib`` | ||
| loads test modules under synthetic names that the worker cannot resolve via | ||
| the normal import machinery, which can cause ``Pool.map`` to hang waiting | ||
| for a worker that died during unpickling. | ||
|
|
||
| Keeping the helpers in this regular (non-test) module side-steps that: it | ||
| is importable under its real dotted name (``tests._pickle_multiprocessing_helpers``) | ||
| in both parent and worker, and the leading underscore keeps pytest from | ||
| collecting it as a test module. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pyarrow as pa | ||
| import pyarrow.compute as pc | ||
| from datafusion import SessionContext, udf | ||
|
|
||
| UDF_NAME = "mp_pickle_add_ten" | ||
|
|
||
|
|
||
| def add_ten_impl(array: pa.Array) -> pa.Array: | ||
| return pc.add(array, 10) | ||
|
|
||
|
|
||
| def build_add_ten_udf(): | ||
| return udf( | ||
| add_ten_impl, | ||
| [pa.int64()], | ||
| pa.int64(), | ||
| volatility="immutable", | ||
| name=UDF_NAME, | ||
| ) | ||
|
|
||
|
|
||
| def register_udf_on_global_ctx() -> None: | ||
| """Pool initializer: install a global ctx in the worker that knows the UDF. | ||
|
|
||
| ``Expr.__setstate__`` resolves UDF references by name against the | ||
| *global* context, so the registration must happen before any task arg is | ||
| unpickled — i.e. in the Pool's ``initializer``, not in the task body. | ||
| """ | ||
| ctx = SessionContext() | ||
| ctx.register_udf(build_add_ten_udf()) | ||
| ctx.set_as_global() | ||
|
|
||
|
|
||
| def apply_builtin_expr(args: tuple) -> list: | ||
| expr, values = args | ||
| ctx = SessionContext() | ||
| batch = pa.RecordBatch.from_arrays([pa.array(values, type=pa.int64())], names=["a"]) | ||
| df = ctx.create_dataframe([[batch]], name="t") | ||
| return df.select(expr.alias("out")).collect()[0].column(0).to_pylist() | ||
|
|
||
|
|
||
| def apply_udf_expr(args: tuple) -> list: | ||
| expr, values = args | ||
| # Reuse the worker's global ctx so the UDF registered by the initializer | ||
| # is visible during execution as well as during arg unpickling. Omit the | ||
| # table name so each call gets a fresh auto-generated one — a worker may | ||
| # process multiple tasks, and reusing a fixed name on the shared ctx would | ||
| # collide on the second call. | ||
| ctx = SessionContext.global_ctx() | ||
| batch = pa.RecordBatch.from_arrays([pa.array(values, type=pa.int64())], names=["a"]) | ||
| df = ctx.create_dataframe([[batch]]) | ||
| return df.select(expr.alias("out")).collect()[0].column(0).to_pylist() |
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.
Right now things pass in 4 minutes but I accidentally committed a change that deadlocked and would have run for 6 hours if I didn't push new commits. Seems like a nice guard to footguns