cayetanobv commented on code in PR #4102:
URL: https://github.com/apache/iceberg-python/pull/4102#discussion_r4799821124


##########
pyiceberg/table/update/snapshot.py:
##########
@@ -348,20 +336,26 @@ def _commit(self) -> UpdatesAndRequirements:
         manifest_list_file_path = 
location_provider.new_metadata_location(file_name)
         self._written_manifest_lists.append(manifest_list_file_path)
 
+        first_row_id: int | None = None
+        added_rows: int | None = None
+
+        if self._transaction.table_metadata.format_version >= 3:
+            first_row_id = self._transaction.table_metadata.next_row_id
+
         with write_manifest_list(
             format_version=self._transaction.table_metadata.format_version,
             output_file=self._io.new_output(manifest_list_file_path),
             snapshot_id=self._snapshot_id,
             parent_snapshot_id=self._parent_snapshot_id,
             sequence_number=next_sequence_number,
             avro_compression=self._compression,
+            first_row_id=first_row_id,
         ) as writer:
             writer.add_manifests(new_manifests)
 
-        first_row_id: int | None = None
-
-        if self._transaction.table_metadata.format_version >= 3:
-            first_row_id = self._transaction.table_metadata.next_row_id
+        if first_row_id is not None:
+            # row IDs assigned to manifests without a first_row_id, including 
existing rows of upgraded tables
+            added_rows = cast(ManifestListWriterV3, writer).next_row_id - 
first_row_id

Review Comment:
   Done. `ManifestListWriter.next_row_id` now returns `None` and 
`ManifestListWriterV3` overrides it, so the producer no longer needs the `cast` 
or the `ManifestListWriterV3` import. Two `# type: ignore[attr-defined]` 
comments in `tests/utils/test_manifest.py` became unused, so I removed them too.



##########
pyiceberg/table/update/snapshot.py:
##########
@@ -348,20 +336,26 @@ def _commit(self) -> UpdatesAndRequirements:
         manifest_list_file_path = 
location_provider.new_metadata_location(file_name)
         self._written_manifest_lists.append(manifest_list_file_path)
 
+        first_row_id: int | None = None
+        added_rows: int | None = None
+
+        if self._transaction.table_metadata.format_version >= 3:
+            first_row_id = self._transaction.table_metadata.next_row_id

Review Comment:
   Good call, added. `test_conflict_append_append` now runs for V3 too, and 
asserts that the three snapshots have `(first_row_id, added_rows)` of `(0, n)`, 
`(n, n)` and `(2n, n)`, so the retried append starts after the rows of the 
concurrent one. As you said, `_attempt_landed()` refreshes the table before 
`_rebuild_snapshot_updates()` calls `_commit()` again, so the retry reads the 
current `next-row-id`.



