-
Notifications
You must be signed in to change notification settings - Fork 131
Freeze PyO3 wrappers & introduce interior mutability to avoid PyO3 borrow errors #1253
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 22 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
92894b5
Refactor schema, config, dataframe, and expression classes to use RwL…
kosiew 7030cec
Add error handling to CaseBuilder methods to preserve builder state
kosiew dba5c6a
Refactor to use parking_lot for interior mutability in schema, config…
kosiew cfc9f2c
Add concurrency tests for SqlSchema, Config, and DataFrame
kosiew 03a1022
Add tests for CaseBuilder to ensure builder state is preserved on suc…
kosiew d6cdfe3
Add test for independent handles in CaseBuilder to verify behavior
kosiew 1755937
Fix CaseBuilder to preserve state correctly in when() method
kosiew b6ce4ae
Refactor to use named constant for boolean literals in test_expr.py
kosiew fd504a1
fix ruff errors
kosiew 4b01772
Refactor to introduce type aliases for cached batches in dataframe.rs
kosiew 1c73410
Cherry pick from #1252
ntjohnson1 d5914c2
Add most expr - cherry pick from #1252
ntjohnson1 fe3ad12
Add source root - cherry pick #1252
ntjohnson1 509850e
Fix license comment formatting in config.rs
kosiew c95e8b1
Refactor caching logic to use a local variable for IPython environmen…
kosiew 799e8fb
Add test for ensuring exposed pyclasses default to frozen
kosiew 6de60bc
Add PyO3 class mutability guidelines reference to contributor guide
kosiew b213bd4
Mark boolean expression classes as frozen for immutability
kosiew 64faca2
Refactor PyCaseBuilder methods to eliminate redundant take/store logic
kosiew 5caec09
Refactor PyConfig methods to improve readability by encapsulating con…
kosiew a905154
Resolve patch apply conflicts for CaseBuilder concurrency improvements
kosiew 428839d
Resolve Config optimization conflicts for improved read/write concurr…
kosiew 34a6078
Refactor PyConfig get methods for improved readability and performance
kosiew 09d9ab8
Refactor test_expr.py to replace positional boolean literals with nam…
kosiew 2df2f5f
fix ruff errors
kosiew 2c76271
Add license header to test_pyclass_frozen.py for compliance
kosiew 8a52e23
Alternate approach to case expression
timsaucer 1b97b41
Replace case builter with keeping the expressions and then applying a…
timsaucer fc27bd5
Update unit tests
timsaucer d247d64
Refactor case and when functions to utilize PyCaseBuilder for improve…
kosiew 9536e02
Update src/expr/conditional_expr.rs
timsaucer 6e384ba
Merge remote-tracking branch 'timsaucer/tsaucer/case_expr_clone_worka…
kosiew 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,125 @@ | ||
# 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. | ||
|
||
from __future__ import annotations | ||
|
||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
import pyarrow as pa | ||
from datafusion import Config, SessionContext, col, lit | ||
from datafusion import functions as f | ||
from datafusion.common import SqlSchema | ||
|
||
|
||
def _run_in_threads(fn, count: int = 8) -> None: | ||
with ThreadPoolExecutor(max_workers=count) as executor: | ||
futures = [executor.submit(fn, i) for i in range(count)] | ||
for future in futures: | ||
# Propagate any exception raised in the worker thread. | ||
future.result() | ||
|
||
|
||
def test_concurrent_access_to_shared_structures() -> None: | ||
"""Exercise SqlSchema, Config, and DataFrame concurrently.""" | ||
|
||
schema = SqlSchema("concurrency") | ||
config = Config() | ||
ctx = SessionContext() | ||
|
||
batch = pa.record_batch([pa.array([1, 2, 3], type=pa.int32())], names=["value"]) | ||
df = ctx.create_dataframe([[batch]]) | ||
|
||
config_key = "datafusion.execution.batch_size" | ||
expected_rows = batch.num_rows | ||
|
||
def worker(index: int) -> None: | ||
schema.name = f"concurrency-{index}" | ||
assert schema.name.startswith("concurrency-") | ||
# Exercise getters that use internal locks. | ||
assert isinstance(schema.tables, list) | ||
assert isinstance(schema.views, list) | ||
assert isinstance(schema.functions, list) | ||
|
||
config.set(config_key, str(1024 + index)) | ||
assert config.get(config_key) is not None | ||
# Access the full config map to stress lock usage. | ||
assert config_key in config.get_all() | ||
|
||
batches = df.collect() | ||
assert sum(batch.num_rows for batch in batches) == expected_rows | ||
|
||
_run_in_threads(worker, count=12) | ||
|
||
|
||
def test_config_set_during_get_all() -> None: | ||
"""Ensure config writes proceed while another thread reads all entries.""" | ||
|
||
config = Config() | ||
key = "datafusion.execution.batch_size" | ||
|
||
def reader() -> None: | ||
for _ in range(200): | ||
# get_all should not hold the lock while converting to Python objects | ||
config.get_all() | ||
|
||
def writer() -> None: | ||
for index in range(200): | ||
config.set(key, str(1024 + index)) | ||
|
||
with ThreadPoolExecutor(max_workers=2) as executor: | ||
reader_future = executor.submit(reader) | ||
writer_future = executor.submit(writer) | ||
reader_future.result(timeout=10) | ||
writer_future.result(timeout=10) | ||
|
||
assert config.get(key) is not None | ||
|
||
|
||
def test_case_builder_reuse_from_multiple_threads() -> None: | ||
"""Ensure the case builder can be safely reused across threads.""" | ||
|
||
ctx = SessionContext() | ||
values = pa.array([0, 1, 2, 3, 4], type=pa.int32()) | ||
df = ctx.create_dataframe([[pa.record_batch([values], names=["value"])]]) | ||
|
||
base_builder = f.case(col("value")) | ||
|
||
def add_case(i: int) -> None: | ||
base_builder.when(lit(i), lit(f"value-{i}")) | ||
|
||
_run_in_threads(add_case, count=8) | ||
|
||
with ThreadPoolExecutor(max_workers=2) as executor: | ||
otherwise_future = executor.submit(base_builder.otherwise, lit("default")) | ||
case_expr = otherwise_future.result() | ||
|
||
result = df.select(case_expr.alias("label")).collect() | ||
assert sum(batch.num_rows for batch in result) == len(values) | ||
|
||
predicate_builder = f.when(col("value") == lit(0), lit("zero")) | ||
|
||
def add_predicate(i: int) -> None: | ||
predicate_builder.when(col("value") == lit(i + 1), lit(f"value-{i + 1}")) | ||
|
||
_run_in_threads(add_predicate, count=4) | ||
|
||
with ThreadPoolExecutor(max_workers=2) as executor: | ||
end_future = executor.submit(predicate_builder.end) | ||
predicate_expr = end_future.result() | ||
|
||
result = df.select(predicate_expr.alias("label")).collect() | ||
assert sum(batch.num_rows for batch in result) == len(values) |
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
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.
I think this reduces code readability. Instead we can whitelist functions like
lit
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.
https://docs.astral.sh/ruff/settings/#lint_flake8-boolean-trap_extend-allowed-calls
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.
I'll implement this.