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


##########
spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/source/RewritableDeletes.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.source;
+
+import java.io.Serializable;
+import java.util.Map;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileMetadata;
+import org.apache.iceberg.PartitionSpec;
+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.Maps;
+import org.apache.iceberg.util.DeleteFileSet;
+
+/** Wraps a set of rewritable delete files which are relativized to a given 
table location */
+class RewritableDeletes implements Serializable {
+  private final String tableLocation;
+  private final Map<String, DeleteFileSet> relativizedRewritableDeletes;
+
+  RewritableDeletes(String tableLocation) {
+    this.tableLocation = tableLocation;
+    this.relativizedRewritableDeletes = Maps.newHashMap();
+  }
+
+  /**
+   * @param dataFileLocation absolute data file location
+   * @param deleteFile delete file with absolute data file location
+   * @param specs map from spec ID to partition spec
+   */
+  public void addRewritableDelete(
+      String dataFileLocation, DeleteFile deleteFile, Map<Integer, 
PartitionSpec> specs) {
+    relativizedRewritableDeletes
+        .computeIfAbsent(relativizePath(dataFileLocation), ignored -> 
DeleteFileSet.create())
+        .add(relativizeDeleteFile(deleteFile, specs));
+  }
+
+  /**
+   * @return map of data to rewritable delete files with relativized paths
+   */
+  public Map<String, DeleteFileSet> relativizedDeletes() {
+    return relativizedRewritableDeletes;
+  }
+
+  /**
+   * Return an iterable of deletes for a given absolute data file location
+   *
+   * @param dataFileLocation absolute data file location
+   * @param specs map from spec ID to partition spec
+   */
+  public Iterable<DeleteFile> deletesFor(
+      String dataFileLocation, Map<Integer, PartitionSpec> specs) {
+    DeleteFileSet relativizedDeletes =
+        relativizedRewritableDeletes.get(relativizePath(dataFileLocation));
+    if (relativizedDeletes == null) {
+      return null;
+    }
+
+    return Iterables.transform(
+        relativizedDeletes,
+        deleteFile ->
+            FileMetadata.deleteFileBuilder(specs.get(deleteFile.specId()))
+                .copy(deleteFile)
+                .withPath(absolutePath(deleteFile.location()))
+                .build());
+  }
+
+  private String absolutePath(String path) {
+    return tableLocation + path;
+  }
+
+  private String relativizePath(String path) {
+    int indexOfRelativeLocation = path.indexOf(tableLocation);
+    Preconditions.checkArgument(
+        indexOfRelativeLocation != -1,
+        "Cannot relativize path: %s in relative location %s",
+        path,
+        tableLocation);
+    return path.substring(indexOfRelativeLocation + tableLocation.length());
+  }
+
+  private DeleteFile relativizeDeleteFile(
+      DeleteFile deleteFile, Map<Integer, PartitionSpec> specs) {
+    return FileMetadata.deleteFileBuilder(specs.get(deleteFile.specId()))
+        .copy(deleteFile)

Review Comment:
   Maybe there's a way to extend planning APIs so that we return all paths 
already relativized but that seems like it would be a bigger change, and it's 
not obvious we should do that until there's evidence that this copy is 
expensive.



-- 
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-unsubscr...@iceberg.apache.org

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


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

Reply via email to