-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Refactor/rename metrics related classes for NaN support #1829
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
382d39d
Refactor/rename metrics related classes for NaN support
yyanyy 6ee1853
add javadoc and null check
yyanyy ee0ae1d
update based on comments in #1790
yyanyy e2da2be
add forgot file
yyanyy 4bb427f
address comments
yyanyy 55b1e5b
move generic testing outside of parquet folder
yyanyy 37a8681
fix timestamp type to use microseconds instead
yyanyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
|
|
||
| public class MetricsUtil { | ||
|
|
||
| private MetricsUtil() { | ||
| } | ||
|
|
||
| /** | ||
| * Construct mapping relationship between column id to NaN value counts from input metrics and metrics config. | ||
| */ | ||
| public static Map<Integer, Long> createNanValueCounts( | ||
| Stream<FieldMetrics> fieldMetrics, MetricsConfig metricsConfig, Schema inputSchema) { | ||
| Preconditions.checkNotNull(metricsConfig, "metricsConfig is required"); | ||
|
|
||
| if (fieldMetrics == null || inputSchema == null) { | ||
| return Maps.newHashMap(); | ||
| } | ||
|
|
||
| return fieldMetrics | ||
| .filter(metrics -> metricsMode(inputSchema, metricsConfig, metrics.id()) != MetricsModes.None.get()) | ||
| .collect(Collectors.toMap(FieldMetrics::id, FieldMetrics::nanValueCount)); | ||
| } | ||
|
|
||
| /** | ||
| * Extract MetricsMode for the given field id from metrics config. | ||
| */ | ||
| public static MetricsModes.MetricsMode metricsMode(Schema inputSchema, MetricsConfig metricsConfig, int fieldId) { | ||
| Preconditions.checkNotNull(inputSchema, "inputSchema is required"); | ||
| Preconditions.checkNotNull(metricsConfig, "metricsConfig is required"); | ||
|
|
||
| String columnName = inputSchema.findColumnName(fieldId); | ||
| return metricsConfig.columnMode(columnName); | ||
|
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. same for metrics config. |
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,10 @@ | |
|
|
||
| import java.math.BigDecimal; | ||
| import java.nio.ByteBuffer; | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.time.LocalDate; | ||
| import java.time.OffsetDateTime; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -42,6 +46,7 @@ | |
| import org.apache.spark.sql.types.BooleanType; | ||
| import org.apache.spark.sql.types.ByteType; | ||
| import org.apache.spark.sql.types.DataType; | ||
| import org.apache.spark.sql.types.DateType; | ||
| import org.apache.spark.sql.types.Decimal; | ||
| import org.apache.spark.sql.types.DecimalType; | ||
| import org.apache.spark.sql.types.DoubleType; | ||
|
|
@@ -52,6 +57,7 @@ | |
| import org.apache.spark.sql.types.ShortType; | ||
| import org.apache.spark.sql.types.StringType; | ||
| import org.apache.spark.sql.types.StructType; | ||
| import org.apache.spark.sql.types.TimestampType; | ||
| import org.apache.spark.unsafe.types.CalendarInterval; | ||
| import org.apache.spark.unsafe.types.UTF8String; | ||
|
|
||
|
|
@@ -115,12 +121,30 @@ public short getShort(int ordinal) { | |
|
|
||
| @Override | ||
| public int getInt(int ordinal) { | ||
| return struct.get(ordinal, Integer.class); | ||
| Object integer = struct.get(ordinal, Object.class); | ||
|
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. The reason for changing this class is discussed in this thread |
||
|
|
||
| if (integer instanceof Integer) { | ||
| return (int) integer; | ||
| } else if (integer instanceof LocalDate) { | ||
| return (int) ((LocalDate) integer).toEpochDay(); | ||
| } else { | ||
| throw new IllegalStateException("Unknown type for int field. Type name: " + integer.getClass().getName()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long getLong(int ordinal) { | ||
| return struct.get(ordinal, Long.class); | ||
| Object longVal = struct.get(ordinal, Object.class); | ||
|
|
||
| if (longVal instanceof Long) { | ||
| return (long) longVal; | ||
| } else if (longVal instanceof OffsetDateTime) { | ||
| return Duration.between(Instant.EPOCH, (OffsetDateTime) longVal).toNanos() / 1000; | ||
| } else if (longVal instanceof LocalDate) { | ||
| return ((LocalDate) longVal).toEpochDay(); | ||
| } else { | ||
| throw new IllegalStateException("Unknown type for long field. Type name: " + longVal.getClass().getName()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -240,6 +264,10 @@ public Object get(int ordinal, DataType dataType) { | |
| return getByte(ordinal); | ||
| } else if (dataType instanceof ShortType) { | ||
| return getShort(ordinal); | ||
| } else if (dataType instanceof DateType) { | ||
| return getInt(ordinal); | ||
| } else if (dataType instanceof TimestampType) { | ||
| return getLong(ordinal); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
inputschema can be null at this point.
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.
I'll add a validation check to ensure they are not null
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.
I agree. Let's add checks to fail when
metricsConfigorinputSchemais null. This is a useful method so I think it is worth keeping it public, but we would ideally not fail with aNullPointerException. This is not called in a tight loop, so it should be fine to add the checks on each call.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.
Yes, sorry I overwrote my own earlier change that addressed this when I renamed this file...