advancedxy commented on code in PR #9563:
URL: https://github.com/apache/iceberg/pull/9563#discussion_r1475446827


##########
spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/source/SparkPlanningUtil.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.util.List;
+import java.util.Objects;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.PartitionScanTask;
+import org.apache.iceberg.ScanTask;
+import org.apache.iceberg.ScanTaskGroup;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.util.Tasks;
+import org.apache.iceberg.util.ThreadPools;
+
+class SparkPlanningUtil {
+
+  public static final String[] NO_LOCATION_PREFERENCE = new String[0];
+
+  private SparkPlanningUtil() {}
+
+  public static String[][] fetchBlockLocations(
+      FileIO io, List<? extends ScanTaskGroup<?>> taskGroups) {
+    String[][] locations = new String[taskGroups.size()][];
+
+    Tasks.range(taskGroups.size())
+        .stopOnFailure()
+        .executeWith(ThreadPools.getWorkerPool())
+        .run(index -> locations[index] = Util.blockLocations(io, 
taskGroups.get(index)));
+
+    return locations;
+  }
+
+  public static String[][] assignExecutors(
+      List<? extends ScanTaskGroup<?>> taskGroups, List<String> 
executorLocations) {
+    String[][] locations = new String[taskGroups.size()][];
+
+    for (int index = 0; index < taskGroups.size(); index++) {
+      locations[index] = assign(taskGroups.get(index), executorLocations);
+    }
+
+    return locations;
+  }
+
+  private static String[] assign(ScanTaskGroup<?> taskGroup, List<String> 
executorLocations) {
+    List<String> locations = Lists.newArrayList();
+
+    for (ScanTask task : taskGroup.tasks()) {
+      if (task.isFileScanTask()) {
+        FileScanTask fileTask = task.asFileScanTask();
+        if (isPartitioned(fileTask) && !fileTask.deletes().isEmpty()) {
+          int index = Math.floorMod(hash(fileTask.partition()), 
executorLocations.size());
+          String executorLocation = executorLocations.get(index);
+          locations.add(executorLocation);
+        }
+      }
+    }
+
+    return locations.toArray(NO_LOCATION_PREFERENCE);
+  }
+
+  private static boolean isPartitioned(PartitionScanTask task) {
+    return task.partition() != null && task.partition().size() > 0;

Review Comment:
   > That's what I started with but task.spec() may be quite expensive.
   
   I think we already called `specs()` in `SparkPartitioningAwareScan` to 
preserve data grouping. If it's quite expensive to call `task.spec()`, it would 
be better that we can reduce this overhead. 
   
   First thing came out of my mind: how about add a `int specId()` method in 
the  `PartitionScanTask` interface? The specId should be easier to store and 
retrieve.  In the driver side, we can leverage `table.specs()` to retrieve the 
actual spec.
   
   > I don't think void transforms will be an issue as we check for presence of 
deletes and V1 tables can't have them.
   
   Maybe some extreme case, such as: v1 table with `identity(c1)` -- dropped 
partition transform -->  v1 table with void transform -- upgrade to v2 --> v2 
table with void transform --> added deletes.



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