You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by GitBox <gi...@apache.org> on 2021/04/13 11:43:36 UTC

[GitHub] [avro] RyanSkraba commented on a change in pull request #1097: AVRO-2872: Fix bug in SpecificCompiler which prevents Logical Types being registered for union fields

RyanSkraba commented on a change in pull request #1097:
URL: https://github.com/apache/avro/pull/1097#discussion_r612363711



##########
File path: lang/java/avro/src/main/java/org/apache/avro/LogicalType.java
##########
@@ -89,4 +91,36 @@ public void validate(Schema schema) {
     }
   }
 
+  /**
+   * Utility function to resolve the logical type from the given Schema
+   * 
+   * @param schema a Schema representing a logical type
+   * @return the logical type represented by the schema, or null if the schema
+   *         doesn't represent a logical type, or if the logical type is ambiguous
+   */
+  public static LogicalType resolveLogicalType(Schema schema) {
+    if (schema.getLogicalType() != null) {
+      return schema.getLogicalType();
+    } else if (schema.isUnion()) {
+      List<Schema> types = schema.getTypes();
+      if (types.size() == 1) {
+        // The union has a single branch that may or may not be a logical type - return
+        // it either way
+        return types.get(0).getLogicalType();
+      } else if (types.size() == 2) {
+        // The union has exactly two branches.
+        // If one branch is null and one is a logical type, return the logical type,
+        // else return neither
+        LogicalType t1 = types.get(0).getLogicalType();
+        LogicalType t2 = types.get(1).getLogicalType();
+        if (t1 != null && t2 == null) {

Review comment:
       I don't think this is correct either!
   
   If t1 is an `Schema.Type.STRING` and t2 is a logicalType `timestamp-millis`, for example, this won't return the right answer.




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