Skip to content

Commit

Permalink
Fix TruncateTransform for falsey values (apache#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungwy authored Jan 18, 2024
1 parent 06e2b2d commit 8b660f8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyiceberg/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def truncate_func(v: Any) -> Any:
else:
raise ValueError(f"Cannot truncate for type: {source}")

return lambda v: truncate_func(v) if v else None
return lambda v: truncate_func(v) if v is not None else None

def satisfies_order_of(self, other: Transform[S, T]) -> bool:
if self == other:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def test_identity_method(type_var: PrimitiveType) -> None:
@pytest.mark.parametrize("type_var", [IntegerType(), LongType()])
@pytest.mark.parametrize(
"input_var,expected",
[(1, 0), (5, 0), (9, 0), (10, 10), (11, 10), (-1, -10), (-10, -10), (-12, -20)],
[(1, 0), (5, 0), (9, 0), (10, 10), (11, 10), (-1, -10), (-10, -10), (-12, -20), (0, 0)],
)
def test_truncate_integer(type_var: PrimitiveType, input_var: int, expected: int) -> None:
trunc = TruncateTransform(10) # type: ignore
Expand All @@ -377,14 +377,15 @@ def test_truncate_integer(type_var: PrimitiveType, input_var: int, expected: int
(Decimal("12.29"), Decimal("12.20")),
(Decimal("0.05"), Decimal("0.00")),
(Decimal("-0.05"), Decimal("-0.10")),
(Decimal("0.0"), Decimal("0.0")),
],
)
def test_truncate_decimal(input_var: Decimal, expected: Decimal) -> None:
trunc = TruncateTransform(10) # type: ignore
assert trunc.transform(DecimalType(9, 2))(input_var) == expected


@pytest.mark.parametrize("input_var,expected", [("abcdefg", "abcde"), ("abc", "abc")])
@pytest.mark.parametrize("input_var,expected", [("abcdefg", "abcde"), ("abc", "abc"), ("", "")])
def test_truncate_string(input_var: str, expected: str) -> None:
trunc = TruncateTransform(5) # type: ignore
assert trunc.transform(StringType())(input_var) == expected
Expand Down

0 comments on commit 8b660f8

Please sign in to comment.