[ 
https://issues.apache.org/jira/browse/GOBBLIN-933?focusedWorklogId=340665&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-340665
 ]

ASF GitHub Bot logged work on GOBBLIN-933:
------------------------------------------

                Author: ASF GitHub Bot
            Created on: 08/Nov/19 18:28
            Start Date: 08/Nov/19 18:28
    Worklog Time Spent: 10m 
      Work Description: haojiliu commented on pull request #2800: [GOBBLIN-933] 
add support for array of unions in json schema_new
URL: https://github.com/apache/incubator-gobblin/pull/2800#discussion_r344307991
 
 

 ##########
 File path: 
gobblin-core/src/main/java/org/apache/gobblin/converter/avro/JsonElementConversionWithAvroSchemaFactory.java
 ##########
 @@ -210,4 +217,116 @@ public Schema schema() {
       return this.schema;
     }
   }
-}
+
+  /**
+   * A converter to convert Union type to avro
+   * Here it will try all the possible converters for one type, for example, 
to convert an int value, it will try all Number converters
+   * until meet the first workable one.
+   * So a known bug here is if there are long and double inside the union 
type, it's possible that all the value will be parse as long
+   * We're doing this since there is no way to determine what exact type it is 
for a JasonElement
+   */
+  public static class UnionConverter extends ComplexConverter {
+    private final List<Schema> schemas;
+    private final List<JsonElementConverter> converters;
+    private final Schema schemaNode;
+
+    public UnionConverter(String fieldName, boolean nullable, String 
sourceType, Schema schemaNode,
+        WorkUnitState state, List<String> ignoreFields) throws 
UnsupportedDateTypeException {
+      super(fieldName, nullable, sourceType);
+      this.schemas = schemaNode.getTypes();
+      converters = new ArrayList<>();
+      for(Schema schema: schemas) {
+        converters.add(getConvertor(fieldName, schema.getType().getName(), 
schemaNode, state, isNullable(), ignoreFields));
+      }
+      this.schemaNode = schemaNode;
+    }
+
+    @Override
+    Object convertField(JsonElement value) {
+       for(JsonElementConverter converter: converters)
+       {
+         try {
+           switch (converter.getTargetType()) {
+             case STRING: {
+               if (value.isJsonPrimitive() && 
value.getAsJsonPrimitive().isString()) {
+                 return converter.convert(value);
+               }
+               break;
+             }
+             case FIXED:
+             case BYTES:
+             case INT:
+             case LONG:
+             case FLOAT:
+             case DOUBLE: {
+               if (value.isJsonPrimitive() && 
value.getAsJsonPrimitive().isNumber()) {
+                 return converter.convert(value);
+               }
+               break;
+             }
+             case BOOLEAN:{
+               if (value.isJsonPrimitive() && 
value.getAsJsonPrimitive().isBoolean()) {
+                 return converter.convert(value);
+               }
+               break;
+             }
+             case ARRAY:{
+               if (value.isJsonArray()) {
+                 return converter.convert(value);
+               }
+               break;
+             }
+             case MAP:
+             case ENUM:
+             case RECORD:{
+               if (value.isJsonObject()) {
+                 return converter.convert(value);
+               }
+               break;
+             }
+             case NULL:{
+               if(value.isJsonNull()) {
+                 return converter.convert(value);
+               }
+               break;
+             }
+             case UNION:
+               return new UnsupportedDateTypeException("does not support union 
type in union");
+             default:
+               return converter.convert(value);
+           }
+         } catch (Exception e){}
 
 Review comment:
   second this
 
----------------------------------------------------------------
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:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 340665)
    Time Spent: 6h 10m  (was: 6h)

> JsonRecordAvroSchemaToAvroConverter does not handle arrays of unions
> --------------------------------------------------------------------
>
>                 Key: GOBBLIN-933
>                 URL: https://issues.apache.org/jira/browse/GOBBLIN-933
>             Project: Apache Gobblin
>          Issue Type: Bug
>            Reporter: Ahmed Abdul Hamid
>            Priority: Major
>          Time Spent: 6h 10m
>  Remaining Estimate: 0h
>
> Using {{JsonRecordAvroSchemaToAvroConverter}} to convert an array of a union 
> type fails. For instance, using it with the following Avro schema:
> {code:java}
> {
>   "name": "arrayField",
>   "type": {
>     "type": "array",
>     "items": ["string", "null"]
>   }
> } {code}
> yields the following error:
> {code:java}
> java.lang.StackOverflowError
>       at org.apache.gobblin.configuration.State.getProp(State.java)
>       at 
> org.apache.gobblin.configuration.WorkUnitState.getProp(WorkUnitState.java:333)
>       at 
> org.apache.gobblin.converter.avro.JsonElementConversionFactory.getConvertor(JsonElementConversionFactory.java:106)
>       at 
> org.apache.gobblin.converter.avro.JsonElementConversionFactory$UnionConverter.getConverter(JsonElementConversionFactory.java:737)
>       at 
> org.apache.gobblin.converter.avro.JsonElementConversionFactory$UnionConverter.<init>(JsonElementConversionFactory.java:729)
>       at 
> org.apache.gobblin.converter.avro.JsonElementConversionFactory.getConvertor(JsonElementConversionFactory.java:160)
>       at 
> org.apache.gobblin.converter.avro.JsonElementConversionFactory$UnionConverter.getConverter(JsonElementConversionFactory.java:737)
>       at 
> org.apache.gobblin.converter.avro.JsonElementConversionFactory$UnionConverter.<init>(JsonElementConversionFactory.java:729)
>  {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to