JingsongLi commented on a change in pull request #10022: [FLINK-14135][hive][orc] Introduce orc ColumnarRow reader for hive connector URL: https://github.com/apache/flink/pull/10022#discussion_r349541880
########## File path: flink-connectors/flink-orc/src/main/java/org/apache/flink/orc/OrcColumnarRowSplitReader.java ########## @@ -0,0 +1,192 @@ +/* + * 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.flink.orc; + +import org.apache.flink.core.fs.Path; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.dataformat.BaseRow; +import org.apache.flink.table.dataformat.ColumnarRow; +import org.apache.flink.table.dataformat.vector.ColumnVector; +import org.apache.flink.table.dataformat.vector.VectorizedColumnBatch; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.ArrayType; +import org.apache.flink.table.types.logical.CharType; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MapType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.VarCharType; + +import org.apache.hadoop.conf.Configuration; +import org.apache.orc.TypeDescription; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.apache.flink.orc.vector.AbstractOrcColumnVector.createVector; +import static org.apache.flink.orc.vector.AbstractOrcColumnVector.createVectorFromConstant; + +/** + * {@link OrcSplitReader} to read ORC files into {@link BaseRow}. + */ +public class OrcColumnarRowSplitReader extends OrcSplitReader<BaseRow> { + + // the vector of rows that is read in a batch + private final VectorizedColumnBatch columnarBatch; + + private final ColumnarRow row; + + public OrcColumnarRowSplitReader( + Configuration conf, + int[] selectedFields, + String[] fullFieldNames, + DataType[] fullFieldTypes, + List<String> partitionKeys, + Map<String, Object> partitionSpec, + List<OrcSplitReader.Predicate> conjunctPredicates, + int batchSize, + Path path, + long splitStart, + long splitLength) throws IOException { + super( + conf, + convertToOrcType(fullFieldNames, fullFieldTypes, partitionKeys), + selectedFields, + conjunctPredicates, + batchSize, + path, + splitStart, + splitLength); + + List<String> nonPartNames = Arrays.stream(fullFieldNames) + .filter(n -> !partitionKeys.contains(n)) + .collect(Collectors.toList()); + + // create and initialize the row batch + ColumnVector[] vectors = new ColumnVector[selectedFields.length]; + for (int i = 0; i < vectors.length; i++) { + String name = fullFieldNames[selectedFields[i]]; + LogicalType type = fullFieldTypes[selectedFields[i]].getLogicalType(); + vectors[i] = partitionKeys.contains(name) ? + createVectorFromConstant(type, partitionSpec.get(name), batchSize) : + createVector(rowBatch.cols[nonPartNames.indexOf(name)]); + } + this.columnarBatch = new VectorizedColumnBatch(vectors); + this.row = new ColumnarRow(columnarBatch); + } + + private static TypeDescription convertToOrcType( + String[] fullFieldNames, + DataType[] fullFieldTypes, + List<String> partitionKeys) { + List<String> fullNameList = Arrays.asList(fullFieldNames); + List<String> orcNames = fullNameList.stream() + .filter(n -> !partitionKeys.contains(n)).collect(Collectors.toList()); + List<TypeDescription> fields = orcNames.stream() + .mapToInt(fullNameList::indexOf) + .mapToObj(i -> fullFieldTypes[i].getLogicalType()) + .map(OrcColumnarRowSplitReader::logicalTypeToOrcType) + .collect(Collectors.toList()); + TypeDescription struct = TypeDescription.createStruct(); + for (int i = 0; i < orcNames.size(); i++) { + struct.addField(orcNames.get(i), fields.get(i)); + } + return struct; + } + + /** + * See {@code org.apache.flink.table.catalog.hive.util.HiveTypeUtil}. + */ + private static TypeDescription logicalTypeToOrcType(LogicalType type) { + type = type.copy(true); + switch (type.getTypeRoot()) { Review comment: It's important to cover every type and usage. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
