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
Decimal truncate transform can produce values that exceed the source precision #17027
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:
CREATETABLEsample (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
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:
Clamp the transform output to the lowest/highest in-range value. @aykut-bozkurt preferred this because it also avoids incorrectly skipping files during filter pushdown.
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.
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.
I would like us to determine the spec text rather than keep writing up PR's
to change the code base. Let's see if we can describe what we want to do at
the spec level and have the debate there. If we can figure out something
that doesn' break previous implementations unless they were already going
to break in that scenario would be ideal for me.
Activity
wombatu-kun commentedon Jul 1, 2026
Traced the root cause on current
main.truncate's result type is the source type unchanged (Truncate.getResultType), andTruncateUtil.truncateDecimaltruncates 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.999with width 10 becomes-10000.000(precision 8). The partition field is still typeddecimal(7,3), soDecimalUtil.toReusedFixLengthBytesrejects 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:max(minTruncatedPrecision, P)(API: Compute truncate decimal result precision based on lowest value bound #12969's approach). @RussellSpitzer noted this needs a spec change and that a fixed "+1 precision" is not always enough for large widths.It is also not decimal-specific.
int/longtruncate overflow the same way near their range limits (@nandorKollar'sInteger.MIN_VALUEexample 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.
nandorKollar commentedon Jul 2, 2026
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.
RussellSpitzer commentedon Jul 2, 2026