diff --git a/pyiceberg/transforms.py b/pyiceberg/transforms.py index 45c8ffd196..48df94b8ce 100644 --- a/pyiceberg/transforms.py +++ b/pyiceberg/transforms.py @@ -20,6 +20,7 @@ import struct from abc import ABC, abstractmethod from collections.abc import Callable +from decimal import Decimal from enum import IntEnum from functools import singledispatch from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar @@ -914,6 +915,16 @@ def _human_string(value: Any, _type: IcebergType) -> str: return str(value) +@_human_string.register(Decimal) +def _(value: Decimal, _type: IcebergType) -> str: + # as_tuple's exponent is the string 'n'/'N'/'F' for NaN and Infinity specials + exponent = value.as_tuple().exponent + if isinstance(exponent, int) and exponent <= 0: + # str() switches to scientific notation for small magnitudes, such as 0E-8 + return f"{value:f}" + return str(value) + + @_human_string.register(bytes) def _(value: bytes, _type: IcebergType) -> str: return _base64encode(value) diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 6ab7b52c4c..3674c1b9c0 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -420,6 +420,10 @@ def test_satisfies_order_of_method(transform: TimeTransform[Any], other_transfor (LongType(), -1234567890000, "-1234567890000"), (StringType(), "a/b/c=d", "a/b/c=d"), (DecimalType(9, 2), Decimal("-1.50"), "-1.50"), + (DecimalType(20, 8), Decimal("0.00000012"), "0.00000012"), + (DecimalType(20, 8), Decimal("0.00000000"), "0.00000000"), + (DecimalType(20, 8), Decimal("-0.00000012"), "-0.00000012"), + (DecimalType(38, 18), Decimal("0.000000000000000001"), "0.000000000000000001"), (FixedType(100), b"foo", "Zm9v"), ], )