Skip to content

Commit f26689a

Browse files
Write decimal default values in plain notation (#4420)
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.
1 parent 8f6ea30 commit f26689a

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

‎pyiceberg/conversions.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ def _(_: DecimalType, val: Decimal) -> str:
524524
decimal point is used to indicate scale, for values with a negative scale,
525525
the scientific notation is used and the exponent must equal the negated scale.
526526
"""
527+
exponent = val.as_tuple().exponent
528+
if isinstance(exponent, int) and exponent <= 0:
529+
# str() switches to scientific notation for small values, such as 0E-8
530+
return f"{val:f}"
527531
return str(val)
528532

529533

‎tests/test_conversions.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,9 @@ def test_datetime_obj_to_bytes(primitive_type: PrimitiveType, value: datetime |
603603
(DecimalType(9, 4), Decimal("123.4500"), "123.4500"),
604604
(DecimalType(9, 0), Decimal("2"), "2"),
605605
(DecimalType(9, 9), Decimal("0.123456789"), "0.123456789"),
606+
(DecimalType(20, 8), Decimal("0.00000000"), "0.00000000"),
607+
(DecimalType(20, 8), Decimal("-0.00000012"), "-0.00000012"),
608+
(DecimalType(38, 18), Decimal("0.000000000000000001"), "0.000000000000000001"),
606609
(DateType(), date(2017, 11, 16), "2017-11-16"),
607610
(TimeType(), time(22, 31, 8, 123456), "22:31:08.123456"),
608611
(TimestampType(), datetime(2017, 11, 16, 22, 31, 8, 123456), "2017-11-16T22:31:08.123456"),

0 commit comments

Comments
 (0)