-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Core: present v4 ContentStats as legacy stat maps via ContentStatsBackedMap #17322
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 3 commits
dfcd6e9
132377a
49a135b
13c0b61
c2695c2
68db8ef
ddc6d8a
a07d902
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 |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /* | ||
| * 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 java.nio.ByteBuffer; | ||
| import java.util.AbstractMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
| import org.apache.iceberg.types.Conversions; | ||
| import org.apache.iceberg.types.Type; | ||
|
|
||
| /** | ||
| * A lazy, read-only {@link Map} view of one stat across the columns of a {@link ContentStats}, | ||
| * keyed by field ID, mirroring the per-column stat maps on {@link ContentFile}. | ||
| */ | ||
| class ContentStatsBackedMap<V> extends AbstractMap<Integer, V> { | ||
| private enum Kind { | ||
| VALUE_COUNT, | ||
| NULL_VALUE_COUNT, | ||
| NAN_VALUE_COUNT, | ||
| LOWER_BOUND, | ||
| UPPER_BOUND | ||
| } | ||
|
|
||
| /** Per-column value counts, or {@code null} if no column tracks the value count. */ | ||
| static <V> Map<Integer, V> valueCounts(ContentStats stats) { | ||
| return viewOrNull(stats, Kind.VALUE_COUNT); | ||
| } | ||
|
|
||
| /** Per-column null value counts, or {@code null} if no column tracks the null value count. */ | ||
| static <V> Map<Integer, V> nullValueCounts(ContentStats stats) { | ||
| return viewOrNull(stats, Kind.NULL_VALUE_COUNT); | ||
| } | ||
|
|
||
| /** Per-column NaN value counts, or {@code null} if no column tracks the NaN value count. */ | ||
| static <V> Map<Integer, V> nanValueCounts(ContentStats stats) { | ||
| return viewOrNull(stats, Kind.NAN_VALUE_COUNT); | ||
| } | ||
|
|
||
| /** Per-column lower bounds, or {@code null} if no column tracks a lower bound. */ | ||
| static <V> Map<Integer, V> lowerBounds(ContentStats stats) { | ||
| return viewOrNull(stats, Kind.LOWER_BOUND); | ||
| } | ||
|
|
||
| /** Per-column upper bounds, or {@code null} if no column tracks an upper bound. */ | ||
| static <V> Map<Integer, V> upperBounds(ContentStats stats) { | ||
| return viewOrNull(stats, Kind.UPPER_BOUND); | ||
| } | ||
|
|
||
| private static <V> Map<Integer, V> viewOrNull(ContentStats stats, Kind kind) { | ||
|
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. nit: this is private, shouldn't this be located next to the other private static methods? |
||
| return isEmpty(stats, kind) ? null : new ContentStatsBackedMap<>(stats, kind); | ||
| } | ||
|
|
||
| private final ContentStats stats; | ||
| private final Kind kind; | ||
| private Set<Entry<Integer, V>> materialized; | ||
|
|
||
| private ContentStatsBackedMap(ContentStats stats, Kind kind) { | ||
| this.stats = stats; | ||
| this.kind = kind; | ||
| } | ||
|
|
||
| @Override | ||
| public V get(Object key) { | ||
| if (!(key instanceof Integer)) { | ||
| return null; | ||
| } | ||
|
|
||
| FieldStats<?> fieldStats = stats.statsFor((Integer) key); | ||
| if (fieldStats == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return statValue(fieldStats, kind); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsKey(Object key) { | ||
| return get(key) != null; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
|
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. If I'm not mistaken, in the constructing methods we return null if the map were empty. Following this, in case we have constructed a map object it's not empty. Can we simply return false here? |
||
| // avoid AbstractMap's default, which materializes entrySet() just to answer emptiness | ||
| return isEmpty(stats, kind); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Entry<Integer, V>> entrySet() { | ||
|
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. Just for my understanding: I'm trying to understand the reason for introducing the cached materialized result. Do we expect
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. entrySet() is the only method that has to materialize (it's a full projection), so its result is cached: AbstractMap routes size(), forEach(), toString(), and equals() through entrySet(), and without the cache any caller that does more than a single pass would rebuild the LinkedHashSet every time. get()/containsKey() stay pass-through and allocate nothing. It isn't equivalent to the old MetricsUtil.valueCounts(stats), which eagerly built and returned a full map on every call. Here the map is a lazy view: a caller that only does get() or a null check never materializes a set, and an empty metric returns null from an allocation-free scan rather than an empty map. That laziness, plus returning null instead of an empty map, is the reason for the class. |
||
| if (materialized == null) { | ||
| Set<Entry<Integer, V>> entries = Sets.newLinkedHashSet(); | ||
| for (FieldStats<?> fieldStats : stats.fieldStats()) { | ||
| if (fieldStats != null) { | ||
| V value = statValue(fieldStats, kind); | ||
| if (value != null) { | ||
| entries.add(new SimpleImmutableEntry<>(fieldStats.fieldId(), value)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| this.materialized = entries; | ||
| } | ||
|
|
||
| return materialized; | ||
| } | ||
|
|
||
| /** Returns whether no column contributes an entry for the metric. */ | ||
| private static boolean isEmpty(ContentStats stats, Kind kind) { | ||
| for (FieldStats<?> fieldStats : stats.fieldStats()) { | ||
|
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. nit: maybe with stream?
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. isEmpty() is on the scan-planning's hot code path. A stream adds a Stream + lambda allocation per call, and ContentStats.fieldStats() returns an Iterable, so it would also need a Streams.stream(...) wrapper. The imperative loop short-circuits on the first contributing field and allocates nothing, so I'd prefer to keep it here. |
||
| if (fieldStats != null && isKnown(fieldStats, kind)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Whether statValue would return a non-null value, without allocating a boxed count or decoding a | ||
| // bound. Must mirror statValue's null-ness. | ||
| private static boolean isKnown(FieldStats<?> fieldStats, Kind kind) { | ||
| switch (kind) { | ||
| case VALUE_COUNT: | ||
| return fieldStats.hasValueCount(); | ||
| case NULL_VALUE_COUNT: | ||
| return fieldStats.hasNullValueCount(); | ||
| case NAN_VALUE_COUNT: | ||
| return fieldStats.hasNanValueCount(); | ||
| case LOWER_BOUND: | ||
| return fieldStats.lowerBound() != null; | ||
| case UPPER_BOUND: | ||
| return fieldStats.upperBound() != null; | ||
| default: | ||
| throw new IllegalArgumentException("Unknown content stats kind: " + kind); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static <V> V statValue(FieldStats<?> fieldStats, Kind kind) { | ||
| switch (kind) { | ||
|
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. nit: new switch style here and above? |
||
| case VALUE_COUNT: | ||
| return fieldStats.hasValueCount() ? (V) Long.valueOf(fieldStats.valueCount()) : null; | ||
| case NULL_VALUE_COUNT: | ||
| return fieldStats.hasNullValueCount() | ||
| ? (V) Long.valueOf(fieldStats.nullValueCount()) | ||
| : null; | ||
| case NAN_VALUE_COUNT: | ||
| return fieldStats.hasNanValueCount() ? (V) Long.valueOf(fieldStats.nanValueCount()) : null; | ||
| case LOWER_BOUND: | ||
| return (V) bound(fieldStats, fieldStats.lowerBound(), StatsUtil.LOWER_BOUND_NAME); | ||
| case UPPER_BOUND: | ||
| return (V) bound(fieldStats, fieldStats.upperBound(), StatsUtil.UPPER_BOUND_NAME); | ||
| default: | ||
| throw new IllegalArgumentException("Unknown content stats kind: " + kind); | ||
| } | ||
| } | ||
|
|
||
| private static ByteBuffer bound(FieldStats<?> fieldStats, Object bound, String boundFieldName) { | ||
| Type boundType = fieldStats.type().fieldType(boundFieldName); | ||
| // toByteBuffer returns null for a null bound | ||
| return boundType == null ? null : Conversions.toByteBuffer(boundType, bound); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,13 +43,22 @@ interface FieldStats<T> { | |
| */ | ||
| boolean tightBounds(); | ||
|
|
||
| /** The total value count, including null and NaN */ | ||
| /** Whether a value count is tracked for this field. */ | ||
| boolean hasValueCount(); | ||
|
|
||
| /** The total value count, including null and NaN, defined only when {@link #hasValueCount()}. */ | ||
| long valueCount(); | ||
|
|
||
| /** The total null value count */ | ||
|
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 think we also need a few more methods:
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 will add
|
||
| /** Whether a null value count is tracked for this field. */ | ||
| boolean hasNullValueCount(); | ||
|
|
||
| /** The total null value count, defined only when {@link #hasNullValueCount()}. */ | ||
| long nullValueCount(); | ||
|
|
||
| /** The total NaN value count */ | ||
| /** Whether a NaN value count is tracked for this field. */ | ||
| boolean hasNanValueCount(); | ||
|
|
||
| /** The total NaN value count, defined only when {@link #hasNanValueCount()}. */ | ||
| long nanValueCount(); | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,18 +143,38 @@ public boolean tightBounds() { | |
| return tightBounds; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasValueCount() { | ||
| return valueCount != null; | ||
| } | ||
|
|
||
| @Override | ||
| public long valueCount() { | ||
| Preconditions.checkState(hasValueCount(), "Field %s does not track a value count", fieldId); | ||
|
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 think this and similar checks should be removed. We don't need extra checks in tight loops that are only going to slow down scan planning. If this is called when the value count is missing then it is a bug in the caller. |
||
| return valueCount; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNullValueCount() { | ||
| return nullValueCount != null; | ||
| } | ||
|
|
||
| @Override | ||
| public long nullValueCount() { | ||
| Preconditions.checkState( | ||
| hasNullValueCount(), "Field %s does not track a null value count", fieldId); | ||
| return nullValueCount; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNanValueCount() { | ||
| return nanValueCount != null; | ||
| } | ||
|
|
||
| @Override | ||
| public long nanValueCount() { | ||
| Preconditions.checkState( | ||
| hasNanValueCount(), "Field %s does not track a NaN value count", fieldId); | ||
| return nanValueCount; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -55,6 +55,11 @@ private StatsUtil() {} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static final int NAN_VALUE_COUNT_OFFSET = 6; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static final int AVG_VALUE_SIZE_OFFSET = 7; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Bound field names, shared with the read-side lookups in ContentStatsBackedMap so a rename or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // typo cannot silently drift between the schema and the reader. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static final String LOWER_BOUND_NAME = "lower_bound"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static final String UPPER_BOUND_NAME = "upper_bound"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Offsets used within geo_lower struct | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final int GEO_LOWER_X_OFFSET = 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final int GEO_LOWER_Y_OFFSET = 11; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -249,12 +254,12 @@ private static Types.StructType geoUpperBound(int baseId) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static Types.NestedField lowerBoundField(Type type, int baseId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Type boundType = isGeoType(type) ? geoLowerBound(baseId) : type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return optional(baseId + LOWER_BOUND_OFFSET, "lower_bound", boundType); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
254
to
-252
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. Not direct relate to your change, but I think iceberg/api/src/main/java/org/apache/iceberg/types/Conversions.java Lines 95 to 145 in c8a4b98
I am not sure the status of upper/lower bound of geometry/geography type in main, but it seems we can run into
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. Confirmed ,this does throw UnsupportedOperationException for geometry and geography bounds. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return optional(baseId + LOWER_BOUND_OFFSET, LOWER_BOUND_NAME, boundType); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static Types.NestedField upperBoundField(Type type, int baseId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Type boundType = isGeoType(type) ? geoUpperBound(baseId) : type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return optional(baseId + UPPER_BOUND_OFFSET, "upper_bound", boundType); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return optional(baseId + UPPER_BOUND_OFFSET, UPPER_BOUND_NAME, boundType); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @VisibleForTesting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
nit:
nullinstead of{@code null}