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/29 07:48:52 UTC

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

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



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

Review comment:
       +1
   I also tangled for a long time, it is more confusing to put them together by force.

##########
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:
       The core difference is matching up schemas by ID or not.




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