anoopj commented on code in PR #17557:
URL: https://github.com/apache/iceberg/pull/17557#discussion_r3744962985
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetMetrics.java:
##########
@@ -579,7 +587,12 @@ private ParquetVariantUtil.VariantMetrics counts() {
}
valueCount += column.getValueCount();
- nullCount += hasOnlyNullVariants ? column.getValueCount() :
stats.getNumNulls();
+ if (hasOnlyNullVariants) {
Review Comment:
We are touching these variant paths, but we don't seem to have test coverage
for it?
##########
parquet/src/test/java/org/apache/iceberg/parquet/TestParquetMissingNullCount.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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 static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Stream;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Files;
+import org.apache.iceberg.ManifestFiles;
+import org.apache.iceberg.ManifestReader;
+import org.apache.iceberg.ManifestWriter;
+import org.apache.iceberg.Metrics;
+import org.apache.iceberg.MetricsConfig;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.TestTables;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.expressions.InclusiveMetricsEvaluator;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types.IntegerType;
+import org.apache.parquet.column.Encoding;
+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.CompressionCodecName;
+import org.apache.parquet.hadoop.metadata.FileMetaData;
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.apache.parquet.schema.Types;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests metrics for files whose column chunk stats omit {@code null_count},
which is allowed by the
+ * format. Parquet reports a missing count as -1 from {@link
Statistics#getNumNulls()}, so it must
+ * not be summed into a total.
+ */
+public class TestParquetMissingNullCount {
+
+ private static final MessageType PARQUET_SCHEMA =
+ Types.buildMessage()
+ .addField(
+ Types.primitive(PrimitiveType.PrimitiveTypeName.INT32,
Type.Repetition.OPTIONAL)
+ .id(1)
+ .named("id"))
+ .named("table");
+
+ private static final PrimitiveType ID_TYPE =
PARQUET_SCHEMA.getType("id").asPrimitiveType();
+
+ private static final Schema SCHEMA = new Schema(optional(1, "id",
IntegerType.get()));
+ private static final PartitionSpec SPEC =
PartitionSpec.builderFor(SCHEMA).build();
+
+ @Test
+ public void testMissingNullCountInSingleRowGroup() {
+ Metrics metrics = metrics(block(statsWithoutNullCount(1, 10), 10));
+
+ assertThat(metrics.nullValueCounts()).doesNotContainKey(1);
+ assertThat(metrics.valueCounts()).containsEntry(1, 10L);
+ // a missing null count does not invalidate the bounds
+ assertThat(metrics.lowerBounds()).containsKey(1);
+ assertThat(metrics.upperBounds()).containsKey(1);
+ }
+
+ @Test
+ public void testMissingNullCountInOneOfTwoRowGroups() {
+ // without accounting for the -1 sentinel, this sums to an incorrect null
count of 0
+ Metrics metrics = metrics(block(statsWithoutNullCount(1, 10), 10),
block(stats(20, 30, 1), 10));
+
+ assertThat(metrics.nullValueCounts()).doesNotContainKey(1);
+ assertThat(metrics.valueCounts()).containsEntry(1, 20L);
+ }
+
+ @Test
+ public void testMissingNullCountAfterKnownNullCount() {
+ // the -1 sentinel must also be detected when it is not the first row group
+ Metrics metrics = metrics(block(stats(20, 30, 5), 10),
block(statsWithoutNullCount(1, 10), 10));
+
+ assertThat(metrics.nullValueCounts()).doesNotContainKey(1);
+ }
+
+ @Test
+ public void testMissingNullCountWithCountsMode() {
+ Metrics metrics =
+ metrics(
+ MetricsConfig.fromProperties(
+ Collections.singletonMap("write.metadata.metrics.default",
"counts")),
+ block(statsWithoutNullCount(1, 10), 10),
+ block(stats(20, 30, 1), 10));
+
+ assertThat(metrics.nullValueCounts()).doesNotContainKey(1);
+ assertThat(metrics.valueCounts()).containsEntry(1, 20L);
+ }
+
+ @Test
+ public void testKnownNullCountsAreSummed() {
+ Metrics metrics = metrics(block(stats(1, 10, 2), 10), block(stats(20, 30,
3), 10));
+
+ assertThat(metrics.nullValueCounts()).containsEntry(1, 5L);
+ assertThat(metrics.valueCounts()).containsEntry(1, 20L);
+ }
+
+ @Test
+ public void testMissingNullCountIsNotWrittenToManifest(@TempDir Path temp)
throws IOException {
+ // ensure the null count is not written into manifest when there's missing
+ // stats for a column chunk.
+ DataFile file =
+ DataFiles.builder(SPEC)
+ .withPath("/path/to/data.parquet")
+ .withFormat(FileFormat.PARQUET)
+ .withFileSizeInBytes(1024)
+ .withMetrics(
+ metrics(block(statsWithoutNullCount(1, 10), 10),
block(stats(20, 30, 1), 10)))
+ .build();
+
+ OutputFile outputFile =
Files.localOutput(temp.resolve("manifest.avro").toFile());
Review Comment:
This test only round-trips bytes through the manifest writer/reader, so it
doesn't need the filesystem. Prefer `InMemoryOutputFile` + toInputFile() (see
TestVariantMetrics for an example)
--
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]