aokolnychyi commented on code in PR #11146:
URL: https://github.com/apache/iceberg/pull/11146#discussion_r1776149156


##########
core/src/main/java/org/apache/iceberg/PartitionStatsUtil.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.Map;
+import java.util.Queue;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Queues;
+import org.apache.iceberg.types.Comparators;
+import org.apache.iceberg.types.Types.StructType;
+import org.apache.iceberg.util.Pair;
+import org.apache.iceberg.util.PartitionMap;
+import org.apache.iceberg.util.PartitionUtil;
+import org.apache.iceberg.util.Tasks;
+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(Partitioning.isPartitioned(table), "table must 
be partitioned");
+    Preconditions.checkArgument(snapshot != null, "snapshot cannot be null");
+
+    StructType partitionType = Partitioning.partitionType(table);
+    List<ManifestFile> manifests = snapshot.allManifests(table.io());
+    Queue<PartitionMap<PartitionStats>> statsByManifest = 
Queues.newConcurrentLinkedQueue();
+    Tasks.foreach(manifests)
+        .stopOnFailure()
+        .throwFailureWhenFinished()
+        .executeWith(ThreadPools.getWorkerPool())
+        .run(manifest -> statsByManifest.add(collectStats(table, manifest, 
partitionType)));
+
+    return mergeStats(statsByManifest, table.specs());
+  }
+
+  /**
+   * 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());

Review Comment:
   Do we have to access iterator here? Why not simply pass `stats`?



##########
core/src/main/java/org/apache/iceberg/PartitionStatsUtil.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.Map;
+import java.util.Queue;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Queues;
+import org.apache.iceberg.types.Comparators;
+import org.apache.iceberg.types.Types.StructType;
+import org.apache.iceberg.util.Pair;
+import org.apache.iceberg.util.PartitionMap;
+import org.apache.iceberg.util.PartitionUtil;
+import org.apache.iceberg.util.Tasks;
+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(Partitioning.isPartitioned(table), "table must 
be partitioned");
+    Preconditions.checkArgument(snapshot != null, "snapshot cannot be null");
+
+    StructType partitionType = Partitioning.partitionType(table);
+    List<ManifestFile> manifests = snapshot.allManifests(table.io());
+    Queue<PartitionMap<PartitionStats>> statsByManifest = 
Queues.newConcurrentLinkedQueue();
+    Tasks.foreach(manifests)
+        .stopOnFailure()
+        .throwFailureWhenFinished()
+        .executeWith(ThreadPools.getWorkerPool())
+        .run(manifest -> statsByManifest.add(collectStats(table, manifest, 
partitionType)));
+
+    return mergeStats(statsByManifest, table.specs());
+  }
+
+  /**
+   * 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(partitionStatsCmp(partitionType));
+    return entries;
+  }
+
+  private static Comparator<PartitionStats> partitionStatsCmp(StructType 
partitionType) {
+    return Comparator.comparing(PartitionStats::partition, 
Comparators.forType(partitionType));
+  }
+
+  private static PartitionMap<PartitionStats> collectStats(
+      Table table, ManifestFile manifest, StructType partitionType) {
+    try (ManifestReader<?> reader = openManifest(table, manifest)) {
+      PartitionMap<PartitionStats> statsMap = 
PartitionMap.create(table.specs());
+      int specId = reader.spec().specId();

Review Comment:
   It was my bad to suggest using `reader.spec()`. It is probably not a concern 
in our case but [it may be expensive in some 
cases](https://github.com/apache/iceberg/blob/b1d38b3cac326da296b770c8ab9219bc4f791666/core/src/main/java/org/apache/iceberg/ManifestReader.java#L120).
 To make sure we are safe in the future, let's use `table.specs()`.
   
   ```
   int specId = manifest.partitionSpecId();
   PartitionSpec spec = table.specs().get(specId);
   ```



##########
core/src/main/java/org/apache/iceberg/PartitionStatsUtil.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.Map;
+import java.util.Queue;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Queues;
+import org.apache.iceberg.types.Comparators;
+import org.apache.iceberg.types.Types.StructType;
+import org.apache.iceberg.util.Pair;
+import org.apache.iceberg.util.PartitionMap;
+import org.apache.iceberg.util.PartitionUtil;
+import org.apache.iceberg.util.Tasks;
+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(Partitioning.isPartitioned(table), "table must 
be partitioned");
+    Preconditions.checkArgument(snapshot != null, "snapshot cannot be null");
+
+    StructType partitionType = Partitioning.partitionType(table);
+    List<ManifestFile> manifests = snapshot.allManifests(table.io());
+    Queue<PartitionMap<PartitionStats>> statsByManifest = 
Queues.newConcurrentLinkedQueue();
+    Tasks.foreach(manifests)
+        .stopOnFailure()
+        .throwFailureWhenFinished()
+        .executeWith(ThreadPools.getWorkerPool())
+        .run(manifest -> statsByManifest.add(collectStats(table, manifest, 
partitionType)));
+
+    return mergeStats(statsByManifest, table.specs());
+  }
+
+  /**
+   * 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(partitionStatsCmp(partitionType));
+    return entries;
+  }
+
+  private static Comparator<PartitionStats> partitionStatsCmp(StructType 
partitionType) {
+    return Comparator.comparing(PartitionStats::partition, 
Comparators.forType(partitionType));
+  }
+
+  private static PartitionMap<PartitionStats> collectStats(
+      Table table, ManifestFile manifest, StructType partitionType) {
+    try (ManifestReader<?> reader = openManifest(table, manifest)) {
+      PartitionMap<PartitionStats> statsMap = 
PartitionMap.create(table.specs());
+      int specId = reader.spec().specId();
+      PartitionData keyTemplate = new PartitionData(partitionType);
+
+      for (ManifestEntry<?> entry : reader.entries()) {
+        ContentFile<?> file = entry.file();
+        StructLike coercedPartition =
+            PartitionUtil.coercePartition(partitionType, reader.spec(), 
file.partition());
+        StructLike key = keyTemplate.copyFor(coercedPartition);
+        Snapshot snapshot = table.snapshot(entry.snapshotId());
+        PartitionStats stats =
+            statsMap.computeIfAbsent(
+                Pair.of(specId, key), ignored -> new PartitionStats(key, 
specId));

Review Comment:
   Minor: `PartitionMap` has 
[overloaded](https://github.com/apache/iceberg/blob/b1d38b3cac326da296b770c8ab9219bc4f791666/core/src/main/java/org/apache/iceberg/util/PartitionMap.java#L173)
 `computeIfAbsent` that accepts spec ID and partition directly, avoiding the 
need to construct `Pair` here.



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