Repository navigation
Support writing V3 manifests and manifest lists - #3624
moomindani wants to merge 1 commit into
Conversation
36f5b56 to
d161e78
Compare
abnobdoss
left a comment
There was a problem hiding this comment.
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.
| """ | ||
| with AvroFile[ManifestFile]( | ||
| input_file, | ||
| MANIFEST_LIST_FILE_SCHEMAS[DEFAULT_READ_VERSION], |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
f4e27bd to
d73060c
Compare
ab25be6 to
543a2af
Compare
|
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. |
543a2af to
c152089
Compare
|
Not stale, this is waiting for review. Rebased onto main, which needed one fix: since #3954, |
|
Tested this branch ( The server names the cause:
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,
)
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 500I 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 |
|
@cayetanobv thanks for testing this against the REST fixture, and for tracking down the cause. You're right: |
|
Yes, I'll take #3621. I'll base the branch on this PR, since |
|
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? |
|
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
left a comment
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| def version(self) -> TableVersion: | ||
| return 3 | ||
|
|
||
| def to_manifest_file(self) -> ManifestFile: |
There was a problem hiding this comment.
super().to_manifest_file() is already V3 layout so I think this can just be manifest_file.first_row_id = self._first_row_id?
There was a problem hiding this comment.
Yes, done: to_manifest_file now just sets manifest_file.first_row_id = self._first_row_id.
| """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: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| 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: |
There was a problem hiding this comment.
Rust clears first_row_id on delete manifests and Java does the same, might be worth matching that here.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
nit: would be nice to do a real _commit() here rather than mocking, it would have caught the missing added_rows too.
There was a problem hiding this comment.
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
c152089 to
8f12b48
Compare
|
@xanderbailey thanks for the careful review. All six points held up. I rewrote the branch as one commit on current @cayetanobv heads-up: the base commits under #4102 have changed, so it will need a rebase onto the new head. |
|
@moomindani thanks for the heads-up. I've rebased #4102 onto the new squashed commit; it applied cleanly. One thing I noticed: on 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. |
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 optionalfirst_row_idis carried into the produced manifest file for rewrites of manifests that already have one.ManifestListWriterV3: implements the spec's First Row ID Assignment: existingfirst_row_idvalues 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'sfirst-row-id, advanced byexisting_rows_count + added_rows_count. It never mutates the caller's manifest files, and exposesnext_row_idfor the commit path. Metadata carriesfirst-row-idandformat-version: 3, matching the JavaManifestListWriter.V3Writer.The snapshot producer passes the table's
next-row-idto the V3 manifest list writer. Setting the snapshot'sadded-rowsand advancing the table'snext-row-idis #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, clearingfirst_row_idon delete manifests, not mutating the input manifests, rejecting unknown row counts, the required and V3-onlyfirst_row_idarguments, and the producer passingfirst_row_idonly for V3. All fail without the implementation and pass with it. Unit test failures outside this PR are identical tomain.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.