Skip to content

Read only the requested columns from remote Parquet files when columns is set - #9164

Open
behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:feat/4114-parquet-columns-partial-read
Open

behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:feat/4114-parquet-columns-partial-read

Conversation

@behroozazarkhalili

Copy link
Copy Markdown
Contributor

Closes #4114.

What changes

load_dataset(..., columns=[...]) without streaming=True currently downloads every Parquet file in full and then keeps the requested columns. With this PR, when columns is set, remote Parquet files are opened with xopen and 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:

  • Without columns, nothing changes: files are downloaded as before.
  • In offline mode (HF_HUB_OFFLINE=1), nothing changes: files come from the download cache.
  • A file of a Hub repository that is already in the Hub cache is read from the local copy (try_to_load_from_cache).
  • Local files are unchanged.
  • New: with 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 empty HF_HOME:

Parquet files in the Hub cache after the load
main train.parquet, validation.parquet, test.parquet
this PR none

The label column 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_proc unset and 2), local copy used when cached, download kept without columns and in offline mode, and _get_cached_file path resolution.

… 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.
@behroozazarkhalili

Copy link
Copy Markdown
Contributor Author

Note on CI: on Python 3.10 the new tests in tests/packaged_modules/test_parquet.py fail with TypeError: object.__init__() takes exactly one argument (the instance to initialize). This is the xPath.__init__ error that currently fails the Python 3.10 jobs on main and that #9119 fixes. On Python 3.14 all the tests of the file pass.

@JonnyFaeth

JonnyFaeth commented Nov 25, 2026 •

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow downloading just some columns of a dataset

2 participants