Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions data/src/main/java/org/apache/iceberg/data/orc/BaseOrcReader.java
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> {
Copy link
Contributor

@rdsr rdsr Jul 8, 2020

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 GenericOrcReader to also be used in Flink. Won't that create problems? E.g GenericOrcReader is being used to construct a readerFunc for IcebergGenerics . For instance it uses LocalTime for 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 FlinkOrcReader which does not rely on GenericOrcReader similar to SparkOrcReader. In this way changes to GenericOrcReader won't break FlinkOrcReader and there is no tight coupling between the two.

@rdblue , @openinx thoughts?

Copy link
Member Author

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 Row type with an array of Java objects, it's the most common way for flink now. In feature, it will use RowData interface , whose implementation could be binary-oriented or java object oriented, I think in that time we could separate the FlinkOrcReader. (issue: https://issues.apache.org/jira/browse/FLINK-16995).

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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?

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. VarBinary uses byte[] instead of ByteBuffer and LocalZonedTimestampType uses Instant (but the Javadoc says its behavior is like OffsetDateTime that 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.

Copy link
Contributor

@rdsr rdsr Jul 11, 2020

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 GenericOrcReader is a pretty small wrapper and the bulk of the functionality is provided by the readers/functions for specific types defined in OrcGenericReaders. In that regard extending the GenericOrcReader doesn't buy us much. We can easily share code by picking and choosing the right readers/functions from GenericOrcReaders and 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 coupling

Copy link
Member Author

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 BaseOrcReader and BaseOrcWriter introduces tight coupling, I tried to de-couple flink writer from generic orc writers and let them share the common writers. But seems it's hard to share the codes because we used a static buildConverter method to build the converter for each data type and few Converter depends on the static buildConverter, makes hard to abstract to the common converters. Just curious why did we implement the orc writer in converter way instead of visiting the types by OrcSchemaWithTypeVisitor and generate relative OrcRowWriter (in this way we could share most of the writers.), the current converter seems strange compared to other parquet writers and avro writers.

Copy link
Member Author

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 ?

Copy link
Contributor

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.

Copy link
Member Author

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.

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);
}
}
}
}
Loading