You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2023/01/19 09:37:39 UTC

[GitHub] [hudi] wzx140 commented on a diff in pull request #7512: [HUDI-5417] support to read avro from non-legacy map/list in parquet log

wzx140 commented on code in PR #7512:
URL: https://github.com/apache/hudi/pull/7512#discussion_r1081013630


##########
hudi-common/src/main/java/org/apache/parquet/avro/HoodieAvroReadSupport.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.parquet.avro;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.OriginalType;
+import org.apache.parquet.schema.Type;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class HoodieAvroReadSupport<T> extends AvroReadSupport<T> {
+
+  public HoodieAvroReadSupport() {
+  }
+
+  @Override
+  public ReadContext init(Configuration configuration, Map<String, String> keyValueMetaData, MessageType fileSchema) {
+    boolean legacyMode = checkLegacyMode(fileSchema.getFields());
+    if (!legacyMode) {
+      configuration.set(AvroWriteSupport.WRITE_OLD_LIST_STRUCTURE, "false");
+    }
+    ReadContext readContext = super.init(configuration, keyValueMetaData, fileSchema);
+    MessageType requestedSchema = readContext.getRequestedSchema();
+    if (!legacyMode) {
+      requestedSchema = new MessageType(requestedSchema.getName(), convertLegacyMap(requestedSchema.getFields()));
+    }
+    return new ReadContext(requestedSchema, readContext.getReadSupportMetadata());
+  }
+
+  /**
+   * Check whether write map/list with legacy mode.
+   * list:
+   * optional group obj_ids (LIST) {
+   *   repeated binary array (UTF8);
+   * }
+   * map:
+   * optional group obj_ids (MAP) {
+   *   repeated group map (MAP_KEY_VALUE) {
+   *      required binary key (UTF8);
+   *      required binary value (UTF8);
+   *   }
+   * }
+   */
+  private boolean checkLegacyMode(List<Type> parquetFields) {
+    for (Type type : parquetFields) {
+      if (!type.isPrimitive()) {
+        GroupType groupType = type.asGroupType();
+        OriginalType originalType = groupType.getOriginalType();
+        if (originalType == OriginalType.MAP
+            && groupType.getFields().get(0).getOriginalType() != OriginalType.MAP_KEY_VALUE) {
+          return false;
+        }
+        if (originalType == OriginalType.LIST
+            && !groupType.getType(0).getName().equals("array")) {
+          return false;
+        }
+        if (!checkLegacyMode(groupType.getFields())) {
+          return false;
+        }
+      }
+    }
+    return true;
+  }
+
+  private List<Type> convertLegacyMap(List<Type> oldTypes) {
+    List<Type> newTypes = new ArrayList<>(oldTypes.size());
+    for (Type type : oldTypes) {
+      if (!type.isPrimitive()) {
+        GroupType parent = type.asGroupType();
+        List<Type> types = convertLegacyMap(parent.getFields());
+        if (type.getOriginalType() == OriginalType.MAP_KEY_VALUE) {

Review Comment:
   To convert non-legacy map schema to legacy map schema. Because there is no AvroWriteSupport.WRITE_OLD_MAP_STRUCTURE according to AvroWriteSupport.WRITE_OLD_LIST_STRUCTURE



-- 
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-unsubscribe@hudi.apache.org

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