This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 48121fe4d0 [core] Fix silent decimal overflow in sum aggregation 
(#11308)
48121fe4d0 is described below

commit 48121fe4d092f5ed1d657e933a4a6331b241ad1d
Author: Dapeng Sun(孙大鹏) <[email protected]>
AuthorDate: Sun Nov 22 18:54:33 2026 +0800

    [core] Fix silent decimal overflow in sum aggregation (#11308)
---
 .../primary-key-table/merge-engine/aggregation.mdx |  1 +
 .../java/org/apache/paimon/utils/DecimalUtils.java | 18 +++--
 .../java/org/apache/paimon/data/DecimalTest.java   | 47 +++++++++++++
 .../mergetree/compact/aggregate/FieldSumAgg.java   | 20 ++++++
 .../compact/aggregate/FieldAggregatorTest.java     | 79 ++++++++++++++++++++++
 5 files changed, 161 insertions(+), 4 deletions(-)

diff --git a/docs/docs/primary-key-table/merge-engine/aggregation.mdx 
b/docs/docs/primary-key-table/merge-engine/aggregation.mdx
index b0ec024956..78b84a55f4 100644
--- a/docs/docs/primary-key-table/merge-engine/aggregation.mdx
+++ b/docs/docs/primary-key-table/merge-engine/aggregation.mdx
@@ -97,6 +97,7 @@ Check [Retraction](#retraction) before accepting 
`UPDATE_BEFORE` or `DELETE` rec
   It supports DECIMAL, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, and DOUBLE 
data types.
 
   For TINYINT, SMALLINT, INTEGER, and BIGINT, overflows are ignored by default.
+  For DECIMAL, a precision overflow makes the aggregated value NULL by default.
   Set `fields.<field-name>.sum.fail-on-overflow` to `true` to throw an 
exception on overflow.
 
 ### product
diff --git 
a/paimon-common/src/main/java/org/apache/paimon/utils/DecimalUtils.java 
b/paimon-common/src/main/java/org/apache/paimon/utils/DecimalUtils.java
index 935f1edfce..425b2f5bd7 100644
--- a/paimon-common/src/main/java/org/apache/paimon/utils/DecimalUtils.java
+++ b/paimon-common/src/main/java/org/apache/paimon/utils/DecimalUtils.java
@@ -53,15 +53,20 @@ public class DecimalUtils {
         if (v1.isCompact()
                 && v2.isCompact()
                 && v1.scale() == v2.scale()
+                && scale == v1.scale()
                 && Decimal.isCompact(precision)) {
-            assert scale == v1.scale(); // no need to rescale
             try {
                 long ls =
                         Math.addExact(v1.toUnscaledLong(), 
v2.toUnscaledLong()); // checks overflow
-                return Decimal.fromUnscaledLong(ls, precision, scale);
+                if (ls < POW10[precision] && ls > -POW10[precision]) {
+                    return Decimal.fromUnscaledLong(ls, precision, scale);
+                }
             } catch (ArithmeticException e) {
                 // overflow, fall through
             }
+            // ls is the exact unscaled result at the target scale: beyond the 
precision
+            // bound the BigDecimal path below would return null anyway
+            return null;
         }
         BigDecimal bd = v1.toBigDecimal().add(v2.toBigDecimal());
         return fromBigDecimal(bd, precision, scale);
@@ -71,16 +76,21 @@ public class DecimalUtils {
         if (v1.isCompact()
                 && v2.isCompact()
                 && v1.scale() == v2.scale()
+                && scale == v1.scale()
                 && Decimal.isCompact(precision)) {
-            assert scale == v1.scale(); // no need to rescale
             try {
                 long ls =
                         Math.subtractExact(
                                 v1.toUnscaledLong(), v2.toUnscaledLong()); // 
checks overflow
-                return fromUnscaledLong(ls, precision, scale);
+                if (ls < POW10[precision] && ls > -POW10[precision]) {
+                    return fromUnscaledLong(ls, precision, scale);
+                }
             } catch (ArithmeticException e) {
                 // overflow, fall through
             }
+            // ls is the exact unscaled result at the target scale: beyond the 
precision
+            // bound the BigDecimal path below would return null anyway
+            return null;
         }
         BigDecimal bd = v1.toBigDecimal().subtract(v2.toBigDecimal());
         return fromBigDecimal(bd, precision, scale);
diff --git 
a/paimon-common/src/test/java/org/apache/paimon/data/DecimalTest.java 
b/paimon-common/src/test/java/org/apache/paimon/data/DecimalTest.java
index a236e94391..70b95a8f61 100644
--- a/paimon-common/src/test/java/org/apache/paimon/data/DecimalTest.java
+++ b/paimon-common/src/test/java/org/apache/paimon/data/DecimalTest.java
@@ -28,6 +28,7 @@ import static 
org.apache.paimon.utils.DecimalUtils.castToBoolean;
 import static org.apache.paimon.utils.DecimalUtils.castToDecimal;
 import static org.apache.paimon.utils.DecimalUtils.castToIntegral;
 import static org.apache.paimon.utils.DecimalUtils.doubleValue;
+import static org.apache.paimon.utils.DecimalUtils.subtract;
 import static org.assertj.core.api.Assertions.assertThat;
 
 /** Test for {@link Decimal}. */
@@ -105,6 +106,52 @@ public class DecimalTest {
         Decimal decimal4 = Decimal.fromBigDecimal(new BigDecimal(15), 18, 0);
     }
 
+    @Test
+    public void testAddSubtractPrecisionOverflow() {
+        // compact fast path: the unscaled result exceeds the declared 
precision
+        Decimal max = Decimal.fromUnscaledLong(99999, 5, 0);
+        Decimal one = Decimal.fromUnscaledLong(1, 5, 0);
+        assertThat(add(max, one, 5, 0)).isNull();
+        assertThat(subtract(Decimal.fromUnscaledLong(-99999, 5, 0), one, 5, 
0)).isNull();
+        assertThat(add(max, Decimal.fromUnscaledLong(-1, 5, 0), 5, 
0).toUnscaledLong())
+                .isEqualTo(99998);
+
+        // compact fast path with a non-zero scale: the bound stays 
10^precision, not
+        // 10^(precision + scale)
+        Decimal scaledMax = Decimal.fromBigDecimal(new BigDecimal("999.99"), 
5, 2);
+        Decimal scaledOne = Decimal.fromBigDecimal(new BigDecimal("0.01"), 5, 
2);
+        assertThat(add(scaledMax, scaledOne, 5, 2)).isNull();
+        assertThat(
+                        subtract(
+                                Decimal.fromBigDecimal(new 
BigDecimal("-999.99"), 5, 2),
+                                scaledOne,
+                                5,
+                                2))
+                .isNull();
+        assertThat(
+                        add(scaledMax, Decimal.fromBigDecimal(new 
BigDecimal("-0.01"), 5, 2), 5, 2)
+                                .toUnscaledLong())
+                .isEqualTo(99998L);
+
+        // non-compact path: fromBigDecimal returns null when precision 
overflows
+        Decimal bigMax =
+                Decimal.fromBigDecimal(
+                        new 
BigDecimal("999999999999999999999999999999999999.99"), 38, 2);
+        Decimal bigOne = Decimal.fromBigDecimal(new BigDecimal("1"), 38, 2);
+        assertThat(add(bigMax, bigOne, 38, 2)).isNull();
+        assertThat(subtract(bigMax, Decimal.fromBigDecimal(new 
BigDecimal("-2"), 38, 2), 38, 2))
+                .isNull();
+
+        // a target scale different from the operands' scale must rescale via 
BigDecimal,
+        // not reuse the compact unscaled value under a new scale
+        Decimal d10 = Decimal.fromUnscaledLong(10, 5, 0);
+        Decimal d15 = Decimal.fromUnscaledLong(15, 5, 0);
+        assertThat(add(d10, d15, 5, 2))
+                .isEqualTo(Decimal.fromBigDecimal(new BigDecimal("25.00"), 5, 
2));
+        assertThat(subtract(d10, d15, 5, 2))
+                .isEqualTo(Decimal.fromBigDecimal(new BigDecimal("-5.00"), 5, 
2));
+    }
+
     @Test
     public void testToString() {
         String val = "0.0000000000000000001";
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldSumAgg.java
 
b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldSumAgg.java
index fce3e2a61a..21650277c4 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldSumAgg.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldSumAgg.java
@@ -56,6 +56,17 @@ public class FieldSumAgg extends FieldAggregator {
                                 inFieldDD,
                                 mergeFieldDD.precision(),
                                 mergeFieldDD.scale());
+                // a precision overflow has no wrap-around semantics: with 
fail-on-overflow the
+                // job fails loudly; by default the aggregated value becomes 
NULL, like product
+                if (failOnOverflow && sum == null) {
+                    throw new ArithmeticException(
+                            String.format(
+                                    "Decimal overflow: %s + %s (precision=%d, 
scale=%d)",
+                                    mergeFieldDD.toBigDecimal(),
+                                    inFieldDD.toBigDecimal(),
+                                    mergeFieldDD.precision(),
+                                    mergeFieldDD.scale()));
+                }
                 break;
             case TINYINT:
                 sum = addByte((byte) accumulator, (byte) inputField);
@@ -106,6 +117,15 @@ public class FieldSumAgg extends FieldAggregator {
                                 inFieldDD,
                                 mergeFieldDD.precision(),
                                 mergeFieldDD.scale());
+                if (failOnOverflow && sum == null) {
+                    throw new ArithmeticException(
+                            String.format(
+                                    "Decimal overflow: %s - %s (precision=%d, 
scale=%d)",
+                                    mergeFieldDD.toBigDecimal(),
+                                    inFieldDD.toBigDecimal(),
+                                    mergeFieldDD.precision(),
+                                    mergeFieldDD.scale()));
+                }
                 break;
             case TINYINT:
                 sum = subtractByte((byte) accumulator, (byte) inputField);
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java
 
b/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java
index e82b4d709f..dbea7a91c6 100644
--- 
a/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java
+++ 
b/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java
@@ -973,6 +973,85 @@ public class FieldAggregatorTest {
         assertThat(fieldSumAgg.retract(null, 
toDecimal(5))).isEqualTo(toDecimal(-5));
     }
 
+    @Test
+    public void testFieldSumDecimalOverflow() {
+        FieldSumAgg fieldSumAgg = createSumWithOverflowCheck(new 
DecimalType(5, 0));
+        Decimal max = Decimal.fromBigDecimal(new BigDecimal("99999"), 5, 0);
+        Decimal one = Decimal.fromBigDecimal(new BigDecimal("1"), 5, 0);
+        assertThatThrownBy(() -> fieldSumAgg.agg(max, 
one)).isInstanceOf(ArithmeticException.class);
+        Decimal negMax = Decimal.fromBigDecimal(new BigDecimal("-99999"), 5, 
0);
+        assertThatThrownBy(
+                        () ->
+                                fieldSumAgg.agg(
+                                        negMax, Decimal.fromBigDecimal(new 
BigDecimal("-1"), 5, 0)))
+                .isInstanceOf(ArithmeticException.class);
+    }
+
+    @Test
+    public void testFieldSumDecimalRetractOverflow() {
+        FieldSumAgg fieldSumAgg = createSumWithOverflowCheck(new 
DecimalType(5, 0));
+        Decimal negMax = Decimal.fromBigDecimal(new BigDecimal("-99999"), 5, 
0);
+        Decimal one = Decimal.fromBigDecimal(new BigDecimal("1"), 5, 0);
+        assertThatThrownBy(() -> fieldSumAgg.retract(negMax, one))
+                .isInstanceOf(ArithmeticException.class);
+        Decimal max = Decimal.fromBigDecimal(new BigDecimal("99999"), 5, 0);
+        Decimal negOne = Decimal.fromBigDecimal(new BigDecimal("-1"), 5, 0);
+        assertThatThrownBy(() -> fieldSumAgg.retract(max, negOne))
+                .isInstanceOf(ArithmeticException.class);
+    }
+
+    @Test
+    public void testFieldSumDecimalOverflowNonCompact() {
+        // precision 38 exceeds the compact fast path, so the BigDecimal path 
is exercised
+        FieldSumAgg fieldSumAgg = createSumWithOverflowCheck(new 
DecimalType(38, 2));
+        Decimal max =
+                Decimal.fromBigDecimal(
+                        new 
BigDecimal("999999999999999999999999999999999999.99"), 38, 2);
+        Decimal one = Decimal.fromBigDecimal(new BigDecimal("1"), 38, 2);
+        assertThatThrownBy(() -> fieldSumAgg.agg(max, 
one)).isInstanceOf(ArithmeticException.class);
+        Decimal negMax =
+                Decimal.fromBigDecimal(
+                        new 
BigDecimal("-999999999999999999999999999999999999.99"), 38, 2);
+        assertThatThrownBy(() -> fieldSumAgg.retract(negMax, one))
+                .isInstanceOf(ArithmeticException.class);
+    }
+
+    @Test
+    public void testFieldSumDecimalOverflowWithScale() {
+        // compact fast path with a non-zero scale: the bound stays 
10^precision
+        FieldSumAgg fieldSumAgg = createSumWithOverflowCheck(new 
DecimalType(5, 2));
+        Decimal max = Decimal.fromBigDecimal(new BigDecimal("999.99"), 5, 2);
+        Decimal one = Decimal.fromBigDecimal(new BigDecimal("0.01"), 5, 2);
+        assertThatThrownBy(() -> fieldSumAgg.agg(max, 
one)).isInstanceOf(ArithmeticException.class);
+        Decimal negMax = Decimal.fromBigDecimal(new BigDecimal("-999.99"), 5, 
2);
+        assertThatThrownBy(() -> fieldSumAgg.retract(negMax, one))
+                .isInstanceOf(ArithmeticException.class);
+    }
+
+    @Test
+    public void testFieldSumDecimalOverflowDisabled() {
+        // by default (fail-on-overflow = false) the aggregated value becomes 
NULL
+        FieldSumAgg fieldSumAgg = new FieldSumAggFactory().create(new 
DecimalType(5, 0));
+        Decimal max = Decimal.fromBigDecimal(new BigDecimal("99999"), 5, 0);
+        Decimal one = Decimal.fromBigDecimal(new BigDecimal("1"), 5, 0);
+        Decimal negMax = Decimal.fromBigDecimal(new BigDecimal("-99999"), 5, 
0);
+        Decimal negOne = Decimal.fromBigDecimal(new BigDecimal("-1"), 5, 0);
+        assertThat(fieldSumAgg.agg(max, one)).isNull();
+        assertThat(fieldSumAgg.agg(negMax, negOne)).isNull();
+        assertThat(fieldSumAgg.retract(max, negOne)).isNull();
+        assertThat(fieldSumAgg.retract(negOne, max)).isNull();
+
+        // non-compact path behaves the same
+        FieldSumAgg nonCompact = new FieldSumAggFactory().create(new 
DecimalType(38, 2));
+        Decimal bigMax =
+                Decimal.fromBigDecimal(
+                        new 
BigDecimal("999999999999999999999999999999999999.99"), 38, 2);
+        Decimal bigNegOne = Decimal.fromBigDecimal(new BigDecimal("-1"), 38, 
2);
+        assertThat(nonCompact.agg(bigMax, Decimal.fromBigDecimal(new 
BigDecimal("1"), 38, 2)))
+                .isNull();
+        assertThat(nonCompact.retract(bigMax, bigNegOne)).isNull();
+    }
+
     private static Decimal toDecimal(int i) {
         return Decimal.fromBigDecimal(
                 new BigDecimal(i), DecimalType.DEFAULT_PRECISION, 
DecimalType.DEFAULT_SCALE);

Reply via email to