rdsr commented on a change in pull request #1103:
URL: https://github.com/apache/iceberg/pull/1103#discussion_r439190054



##########
File path: 
mr/src/main/java/org/apache/iceberg/mr/mapred/IcebergObjectInspectorGenerator.java
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.mr.mapred;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hadoop.hive.serde2.SerDeException;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+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.iceberg.Schema;
+import org.apache.iceberg.types.Types;
+
+class IcebergObjectInspectorGenerator {
+
+  protected ObjectInspector createObjectInspector(Schema schema) throws 
Exception {
+    List<String> columnNames = setColumnNames(schema);
+    List<TypeInfo> columnTypes = 
IcebergSchemaToTypeInfo.getColumnTypes(schema);
+
+    List<ObjectInspector> columnOIs = new ArrayList<>(columnTypes.size());
+    for (int i = 0; i < columnTypes.size(); i++) {
+      columnOIs.add(createObjectInspectorWorker(columnTypes.get(i)));
+    }
+    return 
ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, columnOIs, 
null);
+  }
+
+  protected ObjectInspector createObjectInspectorWorker(TypeInfo typeInfo) 
throws Exception {
+    ObjectInspector.Category typeCategory = typeInfo.getCategory();
+
+    switch (typeCategory) {
+      case PRIMITIVE:
+        PrimitiveTypeInfo pti = (PrimitiveTypeInfo) typeInfo;
+        return 
PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(pti);

Review comment:
       Maybe this can be a visitor?  We recently built a visitor the other way 
round Hive -> Iceberg. 
https://github.com/linkedin/iceberg/blob/master/hive/src/main/java/org/apache/iceberg/hive/legacy/HiveTypeToIcebergType.java

##########
File path: 
mr/src/main/java/org/apache/iceberg/mr/mapred/IcebergSchemaToTypeInfo.java
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.mr.mapred;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hadoop.hive.serde.serdeConstants;
+import org.apache.hadoop.hive.serde2.SerDeException;
+import org.apache.hadoop.hive.serde2.typeinfo.HiveDecimalUtils;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+/**
+ * Class to convert Iceberg types to Hive TypeInfo
+ */
+final class IcebergSchemaToTypeInfo {
+
+  private IcebergSchemaToTypeInfo() {
+
+  }
+
+  private static final ImmutableMap<Object, Object> primitiveTypeToTypeInfo = 
ImmutableMap.builder()
+      .put(Types.BooleanType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.BOOLEAN_TYPE_NAME))
+      .put(Types.IntegerType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.INT_TYPE_NAME))
+      .put(Types.LongType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.BIGINT_TYPE_NAME))
+      .put(Types.FloatType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.FLOAT_TYPE_NAME))
+      .put(Types.DoubleType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.DOUBLE_TYPE_NAME))
+      .put(Types.BinaryType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.BINARY_TYPE_NAME))
+      .put(Types.StringType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.STRING_TYPE_NAME))
+      .put(Types.DateType.get(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.DATE_TYPE_NAME))
+      .put(Types.TimestampType.withoutZone(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.BIGINT_TYPE_NAME))
+      .put(Types.TimestampType.withZone(), 
TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.BIGINT_TYPE_NAME))
+      .build();
+
+  public static List<TypeInfo> getColumnTypes(Schema schema) throws Exception {
+    List<Types.NestedField> fields = schema.columns();
+    List<TypeInfo> types = new ArrayList<>(fields.size());
+    for (Types.NestedField field : fields) {
+      types.add(generateTypeInfo(field.type()));
+    }
+    return types;
+  }
+
+  private static TypeInfo generateTypeInfo(Type type) throws Exception {

Review comment:
       Yea +1 for a visitor. We have TypeInfo to Iceberg Type visitor for 
inspiration 
https://github.com/linkedin/iceberg/blob/master/hive/src/main/java/org/apache/iceberg/hive/legacy/HiveTypeToIcebergType.java

##########
File path: mr/src/main/java/org/apache/iceberg/mr/mapred/IcebergSerDe.java
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.mr.mapred;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.sql.Date;
+import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import javax.annotation.Nullable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.serde2.AbstractSerDe;
+import org.apache.hadoop.hive.serde2.SerDeException;
+import org.apache.hadoop.hive.serde2.SerDeStats;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.io.Writable;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SnapshotsTable;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+public class IcebergSerDe extends AbstractSerDe {
+
+  private Schema schema;
+  private ObjectInspector inspector;
+  private List<Object> row;
+
+  @Override
+  public void initialize(@Nullable Configuration configuration, Properties 
serDeProperties) throws SerDeException {
+    Table table = null;
+    try {
+      table = TableResolver.resolveTableFromConfiguration(configuration, 
serDeProperties);
+    } catch (IOException e) {
+      throw new UncheckedIOException("Unable to resolve table from 
configuration: ", e);
+    }
+    this.schema = table.schema();
+    if (table instanceof SnapshotsTable) {
+      try {
+        this.inspector = new 
IcebergObjectInspectorGenerator().createObjectInspector(schema);
+      } catch (Exception e) {
+        throw new SerDeException(e);
+      }
+    } else {
+      List<Types.NestedField> columns = new ArrayList<>(schema.columns());
+      columns.add(Types.NestedField.optional(Integer.MAX_VALUE,
+          SystemTableUtil.snapshotIdVirtualColumnName(serDeProperties), 
Types.LongType.get()));
+      Schema withVirtualColumn = new Schema(columns);
+      try {
+        this.inspector = new 
IcebergObjectInspectorGenerator().createObjectInspector(withVirtualColumn);
+      } catch (Exception e) {
+        throw new SerDeException(e);
+      }
+    }
+  }
+
+  @Override
+  public Class<? extends Writable> getSerializedClass() {
+    return null;
+  }
+
+  @Override
+  public Writable serialize(Object o, ObjectInspector objectInspector) {
+    return null;

Review comment:
       throw Unsupported?

##########
File path: mr/src/main/java/org/apache/iceberg/mr/mapred/IcebergSerDe.java
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.mr.mapred;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.sql.Date;
+import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import javax.annotation.Nullable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.serde2.AbstractSerDe;
+import org.apache.hadoop.hive.serde2.SerDeException;
+import org.apache.hadoop.hive.serde2.SerDeStats;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.io.Writable;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SnapshotsTable;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+public class IcebergSerDe extends AbstractSerDe {

Review comment:
       @cmathiesen @rdblue . Do u folks see value in having Hive classes in a 
separate hive  module instead of MR?  Similar to Iceberg Pig ? I think we need 
to provide a reader func implementation for Hive to be used with the MR module

##########
File path: mr/src/main/java/org/apache/iceberg/mr/mapred/IcebergSerDe.java
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.mr.mapred;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.sql.Date;
+import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import javax.annotation.Nullable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.serde2.AbstractSerDe;
+import org.apache.hadoop.hive.serde2.SerDeException;
+import org.apache.hadoop.hive.serde2.SerDeStats;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.io.Writable;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SnapshotsTable;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+public class IcebergSerDe extends AbstractSerDe {
+
+  private Schema schema;
+  private ObjectInspector inspector;
+  private List<Object> row;
+
+  @Override
+  public void initialize(@Nullable Configuration configuration, Properties 
serDeProperties) throws SerDeException {
+    Table table = null;
+    try {
+      table = TableResolver.resolveTableFromConfiguration(configuration, 
serDeProperties);
+    } catch (IOException e) {
+      throw new UncheckedIOException("Unable to resolve table from 
configuration: ", e);
+    }
+    this.schema = table.schema();
+    if (table instanceof SnapshotsTable) {
+      try {
+        this.inspector = new 
IcebergObjectInspectorGenerator().createObjectInspector(schema);
+      } catch (Exception e) {
+        throw new SerDeException(e);
+      }
+    } else {
+      List<Types.NestedField> columns = new ArrayList<>(schema.columns());
+      columns.add(Types.NestedField.optional(Integer.MAX_VALUE,
+          SystemTableUtil.snapshotIdVirtualColumnName(serDeProperties), 
Types.LongType.get()));
+      Schema withVirtualColumn = new Schema(columns);
+      try {
+        this.inspector = new 
IcebergObjectInspectorGenerator().createObjectInspector(withVirtualColumn);
+      } catch (Exception e) {
+        throw new SerDeException(e);
+      }
+    }
+  }
+
+  @Override
+  public Class<? extends Writable> getSerializedClass() {
+    return null;
+  }
+
+  @Override
+  public Writable serialize(Object o, ObjectInspector objectInspector) {
+    return null;
+  }
+
+  @Override
+  public SerDeStats getSerDeStats() {
+    return null;
+  }
+
+  @Override
+  public Object deserialize(Writable writable) {

Review comment:
       +1. We should build this similar to reader Funcs. Please see examples of 
this in Spark module org.apache.iceberg.spark.data.SparkValueReaders [for Avro] 
and org.apache.iceberg.spark.data.SparkOrcValueReaders




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to