Repository navigation
feat(data): Support split and parallel scans - Part-4: bounded parallel Arrow reader - #1167
kamcheungting-db wants to merge 4 commits into
Conversation
…el Arrow reader Add ParallelScanReader, which reads the groups of a FileScanTaskGroupStream with a fixed set of workers and returns one Arrow C stream. - Each worker owns a FileScanTaskReader with residual enforcement forced on. Workers claim groups through a planning token: only one calls the group stream at a time, outside all locks. - Completed batches enter an output queue bounded by batch count and by charged bytes. Producers are admitted in FIFO order; a batch larger than the byte budget is admitted only into an empty queue. - ArrowBatchSizer charges a batch as the checked sum of its Arrow buffer spans, after preflighting offsets, lengths, buffer counts, and every layout-dependent size product for overflow. List views and run-end encoded arrays are rejected. - Each task stream's schema is compared with the projected schema before its batches are read. - The first planning, read, schema, or sizing error stops new work, releases queued batches, and is reported from get_next as EIO with the file and byte range in get_last_error. Releasing the stream between callbacks stops and joins the workers. - iceberg_data now links Threads unconditionally. Co-authored-by: Isaac <[email protected]>
…date GCC and AppleClang reject a template parameter that shares the member function's name. Also replace C-style arrays in the batch sizer tests. Co-authored-by: Isaac <[email protected]>
Use std::cmp_* for signed/unsigned comparisons and std::ranges::is_sorted. Co-authored-by: Isaac <[email protected]>
| readers.reserve(static_cast<size_t>(worker_count)); | ||
| sizers.reserve(static_cast<size_t>(worker_count)); | ||
| for (int32_t index = 0; index < worker_count; ++index) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto reader, FileScanTaskReader::Make(reader_options)); |
There was a problem hiding this comment.
These readers share one NameMapping. If two workers open files without field IDs, both can build the same name index at once. Can we make that first lookup thread-safe?
| ICEBERG_PRECHECK(options.reader_options.projected_schema != nullptr, | ||
| "Projected schema must not be null"); | ||
|
|
||
| options.reader_options.apply_residuals = true; |
There was a problem hiding this comment.
On an unpartitioned table, try selecting only name with id IS NOT NULL on a required id. The scan drops id from the projection, but the task still has the filter. Binding it here fails. Can we cover this with a planned scan?
| // Enqueue() reports a stop, so no separate check is needed per batch. | ||
| while (true) { | ||
| ArrowArray batch{}; | ||
| if (int code = stream.get_next(&stream, &batch); code != 0) { |
There was a problem hiding this comment.
Can we check the stop flag before calling get_next? If the stream was released while get_schema was running, this call can still read the rest of the file looking for a match.
| if (batch.release == nullptr) { | ||
| return {}; | ||
| } | ||
| auto charge = sizer.Measure(batch); |
There was a problem hiding this comment.
Can we use ArrowArrayGuard here and release the guard once the batch is in the queue? If sizing or push_back throws, nothing releases the batch.
| /// Options for the per-worker task readers. `apply_residuals` is forced on. | ||
| FileScanTaskReader::Options reader_options; | ||
| /// Number of worker threads. Must be positive. | ||
| int32_t worker_count = 0; |
There was a problem hiding this comment.
Could we take a caller-provided Executor and keep worker_count as the limit for one scan? WriteManifestsWith already follows that pattern. With this API, ten scans with eight workers each start 80 threads.
| snapshot.peak_held_bytes = std::max(snapshot.peak_held_bytes, held_bytes_); | ||
| }); | ||
|
|
||
| producer_cv_.wait(lock, [&] { |
There was a problem hiding this comment.
If we use a shared executor, can the reader yield when the queue fills? If all the pool threads are waiting here, none are available for manifest planning.
| return {}; | ||
| }; | ||
|
|
||
| const int fixed_buffers = FixedBufferCount(view); |
There was a problem hiding this comment.
Can we use ArrowArrayViewGetNumBuffers and ArrowArrayViewGetBufferView here? They cover variadic buffers too. We could keep the overflow checks and drop the separate loop.
- Take a required Options::executor and keep worker_count as the per-scan concurrency limit; the reader no longer creates threads. - Park workers when the output queue or planning token is busy instead of blocking executor threads, so shared pools can still run planning. - Check for cancellation before every task stream callback. - Guard each returned batch with ArrowArrayGuard until it is queued. - Simplify residuals against the table schema before binding them to the projected schema, so planned scans can drop filter-only columns. - Build NameMapping and MappedFields indexes at construction so shared mappings are safe for concurrent lookups. - Size buffers with ArrowArrayViewGetNumBuffers and ArrowArrayViewGetBufferView, keeping the overflow checks. Co-authored-by: Isaac <[email protected]>
What this PR does
Adds
ParallelScanReader(iceberg/data/parallel_scan_reader.h), which reads the groups of aFileScanTaskGroupStream(from #1113) with a fixed set of workers and returns one Arrow C stream.How it works
FileScanTaskReader. Residual filters are always enforced (apply_residualsis forced on). Each task stream's schema is compared with the projected schema before its batches are read.max_queued_batches) and by charged bytes (max_queued_bytes). Producers are admitted in FIFO order. A batch larger than the byte budget enters only an empty queue, as its sole batch. Zero-byte batches still count toward the batch limit. A worker holds at most one completed batch while it waits.ArrowBatchSizercharges a batch as the checked sum of the spans of all present buffers, including children, dictionaries, and variadic buffers. Before nanoarrow sizes anything, it checks offsets, lengths, buffer, child, and dictionary counts, and every layout-dependent size product for overflow. List views and run-end encoded arrays are rejected.get_nextasEIO, with the file and byte range inget_last_error(). Releasing the stream between callbacks stops the workers, releases batches that were not returned, and joins the threads; batches already returned stay valid. No callback, release, or join runs under a lock.iceberg_datanow linksThreads::Threadsunconditionally, including withICEBERG_SPDLOG=OFF.The queue limits bound admission, not process memory: format reader buffers, delete indexes, and arrays already returned to the caller are outside the budget. The header documents this, the per-worker memory estimate, and the preconditions (registered format factories, a
FileIOthat supports concurrent reads, immutable task metadata, a planning executor that outlives the stream, and matchingcase_sensitive).Tests
New
parallel_scan_test, which links onlyiceberg_dataand builds withICEBERG_BUILD_BUNDLE=OFF. It uses hand-built groups and a scoped fake reader registered for Parquet and Avro:ArrowBatchSizer: fixed width, sliced, variable width, nested, dictionary, string view, zero-column structs, unsupported list views, and overflow and malformed-count rejectionsThe suite passes repeatedly under ThreadSanitizer and AddressSanitizer with UndefinedBehaviorSanitizer, with the bundle and spdlog off. Real Parquet and Avro tables are covered in Part-5.
pre-commitis clean.Related
Part 1 #976 · Part 2a #1002 · Part 2b #1062 · Part 2c #1065 · Part 3 #1113 · Part 3.5 #1166 (makes residual evaluation scale across workers) · Part 5 #1168 (end-to-end validation and benchmark).
This pull request and its description were written by Isaac.