Skip to content

Decimal truncate transform can produce values that exceed the source precision #17027

Open

Description

@res-life

Apache Iceberg version

1.9.0

Query engine

Spark

Please describe the bug 🐞

This is a follow-up to #12915, which was automatically closed as stale without a resolution.

The decimal truncate transform can increase the precision of a negative boundary value beyond the precision of the source decimal type. For example, truncating a valid decimal(7,3) value with width 10 maps:

-9999.999 -> -10000.000

The input has precision 7, while the transformed value requires precision 8. A valid source value can therefore produce a partition value that cannot be represented by the transform's declared decimal type.

A minimal Spark SQL reproduction is:

CREATE TABLE sample (a decimal(7,3))
USING iceberg
PARTITIONED BY (truncate(10, a));

INSERT INTO sample VALUES (-9999.999);

The write fails when Iceberg serializes the transformed partition value:

Caused by: java.lang.IllegalArgumentException:
Cannot write value as decimal(7,3), too large: -10000.000
    at org.apache.iceberg.util.DecimalUtil.toReusedFixLengthBytes(DecimalUtil.java:39)
    at org.apache.iceberg.avro.ValueWriters$DecimalWriter.write(ValueWriters.java:345)

The same behavior occurs for other precisions and scales when a negative value near the minimum boundary is rounded down by the truncate transform, such as the decimal(5,4) example in #12915.

Iceberg should handle this boundary case without allowing an unrepresentable transformed value to reach the metadata writer. This may require defining how decimal truncate behaves when the mathematical result exceeds the source precision.

Willingness to contribute

  • I can contribute a fix for this bug independently
  • I would be willing to contribute a fix for this bug with guidance from the Iceberg community
  • I cannot contribute a fix for this bug at this time

Activity

  1. wombatu-kun commented on Jul 1, 2026

    @wombatu-kun
    Contributor

    Traced the root cause on current main. truncate's result type is the source type unchanged (Truncate.getResultType), and TruncateUtil.truncateDecimal truncates toward negative infinity with no bounds check, so the most-negative representable value can floor to one integer digit beyond the source precision: decimal(7,3) value -9999.999 with width 10 becomes -10000.000 (precision 8). The partition field is still typed decimal(7,3), so DecimalUtil.toReusedFixLengthBytes rejects the value at write time and the append aborts.

    This was previously worked in #12969 (a follow-up to #12915), which was closed as stale without converging. The blocker is that any fix is a spec change: the spec currently pins truncate's result type to "Source type" and the decimal arithmetic to v - (((v % W) + W) % W). Three directions were floated there, each a spec change:

    It is also not decimal-specific. int/long truncate overflow the same way near their range limits (@nandorKollar's Integer.MIN_VALUE example on #12969), tracked separately in #13105, which was likewise closed as stale with the same "probably needs a spec change" conclusion.

    Given three prior stale threads on the same underlying problem, it would help to settle the direction before writing code: widen vs clamp vs reject, and decimal-only vs all numeric types. Since each option changes the spec, this likely needs a short dev@ discussion or a maintainer decision on #12969. Happy to implement once there is a direction.

  2. nandorKollar commented on Jul 2, 2026

    @nandorKollar
    Contributor

    The overflow problem has actually been reported multiple times (#9767, #12915, #13105), and I don't see how we can fix it in Iceberg without dropping the current truncate function and replacing it with an overflow-resistant version. The scariest part of this issue is that query engines (at least Spark and Hive) return wrong results instead of failing.

  3. RussellSpitzer commented on Jul 2, 2026

    @RussellSpitzer
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions