ajantha-bhat commented on code in PR #11146:
URL: https://github.com/apache/iceberg/pull/11146#discussion_r1773135088


##########
core/src/main/java/org/apache/iceberg/PartitionStatsUtil.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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 java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Comparators;
+import org.apache.iceberg.types.Types.StructType;
+import org.apache.iceberg.util.PartitionUtil;
+import org.apache.iceberg.util.StructLikeMap;
+import org.apache.iceberg.util.ThreadPools;
+
+public class PartitionStatsUtil {
+
+  private PartitionStatsUtil() {}
+
+  /**
+   * Computes the partition stats for the given snapshot of the table.
+   *
+   * @param table the table for which partition stats to be computed.
+   * @param snapshot the snapshot for which partition stats is computed.
+   * @return the collection of {@link PartitionStats}
+   */
+  public static Collection<PartitionStats> computeStats(Table table, Snapshot 
snapshot) {
+    Preconditions.checkArgument(table != null, "table cannot be null");
+    Preconditions.checkArgument(snapshot != null, "snapshot cannot be null");
+
+    StructType partitionType = Partitioning.partitionType(table);
+    if (partitionType.fields().isEmpty()) {
+      throw new UnsupportedOperationException(
+          "Computing partition stats for an unpartitioned table");
+    }
+
+    List<ManifestFile> manifests = snapshot.allManifests(table.io());
+
+    ExecutorService executorService = ThreadPools.getWorkerPool();
+    List<Future<StructLikeMap<PartitionStats>>> futures = Lists.newArrayList();
+    manifests.forEach(
+        manifest -> {
+          Future<StructLikeMap<PartitionStats>> future =
+              executorService.submit(() -> collectStats(table, manifest, 
partitionType));
+          futures.add(future);
+        });
+
+    StructLikeMap<PartitionStats> statsMap = 
StructLikeMap.create(partitionType);
+    for (Future<StructLikeMap<PartitionStats>> future : futures) {
+      try {
+        future
+            .get()
+            .forEach(
+                (key, value) ->
+                    statsMap.merge(
+                        key,
+                        value,
+                        (existingEntry, newEntry) -> {
+                          existingEntry.appendStats(newEntry);
+                          return existingEntry;
+                        }));
+      } catch (InterruptedException | ExecutionException e) {
+        throw new RuntimeException(e);
+      }
+    }
+
+    return statsMap.values();
+  }
+
+  /**
+   * Sorts the {@link PartitionStats} based on the partition data.
+   *
+   * @param stats collection of {@link PartitionStats} which needs to be 
sorted.
+   * @param partitionType unified partition schema.
+   * @return the list of {@link PartitionStats}
+   */
+  public static List<PartitionStats> sortStats(
+      Collection<PartitionStats> stats, StructType partitionType) {
+    List<PartitionStats> entries = Lists.newArrayList(stats.iterator());
+    entries.sort(
+        Comparator.comparing(PartitionStats::partition, 
Comparators.forType(partitionType)));
+    return entries;
+  }
+
+  private static StructLikeMap<PartitionStats> collectStats(
+      Table table, ManifestFile manifest, StructType partitionType) {
+    try (ManifestReader<?> reader = openManifest(table, manifest)) {
+      StructLikeMap<PartitionStats> statsMap = 
StructLikeMap.create(partitionType);
+
+      for (ManifestEntry<?> entry : reader.entries()) {
+        ContentFile<?> file = entry.file();
+        PartitionSpec spec = table.specs().get(file.specId());
+        PartitionData key =

Review Comment:
   > I think I understand why it starts to fail if we use the existing 
coercePartition method. It fails because the entry object is reused, meaning 
partition we project is mutable.
   
   Thanks for this. 
   
   > Take a look 
[here](https://github.com/apache/iceberg/blob/72fd9ab9f44cfb3e4b08f849cf7b5132f84b9936/core/src/main/java/org/apache/iceberg/util/TableScanUtil.java#L156)
 at how we solve in TableScanUtil.
   
   They create two objects of `PartitionData` (one empty and one with copyFor) 
per `StructProjection`, Instead I used internal code to create one 
`PartitionData` object per `StructProjection`. So, I feel no need to modify 
this code. WDYT?
   
   



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