You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2019/10/10 21:15:52 UTC

[GitHub] [incubator-iceberg] edgarRd commented on a change in pull request #227: ORC column map fix

edgarRd commented on a change in pull request #227: ORC column map fix
URL: https://github.com/apache/incubator-iceberg/pull/227#discussion_r333739073
 
 

 ##########
 File path: orc/src/main/java/org/apache/iceberg/orc/ORCSchemaUtil.java
 ##########
 @@ -0,0 +1,412 @@
+/*
+ * 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.orc;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.types.Types;
+import org.apache.orc.TypeDescription;
+
+/**
+ * Utilities for mapping Iceberg to ORC schemas.
+ */
+final class ORCSchemaUtil {
+
+  private static final String ICEBERG_ID_ATTRIBUTE = "iceberg.id";
+  private static final String ICEBERG_REQUIRED_ATTRIBUTE = "iceberg.required";
+
+  private static final Map<Type.TypeID, TypeDescription.Category> TYPE_MAPPING =
+      ImmutableMap.<Type.TypeID, TypeDescription.Category>builder()
+          .put(Type.TypeID.BOOLEAN, TypeDescription.Category.BOOLEAN)
+          .put(Type.TypeID.INTEGER, TypeDescription.Category.INT)
+          .put(Type.TypeID.TIME, TypeDescription.Category.INT)
+          .put(Type.TypeID.LONG, TypeDescription.Category.LONG)
+          .put(Type.TypeID.FLOAT, TypeDescription.Category.FLOAT)
+          .put(Type.TypeID.DOUBLE, TypeDescription.Category.DOUBLE)
+          .put(Type.TypeID.DATE, TypeDescription.Category.DATE)
+          .put(Type.TypeID.TIMESTAMP, TypeDescription.Category.TIMESTAMP)
+          .put(Type.TypeID.STRING, TypeDescription.Category.STRING)
+          .put(Type.TypeID.UUID, TypeDescription.Category.BINARY)
+          .put(Type.TypeID.FIXED, TypeDescription.Category.BINARY)
+          .put(Type.TypeID.BINARY, TypeDescription.Category.BINARY)
+          .put(Type.TypeID.DECIMAL, TypeDescription.Category.DECIMAL)
+          .build();
+
+  private ORCSchemaUtil() {}
+
+  static TypeDescription toOrc(Schema schema) {
+    final TypeDescription root = TypeDescription.createStruct();
+    final Types.StructType schemaRoot = schema.asStruct();
+    for (Types.NestedField field : schemaRoot.asStructType().fields()) {
+      TypeDescription orcColumType = toOrc(field.fieldId(), field.type(), field.isRequired());
+      root.addField(field.name(), orcColumType);
+    }
+    return root;
+  }
+
+  private static TypeDescription toOrc(Integer fieldId, Type type, boolean isRequired) {
+    final TypeDescription orcType;
+
+    switch (type.typeId()) {
+      case BOOLEAN:
+        orcType = TypeDescription.createBoolean();
+        break;
+      case INTEGER:
+      case TIME:
+        orcType = TypeDescription.createInt();
+        break;
+      case LONG:
+        orcType = TypeDescription.createLong();
+        break;
+      case FLOAT:
+        orcType = TypeDescription.createFloat();
+        break;
+      case DOUBLE:
+        orcType = TypeDescription.createDouble();
+        break;
+      case DATE:
+        orcType = TypeDescription.createDate();
+        break;
+      case TIMESTAMP:
+        orcType = TypeDescription.createTimestamp();
+        break;
+      case STRING:
+        orcType = TypeDescription.createString();
+        break;
+      case UUID:
+      case FIXED:
+      case BINARY:
+        orcType = TypeDescription.createBinary();
+        break;
+      case DECIMAL: {
+        Types.DecimalType decimal = (Types.DecimalType) type;
+        orcType = TypeDescription.createDecimal()
+            .withScale(decimal.scale())
+            .withPrecision(decimal.precision());
+        break;
+      }
+      case STRUCT: {
+        orcType = TypeDescription.createStruct();
+        for (Types.NestedField field : type.asStructType().fields()) {
+          TypeDescription childType = toOrc(field.fieldId(), field.type(), field.isRequired());
+          orcType.addField(field.name(), childType);
+        }
+        break;
+      }
+      case LIST: {
+        Types.ListType list = (Types.ListType) type;
+        TypeDescription elementType = toOrc(list.elementId(), list.elementType(),
+            list.isElementRequired());
+        orcType = TypeDescription.createList(elementType);
+        break;
+      }
+      case MAP: {
+        Types.MapType map = (Types.MapType) type;
+        TypeDescription keyType = toOrc(map.keyId(), map.keyType(), true);
+        TypeDescription valueType = toOrc(map.valueId(), map.valueType(), map.isValueRequired());
+        orcType = TypeDescription.createMap(keyType, valueType);
+        break;
+      }
+      default:
+        throw new IllegalArgumentException("Unhandled type " + type.typeId());
+    }
+
+    // Set Iceberg column attributes for mapping
+    orcType.setAttribute(ICEBERG_ID_ATTRIBUTE, String.valueOf(fieldId));
+    orcType.setAttribute(ICEBERG_REQUIRED_ATTRIBUTE, String.valueOf(isRequired));
+    return orcType;
+  }
+
+  /**
+   * Converts an Iceberg schema to a corresponding ORC schema within the context of an existing
 
 Review comment:
   The logic here is basically what http://iceberg.apache.org/spec/#orc describes in terms of schema evolution rules, I can add that to the comments.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org