Skip to content

Commit 07099ee

Browse files
kevinjqliuCopilot
andauthored
Fix schema equality for empty schemas and identifier fields (#3958)
* Fix schema equality for empty schemas and identifier fields Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Use loops for schema equality regression cases Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Keep schema equality tests focused on regressions Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent dc4e3f8 commit 07099ee

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎pyiceberg/schema.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,13 @@ def __len__(self) -> int:
115115

116116
def __eq__(self, other: Any) -> bool:
117117
"""Return the equality of two instances of the Schema class."""
118-
if not other:
119-
return False
120-
121118
if not isinstance(other, Schema):
122119
return False
123120

124121
if len(self.columns) != len(other.columns):
125122
return False
126123

127-
identifier_field_ids_is_equal = self.identifier_field_ids == other.identifier_field_ids
124+
identifier_field_ids_is_equal = set(self.identifier_field_ids) == set(other.identifier_field_ids)
128125
schema_is_equal = all(lhs == rhs for lhs, rhs in zip(self.columns, other.columns, strict=True))
129126

130127
return identifier_field_ids_is_equal and schema_is_equal

‎tests/test_schema.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ def test_schema_repr_two_fields() -> None:
119119
assert expected == actual
120120

121121

122+
def test_empty_schema_equality() -> None:
123+
assert Schema() == Schema()
124+
125+
126+
def test_schema_equality_ignores_identifier_field_order() -> None:
127+
fields = (
128+
NestedField(field_id=1, name="foo", field_type=LongType(), required=True),
129+
NestedField(field_id=2, name="bar", field_type=LongType(), required=True),
130+
)
131+
assert Schema(*fields, identifier_field_ids=[1, 2]) == Schema(*fields, identifier_field_ids=[2, 1])
132+
133+
122134
def test_schema_raise_on_duplicate_names() -> None:
123135
"""Test schema representation"""
124136
with pytest.raises(ValueError) as exc_info:

0 commit comments

Comments
 (0)