rdblue commented on code in PR #17322: URL: https://github.com/apache/iceberg/pull/17322#discussion_r3641626635
########## core/src/test/java/org/apache/iceberg/TestContentStatsBackedMap.java: ########## @@ -0,0 +1,173 @@ +/* + * 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 static org.apache.iceberg.types.Types.NestedField.optional; +import static org.apache.iceberg.types.Types.NestedField.required; +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public class TestContentStatsBackedMap { + private static final Schema SCHEMA = + new Schema( + required(1, "req", Types.LongType.get()), + optional(2, "opt", Types.LongType.get()), + optional(3, "dbl", Types.DoubleType.get())); + private static final Types.StructType STATS_TYPE = + StatsUtil.statsReadSchema(SCHEMA, List.of(1, 2, 3)); + + private static Types.StructType statsType(String name) { + return STATS_TYPE.field(name).type().asStructType(); + } + + private static final ContentStatsStruct POPULATED_STATS = new ContentStatsStruct(STATS_TYPE); + private static final ContentStatsStruct ONLY_REQUIRED_STATS = new ContentStatsStruct(STATS_TYPE); + + static { + POPULATED_STATS.setStats(1, mockFieldStats("req", 1, 1L, 5L, 10L, null, null)); + POPULATED_STATS.setStats(2, mockFieldStats("opt", 2, 2L, 6L, 20L, 3L, null)); + POPULATED_STATS.setStats(3, mockFieldStats("dbl", 3, 1.0, 9.0, 30L, 7L, 4L)); + + // only a required long column: tracks a value count but no null or NaN count + ONLY_REQUIRED_STATS.setStats(1, mockFieldStats("req", 1, 1L, 5L, 10L, null, null)); + } + + @Test + public void testValueCounts() { + Map<Integer, Long> map = ContentStatsBackedMap.valueCounts(POPULATED_STATS); + assertThat(map).containsOnly(Map.entry(1, 10L), Map.entry(2, 20L), Map.entry(3, 30L)); + } + + @Test + public void testNullValueCountsSkipsColumnsThatDoNotTrackIt() { + // required column 1 does not track null_value_count; only the optional columns do + Map<Integer, Long> map = ContentStatsBackedMap.nullValueCounts(POPULATED_STATS); + assertThat(map.get(1)).isNull(); + assertThat(map.containsKey(1)).isFalse(); + assertThat(map).containsOnly(Map.entry(2, 3L), Map.entry(3, 7L)); + } + + @Test + public void testGetDoesNotUnboxUntrackedNullCount() { + // a deserialized required-column struct leaves null_value_count null; get must not unbox it. + // Column 2 tracks the null count, so the view itself is non-null. + ContentStatsStruct stats = new ContentStatsStruct(STATS_TYPE); + stats.setStats(1, mockFieldStats("req", 1, null, null, null, null, null)); + stats.setStats(2, mockFieldStats("opt", 2, 2L, 6L, 20L, 3L, null)); + Map<Integer, Long> map = ContentStatsBackedMap.nullValueCounts(stats); + assertThat(map.get(1)).isNull(); + assertThat(map).containsOnly(Map.entry(2, 3L)); + } + + @Test + public void testNanValueCountsOnlyForFloatingColumns() { + Map<Integer, Long> map = ContentStatsBackedMap.nanValueCounts(POPULATED_STATS); + assertThat(map).containsOnly(Map.entry(3, 4L)); + } + + @Test + public void testLowerAndUpperBounds() { + Map<Integer, ByteBuffer> lower = ContentStatsBackedMap.lowerBounds(POPULATED_STATS); + Map<Integer, ByteBuffer> upper = ContentStatsBackedMap.upperBounds(POPULATED_STATS); + assertThat(lower.get(1)).isEqualTo(Conversions.toByteBuffer(Types.LongType.get(), 1L)); + assertThat(upper.get(3)).isEqualTo(Conversions.toByteBuffer(Types.DoubleType.get(), 9.0)); + assertThat(lower).containsOnlyKeys(1, 2, 3); + } + + @Test + public void testGetReturnsNullForMissingKey() { + Map<Integer, Long> map = ContentStatsBackedMap.valueCounts(POPULATED_STATS); + assertThat(map.get(999)).isNull(); + } + + @Test + public void testGetReturnsNullForNonIntegerKey() { + // consistent with common Map implementations: a wrong-typed key is absent, not an error + Map<Integer, Long> map = ContentStatsBackedMap.valueCounts(POPULATED_STATS); + assertThat(map.get("not-an-int")).isNull(); + assertThat(map.containsKey("not-an-int")).isFalse(); + } + + @Test + public void testFactoryReturnsNullWhenNoColumnTracksMetric() { + // only a required long column: it tracks neither null_value_count nor nan_value_count + assertThat(ContentStatsBackedMap.nullValueCounts(ONLY_REQUIRED_STATS)).isNull(); + assertThat(ContentStatsBackedMap.nanValueCounts(ONLY_REQUIRED_STATS)).isNull(); + + Map<Integer, Long> valueCounts = ContentStatsBackedMap.valueCounts(ONLY_REQUIRED_STATS); + assertThat(valueCounts).isNotNull().containsOnly(Map.entry(1, 10L)); + } + + @Test + public void testFactoryReturnsPopulatedViewWhenColumnsTrackMetric() { + Map<Integer, Long> nanCounts = ContentStatsBackedMap.nanValueCounts(POPULATED_STATS); + assertThat(nanCounts).isNotNull().containsOnly(Map.entry(3, 4L)); + + Map<Integer, Long> nullCounts = ContentStatsBackedMap.nullValueCounts(POPULATED_STATS); + assertThat(nullCounts).isNotNull().containsOnly(Map.entry(2, 3L), Map.entry(3, 7L)); + } + + @Test + public void testPopulatedViewIsNotEmpty() { + // isEmpty() answers from a scan without materializing entrySet + assertThat(ContentStatsBackedMap.valueCounts(ONLY_REQUIRED_STATS)).isNotEmpty(); Review Comment: I think this is okay, but this relies on the same `isEmpty` implementation as the cases that the map is null and all cases where the map is non-null. As a result, the only thing actually being tested here is that `isEmpty()` is backed by `isEmpty(stats, kind)`. I'm not sure I'd include this but it's fine if you think it's important. -- 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]
