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 2022/05/01 02:57:16 UTC

[GitHub] [iceberg] zinking commented on a diff in pull request #1288: Update scan planning with DeleteFiles in each task

zinking commented on code in PR #1288:
URL: https://github.com/apache/iceberg/pull/1288#discussion_r862416404


##########
core/src/main/java/org/apache/iceberg/DeleteFileIndex.java:
##########
@@ -0,0 +1,270 @@
+/*
+ * 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;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.expressions.ManifestEvaluator;
+import org.apache.iceberg.expressions.Projections;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.ListMultimap;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Multimaps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.util.Pair;
+import org.apache.iceberg.util.StructLikeWrapper;
+import org.apache.iceberg.util.Tasks;
+
+/**
+ * An index of {@link DeleteFile delete files} by sequence number.
+ * <p>
+ * Use {@link #builderFor(FileIO, Iterable)} to construct an index, and {@link #forDataFile(int, long, DataFile)} or
+ * {@link #forEntry(int, ManifestEntry)} to get the the delete files to apply to a given data file.
+ */
+class DeleteFileIndex {
+  private static final DeleteFile[] NO_DELETE_FILES = new DeleteFile[0];
+
+  private final long[] globalSeqs;
+  private final DeleteFile[] globalDeletes;
+  private final Map<Pair<Integer, StructLikeWrapper>, Pair<long[], DeleteFile[]>> sortedDeletesByPartition;
+  private final ThreadLocal<StructLikeWrapper> lookupWrapper = ThreadLocal.withInitial(
+      () -> StructLikeWrapper.wrap(null));
+
+  DeleteFileIndex(long[] globalSeqs, DeleteFile[] globalDeletes,
+                  Map<Pair<Integer, StructLikeWrapper>, Pair<long[], DeleteFile[]>> sortedDeletesByPartition) {
+    this.globalSeqs = globalSeqs;
+    this.globalDeletes = globalDeletes;
+    this.sortedDeletesByPartition = sortedDeletesByPartition;
+  }
+
+  DeleteFile[] forEntry(int specId, ManifestEntry<DataFile> entry) {
+    return forDataFile(specId, entry.sequenceNumber(), entry.file());
+  }
+
+  DeleteFile[] forDataFile(int specId, long sequenceNumber, DataFile file) {
+    Pair<long[], DeleteFile[]> partitionDeletes = sortedDeletesByPartition
+        .get(Pair.of(specId, lookupWrapper.get().set(file.partition())));
+
+    if (partitionDeletes == null) {
+      return limitBySequenceNumber(sequenceNumber, globalSeqs, globalDeletes);
+    } else if (globalDeletes == null) {
+      return limitBySequenceNumber(sequenceNumber, partitionDeletes.first(), partitionDeletes.second());
+    } else {
+      return Stream.concat(
+          Stream.of(limitBySequenceNumber(sequenceNumber, globalSeqs, globalDeletes)),
+          Stream.of(limitBySequenceNumber(sequenceNumber, partitionDeletes.first(), partitionDeletes.second()))
+      ).toArray(DeleteFile[]::new);
+    }
+  }
+
+  private static DeleteFile[] limitBySequenceNumber(long sequenceNumber, long[] seqs, DeleteFile[] files) {
+    if (files == null) {
+      return NO_DELETE_FILES;
+    }
+
+    int pos = Arrays.binarySearch(seqs, sequenceNumber);
+    int start;
+    if (pos < 0) {
+      // the sequence number was not found, where it would be inserted is -(pos + 1)
+      start = -(pos + 1);

Review Comment:
   > Yes, and the tests validate these cases.
   > 
   > If the sequence number is not found and less than all of the sequence numbers in the array, then the insert position is 0 and the `pos` returned is `-(0 + 1) = -1`. Converting back to a start position `-(-1 + 1) = 0`, so we copy the entire array. Similarly, if the sequence number is greater than all of the numbers in the array, the return values is `length`, which results in `copyOfRange(files, length, length)` and produces a 0-length array.
   
   naive question, since not found returns -1, why not just set start to 0 ?



-- 
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: issues-unsubscribe@iceberg.apache.org

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