rdblue commented on code in PR #12496: URL: https://github.com/apache/iceberg/pull/12496#discussion_r1988123170
########## parquet/src/main/java/org/apache/iceberg/parquet/ParquetMetrics.java: ########## @@ -0,0 +1,613 @@ +/* + * + * * 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.parquet; + +import java.nio.ByteBuffer; +import java.util.Comparator; +import java.util.Deque; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.iceberg.FieldMetrics; +import org.apache.iceberg.Metrics; +import org.apache.iceberg.MetricsConfig; +import org.apache.iceberg.MetricsModes; +import org.apache.iceberg.MetricsUtil; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.relocated.com.google.common.collect.Multimap; +import org.apache.iceberg.relocated.com.google.common.collect.Multimaps; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.relocated.com.google.common.collect.Streams; +import org.apache.iceberg.types.Comparators; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.BinaryUtil; +import org.apache.iceberg.util.NaNUtil; +import org.apache.iceberg.util.UnicodeUtil; +import org.apache.iceberg.variants.PhysicalType; +import org.apache.iceberg.variants.ShreddedObject; +import org.apache.iceberg.variants.VariantMetadata; +import org.apache.iceberg.variants.VariantValue; +import org.apache.iceberg.variants.Variants; +import org.apache.parquet.column.statistics.Statistics; +import org.apache.parquet.hadoop.metadata.BlockMetaData; +import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData; +import org.apache.parquet.hadoop.metadata.ColumnPath; +import org.apache.parquet.hadoop.metadata.ParquetMetadata; +import org.apache.parquet.schema.GroupType; +import org.apache.parquet.schema.MessageType; +import org.apache.parquet.schema.PrimitiveType; +import org.apache.parquet.schema.Type; + +class ParquetMetrics { + private ParquetMetrics() {} + + static Metrics metrics( + Schema schema, + MessageType type, + MetricsConfig metricsConfig, + ParquetMetadata metadata, + Stream<FieldMetrics<?>> fields) { + long rowCount = 0L; + Map<Integer, Long> columnSizes = Maps.newHashMap(); + Multimap<ColumnPath, ColumnChunkMetaData> columns = + Multimaps.newMultimap(Maps.newHashMap(), Lists::newArrayList); + for (BlockMetaData block : metadata.getBlocks()) { + rowCount += block.getRowCount(); + for (ColumnChunkMetaData column : block.getColumns()) { + Type.ID id = + type.getColumnDescription(column.getPath().toArray()).getPrimitiveType().getId(); + if (null == id) { + continue; + } + + int fieldId = id.intValue(); + MetricsModes.MetricsMode mode = MetricsUtil.metricsMode(schema, metricsConfig, fieldId); + if (mode != MetricsModes.None.get()) { + columns.put(column.getPath(), column); + columnSizes.put(fieldId, columnSizes.getOrDefault(fieldId, 0L) + column.getTotalSize()); + } + } + } + + Map<Integer, FieldMetrics<?>> metricsById = + fields.collect(Collectors.toMap(FieldMetrics::id, Function.identity())); + + Iterable<FieldMetrics<ByteBuffer>> results = + TypeWithSchemaVisitor.visit( + schema.asStruct(), + type, + new MetricsVisitor(schema, metricsConfig, metricsById, columns)); + + Map<Integer, Long> valueCounts = Maps.newHashMap(); + Map<Integer, Long> nullValueCounts = Maps.newHashMap(); + Map<Integer, Long> nanValueCounts = Maps.newHashMap(); + Map<Integer, ByteBuffer> lowerBounds = Maps.newHashMap(); + Map<Integer, ByteBuffer> upperBounds = Maps.newHashMap(); + + for (FieldMetrics<ByteBuffer> metrics : results) { + int id = metrics.id(); + if (metrics.valueCount() >= 0) { + valueCounts.put(id, metrics.valueCount()); + } + + if (metrics.nullValueCount() >= 0) { + nullValueCounts.put(id, metrics.nullValueCount()); + } + + if (metrics.nanValueCount() >= 0) { + nanValueCounts.put(id, metrics.nanValueCount()); + } + + if (metrics.hasBounds()) { + lowerBounds.put(id, metrics.lowerBound()); + upperBounds.put(id, metrics.upperBound()); + } + } + + return new Metrics( + rowCount, + columnSizes, + valueCounts, + nullValueCounts, + nanValueCounts, + lowerBounds, + upperBounds); + } + + private static class MetricsVisitor + extends TypeWithSchemaVisitor<Iterable<FieldMetrics<ByteBuffer>>> { + private final Schema schema; + private final MetricsConfig metricsConfig; + private final Map<Integer, FieldMetrics<?>> metricsById; + private final Multimap<ColumnPath, ColumnChunkMetaData> columns; + + private MetricsVisitor( + Schema schema, + MetricsConfig metricsConfig, + Map<Integer, FieldMetrics<?>> metricsById, + Multimap<ColumnPath, ColumnChunkMetaData> columns) { + this.schema = schema; + this.metricsConfig = metricsConfig; + this.metricsById = metricsById; + this.columns = columns; + } + + @Override + public Iterable<FieldMetrics<ByteBuffer>> message( + Types.StructType iStruct, + MessageType message, + List<Iterable<FieldMetrics<ByteBuffer>>> fieldResults) { + return Iterables.concat(fieldResults); + } + + @Override + public Iterable<FieldMetrics<ByteBuffer>> struct( + Types.StructType iStruct, + GroupType struct, + List<Iterable<FieldMetrics<ByteBuffer>>> fieldResults) { + return Iterables.concat(fieldResults); + } + + @Override + public Iterable<FieldMetrics<ByteBuffer>> list( + Types.ListType iList, GroupType array, Iterable<FieldMetrics<ByteBuffer>> elementResults) { + // remove lower and upper bounds for repeated fields + return ImmutableList.of(); + } + + @Override + public Iterable<FieldMetrics<ByteBuffer>> map( + Types.MapType iMap, + GroupType map, + Iterable<FieldMetrics<ByteBuffer>> keyResults, + Iterable<FieldMetrics<ByteBuffer>> valueResults) { + // repeated fields are not currently supported + return ImmutableList.of(); + } + + @Override + public Iterable<FieldMetrics<ByteBuffer>> primitive( + org.apache.iceberg.types.Type.PrimitiveType iPrimitive, PrimitiveType primitive) { + Type.ID id = primitive.getId(); + if (null == id) { + return ImmutableList.of(); + } + int fieldId = id.intValue(); + + MetricsModes.MetricsMode mode = MetricsUtil.metricsMode(schema, metricsConfig, fieldId); + if (mode == MetricsModes.None.get()) { + return ImmutableList.of(); + } + + int length = truncateLength(mode); + + FieldMetrics<ByteBuffer> metrics = metricsFromFieldMetrics(fieldId, iPrimitive, length); Review Comment: The main value of doing this in the visitor is to avoid processing the column metadata if there is already a `FieldMetrics` object available. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org