Repository navigation
Read only the requested columns from remote Parquet files when columns is set - #9164
behroozazarkhalili wants to merge 1 commit into
Conversation
… columns is set load_dataset(..., columns=[...]) without streaming downloaded every Parquet file in full before dropping the other columns. Remote files are now opened with xopen so only the requested columns are fetched. Files already in the Hub cache are read locally, and loads without columns or in offline mode keep the download.
|
Note on CI: on Python 3.10 the new tests in |
|
I found a separate configuration propagation regression on 774f603 while reviewing the new _open path (code-analysis finding, reproduced offline). Passing filesystem options through load_dataset(..., download_config=DownloadConfig(storage_options=...)) works without columns, but fails with columns=["text"]. _split_generators has the effective DownloadConfig, while _open constructs a fresh one from self.storage_options/self.token and loses the explicitly supplied options. With the existing mockfs fixture the projected load raises TypeError: MockFileSystem.init() missing ... 'local_root_dir'. This is distinct from the Python 3.10 xPath issue: reproduced on Windows/Python 3.12.14. One narrow fix is to retain the active manager configuration when splitting and use it for opens: @@
dl_manager.download_config.extract_on_the_fly = True
+ self._parquet_download_config = dl_manager.download_config
@@
- download_config = DownloadConfig(storage_options=self.storage_options, token=self.token)
- return xopen(file, "rb", download_config=download_config)
+ return xopen(file, "rb", download_config=self._parquet_download_config)Regression addition to tests/packaged_modules/test_parquet.py (existing imports/fixture): @pytest.mark.parametrize("columns", [None, ["text"]])
@pytest.mark.parametrize("num_proc", [None, 2])
def test_parquet_download_config_storage_options(remote_parquet_url, mockfs, tmp_path, columns, num_proc):
result = load_dataset(
"parquet",
data_files=remote_parquet_url,
columns=columns,
num_proc=num_proc,
split="train",
download_config=DownloadConfig(storage_options=mockfs.storage_options),
cache_dir=str(tmp_path / "cache"),
)
assert result["text"][:] == ["a", "b", "c"]Before: 2 projected cases fail, 2 full-load controls pass. After: all 16 tests in the Parquet module pass, including the existing streaming/local/cache/offline tests; Ruff check and format pass. Tested with PyArrow 25.0.1 and short local basetemp. No live S3/Hub authentication, network-byte savings, Python 3.10, or full-suite claim. AI disclosure: Codex identified, implemented, and tested this under @JonnyFaeth's standing authorization; Jonathan has not personally reviewed this run. |
Closes #4114.
What changes
load_dataset(..., columns=[...])withoutstreaming=Truecurrently downloads every Parquet file in full and then keeps the requested columns. With this PR, whencolumnsis set, remote Parquet files are opened withxopenand only the requested columns are read with range requests, as in streaming mode. The result is written to the Arrow cache as before.This is a behavior change, so here is what stays the same and what does not:
columns, nothing changes: files are downloaded as before.HF_HUB_OFFLINE=1), nothing changes: files come from the download cache.try_to_load_from_cache).columns, the Parquet files are no longer left in the download cache, so a later load with other columns reads from the remote again.The existing answer on the issue is to use
streaming=True. I am opening this because the issue asks for the non-streaming case; feel free to close it if you prefer to keep the download.Check on a Hub dataset
load_dataset("cornell-movie-review-data/rotten_tomatoes", columns=["label"], split="train")with an emptyHF_HOME:maintrain.parquet,validation.parquet,test.parquetThe
labelcolumn is equal to the one of a full load in both cases, and after a full load,columns=["text"]reads the cached files.Tests
tests/packaged_modules/test_parquet.py: remote read without download (num_procunset and 2), local copy used when cached, download kept withoutcolumnsand in offline mode, and_get_cached_filepath resolution.