rdblue commented on code in PR #16936: URL: https://github.com/apache/iceberg/pull/16936#discussion_r4020584676
########## core/src/main/java/org/apache/iceberg/MapBackedContentStats.java: ########## @@ -0,0 +1,273 @@ +/* + * 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.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.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 legacy {@link ContentFile}'s stat maps. */ +class MapBackedContentStats implements ContentStats, StructLike { + private final Types.StructType struct; + private final int[] posToId; + private final Map<Integer, FieldStats<?>> statsById; + + private Map<Integer, Long> valueCounts; + private Map<Integer, Long> nullValueCounts; + private Map<Integer, Long> nanValueCounts; + private Map<Integer, ByteBuffer> lowerBounds; + private Map<Integer, ByteBuffer> upperBounds; + + MapBackedContentStats(Types.StructType contentStatsType) { + Preconditions.checkArgument(contentStatsType != null, "Invalid content stats type: null"); + this.struct = contentStatsType; + List<Types.NestedField> fields = struct.fields(); + this.posToId = new int[fields.size()]; + this.statsById = Maps.newHashMapWithExpectedSize(fields.size()); + for (int i = 0; i < fields.size(); i += 1) { + Types.NestedField field = fields.get(i); + int fieldId = StatsUtil.toFieldId(field.fieldId()); + Types.StructType fieldStatsType = field.type().asStructType(); + posToId[i] = fieldId; + statsById.put( + fieldId, new MapBackedFieldStats<>(fieldStatsType, fieldId, boundType(fieldStatsType))); + } + } + + MapBackedContentStats wrap(ContentFile<?> file) { + this.valueCounts = file.valueCounts(); + this.nullValueCounts = file.nullValueCounts(); + this.nanValueCounts = file.nanValueCounts(); + this.lowerBounds = file.lowerBounds(); + this.upperBounds = file.upperBounds(); + return this; + } + + private boolean hasStats(int id) { + return containsId(valueCounts, id) + || containsId(nullValueCounts, id) + || containsId(nanValueCounts, id) + || containsId(lowerBounds, id) + || containsId(upperBounds, id); + } + + private static boolean containsId(Map<Integer, ?> map, int id) { + return map != null && map.containsKey(id); + } + + @Override + public Iterable<FieldStats<?>> fieldStats() { + return Iterables.transform( + Iterables.filter(statsById.entrySet(), entry -> hasStats(entry.getKey())), + Map.Entry::getValue); + } + + @Override + @SuppressWarnings("unchecked") + public <T> FieldStats<T> statsFor(int fieldId) { + return hasStats(fieldId) ? (FieldStats<T>) statsById.get(fieldId) : null; + } + + @Override + public Types.StructType type() { + return struct; + } + + private static Type boundType(Types.StructType fieldStats) { + for (Types.NestedField field : fieldStats.fields()) { + if (StatsUtil.statOffset(field.fieldId()) == StatsUtil.LOWER_BOUND_OFFSET) { + return field.type(); + } + } + + return null; + } + + @Override + public int size() { + return struct.fields().size(); + } + + @Override + public <T> T get(int pos, Class<T> javaClass) { + int id = posToId[pos]; + return javaClass.cast(hasStats(id) ? statsById.get(id) : null); + } + + @Override + public <T> void set(int pos, T value) { + throw new UnsupportedOperationException( + "Reusable content stats wrapper does not support set()"); + } + + @Override + public ContentStats copy() { + throw new UnsupportedOperationException( + "Reusable content stats wrapper does not support copy(); materialize via a writer instead"); + } + + @Override + public ContentStats copy(Set<Integer> fieldIds) { + throw new UnsupportedOperationException( + "Reusable content stats wrapper does not support copy(); materialize via a writer instead"); + } + + /** Reusable {@link FieldStats} view over one field's entries in a {@link ContentFile}'s maps. */ + private class MapBackedFieldStats<T> implements FieldStats<T>, StructLike { + private final Types.StructType struct; + private final int fieldId; + private final Type boundType; + private final int[] posToOffset; + + MapBackedFieldStats(Types.StructType struct, int fieldId, Type boundType) { + this.struct = struct; + this.fieldId = fieldId; + this.boundType = boundType; + this.posToOffset = posToOffset(struct); + } + + @Override + public int fieldId() { + return fieldId; + } + + @Override + public Types.StructType type() { + return struct; + } + + @Override + @SuppressWarnings("unchecked") + public T lowerBound() { + if (boundType == null) { + return null; + } + + ByteBuffer buf = lowerBounds == null ? null : lowerBounds.get(fieldId); + return buf == null ? null : (T) Conversions.fromByteBuffer(boundType, buf); + } + + @Override + @SuppressWarnings("unchecked") + public T upperBound() { + if (boundType == null) { + return null; + } + + ByteBuffer buf = upperBounds == null ? null : upperBounds.get(fieldId); + return buf == null ? null : (T) Conversions.fromByteBuffer(boundType, buf); + } + + @Override + public boolean tightBounds() { + return false; + } + + @Override + public boolean hasValueCount() { + return count(valueCounts) != null; + } + + @Override + public long valueCount() { + return count(valueCounts); + } + + @Override + public boolean hasNullValueCount() { + return count(nullValueCounts) != null; + } + + @Override + public long nullValueCount() { + return count(nullValueCounts); + } + + @Override + public boolean hasNanValueCount() { + return count(nanValueCounts) != null; + } + + @Override + public long nanValueCount() { + return count(nanValueCounts); + } + + @Override + public Integer avgValueSizeInBytes() { + return null; + } + + private Long count(Map<Integer, Long> counts) { Review Comment: Style: This helper method should be located after the interface-defined methods but before the `private static` methods. -- 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]
