-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Core: guard MetricsUtil.nullValueCounts for columns without null_value_count #17306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| && StatsUtil.tracksStat(fs.type(), fs.fieldId(), StatsUtil.NULL_VALUE_COUNT_OFFSET)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| result.put(fs.fieldId(), fs.nullValueCount()); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 This is going to be part of the PR I'm working on with an evaluator. |
||
| return fieldStatsType.field(toBaseId(fieldId) + statOffset) != null; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
|
||
| public static Types.NestedField contentStatsField(Types.StructType contentStats) { | ||
| return optional(146, "content_stats", contentStats); | ||
| } | ||
|
|
||
| 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 { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while adding test coverage for the |
||
| 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(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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