sunchao commented on code in PR #2276:
URL: https://github.com/apache/iceberg/pull/2276#discussion_r1018632924


##########
core/src/main/java/org/apache/iceberg/util/TableScanUtil.java:
##########
@@ -121,10 +128,66 @@ public static <T extends ScanTask> 
CloseableIterable<ScanTaskGroup<T>> planTaskG
     Function<T, Long> weightFunc =
         task -> Math.max(task.sizeBytes(), task.filesCount() * openFileCost);
 
+    return toTaskGroupIterable(splitTasks, splitSize, lookback, weightFunc);
+  }
+
+  @SuppressWarnings("unchecked")
+  public static <T extends PartitionScanTask> List<ScanTaskGroup<T>> 
planTaskGroups(
+      List<T> tasks,
+      long splitSize,
+      int lookback,
+      long openFileCost,
+      Types.StructType projectedPartitionType) {
+
+    Preconditions.checkArgument(splitSize > 0, "Invalid split size (negative 
or 0): %s", splitSize);
+    Preconditions.checkArgument(
+        lookback > 0, "Invalid split planning lookback (negative or 0): %s", 
lookback);
+    Preconditions.checkArgument(
+        openFileCost >= 0, "Invalid file open cost (negative): %s", 
openFileCost);
+
+    List<T> splitTasks = Lists.newArrayList();
+    for (T task : tasks) {
+      if (task instanceof SplittableScanTask<?>) {
+        ((SplittableScanTask<? extends T>) 
task).split(splitSize).forEach(splitTasks::add);
+      } else {
+        splitTasks.add(task);
+      }
+    }
+
+    Function<T, Long> weightFunc =
+        task -> Math.max(task.sizeBytes(), task.filesCount() * openFileCost);
+
+    Map<Integer, StructProjection> projectionsBySpec = Maps.newHashMap();
+
+    // Group tasks by their partition values
+    StructLikeMap<List<T>> groupedTasks = 
StructLikeMap.create(projectedPartitionType);
+
+    for (T task : splitTasks) {
+      PartitionSpec spec = task.spec();
+      projectionsBySpec.computeIfAbsent(
+          spec.specId(),
+          specId -> StructProjection.create(spec.partitionType(), 
projectedPartitionType));
+      StructProjection projectedStruct = 
projectionsBySpec.get(spec.specId()).copy();
+      groupedTasks
+          .computeIfAbsent(projectedStruct.copy().wrap(task.partition()), k -> 
Lists.newArrayList())
+          .add(task);
+    }
+
+    // Now apply task combining within each partition
+    return groupedTasks.values().stream()
+        .flatMap(
+            ts ->
+                StreamSupport.stream(
+                    toTaskGroupIterable(ts, splitSize, lookback, 
weightFunc).spliterator(), false))
+        .collect(Collectors.toList());
+  }
+
+  private static <T extends ScanTask> CloseableIterable<ScanTaskGroup<T>> 
toTaskGroupIterable(

Review Comment:
   OK, let me change it to `toTaskGroupStream`. 



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