johnclara opened a new issue, #22574:
URL: https://github.com/apache/beam/issues/22574

   ### What happened?
   
   Hello, I am new to beam so I am probably doing something wrong:
   
   I am trying to create PCollections of a SpecificRecord which can convert 
between Rows and the java generated class.
   
   For fields with unions that are non-optional, I am hitting exceptiosn:
   
   Here is a test avro schema:
   ```
   {
     "name": "MultiUnionSpecificRecord",
     "namespace": "specificrecord",
     "type": "record",
     "doc": "",
     "fields": [
       {
         "name": "foo",
         "type": [ "int", "string" ],
         "doc": ""
       }
     ]
   }
   ```
   
   Using this to generate the MultiUnionSpecificRecord class:
   ```
   java -jar .avro-tools-1.9.0.jar compile -string -dateTimeLogicalTypeImpl 
joda schema \
   src/test/resources/MultiUnionSpecificRecord.avsc \
   src/test/java/specificrecord/
   ```
   
   Running this test:
   ```
       MultiUnionSpecificRecord record = MultiUnionSpecificRecord
           .newBuilder()
           .setFoo(0)
           .build();
   
       Pipeline p = TestPipeline.create().enableAbandonedNodeEnforcement(false);
   
       PCollection<MultiUnionSpecificRecord> events = p.apply(
           Create.<MultiUnionSpecificRecord>of(record)
               .withSchema(
                   
AvroUtils.toBeamSchema(specificrecord.MultiUnionSpecificRecord.getClassSchema()),
                   
TypeDescriptor.of(specificrecord.MultiUnionSpecificRecord.class),
                   
AvroUtils.getToRowFunction(specificrecord.MultiUnionSpecificRecord.class, 
specificrecord.MultiUnionSpecificRecord.getClassSchema()),
                   
AvroUtils.getFromRowFunction(specificrecord.MultiUnionSpecificRecord.class)
               )
       );
       System.out.println("Events Schema: " + events.getSchema());
       p.run();
    ```
    
    Hitting this exception:
    ```
        class java.lang.String cannot be cast to class 
org.apache.beam.sdk.schemas.logicaltypes.OneOfType$Value (java.lang.String is 
in module java.base of loader 'bootstrap'; 
org.apache.beam.sdk.schemas.logicaltypes.OneOfType$Value is in unnamed module 
of loader 'app')
        java.lang.ClassCastException: class java.lang.String cannot be cast to 
class org.apache.beam.sdk.schemas.logicaltypes.OneOfType$Value 
(java.lang.String is in module java.base of loader 'bootstrap'; 
org.apache.beam.sdk.schemas.logicaltypes.OneOfType$Value is in unnamed module 
of loader 'app')
                at 
org.apache.beam.sdk.schemas.GetterBasedSchemaProvider$RowValueGettersFactory$GetOneOf.convert(GetterBasedSchemaProvider.java:311)
                at 
org.apache.beam.sdk.schemas.GetterBasedSchemaProvider$RowValueGettersFactory$Converter.get(GetterBasedSchemaProvider.java:346)
                at 
org.apache.beam.sdk.values.RowWithGetters.getValue(RowWithGetters.java:71)
                at 
org.apache.beam.sdk.coders.RowCoderGenerator$EncodeInstruction.encodeDelegate(RowCoderGenerator.java:325)
                at 
org.apache.beam.sdk.coders.Coder$ByteBuddy$ZBaqkdOF.encode(Unknown Source
    ```
    
    Relevant versions:
    Java 11
    ```
    org.apache.beam:beam-sdks-java-core:2.40.0
    ...
    org.apache.avro:avro:1.8.2 -> 1.9.2
    ```
   
   ### Issue Priority
   
   Priority: 3
   
   ### Issue Component
   
   Component: sdk-java-core


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