fhueske commented on a change in pull request #6483: 
[FLINK-7243][flink-formats] Add parquet input format
URL: https://github.com/apache/flink/pull/6483#discussion_r252013399
 
 

 ##########
 File path: 
flink-formats/flink-parquet/src/main/java/org/apache/flink/formats/parquet/utils/RowConverter.java
 ##########
 @@ -0,0 +1,446 @@
+/*
+ * 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.formats.parquet.utils;
+
+import org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.CompositeType;
+import org.apache.flink.api.java.typeutils.MapTypeInfo;
+import org.apache.flink.api.java.typeutils.ObjectArrayTypeInfo;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.types.Row;
+
+import org.apache.parquet.io.api.Binary;
+import org.apache.parquet.io.api.Converter;
+import org.apache.parquet.io.api.GroupConverter;
+import org.apache.parquet.io.api.PrimitiveConverter;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.OriginalType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Array;
+import java.math.BigDecimal;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Extends from {@link GroupConverter} to convert an nested Parquet Record 
into Row.
+ */
+public class RowConverter extends GroupConverter implements ParentDataHolder {
+       private static final Logger LOGGER = 
LoggerFactory.getLogger(RowConverter.class);
+       private final Converter[] converters;
+       private final ParentDataHolder parentDataHolder;
+       private final TypeInformation<?> typeInfo;
+       private Row currentRow;
+       private int posInParentRow;
+
+       public RowConverter(MessageType messageType, TypeInformation<?> 
typeInfo) {
+               this(messageType, typeInfo, null, 0);
+       }
+
+       public RowConverter(GroupType schema, TypeInformation<?> typeInfo, 
ParentDataHolder parent, int pos) {
+               this.typeInfo = typeInfo;
+               this.parentDataHolder = parent;
+               this.posInParentRow = pos;
+               this.converters = new Converter[schema.getFieldCount()];
+
+               int i = 0;
+               if (typeInfo.getArity() >= 1 && (typeInfo instanceof 
CompositeType)) {
+                       for (Type field : schema.getFields()) {
+                               converters[i] = createConverter(field, i, 
((CompositeType<?>) typeInfo).getTypeAt(i), this);
+                               i++;
+                       }
+               }
+       }
+
+       private static Converter createConverter(
+               Type field,
+               int fieldPos,
+               TypeInformation<?> typeInformation,
+               ParentDataHolder parentDataHolder) {
+               if (field.isPrimitive()) {
+                       return new RowConverter.RowPrimitiveConverter(field, 
parentDataHolder, fieldPos);
+               } else if (typeInformation instanceof MapTypeInfo) {
+                       return new RowConverter.MapConverter((GroupType) field, 
(MapTypeInfo) typeInformation,
+                               parentDataHolder, fieldPos);
+               } else if (typeInformation instanceof BasicArrayTypeInfo) {
+                       Type elementType = 
field.asGroupType().getFields().get(0);
+                       Class typeClass = ((BasicArrayTypeInfo) 
typeInformation).getComponentInfo().getTypeClass();
+                       if (typeClass.equals(Character.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Character>((BasicArrayTypeInfo) 
typeInformation, elementType,
+                                       Character.class, parentDataHolder, 
fieldPos);
+                       } else if (typeClass.equals(Boolean.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Boolean>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       Boolean.class, parentDataHolder, 
fieldPos);
+                       } else if (typeClass.equals(Short.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Short>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       Short.class, parentDataHolder, 
fieldPos);
+                       } else if (typeClass.equals(Integer.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Integer>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       Integer.class, parentDataHolder, 
fieldPos);
+                       } else if (typeClass.equals(Long.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Long>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       Long.class, parentDataHolder, fieldPos);
+                       } else if (typeClass.equals(Double.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Double>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       Double.class, parentDataHolder, 
fieldPos);
+                       } else if (typeClass.equals(String.class)) {
+                               return new 
RowConverter.BasicArrayConverter<String>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       String.class, parentDataHolder, 
fieldPos);
+                       } else if (typeClass.equals(Date.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Date>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       Date.class, parentDataHolder, fieldPos);
+                       } else if (typeClass.equals(Time.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Time>((BasicArrayTypeInfo) typeInformation, 
elementType,
+                                       Time.class, parentDataHolder, fieldPos);
+                       } else if (typeClass.equals(Timestamp.class)) {
+                               return new 
RowConverter.BasicArrayConverter<Timestamp>((BasicArrayTypeInfo) 
typeInformation,
+                                       elementType, Timestamp.class, 
parentDataHolder, fieldPos);
+                       } else if (typeClass.equals(BigDecimal.class)) {
+                               return new 
RowConverter.BasicArrayConverter<BigDecimal>((BasicArrayTypeInfo) 
typeInformation,
+                                       elementType, BigDecimal.class, 
parentDataHolder, fieldPos);
+                       }
+
+                       throw new IllegalArgumentException(
+                               String.format("Can't create unsupported 
primitive array type for %s", typeClass.toString()));
+
+               } else if (typeInformation instanceof ObjectArrayTypeInfo) {
+                       return new RowConverter.ObjectArrayConverter(field, 
(ObjectArrayTypeInfo) typeInformation,
+                               parentDataHolder, fieldPos);
+               } else if (typeInformation instanceof RowTypeInfo) {
+                       return new RowConverter((GroupType) field, 
typeInformation, parentDataHolder, fieldPos);
+               }
+
+               // Other are types, we don't support.
+               return null;
 
 Review comment:
   throwing an exception with an error message instead of returning `null` that 
will cause an NPE which needs to be debugged?

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


With regards,
Apache Git Services

Reply via email to