the-other-tim-brown commented on code in PR #669:
URL: https://github.com/apache/incubator-xtable/pull/669#discussion_r2040718628


##########
xtable-core/src/main/java/org/apache/xtable/parquet/ParquetSchemaExtractor.java:
##########
@@ -0,0 +1,535 @@
+/*
+ * 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.xtable.parquet;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.xtable.schema.SchemaUtils;
+import org.apache.xtable.exception.SchemaExtractorException;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.apache.xtable.hudi.idtracking.models.IdMapping;
+import org.apache.avro.Schema;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.apache.xtable.collectors.CustomCollectors;
+import org.apache.xtable.exception.UnsupportedSchemaTypeException;
+import org.apache.xtable.model.schema.InternalField;
+import org.apache.parquet.schema.Type.Repetition;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.schema.InternalType;
+import org.apache.parquet.schema.Type.ID;
+import org.apache.parquet.schema.OriginalType;
+import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName;
+import org.apache.parquet.schema.Types;
+import org.apache.parquet.column.ColumnDescriptor;
+
+
+/**
+ * Class that converts parquet Schema {@link Schema} to Canonical Schema 
{@link InternalSchema} and
+ * vice-versa. This conversion is fully reversible and there is a strict 1 to 
1 mapping between
+ * parquet data types and canonical data types.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class ParquetSchemaExtractor {
+    // parquet only supports string keys in maps
+    private static final InternalField MAP_KEY_FIELD =
+            InternalField.builder()
+                    .name(InternalField.Constants.MAP_KEY_FIELD_NAME)
+                    .schema(
+                            InternalSchema.builder()
+                                    .name("map_key")
+                                    .dataType(InternalType.STRING)
+                                    .isNullable(false)
+                                    .build())
+                    .defaultValue("")
+                    .build();
+    private static final ParquetSchemaExtractor INSTANCE = new 
ParquetSchemaExtractor();
+    private static final String ELEMENT = "element";
+    private static final String KEY = "key";
+    private static final String VALUE = "value";
+
+    public static ParquetSchemaExtractor getInstance() {
+        return INSTANCE;
+    }
+
+    private static boolean groupTypeIsNullable(Type schema) {
+        return schema.getRepetition() == Repetition.REQUIRED ? false : true;
+    }
+
+    private static boolean groupTypeContainsNull(Type schema) {
+        if (!schema.isPrimitive()) {
+            for (Type field : schema.asGroupType().getFields()) {
+                if (field/*.getLogicalTypeAnnotation().toOriginalType()*/ == 
null) {
+                    return true;
+                }
+            }
+        } else {
+            if (schema.equals(null)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /*    private static LogicalTypeAnnotation 
finalizeSchema(LogicalTypeAnnotation targetSchema, InternalSchema inputSchema) {
+            if (inputSchema.isNullable()) {
+                return targetSchema.union(null); // 
LogicalTypeAnnotation.unknownType()
+            }
+            return targetSchema;
+        }*/
+
+    /**
+     * Converts the parquet {@link Schema} to {@link InternalSchema}.
+     *
+     * @param schema     The schema being converted
+     * @param parentPath If this schema is nested within another, this will be 
a dot separated string
+     *                   representing the path from the top most field to the 
current schema.
+     * @return a converted schema
+     */
+    public InternalSchema toInternalSchema(
+            Type schema, String parentPath) {
+        // TODO - Does not handle recursion in parquet schema
+        InternalType newDataType = null;
+        Type.Repetition currentRepetition = null;
+        List<InternalField> subFields = new ArrayList<>();
+        PrimitiveType primitiveType;
+        LogicalTypeAnnotation logicalType;
+        Map<InternalSchema.MetadataKey, Object> metadata = new HashMap<>();
+        String elementName = schema.getName();
+        if (schema.isPrimitive()) {
+            primitiveType = schema.asPrimitiveType();
+            switch (primitiveType.getPrimitiveTypeName()) {
+                // PrimitiveTypes
+                case INT64:
+                    logicalType = schema.getLogicalTypeAnnotation();
+                    if (logicalType instanceof 
LogicalTypeAnnotation.TimestampLogicalTypeAnnotation) {
+                        LogicalTypeAnnotation.TimeUnit timeUnit =
+                                
((LogicalTypeAnnotation.TimestampLogicalTypeAnnotation) logicalType).getUnit();
+                        if (timeUnit == LogicalTypeAnnotation.TimeUnit.MICROS) 
{
+                            newDataType = InternalType.TIMESTAMP;
+                            metadata.put(
+                                    
InternalSchema.MetadataKey.TIMESTAMP_PRECISION,
+                                    InternalSchema.MetadataValue.MICROS);
+                        } else if (timeUnit == 
LogicalTypeAnnotation.TimeUnit.MILLIS) {
+                            newDataType = InternalType.TIMESTAMP_NTZ;

Review Comment:
   You are currently using `TIMESTAMP_NTZ` without checking if it is actually 
this type. checking to see if it is a local timestamp vs adjusted to UTC is 
important so that other systems can read the value properly.



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

To unsubscribe, e-mail: commits-unsubscr...@xtable.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to