You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/07/24 08:35:44 UTC

[GitHub] [iceberg] openinx commented on a change in pull request #1235: avro: Abstract AvroWithPartnerSchemaVisitor

openinx commented on a change in pull request #1235:
URL: https://github.com/apache/iceberg/pull/1235#discussion_r459921033



##########
File path: core/src/main/java/org/apache/iceberg/avro/AvroWithPartnerSchemaVisitor.java
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.avro;
+
+import java.util.Deque;
+import java.util.List;
+import org.apache.avro.Schema;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+
+/**
+ * A abstract avro schema visitor with partner type. This class is for both reading and writing:
+ * - For reading, the avro schema could evolve into the partner type. (schema evolution)
+ * - For writing, the avro schema should be consistent with partner type.
+ *
+ * @param <P> Partner type.
+ * @param <T> Return T.
+ */
+public abstract class AvroWithPartnerSchemaVisitor<P, T> {
+
+  public static <P, T> T visit(P partner, Schema schema, AvroWithPartnerSchemaVisitor<P, T> visitor) {
+    switch (schema.getType()) {
+      case RECORD:
+        return visitRecord(partner, schema, visitor);
+
+      case UNION:
+        return visitUnion(partner, schema, visitor);
+
+      case ARRAY:
+        return visitArray(partner, schema, visitor);
+
+      case MAP:
+        P keyType = visitor.mapKeyType(partner);
+        Preconditions.checkArgument(
+            visitor.isValidMapKey(keyType),
+            "Invalid map: %s is not a string", keyType);
+        return visitor.map(partner, schema, visit(visitor.mapValueType(partner), schema.getValueType(), visitor));
+
+      default:
+        return visitor.primitive(partner, schema);
+    }
+  }
+
+  // ---------------------------------- Static helpers ---------------------------------------------
+
+  private static <P, T> T visitRecord(P struct, Schema record, AvroWithPartnerSchemaVisitor<P, T> visitor) {
+    // check to make sure this hasn't been visited before
+    String name = record.getFullName();
+    Preconditions.checkState(!visitor.recordLevels.contains(name),
+        "Cannot process recursive Avro record %s", name);
+    List<Schema.Field> fields = record.getFields();
+    visitor.recordLevels.push(name);
+
+    List<String> names = Lists.newArrayListWithExpectedSize(fields.size());
+    List<T> results = Lists.newArrayListWithExpectedSize(fields.size());
+
+    if (visitor.schemaEvolution()) {
+      for (Schema.Field field : fields) {
+        int fieldId = AvroSchemaUtil.getFieldId(field);
+        names.add(field.name());
+        results.add(visit(visitor.structFieldTypeById(struct, fieldId), field.schema(), visitor));
+      }
+    } else {
+      String[] fieldNames = visitor.structFieldNames(struct);
+      P[] fieldTypes = visitor.structFieldTypes(struct);
+      Preconditions.checkArgument(fieldTypes.length == fields.size(),
+          "Structs do not match: %s != %s", struct, record);
+      for (int i = 0; i < fieldTypes.length; i += 1) {
+        String fieldName = fieldNames[i];
+        Schema.Field field = fields.get(i);
+        Preconditions.checkArgument(AvroSchemaUtil.makeCompatibleName(fieldName).equals(field.name()),
+            "Structs do not match: field %s != %s", fieldName, field.name());
+        results.add(visit(fieldTypes[i], field.schema(), visitor));

Review comment:
       So the difference between this sub field visit and the previous sub field [visit](https://github.com/apache/iceberg/pull/1235/files#diff-e12cc7b2561a2a909b41535f4eb86a04R78) is:  we use the different methods to get the data type of inner field.   I think we don't need both the structFieldTypeById and structFieldTypes,   is it possible to abstract them to be one method and then we could remove the `if (visitor.schemaEvolution()) {} else {...}` finally ? 




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



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org