stevenzwu commented on code in PR #16936:
URL: https://github.com/apache/iceberg/pull/16936#discussion_r4198417674


##########
core/src/main/java/org/apache/iceberg/MapBackedContentStats.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Conversions;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+/** Reusable {@link ContentStats} view over a {@link ContentFile}'s stat maps. 
*/
+class MapBackedContentStats implements ContentStats {
+  private final Schema tableSchema;
+  private final Map<Integer, FieldStats<?>> statsById = Maps.newHashMap();
+
+  private Types.StructType type;
+  private Map<Integer, Long> valueCounts;
+  private Map<Integer, Long> nullValueCounts;
+  private Map<Integer, Long> nanValueCounts;
+  private Map<Integer, Integer> avgValueSizes;
+  private Map<Integer, ByteBuffer> lowerBounds;
+  private Map<Integer, ByteBuffer> upperBounds;
+
+  MapBackedContentStats(Schema tableSchema) {
+    Preconditions.checkArgument(tableSchema != null, "Invalid table schema: 
null");
+    this.tableSchema = tableSchema;
+  }
+
+  MapBackedContentStats wrap(ContentFile<?> file) {
+    this.valueCounts = file.valueCounts();
+    this.nullValueCounts = file.nullValueCounts();
+    this.nanValueCounts = file.nanValueCounts();
+    this.avgValueSizes = file.avgValueSizes();
+    this.lowerBounds = file.lowerBounds();
+    this.upperBounds = file.upperBounds();
+    this.type = null;
+    statsById.clear();
+    return this;
+  }
+
+  @Override
+  public Iterable<FieldStats<?>> fieldStats() {
+    return Iterables.filter(Iterables.transform(statsFieldIds(), 
this::statsFor), Objects::nonNull);
+  }
+
+  @Override
+  @SuppressWarnings("unchecked")
+  public <T> FieldStats<T> statsFor(int fieldId) {
+    if (!containsFieldInMaps(fieldId) || tableSchema.findField(fieldId) == 
null) {
+      return null;
+    }
+
+    FieldStats<?> fieldStats = statsById.get(fieldId);
+    if (fieldStats == null) {
+      fieldStats = new MapBackedFieldStats<>(fieldId);
+      statsById.put(fieldId, fieldStats);
+    }
+
+    return (FieldStats<T>) fieldStats;
+  }
+
+  @Override
+  public Types.StructType type() {
+    if (type == null) {
+      this.type = StatsUtil.statsReadSchema(tableSchema, statsFieldIds());
+    }
+
+    return type;
+  }
+
+  @Override
+  public ContentStats copy() {
+    throw new UnsupportedOperationException("copy is not implemented");
+  }
+
+  @Override
+  public ContentStats copy(Set<Integer> fieldIds) {
+    throw new UnsupportedOperationException("copy is not implemented");
+  }
+
+  boolean containsFieldInMaps(int fieldId) {
+    return containsId(valueCounts, fieldId)
+        || containsId(nullValueCounts, fieldId)
+        || containsId(nanValueCounts, fieldId)
+        || containsId(avgValueSizes, fieldId)
+        || containsId(lowerBounds, fieldId)
+        || containsId(upperBounds, fieldId);
+  }
+
+  private Set<Integer> statsFieldIds() {
+    Set<Integer> ids =
+        Sets.newHashSetWithExpectedSize(valueCounts == null ? 0 : 
valueCounts.size());
+    addKeys(ids, valueCounts);
+    addKeys(ids, nullValueCounts);
+    addKeys(ids, nanValueCounts);
+    addKeys(ids, avgValueSizes);
+    addKeys(ids, lowerBounds);
+    addKeys(ids, upperBounds);
+    return ids;
+  }
+
+  private static void addKeys(Set<Integer> ids, Map<Integer, ?> map) {
+    if (map != null) {
+      ids.addAll(map.keySet());
+    }
+  }
+
+  private static boolean containsId(Map<Integer, ?> map, int id) {
+    return map != null && map.containsKey(id);
+  }
+
+  /** Reusable {@link FieldStats} view over one field's entries in a {@link 
ContentFile}'s maps. */
+  private class MapBackedFieldStats<T> implements FieldStats<T> {
+    private final int fieldId;
+    private final Types.StructType struct;
+    private final Type columnType;
+    private final Type boundType;
+
+    MapBackedFieldStats(int fieldId) {
+      Types.NestedField column = tableSchema.findField(fieldId);

Review Comment:
   Agree. I'll rename column and columnType to field and fieldType.



##########
core/src/main/java/org/apache/iceberg/MapBackedContentStats.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Conversions;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+/** Reusable {@link ContentStats} view over a {@link ContentFile}'s stat maps. 
*/
+class MapBackedContentStats implements ContentStats {
+  private final Schema tableSchema;
+  private final Map<Integer, FieldStats<?>> statsById = Maps.newHashMap();
+
+  private Types.StructType type;
+  private Map<Integer, Long> valueCounts;
+  private Map<Integer, Long> nullValueCounts;
+  private Map<Integer, Long> nanValueCounts;
+  private Map<Integer, Integer> avgValueSizes;
+  private Map<Integer, ByteBuffer> lowerBounds;
+  private Map<Integer, ByteBuffer> upperBounds;
+
+  MapBackedContentStats(Schema tableSchema) {
+    Preconditions.checkArgument(tableSchema != null, "Invalid table schema: 
null");
+    this.tableSchema = tableSchema;
+  }
+
+  MapBackedContentStats wrap(ContentFile<?> file) {
+    this.valueCounts = file.valueCounts();
+    this.nullValueCounts = file.nullValueCounts();
+    this.nanValueCounts = file.nanValueCounts();
+    this.avgValueSizes = file.avgValueSizes();
+    this.lowerBounds = file.lowerBounds();
+    this.upperBounds = file.upperBounds();
+    this.type = null;
+    statsById.clear();

Review Comment:
   Agree. MapBackedFieldStats reads the current maps, and statsFor already 
returns null when the id is missing from this file, so clear() only rebuilds 
the field stats structs. I'll keep the cache across wrap()



##########
core/src/main/java/org/apache/iceberg/MapBackedContentStats.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Conversions;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+/** Reusable {@link ContentStats} view over a {@link ContentFile}'s stat maps. 
*/
+class MapBackedContentStats implements ContentStats {
+  private final Schema tableSchema;
+  private final Map<Integer, FieldStats<?>> statsById = Maps.newHashMap();
+
+  private Types.StructType type;
+  private Map<Integer, Long> valueCounts;
+  private Map<Integer, Long> nullValueCounts;
+  private Map<Integer, Long> nanValueCounts;
+  private Map<Integer, Integer> avgValueSizes;
+  private Map<Integer, ByteBuffer> lowerBounds;
+  private Map<Integer, ByteBuffer> upperBounds;
+
+  MapBackedContentStats(Schema tableSchema) {
+    Preconditions.checkArgument(tableSchema != null, "Invalid table schema: 
null");
+    this.tableSchema = tableSchema;
+  }
+
+  MapBackedContentStats wrap(ContentFile<?> file) {
+    this.valueCounts = file.valueCounts();
+    this.nullValueCounts = file.nullValueCounts();
+    this.nanValueCounts = file.nanValueCounts();
+    this.avgValueSizes = file.avgValueSizes();
+    this.lowerBounds = file.lowerBounds();
+    this.upperBounds = file.upperBounds();
+    this.type = null;
+    statsById.clear();
+    return this;
+  }
+
+  @Override
+  public Iterable<FieldStats<?>> fieldStats() {
+    return Iterables.filter(Iterables.transform(statsFieldIds(), 
this::statsFor), Objects::nonNull);
+  }
+
+  @Override
+  @SuppressWarnings("unchecked")
+  public <T> FieldStats<T> statsFor(int fieldId) {
+    if (!containsFieldInMaps(fieldId) || tableSchema.findField(fieldId) == 
null) {
+      return null;
+    }
+
+    FieldStats<?> fieldStats = statsById.get(fieldId);
+    if (fieldStats == null) {
+      fieldStats = new MapBackedFieldStats<>(fieldId);
+      statsById.put(fieldId, fieldStats);
+    }
+
+    return (FieldStats<T>) fieldStats;
+  }
+
+  @Override
+  public Types.StructType type() {
+    if (type == null) {
+      this.type = StatsUtil.statsReadSchema(tableSchema, statsFieldIds());
+    }
+
+    return type;
+  }
+
+  @Override
+  public ContentStats copy() {
+    throw new UnsupportedOperationException("copy is not implemented");
+  }
+
+  @Override
+  public ContentStats copy(Set<Integer> fieldIds) {
+    throw new UnsupportedOperationException("copy is not implemented");
+  }
+
+  boolean containsFieldInMaps(int fieldId) {
+    return containsId(valueCounts, fieldId)
+        || containsId(nullValueCounts, fieldId)
+        || containsId(nanValueCounts, fieldId)
+        || containsId(avgValueSizes, fieldId)
+        || containsId(lowerBounds, fieldId)
+        || containsId(upperBounds, fieldId);
+  }
+
+  private Set<Integer> statsFieldIds() {
+    Set<Integer> ids =
+        Sets.newHashSetWithExpectedSize(valueCounts == null ? 0 : 
valueCounts.size());
+    addKeys(ids, valueCounts);
+    addKeys(ids, nullValueCounts);
+    addKeys(ids, nanValueCounts);
+    addKeys(ids, avgValueSizes);
+    addKeys(ids, lowerBounds);
+    addKeys(ids, upperBounds);
+    return ids;
+  }
+
+  private static void addKeys(Set<Integer> ids, Map<Integer, ?> map) {
+    if (map != null) {
+      ids.addAll(map.keySet());
+    }
+  }
+
+  private static boolean containsId(Map<Integer, ?> map, int id) {
+    return map != null && map.containsKey(id);
+  }
+
+  /** Reusable {@link FieldStats} view over one field's entries in a {@link 
ContentFile}'s maps. */
+  private class MapBackedFieldStats<T> implements FieldStats<T> {
+    private final int fieldId;
+    private final Types.StructType struct;
+    private final Type columnType;
+    private final Type boundType;
+
+    MapBackedFieldStats(int fieldId) {
+      Types.NestedField column = tableSchema.findField(fieldId);
+      Preconditions.checkArgument(column != null, "Missing column for field ID 
%s", fieldId);
+      this.fieldId = fieldId;
+      this.columnType = column.type();
+      int baseId = StatsUtil.toBaseId(fieldId);
+      this.struct = StatsUtil.fieldStatsStruct(columnType, baseId, 
MetricsModes.Full.get());

Review Comment:
   Agree. A null fieldStatsStruct is not a valid FieldStats (nested type, or id 
outside the stats range). I'll add a factory that returns null and cache it 
with containsKey so the null is not rebuilt. 



##########
core/src/main/java/org/apache/iceberg/MapBackedContentStats.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Conversions;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+/** Reusable {@link ContentStats} view over a {@link ContentFile}'s stat maps. 
*/
+class MapBackedContentStats implements ContentStats {
+  private final Schema tableSchema;
+  private final Map<Integer, FieldStats<?>> statsById = Maps.newHashMap();
+
+  private Types.StructType type;
+  private Map<Integer, Long> valueCounts;
+  private Map<Integer, Long> nullValueCounts;
+  private Map<Integer, Long> nanValueCounts;
+  private Map<Integer, Integer> avgValueSizes;
+  private Map<Integer, ByteBuffer> lowerBounds;
+  private Map<Integer, ByteBuffer> upperBounds;
+
+  MapBackedContentStats(Schema tableSchema) {
+    Preconditions.checkArgument(tableSchema != null, "Invalid table schema: 
null");
+    this.tableSchema = tableSchema;
+  }
+
+  MapBackedContentStats wrap(ContentFile<?> file) {
+    this.valueCounts = file.valueCounts();
+    this.nullValueCounts = file.nullValueCounts();
+    this.nanValueCounts = file.nanValueCounts();
+    this.avgValueSizes = file.avgValueSizes();
+    this.lowerBounds = file.lowerBounds();
+    this.upperBounds = file.upperBounds();
+    this.type = null;
+    statsById.clear();
+    return this;
+  }
+
+  @Override
+  public Iterable<FieldStats<?>> fieldStats() {
+    return Iterables.filter(Iterables.transform(statsFieldIds(), 
this::statsFor), Objects::nonNull);
+  }
+
+  @Override
+  @SuppressWarnings("unchecked")
+  public <T> FieldStats<T> statsFor(int fieldId) {
+    if (!containsFieldInMaps(fieldId) || tableSchema.findField(fieldId) == 
null) {
+      return null;
+    }
+
+    FieldStats<?> fieldStats = statsById.get(fieldId);
+    if (fieldStats == null) {
+      fieldStats = new MapBackedFieldStats<>(fieldId);
+      statsById.put(fieldId, fieldStats);
+    }
+
+    return (FieldStats<T>) fieldStats;
+  }
+
+  @Override
+  public Types.StructType type() {
+    if (type == null) {
+      this.type = StatsUtil.statsReadSchema(tableSchema, statsFieldIds());
+    }
+
+    return type;
+  }
+
+  @Override
+  public ContentStats copy() {
+    throw new UnsupportedOperationException("copy is not implemented");
+  }
+
+  @Override
+  public ContentStats copy(Set<Integer> fieldIds) {
+    throw new UnsupportedOperationException("copy is not implemented");
+  }
+
+  boolean containsFieldInMaps(int fieldId) {
+    return containsId(valueCounts, fieldId)
+        || containsId(nullValueCounts, fieldId)
+        || containsId(nanValueCounts, fieldId)
+        || containsId(avgValueSizes, fieldId)
+        || containsId(lowerBounds, fieldId)
+        || containsId(upperBounds, fieldId);
+  }
+
+  private Set<Integer> statsFieldIds() {
+    Set<Integer> ids =
+        Sets.newHashSetWithExpectedSize(valueCounts == null ? 0 : 
valueCounts.size());
+    addKeys(ids, valueCounts);
+    addKeys(ids, nullValueCounts);
+    addKeys(ids, nanValueCounts);
+    addKeys(ids, avgValueSizes);
+    addKeys(ids, lowerBounds);
+    addKeys(ids, upperBounds);
+    return ids;
+  }
+
+  private static void addKeys(Set<Integer> ids, Map<Integer, ?> map) {
+    if (map != null) {
+      ids.addAll(map.keySet());
+    }
+  }
+
+  private static boolean containsId(Map<Integer, ?> map, int id) {
+    return map != null && map.containsKey(id);
+  }
+
+  /** Reusable {@link FieldStats} view over one field's entries in a {@link 
ContentFile}'s maps. */
+  private class MapBackedFieldStats<T> implements FieldStats<T> {
+    private final int fieldId;
+    private final Types.StructType struct;
+    private final Type columnType;
+    private final Type boundType;
+
+    MapBackedFieldStats(int fieldId) {
+      Types.NestedField column = tableSchema.findField(fieldId);
+      Preconditions.checkArgument(column != null, "Missing column for field ID 
%s", fieldId);
+      this.fieldId = fieldId;
+      this.columnType = column.type();
+      int baseId = StatsUtil.toBaseId(fieldId);
+      this.struct = StatsUtil.fieldStatsStruct(columnType, baseId, 
MetricsModes.Full.get());
+      Types.NestedField lowerBound =
+          struct == null ? null : struct.field(baseId + 
StatsUtil.LOWER_BOUND_OFFSET);

Review Comment:
   Agree. I'll use struct.fieldType(StatsUtil.LOWER_BOUND_NAME). 
   
   field(int) and fieldType(name) each build their own map, and neither map 
exists yet on the struct from `StatsUtil.fieldStatsStruct`. 



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