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

laskoviymishka pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 9304132dc fix(table): reject deletion vectors with a mismatched 
partition (#2521)
9304132dc is described below

commit 9304132dcd647c6b7bc48498f107543bcd21d905
Author: Harshil Valecha <[email protected]>
AuthorDate: Sat Nov 28 03:28:29 2026 +0530

    fix(table): reject deletion vectors with a mismatched partition (#2521)
    
    RowDelta.Commit never checked that an added deletion vector carries the
    partition spec and partition of the data file it references. The spec
    requires them to match, and scans match DVs to data files by path only,
    so a DV built with the current spec after partition evolution committed
    and was applied as if valid.
    
    Resolve each added DV's referenced data file, from the same row delta or
    the current snapshot, and reject the commit when its spec ID or
    partition differs.
    
    Closes #2520
---
 table/conflict_validation.go              |  15 +-
 table/dynamic_partition_overwrite_test.go |  20 +-
 table/row_delta.go                        |  91 +++++++--
 table/row_delta_test.go                   | 300 ++++++++++++++++++++++++++++++
 4 files changed, 411 insertions(+), 15 deletions(-)

diff --git a/table/conflict_validation.go b/table/conflict_validation.go
index 2d8a94bc1..882f9ec43 100644
--- a/table/conflict_validation.go
+++ b/table/conflict_validation.go
@@ -571,6 +571,14 @@ func (c *conflictContext) forEachChangedEntryInSnapshot(
 // len(referencedPaths); callers MUST batch referenced paths into a
 // single call rather than calling once per path.
 func validateDataFilesExist(cc *conflictContext, referencedPaths []string) 
error {
+       return validateDataFilesExistMatchingDeletes(cc, referencedPaths, nil)
+}
+
+// validateDataFilesExistMatchingDeletes is validateDataFilesExist that also
+// checks each live data file it finds against the added delete files deletes
+// scopes to it (keyed by data file path) with validateDeletesMatchDataFile,
+// so the partition check shares the same manifest walk.
+func validateDataFilesExistMatchingDeletes(cc *conflictContext, 
referencedPaths []string, deletes map[string][]iceberg.DataFile) error {
        if len(referencedPaths) == 0 {
                return nil
        }
@@ -602,8 +610,11 @@ func validateDataFilesExist(cc *conflictContext, 
referencedPaths []string) error
                        if entry.Status() == iceberg.EntryStatusDELETED {
                                continue
                        }
-                       path := entry.DataFile().FilePath()
-                       delete(needed, path)
+                       data := entry.DataFile()
+                       if err := validateDeletesMatchDataFile(data, 
deletes[data.FilePath()]); err != nil {
+                               return err
+                       }
+                       delete(needed, data.FilePath())
                }
                if len(needed) == 0 {
                        return nil
diff --git a/table/dynamic_partition_overwrite_test.go 
b/table/dynamic_partition_overwrite_test.go
index 1f47b3e97..a7404e3de 100644
--- a/table/dynamic_partition_overwrite_test.go
+++ b/table/dynamic_partition_overwrite_test.go
@@ -600,7 +600,7 @@ func 
TestDynamicPartitionOverwriteKeepsDeleteForSurvivingOldSpecData(t *testing.
        posDelete := newTestPosDeleteFileForSpec(t, currentSpec, 
"file://old-spec-delete.parquet",
                nullPartition, oldData.FilePath())
        addDelete := New(Identifier{"db", "tbl"}, evolved, "metadata.json", 
fsF, nil).NewTransaction()
-       require.NoError(t, 
addDelete.NewRowDelta(nil).AddDeletes(posDelete).Commit(ctx))
+       commitPosDeleteSkippingRowDelta(t, addDelete, fs, posDelete)
        withDelete, err := addDelete.meta.Build()
        require.NoError(t, err)
 
@@ -773,7 +773,7 @@ func 
TestDynamicPartitionOverwritePrunedKeepsDeleteForSurvivingOldSpecData(t *te
        posDelete := newTestPosDeleteFileForSpec(t, currentSpec, 
"file://old-spec-delete.parquet",
                nullPartition, oldData.FilePath())
        addDelete := New(Identifier{"db", "tbl"}, evolved, "metadata.json", 
fsF, nil).NewTransaction()
-       require.NoError(t, 
addDelete.NewRowDelta(nil).AddDeletes(posDelete).Commit(ctx))
+       commitPosDeleteSkippingRowDelta(t, addDelete, fs, posDelete)
        withDelete, err := addDelete.meta.Build()
        require.NoError(t, err)
 
@@ -800,3 +800,19 @@ func 
TestDynamicPartitionOverwritePrunedKeepsDeleteForSurvivingOldSpecData(t *te
        require.Equal(t, []string{posDelete.FilePath()}, deletePaths,
                "deletes for surviving old-spec data must not be dropped")
 }
+
+// commitPosDeleteSkippingRowDelta commits a position delete through the
+// snapshot producer, skipping RowDelta's validation. The fixtures above store
+// an old-spec data file's delete under the current spec with a null partition
+// value. No current iceberg-go writer produces that layout and RowDelta now
+// rejects it, but RowDelta accepted it before that check, so tables committed
+// earlier may contain it.
+func commitPosDeleteSkippingRowDelta(t *testing.T, txn *Transaction, fs 
iceio.WriteFileIO, del iceberg.DataFile) {
+       t.Helper()
+
+       sp := newFastAppendFilesProducer(OpDelete, txn, fs, nil, nil)
+       sp.appendDeleteFile(del)
+       updates, reqs, err := sp.commit(t.Context())
+       require.NoError(t, err)
+       require.NoError(t, txn.apply(updates, reqs))
+}
diff --git a/table/row_delta.go b/table/row_delta.go
index eec09a8a8..3fda05234 100644
--- a/table/row_delta.go
+++ b/table/row_delta.go
@@ -24,7 +24,6 @@ import (
        "maps"
 
        "github.com/apache/iceberg-go"
-       iceberginternal "github.com/apache/iceberg-go/internal"
        iceio "github.com/apache/iceberg-go/io"
 )
 
@@ -272,6 +271,10 @@ func (rd *RowDelta) Commit(ctx context.Context) error {
                return err
        }
 
+       if err := rd.validateDeletesMatchAddedDataFiles(); err != nil {
+               return err
+       }
+
        referenced, err := rd.txn.headDataFiles(ctx, fs, 
rd.referencedDataFiles(), rd.dataFiles)
        if err != nil {
                return err
@@ -367,10 +370,10 @@ func (rd *RowDelta) Commit(ctx context.Context) error {
 }
 
 // referencedDataFiles returns the distinct data files that this delta's
-// position deletes name in referenced_data_file. A position delete that
-// leaves the field unset, such as a v2 file covering several data files,
-// names its targets only in its file_path column, which is not read here,
-// so those targets go unchecked.
+// position deletes target, named in referenced_data_file or in equal
+// file_path bounds (see referencedDataFilePath). A position delete that
+// covers several data files names its targets only in its file_path
+// column, which is not read here, so those targets go unchecked.
 func (rd *RowDelta) referencedDataFiles() []string {
        var referenced []string
        seen := make(map[string]struct{})
@@ -378,13 +381,13 @@ func (rd *RowDelta) referencedDataFiles() []string {
                if f.ContentType() != iceberg.EntryContentPosDeletes {
                        continue
                }
-               ref := iceberginternal.BorrowedDataFileReferencedDataFile(f)
-               if ref == nil || *ref == "" {
+               ref := referencedDataFilePath(f)
+               if ref == "" {
                        continue
                }
-               if _, ok := seen[*ref]; !ok {
-                       seen[*ref] = struct{}{}
-                       referenced = append(referenced, *ref)
+               if _, ok := seen[ref]; !ok {
+                       seen[ref] = struct{}{}
+                       referenced = append(referenced, ref)
                }
        }
 
@@ -494,6 +497,72 @@ func (rd *RowDelta) validateAddedDeletionVectors() error {
        return nil
 }
 
+// deletesByDataFile groups this delta's position deletes that target a
+// single data file, a deletion vector or a v2 position delete that names it
+// in referenced_data_file or in equal file_path bounds, by that file's path.
+func (rd *RowDelta) deletesByDataFile() map[string][]iceberg.DataFile {
+       byRef := make(map[string][]iceberg.DataFile)
+       for _, f := range rd.delFiles {
+               if f.ContentType() != iceberg.EntryContentPosDeletes {
+                       continue
+               }
+               if ref := referencedDataFilePath(f); ref != "" {
+                       byRef[ref] = append(byRef[ref], f)
+               }
+       }
+
+       return byRef
+}
+
+// validateDeletesMatchAddedDataFiles rejects a position delete scoped to a
+// data file added in the same delta when their partition spec or partition
+// differ. Deletes for data files already on the branch head are checked by
+// validateDataFilesExist when the transaction commits.
+func (rd *RowDelta) validateDeletesMatchAddedDataFiles() error {
+       byRef := rd.deletesByDataFile()
+       if len(byRef) == 0 {
+               return nil
+       }
+       for _, df := range rd.dataFiles {
+               if err := validateDeletesMatchDataFile(df, 
byRef[df.FilePath()]); err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
+
+// validateDeletesMatchDataFile rejects a delete in dels, each scoped to
+// data, whose partition spec or partition differs from data's. The spec
+// requires them to match. Scans match deletion vectors by path alone, so a
+// mismatched DV would otherwise commit and be applied, and a mismatched v2
+// position delete would commit a table whose scans fail.
+func validateDeletesMatchDataFile(data iceberg.DataFile, dels 
[]iceberg.DataFile) error {
+       if len(dels) == 0 {
+               return nil
+       }
+       dataKey, err := canonicalPartitionKey(data.SpecID(), 
dataFilePartition(data))
+       if err != nil {
+               return fmt.Errorf("building partition key for data file %s: 
%w", data.FilePath(), err)
+       }
+       for _, del := range dels {
+               if del.SpecID() != data.SpecID() {
+                       return fmt.Errorf("%w: delete file %s uses partition 
spec %d, but its referenced data file %s uses partition spec %d; write the 
delete file with the data file's spec",
+                               ErrInvalidOperation, del.FilePath(), 
del.SpecID(), data.FilePath(), data.SpecID())
+               }
+               delKey, err := canonicalPartitionKey(del.SpecID(), 
dataFilePartition(del))
+               if err != nil {
+                       return fmt.Errorf("building partition key for delete 
file %s: %w", del.FilePath(), err)
+               }
+               if delKey != dataKey {
+                       return fmt.Errorf("%w: delete file %s partition does 
not match its referenced data file %s",
+                               ErrInvalidOperation, del.FilePath(), 
data.FilePath())
+               }
+       }
+
+       return nil
+}
+
 // findReplacedLiveDVs walks the current snapshot's delete manifests and
 // collects all live deletion vectors whose referenced data file this delta
 // adds a replacement for. This is used to enforce the v3 spec invariant
@@ -760,7 +829,7 @@ func (rd *RowDelta) validate(cc *conflictContext, 
referenced []string) error {
        }
 
        if len(referenced) > 0 {
-               if err := validateDataFilesExist(cc, referenced); err != nil {
+               if err := validateDataFilesExistMatchingDeletes(cc, referenced, 
rd.deletesByDataFile()); err != nil {
                        return err
                }
        }
diff --git a/table/row_delta_test.go b/table/row_delta_test.go
index 8734f962c..6ad4ae4b4 100644
--- a/table/row_delta_test.go
+++ b/table/row_delta_test.go
@@ -28,6 +28,7 @@ import (
 
        "github.com/apache/arrow-go/v18/arrow"
        "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/decimal128"
        "github.com/apache/arrow-go/v18/arrow/memory"
        "github.com/apache/arrow-go/v18/parquet"
        "github.com/apache/arrow-go/v18/parquet/pqarrow"
@@ -2439,3 +2440,302 @@ func 
TestRowDeltaDeleteForDataFileRemovedInSameTransaction(t *testing.T) {
                }
        }
 }
+
+// newPartitionedRowDeltaTable returns a table partitioned by identity(data).
+func newPartitionedRowDeltaTable(t *testing.T, formatVersion int) *table.Table 
{
+       t.Helper()
+
+       return newRowDeltaTableWithType(t, formatVersion, 
iceberg.PrimitiveTypes.String)
+}
+
+// newRowDeltaTableWithType returns a table partitioned by identity(data),
+// where data has the given type.
+func newRowDeltaTableWithType(t *testing.T, formatVersion int, dataType 
iceberg.Type) *table.Table {
+       t.Helper()
+
+       location := filepath.ToSlash(t.TempDir())
+
+       schema := iceberg.NewSchema(0,
+               iceberg.NestedField{ID: 1, Name: "id", Type: 
iceberg.PrimitiveTypes.Int64, Required: true},
+               iceberg.NestedField{ID: 2, Name: "data", Type: dataType, 
Required: false},
+       )
+       spec := iceberg.NewPartitionSpec(iceberg.PartitionField{
+               SourceIDs: []int{2}, FieldID: 1000, Transform: 
iceberg.IdentityTransform{}, Name: "data",
+       })
+
+       meta, err := table.NewMetadata(schema, &spec, table.UnsortedSortOrder, 
location,
+               iceberg.Properties{table.PropertyFormatVersion: 
formatVersionStr(formatVersion)})
+       require.NoError(t, err)
+
+       return table.New(
+               table.Identifier{"db", "row_delta_partitioned_test"},
+               meta, location+"/metadata/v1.metadata.json",
+               func(ctx context.Context) (iceio.IO, error) {
+                       return iceio.LocalFS{}, nil
+               },
+               &rowDeltaCatalog{meta},
+       )
+}
+
+func buildPartitionedDataFile(t *testing.T, spec iceberg.PartitionSpec, path 
string, partition map[int]any) iceberg.DataFile {
+       t.Helper()
+
+       b, err := iceberg.NewDataFileBuilder(spec, iceberg.EntryContentData,
+               path, iceberg.ParquetFile, partition, nil, nil, 10, 1024)
+       require.NoError(t, err)
+
+       return b.Build()
+}
+
+func buildPartitionedDV(t *testing.T, spec iceberg.PartitionSpec, path, 
refDataFile string, partition map[int]any) iceberg.DataFile {
+       t.Helper()
+
+       b, err := iceberg.NewDataFileBuilder(spec, 
iceberg.EntryContentPosDeletes,
+               path, iceberg.PuffinFile, partition, nil, nil, 2, 128)
+       require.NoError(t, err)
+
+       return 
b.ReferencedDataFile(refDataFile).ContentOffset(4).ContentSizeInBytes(64).Build()
+}
+
+// commitPartitionedDataFile commits a data file in partition data=a and
+// returns the updated table and the file's path.
+func commitPartitionedDataFile(t *testing.T, tbl *table.Table) (*table.Table, 
string) {
+       t.Helper()
+
+       dataPath := tbl.Location() + "/data/data=a/insert.parquet"
+       tx := tbl.NewTransaction()
+       require.NoError(t, tx.NewRowDelta(nil).
+               AddRows(buildPartitionedDataFile(t, tbl.Spec(), dataPath, 
map[int]any{1000: "a"})).
+               Commit(t.Context()))
+       tbl, err := tx.Commit(t.Context())
+       require.NoError(t, err)
+
+       return tbl, dataPath
+}
+
+// commitDeletesErr stages dels in a row delta and commits the transaction,
+// returning the first error. Deletes for data files already on the branch
+// head are checked when the transaction commits.
+func commitDeletesErr(t *testing.T, tbl *table.Table, dels 
...iceberg.DataFile) error {
+       t.Helper()
+
+       tx := tbl.NewTransaction()
+       if err := tx.NewRowDelta(nil).AddDeletes(dels...).Commit(t.Context()); 
err != nil {
+               return err
+       }
+       _, err := tx.Commit(t.Context())
+
+       return err
+}
+
+// Why: the spec requires a delete file to carry the partition of the data
+// file it references, and scans match deletion vectors by path only.
+// Condition: a DV for a committed data file in data=a declares data=b.
+// Assertion: Commit rejects it; the same DV with data=a commits.
+func TestRowDeltaDVPartitionMustMatchDataFile(t *testing.T) {
+       t.Run("mismatched partition", func(t *testing.T) {
+               tbl, dataPath := commitPartitionedDataFile(t, 
newPartitionedRowDeltaTable(t, 3))
+
+               dv := buildPartitionedDV(t, tbl.Spec(), 
tbl.Location()+"/data/dv.puffin", dataPath, map[int]any{1000: "b"})
+               err := commitDeletesErr(t, tbl, dv)
+               require.ErrorIs(t, err, table.ErrInvalidOperation)
+               require.ErrorContains(t, err, "partition does not match")
+       })
+
+       t.Run("matching partition", func(t *testing.T) {
+               tbl, dataPath := commitPartitionedDataFile(t, 
newPartitionedRowDeltaTable(t, 3))
+
+               dv := buildPartitionedDV(t, tbl.Spec(), 
tbl.Location()+"/data/dv.puffin", dataPath, map[int]any{1000: "a"})
+               tx := tbl.NewTransaction()
+               require.NoError(t, 
tx.NewRowDelta(nil).AddDeletes(dv).Commit(t.Context()))
+               _, err := tx.Commit(t.Context())
+               require.NoError(t, err)
+       })
+
+       t.Run("data file added in the same row delta", func(t *testing.T) {
+               tbl := newPartitionedRowDeltaTable(t, 3)
+               dataPath := tbl.Location() + "/data/data=a/insert.parquet"
+
+               err := tbl.NewTransaction().NewRowDelta(nil).
+                       AddRows(buildPartitionedDataFile(t, tbl.Spec(), 
dataPath, map[int]any{1000: "a"})).
+                       AddDeletes(buildPartitionedDV(t, tbl.Spec(), 
tbl.Location()+"/data/dv.puffin", dataPath, map[int]any{1000: "b"})).
+                       Commit(t.Context())
+               require.ErrorIs(t, err, table.ErrInvalidOperation)
+               require.ErrorContains(t, err, "partition does not match")
+       })
+}
+
+// Why: after partition evolution a caller can build a DV with the current
+// spec instead of the older spec its data file was written with.
+// Condition: the data file uses spec 0, the DV uses the evolved spec 1.
+// Assertion: Commit rejects the spec mismatch.
+func TestRowDeltaDVSpecMustMatchDataFileAfterEvolution(t *testing.T) {
+       tbl, dataPath := commitPartitionedDataFile(t, 
newPartitionedRowDeltaTable(t, 3))
+
+       tx := tbl.NewTransaction()
+       require.NoError(t, tx.UpdateSpec(false).AddIdentity("id").Commit())
+       tbl, err := tx.Commit(t.Context())
+       require.NoError(t, err)
+       spec := tbl.Spec()
+       require.Equal(t, 1, spec.ID())
+
+       dv := buildPartitionedDV(t, spec, tbl.Location()+"/data/dv.puffin", 
dataPath,
+               map[int]any{1000: "a", 1001: int64(1)})
+       err = commitDeletesErr(t, tbl, dv)
+       require.ErrorIs(t, err, table.ErrInvalidOperation)
+       require.ErrorContains(t, err, "uses partition spec 1")
+}
+
+// Why: manifests decode decimal partition values as DecimalLiteral while
+// callers and writers build iceberg.Decimal; the commit check must compare
+// them by value.
+// Condition: a decimal(9,2) data file is committed, then a DV for it.
+// Assertion: a DV with the same decimal partition commits; one with a
+// different decimal is rejected.
+func TestRowDeltaDVDecimalPartitionComparedByValue(t *testing.T) {
+       commitDecimalDataFile := func(t *testing.T) (*table.Table, string) {
+               t.Helper()
+
+               tbl := newRowDeltaTableWithType(t, 3, 
iceberg.MustDecimalTypeOf(9, 2))
+               dataPath := tbl.Location() + "/data/insert.parquet"
+               partition := map[int]any{1000: iceberg.Decimal{Val: 
decimal128.FromI64(1234), Scale: 2}}
+               tx := tbl.NewTransaction()
+               require.NoError(t, tx.NewRowDelta(nil).
+                       AddRows(buildPartitionedDataFile(t, tbl.Spec(), 
dataPath, partition)).
+                       Commit(t.Context()))
+               tbl, err := tx.Commit(t.Context())
+               require.NoError(t, err)
+
+               return tbl, dataPath
+       }
+
+       t.Run("same decimal", func(t *testing.T) {
+               tbl, dataPath := commitDecimalDataFile(t)
+
+               dv := buildPartitionedDV(t, tbl.Spec(), 
tbl.Location()+"/data/dv.puffin", dataPath,
+                       map[int]any{1000: iceberg.Decimal{Val: 
decimal128.FromI64(1234), Scale: 2}})
+               tx := tbl.NewTransaction()
+               require.NoError(t, 
tx.NewRowDelta(nil).AddDeletes(dv).Commit(t.Context()))
+               _, err := tx.Commit(t.Context())
+               require.NoError(t, err)
+       })
+
+       t.Run("different decimal", func(t *testing.T) {
+               tbl, dataPath := commitDecimalDataFile(t)
+
+               dv := buildPartitionedDV(t, tbl.Spec(), 
tbl.Location()+"/data/dv.puffin", dataPath,
+                       map[int]any{1000: iceberg.Decimal{Val: 
decimal128.FromI64(9999), Scale: 2}})
+               err := commitDeletesErr(t, tbl, dv)
+               require.ErrorIs(t, err, table.ErrInvalidOperation)
+               require.ErrorContains(t, err, "partition does not match")
+       })
+}
+
+// Why: v2 position deletes that set referenced_data_file are bound to one
+// data file the same way DVs are, and iceberg-go's scan rejects a mismatch.
+// Condition: a v2 position delete for a data=a file declares data=b.
+// Assertion: Commit rejects it instead of committing an unscannable table.
+func TestRowDeltaV2PosDeletePartitionMustMatchReferencedDataFile(t *testing.T) 
{
+       tbl, dataPath := commitPartitionedDataFile(t, 
newPartitionedRowDeltaTable(t, 2))
+
+       b, err := iceberg.NewDataFileBuilder(tbl.Spec(), 
iceberg.EntryContentPosDeletes,
+               tbl.Location()+"/data/pos-del.parquet", iceberg.ParquetFile, 
map[int]any{1000: "b"}, nil, nil, 5, 512)
+       require.NoError(t, err)
+
+       err = commitDeletesErr(t, tbl, b.ReferencedDataFile(dataPath).Build())
+       require.ErrorIs(t, err, table.ErrInvalidOperation)
+       require.ErrorContains(t, err, "partition does not match")
+}
+
+// Why: manifests decode date partition values as iceberg.Date, while a
+// writer that applies the transform itself holds the day as an int32.
+// Condition: a v3 DV for a date-partitioned data file carries the same day
+// as an int32.
+// Assertion: Commit accepts it; a different day is still rejected.
+func TestRowDeltaDVDatePartitionComparedByValue(t *testing.T) {
+       commitDateDataFile := func(t *testing.T) (*table.Table, string) {
+               t.Helper()
+
+               tbl := newRowDeltaTableWithType(t, 3, 
iceberg.PrimitiveTypes.Date)
+               dataPath := tbl.Location() + "/data/insert.parquet"
+               tx := tbl.NewTransaction()
+               require.NoError(t, tx.NewRowDelta(nil).
+                       AddRows(buildPartitionedDataFile(t, tbl.Spec(), 
dataPath, map[int]any{1000: iceberg.Date(19000)})).
+                       Commit(t.Context()))
+               tbl, err := tx.Commit(t.Context())
+               require.NoError(t, err)
+
+               return tbl, dataPath
+       }
+
+       t.Run("same day", func(t *testing.T) {
+               tbl, dataPath := commitDateDataFile(t)
+
+               dv := buildPartitionedDV(t, tbl.Spec(), 
tbl.Location()+"/data/dv.puffin", dataPath,
+                       map[int]any{1000: int32(19000)})
+               tx := tbl.NewTransaction()
+               require.NoError(t, 
tx.NewRowDelta(nil).AddDeletes(dv).Commit(t.Context()))
+               _, err := tx.Commit(t.Context())
+               require.NoError(t, err)
+       })
+
+       t.Run("different day", func(t *testing.T) {
+               tbl, dataPath := commitDateDataFile(t)
+
+               dv := buildPartitionedDV(t, tbl.Spec(), 
tbl.Location()+"/data/dv.puffin", dataPath,
+                       map[int]any{1000: int32(19001)})
+               err := commitDeletesErr(t, tbl, dv)
+               require.ErrorIs(t, err, table.ErrInvalidOperation)
+               require.ErrorContains(t, err, "partition does not match")
+       })
+}
+
+// Why: a v2 position delete whose file_path lower and upper bounds are equal
+// targets one data file even without referenced_data_file, and scans scope
+// it to that file the same way.
+// Condition: such a delete for a data=a file declares data=b.
+// Assertion: Commit rejects it.
+func TestRowDeltaV2PosDeleteScopedByBoundsMustMatchDataFile(t *testing.T) {
+       tbl, dataPath := commitPartitionedDataFile(t, 
newPartitionedRowDeltaTable(t, 2))
+
+       filePath, ok := 
iceberg.PositionalDeleteSchema.FindFieldByName("file_path")
+       require.True(t, ok)
+       bound, err := iceberg.StringLiteral(dataPath).MarshalBinary()
+       require.NoError(t, err)
+
+       b, err := iceberg.NewDataFileBuilder(tbl.Spec(), 
iceberg.EntryContentPosDeletes,
+               tbl.Location()+"/data/pos-del.parquet", iceberg.ParquetFile, 
map[int]any{1000: "b"}, nil, nil, 5, 512)
+       require.NoError(t, err)
+       del := b.LowerBoundValues(map[int][]byte{filePath.ID: bound}).
+               UpperBoundValues(map[int][]byte{filePath.ID: bound}).
+               Build()
+
+       err = commitDeletesErr(t, tbl, del)
+       require.ErrorIs(t, err, table.ErrInvalidOperation)
+       require.ErrorContains(t, err, "partition does not match")
+}
+
+// Why: a delete scoped to one data file only by equal file_path bounds must
+// go through the same existence check as one that sets referenced_data_file.
+// Condition: such a delete names a data file that is not in the table.
+// Assertion: the transaction commit reports the missing data file.
+func TestRowDeltaV2PosDeleteScopedByBoundsRequiresLiveDataFile(t *testing.T) {
+       tbl, _ := commitPartitionedDataFile(t, newPartitionedRowDeltaTable(t, 
2))
+       missing := tbl.Location() + "/data/data=a/missing.parquet"
+
+       filePath, ok := 
iceberg.PositionalDeleteSchema.FindFieldByName("file_path")
+       require.True(t, ok)
+       bound, err := iceberg.StringLiteral(missing).MarshalBinary()
+       require.NoError(t, err)
+
+       b, err := iceberg.NewDataFileBuilder(tbl.Spec(), 
iceberg.EntryContentPosDeletes,
+               tbl.Location()+"/data/pos-del.parquet", iceberg.ParquetFile, 
map[int]any{1000: "a"}, nil, nil, 5, 512)
+       require.NoError(t, err)
+       del := b.LowerBoundValues(map[int][]byte{filePath.ID: bound}).
+               UpperBoundValues(map[int][]byte{filePath.ID: bound}).
+               Build()
+
+       err = commitDeletesErr(t, tbl, del)
+       require.ErrorIs(t, err, table.ErrDataFilesMissing)
+       require.ErrorContains(t, err, missing)
+}

Reply via email to