diff --git a/pyiceberg/transforms.py b/pyiceberg/transforms.py index 74925c695f..45c8ffd196 100644 --- a/pyiceberg/transforms.py +++ b/pyiceberg/transforms.py @@ -884,11 +884,9 @@ def truncate_func(v: Any) -> Any: def satisfies_order_of(self, other: Transform[S, T]) -> bool: if self == other: return True - elif ( - isinstance(self.source_type, StringType) - and isinstance(other, TruncateTransform) - and isinstance(other.source_type, StringType) - ): + elif isinstance(other, TruncateTransform): + # Matches the Java implementation: a truncation satisfies the order of any truncation with a + # width less than or equal to its own. return self.width >= other.width return False diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 998d8a224d..cc63aa8e35 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -513,6 +513,24 @@ def test_truncate_method(type_var: PrimitiveType, value: Any, expected_human_str assert truncate_transform.satisfies_order_of(truncate_transform) +# Ported from TestSortOrder#testSatisfiesTruncateFieldOrder in the Java reference implementation: +# https://github.com/apache/iceberg/blob/main/core/src/test/java/org/apache/iceberg/TestSortOrder.java +@pytest.mark.parametrize( + "transform,other,expected", + [ + (IdentityTransform(), TruncateTransform(2), True), + (TruncateTransform(2), IdentityTransform(), False), + (TruncateTransform(4), IdentityTransform(), False), + (TruncateTransform(4), TruncateTransform(2), True), + (TruncateTransform(2), TruncateTransform(4), False), + (TruncateTransform(2), TruncateTransform(2), True), + (TruncateTransform(3), BucketTransform(3), False), + ], +) +def test_truncate_satisfies_order_of(transform: Transform[Any, Any], other: Transform[Any, Any], expected: bool) -> None: + assert transform.satisfies_order_of(other) is expected + + def test_unknown_transform() -> None: unknown_transform = UnknownTransform("unknown") # type: ignore assert str(unknown_transform) == str(eval(repr(unknown_transform)))