KurtYoung 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_r352402355
 
 

 ##########
 File path: 
flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/connectors/hive/read/HiveTableInputFormat.java
 ##########
 @@ -0,0 +1,258 @@
+/*
+ * 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.connectors.hive.read;
+
+import org.apache.flink.api.common.io.LocatableInputSplitAssigner;
+import org.apache.flink.api.common.io.statistics.BaseStatistics;
+import org.apache.flink.api.java.hadoop.common.HadoopInputFormatCommonBase;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connectors.hive.FlinkHiveException;
+import org.apache.flink.connectors.hive.HiveOptions;
+import org.apache.flink.connectors.hive.HiveTablePartition;
+import org.apache.flink.core.io.InputSplitAssigner;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.types.DataType;
+
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.mapred.InputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.security.Credentials;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.util.ReflectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.IntStream;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.hadoop.mapreduce.lib.input.FileInputFormat.INPUT_DIR;
+
+/**
+ * The HiveTableInputFormat are inspired by the HCatInputFormat and 
HadoopInputFormatBase.
+ * It's used to read from hive partition/non-partition table.
+ */
+public class HiveTableInputFormat extends HadoopInputFormatCommonBase<BaseRow, 
HiveTableInputSplit> {
+
+       private static final long serialVersionUID = 1L;
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(HiveTableInputFormat.class);
+
+       private JobConf jobConf;
+
+       private String hiveVersion;
+
+       private List<String> partitionKeys;
+
+       private DataType[] fieldTypes;
+
+       private String[] fieldNames;
+
+       //For non-partition hive table, partitions only contains one partition 
which partitionValues is empty.
+       private List<HiveTablePartition> partitions;
+
+       // indices of fields to be returned, with projection applied (if any)
+       private int[] selectedFields;
+
+       private transient SplitReader reader;
+
+       private transient Configuration parameters;
+
+       public HiveTableInputFormat(
+                       JobConf jobConf,
+                       CatalogTable catalogTable,
+                       List<HiveTablePartition> partitions,
+                       int[] projectedFields,
+                       String hiveVersion) {
+               super(jobConf.getCredentials());
+               this.partitionKeys = catalogTable.getPartitionKeys();
+               this.fieldTypes = catalogTable.getSchema().getFieldDataTypes();
+               this.fieldNames = catalogTable.getSchema().getFieldNames();
+               this.hiveVersion = hiveVersion;
+               checkNotNull(catalogTable, "catalogTable can not be null.");
+               this.partitions = checkNotNull(partitions, "partitions can not 
be null.");
+               this.jobConf = new JobConf(jobConf);
+               int rowArity = catalogTable.getSchema().getFieldCount();
+               selectedFields = projectedFields != null ? projectedFields : 
IntStream.range(0, rowArity).toArray();
+       }
+
+       @Override
+       public void configure(org.apache.flink.configuration.Configuration 
parameters) {
+               this.parameters = parameters;
+       }
+
+       @Override
+       public void open(HiveTableInputSplit split) throws IOException {
+               if 
(!parameters.getBoolean(HiveOptions.TABLE_EXEC_HIVE_FALLBACK_MAPRED_READER) &&
+                               
useOrcVectorizedRead(split.getHiveTablePartition())) {
+                       this.reader = new HiveVectorizedOrcSplitReader(
+                                       jobConf, fieldNames, fieldTypes, 
selectedFields, split);
+               } else {
+                       this.reader = new HiveMapredSplitReader(jobConf, 
partitionKeys, fieldTypes, selectedFields, split);
+               }
+       }
+
+       private boolean useOrcVectorizedRead(HiveTablePartition partition) {
+               boolean isOrc = 
partition.getStorageDescriptor().getSerdeInfo().getSerializationLib()
+                               .toLowerCase().contains("orc");
+               if (!isOrc) {
+                       return false;
+               }
+
+               for (int i : selectedFields) {
+                       switch (fieldTypes[i].getLogicalType().getTypeRoot()) {
+                               case CHAR:
+                               case VARCHAR:
+                               case BOOLEAN:
+                               case BINARY:
+                               case VARBINARY:
+                               case DECIMAL:
+                               case TINYINT:
+                               case SMALLINT:
+                               case INTEGER:
+                               case BIGINT:
+                               case FLOAT:
+                               case DOUBLE:
+                               case DATE:
+                               case TIME_WITHOUT_TIME_ZONE:
+                               case TIMESTAMP_WITHOUT_TIME_ZONE:
+                               case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+                                       break;
+                               case TIMESTAMP_WITH_TIME_ZONE:
+                               case INTERVAL_YEAR_MONTH:
+                               case INTERVAL_DAY_TIME:
+                               case ARRAY:
+                               case MULTISET:
+                               case MAP:
+                               case ROW:
+                               case DISTINCT_TYPE:
+                               case STRUCTURED_TYPE:
+                               case NULL:
+                               case ANY:
+                               case SYMBOL:
+                               default:
+                                       LOG.info("Fallback to hadoop mapred 
reader, unsupported field type: " + fieldTypes[i]);
+                                       return false;
+                       }
+               }
+
+               if (hiveVersion.startsWith("1.")) {
 
 Review comment:
   move this up before type checks

----------------------------------------------------------------
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

Reply via email to