edgarRd commented on a change in pull request #199: ORC metrics and listPartition support URL: https://github.com/apache/incubator-iceberg/pull/199#discussion_r291774669
########## File path: orc/src/main/java/org/apache/iceberg/orc/OrcMetrics.java ########## @@ -0,0 +1,142 @@ +/* + * 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.orc; + +import static org.apache.iceberg.types.Conversions.toByteBuffer; + +import com.google.common.collect.Maps; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.iceberg.Metrics; +import org.apache.iceberg.Schema; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.hadoop.HadoopInputFile; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; +import org.apache.orc.ColumnStatistics; +import org.apache.orc.DateColumnStatistics; +import org.apache.orc.DecimalColumnStatistics; +import org.apache.orc.DoubleColumnStatistics; +import org.apache.orc.IntegerColumnStatistics; +import org.apache.orc.OrcFile; +import org.apache.orc.Reader; +import org.apache.orc.StringColumnStatistics; +import org.apache.orc.TimestampColumnStatistics; + +public class OrcMetrics { + + private OrcMetrics() {} + + public static Metrics fromInputFile(InputFile file) { + final Configuration config = (file instanceof HadoopInputFile) + ? ((HadoopInputFile)file).getConf() + : new Configuration(); + return fromInputFile(file, config); + } + + public static Metrics fromInputFile(InputFile file, Configuration config) { + try { + final Reader orcReader = OrcFile.createReader(new Path(file.location()), + OrcFile.readerOptions(config)); + final Schema schema = TypeConversion.fromOrc(orcReader.getSchema()); + + ColumnStatistics[] colStats = orcReader.getStatistics(); + Map<Integer, Long> columSizes = Maps.newHashMapWithExpectedSize(colStats.length); + Map<Integer, Long> valueCounts = Maps.newHashMapWithExpectedSize(colStats.length); + Map<Integer, ByteBuffer> lowerBounds = Maps.newHashMap(); + Map<Integer, ByteBuffer> upperBounds = Maps.newHashMap(); + + for(Types.NestedField col : schema.columns()) { + final int i = col.fieldId(); + columSizes.put(i, colStats[i].getBytesOnDisk()); Review comment: I think this assumption of `id`->`index` in column statistics is directly done in ORC itself. I don't think there's anything else to map column to stats object if not for the index, which afaict is an assumption done in the [Reader interface](https://github.com/apache/orc/blob/master/java/core/src/java/org/apache/orc/Reader.java), the [implementation follows this assumption](https://github.com/apache/orc/blob/master/java/core/src/java/org/apache/orc/impl/ReaderImpl.java#L254). > E.g if we assume the initial schema is {0 a int, 1 b int} and we add a column in the middle so the new schema is {0 a int, 2 c int, 1 b int} . So col c has fieldId 2 but is at index 1. Note that I'm making an assumption about ORC here about col c being at index 1. In this example, if we follow the assumption done that array index (in column stats) maps to column id (preserved in `filedId`) then this is still correct. I don't think `ColumnStatistics` in the ProtoBuf object (in the file footer) gets rearranged to match the column order; however, it seems like the indices are preserved. > I see Parquet does it by field name org.apache.iceberg.parquet.ParquetUtil#footerMetrics This seems to be possible because the column object obtained from the iteration contains the related stats information; instead in ORC these two pieces of information are detached. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
