-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Truncate stats from Parquet files #254
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 1 commit
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,79 @@ | ||
| /* | ||
| * 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.util; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import java.nio.ByteBuffer; | ||
| import org.apache.iceberg.expressions.Literal; | ||
|
|
||
| public class BinaryUtil { | ||
| // not meant to be instantiated | ||
| private BinaryUtil() { | ||
| } | ||
|
|
||
| /** | ||
| * Truncates the input byte buffer to the given length | ||
| */ | ||
| public static ByteBuffer truncateBinary(ByteBuffer input, int length) { | ||
| Preconditions.checkArgument(length > 0 && length < input.remaining(), | ||
| "Truncate length should be positive and lower than the number of remaining elements"); | ||
| byte[] array = new byte[length]; | ||
| input.duplicate().get(array); | ||
| return ByteBuffer.wrap(array); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a byte buffer whose length is lesser than or equal to truncateLength and is lower than the given input | ||
| */ | ||
| public static Literal<ByteBuffer> truncateBinaryMin(Literal<ByteBuffer> input, int length) { | ||
| ByteBuffer inputBuffer = input.value(); | ||
| if (length >= inputBuffer.remaining()) { | ||
| return input; | ||
| } | ||
| return Literal.of(truncateBinary(inputBuffer, length)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a byte buffer whose length is lesser than or equal to truncateLength and is greater than the given input | ||
| */ | ||
| public static Literal<ByteBuffer> truncateBinaryMax(Literal<ByteBuffer> input, int length) { | ||
| ByteBuffer inputBuffer = input.value(); | ||
| if (length >= inputBuffer.remaining()) { | ||
| return input; | ||
| } | ||
|
|
||
| // Truncate the input to the specified truncate length. | ||
| ByteBuffer truncatedInput = truncateBinary(inputBuffer, length); | ||
|
|
||
| // Try incrementing the bytes from the end. If all bytes overflow after incrementing, then return null | ||
| for (int i = length - 1; i >= 0; --i) { | ||
| byte element = truncatedInput.get(i); | ||
| element = (byte) (element + 1); | ||
| if (element != 0) { // No overflow | ||
| truncatedInput.put(i, element); | ||
| // Return a byte buffer whose position is zero and limit is i + 1 | ||
| truncatedInput.position(0); | ||
| truncatedInput.limit(i + 1); | ||
| return Literal.of(truncatedInput); | ||
| } | ||
| } | ||
| return null; // Cannot find a valid upper bound | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * 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.util; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.expressions.Literal; | ||
|
|
||
| public class UnicodeUtil { | ||
| // not meant to be instantiated | ||
| private UnicodeUtil() { | ||
| } | ||
|
|
||
| /** | ||
| * Determines if the given character value is a unicode high-surrogate code unit. | ||
| * The range of high-surrogates is 0xD800 - 0xDBFF. | ||
| */ | ||
| public static boolean isCharHighSurrogate(char ch) { | ||
| return (ch & '\uFC00') == '\uD800'; // 0xDC00 - 0xDFFF shouldn't match | ||
| } | ||
|
|
||
| /** | ||
| * Truncates the input charSequence such that the truncated charSequence is a valid unicode string | ||
| * and the number of unicode characters in the truncated charSequence is lesser than or equal to length | ||
| */ | ||
| public static CharSequence truncateString(CharSequence input, int length) { | ||
| Preconditions.checkArgument(length > 0, "Truncate length should be positive"); | ||
| StringBuffer sb = new StringBuffer(input); | ||
| // Get the number of unicode characters in the input | ||
| int numUniCodeCharacters = sb.codePointCount(0, sb.length()); | ||
| // No need to truncate if the number of unicode characters in the char sequence is <= truncate length | ||
| if (length >= numUniCodeCharacters) { | ||
| return input; | ||
| } | ||
| // Get the offset in the input charSequence where the number of unicode characters = truncate length | ||
| int offsetByCodePoint = sb.offsetByCodePoints(0, length); | ||
| return input.subSequence(0, offsetByCodePoint); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a valid unicode charsequence that is lower than the given input such that the | ||
| * number of unicode characters in the truncated charSequence is lesser than or equal to length | ||
| */ | ||
| public static Literal<CharSequence> truncateStringMin(Literal<CharSequence> input, int length) { | ||
| // Truncate the input to the specified truncate length. | ||
| CharSequence truncatedInput = truncateString(input.value(), length); | ||
| return Literal.of(truncatedInput); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a valid unicode charsequence that is greater than the given input such that the | ||
| * number of unicode characters in the truncated charSequence is lesser than or equal to length | ||
| */ | ||
| public static Literal<CharSequence> truncateStringMax(Literal<CharSequence> input, int length) { | ||
| CharSequence inputCharSeq = input.value(); | ||
| // Truncate the input to the specified truncate length. | ||
| StringBuffer truncatedStringBuffer = new StringBuffer(truncateString(inputCharSeq, length)); | ||
|
|
||
| // No need to increment if the input length is under the truncate length | ||
| if (inputCharSeq.length() == truncatedStringBuffer.length()) { | ||
| return input; | ||
| } | ||
|
|
||
| // Try incrementing the code points from the end | ||
| for (int i = length - 1; i >= 0; i--) { | ||
| int nextCodePoint = truncatedStringBuffer.codePointAt(i) + 1; | ||
| // No overflow | ||
| if (nextCodePoint != 0 && Character.isValidCodePoint(nextCodePoint)) { | ||
| // Get the offset in the truncated string buffer where the number of unicode characters = i | ||
| int offsetByCodePoint = truncatedStringBuffer.offsetByCodePoints(0, i); | ||
| truncatedStringBuffer.setLength(offsetByCodePoint); | ||
| // Append next code point to the truncated substring | ||
| truncatedStringBuffer.appendCodePoint(nextCodePoint); | ||
| return Literal.of(truncatedStringBuffer.toString()); | ||
| } | ||
| } | ||
| return null; // Cannot find a valid upper bound | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| import java.util.Set; | ||
| import org.apache.iceberg.Metrics; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.TableProperties; | ||
| import org.apache.iceberg.exceptions.RuntimeIOException; | ||
| import org.apache.iceberg.expressions.Literal; | ||
| import org.apache.iceberg.io.InputFile; | ||
|
|
@@ -47,21 +48,33 @@ | |
| import org.apache.parquet.schema.MessageType; | ||
|
|
||
| import static org.apache.iceberg.parquet.ParquetConversions.fromParquetPrimitive; | ||
| import static org.apache.iceberg.util.BinaryUtil.truncateBinaryMax; | ||
| import static org.apache.iceberg.util.BinaryUtil.truncateBinaryMin; | ||
| import static org.apache.iceberg.util.UnicodeUtil.truncateStringMax; | ||
| import static org.apache.iceberg.util.UnicodeUtil.truncateStringMin; | ||
|
|
||
| public class ParquetUtil { | ||
| // not meant to be instantiated | ||
| private ParquetUtil() { | ||
| } | ||
|
|
||
| public static Metrics fileMetrics(InputFile file) { | ||
| return fileMetrics(file, TableProperties.WRITE_METADATA_TRUNCATE_BYTES_DEFAULT); | ||
|
rdblue marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public static Metrics fileMetrics(InputFile file, int statsTruncateLength) { | ||
| try (ParquetFileReader reader = ParquetFileReader.open(ParquetIO.file(file))) { | ||
| return footerMetrics(reader.getFooter()); | ||
| return footerMetrics(reader.getFooter(), statsTruncateLength); | ||
| } catch (IOException e) { | ||
| throw new RuntimeIOException(e, "Failed to read footer of file: %s", file); | ||
| } | ||
| } | ||
|
|
||
| public static Metrics footerMetrics(ParquetMetadata metadata) { | ||
| return footerMetrics(metadata, TableProperties.WRITE_METADATA_TRUNCATE_BYTES_DEFAULT); | ||
|
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 there is a need for this to be left. The truncate length should always be included when getting metrics. The version that uses the default is called from two places:
I think both should be updated. The write adapter should use the config setting, and the Spark util method can default this inline. Then we can get rid of this method.
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. Yes, would be good to get rid of this. I updated the PR with these changes. |
||
| } | ||
|
|
||
| public static Metrics footerMetrics(ParquetMetadata metadata, int statsTruncateLength) { | ||
| long rowCount = 0; | ||
| Map<Integer, Long> columnSizes = Maps.newHashMap(); | ||
| Map<Integer, Long> valueCounts = Maps.newHashMap(); | ||
|
|
@@ -89,11 +102,14 @@ public static Metrics footerMetrics(ParquetMetadata metadata) { | |
| increment(nullValueCounts, fieldId, stats.getNumNulls()); | ||
|
|
||
| Types.NestedField field = fileSchema.findField(fieldId); | ||
| if (field != null && stats.hasNonNullValue() && shouldStoreBounds(path, fileSchema)) { | ||
| updateMin(lowerBounds, fieldId, | ||
| fromParquetPrimitive(field.type(), column.getPrimitiveType(), stats.genericGetMin())); | ||
| updateMax(upperBounds, fieldId, | ||
| fromParquetPrimitive(field.type(), column.getPrimitiveType(), stats.genericGetMax())); | ||
| if (field != null && stats.hasNonNullValue() && shouldStoreBounds(path, fileSchema) | ||
| && statsTruncateLength > 0) { | ||
| updateMin(lowerBounds, fieldId, field.type(), | ||
| fromParquetPrimitive(field.type(), column.getPrimitiveType(), | ||
| stats.genericGetMin()), statsTruncateLength); | ||
| updateMax(upperBounds, fieldId, field.type(), | ||
| fromParquetPrimitive(field.type(), column.getPrimitiveType(), | ||
| stats.genericGetMax()), statsTruncateLength); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -151,18 +167,40 @@ private static void increment(Map<Integer, Long> columns, int fieldId, long amou | |
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static <T> void updateMin(Map<Integer, Literal<?>> lowerBounds, int id, Literal<T> min) { | ||
| private static <T> void updateMin(Map<Integer, Literal<?>> lowerBounds, int id, Type type, | ||
| Literal<T> min, int truncateLength) { | ||
| Literal<T> currentMin = (Literal<T>) lowerBounds.get(id); | ||
| if (currentMin == null || min.comparator().compare(min.value(), currentMin.value()) < 0) { | ||
| lowerBounds.put(id, min); | ||
| switch (type.typeId()) { | ||
| case STRING: | ||
| lowerBounds.put(id, truncateStringMin((Literal<CharSequence>) min, truncateLength)); | ||
| break; | ||
| case FIXED: | ||
| case BINARY: | ||
| lowerBounds.put(id, truncateBinaryMin((Literal<ByteBuffer>) min, truncateLength)); | ||
| break; | ||
| default: | ||
| lowerBounds.put(id, min); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static <T> void updateMax(Map<Integer, Literal<?>> upperBounds, int id, Literal<T> max) { | ||
| private static <T> void updateMax(Map<Integer, Literal<?>> upperBounds, int id, Type type, | ||
| Literal<T> max, int truncateLength) { | ||
| Literal<T> currentMax = (Literal<T>) upperBounds.get(id); | ||
| if (currentMax == null || max.comparator().compare(max.value(), currentMax.value()) > 0) { | ||
| upperBounds.put(id, max); | ||
| switch (type.typeId()) { | ||
| case STRING: | ||
| upperBounds.put(id, truncateStringMax((Literal<CharSequence>) max, truncateLength)); | ||
| break; | ||
| case FIXED: | ||
| case BINARY: | ||
| upperBounds.put(id, truncateBinaryMax((Literal<ByteBuffer>) max, truncateLength)); | ||
| break; | ||
| default: | ||
| upperBounds.put(id, max); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.