voonhous commented on code in PR #17581:
URL: https://github.com/apache/hudi/pull/17581#discussion_r2639175932


##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaUtils.java:
##########
@@ -681,4 +681,44 @@ public static String getRecordQualifiedName(String 
tableName) {
     // Delegate to AvroSchemaUtils
     return AvroSchemaUtils.getAvroRecordQualifiedName(tableName);
   }
+
+  /**
+   * Resolves a union schema by finding the schema matching the given full 
name.
+   * Handles both simple nullable unions (null + non-null) and complex unions 
with multiple types.
+   *
+   * <p>This method supports the following union types:
+   * <ul>
+   *   <li>Simple nullable unions: {@code ["null", "Type"]} - returns the 
non-null type</li>
+   *   <li>Complex unions: {@code ["null", "TypeA", "TypeB"]} - returns the 
type matching fieldSchemaFullName</li>
+   *   <li>Non-union schemas - returns the schema as-is</li>
+   * </ul>
+   *
+   * @param schema the schema to resolve (may or may not be a union)
+   * @param fieldSchemaFullName the full name of the schema to find within the 
union
+   * @return the resolved schema
+   * @throws org.apache.hudi.internal.schema.HoodieSchemaException if the 
union cannot be resolved or no matching type is found
+   */
+  public static HoodieSchema resolveUnionSchema(HoodieSchema schema, String 
fieldSchemaFullName) {
+    if (schema.getType() != HoodieSchemaType.UNION) {
+      return schema;
+    }
+
+    List<HoodieSchema> innerTypes = schema.getTypes();
+    if (innerTypes.size() == 2 && schema.isNullable()) {
+      // this is a basic nullable field so handle it more efficiently
+      return schema.getNonNullType();
+    }
+
+    HoodieSchema nonNullType = innerTypes.stream()
+        .filter(it -> it.getType() != HoodieSchemaType.NULL && 
java.util.Objects.equals(it.getFullName(), fieldSchemaFullName))

Review Comment:
   Done



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

Reply via email to