-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Make metrics collection configurable via table properties #263
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
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
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,61 @@ | ||
| /* | ||
| * 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 com.google.common.collect.Maps; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.MetricsModes.MetricsMode; | ||
|
|
||
| import static org.apache.iceberg.TableProperties.DEFAULT_WRITE_METRICS_MODE; | ||
| import static org.apache.iceberg.TableProperties.DEFAULT_WRITE_METRICS_MODE_DEFAULT; | ||
|
|
||
| public class MetricsConfig { | ||
|
|
||
| private static final String COLUMN_CONF_PREFIX = "write.metadata.metrics.column."; | ||
|
|
||
| private Map<String, MetricsMode> columnModes = Maps.newHashMap(); | ||
| private MetricsMode defaultMode; | ||
|
|
||
| private MetricsConfig() {} | ||
|
|
||
| public static MetricsConfig getDefault() { | ||
| MetricsConfig spec = new MetricsConfig(); | ||
| spec.defaultMode = MetricsModes.fromString(DEFAULT_WRITE_METRICS_MODE_DEFAULT); | ||
| return spec; | ||
| } | ||
|
|
||
| public static MetricsConfig fromProperties(Map<String, String> props) { | ||
| MetricsConfig spec = new MetricsConfig(); | ||
| props.keySet().stream() | ||
| .filter(key -> key.startsWith(COLUMN_CONF_PREFIX)) | ||
| .forEach(key -> { | ||
| MetricsMode mode = MetricsModes.fromString(props.get(key)); | ||
| String columnAlias = key.replaceFirst(COLUMN_CONF_PREFIX, ""); | ||
| spec.columnModes.put(columnAlias, mode); | ||
| }); | ||
| String defaultModeAsString = props.getOrDefault(DEFAULT_WRITE_METRICS_MODE, DEFAULT_WRITE_METRICS_MODE_DEFAULT); | ||
| spec.defaultMode = MetricsModes.fromString(defaultModeAsString); | ||
| return spec; | ||
| } | ||
|
|
||
| public MetricsMode columnMode(String columnAlias) { | ||
| return columnModes.getOrDefault(columnAlias, defaultMode); | ||
| } | ||
| } |
147 changes: 147 additions & 0 deletions
147
core/src/main/java/org/apache/iceberg/MetricsModes.java
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,147 @@ | ||
| /* | ||
| * 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 com.google.common.base.Preconditions; | ||
| import java.util.Locale; | ||
| import java.util.Objects; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * This class defines different metrics modes, which allow users to control the collection of | ||
| * value_counts, null_value_counts, lower_bounds, upper_bounds for different columns in metadata. | ||
| */ | ||
| public class MetricsModes { | ||
|
|
||
| private static final Pattern TRUNCATE = Pattern.compile("truncate\\((\\d+)\\)"); | ||
|
|
||
| private MetricsModes() {} | ||
|
|
||
| public static MetricsMode fromString(String mode) { | ||
| if ("none".equalsIgnoreCase(mode)) { | ||
| return None.get(); | ||
| } else if ("counts".equalsIgnoreCase(mode)) { | ||
| return Counts.get(); | ||
| } else if ("full".equalsIgnoreCase(mode)) { | ||
| return Full.get(); | ||
| } | ||
|
|
||
| Matcher truncateMatcher = TRUNCATE.matcher(mode.toLowerCase(Locale.ENGLISH)); | ||
| if (truncateMatcher.matches()) { | ||
| int length = Integer.parseInt(truncateMatcher.group(1)); | ||
| return Truncate.withLength(length); | ||
| } | ||
|
|
||
| throw new IllegalArgumentException("Invalid metrics mode: " + mode); | ||
| } | ||
|
|
||
| public interface MetricsMode {} | ||
|
|
||
| /** | ||
| * Under this mode, value_counts, null_value_counts, lower_bounds, upper_bounds are not persisted. | ||
| */ | ||
| public static class None implements MetricsMode { | ||
| private static final None INSTANCE = new None(); | ||
|
|
||
| public static None get() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "none"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Under this mode, only value_counts, null_value_counts are persisted. | ||
| */ | ||
| public static class Counts implements MetricsMode { | ||
| private static final Counts INSTANCE = new Counts(); | ||
|
|
||
| public static Counts get() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "counts"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Under this mode, value_counts, null_value_counts and truncated lower_bounds, upper_bounds are persisted. | ||
| */ | ||
| public static class Truncate implements MetricsMode { | ||
| private final int length; | ||
|
|
||
| private Truncate(int length) { | ||
| this.length = length; | ||
| } | ||
|
|
||
| public static Truncate withLength(int length) { | ||
| Preconditions.checkArgument(length > 0, "Truncate length should be positive"); | ||
| return new Truncate(length); | ||
| } | ||
|
|
||
| public int length() { | ||
| return length; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("truncate(%d)", length); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
| if (obj == null || getClass() != obj.getClass()) { | ||
| return false; | ||
| } | ||
| Truncate truncate = (Truncate) obj; | ||
| return length == truncate.length; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(length); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Under this mode, value_counts, null_value_counts and full lower_bounds, upper_bounds are persisted. | ||
| */ | ||
| public static class Full implements MetricsMode { | ||
| private static final Full INSTANCE = new Full(); | ||
|
|
||
| public static Full get() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "full"; | ||
| } | ||
| } | ||
| } | ||
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
54 changes: 54 additions & 0 deletions
54
core/src/test/java/org/apache/iceberg/TestMetricsModes.java
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,54 @@ | ||
| /* | ||
| * 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 org.apache.iceberg.MetricsModes.Counts; | ||
| import org.apache.iceberg.MetricsModes.Full; | ||
| import org.apache.iceberg.MetricsModes.None; | ||
| import org.apache.iceberg.MetricsModes.Truncate; | ||
| import org.junit.Assert; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.ExpectedException; | ||
|
|
||
| public class TestMetricsModes { | ||
|
|
||
| @Rule | ||
| public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
|
||
| @Test | ||
| public void testMetricsModeParsing() { | ||
| Assert.assertEquals(None.get(), MetricsModes.fromString("none")); | ||
| Assert.assertEquals(None.get(), MetricsModes.fromString("nOnE")); | ||
| Assert.assertEquals(Counts.get(), MetricsModes.fromString("counts")); | ||
| Assert.assertEquals(Counts.get(), MetricsModes.fromString("coUntS")); | ||
| Assert.assertEquals(Truncate.withLength(1), MetricsModes.fromString("truncate(1)")); | ||
| Assert.assertEquals(Truncate.withLength(10), MetricsModes.fromString("truNcAte(10)")); | ||
| Assert.assertEquals(Full.get(), MetricsModes.fromString("full")); | ||
| Assert.assertEquals(Full.get(), MetricsModes.fromString("FULL")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidTruncationLength() { | ||
| exceptionRule.expect(IllegalArgumentException.class); | ||
| exceptionRule.expectMessage("length should be positive"); | ||
| MetricsModes.fromString("truncate(0)"); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
It would be good to have some docs here for what the modes are.
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've added short descriptions to
MetricsModesand eachMetricsMode.