Skip to content

Support writing V3 manifests and manifest lists - #3624

Open
moomindani wants to merge 1 commit into
apache:mainfrom
moomindani:moomindani/v3-manifest-write
Open

moomindani wants to merge 1 commit into
apache:mainfrom
moomindani:moomindani/v3-manifest-write

Conversation

@moomindani

@moomindani moomindani commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3620 (part of #1551)

Rationale for this change

Adds the writer side for V3 manifests and manifest lists (the V3 read schemas already exist):

  • ManifestWriterV3: writes manifest entries with the V3 record layout so the V3-only data file fields (first_row_id, referenced_data_file, content_offset, content_size_in_bytes) are written. An optional first_row_id is carried into the produced manifest file for rewrites of manifests that already have one.
  • ManifestListWriterV3: implements the spec's First Row ID Assignment: existing first_row_id values of data manifests are preserved, delete manifests are written with a null one, and unassigned data manifests get a running value starting at the snapshot's first-row-id, advanced by existing_rows_count + added_rows_count. It never mutates the caller's manifest files, and exposes next_row_id for the commit path. Metadata carries first-row-id and format-version: 3, matching the Java ManifestListWriter.V3Writer.

The snapshot producer passes the table's next-row-id to the V3 manifest list writer. Setting the snapshot's added-rows and advancing the table's next-row-id is #4102 (#3621).

Are these changes tested?

Yes, eleven new tests: V3 manifest round-trips (including deletion vector fields and a carried first_row_id), first-row-id assignment across unassigned, preserved, delete, and second unassigned manifests, clearing first_row_id on delete manifests, not mutating the input manifests, rejecting unknown row counts, the required and V3-only first_row_id arguments, and the producer passing first_row_id only for V3. All fail without the implementation and pass with it. Unit test failures outside this PR are identical to main.

Are there any user-facing changes?

No user-facing behavior changes; this adds writer capabilities used by upcoming V3 write support.

This pull request and its description were written by Claude Fable 5.

@abnobdoss abnobdoss left a comment

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.

Thanks for picking up the v3 writer work! The first-row-id assignment lines up with the Java writer nicely; I left a few comments, mostly on the read version handling.

Comment thread pyiceberg/manifest.py
"""
with AvroFile[ManifestFile](
input_file,
MANIFEST_LIST_FILE_SCHEMAS[DEFAULT_READ_VERSION],

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.

Per the comment on _wrap_data_file: we likely need to go through all the uses of DEFAULT_READ_VERSION, including this one. Reading a v3 manifest list here drops first_row_id, which is what makes a round trip fail. fetch_manifest_entry has the same issue for the new data file fields.

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 by adding LATEST_MANIFEST_ENTRY_READ_VERSION / LATEST_MANIFEST_LIST_READ_VERSION (derived from max() of the respective schema dicts, so they stay in sync as new format versions are added) and switching fetch_manifest_entry / read_manifest_list to read with these instead of DEFAULT_READ_VERSION. Reading an older file with the latest schema is safe since the added fields are all optional, so Avro schema resolution fills them with None. DEFAULT_READ_VERSION itself is unchanged and still used for constructing/writing records, so this only touches the two read call sites. Also added the corresponding V3 properties on DataFile (first_row_id, referenced_data_file, content_offset, content_size_in_bytes) so the values are actually exposed to callers, and extended the round-trip test to assert on their actual values rather than just presence.

Comment thread pyiceberg/manifest.py Outdated
so that is the layout to zip the positional data against.
"""
if len(manifest_file._data) >= len(MANIFEST_LIST_FILE_SCHEMAS[3].fields):
return copy(manifest_file)

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.

copy(manifest_file) doesn't seem to work in its current form: the copy and the original share the same underlying _data list, so writing to one writes to both.

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 — ManifestListWriterV3._wrap now always rebinds through from_args instead of copy(), so mutating the returned manifest file's first_row_id never leaks back into the caller's record.

One thing to flag separately: ManifestListWriterV2.prepare_manifest has the same shallow-copy sharing bug (copy(manifest_file) there also shares _data). I didn't touch it since it's pre-existing and out of scope for this PR, but wanted to make sure it's on your radar — happy to file a follow-up issue if useful.

Comment thread pyiceberg/manifest.py Outdated
if len(data_file._data) >= len(DATA_FILE_TYPE[3].fields):
return data_file
args = {
field.name: value for field, value in zip(DATA_FILE_TYPE[DEFAULT_READ_VERSION].fields, data_file._data, strict=True)

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.

Should this be DEFAULT_READ_VERSION? It assumes any record that isn't v3 is v2, but DATA_FILE_TYPE has 15 fields for v1, 16 for v2 and 20 for v3, so a v1-bound DataFile would fail the strict zip with a confusing error.

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 by adding _layout_version_from_field_count, which recovers the source layout from the record's field count (each version's layout currently has a distinct field count) instead of assuming V2.

I did think about how this holds up for a future V4: if V4 only adds fields (the pattern so far), field-count inference keeps working automatically — no code change needed beyond adding the V4 schema entry, same as how LATEST_MANIFEST_ENTRY_READ_VERSION / LATEST_MANIFEST_LIST_READ_VERSION (see the other thread) are derived via max() rather than hardcoded. But if V4 ever lands with the same field count as an existing version, or changes a field non-additively, field-count inference can no longer tell them apart — so I added an explicit check that raises Ambiguous layout in that case instead of silently binding to the wrong one. That failure mode felt important to guard against explicitly rather than leave implicit.

One thing I'd like your take on: this is still inference, not the record actually knowing its own version. The more principled fix would have Record/DataFile/ManifestFile carry the format version they were bound with, so rebinding wouldn't need to guess at all — that's how Java's manifest records work, since they're self-describing rather than positional. I considered that, but went with field-count inference for now since it's consistent with the existing code's approach (the pre-existing early-return already used a field-count threshold) and, after switching reads to the latest schema, most records reaching this rebind path are already V3-shaped — it's now mostly a defensive path for records constructed programmatically at an older version, not a hot path. Changing Record's core design to carry its own version felt like the right call for a dedicated follow-up, not something to fold into this PR's scope, but let me know if you'd weigh that differently.

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Oct 7, 2026
@moomindani
moomindani force-pushed the moomindani/v3-manifest-write branch from 543a2af to c152089 Compare October 7, 2026 09:59
@moomindani

Copy link
Copy Markdown
Contributor Author

Not stale, this is waiting for review. Rebased onto main, which needed one fix: since #3954, DataFile.from_args rejects fields outside the target layout, so rebinding a V1 data file to V3 failed on block_size_in_bytes. c152089 drops the V1-only fields, and test_write_manifest_v3_rebinds_v1_data_file covers it. @kevinjqliu, since this touches the path changed in #3954, would you be able to take a look?

@cayetanobv

Copy link
Copy Markdown

Tested this branch (c152089da) against the Apache REST catalog fixture 1.10.1. The manifest work does what it says: ManifestWriterV3 and ManifestListWriterV3 are there and write_manifest dispatches on version 3. add_files to a v3 table still fails, but one step later than before.

CommitStateUnknownException: RESTError 500

The server names the cause:

java.lang.IllegalArgumentException: Invalid added-rows (required when first-row-id is set): null

_SnapshotProducer._commit sets first_row_id and not added_rows (table/update/snapshot.py, lines 337-361 on this branch):

first_row_id: int | None = None
if self._transaction.table_metadata.format_version >= 3:
    first_row_id = self._transaction.table_metadata.next_row_id
...
snapshot = Snapshot(
    snapshot_id=self._snapshot_id,
    ...
    first_row_id=first_row_id,
)

_calculate_added_rows is defined at line 212 in the same class and has no caller, so the AddSnapshotUpdate carries first-row-id without added-rows.

Reproduction, against the REST fixture with an S3 warehouse:

from pyiceberg.catalog import load_catalog
import pyarrow as pa, pyarrow.parquet as pq, tempfile, pathlib

cat = load_catalog("t", **{"type": "rest", "uri": "http://localhost:8181", ...})
d = pathlib.Path(tempfile.mkdtemp()); src = d / "data.parquet"
pq.write_table(pa.table({"id": pa.array([1, 2, 3], type=pa.int64())}), src)

t = cat.create_table("ns.reg", schema=..., properties={"format-version": "3"})
t.add_files([f"file://{src}"])     # RESTError 500

I am glad to open a follow-up with the assignment and a test, or it may belong in this pull request.

Context: we are evaluating version 3 geometry tables for Portolan, which is why we exercise add_files on version 3 rather than append.

@moomindani

Copy link
Copy Markdown
Contributor Author

@cayetanobv thanks for testing this against the REST fixture, and for tracking down the cause. You're right: _commit sets first_row_id but never added_rows, and _calculate_added_rows has no caller. That gap is on main too, not something this branch introduces. This PR covers the manifest writers only; assigning row lineage on commit is #3621, which is still unassigned. A follow-up from you would be very welcome. Would you like to take #3621? Your add_files repro would make a good regression test.

@cayetanobv

Copy link
Copy Markdown

Yes, I'll take #3621. I'll base the branch on this PR, since added-rows comes from ManifestListWriterV3.next_row_id, and rebase once it merges. The add_files repro against the REST fixture goes in as the regression test.

@xanderbailey

Copy link
Copy Markdown
Contributor

Super excited to see this PR, I'm in the process of working on #3222 is there anything I can do here to help out? Is this just in need of review now?

@moomindani

Copy link
Copy Markdown
Contributor Author

Thanks! Yes, it is ready and only needs a committer review; the branch is mergeable and CI is green. If your encryption work builds on the V3 writers, trying it against this branch and leaving a review with what you find would help, since it shows a second use case beyond row lineage (#3621).

@xanderbailey xanderbailey left a comment

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.

Compared this against iceberg-rust. Main thing missing I think is added_rows on the snapshot, and I think the branch needs a rebase now that #3972 is in maybe?

parent_snapshot_id=self._parent_snapshot_id,
sequence_number=next_sequence_number,
avro_compression=self._compression,
first_row_id=first_row_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.

I think we also need added_rows on the snapshot here, otherwise AddSnapshotUpdate fails on main with Cannot add snapshot without added rows (Java rejects it too). Might need to check that I'm reading this right.

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.

You're reading it right: since #3972, AddSnapshotUpdate rejects a V3 snapshot without added_rows, and _commit never sets it. That is already the case on main, so this PR doesn't change it. I'd keep this PR to the writers and leave the fix to #4102 (closes #3621). It is stacked on this PR and sets added_rows to writer.next_row_id - first_row_id, as Java's SnapshotProducer does.

Comment thread pyiceberg/manifest.py Outdated
def first_row_id(self) -> int | None:
return self._data[16]
# V3-only field; absent on records bound to an older layout
return self._data[16] if len(self._data) > 16 else 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.

Claude finding, have not verified: This doesn't quite work for V2 records, V2 has referenced_data_file at index 16 so first_row_id returns the referenced file path. Not new to this PR, but since everything defaults to V3 now I'd probably just drop the guards.

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.

Confirmed, and agreed. main now reads every record with the V3 layout (DEFAULT_READ_VERSION = 3), so I dropped the version guards and the tests for them.

Comment thread pyiceberg/manifest.py
def version(self) -> TableVersion:
return 3

def to_manifest_file(self) -> ManifestFile:

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.

super().to_manifest_file() is already V3 layout so I think this can just be manifest_file.first_row_id = self._first_row_id?

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.

Yes, done: to_manifest_file now just sets manifest_file.first_row_id = self._first_row_id.

Comment thread pyiceberg/manifest.py Outdated
"""The row ID after the last assigned one; the table's `next-row-id` after this snapshot."""
return self._next_row_id

def _wrap(self, manifest_file: ManifestFile) -> ManifestFile:

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.

Do we still need the field count detection? Nothing binds to V1/V2 anymore as far as I can see. Rust keeps one ManifestFile and converts when serializing. Could this just be a copy()? WDYT?

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.

Dropped the field-count detection. I didn't use copy(), though: Record uses __slots__ = ("_data",), and copy.copy returns a record that shares _data, so setting first_row_id on the copy would also change the caller's manifest file. prepare_manifest builds a fresh ManifestFile(*manifest_file._data) instead, and test_write_manifest_list_v3_does_not_mutate_input_manifests pins that.

Comment thread pyiceberg/manifest.py Outdated
def prepare_manifest(self, manifest_file: ManifestFile) -> ManifestFile:
wrapped_manifest_file = super().prepare_manifest(self._wrap(manifest_file))

if wrapped_manifest_file.content == ManifestContent.DATA and wrapped_manifest_file.first_row_id is 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.

Rust clears first_row_id on delete manifests and Java does the same, might be worth matching that here.

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.

Done: delete manifests are now written with a null first_row_id, matching Java and Rust. Covered by test_write_manifest_list_v3_clears_first_row_id_on_delete_manifests.

overwrite.delete_data_file(stale_file)


def test_manifest_list_receives_first_row_id_for_v3(table_v3: Table) -> 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.

nit: would be nice to do a real _commit() here rather than mocking, it would have caught the missing added_rows too.

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.

Agreed. A real _commit() on a V3 table fails until added_rows is set, though, so here it could only assert the failure. #4102 sets added_rows and adds end-to-end commits against the REST fixture, so I'd let the real-commit test land there and keep this one scoped to what the producer passes to the writer.

Add ManifestWriterV3 and ManifestListWriterV3, rebased onto main, which
now binds every record to the V3 layout. The manifest list writer
assigns first-row-id to data manifests that lack one, clears it on
delete manifests as Java does, and works on a fresh record so the
caller's manifest files are never mutated. The snapshot producer passes
the table's next-row-id to the V3 manifest list writer.

Closes apache#3620

Generated-by: Claude Code
@moomindani
moomindani force-pushed the moomindani/v3-manifest-write branch from c152089 to 8f12b48 Compare November 25, 2026 23:35
@moomindani

Copy link
Copy Markdown
Contributor Author

@xanderbailey thanks for the careful review. All six points held up. I rewrote the branch as one commit on current main; the replies above say what changed for each.

@cayetanobv heads-up: the base commits under #4102 have changed, so it will need a rebase onto the new head.

@cayetanobv

Copy link
Copy Markdown

@moomindani thanks for the heads-up. I've rebased #4102 onto the new squashed commit; it applied cleanly. One thing I noticed: on 8f12b48a, a few tests in tests/table/test_validate.py fail when run after tests/utils/test_manifest.py. It looks like the cache tests' importlib.reload(pyiceberg.manifest) leaves different class objects behind for later tests. I hit the same thing in #4102 and fixed it on my side.

What are your plans for merging #3624? Is anything still pending from the review, or is it waiting on a final approval? Knowing the timeline will help me plan the next steps on #4102.

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.

Support writing V3 manifests and manifest lists

4 participants