Skip to content

feat(data): Support split and parallel scans - Part-4: bounded parallel Arrow reader - #1167

Open
kamcheungting-db wants to merge 3 commits into
apache:mainfrom
kamcheungting-db:feat/parallel-task-reader
Open

kamcheungting-db wants to merge 3 commits into
apache:mainfrom
kamcheungting-db:feat/parallel-task-reader

Conversation

@kamcheungting-db

@kamcheungting-db kamcheungting-db commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Adds ParallelScanReader (iceberg/data/parallel_scan_reader.h), which reads the groups of a FileScanTaskGroupStream (from #1113) with a fixed set of workers and returns one Arrow C stream.

auto groups = scan->PlanTasksStream();
auto stream = ParallelScanReader::Open(
    std::move(*groups), {.reader_options = reader_options,
                         .worker_count = 8,
                         .max_queued_batches = 16,
                         .max_queued_bytes = 64 << 20});

How it works

                          ParallelScanReader::Open(groups, options)
                                       │ validate options, build the projected Arrow schema,
                                       │ make one FileScanTaskReader + ArrowBatchSizer per worker
                                       v
  ┌──────────────────────────────── workers 1..N ────────────────────────────────┐
  │                                                                              │
  │  ClaimGroup()                                                                │
  │    wait for the planning token ──> groups->Next() outside all locks          │
  │    (one worker at a time)          │                                         │
  │                                    ├─ group ──> release token, read it       │
  │                                    ├─ end   ──> destroy group stream, done   │
  │                                    └─ error ──> Fail(error)                  │
  │                                                                              │
  │  for each task in the group, in order:                                       │
  │    reader.Open(task) ──> check task schema == projected schema               │
  │      for each batch:                                                         │
  │        sizer.Measure(batch)   preflight overflow checks, then sum buffer     │
  │                               spans (children, dictionaries, variadic)       │
  │        Enqueue(batch, charge) take a FIFO ticket; wait until it is the head  │
  │                               ticket AND it fits; holds at most this batch   │
  └───────────────────────────────────────┬──────────────────────────────────────┘
                                          v
                ┌──────────── output queue ─────────────┐
                │ admit when:                           │
                │   count < max_queued_batches  AND     │
                │   charge <= max_queued_bytes:         │
                │     queued + charge <= budget         │
                │   charge >  max_queued_bytes:         │
                │     queue is empty (sole batch)       │
                └───────────────────┬───────────────────┘
                                    v
                    get_next ──> pop front, subtract charge, wake producers
                                 (caller owns the array from here)
                    end of stream when all workers finished and the queue is empty

  Fail(error)  (first error wins)        release() between callbacks
    stop new work                           stop new work, wake all waiters
    release queued batches                  release queued batches
    get_next returns EIO;                   join workers (outside locks)
    get_last_error() names the              destroy the group stream
    file and byte range                     returned arrays stay valid
  • Planning. Workers claim groups through a planning token: only one calls the single-pass group stream at a time, and never under a lock. The stream is destroyed as soon as it is exhausted, so planning metrics are reported at EOF.
  • Reading. Each worker owns a FileScanTaskReader. Residual filters are always enforced (apply_residuals is forced on). Each task stream's schema is compared with the projected schema before its batches are read.
  • Queue. Batches enter a queue bounded by count (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.
  • Charging. ArrowBatchSizer charges 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.
  • Errors and release. The first planning, read, schema, or sizing error stops new work, releases queued batches, and is returned from the next get_next as EIO, with the file and byte range in get_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_data now links Threads::Threads unconditionally, including with ICEBERG_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 FileIO that supports concurrent reads, immutable task metadata, a planning executor that outlives the stream, and matching case_sensitive).

Tests

New parallel_scan_test, which links only iceberg_data and builds with ICEBERG_BUILD_BUNDLE=OFF. It uses hand-built groups and a scoped fake reader registered for Parquet and Avro:

  • zero, one, and many groups; 1 versus N workers return the same rows; mixed formats in a group; residuals enforced
  • task-local order across interleaved groups
  • schema mismatch, open failure, read failure (with the file range in the message), reader exceptions, and planning failures and exceptions; errors are sticky
  • bytes binding before count, count binding before bytes, zero-byte batches, oversized singletons, and an oversized batch not starved by small ones
  • releasing between callbacks while producers wait, releasing without reading, and callers keeping a returned array after release; every fake array is released exactly once
  • the group stream is destroyed at the end of planning, and its destructor can query metrics
  • ArrowBatchSizer: fixed width, sliced, variable width, nested, dictionary, string view, zero-column structs, unsupported list views, and overflow and malformed-count rejections

The 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-commit is 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.

…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 <no-reply@databricks.com>
kamcheungting-db and others added 2 commits November 23, 2026 20:24
…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 <no-reply@databricks.com>
Use std::cmp_* for signed/unsigned comparisons and std::ranges::is_sorted.

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
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.

1 participant