-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Write null byte when indexing numeric dimensions with Hadoop #7020
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
gianm
merged 4 commits into
apache:master
from
ferristseng:feature-hadoop-index-numeric-dim
Mar 12, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ | |
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
|
|
||
| /** | ||
| */ | ||
|
|
@@ -63,6 +64,34 @@ public class InputRowSerde | |
| private static final IndexSerdeTypeHelper FLOAT_HELPER = new FloatIndexSerdeTypeHelper(); | ||
| private static final IndexSerdeTypeHelper DOUBLE_HELPER = new DoubleIndexSerdeTypeHelper(); | ||
|
|
||
| private static <T extends Number> void writeNullableNumeric( | ||
| T ret, | ||
| final ByteArrayDataOutput out, | ||
| final Supplier<T> getDefault, | ||
| final Consumer<T> write) | ||
| { | ||
| if (ret == null) { | ||
| ret = getDefault.get(); | ||
| } | ||
|
|
||
| // Write the null byte only if the default numeric value is still null. | ||
| if (ret == null) { | ||
| out.writeByte(NullHandling.IS_NULL_BYTE); | ||
| return; | ||
| } | ||
|
|
||
| if (NullHandling.sqlCompatible()) { | ||
| out.writeByte(NullHandling.IS_NOT_NULL_BYTE); | ||
| } | ||
|
|
||
| write.accept(ret); | ||
| } | ||
|
|
||
| private static boolean isNullByteSet(final ByteArrayDataInput in) | ||
| { | ||
| return NullHandling.sqlCompatible() && in.readByte() == NullHandling.IS_NULL_BYTE; | ||
| } | ||
|
|
||
| public interface IndexSerdeTypeHelper<T> | ||
| { | ||
| ValueType getType(); | ||
|
|
@@ -175,22 +204,18 @@ public void serialize(ByteArrayDataOutput out, Object value) | |
| exceptionToThrow = pe; | ||
| } | ||
|
|
||
| if (ret == null) { | ||
| // remove null -> zero conversion when https://github.com/apache/incubator-druid/pull/5278 series of patches is merged | ||
| // we'll also need to change the serialized encoding so that it can represent numeric nulls | ||
| ret = DimensionHandlerUtils.ZERO_LONG; | ||
| } | ||
| out.writeLong(ret); | ||
| writeNullableNumeric(ret, out, NullHandling::defaultLongValue, out::writeLong); | ||
|
|
||
| if (exceptionToThrow != null) { | ||
| throw exceptionToThrow; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Long deserialize(ByteArrayDataInput in) | ||
| { | ||
| return in.readLong(); | ||
| return isNullByteSet(in) ? null : in.readLong(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -214,22 +239,18 @@ public void serialize(ByteArrayDataOutput out, Object value) | |
| exceptionToThrow = pe; | ||
| } | ||
|
|
||
| if (ret == null) { | ||
| // remove null -> zero conversion when https://github.com/apache/incubator-druid/pull/5278 series of patches is merged | ||
| // we'll also need to change the serialized encoding so that it can represent numeric nulls | ||
| ret = DimensionHandlerUtils.ZERO_FLOAT; | ||
| } | ||
| out.writeFloat(ret); | ||
| writeNullableNumeric(ret, out, NullHandling::defaultFloatValue, out::writeFloat); | ||
|
|
||
| if (exceptionToThrow != null) { | ||
| throw exceptionToThrow; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Float deserialize(ByteArrayDataInput in) | ||
| { | ||
| return in.readFloat(); | ||
| return isNullByteSet(in) ? null : in.readFloat(); | ||
|
Member
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. return Optional.ofNullable(in)
.filter(InputRowSerde::isNotNullByteSet)
.map(ByteArrayDataInput::readFloat)
.get(); |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -253,22 +274,18 @@ public void serialize(ByteArrayDataOutput out, Object value) | |
| exceptionToThrow = pe; | ||
| } | ||
|
|
||
| if (ret == null) { | ||
| // remove null -> zero conversion when https://github.com/apache/incubator-druid/pull/5278 series of patches is merged | ||
| // we'll also need to change the serialized encoding so that it can represent numeric nulls | ||
| ret = DimensionHandlerUtils.ZERO_DOUBLE; | ||
| } | ||
| out.writeDouble(ret); | ||
| writeNullableNumeric(ret, out, NullHandling::defaultDoubleValue, out::writeDouble); | ||
|
|
||
| if (exceptionToThrow != null) { | ||
| throw exceptionToThrow; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Double deserialize(ByteArrayDataInput in) | ||
| { | ||
| return in.readDouble(); | ||
| return isNullByteSet(in) ? null : in.readDouble(); | ||
|
Member
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. return Optional.ofNullable(in)
.filter(InputRowSerde::isNotNullByteSet)
.map(ByteArrayDataInput::readDouble)
.get(); |
||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Perhaps it would be better to use a functional programming style here.
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.
Eh, I sort of prefer it the way it currently is, seems clearer to me, is there any reason it would be better other than preference?
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.
Alright, I prefer the functional style because it makes the code more readable. If we don't use
Optional, then we need add the@Nullableannotation for this method. It's up to you. 😅Uh oh!
There was an error while loading. Please reload this page.
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 think I prefer the non-functional style. Also, maybe I'm misunderstanding, but wouldn't the
get()cause the code to throw if the null byte is set?I'll add
@Nullableannotations to these deserialize methodsThere 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.
@ferristseng If the null byte is set, then
getwill return anullvalue. What you describe should be theorElseThrowfunction. Thanks for your contribution.