You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Gaurav <ga...@gmail.com> on 2011/12/08 15:07:06 UTC

Python Union Behavior not consistent

Hi,

I was using following schema to encode some data and found that while python
integers are written as double, which is not correct. Atleast it should
respect the order in which union is written.
{"type":"map","values":["int","long","float","double","string","boolean"]}


A small change in io.py can fix this:

  def write_union(self, writers_schema, datum, encoder):
    """
    A union is encoded by first writing a long value indicating
    the zero-based position within the union of the schema of its value.
    The value is then encoded per the indicated schema within the union.
    """
    # resolve union
    index_of_schema = -1
    for i, candidate_schema in enumerate(writers_schema.schemas):
      if validate(candidate_schema, datum):
        index_of_schema = i
*        break*
    if index_of_schema < 0: raise AvroTypeException(writers_schema, datum)

Please include this, if it makes sense.

Thanks,
Gaurav Nanda

--
View this message in context: http://apache-avro.679487.n3.nabble.com/Python-Union-Behavior-not-consistent-tp3570352p3570352.html
Sent from the Avro - Users mailing list archive at Nabble.com.

Re: Python Union Behavior not consistent

Posted by Gaurav Nanda <ga...@gmail.com>.
Logged AVRO#973.

On Sat, Dec 10, 2011 at 6:10 AM, Doug Cutting <cu...@apache.org> wrote:
> Gaurav,
>
> This looks like a good change.  Can you please file an issue in Jira?
>
> Thanks,
>
> Doug
>
> On 12/08/2011 06:07 AM, Gaurav wrote:
>> Hi,
>>
>> I was using following schema to encode some data and found that while python
>> integers are written as double, which is not correct. Atleast it should
>> respect the order in which union is written.
>> {"type":"map","values":["int","long","float","double","string","boolean"]}
>>
>>
>> A small change in io.py can fix this:
>>
>>   def write_union(self, writers_schema, datum, encoder):
>>     """
>>     A union is encoded by first writing a long value indicating
>>     the zero-based position within the union of the schema of its value.
>>     The value is then encoded per the indicated schema within the union.
>>     """
>>     # resolve union
>>     index_of_schema = -1
>>     for i, candidate_schema in enumerate(writers_schema.schemas):
>>       if validate(candidate_schema, datum):
>>         index_of_schema = i
>> *        break*
>>     if index_of_schema < 0: raise AvroTypeException(writers_schema, datum)
>>
>> Please include this, if it makes sense.
>>
>> Thanks,
>> Gaurav Nanda
>>
>> --
>> View this message in context: http://apache-avro.679487.n3.nabble.com/Python-Union-Behavior-not-consistent-tp3570352p3570352.html
>> Sent from the Avro - Users mailing list archive at Nabble.com.

Re: Python Union Behavior not consistent

Posted by Doug Cutting <cu...@apache.org>.
Gaurav,

This looks like a good change.  Can you please file an issue in Jira?

Thanks,

Doug

On 12/08/2011 06:07 AM, Gaurav wrote:
> Hi,
> 
> I was using following schema to encode some data and found that while python
> integers are written as double, which is not correct. Atleast it should
> respect the order in which union is written.
> {"type":"map","values":["int","long","float","double","string","boolean"]}
> 
> 
> A small change in io.py can fix this:
> 
>   def write_union(self, writers_schema, datum, encoder):
>     """
>     A union is encoded by first writing a long value indicating
>     the zero-based position within the union of the schema of its value.
>     The value is then encoded per the indicated schema within the union.
>     """
>     # resolve union
>     index_of_schema = -1
>     for i, candidate_schema in enumerate(writers_schema.schemas):
>       if validate(candidate_schema, datum):
>         index_of_schema = i
> *        break*
>     if index_of_schema < 0: raise AvroTypeException(writers_schema, datum)
> 
> Please include this, if it makes sense.
> 
> Thanks,
> Gaurav Nanda
> 
> --
> View this message in context: http://apache-avro.679487.n3.nabble.com/Python-Union-Behavior-not-consistent-tp3570352p3570352.html
> Sent from the Avro - Users mailing list archive at Nabble.com.