ihuzenko commented on a change in pull request #1559: DRILL-540: Allow querying 
hive views in Drill
URL: https://github.com/apache/drill/pull/1559#discussion_r242150334
 
 

 ##########
 File path: 
contrib/storage-hive/core/src/main/java/org/apache/drill/exec/planner/types/HiveToRelDataTypeConverter.java
 ##########
 @@ -0,0 +1,158 @@
+/*
+ * 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.drill.exec.planner.types;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.sql.SqlCollation;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.util.Util;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
+import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class is responsible for data type conversions
+ * from {@link org.apache.hadoop.hive.metastore.api.FieldSchema} instances
+ * to {@link  org.apache.calcite.rel.type.RelDataType} instances
+ */
+public class HiveToRelDataTypeConverter {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(HiveToRelDataTypeConverter.class);
+
+  private static final String UNSUPPORTED_HIVE_DATA_TYPE_ERROR_MSG = 
"Unsupported Hive data type %s. %n" +
+      "Following Hive data types are supported in Drill INFORMATION_SCHEMA: " +
+      "BOOLEAN, BYTE, SHORT, INT, LONG, FLOAT, DOUBLE, DATE, TIMESTAMP, 
BINARY, DECIMAL, STRING, " +
+      "VARCHAR, CHAR, LIST, MAP, STRUCT and UNION";
+
+
+  private final RelDataTypeFactory typeFactory;
+
+  public HiveToRelDataTypeConverter(RelDataTypeFactory typeFactory) {
+    this.typeFactory = typeFactory;
+  }
+
+  /**
+   * Performs conversion from Hive field to nullable RelDataType
+   *
+   * @param field - representation of data type in Hive Metastore
+   * @return appropriate nullable RelDataType for using with Calcite
+   * @throws RuntimeException for unsupported data types, check
+   *                          {@link 
HiveToRelDataTypeConverter#UNSUPPORTED_HIVE_DATA_TYPE_ERROR_MSG}
+   *                          for details about supported hive types
+   */
+  public RelDataType convertToNullableRelDataType(FieldSchema field) {
+    TypeInfo fieldTypeInfo = 
TypeInfoUtils.getTypeInfoFromTypeString(field.getType());
+    RelDataType relDataType = convertToRelDataType(fieldTypeInfo);
+    return typeFactory.createTypeWithNullability(relDataType, true);
+  }
+
+  private RelDataType convertToRelDataType(TypeInfo typeInfo) {
+    final Category typeCategory = typeInfo.getCategory();
+    switch (typeCategory) {
+      case PRIMITIVE:
+        return getRelDataType((PrimitiveTypeInfo) typeInfo);
+      case LIST:
+        return getRelDataType((ListTypeInfo) typeInfo);
+      case MAP:
+        return getRelDataType((MapTypeInfo) typeInfo);
+      case STRUCT:
+        return getRelDataType((StructTypeInfo) typeInfo);
+      case UNION:
+        logger.warn("There is no UNION data type in SQL. Converting it to Sql 
type OTHER to avoid " +
+            "breaking INFORMATION_SCHEMA queries");
+        return typeFactory.createSqlType(SqlTypeName.OTHER);
+    }
+    throw new 
RuntimeException(String.format(UNSUPPORTED_HIVE_DATA_TYPE_ERROR_MSG, 
typeCategory));
+  }
+
+  private RelDataType getRelDataType(StructTypeInfo structTypeInfo) {
+    final List<String> fieldNames = structTypeInfo.getAllStructFieldNames();
+    final List<RelDataType> relDataTypes = 
structTypeInfo.getAllStructFieldTypeInfos()
+        .stream()
 
 Review comment:
   fixed
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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