You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2021/03/11 09:00:52 UTC

[GitHub] [iceberg] openinx commented on a change in pull request #2320: Core: add delete row reader

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



##########
File path: data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
##########
@@ -110,6 +113,41 @@ protected long pos(T record) {
     return applyEqDeletes(applyPosDeletes(records));
   }
 
+  public CloseableIterable<T> matchEqDeletes(CloseableIterable<T> records) {
+    if (eqDeletes.isEmpty()) {
+      return records;
+    }
+
+    Multimap<Set<Integer>, DeleteFile> filesByDeleteIds = Multimaps.newMultimap(Maps.newHashMap(), Lists::newArrayList);
+    for (DeleteFile delete : eqDeletes) {
+      filesByDeleteIds.put(Sets.newHashSet(delete.equalityFieldIds()), delete);
+    }
+
+    List<Predicate<T>> deleteSetFilters = Lists.newArrayList();
+    for (Map.Entry<Set<Integer>, Collection<DeleteFile>> entry : filesByDeleteIds.asMap().entrySet()) {
+      Set<Integer> ids = entry.getKey();
+      Iterable<DeleteFile> deletes = entry.getValue();
+
+      Schema deleteSchema = TypeUtil.select(requiredSchema, ids);
+
+      // a projection to select and reorder fields of the file schema to match the delete rows
+      StructProjection projectRow = StructProjection.create(requiredSchema, deleteSchema);
+
+      Iterable<CloseableIterable<Record>> deleteRecords = Iterables.transform(deletes,
+          delete -> openDeletes(delete, deleteSchema));
+      StructLikeSet deleteSet = Deletes.toEqualitySet(
+          // copy the delete records because they will be held in a set
+          CloseableIterable.transform(CloseableIterable.concat(deleteRecords), Record::copy),
+          deleteSchema.asStruct());
+
+      Predicate<T> predicate = record -> deleteSet.contains(projectRow.wrap(asStructLike(record)));
+      deleteSetFilters.add(predicate);
+    }

Review comment:
       I saw almost all of the code are the same between the `matchEqDeletes` and `applyEqDeletes`,  actually the similar code are generating the `Predicate` that whether a row is in one of the equality deletion set.  So I think we could get them into one method to generate those `Predicates` .   The `matchEqDeletes` is actually finding out all the rows that has been deleted by equality deletes,  we could use the  `inDeleteSet1 OR inDeleteSet2 OR inDeleteSet3` to find out the expected rows;  the `appyEqDeletes` is actually finding out all the rows that should be visible users after applying equality deletions,  we could use the `NOT inDeleteSet1 AND NOT inDeleteSet2 AND NOT inDeleteSet3` to find out the visible rows.  I tried to make patch for this: 
   
   ```diff
   From cbdc488d96d1ee3e51f019898db27c73846c46bb Mon Sep 17 00:00:00 2001
   From: huzheng <op...@gmail.com>
   Date: Thu, 11 Mar 2021 16:55:20 +0800
   Subject: [PATCH] Reuse the codes
   
   ---
    .../apache/iceberg/util/ChainedFilter.java    | 42 ----------
    .../org/apache/iceberg/data/DeleteFilter.java | 77 ++++++++++---------
    .../iceberg/spark/source/DeleteRowReader.java |  2 +-
    3 files changed, 40 insertions(+), 81 deletions(-)
    delete mode 100644 core/src/main/java/org/apache/iceberg/util/ChainedFilter.java
   
   diff --git a/core/src/main/java/org/apache/iceberg/util/ChainedFilter.java b/core/src/main/java/org/apache/iceberg/util/ChainedFilter.java
   deleted file mode 100644
   index a2a26c366..000000000
   --- a/core/src/main/java/org/apache/iceberg/util/ChainedFilter.java
   +++ /dev/null
   @@ -1,42 +0,0 @@
   -/*
   - * Licensed to the Apache Software Foundation (ASF) under one
   - * or more contributor license agreements.  See the NOTICE file
   - * distributed with this work for additional information
   - * regarding copyright ownership.  The ASF licenses this file
   - * to you under the Apache License, Version 2.0 (the
   - * "License"); you may not use this file except in compliance
   - * with the License.  You may obtain a copy of the License at
   - *
   - *   http://www.apache.org/licenses/LICENSE-2.0
   - *
   - * Unless required by applicable law or agreed to in writing,
   - * software distributed under the License is distributed on an
   - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   - * KIND, either express or implied.  See the License for the
   - * specific language governing permissions and limitations
   - * under the License.
   - */
   -
   -package org.apache.iceberg.util;
   -
   -import java.util.List;
   -import java.util.function.Predicate;
   -
   -public class ChainedFilter<T> extends Filter<T> {
   -  private final List<Predicate<T>> filters;
   -
   -  public ChainedFilter(List<Predicate<T>> filters) {
   -    this.filters = filters;
   -  }
   -
   -  @Override
   -  protected boolean shouldKeep(T item) {
   -    for (Predicate<T> filter : filters) {
   -      if (filter.test(item)) {
   -        return true;
   -      }
   -    }
   -
   -    return false;
   -  }
   -}
   diff --git a/data/src/main/java/org/apache/iceberg/data/DeleteFilter.java b/data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
   index 285195cb6..201fb20ee 100644
   --- a/data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
   +++ b/data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
   @@ -49,7 +49,6 @@ import org.apache.iceberg.relocated.com.google.common.collect.Multimaps;
    import org.apache.iceberg.relocated.com.google.common.collect.Sets;
    import org.apache.iceberg.types.TypeUtil;
    import org.apache.iceberg.types.Types;
   -import org.apache.iceberg.util.ChainedFilter;
    import org.apache.iceberg.util.Filter;
    import org.apache.iceberg.util.StructLikeSet;
    import org.apache.iceberg.util.StructProjection;
   @@ -113,44 +112,30 @@ public abstract class DeleteFilter<T> {
        return applyEqDeletes(applyPosDeletes(records));
      }
    
   -  public CloseableIterable<T> matchEqDeletes(CloseableIterable<T> records) {
   -    if (eqDeletes.isEmpty()) {
   -      return records;
   -    }
   -
   -    Multimap<Set<Integer>, DeleteFile> filesByDeleteIds = Multimaps.newMultimap(Maps.newHashMap(), Lists::newArrayList);
   -    for (DeleteFile delete : eqDeletes) {
   -      filesByDeleteIds.put(Sets.newHashSet(delete.equalityFieldIds()), delete);
   -    }
   -
   -    List<Predicate<T>> deleteSetFilters = Lists.newArrayList();
   -    for (Map.Entry<Set<Integer>, Collection<DeleteFile>> entry : filesByDeleteIds.asMap().entrySet()) {
   -      Set<Integer> ids = entry.getKey();
   -      Iterable<DeleteFile> deletes = entry.getValue();
   -
   -      Schema deleteSchema = TypeUtil.select(requiredSchema, ids);
   -
   -      // a projection to select and reorder fields of the file schema to match the delete rows
   -      StructProjection projectRow = StructProjection.create(requiredSchema, deleteSchema);
   +  public CloseableIterable<T> findEqualityDeleteRows(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);
    
   -      Iterable<CloseableIterable<Record>> deleteRecords = Iterables.transform(deletes,
   -          delete -> openDeletes(delete, deleteSchema));
   -      StructLikeSet deleteSet = Deletes.toEqualitySet(
   -          // copy the delete records because they will be held in a set
   -          CloseableIterable.transform(CloseableIterable.concat(deleteRecords), Record::copy),
   -          deleteSchema.asStruct());
   -
   -      Predicate<T> predicate = record -> deleteSet.contains(projectRow.wrap(asStructLike(record)));
   -      deleteSetFilters.add(predicate);
   -    }
   +    Filter<T> deletedRowsFilter = new Filter<T>() {
   +      @Override
   +      protected boolean shouldKeep(T item) {
   +        return deletedRows.test(item);
   +      }
   +    };
    
   -    Filter<T> findDeleteRows = new ChainedFilter<>(deleteSetFilters);
   -    return findDeleteRows.filter(records);
   +    return deletedRowsFilter.filter(records);
      }
    
   -  private CloseableIterable<T> applyEqDeletes(CloseableIterable<T> records) {
   +  /**
   +   * Returns a list of {@link Predicate}s, and each {@link Predicate} indicate whether a row is in an equality delete
   +   * set.
   +   */
   +  private List<Predicate<T>> applyEqDeletes() {
   +    List<Predicate<T>> isInDeleteSets = Lists.newArrayList();
        if (eqDeletes.isEmpty()) {
   -      return records;
   +      return isInDeleteSets;
        }
    
        Multimap<Set<Integer>, DeleteFile> filesByDeleteIds = Multimaps.newMultimap(Maps.newHashMap(), Lists::newArrayList);
   @@ -158,7 +143,6 @@ public abstract class DeleteFilter<T> {
          filesByDeleteIds.put(Sets.newHashSet(delete.equalityFieldIds()), delete);
        }
    
   -    CloseableIterable<T> filteredRecords = records;
        for (Map.Entry<Set<Integer>, Collection<DeleteFile>> entry : filesByDeleteIds.asMap().entrySet()) {
          Set<Integer> ids = entry.getKey();
          Iterable<DeleteFile> deletes = entry.getValue();
   @@ -175,11 +159,28 @@ public abstract class DeleteFilter<T> {
              CloseableIterable.transform(CloseableIterable.concat(deleteRecords), Record::copy),
              deleteSchema.asStruct());
    
   -      filteredRecords = Deletes.filter(filteredRecords,
   -          record -> projectRow.wrap(asStructLike(record)), deleteSet);
   +      Predicate<T> isInDeleteSet = record -> deleteSet.contains(projectRow.wrap(asStructLike(record)));
   +      isInDeleteSets.add(isInDeleteSet);
        }
    
   -    return filteredRecords;
   +    return isInDeleteSets;
   +  }
   +
   +  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);
   +
   +    Filter<T> remainingRowsFilter = new Filter<T>() {
   +      @Override
   +      protected boolean shouldKeep(T item) {
   +        return remainingRows.test(item);
   +      }
   +    };
   +
   +    return remainingRowsFilter.filter(records);
      }
    
      private CloseableIterable<T> applyPosDeletes(CloseableIterable<T> records) {
   diff --git a/spark/src/main/java/org/apache/iceberg/spark/source/DeleteRowReader.java b/spark/src/main/java/org/apache/iceberg/spark/source/DeleteRowReader.java
   index d8dc55266..5c4311a33 100644
   --- a/spark/src/main/java/org/apache/iceberg/spark/source/DeleteRowReader.java
   +++ b/spark/src/main/java/org/apache/iceberg/spark/source/DeleteRowReader.java
   @@ -59,7 +59,7 @@ public class DeleteRowReader extends RowDataReader {
        // update the current file for Spark's filename() function
        InputFileBlockHolder.set(file.path().toString(), task.start(), task.length());
    
   -    return matches.matchEqDeletes(open(task, requiredSchema, idToConstant)).iterator();
   +    return matches.findEqualityDeleteRows(open(task, requiredSchema, idToConstant)).iterator();
      }
    
      protected class SparkDeleteMatcher extends DeleteFilter<InternalRow> {
   -- 
   2.20.1 (Apple Git-117)
   ```




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org