Skip to content
Open
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
11 changes: 1 addition & 10 deletions pyiceberg/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,6 @@ class TruncateTransform(Transform[S, S]):
"""

root: str = Field()
_source_type: IcebergType = PrivateAttr()
_width: PositiveInt = PrivateAttr()

def __init__(self, width: int, **data: Any):
Expand All @@ -796,10 +795,6 @@ def result_type(self, source: IcebergType) -> IcebergType:
def preserves_order(self) -> bool:
return True

@property
def source_type(self) -> IcebergType:
return self._source_type

def project(self, name: str, pred: BoundPredicate) -> UnboundPredicate | None:
field_type = pred.term.ref().field.field_type

Expand Down Expand Up @@ -896,11 +891,7 @@ def truncate_func(v: Any) -> Any:
def satisfies_order_of(self, other: Transform[S, T]) -> bool:
if self == other:
return True
elif (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't appear that we use source_type anywhere after this change. Can you take this opportunity to remove it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Took this opportunity to clean up _source_type and the source_type property as well, and expanded the unit test suite to verify cross-transform comparisons and attribute cleanup.

Thanks for the review, @rambleraptor!

isinstance(self.source_type, StringType)
and isinstance(other, TruncateTransform)
and isinstance(other.source_type, StringType)
):
elif isinstance(other, TruncateTransform):
return self.width >= other.width

return False
Expand Down
26 changes: 26 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,32 @@ def test_truncate_method(type_var: PrimitiveType, value: Any, expected_human_str
assert truncate_transform.satisfies_order_of(truncate_transform)


def test_truncate_satisfies_order_of() -> None:
# Width comparison
assert TruncateTransform(5).satisfies_order_of(TruncateTransform(3))
assert TruncateTransform(5).satisfies_order_of(TruncateTransform(5))
assert not TruncateTransform(3).satisfies_order_of(TruncateTransform(5))
assert TruncateTransform(10).satisfies_order_of(TruncateTransform(1))
assert not TruncateTransform(1).satisfies_order_of(TruncateTransform(10))
assert TruncateTransform(1).satisfies_order_of(TruncateTransform(1))

# Cross-transform comparisons
assert not TruncateTransform(5).satisfies_order_of(BucketTransform(5))
assert not TruncateTransform(5).satisfies_order_of(IdentityTransform())
assert not TruncateTransform(5).satisfies_order_of(VoidTransform())
assert not TruncateTransform(5).satisfies_order_of(DayTransform())
assert not TruncateTransform(5).satisfies_order_of(YearTransform())
assert not TruncateTransform(5).satisfies_order_of(UnknownTransform("unknown"))

# Identity naturally satisfies TruncateTransform because Truncate preserves order
assert IdentityTransform().satisfies_order_of(TruncateTransform(5))

# Verify unused source_type was cleanly removed
t = TruncateTransform(5)
assert not hasattr(t, "source_type")
assert not hasattr(t, "_source_type")


def test_unknown_transform() -> None:
unknown_transform = UnknownTransform("unknown") # type: ignore
assert str(unknown_transform) == str(eval(repr(unknown_transform)))
Expand Down