dragongu opened a new pull request, #55753:
URL: https://github.com/apache/arrow/pull/55753

   ### 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.
   
   ```python
   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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to