You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Float/Double literal to Decimal rounds on the exact binary value instead of the shortest decimal repr #4441
FloatLiteral.to(DecimalType) and DoubleLiteral.to(DecimalType) convert the float with Decimal(self.value), which uses the exact binary expansion of the double. Values whose decimal spelling ends in 5 then round the wrong way under ROUND_HALF_UP:
Match Apache Iceberg Java, which converts with BigDecimal.valueOf(value).setScale(scale, RoundingMode.HALF_UP) in Literals.java (all four numeric literal types). BigDecimal.valueOf(double) uses Double.toString, i.e. the shortest decimal that round-trips to the double, so 2.675 quantizes to 2.68. Spark and Postgres agree (2.675 casts to 2.68).
Root cause
pyiceberg/expressions/literals.py, FloatLiteral.to and DoubleLiteral.to for DecimalType:
Decimal(2.675) is 2.67499999999999982..., which quantizes down to 2.67. Using Decimal(str(self.value)) instead gives Decimal("2.675"), which quantizes to 2.68, matching BigDecimal.valueOf.
Hi @Shahzeb-DataEngineer, I have a fix ready for this (using Decimal(str(value)) in both FloatLiteral and DoubleLiteral, with regression tests). I’ll open a PR, unless you’ve already started one
Activity
Ankitraj-builds commentedon Nov 20, 2026
Hi @Shahzeb-DataEngineer, I have a fix ready for this (using Decimal(str(value)) in both FloatLiteral and DoubleLiteral, with regression tests). I’ll open a PR, unless you’ve already started one
Ankitraj-builds commentedon Nov 20, 2026
Opened #4442 for this. @Fokko could you take a look when you have time?