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


##########
core/src/main/java/org/apache/iceberg/avro/SchemaToType.java:
##########
@@ -104,13 +104,23 @@ public Type record(Schema record, List<String> names, 
List<Type> fieldTypes) {
 
   @Override
   public Type union(Schema union, List<Type> options) {
-    Preconditions.checkArgument(
-        AvroSchemaUtil.isOptionSchema(union), "Unsupported type: non-option 
union: %s", union);
-    // records, arrays, and maps will check nullability later
-    if (options.get(0) == null) {
-      return options.get(1);
+    if (AvroSchemaUtil.isOptionSchema(union)) {
+      if (options.get(0) == null) {
+        return options.get(1);
+      } else {
+        return options.get(0);
+      }
     } else {
-      return options.get(0);
+      // Create list of Iceberg schema fields
+      List<Types.NestedField> fields = 
Lists.newArrayListWithExpectedSize(options.size());
+      int tagIndex = 0;
+      fields.add(Types.NestedField.required(allocateId(), "tag", 
Types.IntegerType.get()));
+      for (Type option : options) {
+        if (option != null) {

Review Comment:
   I'm thinking through the case of a non-option union with null, `["int", 
"null", "float"]`. That would produce: `struct<1: tag required int, 2: field0 
optional int, 3: field1 optional float>`.
   
   When the union's value is null, the would be `row(tag=1, field0=null, 
field1=null)` and when the union's value is a float it would be `row(tag=2, 
field0=null, field1=3.1)`. The mismatch between field names and the `tag` value 
is odd to me. I think the tag index should be updated every time through the 
loop, regardless of whether the option type is `null`.
   
   Also, since these are called options, what about using "option" rather than 
"field"?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to