Skip to content

[Data] Iceberg DSv2 [2/15]: Route read_iceberg through Datasource V2 - #68436

Draft
AarryaSaraf wants to merge 1 commit into
iceberg-dsv2-rest-planningfrom
iceberg-dsv2-parity-4-wire
Draft

AarryaSaraf wants to merge 1 commit into
iceberg-dsv2-rest-planningfrom
iceberg-dsv2-parity-4-wire

Conversation

@AarryaSaraf

@AarryaSaraf AarryaSaraf commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds IcebergDatasourceV2 and routes read_iceberg through it when the private DataContext._use_iceberg_datasource_v2 is set (default False). The flag is temporary: [7/15] deletes it and makes V2 the default. 2 of 15 in the stack moving read_iceberg onto Datasource V2 (#66181). It connects V2's listing, scanning and reading components from #68152.

Before

read_iceberg always runs on the V1 IcebergDatasource. The V2 components from #68152 exist, but nothing constructs them.

After

  1. The V2 datasource. Files: _internal/datasource_v2/formats/iceberg/iceberg_datasource_v2.py (new).
    • It loads the catalog and table on the driver only. The listing and read tasks get the table's metadata and FileIO, as V1's read tasks do, because the catalog's config may exist only on the driver.
    • It pins the snapshot once, so every task reads the same snapshot whatever is written meanwhile.
    • It hands the listing task ServerSidePlanning.for_table(table) from [1/15], so a REST catalog that plans scans on its server plans V2's reads too.
    • It packs files into read tasks with the shared OnlineBinPacker at 128 MiB per task (RAY_DATA_ICEBERG_BIN_PACKING_BYTES). For now that is 128 MiB of compressed file bytes, the only size Iceberg's metadata records.
    • It takes the same constructor arguments as V1, so read_iceberg's signature is unchanged.
  2. The switch. Files: context.py, read_api.py. With DataContext.get_current()._use_iceberg_datasource_v2 = True, read_iceberg builds the V2 datasource. parallelism and override_num_blocks are then ignored with a warning, because bin packing sets the number of read tasks.
  3. Tests run on both readers. Files: tests/datasource/test_iceberg.py.
    • The read tests now take a both_iceberg_readers fixture: V1, V2 with one ArrowScan call per file, and V2 with one call per read task.
    • New cases:
      • A filter with a part Iceberg can't translate (col("x") * 2 > 10) is applied above the read.
      • A table with no data files reads as empty.
      • The snapshot stays pinned across executions.
      • count() works with an empty projection.
      • Through a fake REST catalog that plans on its server, a read and count() cover exactly the server's plan, on all three readers.
      • select_columns keeps the requested order.
      • Planning samples no file, and the schema hooks accept a missing sample.
      • count() doesn't take the metadata path yet; [3/15] adds it.
flowchart TD
    subgraph Before
        subgraph BS["Subsystem: API"]
            B1["read_iceberg"] --> B2["V1 IcebergDatasource"]
        end
    end
    subgraph After
        subgraph AS["Subsystem: API"]
            A1["read_iceberg"] --> A0{"DataContext._use_iceberg_datasource_v2"}
            A0 -->|"False (default)"| A2["V1 IcebergDatasource"]
        end
        subgraph AIO["Subsystem: IO (formats/iceberg/)"]
            A3["IcebergDatasourceV2<br/>catalog and snapshot on the driver"] --> A4["ListFiles: IcebergFileIndexer"]
            A4 --> A5["OnlineBinPacker<br/>128 MiB per read task"]
            A5 --> A6["ReadFiles: IcebergScanner + IcebergFileReader"]
        end
        A0 -->|True| A3
    end
    classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000
    class A0,A3 added
Loading

Subsystems

API (read_api.py, context.py); IO (formats/iceberg/iceberg_datasource_v2.py).

Testing

pytest python/ray/data/tests/datasource/test_iceberg.py: 149 passed, 6 skipped at this PR's head (pyarrow 24.0.0, PyIceberg 0.11.1). ruff 0.8.4 (default and --select I), black 22.10.0, pydoclint 0.9.1 and pyrefly 0.51.0 (CI's environment) are clean.

Not a duplicate: apart from this stack, no open PR moves read_iceberg to Datasource V2. I searched open PRs with "iceberg" in the title on 2026-11-21; the only other one, #68252, fixes Iceberg writes on actor pools.

