Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1777

Select best matching record when writing a union in python

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Reopened
    • Major
    • Resolution: Unresolved
    • 1.7.7
    • None
    • python
    • None

    Description

      Unlike javascript, python is not using wrapped types.
      So when writing a union it needs to guess find out which type it will output.
      At the moment it takes the last validating type.

      I propose to take the type with the most matching fields.

      So I propose to change in io.py:

      # resolve union
      index_of_schema = -1
      for i, candidate_schema in enumerate(writers_schema.schemas):
        if validate(candidate_schema, datum):
          index_of_schema = i
      if index_of_schema < 0: raise AvroTypeException(writers_schema, datum)
      

      into

      # resolve union
      index_of_schema = -1
      found_fields = -1
      for i, candidate_schema in enumerate(writers_schema.schemas):
        if validate(candidate_schema, datum):
          nr_fields = candidate_schema.type in ['record', 'error', 'request'] and len(candidate_schema.fields) or 1
          if nr_fields > found_fields:
            index_of_schema = i
            found_fields = nr_fields
      if index_of_schema < 0: raise AvroTypeException(writers_schema, datum)
      

      If you want, I can create a pull request for this. And apply it both on py3 as py.

      Attachments

        Activity

          People

            dkulp Daniel Kulp
            steven.aerts Steven Aerts
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated: