Fokko commented on code in PR #4102:
URL: https://github.com/apache/iceberg-python/pull/4102#discussion_r4798651931
##########
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:
I see that `.model_copy(update={"next_row_id": 0})` works around
`new_table_metadata` leaving `next_row_id` as `None`. In Java, the builder
initializes it to `INITIAL_ROW_ID`
([TableMetadata.java#L975](https://github.com/apache/iceberg/blob/3747194049915f33c5accb5455273550cba04d0c/core/src/main/java/org/apache/iceberg/TableMetadata.java#L975)),
and the spec marks `next-row-id` as required for v3. Could we open a small
follow-up that defaults `TableMetadataV3.next_row_id` to `INITIAL_ROW_ID`? Then
this workaround can go away.
##########
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:
`cast` is a bit of a code smell here. In Java, `nextRowId()` lives on the
base `ManifestListWriter` and returns `null` for V1/V2
([ManifestListWriter.java#L95-L97](https://github.com/apache/iceberg/blob/3747194049915f33c5accb5455273550cba04d0c/core/src/main/java/org/apache/iceberg/ManifestListWriter.java#L95-L97)),
so the producer can just do `writer.nextRowId() - base.nextRowId()`
([SnapshotProducer.java#L359](https://github.com/apache/iceberg/blob/3747194049915f33c5accb5455273550cba04d0c/core/src/main/java/org/apache/iceberg/SnapshotProducer.java#L359)).
Could we add a `next_row_id -> int | None` property to `ManifestListWriter`
that returns `None`? That would let us drop the `cast` and the
`ManifestListWriterV3` import:
```python
if first_row_id is not None and (next_row_id := writer.next_row_id) is not
None:
added_rows = next_row_id - first_row_id
```
##########
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:
The spec says: "If a commit is retried, the `first-row-id` must be
reassigned based on the table's current `next-row-id`". I think
`_rebuild_snapshot_updates` already handles this because it calls `_commit()`
again on refreshed metadata, but nothing tests it. How about adding `3` to the
`format_version` parametrize of `test_conflict_append_append` in
`tests/integration/test_writes/test_optimistic_concurrency.py`, and asserting
that the second snapshot's `first_row_id` starts where the first one ended?
WDYT?
##########
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:
Nit: could we move these imports to the top of the file? The same goes for
`import pyarrow.parquet as pq` further down.
--
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]