-
Notifications
You must be signed in to change notification settings - Fork 3k
Flink: Add Orc value reader, writer implementations #1158
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
127 changes: 127 additions & 0 deletions
127
data/src/main/java/org/apache/iceberg/data/orc/BaseOrcReader.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,127 @@ | ||
| /* | ||
| * 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.iceberg.data.orc; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.orc.OrcRowReader; | ||
| import org.apache.iceberg.orc.OrcSchemaWithTypeVisitor; | ||
| import org.apache.iceberg.orc.OrcValueReader; | ||
| import org.apache.iceberg.orc.OrcValueReaders; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.orc.TypeDescription; | ||
|
|
||
| public abstract class BaseOrcReader<T> implements OrcRowReader<T> { | ||
| private final OrcValueReader<?> reader; | ||
|
|
||
| protected BaseOrcReader(org.apache.iceberg.Schema expectedSchema, | ||
| TypeDescription readOrcSchema, | ||
| Map<Integer, ?> idToConstant) { | ||
| this.reader = OrcSchemaWithTypeVisitor.visit(expectedSchema, readOrcSchema, new ReadBuilder(idToConstant)); | ||
| } | ||
|
|
||
| protected OrcValueReader<?> getReader() { | ||
| return this.reader; | ||
| } | ||
|
|
||
| protected abstract OrcValueReader<T> createStructReader(List<OrcValueReader<?>> fields, | ||
| Types.StructType expected, Map<Integer, ?> idToConstant); | ||
|
|
||
| private class ReadBuilder extends OrcSchemaWithTypeVisitor<OrcValueReader<?>> { | ||
| private final Map<Integer, ?> idToConstant; | ||
|
|
||
| private ReadBuilder(Map<Integer, ?> idToConstant) { | ||
| this.idToConstant = idToConstant; | ||
| } | ||
|
|
||
| @Override | ||
| public OrcValueReader<?> record( | ||
| Types.StructType expected, TypeDescription record, List<String> names, List<OrcValueReader<?>> fields) { | ||
| return createStructReader(fields, expected, idToConstant); | ||
| } | ||
|
|
||
| @Override | ||
| public OrcValueReader<?> list(Types.ListType iList, TypeDescription array, OrcValueReader<?> elementReader) { | ||
| return GenericOrcReaders.array(elementReader); | ||
| } | ||
|
|
||
| @Override | ||
| public OrcValueReader<?> map( | ||
| Types.MapType iMap, TypeDescription map, OrcValueReader<?> keyReader, OrcValueReader<?> valueReader) { | ||
| return GenericOrcReaders.map(keyReader, valueReader); | ||
| } | ||
|
|
||
| @Override | ||
| public OrcValueReader<?> primitive(Type.PrimitiveType iPrimitive, TypeDescription primitive) { | ||
| switch (primitive.getCategory()) { | ||
| case BOOLEAN: | ||
| return OrcValueReaders.booleans(); | ||
| case BYTE: | ||
| // Iceberg does not have a byte type. Use int | ||
| case SHORT: | ||
| // Iceberg does not have a short type. Use int | ||
| case INT: | ||
| return OrcValueReaders.ints(); | ||
| case LONG: | ||
| switch (iPrimitive.typeId()) { | ||
| case TIME: | ||
| return GenericOrcReaders.times(); | ||
| case LONG: | ||
| return OrcValueReaders.longs(); | ||
| default: | ||
| throw new IllegalStateException( | ||
| String.format("Invalid iceberg type %s corresponding to ORC type %s", iPrimitive, primitive)); | ||
| } | ||
|
|
||
| case FLOAT: | ||
| return OrcValueReaders.floats(); | ||
| case DOUBLE: | ||
| return OrcValueReaders.doubles(); | ||
| case DATE: | ||
| return GenericOrcReaders.dates(); | ||
| case TIMESTAMP: | ||
| return GenericOrcReaders.timestamps(); | ||
| case TIMESTAMP_INSTANT: | ||
| return GenericOrcReaders.timestampTzs(); | ||
| case DECIMAL: | ||
| return GenericOrcReaders.decimals(); | ||
| case CHAR: | ||
| case VARCHAR: | ||
| case STRING: | ||
| return GenericOrcReaders.strings(); | ||
| case BINARY: | ||
| switch (iPrimitive.typeId()) { | ||
| case UUID: | ||
| return GenericOrcReaders.uuids(); | ||
| case FIXED: | ||
| return OrcValueReaders.bytes(); | ||
| case BINARY: | ||
| return GenericOrcReaders.bytes(); | ||
| default: | ||
| throw new IllegalStateException( | ||
| String.format("Invalid iceberg type %s corresponding to ORC type %s", iPrimitive, primitive)); | ||
| } | ||
| default: | ||
| throw new IllegalArgumentException("Unhandled type " + primitive); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
It seems that we are extending the
GenericOrcReaderto also be used in Flink. Won't that create problems? E.gGenericOrcReaderis being used to construct areaderFuncfor IcebergGenerics . For instance it usesLocalTimefor Iceberg's Time datatype. Won't Flink have its own in-memory representation for primitive types and maybe also for map and list types?I think it will be better to have a completely separate
FlinkOrcReaderwhich does not rely onGenericOrcReadersimilar toSparkOrcReader. In this way changes toGenericOrcReaderwon't breakFlinkOrcReaderand there is no tight coupling between the two.@rdblue , @openinx 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.
In current flink stable version, Flink is using the
Rowtype with an array of Java objects, it's the most common way for flink now. In feature, it will useRowDatainterface , whose implementation could be binary-oriented or java object oriented, I think in that time we could separate theFlinkOrcReader. (issue: https://issues.apache.org/jira/browse/FLINK-16995).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.
What about Flink's primitive types do they align with well with Iceberg Generics?
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 agree with @rdsr - main concern would be the flexibility for changes to
GenericOrcReader, but I guess the trade-off is vs code re-usability. If there's confidence that the generics readers are fairly stable then it should not be a huge issue, but the concern seems valid on coupling these readers. I wonder if instead of using inheritance a delegator approach would be possible avoid a tight coupling.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.
My understanding is that Flink does (or can) use the same representations, except for structs. It would be good to have a response for @openinx or @JingsongLi, though. From looking at the Flink code, not all of the default conversions are these types.
VarBinaryusesbyte[]instead ofByteBufferandLocalZonedTimestampTypeusesInstant(but the Javadoc says its behavior is likeOffsetDateTimethat we use). That said, it looks like Flink might support multiple conversions.Depending on what Flink uses internally, @rdsr might be right about building a set of readers specific to those types. But if we can make this more generic easily, then I like the idea of doing that. Ideally, I think new object models would be created by providing a few methods to create and read into an object, kind of like our methods to plug in struct types.
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.
Yea, I also think the
GenericOrcReaderis a pretty small wrapper and the bulk of the functionality is provided by the readers/functions for specific types defined inOrcGenericReaders. In that regard extending theGenericOrcReaderdoesn't buy us much. We can easily share code by picking and choosing the right readers/functions fromGenericOrcReadersand providing flink specific type readers where flink types diverge from Iceberg Generics. The good thing about doing this IMO is that we get rid of extending classes which makes code changes brittle and introduces tight couplingThere 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 agreed that extending the
BaseOrcReaderandBaseOrcWriterintroduces tight coupling, I tried to de-coupleflinkwriter from generic orc writers and let them share the common writers. But seems it's hard to share the codes because we used a staticbuildConvertermethod to build the converter for each data type and few Converter depends on the staticbuildConverter, makes hard to abstract to the common converters. Just curious why did we implement the orc writer inconverterway instead of visiting the types byOrcSchemaWithTypeVisitorand generate relativeOrcRowWriter(in this way we could share most of the writers.), the currentconverterseems strange compared to otherparquetwriters andavrowriters.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.
@edgarRd @rdsr @rdblue I did a refactor for the GenericOrcWriter and moved the common writers to
GenericOrcWriter, the pull request is here: https://github.com/apache/iceberg/pull/1197/files. Mind to take a look ?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.
@chenjunjiedada opened an issue for the concern about data types and @JingsongLi clarified the types that Flink uses there. @rdsr was right and it isn't correct to copy generics with a different row type.
Sounds like #1197 is a good start. We should probably reverse how we have refactored the Avro and Parquet generics as well.
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.
Yes, you are right. After the #1197 get merged, I will recreate this patch for reviewing. Thanks.