wmoustafa commented on code in PR #7392:
URL: https://github.com/apache/iceberg/pull/7392#discussion_r1280023106


##########
core/src/main/java/org/apache/iceberg/avro/AvroWithPartnerByStructureVisitor.java:
##########
@@ -93,14 +94,23 @@ private static <P, T> T visitRecord(
   private static <P, T> T visitUnion(
       P type, Schema union, AvroWithPartnerByStructureVisitor<P, T> visitor) {
     List<Schema> types = union.getTypes();
-    Preconditions.checkArgument(
-        AvroSchemaUtil.isOptionSchema(union), "Cannot visit non-option union: 
%s", union);
     List<T> options = Lists.newArrayListWithExpectedSize(types.size());
-    for (Schema branch : types) {
-      if (branch.getType() == Schema.Type.NULL) {
-        options.add(visit(visitor.nullType(), branch, visitor));
-      } else {
-        options.add(visit(type, branch, visitor));
+    if (AvroSchemaUtil.isOptionSchema(union)) {
+      for (Schema branch : types) {
+        if (branch.getType() == Schema.Type.NULL) {
+          options.add(visit(visitor.nullType(), branch, visitor));
+        } else {
+          options.add(visit(type, branch, visitor));
+        }
+      }
+    } else {
+      List<Schema> nonNullTypes =
+          types.stream().filter(t -> t.getType() != 
Schema.Type.NULL).collect(Collectors.toList());
+      for (int i = 0; i < nonNullTypes.size(); i++) {
+        // In the case of complex union, the corresponding "type" is a struct. 
Non-null type i in
+        // the union maps to struct filed i + 1 because the first struct field 
is the "tag".
+        options.add(
+            visit(visitor.fieldNameAndType(type, i + 1).second(), 
nonNullTypes.get(i), visitor));

Review Comment:
   I am trying to avoid that to keep the code clean. If we visit NULL we will 
have to manipulate the `i` index differently before and after we visit the 
NULL. We can also keep the above code and add one line for the NULL if it 
exists, but it will not look very clean either. Do you think it is worth it?



-- 
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: issues-unsubscr...@iceberg.apache.org

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


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

Reply via email to