Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/src/main/java/org/apache/iceberg/MetricsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,10 @@ static Map<Integer, Long> nullValueCounts(ContentStats stats) {

Map<Integer, Long> result = Maps.newHashMap();
for (FieldStats<?> fs : stats.fieldStats()) {
if (fs != null) {
// null_value_count is only stored for optional (nullable) columns; skip columns that do not
// track it so the primitive nullValueCount() accessor is never called on an absent value.
if (fs != null

@stevenzwu stevenzwu Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this guard is the main fix of this PR

&& StatsUtil.tracksStat(fs.type(), fs.fieldId(), StatsUtil.NULL_VALUE_COUNT_OFFSET)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we should be "fixing" this. I probably should have objected more strongly to adding these conversion methods because I don't think it makes sense to convert to maps. Either we should expose everything as ContentStats and not translate at all, or we should make Map implementations that lazily convert from ContentStats in get.

result.put(fs.fieldId(), fs.nullValueCount());
}
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/apache/iceberg/StatsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ static int statOffset(int statId) {
return statId % NUM_RESERVED_FIELD_STATS_IDS;
}

/**
* Returns whether the field stats struct for the given field ID tracks the metric at the offset.
*/
static boolean tracksStat(Types.StructType fieldStatsType, int fieldId, int statOffset) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think this is the right approach.

We need to signal whether a stat is present or not and I'm not confident that the right way to do that is to return it as null. That's an open question, but I think the range of possibilities is either to go back to Long for null count, or to add a specific hasNullCount() accessor, not to have a util method that does the null check.

This is going to be part of the PR I'm working on with an evaluator.

return fieldStatsType.field(toBaseId(fieldId) + statOffset) != null;

@stevenzwu stevenzwu Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought field id is the more canonical identifier and hence went with this route.

Another option is to use field name to get the field.

static boolean tracksStat(Types.StructType fieldStatsType, String statFieldName) {
  return fieldStatsType.field(statFieldName) != null;
}

}

public static Types.NestedField contentStatsField(Types.StructType contentStats) {
return optional(146, "content_stats", contentStats);
}
Expand Down
85 changes: 85 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestMetricsUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.util.List;
import java.util.Map;
import org.apache.iceberg.types.Types;
import org.junit.jupiter.api.Test;

public class TestMetricsUtil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while adding test coverage for the MetricsUtil.nullValueCounts(stats) util method, I thought it is good to let AI generate other test coverage code too.

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 = StatsUtil.statsReadSchema(SCHEMA, List.of(1, 2, 3));

private static Types.StructType statsType(String name) {
return STATS.field(name).type().asStructType();
}

@Test
public void testValueCounts() {
ContentStatsStruct stats = new ContentStatsStruct(STATS);
stats.setStats(1, new FieldStatsStruct<>(statsType("req"), 1L, 5L, false, 10L, 0L, 0L, null));
stats.setStats(2, new FieldStatsStruct<>(statsType("opt"), 1L, 5L, false, 20L, 0L, 0L, null));
stats.setStats(3, new FieldStatsStruct<>(statsType("dbl"), 1.0, 5.0, false, 30L, 0L, 0L, null));

// value_count is tracked for every column
assertThat(MetricsUtil.valueCounts(stats))
.containsOnly(Map.entry(1, 10L), Map.entry(2, 20L), Map.entry(3, 30L));
}

@Test
public void testNullValueCountsSkipsColumnsThatDoNotTrackIt() {
ContentStatsStruct stats = new ContentStatsStruct(STATS);
// A required column has no null_value_count field; a deserialized struct leaves it null, so
// nullValueCounts must not call the primitive accessor on it.
stats.setStats(1, new FieldStatsStruct<Long>(statsType("req")));
stats.setStats(2, new FieldStatsStruct<>(statsType("opt"), 1L, 5L, false, 20L, 3L, 0L, null));
stats.setStats(3, new FieldStatsStruct<>(statsType("dbl"), 1.0, 5.0, false, 30L, 7L, 0L, null));

// required column 1 is skipped; the optional columns are kept
assertThat(MetricsUtil.nullValueCounts(stats)).containsOnly(Map.entry(2, 3L), Map.entry(3, 7L));
}

@Test
public void testNanValueCountsOnlyForFloatingColumns() {
ContentStatsStruct stats = new ContentStatsStruct(STATS);
stats.setStats(2, new FieldStatsStruct<>(statsType("opt"), 1L, 5L, false, 20L, 0L, 0L, null));
stats.setStats(3, new FieldStatsStruct<>(statsType("dbl"), 1.0, 5.0, false, 30L, 0L, 4L, null));

// nan_value_count is tracked only for floating-point columns
assertThat(MetricsUtil.nanValueCounts(stats)).containsOnly(Map.entry(3, 4L));
}

@Test
public void testNullContentStatsReturnsNull() {
assertThat(MetricsUtil.valueCounts(null)).isNull();
assertThat(MetricsUtil.nullValueCounts(null)).isNull();
assertThat(MetricsUtil.nanValueCounts(null)).isNull();
assertThat(MetricsUtil.lowerBounds(null)).isNull();
assertThat(MetricsUtil.upperBounds(null)).isNull();
}
}
19 changes: 19 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestStatsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,23 @@ private static void assertSameStructure(Types.StructType expected, Types.StructT
}
}
}

@Test
public void testTracksStat() {
Schema schema =
new Schema(
Types.NestedField.required(1, "req", Types.LongType.get()),
Types.NestedField.optional(2, "opt", Types.LongType.get()));
Types.StructType stats = StatsUtil.statsReadSchema(schema, List.of(1, 2));
Types.StructType reqStats = stats.field("req").type().asStructType();
Types.StructType optStats = stats.field("opt").type().asStructType();

// value_count is tracked for every column
assertThat(StatsUtil.tracksStat(reqStats, 1, StatsUtil.VALUE_COUNT_OFFSET)).isTrue();
assertThat(StatsUtil.tracksStat(optStats, 2, StatsUtil.VALUE_COUNT_OFFSET)).isTrue();

// null_value_count is tracked only for optional (nullable) columns
assertThat(StatsUtil.tracksStat(reqStats, 1, StatsUtil.NULL_VALUE_COUNT_OFFSET)).isFalse();
assertThat(StatsUtil.tracksStat(optStats, 2, StatsUtil.NULL_VALUE_COUNT_OFFSET)).isTrue();
}
}
Loading