##########
tests/table/test_snapshots.py:
##########
@@ -844,3 +844,109 @@ def 
test_overwrite_rejects_explicit_delete_without_parent_snapshot(
         with empty.transaction() as tx:
             with tx.update_snapshot().overwrite() as overwrite:
                 overwrite.delete_data_file(stale_file)
+
+
+def test_manifest_list_receives_first_row_id_for_v3(table_v3: Table) -> None:
+    """The V3 manifest list writer requires first-row-id, so the producer has 
to supply it."""
+    from unittest import mock
+
+    from pyiceberg.table.update.snapshot import _FastAppendFiles
+
+    txn = table_v3.transaction()
+    append = _FastAppendFiles(operation=Operation.APPEND, transaction=txn, 
io=table_v3.io)
+
+    with (
+        mock.patch("pyiceberg.table.update.snapshot.write_manifest_list") as 
writer,
+        mock.patch.object(_FastAppendFiles, "_manifests", return_value=[]),
+    ):
+        append._commit()
+
+    assert writer.call_args.kwargs["format_version"] == 3
+    assert writer.call_args.kwargs["first_row_id"] == 
table_v3.metadata.next_row_id
+
+
+def test_manifest_list_has_no_first_row_id_for_v2(table_v2: Table) -> None:
+    from unittest import mock
+
+    from pyiceberg.table.update.snapshot import _FastAppendFiles
+
+    txn = table_v2.transaction()
+    append = _FastAppendFiles(operation=Operation.APPEND, transaction=txn, 
io=table_v2.io)
+
+    with (
+        mock.patch("pyiceberg.table.update.snapshot.write_manifest_list") as 
writer,
+        mock.patch.object(_FastAppendFiles, "_manifests", return_value=[]),
+    ):
+        append._commit()
+
+    assert writer.call_args.kwargs["format_version"] == 2
+    assert writer.call_args.kwargs["first_row_id"] is None
+
+
[email protected]
+def empty_table_v3(tmp_path: Path, arrow_table_simple: pa.Table) -> Table:
+    """An empty V3 table; writing V3 metadata files is not supported, so 
commits stay staged in a transaction."""
+    from pyiceberg.catalog.noop import NoopCatalog
+    from pyiceberg.io import load_file_io
+    from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC
+    from pyiceberg.table.metadata import new_table_metadata
+    from pyiceberg.table.sorting import UNSORTED_SORT_ORDER
+
+    metadata = new_table_metadata(
+        schema=Catalog._convert_schema_if_needed(arrow_table_simple.schema, 
format_version=3),
+        partition_spec=UNPARTITIONED_PARTITION_SPEC,
+        sort_order=UNSORTED_SORT_ORDER,
+        location=str(tmp_path),
+        properties={"format-version": "3"},
+    ).model_copy(update={"next_row_id": 0})

Review Comment:
   #3551 already covers this: `new_table_metadata` sets 
`next_row_id=INITIAL_ROW_ID` for V3, as Java's builder does, and it adds 
`test_new_table_metadata_v3_initializes_next_row_id`. I'll drop the 
`model_copy` here once it merges, instead of opening a separate follow-up.



##########
tests/table/test_snapshots.py:
##########
@@ -844,3 +844,109 @@ def 
test_overwrite_rejects_explicit_delete_without_parent_snapshot(
         with empty.transaction() as tx:
             with tx.update_snapshot().overwrite() as overwrite:
                 overwrite.delete_data_file(stale_file)
+
+
+def test_manifest_list_receives_first_row_id_for_v3(table_v3: Table) -> None:
+    """The V3 manifest list writer requires first-row-id, so the producer has 
to supply it."""
+    from unittest import mock
+
+    from pyiceberg.table.update.snapshot import _FastAppendFiles
+
+    txn = table_v3.transaction()
+    append = _FastAppendFiles(operation=Operation.APPEND, transaction=txn, 
io=table_v3.io)
+
+    with (
+        mock.patch("pyiceberg.table.update.snapshot.write_manifest_list") as 
writer,
+        mock.patch.object(_FastAppendFiles, "_manifests", return_value=[]),
+    ):
+        append._commit()
+
+    assert writer.call_args.kwargs["format_version"] == 3
+    assert writer.call_args.kwargs["first_row_id"] == 
table_v3.metadata.next_row_id
+
+
+def test_manifest_list_has_no_first_row_id_for_v2(table_v2: Table) -> None:
+    from unittest import mock
+
+    from pyiceberg.table.update.snapshot import _FastAppendFiles
+
+    txn = table_v2.transaction()
+    append = _FastAppendFiles(operation=Operation.APPEND, transaction=txn, 
io=table_v2.io)
+
+    with (
+        mock.patch("pyiceberg.table.update.snapshot.write_manifest_list") as 
writer,
+        mock.patch.object(_FastAppendFiles, "_manifests", return_value=[]),
+    ):
+        append._commit()
+
+    assert writer.call_args.kwargs["format_version"] == 2
+    assert writer.call_args.kwargs["first_row_id"] is None
+
+
[email protected]
+def empty_table_v3(tmp_path: Path, arrow_table_simple: pa.Table) -> Table:
+    """An empty V3 table; writing V3 metadata files is not supported, so 
commits stay staged in a transaction."""
+    from pyiceberg.catalog.noop import NoopCatalog

Review Comment:
   Done, they're at the top of the file now, `pyarrow.parquet` included.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to