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


##########
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:
   The equivalent check on the rewrite path 
(`validateAddedDeletionVectorTargets`) wraps `ErrInvalidOperation`. Could these 
two errors (here and L544) wrap it too, so callers can match them 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 != "" {
+                       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:
   This walks the planning snapshot's live data entries until every reference 
is found, which in the worst case (a missing reference, or a target in the 
oldest manifest) means the whole table. `RowDelta.validate` then calls 
`validateDataFilesExist`, which always runs and walks the same head data 
manifests (O(all data manifests × entries) per its doc). A data file's 
partition never changes, so this check could move into that walk: pass a map 
from data file path to the expected (spec, partition key) of its deletes, and 
compare when the entry is found. That's one manifest pass per commit, and it 
also checks against the real head on retry. The check against `rd.dataFiles` 
added in the same delta can stay up front.



##########
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:
   This resolves the target with `referencedDataFilePath`, which includes equal 
`file_path` bounds, but the existence check gets its paths from 
`rd.referencedDataFiles()`, which only reads the explicit 
`referenced_data_file`. So for a delete scoped only by bounds, a missing target 
isn't covered by any conflict check, contrary to the doc comment above ("left 
to the conflict checks"). Switching `referencedDataFiles()` to 
`referencedDataFilePath` would line the two up (matching Java's 
`ContentFileUtil.referencedDataFile`) and fits well with folding this into 
`validateDataFilesExist`.



##########
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:
   Which writer produces this layout? On main, the position-delete fanout 
writer takes the spec ID from each data file's `partitionContext` 
(`pos_delete_partitioned_fanout_writer.go`), and the merge-on-read path in 
`transaction_delete.go` commits through its own producer rather than 
`RowDelta`. If this describes an older iceberg-go or another engine, the 
comment should say so. nit: the `table_test` package already has a 
`commitLegacyPosDelete` with a different signature 
(`row_lineage_prune_delete_test.go`); a different name would be less confusing.



##########
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:
   nit: this line runs past the wrap width of the rest of the comment. Could 
you reflow it?



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