vinishjail97 commented on code in PR #650:
URL: https://github.com/apache/incubator-xtable/pull/650#discussion_r1960526043


##########
xtable-core/src/main/java/org/apache/xtable/parquet/ParquetSchemaExtractror.java:
##########
@@ -0,0 +1,470 @@
+/*
+ * 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.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import org.apache.parquet.LogicalType;
+import org.apache.parquet.LogicalTypes;
+import org.apache.parquet.Schema;
+
+import org.apache.xtable.collectors.CustomCollectors;
+import org.apache.xtable.exception.SchemaExtractorException;
+import org.apache.xtable.exception.UnsupportedSchemaTypeException;
+import org.apache.xtable.hudi.idtracking.IdTracker;
+import org.apache.xtable.hudi.idtracking.models.IdMapping;
+import org.apache.xtable.model.schema.InternalField;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.schema.InternalType;
+import org.apache.xtable.schema.SchemaUtils;
+
+/**
+ * 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 ParquetSchemaConverter {
+  // 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 ParquetSchemaConverter INSTANCE = new 
ParquetSchemaConverter();
+  private static final String ELEMENT = "element";
+  private static final String KEY = "key";
+  private static final String VALUE = "value";
+
+  public static parquetSchemaConverter getInstance() {
+    return INSTANCE;
+  }
+
+  public InternalSchema toInternalSchema(Schema schema) {
+    Map<String, IdMapping> fieldNameToIdMapping =
+        IdTracker.getInstance()
+            .getIdTracking(schema)
+            .map(
+                idTracking ->
+                    idTracking.getIdMappings().stream()
+                        .collect(Collectors.toMap(IdMapping::getName, 
Function.identity())))
+            .orElse(Collections.emptyMap());
+    return toInternalSchema(schema, null, fieldNameToIdMapping);
+  }
+
+  /**
+   * 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.
+   * @param fieldNameToIdMapping map of fieldName to IdMapping to track field 
IDs provided by the
+   *     source schema. If source schema does not contain IdMappings, map will 
be empty.
+   * @return a converted schema
+   */
+  private InternalSchema toInternalSchema(
+      Schema schema, String parentPath, Map<String, IdMapping> 
fieldNameToIdMapping) {
+    // TODO - Does not handle recursion in parquet schema
+    InternalType newDataType;
+    Map<InternalSchema.MetadataKey, Object> metadata = new HashMap<>();
+    switch (schema.getType()) {
+      case INT:
+        LogicalType logicalType = schema.getLogicalType();
+        if (logicalType instanceof LogicalTypes.Date) {
+          newDataType = InternalType.DATE;
+        } else {
+          newDataType = InternalType.INT;
+        }
+        break;
+      case STRING:
+        newDataType = InternalType.STRING;
+        break;
+      case BOOLEAN:
+        newDataType = InternalType.BOOLEAN;
+        break;
+      case BYTES:
+      case JSON:
+      case BSON:
+      case FIXED:
+        logicalType = schema.getLogicalType();

Review Comment:
   Any reason why combining all of them in a single classification ? FIXED can 
be separate IMO.  BYTES, JSON BSON are byte array kind of types.



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