AI assistance (Claude Code) was used. I have reviewed every changed line and ran the tests above locally.

Signed-off-by: Aarrya [email protected]

🤖 Generated with Claude Code

# ``ArrowScan`` strips field ids from the batches it yields.
return schema_to_pyarrow(self._projected_schema(), include_field_ids=False)

def _get_file_indexer(self) -> IcebergFileIndexer:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The listing task builds a DataScan with no catalog, so PyIceberg never uses server-side scan planning there. _should_use_server_side_planning() returns False when catalog is None. V1 calls table.scan(), which passes the catalog, so V1 does ask the server when rest-scan-planning-enabled is on.

So with a REST catalog that plans scans, V1 gets the file list from the server, but V2 reads the manifests itself. That fails if the client can't read them, and it skips whatever the server does during planning.

I checked on PyIceberg 0.11.1 by setting supports_server_side_planning to True:

V1 Table.scan().plan_files():                 server-side
V2 DataScan(table_metadata, io).plan_files(): local

Since the catalog only lives on the driver, can we check self.table.catalog.supports_server_side_planning() here? If it's True, either plan on the driver and pass the planned tasks to the indexer, or use V1 for now. A test can patch supports_server_side_planning to True and check that V2 doesn't plan locally.

I didn't see this handled in any of the 14 PRs. If I missed it, please point me to the PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#68498 [1/15] fixes this: the listing task rebuilds the REST catalog from the driver's properties and passes it to DataScan, so V2 plans on the server as V1 does. test_read_uses_the_rest_catalogs_plan covers it on both readers.

One follow-up: with server planning, count() can return too many rows when the server leaves out residual-filter. Details: #68437 (comment)

# ``ArrowScan`` strips field ids from the batches it yields.
return schema_to_pyarrow(self._projected_schema(), include_field_ids=False)

def _get_file_indexer(self) -> IcebergFileIndexer:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about cost: V1 saves its plan_files result on the datasource, so a plain read that runs several times (one per training epoch, say) plans once. Here the listing task reads the manifest list and the manifests again on every run, even though the snapshot is pinned and the result is always the same.

For big tables, that's repeated planning time per run. Is that acceptable, or worth a follow-up that plans once and reuses the result? Delta V2 does this in #68407.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: from #68438 [4/15], listing also reads every Parquet file's footer, so each run of the dataset now repeats the footer reads as well as the planning. That makes planning once and reusing the result more worth it. Do you plan to do it in this stack or as a follow-up?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Planning reads only Iceberg metadata (the manifest list and manifests), plus 8 footers since the sizing change, which is small next to the data each run reads. I'd rather not add a cache for that.

@abhishekverma-ray

Copy link
Copy Markdown
Contributor

#68152 merged, so this needs a rebase on master. Right now it conflicts and the diff still shows #68152's files. After the rebase, it should only show iceberg_datasource_v2.py, context.py, read_api.py and the tests.

The commit that turns [2/5] and [3/5] into a working reader.
`ray.data.read_iceberg()` goes through `DataSourceV2` (`ListFiles` ->
`ReadFiles`) when the private, test-only
`DataContext._use_iceberg_datasource_v2` is set (default `False`, no env
var), so users can't turn it on before the Iceberg V2 path is ready. V1 is
untouched: outside the tests this commit deletes no line of existing code.

`formats/iceberg/iceberg_datasource_v2.py` is the driver side. Two things
it does that are worth review:

- The schema comes from table metadata, not from a file. Iceberg records
  the schema in the table, so `schema_to_pyarrow(scan.projection())` is
  exact and free; the V2 sampling path (open up to 16 files at plan time
  and infer) is declined by extending `DataSourceWithMetadata`, whose
  `schema_needs_file_sample` is `False`.
- The catalog is loaded on the driver only. The listing and read tasks
  get the loaded table's metadata and `FileIO`, as V1's read tasks do, so a
  catalog configured only where the driver runs (`.pyiceberg.yaml`,
  `PYICEBERG_CATALOG__*`) works, and although listing now runs in a task at
  execution time, two executions of one `Dataset` and a retried task all
  plan against the snapshot resolved there. `None` is a legitimate resolved
  snapshot -- a table with no snapshot yet -- so "not resolved" is tracked
  by a latch, not by the id being `None`.

`parallelism` and `override_num_blocks` warn and are ignored, since
listing and packing decide task granularity. That is a deliberate
difference from V1, which bin-packs files into exactly `parallelism`
chunks.

