harshilv17 commented on code in PR #2521:
URL: https://github.com/apache/iceberg-go/pull/2521#discussion_r4722861865


##########
table/row_delta.go:
##########
@@ -494,6 +498,85 @@ func (rd *RowDelta) validateAddedDeletionVectors() error {
        return nil
 }
 
+// validateDeletesMatchDataFiles rejects an added position delete file
+// scoped to 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, when
+// its partition spec or partition differs from that data file'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. A reference to a data file that is neither
+// added here nor live in the current snapshot is left to the conflict
+// checks.
+func (rd *RowDelta) validateDeletesMatchDataFiles(ctx context.Context, fs 
iceio.IO, meta *MetadataBuilder) error {
+       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)
+               }
+       }
+       if len(byRef) == 0 {
+               return nil
+       }
+
+       check := func(data iceberg.DataFile) error {
+               dels, ok := byRef[data.FilePath()]
+               if !ok {
+                       return nil
+               }
+               delete(byRef, data.FilePath())
+               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("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",
+                                       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("delete file %s partition 
does not match its referenced data file %s",
+                                       del.FilePath(), data.FilePath())
+                       }
+               }
+
+               return nil
+       }
+
+       for _, df := range rd.dataFiles {
+               if err := check(df); err != nil {
+                       return err
+               }
+       }
+       if len(byRef) == 0 {
+               return nil
+       }
+
+       snap := rd.txn.planningSnapshot(meta)
+       if snap == nil {
+               return nil
+       }
+       for entry, err := range snap.entries(ctx, fs, 
iceberg.ManifestContentData, true) {

Review Comment:
   Moved it into the head walk in be2035d8: 
`validateDataFilesExistMatchingDeletes` checks each live data file against the 
deletes scoped to it, so the commit reads the head data manifests once and the 
check runs against the real head on retry. `validateDataFilesExist` keeps its 
signature for the other callers. The check against `rd.dataFiles` stays up 
front. One gap: a data file added by an earlier row delta in the same 
transaction isn't on the head yet, so its deletes are no longer 
partition-checked. I can add that back if you want it.
   



##########
table/row_delta.go:
##########
@@ -494,6 +498,85 @@ func (rd *RowDelta) validateAddedDeletionVectors() error {
        return nil
 }
 
+// validateDeletesMatchDataFiles rejects an added position delete file
+// scoped to 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, when
+// its partition spec or partition differs from that data file'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. A reference to a data file that is neither
+// added here nor live in the current snapshot is left to the conflict
+// checks.
+func (rd *RowDelta) validateDeletesMatchDataFiles(ctx context.Context, fs 
iceio.IO, meta *MetadataBuilder) error {
+       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)
+               }
+       }
+       if len(byRef) == 0 {
+               return nil
+       }
+
+       check := func(data iceberg.DataFile) error {
+               dels, ok := byRef[data.FilePath()]
+               if !ok {
+                       return nil
+               }
+               delete(byRef, data.FilePath())
+               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("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",

Review Comment:
   Both errors wrap `ErrInvalidOperation` now, and the tests check it with 
`errors.Is`.
   



##########
table/row_delta.go:
##########
@@ -494,6 +498,85 @@ func (rd *RowDelta) validateAddedDeletionVectors() error {
        return nil
 }
 
+// validateDeletesMatchDataFiles rejects an added position delete file
+// scoped to 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, when
+// its partition spec or partition differs from that data file'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. A reference to a data file that is neither
+// added here nor live in the current snapshot is left to the conflict
+// checks.
+func (rd *RowDelta) validateDeletesMatchDataFiles(ctx context.Context, fs 
iceio.IO, meta *MetadataBuilder) error {
+       byRef := make(map[string][]iceberg.DataFile)
+       for _, f := range rd.delFiles {
+               if f.ContentType() != iceberg.EntryContentPosDeletes {
+                       continue
+               }
+               if ref := referencedDataFilePath(f); ref != "" {

Review Comment:
   Switched `referencedDataFiles()` to `referencedDataFilePath`, so a delete 
scoped only by bounds now goes through the existence check too. Added 
`TestRowDeltaV2PosDeleteScopedByBoundsRequiresLiveDataFile`, which fails 
without the change.
   



##########
table/row_delta.go:
##########
@@ -494,6 +498,85 @@ func (rd *RowDelta) validateAddedDeletionVectors() error {
        return nil
 }
 
+// validateDeletesMatchDataFiles rejects an added position delete file
+// scoped to 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, when
+// its partition spec or partition differs from that data file's. The spec 
requires them to match. Scans

Review Comment:
   That comment is gone. The check is split into 
`validateDeletesMatchAddedDataFiles` and `validateDeletesMatchDataFile` with 
rewrapped docs.
   



##########
table/dynamic_partition_overwrite_test.go:
##########
@@ -800,3 +800,18 @@ func 
TestDynamicPartitionOverwritePrunedKeepsDeleteForSurvivingOldSpecData(t *te
        require.Equal(t, []string{posDelete.FilePath()}, deletePaths,
                "deletes for surviving old-spec data must not be dropped")
 }
+
+// commitLegacyPosDelete commits a position delete through the snapshot

Review Comment:
   None I know of. The layout comes from the #2381 fixtures, and RowDelta 
accepted it before this check. I reworded the comment to say that and renamed 
the helper to `commitPosDeleteSkippingRowDelta`.
   



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