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

avro.schema.LogicalSchema does not compare correctly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.11.1
    • None
    • python
    • None

    Description

      Say we create an avro logical type, e.g. Date:

      {"type": "int", "logicalType": "date"} 

      This schema can be created either by:
      1) providing the above JSON representation to `avro.schema.parse()`.
      2) call `make_logical_schema()`, which calls the underlying constructors.

      However, the two instances are not considered equal. Because 2) does not set the `logicalType` in props, while 1) does.

      Example:

      import avro.constants
      import avro.schema
      
      logical_type = avro.constants.DATE
      type_ = "int"
      other_props = None
      logical_schema = avro.schema.make_logical_schema(logical_type, type_, other_props or {})
      print(logical_schema)
      # Output: {"type": "int"}
      
      expected_schema_json = '{"type": "int", "logicalType": "date"}'
      expected_schema = avro.schema.parse(expected_schema_json)
      print(expected_schema.__eq__(logical_schema)) # Compare by EqualByPropsMixin
      
      # Output: False
      

      Patch:
      Below patch solves the issue for me. I'm unawave if this has unintended consequences or side effects, so please consider it a proposal rather than a given solution.

      import avro.constants
      import avro.schema
      
      class LogicalSchemaPatch(avro.schema.PropertiesMixin):
          '''Patch for avro.Schema.LogicalSchema that inherits from PropertiesMixin
          to be able to set logicalType prop.
          This fixes class comparison for logical types (which is done by props).
          '''
          def __init__(self, logical_type):
              self.set_prop("logicalType", logical_type)
              self.logical_type = logical_type
      avro.schema.LogicalSchema = LogicalSchemaPatch
      
      logical_type = avro.constants.DATE
      type_ = "int"
      other_props = None
      logical_schema = avro.schema.make_logical_schema(logical_type, type_, other_props or {})
      print(logical_schema)
      # Output: {"logicalType": "date", "type": "int"}
      
      expected_schema_json = '{"type": "int", "logicalType": "date"}'
      expected_schema = avro.schema.parse(expected_schema_json)
      print(expected_schema.__eq__(logical_schema)) # Compare by EqualByPropsMixin
      
      # Output: True
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            th0ger Martin Thøgersen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: