Skip to content

GH-55752: [Python] Support incremental file listing with an iterator - #55753

Open
dragongu wants to merge 1 commit into
apache:mainfrom
dragongu:python-file-info-generator
Open

dragongu wants to merge 1 commit into
apache:mainfrom
dragongu:python-file-info-generator

Conversation

@dragongu

Copy link
Copy Markdown

Rationale for this change

FileSystem.get_file_info(selector) materializes the entire listing before returning. The primary use case is processing very large S3 prefixes through PyArrow's native S3FileSystem: callers should be able to begin computing on objects from the first listing page while subsequent pages are still being fetched, rather than waiting for the complete listing.

Arrow C++ already exposes FileSystem::GetFileInfoGenerator(). This PR binds that existing functionality to a synchronous Python iterator without changing the C++ filesystem backends or the existing get_file_info() API.

What changes are included in this PR?

  • Add FileSystem.get_file_info_iterator(selector) and the public pyarrow.fs.FileInfoIterator type. The iterator fetches results in batches internally but yields individual FileInfo objects.
  • Defer discovery until the first next() call. Creating, closing, or deleting an unused iterator does not start listing.
  • Keep the filesystem alive during iteration and release the underlying generator and batch buffer on exhaustion, error, or explicit close(). Closing is idempotent.
  • Reject overlapping next() and close() calls on the same iterator with ValueError, since the underlying C++ generator is not async-reentrant.
  • Add API documentation and tests for result equivalence, recursive and non-recursive selectors, lazy startup, iteration across batch boundaries, early termination, resource lifetime, handler errors, invalid selectors, subtree paths, and concurrent operation rejection.

Incremental discovery depends on the backend. Native LocalFileSystem and S3FileSystem expose batches as discovery progresses; SubTreeFileSystem delegates to its underlying filesystem. GcsFileSystem, AzureFileSystem, HadoopFileSystem, and PyFileSystem (including the fsspec integration) retain the C++ fallback, which materializes the full selection on the first next() call before yielding entries from memory. This PR does not add streaming implementations for those backends.

Closing an iterator releases its resources but does not guarantee cancellation of work already started by the backend. In particular, this PR does not promise that early termination cancels S3 pagination requests or that backend prefetch memory is strictly bounded.

Are these changes tested?

Yes. The standard CMake _fs target was rebuilt and the complete python/pyarrow/tests/test_fs.py suite was run against the new extension: 191 passed, 406 skipped, 8 xfailed, with exit code 0. The expected failures are existing MockFileSystem serialization limitations.

The basic listing and close tests use the existing 13 filesystem configurations, allowing enabled CI environments to exercise the new API on their respective backends. The local environment did not have S3, GCS, Azure, or HDFS enabled, and fsspec configurations were also skipped because the dependency was unavailable. Consequently, S3 pagination behavior was confirmed by source inspection but has not been exercised locally; remote-backend validation remains for CI.

Local lint checks passed: flake8, cython-lint, clang-format 18.1.8, numpydoc validation using the project's configured checks, sphinx-lint, and git diff --check. Wheel/sdist builds, a full documentation build, and performance benchmarks were not run.

Are there any user-facing changes?

Yes. This adds a synchronous, lazy, flat iterator API. Existing get_file_info() behavior is unchanged, and this PR introduces no breaking changes to existing public APIs.

import pyarrow.fs as pafs

s3 = pafs.S3FileSystem()
selector = pafs.FileSelector("bucket/large-prefix", recursive=True)
iterator = s3.get_file_info_iterator(selector)
try:
    for info in iterator:
        process(info)
finally:
    iterator.close()

Each item is a FileInfo, not a list or a batch. Entry order is not guaranteed. When stopping early, callers should explicitly call close(); exhausting the iterator releases resources automatically. True incremental discovery is currently available through native LocalFileSystem/S3FileSystem and wrappers that preserve their generator implementation, not through the fsspec S3 integration.

…rator

Expose the C++ file discovery generator as a lazy, flat Python iterator. Fetch entries in batches internally and yield FileInfo objects individually. Start discovery on the first next() call and release resources on exhaustion, error, or close(). Reject concurrent next()/close() calls.

LocalFileSystem and native S3FileSystem expose discovery results incrementally, allowing processing to overlap with listing. Other backends retain the C++ single-batch fallback. Document that prefetched work may continue after early termination.

Add tests for backend result equivalence, lazy startup, cross-batch iteration, early close, lifetime, Python handler errors, and concurrent operation rejection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant