feat: Add streaming and concurrent file reads to ArrowScan to reduce memory usage and increase throughput#3046
Open
sumedhsakdeo wants to merge 12 commits intoapache:mainfrom
Open
Conversation
ab8c31b to
7ad9910
Compare
Add batch_size parameter to _task_to_record_batches, _record_batches_from_scan_tasks_and_deletes, ArrowScan.to_record_batches, and DataScan.to_arrow_batch_reader so users can control the number of rows per RecordBatch returned by PyArrow's Scanner. Closes partially apache#3036 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
7ad9910 to
c86f0be
Compare
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
c86f0be to
05e07d1
Compare
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When streaming=True, batches are yielded as they are produced by PyArrow without materializing entire files into memory. Files are still processed sequentially, preserving file ordering. The inner method handles the global limit correctly when called with all tasks, avoiding double-counting. This addresses the OOM issue in apache#3036 for single-file-at-a-time streaming. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add _bounded_concurrent_batches() with proper lock discipline: - Queue backpressure caps memory (scan.max-buffered-batches, default 16) - Semaphore limits concurrent file reads (concurrent_files param) - Cancel event with timeouts on all blocking ops (no lock over IO) - Error propagation and early termination support When streaming=True and concurrent_files > 1, batches are yielded as they arrive from parallel file reads. File ordering is not guaranteed (documented). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
05e07d1 to
05f330d
Compare
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
05f330d to
240a860
Compare
Setting `mock.call_count = 0` does not actually reset the mock's internal call tracking, causing the second assertion to see accumulated calls from both test phases. Use `reset_mock()` instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add a parametrized benchmark case for default (executor.map) with max_workers=4 to compare memory/throughput against unbounded threading. Add TTFR (time to first record) measurement across all configurations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses #3036 — ArrowScan.to_record_batches() uses executor.map + list() which eagerly materializes all record batches per file into memory, causing OOM on large tables.
This PR adds three parameters to to_arrow_batch_reader() that give users control over memory usage and parallelism:
Problem
The current implementation materializes all batches from each file via list() inside executor.map, which runs up to min(32, cpu_count+4) files in parallel. For large files this means all batches from ~20 files are held in memory simultaneously
before any are yielded to the consumer.
Solution
Before: OOM on large tables
After: bounded memory, tunable parallelism
Default behavior is unchanged —
streaming=Falsepreserves the existing executor.map + list() path for backwards compatibility.Architecture
When
streaming=True, batches flow through _bounded_concurrent_batches:Ordering semantics:
streaming=False)streaming=TruePR Stack
Breakdown of this large PR into smaller PRs:
batch_sizeforwardingstreamingflag — stop materializing entire filesconcurrent_files— bounded concurrent streamingbenchmarkBenchmark results
32 files × 500K rows, 5 columns (int64, float64, string, bool, timestamp), batch_size=131,072 (PyArrow default):
TTFR = Time to First Record
Note on throughput plateau at cf=8: This benchmark runs against local filesystem where Parquet reads are CPU-bound (decompression + decoding). Throughput plateaus once enough threads saturate available cores. On cloud storage (S3/GCS/ADLS), reads are I/O-bound with 50-200ms per-file latency, so higher
concurrent_filesvalues (16-64+) would continue to show throughput gains until network bandwidth saturates. The optimalconcurrent_fileswill be higher for remote storage than what this local benchmark suggests.Positional deletes, row filters, and limit are handled correctly in all modes.
Are these changes tested?
Yes. 23 new unit tests across two test files, plus a micro-benchmark:
streaming, concurrent), positional deletes with limit, concurrent_files < 1 raises ValueError
cleanly, concurrency limit enforced, empty task list, ArrowScan integration with limit
Are there any user-facing changes?
Yes. Three new optional parameters on DataScan.to_arrow_batch_reader():
All parameters are optional with backwards-compatible defaults. Existing code is unaffected.
Documentation updated in mkdocs/docs/api.md with usage examples and ordering semantics.