This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-4347-e69c6e7153a2c4a39ed669683e116766d7ea6cf4
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git

commit 36069b76e16b09d144d0b6450776d860975f25af
Author: Rahul Shivu Mahadev <[email protected]>
AuthorDate: Wed Nov 25 19:52:41 2026 +0000

    Test add_files duplicate checks on branches (#4347)
    
    * Check add_files duplicates against the target branch
    
    add_files(branch=...) checked duplicates only against the current
    snapshot, so adding a file to a branch twice committed it again and
    doubled the rows. The check now also covers the target branch's
    snapshot, reading shared snapshots and manifests once.
    
    Co-authored-by: Isaac <[email protected]>
    
    * Keep only the add_files branch tests
    
    #4446 made the duplicate check use the target branch, so the code change is 
no
    longer needed. Keep the branch tests as coverage for it, and drop the test 
for
    a file referenced only by main, which #4446 allows.
    
    Co-authored-by: Isaac <[email protected]>
    
    ---------
    
    Co-authored-by: Isaac <[email protected]>
---
 tests/integration/test_add_files.py | 58 +++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/tests/integration/test_add_files.py 
b/tests/integration/test_add_files.py
index a1d45451d..e44db89f9 100644
--- a/tests/integration/test_add_files.py
+++ b/tests/integration/test_add_files.py
@@ -22,6 +22,7 @@ import re
 import threading
 from collections.abc import Iterator
 from datetime import date
+from pathlib import Path
 from unittest import mock
 
 import pyarrow as pa
@@ -1040,3 +1041,60 @@ def test_add_files_to_branch(spark: SparkSession, 
session_catalog: Catalog, form
 
     for col in branch_df.columns:
         assert branch_df.filter(branch_df[col].isNotNull()).count() == 6, 
"Expected all 6 rows to be non-null"
+
+
+def _create_table_with_branch(catalog: Catalog, branch: str) -> Table:
+    catalog.create_namespace("default")
+    tbl = catalog.create_table("default.test_add_files_branch_duplicate", 
schema=TABLE_SCHEMA)
+    tbl.append(ARROW_TABLE)
+    assert tbl.metadata.current_snapshot_id is not None
+    
tbl.manage_snapshots().create_branch(snapshot_id=tbl.metadata.current_snapshot_id,
 branch_name=branch).commit()
+    return tbl
+
+
[email protected]
+def 
test_add_files_to_branch_rejects_file_already_referenced_by_branch(catalog: 
Catalog, tmp_path: Path) -> None:
+    branch = "branch1"
+    tbl = _create_table_with_branch(catalog, branch)
+    file_path = f"file://{tmp_path}/duplicate.parquet"
+    _write_parquet(tbl.io, file_path, ARROW_SCHEMA, ARROW_TABLE)
+
+    tbl.add_files(file_paths=[file_path], branch=branch)
+    with pytest.raises(ValueError, match="already referenced by table"):
+        tbl.add_files(file_paths=[file_path], branch=branch)
+
+    # the branch holds the row from main and the row from the file, each 
exactly once
+    branch_rows = 
tbl.scan(snapshot_id=tbl.metadata.refs[branch].snapshot_id).to_arrow().to_pylist()
+    assert branch_rows == ARROW_TABLE.to_pylist() * 2
+
+
[email protected]
+def 
test_add_files_to_branch_rejects_file_added_earlier_in_transaction(catalog: 
Catalog, tmp_path: Path) -> None:
+    branch = "branch1"
+    tbl = _create_table_with_branch(catalog, branch)
+    file_path = f"file://{tmp_path}/duplicate.parquet"
+    _write_parquet(tbl.io, file_path, ARROW_SCHEMA, ARROW_TABLE)
+
+    with tbl.transaction() as tx:
+        tx.add_files(file_paths=[file_path], branch=branch)
+        with pytest.raises(ValueError, match="already referenced by table"):
+            tx.add_files(file_paths=[file_path], branch=branch)
+
+    branch_rows = 
tbl.scan(snapshot_id=tbl.metadata.refs[branch].snapshot_id).to_arrow().to_pylist()
+    assert branch_rows == ARROW_TABLE.to_pylist() * 2
+
+
[email protected]
+def test_add_files_to_branch_accepts_distinct_file_with_same_rows(catalog: 
Catalog, tmp_path: Path) -> None:
+    branch = "branch1"
+    tbl = _create_table_with_branch(catalog, branch)
+    first_path = f"file://{tmp_path}/first.parquet"
+    second_path = f"file://{tmp_path}/second.parquet"
+    _write_parquet(tbl.io, first_path, ARROW_SCHEMA, ARROW_TABLE)
+    _write_parquet(tbl.io, second_path, ARROW_SCHEMA, ARROW_TABLE)
+
+    tbl.add_files(file_paths=[first_path], branch=branch)
+    tbl.add_files(file_paths=[second_path], branch=branch)
+
+    branch_rows = 
tbl.scan(snapshot_id=tbl.metadata.refs[branch].snapshot_id).to_arrow().to_pylist()
+    assert branch_rows == ARROW_TABLE.to_pylist() * 3

Reply via email to