-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-3396: [WIP] [Java] VectorSchemaRoot.create(schema, allocator) does not create the expected vector type for dictionary-encoded fields #2681
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
Closed
Closed
Changes from all commits
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
132 changes: 132 additions & 0 deletions
132
java/vector/src/test/java/org/apache/arrow/vector/TestDictionaryEncodedVector.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,132 @@ | ||
| /** | ||
| * 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.arrow.vector; | ||
|
|
||
| import static java.nio.channels.Channels.newChannel; | ||
| import static java.util.Arrays.asList; | ||
| import static org.apache.arrow.vector.TestUtils.newVarCharVector; | ||
|
|
||
| import java.io.*; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.Vector; | ||
|
|
||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.dictionary.Dictionary; | ||
| import org.apache.arrow.vector.dictionary.DictionaryProvider; | ||
| import org.apache.arrow.vector.ipc.ArrowStreamReader; | ||
| import org.apache.arrow.vector.ipc.ArrowStreamWriter; | ||
| import org.apache.arrow.vector.types.pojo.*; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class TestDictionaryEncodedVector { | ||
|
|
||
| private BufferAllocator allocator; | ||
|
|
||
| @Before | ||
| public void init() { | ||
| allocator = new RootAllocator(Long.MAX_VALUE); | ||
| } | ||
|
|
||
| @After | ||
| public void terminate() throws Exception { | ||
| allocator.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDicionaryEncodedVector() throws IOException { | ||
|
|
||
| // Create the dictionary | ||
| VarCharVector dictVector = (VarCharVector) | ||
| FieldType.nullable(new ArrowType.Utf8()) | ||
| .createNewSingleVector("enum1_dict", allocator, null); | ||
| dictVector.allocateNew(); | ||
| dictVector.set(0, "foo".getBytes(StandardCharsets.UTF_8)); | ||
| dictVector.set(1, "bar".getBytes(StandardCharsets.UTF_8)); | ||
| dictVector.setValueCount(2); | ||
|
|
||
| DictionaryEncoding enum1Encoding = new DictionaryEncoding( | ||
| 0, | ||
| true, | ||
| new ArrowType.Int(8, true) | ||
| ); | ||
|
|
||
| DictionaryProvider dictionaryProvider = new DictionaryProvider.MapDictionaryProvider( | ||
| new Dictionary(dictVector, enum1Encoding) | ||
| ); | ||
|
|
||
| // Create the dictionary encoded vector | ||
| Schema schema = new Schema( | ||
| asList( | ||
| new Field( | ||
| "enum1", | ||
| true, | ||
| // this is the type of the decoded value | ||
| new ArrowType.Utf8(), | ||
| enum1Encoding, | ||
| Collections.<Field>emptyList()) | ||
| )); | ||
|
|
||
| // This doesn't not work without the patch | ||
| // root.getVector("enum1") returns a VarCharVector instead of a TinyIntVector | ||
| VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); | ||
| TinyIntVector vector = (TinyIntVector) root.getVector("enum1"); | ||
| vector.allocateNew(); | ||
|
|
||
| // This is the encoded vector | ||
| // TinyIntVector vector = new TinyIntVector("enum1", allocator); | ||
| // vector.allocateNew(); | ||
| vector.set(0, 1); | ||
| vector.set(1, 0); | ||
| vector.set(2, 1); | ||
| vector.setValueCount(3); | ||
| root.setRowCount(3); | ||
|
|
||
| // Test round trip | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| // FileOutputStream out = new FileOutputStream("/tmp/dictionary_encoded.arrow"); | ||
| ArrowStreamWriter writer = new ArrowStreamWriter(root, dictionaryProvider, newChannel(out)); | ||
|
|
||
| writer.start(); | ||
| writer.writeBatch(); | ||
| writer.close(); | ||
|
|
||
| dictVector.close(); | ||
| root.close(); | ||
|
|
||
| byte[] data = out.toByteArray(); | ||
| out.close(); | ||
|
|
||
| // Read | ||
| ByteArrayInputStream in = new ByteArrayInputStream(data); | ||
|
|
||
| // FileInputStream in = new FileInputStream("/tmp/dictionary_encoded.arrow"); | ||
| ArrowStreamReader reader = new ArrowStreamReader(in, allocator); | ||
| root = reader.getVectorSchemaRoot(); | ||
| reader.loadNextBatch(); | ||
| System.out.println(root.contentToTSVString()); | ||
|
|
||
| reader.close(); | ||
| root.close(); | ||
| } | ||
| } | ||
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.
This should be a style problem.
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.
Agreed, I wonder if this PR is old enough that we didn't have checkstyle enforcement?
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.
@emkornfield , I have prepared a doc for the dictionary related use cases.
This PR solves problem 2 in the doc (misleading constructor), and it is a relatively small problem and involves small changes.
Can we first merge this PR and close this issue? It looks to me except some style problems, and some comments need to be removed.