From 7444f2679617e6cedf237ec020f80a1a481ee54b Mon Sep 17 00:00:00 2001 From: Gedalia Kliger <117381134+gedaliakliger@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:57:23 +0000 Subject: [PATCH] Write decimal JSON single values in plain notation str() renders a Decimal with a small magnitude in scientific notation, so a decimal(20, 8) default of zero was written to table metadata as 0E-8. The spec uses the digits after the decimal point to carry the scale, and Java writes toPlainString() for a non-negative scale. --- pyiceberg/conversions.py | 4 ++++ tests/test_conversions.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/pyiceberg/conversions.py b/pyiceberg/conversions.py index 6b3d84135b..919bf44f6d 100644 --- a/pyiceberg/conversions.py +++ b/pyiceberg/conversions.py @@ -506,6 +506,10 @@ def _(_: DecimalType, val: Decimal) -> str: decimal point is used to indicate scale, for values with a negative scale, the scientific notation is used and the exponent must equal the negated scale. """ + exponent = val.as_tuple().exponent + if isinstance(exponent, int) and exponent <= 0: + # str() switches to scientific notation for small values, such as 0E-8 + return f"{val:f}" return str(val) diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 8a6a8d3c84..8eb1b30604 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -578,6 +578,9 @@ def test_datetime_obj_to_bytes(primitive_type: PrimitiveType, value: datetime | (DecimalType(9, 4), Decimal("123.4500"), "123.4500"), (DecimalType(9, 0), Decimal("2"), "2"), (DecimalType(9, 9), Decimal("0.123456789"), "0.123456789"), + (DecimalType(20, 8), Decimal("0.00000000"), "0.00000000"), + (DecimalType(20, 8), Decimal("-0.00000012"), "-0.00000012"), + (DecimalType(38, 18), Decimal("0.000000000000000001"), "0.000000000000000001"), (DateType(), date(2017, 11, 16), "2017-11-16"), (TimeType(), time(22, 31, 8, 123456), "22:31:08.123456"), (TimestampType(), datetime(2017, 11, 16, 22, 31, 8, 123456), "2017-11-16T22:31:08.123456"),