[ 
https://issues.apache.org/jira/browse/AVRO-361?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Scott Carey updated AVRO-361:
-----------------------------

    Attachment: AVRO-361.patch

This patch fixes this issue, and additionally checks for named types with 
conflicting names.

The unit test is fairly exhaustive, testing that both the Schema.parse() API 
and the Schema.create() API fail with improper unions.
It is checked that duplicate named types with different names are valid, and 
that duplicate types with conflicting names fail.


Note:
I'm not sure when the "induce" flag for the check() method in TestSchema.java 
is valid to use, but in the additional test in this patch, if the "induce" flag 
is set, the test will fail.  
I am not sure if that is supposed to work for this case or not, but if it is 
supposed to work, then there is another bug somewhere else.

> Specific Compiler fails to handle union with two fixed branches
> ---------------------------------------------------------------
>
>                 Key: AVRO-361
>                 URL: https://issues.apache.org/jira/browse/AVRO-361
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>            Reporter: Scott Carey
>            Priority: Blocker
>             Fix For: 1.3.0
>
>         Attachments: AVRO-361.patch, AVRO-361.patch
>
>
> The following record fails to compile with the specific compiler:
> {code}
> {"name": "ipAddr", "type": "record", "fields":[
>   {"name": "addr", "type": [
>     {"name": "IPv6", "type": "fixed", "size": 16 },
>     {"name": "IPv4", "type": "fixed", "size": 4 }]
>   }
>  ]
> }   
> {code}
> The stack trace is:
> {noformat}
> org.apache.avro.AvroRuntimeException: Ambiguous union: 
> [{"type":"fixed","name":"IPv6","size":16},{"type":"fixed","name":"IPv4","size":4}]
>       at org.apache.avro.Schema$UnionSchema.<init>(Schema.java:613)
>       at org.apache.avro.Schema.parse(Schema.java:874)
>       at org.apache.avro.Schema.parse(Schema.java:825)
>       at org.apache.avro.Schema.parse(Schema.java:709)
>       at 
> org.apache.avro.specific.SpecificCompiler.compileSchema(SpecificCompiler.java:111)\
> {noformat}
> This is on trunk:
> $ svn info
> Path: .
> URL: http://svn.apache.org/repos/asf/hadoop/avro/trunk/lang/java
> Repository Root: http://svn.apache.org/repos/asf
> Repository UUID: 13f79535-47bb-0310-9956-ffa450edef68
> Revision: 901469
> The code for UnionSchema in Schema.java has this constructor: 
> {code}
> public UnionSchema(List<Schema> types) {
>       super(Type.UNION);
>       this.types = types;
>       int seen = 0;
>       for (Schema type : types) {                 // check legality of union
>         switch (type.getType()) {
>         case UNION: 
>           throw new AvroRuntimeException("Nested union: "+this);
>         case RECORD:
>           if (type.getName() != null)
>             continue;
>         default:
>           int mask = 1 << type.getType().ordinal();
>           if ((seen & mask) != 0)
>             throw new AvroRuntimeException("Ambiguous union: "+this);
>           seen |= mask;
>         }
>       }
>     }
> {code}
> That allows only one member of any type other than RECORD.  The spec says:
> {quote}
> Unions may not contain more than one schema with the same
> type, except for the named types record, fixed and enum.
> {quote}
> The code above does not adhere to this.
> I am attaching a patch for only this code, but a unit test with a test schema 
> that has two records, two fixed, and two enum in it as well as one of each of 
> the unnamed types is probably necessary as well.  I am not yet familiar with 
> the test infrastructure.
> I am also not familiar with what else this may impact.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to