openinx commented on a change in pull request #2364:
URL: https://github.com/apache/iceberg/pull/2364#discussion_r678179542



##########
File path: data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
##########
@@ -139,43 +173,122 @@ protected long pos(T record) {
           CloseableIterable.transform(CloseableIterable.concat(deleteRecords), 
Record::copy),
           deleteSchema.asStruct());
 
-      Predicate<T> isInDeleteSet = record -> 
deleteSet.contains(projectRow.wrap(asStructLike(record)));
-      isInDeleteSets.add(isInDeleteSet);
+      isDeleted = isDeleted == null ? record -> 
deleteSet.contains(projectRow.wrap(asStructLike(record))) :
+              isDeleted.or(record -> 
deleteSet.contains(projectRow.wrap(asStructLike(record))));
     }
 
-    return isInDeleteSets;
+    return isDeleted;
   }
 
-  public CloseableIterable<T> findEqualityDeleteRows(CloseableIterable<T> 
records) {
+  private Predicate<T> buildPosDeletePredicate() {
+    if (posDeletes.isEmpty()) {
+      return null;
+    }
+
+    List<CloseableIterable<Record>> deletes = Lists.transform(posDeletes, 
this::openPosDeletes);
+    Set<Long> deleteSet = Deletes.toPositionSet(dataFile.path(), 
CloseableIterable.concat(deletes));
+    if (deleteSet.isEmpty()) {
+      return null;
+    }
+
+    return record -> deleteSet.contains(pos(record));
+  }
+
+  public CloseableIterable<T> keepRowsFromDeletes(CloseableIterable<T> 
records) {
+    Predicate<T> isDeletedFromPosDeletes = buildPosDeletePredicate();
+    if (isDeletedFromPosDeletes == null) {
+      return keepRowsFromEqualityDeletes(records);
+    }
+
+    Predicate<T> isDeletedFromEqDeletes = buildEqDeletePredicate();
+    if (isDeletedFromEqDeletes == null) {
+      return keepRowsFromPosDeletes(records);
+    }
+
+    CloseableIterable<T> markedRecords;
+
+    if (posDeletes.stream().mapToLong(DeleteFile::recordCount).sum() < 
setFilterThreshold) {
+      markedRecords = CloseableIterable.transform(records, record -> {
+        if (isDeletedFromPosDeletes.test(record) || 
isDeletedFromEqDeletes.test(record)) {
+          deleteMarker().accept(record);
+        }
+        return record;
+      });
+
+    } else {
+      List<CloseableIterable<Record>> deletes = Lists.transform(posDeletes, 
this::openPosDeletes);
+      markedRecords = 
CloseableIterable.transform(Deletes.streamingDeletedRowMarker(records, 
this::pos,
+          Deletes.deletePositions(dataFile.path(), deletes), deleteMarker()), 
record -> {
+          if (!isDeletedRow(record) && isDeletedFromEqDeletes.test(record)) {
+            deleteMarker().accept(record);
+          }
+          return record;
+        });
+    }
+    return deletedRowsSelector().filter(markedRecords);
+  }
+
+  private CloseableIterable<T> selectRowsFromDeletes(CloseableIterable<T> 
records, Predicate<T> isDeleted) {
+    CloseableIterable<T> markedRecords = CloseableIterable.transform(records, 
record -> {
+      if (isDeleted.test(record)) {
+        deleteMarker().accept(record);
+      }
+      return record;
+    });
+
+    return deletedRowsSelector().filter(markedRecords);
+  }
+
+  public CloseableIterable<T> keepRowsFromEqualityDeletes(CloseableIterable<T> 
records) {
     // Predicate to test whether a row has been deleted by equality deletions.
-    Predicate<T> deletedRows = applyEqDeletes().stream()
-        .reduce(Predicate::or)
-        .orElse(t -> false);
+    Predicate<T> isDeleted = buildEqDeletePredicate();
+    if (isDeleted == null) {
+      return CloseableIterable.empty();
+    }
 
-    Filter<T> deletedRowsFilter = new Filter<T>() {
-      @Override
-      protected boolean shouldKeep(T item) {
-        return deletedRows.test(item);
+    return selectRowsFromDeletes(records, isDeleted);
+  }
+
+  public CloseableIterable<T> keepRowsFromPosDeletes(CloseableIterable<T> 
records) {
+    // if there are fewer deletes than a reasonable number to keep in memory, 
use a set
+    if (posDeletes.stream().mapToLong(DeleteFile::recordCount).sum() < 
setFilterThreshold) {
+      // Predicate to test whether a row has been deleted by equality 
deletions.
+      Predicate<T> isDeleted = buildPosDeletePredicate();
+      if (isDeleted == null) {
+        return CloseableIterable.empty();
       }
-    };
-    return deletedRowsFilter.filter(records);
+      return selectRowsFromDeletes(records, isDeleted);
+    } else {
+      List<CloseableIterable<Record>> deletes = Lists.transform(posDeletes, 
this::openPosDeletes);
+      CloseableIterable<T> markedRecords = 
Deletes.streamingDeletedRowMarker(records, this::pos,
+              Deletes.deletePositions(dataFile.path(), deletes), 
deleteMarker());
+
+      return deletedRowsSelector().filter(markedRecords);
+    }
   }
 
   private CloseableIterable<T> applyEqDeletes(CloseableIterable<T> records) {
     // Predicate to test whether a row should be visible to user after 
applying equality deletions.
-    Predicate<T> remainingRows = applyEqDeletes().stream()
-        .map(Predicate::negate)
-        .reduce(Predicate::and)
-        .orElse(t -> true);
+    Predicate<T> isDeleted = buildEqDeletePredicate();

Review comment:
       Looks like we are separating the RewriteDeletes path and normal read 
path into two branches: 
   For the RewriteDeletes path, we introduced three new methods: 
   * keepRowsFromDeletes
   * keepRowsFromEqualityDeletes
   * keepRowsFromPosDeletes
   
   For the normal read path, we introduced another three methods: 
   * applyEqDeletes
   * applyPosDeletes
   * filter
   
   I remember there's an issue that we discussed to introduce the `is_deleted` 
meta column because we want to unify all the rewrite paths and normal read path 
?  ( I cannot find the specific PR 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