You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "amogh-jahagirdar (via GitHub)" <gi...@apache.org> on 2023/01/22 23:36:40 UTC

[GitHub] [iceberg] amogh-jahagirdar commented on a diff in pull request #6581: Spark 3.3: Add RemoveDanglingDeletes action

amogh-jahagirdar commented on code in PR #6581:
URL: https://github.com/apache/iceberg/pull/6581#discussion_r1083561493


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/actions/RemoveDanglingDeletesSparkAction.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.spark.actions;
+
+import static org.apache.iceberg.MetadataTableType.ENTRIES;
+import static org.apache.spark.sql.functions.min;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.DeleteFiles;
+import org.apache.iceberg.FileMetadata;
+import org.apache.iceberg.PartitionData;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Partitioning;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.actions.RemoveDanglingDeleteFiles;
+import org.apache.iceberg.actions.RemoveDanglingDeleteFilesActionResult;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.spark.JobGroupInfo;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.api.java.function.MapFunction;
+import org.apache.spark.sql.Column;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Encoders;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema;
+
+/**
+ * An action that removes dangling delete files from the current snapshot. A delete file is dangling
+ * if its deletes no longer applies to any data file.
+ *
+ * <p>The following dangling delete files are removed:
+ *
+ * <ul>
+ *   <li>Position delete files with a sequence number less than that of any data file in the same
+ *       partition
+ *   <li>Equality delete files with a sequence number less than or equal to that of any data file in
+ *       the same partition
+ * </ul>
+ */
+public class RemoveDanglingDeletesSparkAction
+    extends BaseSnapshotUpdateSparkAction<RemoveDanglingDeletesSparkAction>
+    implements RemoveDanglingDeleteFiles {
+
+  private final Table table;
+
+  protected RemoveDanglingDeletesSparkAction(SparkSession spark, Table table) {
+    super(spark);
+    this.table = table;
+  }
+
+  @Override
+  protected RemoveDanglingDeletesSparkAction self() {
+    return this;
+  }
+
+  @Override
+  public Result execute() {
+    if (table.specs().size() == 1 && table.spec().isUnpartitioned()) {
+      // ManifestFilterManager already performs this table-wide on each commit
+      return RemoveDanglingDeleteFilesActionResult.empty();
+    }
+
+    String desc = String.format("Remove dangling delete files for %s", table.name());
+    JobGroupInfo info = newJobGroupInfo("REWRITE-MANIFESTS", desc);
+    return withJobGroupInfo(info, this::doExecute);
+  }
+
+  private Result doExecute() {
+    Dataset<Row> entries =
+        loadMetadataTable(table, ENTRIES)
+            .filter("status < 2") // live entries
+            .selectExpr(
+                "data_file.partition as partition",
+                "data_file.spec_id as spec_id",
+                "data_file.file_path as file_path",
+                "data_file.content as content",
+                "data_file.file_size_in_bytes as file_size_in_bytes",

Review Comment:
   Sorry if it's a naive question, do we need to project file_size_in_bytes for this action? 



##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/actions/RemoveDanglingDeletesSparkAction.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.spark.actions;
+
+import static org.apache.iceberg.MetadataTableType.ENTRIES;
+import static org.apache.spark.sql.functions.min;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.DeleteFiles;
+import org.apache.iceberg.FileMetadata;
+import org.apache.iceberg.PartitionData;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Partitioning;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.actions.RemoveDanglingDeleteFiles;
+import org.apache.iceberg.actions.RemoveDanglingDeleteFilesActionResult;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.spark.JobGroupInfo;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.api.java.function.MapFunction;
+import org.apache.spark.sql.Column;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Encoders;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema;
+
+/**
+ * An action that removes dangling delete files from the current snapshot. A delete file is dangling
+ * if its deletes no longer applies to any data file.
+ *
+ * <p>The following dangling delete files are removed:
+ *
+ * <ul>
+ *   <li>Position delete files with a sequence number less than that of any data file in the same
+ *       partition
+ *   <li>Equality delete files with a sequence number less than or equal to that of any data file in
+ *       the same partition
+ * </ul>
+ */
+public class RemoveDanglingDeletesSparkAction
+    extends BaseSnapshotUpdateSparkAction<RemoveDanglingDeletesSparkAction>
+    implements RemoveDanglingDeleteFiles {
+
+  private final Table table;
+
+  protected RemoveDanglingDeletesSparkAction(SparkSession spark, Table table) {
+    super(spark);
+    this.table = table;
+  }
+
+  @Override
+  protected RemoveDanglingDeletesSparkAction self() {
+    return this;
+  }
+
+  @Override
+  public Result execute() {
+    if (table.specs().size() == 1 && table.spec().isUnpartitioned()) {
+      // ManifestFilterManager already performs this table-wide on each commit
+      return RemoveDanglingDeleteFilesActionResult.empty();
+    }
+
+    String desc = String.format("Remove dangling delete files for %s", table.name());
+    JobGroupInfo info = newJobGroupInfo("REWRITE-MANIFESTS", desc);
+    return withJobGroupInfo(info, this::doExecute);
+  }
+
+  private Result doExecute() {
+    Dataset<Row> entries =
+        loadMetadataTable(table, ENTRIES)
+            .filter("status < 2") // live entries
+            .selectExpr(
+                "data_file.partition as partition",
+                "data_file.spec_id as spec_id",
+                "data_file.file_path as file_path",
+                "data_file.content as content",
+                "data_file.file_size_in_bytes as file_size_in_bytes",
+                "data_file.record_count as record_count",
+                "sequence_number");
+
+    DeleteFiles deleteFiles = table.newDelete();
+    List<DeleteFile> toRemove = withReusableDS(entries, this::danglingDeletes);
+    toRemove.forEach(deleteFiles::deleteFile);
+    deleteFiles.commit();
+
+    return new RemoveDanglingDeleteFilesActionResult(toRemove);
+  }
+
+  /**
+   * Calculate dangling delete files
+   *
+   * <ul>
+   *   <li>Group all files by partition, calculate the minimum data file sequence number in each.
+   *   <li>For each partition, check if any position delete files have a sequence number less than
+   *       the partition's min_data_sequence_number
+   *   <li>For each partition, check if any equality delete files have a sequence number less than
+   *       or equal to the partition's min_data_sequence_number
+   * </ul>
+   *
+   * @param entries dataset of file entries, marked by content (0 for data, 1 for posDeletes, 2 for
+   *     eqDeletes)
+   * @return list of dangling delete files
+   */
+  private List<DeleteFile> danglingDeletes(Dataset<Row> entries) {
+    List<DeleteFile> removedDeleteFiles = Lists.newArrayList();
+
+    // Minimum sequence number of data files in each partition
+    Dataset<Row> minDataSeqNumberPerPartition =
+        entries
+            .filter("content == 0") // data files
+            .groupBy("partition", "spec_id")
+            .agg(min("sequence_number"))
+            .toDF("partition", "spec_id", "min_data_sequence_number");
+
+    // Dangling position delete files
+    Column joinCond =
+        minDataSeqNumberPerPartition
+            .col("partition")
+            .equalTo(entries.col("partition"))
+            .and(minDataSeqNumberPerPartition.col("spec_id").equalTo(entries.col("spec_id")));
+    Dataset<Row> posDeleteDs =
+        entries
+            .filter("content == 1") // position delete files
+            .join(minDataSeqNumberPerPartition, joinCond)
+            .filter("sequence_number < min_data_sequence_number")
+            .select(
+                entries.col("partition"),
+                entries.col("spec_id"),
+                entries.col("file_path"),
+                entries.col("file_size_in_bytes"),
+                entries.col("record_count"));
+    MakeDeleteFile makePosDeleteFn =
+        new MakeDeleteFile(true, Partitioning.partitionType(table), table.specs());
+    Dataset<DeleteFile> posDeletesToRemove =
+        posDeleteDs.map(makePosDeleteFn, Encoders.javaSerialization(DeleteFile.class));
+
+    removedDeleteFiles.addAll(posDeletesToRemove.collectAsList());
+
+    // Dangling equality delete files
+    Dataset<Row> eqDeleteDs =
+        entries
+            .filter("content == 2") // equality delete files
+            .join(minDataSeqNumberPerPartition, joinCond)
+            .filter("sequence_number <= min_data_sequence_number")
+            .select(
+                entries.col("partition"),
+                entries.col("spec_id"),
+                entries.col("file_path"),
+                entries.col("file_size_in_bytes"),
+                entries.col("record_count"));
+    MakeDeleteFile makeEqDeleteFn =
+        new MakeDeleteFile(false, Partitioning.partitionType(table), table.specs());
+    Dataset<DeleteFile> eqDeletesToRemove =
+        eqDeleteDs.map(makeEqDeleteFn, Encoders.javaSerialization(DeleteFile.class));
+
+    removedDeleteFiles.addAll(eqDeletesToRemove.collectAsList());
+    return removedDeleteFiles;
+  }
+
+  private static class MakeDeleteFile implements MapFunction<Row, DeleteFile> {
+
+    private final boolean posDeletes;
+    private final Types.StructType partitionType;
+    private final Map<Integer, PartitionSpec> specsById;
+
+    /**
+     * Map function that transforms entries table rows into {@link DeleteFile}
+     *
+     * @param posDeletes true for position deletes, false for equality deletes
+     * @param partitionType partition type of table
+     * @param specsById table's partition specs
+     */
+    MakeDeleteFile(
+        boolean posDeletes, Types.StructType partitionType, Map<Integer, PartitionSpec> specsById) {
+      this.posDeletes = posDeletes;
+      this.partitionType = partitionType;
+      this.specsById = specsById;
+    }
+
+    @Override
+    public DeleteFile call(Row row) throws Exception {
+      PartitionData partition = new PartitionData(partitionType);
+      GenericRowWithSchema partitionRow = row.getAs(0);
+
+      for (int i = 0; i < partitionRow.length(); i++) {
+        partition.set(i, partitionRow.get(i));
+      }

Review Comment:
   Style nit: newline after the loop 



##########
api/src/main/java/org/apache/iceberg/actions/RemoveDanglingDeleteFiles.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.actions;
+
+import java.util.List;
+import org.apache.iceberg.DeleteFile;
+
+/**
+ * An action that removes dangling delete files from the current snapshot. A delete file is dangling
+ * if its deletes no longer applies to any data file.
+ *
+ * <p>The following dangling delete files are removed:
+ *
+ * <ul>
+ *   <li>Position delete files with a sequence number less than that of any data file in the same
+ *       partition
+ *   <li>Equality delete files with a sequence number less than or equal to that of any data file in
+ *       the same partition
+ * </ul>
+ */
+public interface RemoveDanglingDeleteFiles
+    extends Action<RemoveDanglingDeleteFiles, RemoveDanglingDeleteFiles.Result> {
+
+  /** The action result that contains a summary of the execution. */
+  interface Result {
+    /** Removes representation of removed delete files. */

Review Comment:
   Should this comment just be "List of removed 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.

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