public static PartitionStatisticsFile computeAndWriteStatsFile(Table table, String branch) {
    Snapshot currentSnapshot = SnapshotUtil.latestSnapshot(table, branch);
    if (currentSnapshot == null) {
      Preconditions.checkArgument(
          branch == null, "Couldn't find the snapshot for the branch %s", branch);
      return null;
    }

    StructType partitionType = Partitioning.partitionType(table);
    Collection<PartitionStats> stats = PartitionStatsUtil.computeStats(table, currentSnapshot);
    List<PartitionStats> sortedStats = PartitionStatsUtil.sortStats(stats, partitionType);
    return writePartitionStatsFile(
        table, currentSnapshot.snapshotId(), schema(partitionType), sortedStats.iterator());
  }
Computes and writes the {@link PartitionStatisticsFile} for a given table and branch.

@param table The {@link Table} for which the partition statistics is computed.
@param branch A branch information to select the required snapshot.
@return {@link PartitionStatisticsFile} for the given branch.
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/data/PartitionStatsHandler.java