You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Martin Thøgersen (Jira)" <ji...@apache.org> on 2022/08/23 07:36:00 UTC

[jira] [Created] (AVRO-3615) avro.schema.LogicalSchema does not compare correctly

Martin Thøgersen created AVRO-3615:
--------------------------------------

             Summary: avro.schema.LogicalSchema does not compare correctly
                 Key: AVRO-3615
                 URL: https://issues.apache.org/jira/browse/AVRO-3615
             Project: Apache Avro
          Issue Type: Bug
          Components: python
    Affects Versions: 1.11.1
            Reporter: Martin Thøgersen


Say we create an avro logical type, e.g. [Date|https://avro.apache.org/docs/1.11.1/specification/#date]:
{code:json}
{"type": "int", "logicalType": "date"} {code}
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:*
{code:python}
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
{code}
*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.
{code:python}
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
{code}



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