Fokko commented on code in PR #4333:
URL: https://github.com/apache/iceberg-python/pull/4333#discussion_r4698721956


##########
tests/table/test_commit_retry.py:
##########
@@ -747,8 +747,82 @@ def 
test_validate_concurrency_raises_on_missing_starting_snapshot(catalog: Catal
 
     from pyiceberg.table.update.snapshot import CommitWindow
 
-    with pytest.raises(ValidationException, match="Cannot find starting 
snapshot"):
-        CommitWindow.resolve(table.metadata, base_id=99999999, branch="main")
+    window = CommitWindow.resolve(table.metadata, base_id=99999999, 
branch="main")
+
+    assert window.base is None
+    assert window.base_missing
+    assert window.head is not None
+    assert not window.is_empty()
+
+
+def _hiding_starting_snapshot(rebuild: Any) -> Any:
+    def patched_rebuild(self_tx: Any) -> None:
+        metadata_cls = type(self_tx._table.metadata)
+        base_id = self_tx._snapshot_producers[0]._starting_snapshot_id
+        original_snapshot_by_id = metadata_cls.snapshot_by_id
+
+        def snapshot_by_id(self_md: Any, snapshot_id: int) -> Any:
+            return None if snapshot_id == base_id else 
original_snapshot_by_id(self_md, snapshot_id)
+
+        with patch.object(metadata_cls, "snapshot_by_id", snapshot_by_id):
+            rebuild(self_tx)
+
+    return patched_rebuild
+
+
+def test_append_retry_without_starting_snapshot(catalog: Catalog) -> None:
+    """An append retries on catalogs that only expose the current snapshot."""
+    catalog.create_namespace("default")
+    schema = _test_schema()
+    catalog.create_table("default.hidden_history_append_test", schema=schema)
+
+    import pyarrow as pa
+
+    import pyiceberg.table as _table_module
+
+    df = pa.table({"x": [1, 2, 3]})
+    table = catalog.load_table("default.hidden_history_append_test")
+    table.append(df)
+
+    tbl1 = catalog.load_table("default.hidden_history_append_test")
+    tbl2 = catalog.load_table("default.hidden_history_append_test")
+
+    tbl1.append(df)
+
+    RuntimeTransaction = _table_module.Transaction
+    original_rebuild = RuntimeTransaction._rebuild_snapshot_updates
+
+    with patch.object(RuntimeTransaction, "_rebuild_snapshot_updates", 
_hiding_starting_snapshot(original_rebuild)):
+        tbl2.append(df)
+
+    assert 
len(catalog.load_table("default.hidden_history_append_test").scan().to_arrow()) 
== 9
+
+
+def test_delete_retry_without_starting_snapshot_raises_commit_failed(catalog: 
Catalog) -> None:
+    """A delete cannot validate without the starting snapshot and raises 
CommitFailedException."""
+    catalog.create_namespace("default")
+    schema = _test_schema()
+    catalog.create_table("default.hidden_history_delete_test", schema=schema)
+
+    import pyarrow as pa
+
+    import pyiceberg.table as _table_module
+
+    df = pa.table({"x": [1, 2, 3]})
+    table = catalog.load_table("default.hidden_history_delete_test")
+    table.append(df)
+
+    tbl1 = catalog.load_table("default.hidden_history_delete_test")
+    tbl2 = catalog.load_table("default.hidden_history_delete_test")
+
+    tbl1.append(df)
+
+    RuntimeTransaction = _table_module.Transaction
+    original_rebuild = RuntimeTransaction._rebuild_snapshot_updates
+
+    with patch.object(RuntimeTransaction, "_rebuild_snapshot_updates", 
_hiding_starting_snapshot(original_rebuild)):
+        with pytest.raises(CommitFailedException, match="Cannot determine 
history"):

Review Comment:
   This test hides only the starting snapshot while `tbl1`'s commit stays 
visible, so Java would validate this case instead of failing. It would be nice 
to have one test where the delete succeeds with a single concurrent commit, and 
one where two concurrent commits make the history unresolvable.



##########
pyiceberg/table/update/snapshot.py:
##########
@@ -487,6 +491,12 @@ def _validate_concurrency(self) -> None:
         if self._commit_window is None or self._commit_window.is_empty():
             return
 
+        if self._commit_window.base_missing:
+            raise CommitFailedException(
+                f"Cannot determine history between starting snapshot 
{self._commit_window.base_id} "

Review Comment:
   When the chain really is broken, Java raises `ValidationException` with this 
message. Raising `CommitFailedException` instead is a reasonable choice, but it 
diverges from Java, so I'd call it out explicitly (or align with Java).



##########
pyiceberg/table/update/snapshot.py:
##########
@@ -109,26 +110,29 @@ class CommitWindow:
 
     base: The snapshot when the operation began (exclusive lower bound, fixed 
across retries).
     head: The branch HEAD snapshot after metadata refresh (inclusive upper 
bound).
+    base_id: The starting snapshot id, also kept when the catalog no longer 
exposes it.
+    base_missing: True when the starting snapshot is not in the refreshed 
metadata, for
+        example when the catalog only exposes the current snapshot.
     """
 
     base: Snapshot | None
     head: Snapshot | None
+    base_id: int | None = None
+    base_missing: bool = False

Review Comment:
   If validation works from the id, `CommitWindow` only needs `base_id` and 
`base_missing` can go. `_starting_sequence_number` would also need to handle a 
missing starting snapshot; Java's `startingSequenceNumber` falls back to 
`INITIAL_SEQUENCE_NUMBER` in that case.



##########
pyiceberg/table/update/snapshot.py:
##########
@@ -487,6 +491,12 @@ def _validate_concurrency(self) -> None:
         if self._commit_window is None or self._commit_window.is_empty():
             return
 
+        if self._commit_window.base_missing:

Review Comment:
   Java doesn't bail out here. `MergingSnapshotProducer.validationHistory` 
walks back from the head with 
`SnapshotUtil.ancestorsBetween(parent.snapshotId(), startingSnapshotId, 
base::snapshot)`, which stops on the id and never looks up the starting 
snapshot. It then checks:
   
   ```java
   ValidationException.check(
       lastSnapshot == null || Objects.equals(lastSnapshot.parentId(), 
startingSnapshotId),
       "Cannot determine history between starting snapshot %s and the last 
known ancestor %s", ...);
   ```
   
   So only the snapshots after the starting snapshot need to be present. With a 
single concurrent commit (the common case on Nessie), the head's parent is the 
starting snapshot and validation runs normally. Could we make 
`_validation_history` take `from_snapshot_id` and do the same parent id check 
(`ancestors_between_ids` already does most of the walk)? Then this guard isn't 
needed.



-- 
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