timothyw553 commented on code in PR #19274:
URL: https://github.com/apache/iceberg/pull/19274#discussion_r4771394287


##########
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:
   this overload ignores the partition spec, so partition source columns like 
`day(ts)` can drop to `counts` or `none` and break metadata-only deletes. 
should we use `MetricsConfig.forTable(current)` instead, and add regression 
tests for partition-column stats and metadata-only deletes?
   
   ```diff
   -    this.metricsConfig =
   -        MetricsConfig.from(current.properties(), current.schema(), 
current.sortOrder());
   +    this.metricsConfig = MetricsConfig.forTable(current);
   ```



##########
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:
   this rebuilds the excluded field id sets and resolves column modes for every 
single file while holding the writer lock. could we precompute those exclusion 
sets once per rewrite instead of 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:
   is this meant to be core-only for now, since spark's rewrite action uses 
`deleteManifest`/`addManifest` and won't expose this? if so, maybe we can also 
keep `MetricsUtil.pruneColumnStats` package-private 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