-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
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
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,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 | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
api/src/main/java/org/apache/iceberg/util/UnicodeUtil.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,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 | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.