Anurag-S1ngh commented on code in PR #19274:
URL: https://github.com/apache/iceberg/pull/19274#discussion_r4811350216


##########
core/src/main/java/org/apache/iceberg/BaseRewriteManifests.java:
##########
@@ -342,6 +357,38 @@ private WriterWrapper getWriter(Object key, int 
partitionSpecId) {
         Pair.of(key, partitionSpecId), k -> new 
WriterWrapper(specsById.get(partitionSpecId)));
   }
 
+  private Function<DataFile, Object> effectiveClusterByFunc() {
+    return clusterByFunc != null ? clusterByFunc : file -> SINGLE_CLUSTER_KEY;
+  }
+
+  private void initColumnStatsPruning() {
+    if (!pruneColumnStats) {
+      return;
+    }
+
+    TableMetadata current = ops().current();
+    this.tableSchema = current.schema();
+    this.metricsConfig =

Review Comment:
   Switched to `MetricsConfig.forTable(current)`, which includes the partition 
spec and keeps order-preserving partition source columns at full stats. Added 
`testPruneColumnStatsKeepsOrderPreservingPartitionColumn`.



##########
core/src/main/java/org/apache/iceberg/MetricsUtil.java:
##########
@@ -78,6 +78,67 @@ public static Metrics copyWithoutFieldCountsAndBounds(
         copyWithoutKeys(metrics.originalTypes(), excludedFieldIds));
   }
 
+  /**
+   * Copies a metrics object, dropping stats that exceed the metrics modes 
configured for the table.
+   *
+   * <p>Stats are only removed, never re-truncated: columns configured as 
{@link MetricsModes.None}
+   * lose all stats, columns configured as {@link MetricsModes.Counts} lose 
their bounds, and
+   * columns configured as {@link MetricsModes.Truncate} or {@link 
MetricsModes.Full} keep their
+   * stats unchanged. Stats for fields that are not present in the schema are 
left untouched.
+   *
+   * @param schema the table schema the metrics are bound against
+   * @param metricsConfig the table's metrics configuration
+   * @param metrics the metrics to prune
+   * @return a new metrics object retaining only the stats permitted by the 
metrics configuration
+   */
+  public static Metrics pruneColumnStats(
+      Schema schema, MetricsConfig metricsConfig, Metrics metrics) {
+    Set<Integer> allStatsExcludedIds = Sets.newHashSet();

Review Comment:
   Precomputed the column modes once in `initColumnStatsPruning` and pass them 
to `pruneColumnStats` for each file instead of rebuilding the exclusion sets 
per file.



##########
api/src/main/java/org/apache/iceberg/RewriteManifests.java:
##########
@@ -86,4 +86,26 @@ public interface RewriteManifests extends 
SnapshotUpdate<RewriteManifests> {
    * @return this for method chaining
    */
   RewriteManifests addManifest(ManifestFile manifest);
+
+  /**
+   * Prunes column-level stats from rewritten data files that exceed the 
table's current {@code
+   * write.metadata.metrics.*} configuration.
+   *
+   * <p>Rewriting manifests normally copies each data file's stats verbatim, 
so tightening the
+   * metrics configuration does not shrink existing manifests. When this is 
enabled, stats are
+   * dropped to match the configured modes as files are rewritten: columns 
configured as {@code
+   * none} lose all stats and columns configured as {@code counts} lose their 
bounds. Existing
+   * bounds are never re-truncated, so columns configured as {@code 
truncate(n)} or {@code full}
+   * keep their stats unchanged.
+   *
+   * <p>Pruning only applies to files in manifests that are actually 
rewritten. If no clustering
+   * function is set via {@link #clusterBy(Function)}, enabling this triggers 
a full rewrite that
+   * groups all data files together.
+   *
+   * @return this for method chaining
+   */
+  default RewriteManifests pruneColumnStats() {

Review Comment:
   Made `MetricsUtil.pruneColumnStats` package-private. It is only used from 
core today, so the API can stay internal until another module needs it.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to