gengliangwang commented on PR #10962:
URL: https://github.com/apache/iceberg/pull/10962#issuecomment-7450719536

   This still reproduces on 1.12.0, with the core API alone. A minimal program 
for it (it only writes metadata; the data and delete files are never opened):
   
   <details>
   <summary>EqDeleteDroppedByRewrite.java</summary>
   
   ```java
   import java.util.List;
   import java.util.Map;
   import org.apache.hadoop.conf.Configuration;
   import org.apache.iceberg.DataFile;
   import org.apache.iceberg.DataFiles;
   import org.apache.iceberg.DeleteFile;
   import org.apache.iceberg.FileMetadata;
   import org.apache.iceberg.FileScanTask;
   import org.apache.iceberg.PartitionSpec;
   import org.apache.iceberg.Schema;
   import org.apache.iceberg.Snapshot;
   import org.apache.iceberg.Table;
   import org.apache.iceberg.hadoop.HadoopTables;
   import org.apache.iceberg.io.CloseableIterable;
   import org.apache.iceberg.types.Types;
   
   /**
    * A rewrite of data files that keeps the sequence number of the snapshot it 
read
    * (RewriteFiles.dataSequenceNumber, as RewriteDataFiles does with 
use-starting-sequence-number)
    * against an equality delete that is committed while the rewrite runs.
    *
    * <p>Usage: EqDeleteDroppedByRewrite &lt;empty directory&gt; 
[commit-between]
    *
    * <p>With "commit-between", one more snapshot is committed between the 
equality delete and the
    * rewrite. Only metadata is written: the data and delete files are never 
opened.
    */
   public class EqDeleteDroppedByRewrite {
     public static void main(String[] args) throws Exception {
       String location = args[0];
       boolean commitBetween = args.length > 1 && 
args[1].equals("commit-between");
   
       Schema schema =
           new Schema(
               Types.NestedField.required(1, "id", Types.LongType.get()),
               Types.NestedField.optional(2, "data", Types.StringType.get()));
       PartitionSpec spec = PartitionSpec.unpartitioned();
       Table table =
           new HadoopTables(new Configuration())
               .create(schema, spec, Map.of("format-version", "2"), location);
   
       // Snapshot 1 (sequence number 1): one data file. The rewrite starts 
from this snapshot.
       DataFile fileA = dataFile(spec, location + "/data/a.parquet");
       table.newAppend().appendFile(fileA).commit();
       Snapshot readByRewrite = table.currentSnapshot();
   
       // Snapshot 2 (sequence number 2): an equality delete, committed while 
the rewrite runs.
       // It applies to every data file with a smaller data sequence number, so 
to a.parquet.
       DeleteFile equalityDelete =
           FileMetadata.deleteFileBuilder(spec)
               .ofEqualityDeletes(1)
               .withPath(location + "/data/eq-delete.parquet")
               .withFileSizeInBytes(10)
               .withRecordCount(1)
               .build();
       table.newRowDelta().addDeletes(equalityDelete).commit();
   
       if (commitBetween) {
         // Snapshot 3 (sequence number 3): any other commit, here an append.
         table.newAppend().appendFile(dataFile(spec, location + 
"/data/b.parquet")).commit();
       }
   
       print(table, "before the rewrite is committed");
   
       // The rewrite: a.parquet is replaced by a file with the same rows, 
which keeps the
       // sequence number of the snapshot that was read, so that the equality 
delete still
       // applies to it. The validation ignores equality deletes for that 
reason.
       table
           .newRewrite()
           .validateFromSnapshot(readByRewrite.snapshotId())
           .dataSequenceNumber(readByRewrite.sequenceNumber())
           .deleteFile(fileA)
           .addFile(dataFile(spec, location + "/data/a-rewritten.parquet"))
           .commit();
   
       print(table, "after the rewrite is committed");
       System.out.println("summary of the rewrite: " + 
table.currentSnapshot().summary());
     }
   
     private static DataFile dataFile(PartitionSpec spec, String path) {
       return DataFiles.builder(spec)
           .withPath(path)
           .withFileSizeInBytes(100)
           .withRecordCount(10)
           .build();
     }
   
     private static void print(Table table, String when) throws Exception {
       table.refresh();
       System.out.println("== " + when + " (snapshot sequence number "
           + table.currentSnapshot().sequenceNumber() + ")");
       try (CloseableIterable<FileScanTask> tasks = 
table.newScan().planFiles()) {
         for (FileScanTask task : tasks) {
           List<String> deletes = 
task.deletes().stream().map(DeleteFile::location).toList();
           String name = 
task.file().location().substring(task.file().location().lastIndexOf('/') + 1);
           System.out.println("  " + name + " (data sequence number "
               + task.file().dataSequenceNumber() + "): deletes applied = "
               + deletes.stream().map(d -> d.substring(d.lastIndexOf('/') + 
1)).toList());
         }
       }
     }
   }
   ```
   
   </details>
   
   Output of `EqDeleteDroppedByRewrite <dir> commit-between` with Iceberg 
1.12.0:
   
   ```
   == before the rewrite is committed (snapshot sequence number 3)
     b.parquet (data sequence number 3): deletes applied = []
     a.parquet (data sequence number 1): deletes applied = [eq-delete.parquet]
   == after the rewrite is committed (snapshot sequence number 4)
     a-rewritten.parquet (data sequence number 1): deletes applied = []
     b.parquet (data sequence number 3): deletes applied = []
   summary of the rewrite: {..., added-data-files=1, deleted-data-files=1, 
removed-equality-delete-files=1, removed-delete-files=1, 
removed-equality-deletes=1, ..., total-delete-files=0, 
total-equality-deletes=0, ...}
   ```
   
   Without the commit between the equality delete and the rewrite, the delete 
file is kept and still applies to `a-rewritten.parquet`.
   
   This is the sequence of #10312: a rewrite that keeps the sequence number of 
the snapshot it read (`RewriteFiles.dataSequenceNumber`, which 
`RewriteDataFiles` uses by default), an equality delete committed while the 
rewrite runs, one more commit, and no data file older than the delete left in 
the table other than the rewritten ones. The rewrite's commit then removes the 
equality delete, because `minDataSequenceNumber` in 
`MergingSnapshotProducer.apply` is computed without the data files that the 
commit itself adds with the older sequence number, and the deleted rows are 
visible again.
   
   It would be great to get this fix in; the branch has conflicts with main by 
now.
   


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