-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PARQUET-1214: Column indexes: Truncate min/max values #481
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
6 commits
Select commit
Hold shift + click to select a range
67237dc
PARQUET-1214: Column indexes: Truncate min/max values
3bf0ce9
PARQUET-1214: Introduce property for truncate length
gszadovszky a8630a4
PARQUET-1214: Fix rebase conflicts
gszadovszky 347d808
PARQUET-1214: Updates according to review comments
gszadovszky 1cf1451
PARQUET-1214: Review comments vol.2
gszadovszky 854f57b
PARQUET-1214: Remove degug logging
gszadovszky 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
208 changes: 208 additions & 0 deletions
208
...-column/src/main/java/org/apache/parquet/internal/column/columnindex/BinaryTruncator.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,208 @@ | ||
| /* | ||
| * 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.parquet.internal.column.columnindex; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.nio.CharBuffer; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.CharsetDecoder; | ||
| import java.nio.charset.CoderResult; | ||
| import java.nio.charset.CodingErrorAction; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import org.apache.parquet.io.api.Binary; | ||
| import org.apache.parquet.schema.OriginalType; | ||
| import org.apache.parquet.schema.PrimitiveType; | ||
|
|
||
| /** | ||
| * Class for truncating min/max values for binary types. | ||
| */ | ||
| abstract class BinaryTruncator { | ||
| enum Validity { | ||
| VALID, MALFORMED, UNMAPPABLE; | ||
| } | ||
|
|
||
| private static class CharsetValidator { | ||
| private final CharBuffer dummyBuffer = CharBuffer.allocate(1024); | ||
| private final CharsetDecoder decoder; | ||
|
|
||
| CharsetValidator(Charset charset) { | ||
| decoder = charset.newDecoder(); | ||
| decoder.onMalformedInput(CodingErrorAction.REPORT); | ||
| decoder.onUnmappableCharacter(CodingErrorAction.REPORT); | ||
| } | ||
|
|
||
| Validity checkValidity(ByteBuffer buffer) { | ||
| int pos = buffer.position(); | ||
| CoderResult result = CoderResult.OVERFLOW; | ||
| while (result.isOverflow()) { | ||
| dummyBuffer.clear(); | ||
| result = decoder.decode(buffer, dummyBuffer, true); | ||
| } | ||
| buffer.position(pos); | ||
| if (result.isUnderflow()) { | ||
| return Validity.VALID; | ||
| } else if (result.isMalformed()) { | ||
| return Validity.MALFORMED; | ||
| } else { | ||
| return Validity.UNMAPPABLE; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static final BinaryTruncator NO_OP_TRUNCATOR = new BinaryTruncator() { | ||
| @Override | ||
| Binary truncateMin(Binary minValue, int length) { | ||
| return minValue; | ||
| } | ||
|
|
||
| @Override | ||
| Binary truncateMax(Binary maxValue, int length) { | ||
| return maxValue; | ||
| } | ||
| }; | ||
|
|
||
| private static final BinaryTruncator DEFAULT_UTF8_TRUNCATOR = new BinaryTruncator() { | ||
| private final CharsetValidator validator = new CharsetValidator(StandardCharsets.UTF_8); | ||
|
|
||
| @Override | ||
| Binary truncateMin(Binary minValue, int length) { | ||
| if (minValue.length() <= length) { | ||
| return minValue; | ||
| } | ||
| ByteBuffer buffer = minValue.toByteBuffer(); | ||
| byte[] array; | ||
| if (validator.checkValidity(buffer) == Validity.VALID) { | ||
| array = truncateUtf8(buffer, length); | ||
| } else { | ||
| array = truncate(buffer, length); | ||
| } | ||
| return array == null ? minValue : Binary.fromConstantByteArray(array); | ||
| } | ||
|
|
||
| @Override | ||
| Binary truncateMax(Binary maxValue, int length) { | ||
| if (maxValue.length() <= length) { | ||
| return maxValue; | ||
| } | ||
| byte[] array; | ||
| ByteBuffer buffer = maxValue.toByteBuffer(); | ||
| if (validator.checkValidity(buffer) == Validity.VALID) { | ||
| array = incrementUtf8(truncateUtf8(buffer, length)); | ||
| } else { | ||
| array = increment(truncate(buffer, length)); | ||
| } | ||
| return array == null ? maxValue : Binary.fromConstantByteArray(array); | ||
| } | ||
|
|
||
| // Simply truncate to length | ||
| private byte[] truncate(ByteBuffer buffer, int length) { | ||
| assert length < buffer.remaining(); | ||
| byte[] array = new byte[length]; | ||
| buffer.get(array); | ||
| return array; | ||
| } | ||
|
|
||
| // Trying to increment the bytes from the last one to the beginning | ||
| private byte[] increment(byte[] array) { | ||
| for (int i = array.length - 1; i >= 0; --i) { | ||
| byte elem = array[i]; | ||
| ++elem; | ||
| array[i] = elem; | ||
| if (elem != 0) { // Did not overflow: 0xFF -> 0x00 | ||
| return array; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| // Truncates the buffer to length or less so the remaining bytes form a valid UTF-8 string | ||
| private byte[] truncateUtf8(ByteBuffer buffer, int length) { | ||
| assert length < buffer.remaining(); | ||
| ByteBuffer newBuffer = buffer.slice(); | ||
| newBuffer.limit(newBuffer.position() + length); | ||
| while (validator.checkValidity(newBuffer) != Validity.VALID) { | ||
| newBuffer.limit(newBuffer.limit() - 1); | ||
| if (newBuffer.remaining() == 0) { | ||
| return null; | ||
| } | ||
| } | ||
| byte[] array = new byte[newBuffer.remaining()]; | ||
| newBuffer.get(array); | ||
| return array; | ||
| } | ||
|
|
||
| // Trying to increment the bytes from the last one to the beginning until the bytes form a valid UTF-8 string | ||
| private byte[] incrementUtf8(byte[] array) { | ||
| if (array == null) { | ||
| return null; | ||
| } | ||
| ByteBuffer buffer = ByteBuffer.wrap(array); | ||
| for (int i = array.length - 1; i >= 0; --i) { | ||
| byte prev = array[i]; | ||
| byte inc = prev; | ||
| while (++inc != 0) { // Until overflow: 0xFF -> 0x00 | ||
| array[i] = inc; | ||
|
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. This one seems to increment bytes up to FF, but then not write the overflow back to the array. I.e., if I understand correctly, the array may go through the following changes: 42 FB I think the last one should be 43 00 |
||
| switch (validator.checkValidity(buffer)) { | ||
| case VALID: | ||
| return array; | ||
| case UNMAPPABLE: | ||
| continue; // Increment the i byte once more | ||
| case MALFORMED: | ||
| break; // Stop incrementing the i byte; go to the i-1 | ||
| } | ||
| break; // MALFORMED | ||
| } | ||
| array[i] = prev; | ||
| } | ||
| return null; // All characters are the largest possible; unable to increment | ||
| } | ||
| }; | ||
|
|
||
| static BinaryTruncator getTruncator(PrimitiveType type) { | ||
| if (type == null) { | ||
| return NO_OP_TRUNCATOR; | ||
| } | ||
| switch (type.getPrimitiveTypeName()) { | ||
| case INT96: | ||
| return NO_OP_TRUNCATOR; | ||
| case BINARY: | ||
| case FIXED_LEN_BYTE_ARRAY: | ||
| OriginalType originalType = type.getOriginalType(); | ||
| if (originalType == null) { | ||
| return DEFAULT_UTF8_TRUNCATOR; | ||
| } | ||
| switch (originalType) { | ||
| case UTF8: | ||
| case ENUM: | ||
| case JSON: | ||
| case BSON: | ||
| return DEFAULT_UTF8_TRUNCATOR; | ||
| default: | ||
| return NO_OP_TRUNCATOR; | ||
| } | ||
| default: | ||
| throw new IllegalArgumentException("No truncator is available for the type: " + type); | ||
| } | ||
| } | ||
|
|
||
| abstract Binary truncateMin(Binary minValue, int length); | ||
|
|
||
| abstract Binary truncateMax(Binary maxValue, int length); | ||
| } | ||
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 seems to me that this only increments bytes if they do not overflow. I think they should be incremented even if they overflow.
An example for the difference between the two approaches:
Even though 00 43 FF FF is a valid max value as well, 00 43 00 00 is closer to the original value thus it results in better filtering.