Add object_store parameter to register/read file methods for thread-safe credentials - #1625
Add object_store parameter to register/read file methods for thread-safe credentials#1625qzyu999 wants to merge 2 commits into
Conversation
|
Hi @timsaucer, this PR would help improve the efficiency of some operations related apache/iceberg-python#3554, PTAL and LMK if you have any questions, thanks! |
Add an optional object_store parameter to register_parquet, read_parquet, register_csv, read_csv, register_json, read_json, register_avro, read_avro, register_arrow, and read_arrow methods. When provided, the store is automatically registered for the URL scheme and host parsed from the path, removing the need to manually call register_object_store separately. This enables thread-safe cloud credential handling without os.environ mutation. Includes unit tests (mock-based) for URL parsing logic and integration tests using LocalFileSystem for end-to-end validation. Closes apache#1624
81d33cb to
b225b3a
Compare
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Adds an object_store keyword parameter to Python SessionContext file-based register/read methods so callers can pass a preconfigured object store (thread-safe credentials) and have it automatically registered based on the URL in path.
Changes:
- Added
_register_object_store_for_path()helper usingurllib.parse.urlparseand wired it intoregister_*/read_*methods via a newobject_storeparameter. - Expanded docstrings for Parquet register/read to document the new parameter (with examples).
- Added a new Python test module covering URL parsing, delegation to
register_object_store, and end-to-end usage withLocalFileSystem.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| python/datafusion/context.py | Introduces _register_object_store_for_path() and adds object_store param plumbing + documentation to file-based register/read APIs. |
| python/tests/test_object_store_param.py | New tests for URL parsing + registration delegation + local end-to-end flow. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| parsed = urlparse(str(path)) | ||
| if not parsed.scheme or not parsed.netloc: | ||
| msg = ( | ||
| f"Cannot determine object store URL from path {path!r}. " | ||
| "The path must use a URL scheme (e.g. 's3://bucket/key')." | ||
| ) | ||
| raise ValueError(msg) | ||
| scheme = f"{parsed.scheme}://" | ||
| host = parsed.netloc | ||
| self.register_object_store(scheme, store, host=host) |
| file_url = f"file://{tmp_path}/test.parquet" | ||
|
|
||
| ctx.register_parquet("test_tbl", file_url, object_store=store) |
| def _register_object_store_for_path( | ||
| self, path: str | pathlib.Path, store: Any | ||
| ) -> None: | ||
| """Parse a URL path and register the given object store for its scheme and host. |
| Raises: | ||
| ValueError: If the path does not contain a recognized URL scheme. |
Which issue does this PR close?
Closes #1624.
Rationale for this change
When using datafusion-python as a compute backend from multi-threaded Python applications (e.g., PyIceberg), cloud credentials must currently be set via
os.environbefore callingregister_parquet(). Sinceos.environis process-global, concurrent threads mutating it causes credential cross-contamination, requiring a global lock that serializes all I/O operations and negates any parallelism benefit.The existing
register_object_store()method already solves thread-safety, but requires callers to manually parse bucket names from paths, determine the correct URL scheme, and make a separate call beforeregister_parquet. This PR adds a convenienceobject_storeparameter that handles all of this internally.What changes are included in this PR?
New private helper
_register_object_store_for_path(path, store)parses a URL path viaurllib.parse.urlparseto extract scheme and host, then callsregister_object_store()internally.New
object_storeparameter added to all file-based register/read methods:register_parquet/read_parquetregister_csv/read_csvregister_json/read_jsonregister_avro/read_avroregister_arrow/read_arrowTest suite (
python/tests/test_object_store_param.py) with 23 tests:register_object_storeLocalFileSystemwith real Parquet filesAll changes are purely in the Python layer no Rust code modified.
Are there any user-facing changes?
Yes. All file-based
register_*andread_*methods onSessionContextnow accept an optionalobject_storekeyword argument. When provided with a pre-configured store instance (e.g.,AmazonS3,GoogleCloud,MicrosoftAzure), the store is automatically registered for the URL parsed from the path.Example usage:
This is a backward-compatible addition (the parameter defaults to
Noneand existing call sites are unaffected).