Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Invert case_sensitive logic in StructType #1147

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pyiceberg/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,13 @@ def field(self, field_id: int) -> Optional[NestedField]:

def field_by_name(self, name: str, case_sensitive: bool = True) -> Optional[NestedField]:
if case_sensitive:
name_lower = name.lower()
for field in self.fields:
if field.name.lower() == name_lower:
if field.name == name:
return field
else:
name_lower = name.lower()
for field in self.fields:
if field.name == name:
if field.name.lower() == name_lower:
return field
return None

Expand Down
14 changes: 14 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ def test_struct_type() -> None:
assert type_var == pickle.loads(pickle.dumps(type_var))


def test_struct_field_by_name() -> None:
lower_field = NestedField(1, "lower_case_field", IntegerType(), required=True)
upper_field = NestedField(2, "UPPER_CASE_FIELD", IntegerType(), required=True)
type_var = StructType(lower_field, upper_field)

assert type_var.field_by_name("lower_case_field", case_sensitive=False) == lower_field
assert type_var.field_by_name("upper_case_field", case_sensitive=False) == upper_field
assert type_var.field_by_name("nonexistent_field", case_sensitive=False) is None

assert type_var.field_by_name("lower_case_field", case_sensitive=True) == lower_field
assert type_var.field_by_name("upper_case_field", case_sensitive=True) is None
assert type_var.field_by_name("nonexistent_field", case_sensitive=True) is None


def test_list_type() -> None:
type_var = ListType(
1,
Expand Down