openinx commented on a change in pull request #2216:
URL: https://github.com/apache/iceberg/pull/2216#discussion_r589150323
##########
File path: data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
##########
@@ -110,7 +110,44 @@ protected long pos(T record) {
return applyEqDeletes(applyPosDeletes(records));
}
- private CloseableIterable<T> applyEqDeletes(CloseableIterable<T> records) {
+ public CloseableIterable<T> matchEqDeletes(CloseableIterable<T> records) {
Review comment:
I'd like to introduce a `ChainFilter` to simply the whole filter logics:
```patch
From ae394375024e11cd90e2cde280bbbf9ea9999c46 Mon Sep 17 00:00:00 2001
From: huzheng <[email protected]>
Date: Mon, 8 Mar 2021 11:30:50 +0800
Subject: [PATCH] filter
---
.../org/apache/iceberg/deletes/Deletes.java | 36 ----------------
.../org/apache/iceberg/util/ChainFilter.java | 42 +++++++++++++++++++
.../org/apache/iceberg/data/DeleteFilter.java | 12 ++++--
3 files changed, 50 insertions(+), 40 deletions(-)
create mode 100644
core/src/main/java/org/apache/iceberg/util/ChainFilter.java
diff --git a/core/src/main/java/org/apache/iceberg/deletes/Deletes.java
b/core/src/main/java/org/apache/iceberg/deletes/Deletes.java
index c2f21fe19..62154f7d6 100644
--- a/core/src/main/java/org/apache/iceberg/deletes/Deletes.java
+++ b/core/src/main/java/org/apache/iceberg/deletes/Deletes.java
@@ -24,7 +24,6 @@ import java.io.UncheckedIOException;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
-import java.util.function.BiFunction;
import java.util.function.Function;
import org.apache.iceberg.Accessor;
import org.apache.iceberg.MetadataColumns;
@@ -41,10 +40,8 @@ import
org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.types.Comparators;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.util.Filter;
-import org.apache.iceberg.util.Pair;
import org.apache.iceberg.util.SortedMerge;
import org.apache.iceberg.util.StructLikeSet;
-import org.apache.iceberg.util.StructProjection;
public class Deletes {
private static final Schema POSITION_DELETE_SCHEMA = new Schema(
@@ -80,17 +77,6 @@ public class Deletes {
return filter.filter(rows);
}
- public static <T> CloseableIterable<T> match(CloseableIterable<T> rows,
- BiFunction<T,
StructProjection, StructLike> rowToDeleteKey,
- List<Pair<StructProjection,
StructLikeSet>> unprojectedDeleteSets) {
- if (unprojectedDeleteSets.isEmpty()) {
- return rows;
- }
-
- EqualitySetDeleteMatcher<T> equalityFilter = new
EqualitySetDeleteMatcher<>(rowToDeleteKey, unprojectedDeleteSets);
- return equalityFilter.filter(rows);
- }
-
public static StructLikeSet toEqualitySet(CloseableIterable<StructLike>
eqDeletes, Types.StructType eqType) {
try (CloseableIterable<StructLike> deletes = eqDeletes) {
StructLikeSet deleteSet = StructLikeSet.create(eqType);
@@ -157,28 +143,6 @@ public class Deletes {
}
}
- private static class EqualitySetDeleteMatcher<T> extends Filter<T> {
- private final List<Pair<StructProjection, StructLikeSet>> deleteSets;
- private final BiFunction<T, StructProjection, StructLike>
extractEqStruct;
-
- protected EqualitySetDeleteMatcher(BiFunction<T, StructProjection,
StructLike> extractEq,
- List<Pair<StructProjection,
StructLikeSet>> deleteSets) {
- this.extractEqStruct = extractEq;
- this.deleteSets = deleteSets;
- }
-
- @Override
- protected boolean shouldKeep(T row) {
- for (Pair<StructProjection, StructLikeSet> deleteSet : deleteSets) {
- if (deleteSet.second().contains(extractEqStruct.apply(row,
deleteSet.first()))) {
- return true;
- }
- }
-
- return false;
- }
- }
-
private static class PositionSetDeleteFilter<T> extends Filter<T> {
private final Function<T, Long> rowToPosition;
private final Set<Long> deleteSet;
diff --git a/core/src/main/java/org/apache/iceberg/util/ChainFilter.java
b/core/src/main/java/org/apache/iceberg/util/ChainFilter.java
new file mode 100644
index 000000000..354b0dc38
--- /dev/null
+++ b/core/src/main/java/org/apache/iceberg/util/ChainFilter.java
@@ -0,0 +1,42 @@
+/*
+ * 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 ChainFilter<T> extends Filter<T> {
+
+ private final List<Predicate<T>> filters;
+
+ public ChainFilter(List<Predicate<T>> filters) {
+ this.filters = filters;
+ }
+
+ @Override
+ protected boolean shouldKeep(T item) {
+ for (Predicate<T> filter : filters) {
+ if (!filter.test(item)) {
+ return false;
+ }
+ }
+ 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 884641355..6d4986ebf 100644
--- a/data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
+++ b/data/src/main/java/org/apache/iceberg/data/DeleteFilter.java
@@ -23,6 +23,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.function.Predicate;
import org.apache.iceberg.Accessor;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DeleteFile;
@@ -48,7 +49,8 @@ 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.Pair;
+import org.apache.iceberg.util.ChainFilter;
+import org.apache.iceberg.util.Filter;
import org.apache.iceberg.util.StructLikeSet;
import org.apache.iceberg.util.StructProjection;
import org.apache.parquet.Preconditions;
@@ -121,7 +123,7 @@ public abstract class DeleteFilter<T> {
filesByDeleteIds.put(Sets.newHashSet(delete.equalityFieldIds()),
delete);
}
- List<Pair<StructProjection, StructLikeSet>> unprojectedDeleteSets =
Lists.newArrayList();
+ 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();
@@ -138,10 +140,12 @@ public abstract class DeleteFilter<T> {
CloseableIterable.transform(CloseableIterable.concat(deleteRecords),
Record::copy),
deleteSchema.asStruct());
- unprojectedDeleteSets.add(Pair.of(projectRow, deleteSet));
+ Predicate<T> predicate = record ->
deleteSet.contains(projectRow.wrap(asStructLike(record)));
+ deleteSetFilters.add(predicate);
}
- return Deletes.match(records, (record, projection) ->
projection.wrap(asStructLike(record)), unprojectedDeleteSets);
+ Filter<T> findDeleteRows = new ChainFilter<>(deleteSetFilters);
+ return findDeleteRows.filter(records);
}
protected CloseableIterable<T> applyEqDeletes(CloseableIterable<T>
records) {
--
2.20.1 (Apple Git-117)
```
##########
File path: core/src/main/java/org/apache/iceberg/BaseRewriteFiles.java
##########
@@ -57,4 +57,23 @@ public RewriteFiles rewriteFiles(Set<DataFile>
filesToDelete, Set<DataFile> file
return this;
}
+
+ @Override
+ public RewriteFiles rewriteDeletes(Set<DeleteFile> deletesToDelete,
Set<DeleteFile> deletesToAdd) {
+ Preconditions.checkArgument(deletesToDelete != null &&
!deletesToDelete.isEmpty(),
+ "Files to delete cannot be null or empty");
+ Preconditions.checkArgument(deletesToAdd != null &&
!deletesToAdd.isEmpty(),
+ "Files to add can not be null or empty");
+
+ for (DeleteFile toDelete : deletesToDelete) {
+ delete(toDelete);
Review comment:
Nit: for the replacing equality delete with position deletes, we'd
better to assert the delete files to be equality delete files ? the delete
file to add MUST be position delete files ?
##########
File path: core/src/main/java/org/apache/iceberg/deletes/Deletes.java
##########
@@ -77,6 +80,17 @@ private Deletes() {
return filter.filter(rows);
}
+ public static <T> CloseableIterable<T> match(CloseableIterable<T> rows,
Review comment:
`match` is not a good candidate for me to express the meaning of finding
the existing row data that hits the equality delete sets. I may need a better
name for this.
##########
File path: api/src/main/java/org/apache/iceberg/RewriteFiles.java
##########
@@ -42,4 +42,13 @@
* @return this for method chaining
*/
RewriteFiles rewriteFiles(Set<DataFile> filesToDelete, Set<DataFile>
filesToAdd);
+
+ /**
+ * Add a rewrite that replaces one set of deletes with another that contains
the same deleted rows.
+ *
+ * @param deletesToDelete files that will be replaced, cannot be null or
empty.
+ * @param deletesToAdd files that will be added, cannot be null or empty.
+ * @return this for method chaining
+ */
+ RewriteFiles rewriteDeletes(Set<DeleteFile> deletesToDelete, Set<DeleteFile>
deletesToAdd);
Review comment:
Before we start the replacing equality deletes with position deletes, I
think we need to refactor the RewriteFiles API to adjust more cases:
1. Rewrite data files and remove all the delete rows. The files to delete
will be a set of data files and a set of delete files, and the files to add
will be a set of data files.
2. Replace equality deletes with position deletes, the files to delete will
be a set of equality delete files (we will need to ensure that all delete files
are equality delete files ? ) , the files to add will be a set of position
delete files.
3. Merging small delete files into a bigger delete files. The files to
delete will be a set of equality/position delete files, the files to add will
be a set of equality/position delete files.
----------------------------------------------------------------
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]