-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Handle null values in Range Partition dimension distribution #11973
Merged
abhishekagarwal87
merged 4 commits into
apache:master
from
kfaraz:range_partition_handle_null
Nov 24, 2021
+209
−6
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 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 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
110 changes: 110 additions & 0 deletions
110
...e/druid/indexing/common/task/batch/parallel/distribution/ArrayOfStringsNullSafeSerde.java
This file contains 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,110 @@ | ||
/* | ||
* 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.druid.indexing.common.task.batch.parallel.distribution; | ||
|
||
import org.apache.datasketches.ArrayOfItemsSerDe; | ||
import org.apache.datasketches.ArrayOfStringsSerDe; | ||
import org.apache.datasketches.Util; | ||
import org.apache.datasketches.memory.Memory; | ||
import org.apache.datasketches.memory.WritableMemory; | ||
import org.apache.druid.data.input.StringTuple; | ||
import org.apache.druid.java.util.common.IAE; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Serde for {@link StringTuple}. | ||
* <p> | ||
* The implementation is the same as {@link ArrayOfStringsSerDe}, except this | ||
* class handles null String values as well. | ||
*/ | ||
public class ArrayOfStringsNullSafeSerde extends ArrayOfItemsSerDe<String> | ||
{ | ||
|
||
private static final int NULL_STRING_LENGTH = -1; | ||
|
||
@Override | ||
public byte[] serializeToByteArray(final String[] items) | ||
{ | ||
// Determine the bytes for each String | ||
int length = 0; | ||
final byte[][] itemsBytes = new byte[items.length][]; | ||
for (int i = 0; i < items.length; i++) { | ||
length += Integer.BYTES; | ||
|
||
// Do not initialize the byte array for a null String | ||
if (items[i] != null) { | ||
itemsBytes[i] = items[i].getBytes(StandardCharsets.UTF_8); | ||
length += itemsBytes[i].length; | ||
} | ||
} | ||
|
||
// Create a single byte array for all the Strings | ||
final byte[] bytes = new byte[length]; | ||
final WritableMemory mem = WritableMemory.writableWrap(bytes); | ||
long offsetBytes = 0; | ||
for (int i = 0; i < items.length; i++) { | ||
if (itemsBytes[i] != null) { | ||
// Write the length of the array and the array itself | ||
mem.putInt(offsetBytes, itemsBytes[i].length); | ||
offsetBytes += Integer.BYTES; | ||
mem.putByteArray(offsetBytes, itemsBytes[i], 0, itemsBytes[i].length); | ||
offsetBytes += itemsBytes[i].length; | ||
} else { | ||
mem.putInt(offsetBytes, NULL_STRING_LENGTH); | ||
offsetBytes += Integer.BYTES; | ||
} | ||
} | ||
|
||
return bytes; | ||
} | ||
|
||
@Override | ||
public String[] deserializeFromMemory(final Memory mem, final int numItems) | ||
{ | ||
final String[] array = new String[numItems]; | ||
long offsetBytes = 0; | ||
for (int i = 0; i < numItems; i++) { | ||
// Read the length of the ith String | ||
Util.checkBounds(offsetBytes, Integer.BYTES, mem.getCapacity()); | ||
final int strLength = mem.getInt(offsetBytes); | ||
offsetBytes += Integer.BYTES; | ||
|
||
if (strLength >= 0) { | ||
// Read the bytes for the String | ||
final byte[] bytes = new byte[strLength]; | ||
Util.checkBounds(offsetBytes, strLength, mem.getCapacity()); | ||
mem.getByteArray(offsetBytes, bytes, 0, strLength); | ||
offsetBytes += strLength; | ||
array[i] = new String(bytes, StandardCharsets.UTF_8); | ||
} else if (strLength != NULL_STRING_LENGTH) { | ||
throw new IAE( | ||
"Illegal strLength [%s] at offset [%s]. Must be %s, 0 or a positive integer.", | ||
strLength, | ||
offsetBytes, | ||
NULL_STRING_LENGTH | ||
); | ||
} | ||
} | ||
return array; | ||
} | ||
|
||
} | ||
|
91 changes: 91 additions & 0 deletions
91
...uid/indexing/common/task/batch/parallel/distribution/ArrayOfStringsNullSafeSerdeTest.java
This file contains 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,91 @@ | ||
/* | ||
* 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.druid.indexing.common.task.batch.parallel.distribution; | ||
|
||
import org.apache.datasketches.memory.Memory; | ||
import org.apache.druid.java.util.common.IAE; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class ArrayOfStringsNullSafeSerdeTest | ||
{ | ||
|
||
private final ArrayOfStringsNullSafeSerde serde = new ArrayOfStringsNullSafeSerde(); | ||
|
||
@Test | ||
public void testStringArray() | ||
{ | ||
testSerde("abc", "def", "xyz"); | ||
testSerde("abc", "123", "456.0"); | ||
} | ||
|
||
@Test | ||
public void testSingletonArray() | ||
{ | ||
testSerde("abc"); | ||
testSerde("xyz"); | ||
} | ||
|
||
@Test | ||
public void testEmptyArray() | ||
{ | ||
testSerde(); | ||
} | ||
|
||
@Test | ||
public void testArrayWithNullString() | ||
{ | ||
testSerde((String) null); | ||
testSerde("abc", null, "def"); | ||
testSerde(null, null, null); | ||
} | ||
|
||
@Test | ||
public void testArrayWithEmptyString() | ||
kfaraz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
testSerde(""); | ||
testSerde("abc", "def", ""); | ||
testSerde("", "", ""); | ||
testSerde("", null, "abc"); | ||
} | ||
|
||
@Test | ||
public void testIllegalStrLength() | ||
{ | ||
// bytes for length = -2 | ||
final byte[] bytes = {-2, -1, -1, -1}; | ||
IAE exception = Assert.assertThrows( | ||
IAE.class, | ||
() -> serde.deserializeFromMemory(Memory.wrap(bytes), 1) | ||
); | ||
Assert.assertEquals( | ||
"Illegal strLength [-2] at offset [4]. Must be -1, 0 or a positive integer.", | ||
exception.getMessage() | ||
); | ||
} | ||
|
||
private void testSerde(String... inputArray) | ||
{ | ||
byte[] bytes = serde.serializeToByteArray(inputArray); | ||
String[] deserialized = serde.deserializeFromMemory(Memory.wrap(bytes), inputArray.length); | ||
Assert.assertEquals(inputArray, deserialized); | ||
} | ||
|
||
} |
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.
can this be selectively turned on only when more than one dimension is being used? I don't know for certain what the impact of not skipping null will be but then that impact will be limited to new range partitioning only. or it can be based on a flag that you can pass via the context. thoughts?
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 we should be fine without the flag.
The following effects would be observed on single dim:
With the addition of (multi dimension) range partitioning, single dim is inevitably being affected as it now goes through the multi dim flow itself. So this would only be another part of that overall effect.