amogh-jahagirdar commented on code in PR #17159: URL: https://github.com/apache/iceberg/pull/17159#discussion_r3580235586
########## core/src/main/java/org/apache/iceberg/FieldStatsStruct.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.io.Serializable; +import java.nio.ByteBuffer; +import java.util.List; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.ByteBuffers; + +class FieldStatsStruct<T> implements FieldStats<T>, StructLike, Serializable { + private final Types.StructType struct; + private final Type boundType; + private final int[] posToOffset; + private final int fieldId; + + private Object lowerBound = null; + private Object upperBound = null; + private boolean tightBounds = false; + private Long valueCount = null; + private Long nullValueCount = null; + private Long nanValueCount = null; + private Integer avgValueSize = null; + + FieldStatsStruct(Types.StructType struct) { + this.struct = struct; + this.posToOffset = posToOffset(struct); + this.fieldId = StatsUtil.toFieldId(struct.fields().get(0).fieldId()); + this.boundType = struct.fieldType("lower_bound"); + } + + FieldStatsStruct( + Types.StructType struct, + T lowerBound, + T upperBound, + boolean tightBounds, + long valueCount, + long nullValueCount, + long nanValueCount, + Integer avgValueSize) { + this(struct); + setLowerBound(lowerBound); + setUpperBound(upperBound); + this.tightBounds = tightBounds; + this.valueCount = valueCount; + this.nullValueCount = nullValueCount; + this.nanValueCount = nanValueCount; + this.avgValueSize = avgValueSize; + } + + private FieldStatsStruct(FieldStatsStruct<T> toCopy) { + this(toCopy.struct); + // bounds are stored using the internal representation, which is a byte array for binary types + this.lowerBound = + toCopy.lowerBound instanceof byte[] + ? ((byte[]) toCopy.lowerBound).clone() + : toCopy.lowerBound; + this.upperBound = + toCopy.upperBound instanceof byte[] + ? ((byte[]) toCopy.upperBound).clone() + : toCopy.upperBound; + this.tightBounds = toCopy.tightBounds; + this.valueCount = toCopy.valueCount; + this.nullValueCount = toCopy.nullValueCount; + this.nanValueCount = toCopy.nanValueCount; + this.avgValueSize = toCopy.avgValueSize; + } + + void fromFieldMetrics(FieldMetrics<T> fieldMetrics) { + Preconditions.checkArgument( + fieldMetrics.id() == fieldId, + "Cannot store stats for field ID: %s (expected %s)", + fieldMetrics.id(), + fieldId); + + setLowerBound(fieldMetrics.lowerBound()); + setUpperBound(fieldMetrics.upperBound()); + this.tightBounds = false; + this.valueCount = fieldMetrics.valueCount(); + this.nullValueCount = fieldMetrics.nullValueCount() < 0 ? null : fieldMetrics.nullValueCount(); + this.nanValueCount = fieldMetrics.nanValueCount() < 0 ? null : fieldMetrics.nanValueCount(); + this.avgValueSize = null; + } + + private boolean isBinary() { + return boundType != null + && (boundType.typeId() == Type.TypeID.FIXED || boundType.typeId() == Type.TypeID.BINARY); + } + + private void setLowerBound(Object lowerBound) { + this.lowerBound = isBinary() ? ByteBuffers.toByteArray((ByteBuffer) lowerBound) : lowerBound; + } + + private void setUpperBound(Object upperBound) { + this.upperBound = isBinary() ? ByteBuffers.toByteArray((ByteBuffer) upperBound) : upperBound; + } + + @Override + public int fieldId() { + return fieldId; + } + + @Override + public Types.StructType type() { + return struct; + } + + @Override + @SuppressWarnings("unchecked") + public T lowerBound() { + return (T) + (lowerBound != null && isBinary() ? ByteBuffer.wrap((byte[]) lowerBound) : lowerBound); + } + + @Override + @SuppressWarnings("unchecked") + public T upperBound() { + return (T) + (upperBound != null && isBinary() ? ByteBuffer.wrap((byte[]) upperBound) : upperBound); + } + + @Override + public boolean tightBounds() { + return tightBounds; + } + + @Override + public long valueCount() { + return valueCount; Review Comment: Based on the comment below the change to make this API a `long` was a conscious one but given the additional change to MetricsUtil that's made wouldn't this NPE in cases where value / null / nan counts are null? ########## core/src/main/java/org/apache/iceberg/ContentStatsStruct.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.io.Serializable; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.types.TypeUtil; +import org.apache.iceberg.types.Types; + +class ContentStatsStruct implements ContentStats, StructLike, Serializable { + private final Map<Integer, FieldStats<?>> idToFieldStats = Maps.newHashMap(); + private final Types.StructType struct; + private final int[] posToId; + + ContentStatsStruct(Types.StructType struct) { + this.struct = struct; + this.posToId = posToId(struct); + } + + private ContentStatsStruct(ContentStatsStruct toCopy, Set<Integer> fieldIds) { + this(fieldIds != null ? TypeUtil.select(toCopy.struct, fieldIds) : toCopy.struct); + if (fieldIds != null) { + for (int fieldId : fieldIds) { + idToFieldStats.put(fieldId, toCopy.idToFieldStats.get(fieldId).copy()); + } + } else { + for (Map.Entry<Integer, FieldStats<?>> entry : toCopy.idToFieldStats.entrySet()) { + idToFieldStats.put(entry.getKey(), entry.getValue().copy()); + } + } + } + + @Override + public Iterable<FieldStats<?>> fieldStats() { + return idToFieldStats.values(); + } + + @Override + @SuppressWarnings("unchecked") + public <T> FieldStats<T> statsFor(int id) { + return (FieldStats<T>) idToFieldStats.get(id); + } + + public <T> void setStats(int id, FieldStats<T> fieldStats) { + Preconditions.checkArgument( + struct.field(StatsUtil.toBaseId(id)) != null, + "Cannot set stats for unknown field ID: %s", + id); + Preconditions.checkArgument( + id == fieldStats.fieldId(), + "Mismatched field stats for ID %s: actual ID %s", + id, + fieldStats.fieldId()); + + idToFieldStats.put(id, fieldStats); + } + + @Override + public Types.StructType type() { + return struct; + } + + @Override + public int size() { + return struct.fields().size(); + } + + @Override + public <T> T get(int pos, Class<T> javaClass) { + return javaClass.cast(idToFieldStats.get(posToId[pos])); Review Comment: `BaseContentStats` get used to check `pos` against the struct size, and it looks like the old code returned null in case the pos was "out of bounds" of the struct. It looks like that was done for Avro because on the write path Avro calls get() for every field in the writer schema. I don't think we're fully plumbed through which is why we don't see any issues but are we handling that case differently in this PR, or we're just deferring that? If I recall correctly we wanted to use this in memory representation regardless of format version (because we theoretically could adapt the existing stats structure to this) ########## core/src/main/java/org/apache/iceberg/ContentStats.java: ########## @@ -35,6 +35,12 @@ interface ContentStats extends StructLike { */ <T> FieldStats<T> statsFor(int fieldId); - /** The stats struct holding nested structs with their respective field stats */ - Types.StructType statsStruct(); + /** Container struct type containing tracked field-level stats structs. */ + Types.StructType type(); + + /** Returns a copy, deep-copying all field stats. */ Review Comment: nit: Returns a deep copy of all field stats? ########## core/src/main/java/org/apache/iceberg/FieldStats.java: ########## @@ -46,17 +44,20 @@ interface FieldStats<T> extends StructLike { boolean tightBounds(); /** The total value count, including null and NaN */ - Long valueCount(); + long valueCount(); /** The total null value count */ - Long nullValueCount(); + long nullValueCount(); /** The total NaN value count */ - Long nanValueCount(); + long nanValueCount(); Review Comment: Curious what's driving this, from a spec perspective is there an open question about if these stats should be required? My understanding of the parquet spec is that value counts are required but null/NaN counts are optional -- 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]
