-
Notifications
You must be signed in to change notification settings - Fork 35
Fix ORC schema visitors to support reading ORC files with deeply nest… #81
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
rzhang10
merged 2 commits into
linkedin:li-0.11.x
from
rzhang10:fix_deeply_nested_union_orc_reader
Sep 7, 2021
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.apache.iceberg.io.CloseableIterable; | ||
| import org.apache.iceberg.orc.ORC; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Iterators; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.spark.data.vectorized.VectorizedSparkOrcReaders; | ||
| import org.apache.iceberg.types.Types; | ||
|
|
@@ -39,6 +40,7 @@ | |
| import org.apache.orc.Writer; | ||
| import org.apache.orc.storage.ql.exec.vector.BytesColumnVector; | ||
| import org.apache.orc.storage.ql.exec.vector.LongColumnVector; | ||
| import org.apache.orc.storage.ql.exec.vector.StructColumnVector; | ||
| import org.apache.orc.storage.ql.exec.vector.UnionColumnVector; | ||
| import org.apache.orc.storage.ql.exec.vector.VectorizedRowBatch; | ||
| import org.apache.spark.sql.catalyst.InternalRow; | ||
|
|
@@ -215,4 +217,100 @@ public void testSingleComponentUnion() throws IOException { | |
| assertEquals(expectedSchema, expectedSecondRow, rowIterator.next()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeeplyNestedUnion() throws IOException { | ||
| TypeDescription orcSchema = | ||
| TypeDescription.fromString("struct<c1:uniontype<int,struct<c2:string,c3:uniontype<int,string>>>>"); | ||
|
|
||
| Schema expectedSchema = new Schema( | ||
| Types.NestedField.optional(0, "c1", Types.StructType.of( | ||
| Types.NestedField.optional(1, "tag_0", Types.IntegerType.get()), | ||
| Types.NestedField.optional(2, "tag_1", | ||
| Types.StructType.of(Types.NestedField.optional(3, "c2", Types.StringType.get()), | ||
| Types.NestedField.optional(4, "c3", Types.StructType.of( | ||
| Types.NestedField.optional(5, "tag_0", Types.IntegerType.get()), | ||
| Types.NestedField.optional(6, "tag_1", Types.StringType.get())))))))); | ||
|
|
||
| final InternalRow expectedFirstRow = new GenericInternalRow(1); | ||
| final InternalRow inner1 = new GenericInternalRow(2); | ||
| inner1.update(0, null); | ||
| final InternalRow inner2 = new GenericInternalRow(2); | ||
| inner2.update(0, UTF8String.fromString("foo0")); | ||
| final InternalRow inner3 = new GenericInternalRow(2); | ||
| inner3.update(0, 0); | ||
| inner3.update(1, null); | ||
| inner2.update(1, inner3); | ||
| inner1.update(1, inner2); | ||
| expectedFirstRow.update(0, inner1); | ||
|
|
||
| Configuration conf = new Configuration(); | ||
|
|
||
| File orcFile = temp.newFile(); | ||
| Path orcFilePath = new Path(orcFile.getPath()); | ||
|
|
||
| Writer writer = OrcFile.createWriter(orcFilePath, | ||
| OrcFile.writerOptions(conf) | ||
| .setSchema(orcSchema).overwrite(true)); | ||
|
|
||
| VectorizedRowBatch batch = orcSchema.createRowBatch(); | ||
| UnionColumnVector innerUnion1 = (UnionColumnVector) batch.cols[0]; | ||
| LongColumnVector innerInt1 = (LongColumnVector) innerUnion1.fields[0]; | ||
| innerInt1.fillWithNulls(); | ||
| StructColumnVector innerStruct2 = (StructColumnVector) innerUnion1.fields[1]; | ||
| BytesColumnVector innerString2 = (BytesColumnVector) innerStruct2.fields[0]; | ||
| UnionColumnVector innerUnion3 = (UnionColumnVector) innerStruct2.fields[1]; | ||
| LongColumnVector innerInt3 = (LongColumnVector) innerUnion3.fields[0]; | ||
| BytesColumnVector innerString3 = (BytesColumnVector) innerUnion3.fields[1]; | ||
| innerString3.fillWithNulls(); | ||
|
|
||
| for (int r = 0; r < NUM_OF_ROWS; ++r) { | ||
| int row = batch.size++; | ||
| innerUnion1.tags[row] = 1; | ||
| innerString2.setVal(row, ("foo" + row).getBytes(StandardCharsets.UTF_8)); | ||
| innerUnion3.tags[row] = 0; | ||
| innerInt3.vector[row] = r; | ||
| // If the batch is full, write it out and start over. | ||
| if (batch.size == batch.getMaxSize()) { | ||
| writer.addRowBatch(batch); | ||
| batch.reset(); | ||
| innerInt1.fillWithNulls(); | ||
| innerString3.fillWithNulls(); | ||
| } | ||
| } | ||
| if (batch.size != 0) { | ||
| writer.addRowBatch(batch); | ||
| batch.reset(); | ||
| } | ||
| writer.close(); | ||
|
|
||
|
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. Could you also add a test for
Member
Author
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. Added |
||
| // test non-vectorized reader | ||
| List<InternalRow> results = Lists.newArrayList(); | ||
| try (CloseableIterable<InternalRow> reader = ORC.read(Files.localInput(orcFile)) | ||
| .project(expectedSchema) | ||
| .createReaderFunc(readOrcSchema -> new SparkOrcReader(expectedSchema, readOrcSchema)) | ||
| .build()) { | ||
| reader.forEach(results::add); | ||
| final InternalRow actualFirstRow = results.get(0); | ||
|
|
||
| Assert.assertEquals(results.size(), NUM_OF_ROWS); | ||
| assertEquals(expectedSchema, expectedFirstRow, actualFirstRow); | ||
| } | ||
|
|
||
| // test vectorized reader | ||
| try (CloseableIterable<ColumnarBatch> reader = ORC.read(Files.localInput(orcFile)) | ||
| .project(expectedSchema) | ||
| .createBatchedReaderFunc(readOrcSchema -> | ||
| VectorizedSparkOrcReaders.buildReader(expectedSchema, readOrcSchema, ImmutableMap.of())) | ||
| .build()) { | ||
| final Iterator<InternalRow> actualRows = batchesToRows(reader.iterator()); | ||
| final InternalRow actualFirstRow = actualRows.next(); | ||
|
|
||
| assertEquals(expectedSchema, expectedFirstRow, actualFirstRow); | ||
| } | ||
| } | ||
|
|
||
| private Iterator<InternalRow> batchesToRows(Iterator<ColumnarBatch> batches) { | ||
| return Iterators.concat(Iterators.transform(batches, ColumnarBatch::rowIterator)); | ||
| } | ||
| } | ||
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.
Could you give an example how this is used?
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's used by the
ApplyNameMappingvisitor to correctly recognize the union converted struct field names, for example sth likec1.tag_1.c2and assign the correct ID to such field.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 didn't check the ApplyNameMapping visitor, but can you verify that using same ordinal across unions - i.e., (1, "tag_0") and again (5, "tag_0") as in your test case - could lead to any issues?
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 won't, the (1, tag_0) and (5, tag_0) are two fields at different nested levels inside the schema so there's no ambiguity.