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


##########
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:
   To clarify:
   
   1- Does this depend on whether there is a `null` option or not? I think the 
behavior is the same. The current logic creates `field_i` corresponding to 
non-null union options, starting from 0. The behavior sounds the same whether 
`null` is there (in the options) or not. This means that `tag` convention does 
not depend on whether there is a `null` option or not. Do you agree?
   
   2- `tag` is set at read time. In the read PR, it is set in such a way that 
it is zero-based index as well. The test cases are 
[here](https://github.com/apache/iceberg/pull/5704/files#diff-409a0a443647e843af30276987f07ddd067c2ef7660d632e89bfce0d783c18e3R94).
 So in both cases, whether there is a null option or not, it will match `i` in 
`field_i`.
   
   3- I think when the union value is null, the struct will be null as well 
(although this does not affect the two points above).



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