Tests, in `python/ray/data/tests/datasource/test_iceberg.py`. The ten
read-path tests now run against both readers through the
`both_iceberg_readers` fixture, so every row assertion in them is a parity
assertion (where a filter sits in the plan differs: V1 pushes it into the
read, V2 keeps it above, since this stack has no filter pushdown, and the
three cases that filter on a column the select dropped are skipped on V2); seven already existed and three are new. Two of the three
branch on the reader instead of asserting the same thing, pinning a V1
limitation V2 does not have: a predicate Iceberg cannot express (V1 raises
at read time, V2 filters above the read) and a declared-but-empty table
(V1 reports no schema at all). The others are new because V2 needs them
and V1 never could fail them: that two executions of one `Dataset` see the
same snapshot, and that `count()` with an empty projection is right.

Three more cover the hooks the shared-seams commit [1/5] added but could
not test there, since Iceberg is their first real implementation: that
planning opens no file at all, not even to sample one (`sample_files` is
patched to raise); that `infer_schema` and `resolve_partitioning` accept a
`None` sample, which is the whole point of `schema_needs_file_sample`; and
that a source which declines the metadata-only `count()` gets an ordinary
read instead.

Deliberately not optimized here: `count()` decodes one column per file
rather than summing `record_count` from metadata, which [5/5] fixes. The
rest are follow-ups after parity -- a limit does not stop the
`plan_files()` walk early, only the per-unit read; every file goes through
`ArrowScan` even when it has no delete files, an `AlwaysTrue` residual and
no schema drift, so none of the native Parquet reader's benefits apply;
and an oversized file becomes one oversized task, because an Iceberg scan
task names a file and never a range within one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Aarrya <[email protected]>
@AarryaSaraf
AarryaSaraf force-pushed the iceberg-dsv2-parity-4-wire branch from edcd455 to c4c7a9c Compare November 24, 2026 01:24
@AarryaSaraf AarryaSaraf changed the title [Data] Iceberg DSv2 [1/14]: Route read_iceberg through Datasource V2 [Data] Iceberg DSv2 [2/15]: Route read_iceberg through Datasource V2 Nov 24, 2026
@AarryaSaraf
AarryaSaraf removed this pull request from stack #68450 November 24, 2026 01:50
@AarryaSaraf
AarryaSaraf changed the base branch from master to iceberg-dsv2-rest-planning November 24, 2026 01:50
@AarryaSaraf
AarryaSaraf added this pull request to stack #68499 November 24, 2026 01:51
return self.table.scan(
row_filter=self._row_filter,
selected_fields=self._selected_fields,
snapshot_id=self.snapshot_id,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V2 reads the schema of the last snapshot, not the table's current schema. snapshot_id is always set here (pinned to the current snapshot), and PyIceberg's projection() then uses that snapshot's schema_id. A schema change (ALTER TABLE ... ADD/RENAME/DROP COLUMN, or update_schema()) writes no snapshot, so until the next data write V2 reads the old schema. V1, table.scan() and Spark read the current schema unless you time travel.

On this branch, after update_schema() on the test table and no new write:

  • add col_d: V1 [col_a, col_b, col_c, col_d], V2 [col_a, col_b, col_c]
  • rename col_b → col_b_renamed: V1 col_b_renamed, V2 col_b
  • drop col_b: V1 [col_a, col_c], V2 [col_a, col_b, col_c], with the dropped column's data

So select_columns(["col_a", "col_d"]) fails on V2 with Could not find column: 'col_d'.

Suggested fix: keep pinning the snapshot for planning, but build the projection from the current schema unless the caller passed snapshot_id. It's the same at #68449, and the schema-evolution tests don't run on V2.

self._snapshot_id = (
snapshot_id
if snapshot_id is not None
else self._scan_kwargs.pop("snapshot_id", None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_iceberg(snapshot_id=S, scan_kwargs={"snapshot_id": S}) crashes on V2. The pop only runs when the argument is None, so scan_kwargs keeps its snapshot_id, and table.scan(snapshot_id=..., **scan_kwargs) gets it twice: TypeError: Table.scan() got multiple values for keyword argument 'snapshot_id'. V1 returns 101 rows. The comment says both spellings should work with the argument winning; always popping fixes it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@abhishekverma-ray abhishekverma-ray self-assigned this Nov 24, 2026

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.

2 participants