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


##########
core/src/main/java/org/apache/iceberg/MapBackedContentStats.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.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 MetricsConfig metricsConfig;
+  private final Types.StructType type;
+  private final Map<Integer, FieldStats<?>> statsById = Maps.newHashMap();
+
+  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, MetricsConfig metricsConfig) {
+    Preconditions.checkArgument(tableSchema != null, "Invalid table schema: 
null");
+    Preconditions.checkArgument(metricsConfig != null, "Invalid metrics 
config: null");
+    this.tableSchema = tableSchema;
+    this.metricsConfig = metricsConfig;
+    this.type = StatsUtil.statsWriteSchema(tableSchema, metricsConfig);

Review Comment:
   Using `statsWriteSchema` may not be consistent with the values that are 
available from the stats maps. This returns a `FieldStats` instance for any ID 
present in one of the maps, but that instance fails if the ID is not also 
present in this type ([precondition in the 
constructor](https://github.com/apache/iceberg/pull/16936/changes#diff-e078c4efcbf284df842cdbedd3d1e7d60ba350d6a5f55c27691e635deeffee5aR119-R122)).
   
   I think we have a couple of options to fix this. Option 1 is to make the 
schema match the set of field IDs by passing the field ID set into 
`statsReadSchema`. Option 2 is to check whether the field is in the current 
stats schema and only return `FieldStats` if it is present.
   
   I prefer option 1, since the stats read schema matches the use case of this 
class. It is the widest set of field stats columns in case the stats are 
present in a file. That's the closest comparison to what is happening in this 
class. And a secondary benefit is that we no longer need to pass 
`MetricsConfig`.
   
   Another way of thinking about this is that we don't have to impose the 
current stats write schema when accessing stats in memory (we never did for the 
maps) and we should impose that write schema at write time in the 
`ContentStats` write adapter. Since this isn't the write adapter, we should use 
the widest schema we can.



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