[ 
https://issues.apache.org/jira/browse/AVRO-3695?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17653115#comment-17653115
 ] 

Mark Farnan commented on AVRO-3695:
-----------------------------------

Digging into this further, the problem seems to be in the result of "to_value" 
on the struct. 

The current implementation produces this(FAIL):
{code:java}
Record([("item", Union(1, Record([("type", Enum(0, "Long")), ("value", Union(0, 
Long(34)))])))]){code}
 
Where as the correct value, that passes validation and works with to_avro_datum 
is this:
{code:java}
Record([("item", Union(1, Long(34)))]){code}
 

There seem to be two different issues with the Value being generated by 
'to-value'
 # It is creating TWO seperate unions.  One for the NULL/NotNull (Some/None),   
and one for the actuall Enum. 
 ## This has a secondary issue in that the index in the second enumeration is 
flat out wrong.  (Offset by one to the left.).  So even if allowed to pass by 
relaxing schema validation, the on-wire representation is incorrect.
 # The enumeration is being wrapped in another Record, which is not right. It's 
not a record, just a union.

 

I've made a temporary workaround to in ser.rs to change the way the enum is 
serialized, and removed the Some() structure from the table generated by 
avrogen-rs and added 'Null' to the top of the enum so the indexes are correct.  
 But this seems fragile and I'm not at all sure it is the correct way to solve 
it. 

This seems somewhat related to the issues with 'fixed',  in that the to_value 
code may need some hints in order to correctly create a value that schema 
validates. 
 

> [Rust] Can't validate schema with value when Nullable union has multiple 
> types.
> -------------------------------------------------------------------------------
>
>                 Key: AVRO-3695
>                 URL: https://issues.apache.org/jira/browse/AVRO-3695
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: rust
>            Reporter: Mark Farnan
>            Priority: Blocker
>              Labels: pull-request-available, rust
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> If a union has more than one 'type' (other than null),  to_avro_datum is 
> unable to validate the schema with the value type,  and throws a validation 
> error. 
>  
> Schema: 
>  
> {code:java}
> { "type": "record", "namespace": "datatypes", "name": "nullunion", "fields": 
> [ {"name": "item", "type": [ "null", "long", "double" ] } ] } {code}
>  
> Struct:  (This was generated by rsgen-avro)
> {code:java}
> /// Auto-generated type for unnamed Avro union variants.
> #[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
> pub enum UnionLongDouble {    Long(i64),    Double(f64),}
> #[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
> pub struct NullUnion {    pub item: Option<UnionLongDouble>,}{code}
>  
> {code:java}
> #[test]
> fn test_multivalue_union_tovalue() {
>     let nuschema = r#"
>     {
>         "type": "record",
>         "namespace": "datatypes",
>         "name": "nullunion",
>         "fields":
>         [
>             {"name": "item",
>                 "type": [
>                     "null",
>                     "long",
>                     "double"
>                 ]
>             }
>         ]
>     }
>     "#;   
>     let nullunion = NullUnion {
>         item: Some(UnionLongDouble::Long(34)),
>     };    let schema = Schema::parse_str(nuschema).unwrap();    
>     let nu_value = to_value(nullunion).unwrap();
>     let nu_encoded = to_avro_datum(&schema, nu_value).unwrap();
> } {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to