-
Notifications
You must be signed in to change notification settings - Fork 4.2k
GH-14909: [Java] Prevent potential memory leak of ListSubfieldEncoder and StructSubfieldEncoder #14910
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
GH-14909: [Java] Prevent potential memory leak of ListSubfieldEncoder and StructSubfieldEncoder #14910
Changes from 1 commit
Commits
Show all changes
2 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
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 |
|---|---|---|
|
|
@@ -916,6 +916,132 @@ public void testNoMemoryLeak() { | |
| assertEquals("decode memory leak", 0, allocator.getAllocatedMemory()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testListNoMemoryLeak() { | ||
| // Create a new value vector | ||
| try (final ListVector vector = ListVector.empty("vector", allocator); | ||
| final ListVector dictionaryVector = ListVector.empty("dict", allocator)) { | ||
|
|
||
| UnionListWriter writer = vector.getWriter(); | ||
| writer.allocate(); | ||
| writeListVector(writer, new int[]{10, 20}); | ||
| writer.setValueCount(1); | ||
|
|
||
| UnionListWriter dictWriter = dictionaryVector.getWriter(); | ||
| dictWriter.allocate(); | ||
| writeListVector(dictWriter, new int[]{10}); | ||
| dictionaryVector.setValueCount(1); | ||
|
|
||
| Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); | ||
| ListSubfieldEncoder encoder = new ListSubfieldEncoder(dictionary, allocator); | ||
|
|
||
| try (final ListVector encoded = (ListVector) encoder.encodeListSubField(vector)) { | ||
| fail("There should be an exception when encoding"); | ||
| } catch (Exception e) { | ||
| assertEquals("Dictionary encoding not defined for value:" + 20, e.getMessage()); | ||
|
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. nit: why concat here, when the string is fully hardcoded below? |
||
| } | ||
| } | ||
| assertEquals("list encode memory leak", 0, allocator.getAllocatedMemory()); | ||
|
|
||
| try (final ListVector indices = ListVector.empty("indices", allocator); | ||
| final ListVector dictionaryVector = ListVector.empty("dict", allocator)) { | ||
|
|
||
| UnionListWriter writer = indices.getWriter(); | ||
| writer.allocate(); | ||
| writeListVector(writer, new int[]{3}); | ||
| writer.setValueCount(1); | ||
|
|
||
| UnionListWriter dictWriter = dictionaryVector.getWriter(); | ||
| dictWriter.allocate(); | ||
| writeListVector(dictWriter, new int[]{10, 20}); | ||
| dictionaryVector.setValueCount(1); | ||
|
|
||
| Dictionary dictionary = | ||
| new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); | ||
|
|
||
| try (final ValueVector decoded = ListSubfieldEncoder.decodeListSubField(indices, dictionary, allocator)) { | ||
| fail("There should be an exception when decoding"); | ||
| } catch (Exception e) { | ||
| assertEquals("Provided dictionary does not contain value for index 3", e.getMessage()); | ||
| } | ||
| } | ||
| assertEquals("list decode memory leak", 0, allocator.getAllocatedMemory()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStructNoMemoryLeak() { | ||
| try (final StructVector vector = StructVector.empty("vector", allocator); | ||
| final VarCharVector dictVector1 = new VarCharVector("f0", allocator); | ||
| final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { | ||
|
|
||
| vector.addOrGet("f0", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); | ||
| vector.addOrGet("f1", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); | ||
|
|
||
| NullableStructWriter writer = vector.getWriter(); | ||
| writer.allocate(); | ||
| writeStructVector(writer, "aa", "baz"); | ||
| writer.setValueCount(1); | ||
|
|
||
| DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); | ||
| setVector(dictVector1, | ||
| "aa".getBytes(StandardCharsets.UTF_8)); | ||
| setVector(dictVector2, | ||
| "foo".getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| provider.put(new Dictionary(dictVector1, new DictionaryEncoding(1L, false, null))); | ||
| provider.put(new Dictionary(dictVector2, new DictionaryEncoding(2L, false, null))); | ||
|
|
||
| StructSubfieldEncoder encoder = new StructSubfieldEncoder(allocator, provider); | ||
| Map<Integer, Long> columnToDictionaryId = new HashMap<>(); | ||
| columnToDictionaryId.put(0, 1L); | ||
| columnToDictionaryId.put(1, 2L); | ||
|
|
||
| try (final StructVector encoded = (StructVector) encoder.encode(vector, columnToDictionaryId)) { | ||
| fail("There should be an exception when encoding"); | ||
| } catch (Exception e) { | ||
| assertEquals("Dictionary encoding not defined for value:" + "baz", e.getMessage()); | ||
| } | ||
| } | ||
| assertEquals("struct encode memory leak", 0, allocator.getAllocatedMemory()); | ||
|
|
||
| try (final StructVector indices = StructVector.empty("indices", allocator); | ||
| final VarCharVector dictVector1 = new VarCharVector("f0", allocator); | ||
| final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { | ||
|
|
||
| DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); | ||
| setVector(dictVector1, | ||
| "aa".getBytes(StandardCharsets.UTF_8)); | ||
| setVector(dictVector2, | ||
| "foo".getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| provider.put(new Dictionary(dictVector1, new DictionaryEncoding(1L, false, null))); | ||
| provider.put(new Dictionary(dictVector2, new DictionaryEncoding(2L, false, null))); | ||
|
|
||
| ArrowType int32 = new ArrowType.Int(32, true); | ||
| indices.addOrGet("f0", | ||
| new FieldType(true, int32, provider.lookup(1L).getEncoding()), | ||
| IntVector.class); | ||
| indices.addOrGet("f1", | ||
| new FieldType(true, int32, provider.lookup(2L).getEncoding()), | ||
| IntVector.class); | ||
|
|
||
| NullableStructWriter writer = indices.getWriter(); | ||
| writer.allocate(); | ||
| writer.start(); | ||
| writer.integer("f0").writeInt(1); | ||
| writer.integer("f1").writeInt(3); | ||
| writer.end(); | ||
| writer.setValueCount(1); | ||
|
|
||
| try (final StructVector decode = StructSubfieldEncoder.decode(indices, provider, allocator)) { | ||
| fail("There should be an exception when decoding"); | ||
| } catch (Exception e) { | ||
| assertEquals("Provided dictionary does not contain value for index 3", e.getMessage()); | ||
| } | ||
| } | ||
| assertEquals("struct decode memory leak", 0, allocator.getAllocatedMemory()); | ||
| } | ||
|
|
||
| private void testDictionary(Dictionary dictionary, ToIntBiFunction<ValueVector, Integer> valGetter) { | ||
| try (VarCharVector vector = new VarCharVector("vector", allocator)) { | ||
| setVector(vector, "1", "3", "5", "7", "9"); | ||
|
|
||
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 use
AutoCloseables#close(Throwable, AutoCloseable...)for this kind of thing?