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 82ab0bf4a7 [python] Honor fail-on-overflow for sum and product 
aggregators (#11032)
82ab0bf4a7 is described below

commit 82ab0bf4a76cd32dc58e4d15c292798aebd51227
Author: AuroraVoyage <[email protected]>
AuthorDate: Tue Nov 10 17:13:19 2026 +0800

    [python] Honor fail-on-overflow for sum and product aggregators (#11032)
---
 .../pypaimon/common/options/core_options.py        |  22 ++
 .../python/read/reader/aggregate/aggregators.py    | 252 +++++++++++----------
 .../pypaimon/tests/test_field_aggregators.py       |  84 +++++--
 3 files changed, 224 insertions(+), 134 deletions(-)

diff --git a/paimon-python/pypaimon/common/options/core_options.py 
b/paimon-python/pypaimon/common/options/core_options.py
index 2e65a0d0f3..1d2e8ce7c1 100644
--- a/paimon-python/pypaimon/common/options/core_options.py
+++ b/paimon-python/pypaimon/common/options/core_options.py
@@ -160,6 +160,8 @@ class CoreOptions:
     FIELDS_PREFIX = "fields"
     DISTINCT = "distinct"
     LIST_AGG_DELIMITER = "list-agg-delimiter"
+    SUM_FAIL_ON_OVERFLOW = "sum.fail-on-overflow"
+    PRODUCT_FAIL_ON_OVERFLOW = "product.fail-on-overflow"
     NESTED_KEY = "nested-key"
     NESTED_KEY_NULL_STRATEGY = "nested-key-null-strategy"
     NESTED_SEQUENCE_FIELD = "nested-sequence-field"
@@ -2056,6 +2058,26 @@ class CoreOptions:
     def dynamic_partition_overwrite(self) -> bool:
         return self.options.get(CoreOptions.DYNAMIC_PARTITION_OVERWRITE)
 
+    def field_sum_fail_on_overflow(self, field_name: str) -> bool:
+        return self.options.get(
+            ConfigOptions.key(
+                f'{CoreOptions.FIELDS_PREFIX}.{field_name}.'
+                f'{CoreOptions.SUM_FAIL_ON_OVERFLOW}'
+            )
+            .boolean_type()
+            .default_value(False)
+        )
+
+    def field_product_fail_on_overflow(self, field_name: str) -> bool:
+        return self.options.get(
+            ConfigOptions.key(
+                f'{CoreOptions.FIELDS_PREFIX}.{field_name}.'
+                f'{CoreOptions.PRODUCT_FAIL_ON_OVERFLOW}'
+            )
+            .boolean_type()
+            .default_value(False)
+        )
+
     def field_listagg_delimiter(self, field_name: str) -> str:
         return self.options.get(
             ConfigOptions.key(
diff --git 
a/paimon-python/pypaimon/execution/python/read/reader/aggregate/aggregators.py 
b/paimon-python/pypaimon/execution/python/read/reader/aggregate/aggregators.py
index 73c5e4c6b9..4fbd7bf8d8 100644
--- 
a/paimon-python/pypaimon/execution/python/read/reader/aggregate/aggregators.py
+++ 
b/paimon-python/pypaimon/execution/python/read/reader/aggregate/aggregators.py
@@ -75,15 +75,40 @@ NAME_HLL_SKETCH = "hll_sketch"
 NAME_RBM32 = "rbm32"
 
 
-# Integer range limits used for overflow checking.
-_BYTE_MIN = -128
-_BYTE_MAX = 127
-_SHORT_MIN = -32768
-_SHORT_MAX = 32767
-_INT_MIN = -(1 << 31)
-_INT_MAX = (1 << 31) - 1
-_LONG_MIN = -(1 << 63)
-_LONG_MAX = (1 << 63) - 1
+# Two's-complement widths. Java casts the wide result back to the field
+# type when fail-on-overflow is false.
+_INTEGER_BITS = {
+    "TINYINT": 8,
+    "SMALLINT": 16,
+    "INT": 32,
+    "INTEGER": 32,
+    "BIGINT": 64,
+}
+
+
+def _finish_integer(value, base_type, fail_on_overflow, message):
+    """Return ``value`` in the signed range of ``base_type``.
+
+    Matches Java: overflow raises when fail-on-overflow is set, and
+    otherwise wraps by discarding the high bits.
+    """
+    bits = _INTEGER_BITS[base_type]
+    limit = 1 << (bits - 1)
+    if value < -limit or value > limit - 1:
+        if fail_on_overflow:
+            raise ArithmeticError(message)
+        span = limit << 1
+        value %= span
+        if value >= limit:
+            value -= span
+    return value
+
+
+def _div_toward_zero(left, right):
+    """Integer division that truncates toward zero, like Java ``/``."""
+    if (left >= 0) == (right >= 0):
+        return abs(left) // abs(right)
+    return -(abs(left) // abs(right))
 
 # Base SQL type names treated as numeric for sum/product-style
 # aggregators. NUMERIC / DEC are SQL synonyms accepted by the parser;
@@ -401,12 +426,14 @@ class FieldSumAgg(FieldAggregator):
     """
     Numeric sum aggregator.
 
-    Returns the non-null operand if either side is ``None``. Performs
-    overflow checking for integral types and preserves decimal
-    precision and scale for DECIMAL values.
+    Returns the non-null operand if either side is ``None``. Integral
+    overflow follows ``fields.<field>.sum.fail-on-overflow``: the default
+    wraps in two's complement, and ``true`` raises ``ArithmeticError``.
+    DECIMAL keeps its precision and scale.
     """
-    def __init__(self, name: str, field_type: DataType):
+    def __init__(self, name: str, field_type: DataType, fail_on_overflow: bool 
= False):
         super().__init__(name, field_type)
+        self._fail_on_overflow = fail_on_overflow
         self._base_type = _atomic_base_name(field_type)
         if self._base_type in _DECIMAL_TYPES:
             self._precision, self._scale = 
Decimal.extract_decimal_precision_scale(field_type.type)
@@ -430,35 +457,31 @@ class FieldSumAgg(FieldAggregator):
 
         elif self._base_type == "TINYINT":
             value = accumulator + input_field
-            if value < _BYTE_MIN or value > _BYTE_MAX:
-                raise ArithmeticError(
-                    "byte overflow: {} + {} = {}".format(accumulator, 
input_field, value)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "byte overflow: {} + {} = {}".format(accumulator, input_field, 
value),
+            )
 
         elif self._base_type == "SMALLINT":
             value = accumulator + input_field
-            if value < _SHORT_MIN or value > _SHORT_MAX:
-                raise ArithmeticError(
-                    "short overflow: {} + {} = {}".format(accumulator, 
input_field, value)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "short overflow: {} + {} = {}".format(accumulator, 
input_field, value),
+            )
 
         elif self._base_type in _INT_TYPES:
             value = accumulator + input_field
-            if value < _INT_MIN or value > _INT_MAX:
-                raise ArithmeticError(
-                    "int overflow: {} + {}".format(accumulator, input_field)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "int overflow: {} + {}".format(accumulator, input_field),
+            )
 
         elif self._base_type == "BIGINT":
             value = accumulator + input_field
-            if value < _LONG_MIN or value > _LONG_MAX:
-                raise ArithmeticError(
-                    "long overflow: {} + {}".format(accumulator, input_field)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "long overflow: {} + {}".format(accumulator, input_field),
+            )
 
         elif self._base_type in _FLOAT_TYPES:
             return accumulator + input_field
@@ -483,35 +506,31 @@ class FieldSumAgg(FieldAggregator):
 
         elif self._base_type == "TINYINT":
             value = accumulator - retract_field
-            if value < _BYTE_MIN or value > _BYTE_MAX:
-                raise ArithmeticError(
-                    "byte overflow: {} - {} = {}".format(accumulator, 
retract_field, value)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "byte overflow: {} - {} = {}".format(accumulator, 
retract_field, value),
+            )
 
         elif self._base_type == "SMALLINT":
             value = accumulator - retract_field
-            if value < _SHORT_MIN or value > _SHORT_MAX:
-                raise ArithmeticError(
-                    "short overflow: {} - {} = {}".format(accumulator, 
retract_field, value)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "short overflow: {} - {} = {}".format(accumulator, 
retract_field, value),
+            )
 
         elif self._base_type in _INT_TYPES:
             value = accumulator - retract_field
-            if value < _INT_MIN or value > _INT_MAX:
-                raise ArithmeticError(
-                    "int overflow: {} - {}".format(accumulator, retract_field)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "int overflow: {} - {}".format(accumulator, retract_field),
+            )
 
         elif self._base_type == "BIGINT":
             value = accumulator - retract_field
-            if value < _LONG_MIN or value > _LONG_MAX:
-                raise ArithmeticError(
-                    "long overflow: {} - {}".format(accumulator,  
retract_field)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "long overflow: {} - {}".format(accumulator, retract_field),
+            )
 
         elif self._base_type in _FLOAT_TYPES:
             return accumulator - retract_field
@@ -529,27 +548,31 @@ class FieldSumAgg(FieldAggregator):
 
         elif self._base_type == "TINYINT":
             result = -value
-            if result < _BYTE_MIN or result > _BYTE_MAX:
-                raise ArithmeticError("byte overflow: -{} = {}".format(value, 
result))
-            return result
+            return _finish_integer(
+                result, self._base_type, self._fail_on_overflow,
+                "byte overflow: -{} = {}".format(value, result),
+            )
 
         elif self._base_type == "SMALLINT":
             result = -value
-            if result < _SHORT_MIN or result > _SHORT_MAX:
-                raise ArithmeticError("short overflow: -{} = {}".format(value, 
result))
-            return result
+            return _finish_integer(
+                result, self._base_type, self._fail_on_overflow,
+                "short overflow: -{} = {}".format(value, result),
+            )
 
         elif self._base_type in _INT_TYPES:
             result = -value
-            if result < _INT_MIN or result > _INT_MAX:
-                raise ArithmeticError("int overflow: -{}".format(value))
-            return result
+            return _finish_integer(
+                result, self._base_type, self._fail_on_overflow,
+                "int overflow: -{}".format(value),
+            )
 
         elif self._base_type == "BIGINT":
             result = -value
-            if result < _LONG_MIN or result > _LONG_MAX:
-                raise ArithmeticError("long overflow: -{}".format(value))
-            return result
+            return _finish_integer(
+                result, self._base_type, self._fail_on_overflow,
+                "long overflow: -{}".format(value),
+            )
 
         elif self._base_type in _FLOAT_TYPES:
             return -value
@@ -565,9 +588,11 @@ class FieldProductAgg(FieldAggregator):
 
     Null values are ignored and the non-null operand is returned.
     Otherwise, returns the product of accumulator and input value.
+    Integral overflow follows ``fields.<field>.product.fail-on-overflow``.
     """
-    def __init__(self, name: str, field_type: DataType):
+    def __init__(self, name: str, field_type: DataType, fail_on_overflow: bool 
= False):
         super().__init__(name, field_type)
+        self._fail_on_overflow = fail_on_overflow
         self._base_type = _atomic_base_name(field_type)
         if self._base_type in _DECIMAL_TYPES:
             self._precision, self._scale = 
Decimal.extract_decimal_precision_scale(field_type.type)
@@ -591,35 +616,31 @@ class FieldProductAgg(FieldAggregator):
 
         elif self._base_type == "TINYINT":
             value = accumulator * input_field
-            if value < _BYTE_MIN or value > _BYTE_MAX:
-                raise ArithmeticError(
-                    "byte overflow: {} * {} = {}".format(accumulator, 
input_field, value)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "byte overflow: {} * {} = {}".format(accumulator, input_field, 
value),
+            )
 
         elif self._base_type == "SMALLINT":
             value = accumulator * input_field
-            if value < _SHORT_MIN or value > _SHORT_MAX:
-                raise ArithmeticError(
-                    "short overflow: {} * {} = {}".format(accumulator, 
input_field, value)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "short overflow: {} * {} = {}".format(accumulator, 
input_field, value),
+            )
 
         elif self._base_type in _INT_TYPES:
             value = accumulator * input_field
-            if value < _INT_MIN or value > _INT_MAX:
-                raise ArithmeticError(
-                    "int overflow: {} * {}".format(accumulator, input_field)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "int overflow: {} * {}".format(accumulator, input_field),
+            )
 
         elif self._base_type == "BIGINT":
             value = accumulator * input_field
-            if value < _LONG_MIN or value > _LONG_MAX:
-                raise ArithmeticError(
-                    "long overflow: {} * {}".format(accumulator, input_field)
-                )
-            return value
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "long overflow: {} * {}".format(accumulator, input_field),
+            )
 
         elif self._base_type in _FLOAT_TYPES:
             return accumulator * input_field
@@ -643,42 +664,32 @@ class FieldProductAgg(FieldAggregator):
             return None if value is None else value.to_big_decimal()
 
         elif self._base_type == "TINYINT":
-            value = int(accumulator / retract_field)
-            if value > _BYTE_MAX or value < _BYTE_MIN:
-                raise ArithmeticError(
-                    "byte overflow: {} / {} = {}".format(accumulator, 
retract_field, value)
-                )
-            return value
+            value = _div_toward_zero(accumulator, retract_field)
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "byte overflow: {} / {} = {}".format(accumulator, 
retract_field, value),
+            )
 
         elif self._base_type == "SMALLINT":
-            value = int(accumulator / retract_field)
-            if value > _SHORT_MAX or value < _SHORT_MIN:
-                raise ArithmeticError(
-                    "short overflow: {} / {} = {}".format(accumulator, 
retract_field, value)
-                )
-            return value
+            value = _div_toward_zero(accumulator, retract_field)
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "short overflow: {} / {} = {}".format(accumulator, 
retract_field, value),
+            )
 
         elif self._base_type in _INT_TYPES:
-            if accumulator == _INT_MIN and retract_field == -1:
-                raise ArithmeticError(
-                    "int overflow: {} / {}".format(accumulator, retract_field)
-                )
-            return int(accumulator / retract_field)
+            value = _div_toward_zero(accumulator, retract_field)
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "int overflow: {} / {}".format(accumulator, retract_field),
+            )
 
         elif self._base_type == "BIGINT":
-            if accumulator == _LONG_MIN and retract_field == -1:
-                raise ArithmeticError(
-                    "long overflow: {} / {}".format(accumulator, retract_field)
-                )
-
-            # Java integer division truncates toward zero, while Python's "//"
-            # floors toward negative infinity. Divide absolute values first, 
then
-            # restore the sign to match Java semantics without converting 
through
-            # float (which would lose precision for BIGINT values).
-            if (accumulator >= 0) == (retract_field >= 0):
-                return abs(accumulator) // abs(retract_field)
-            else:
-                return -(abs(accumulator) // abs(retract_field))
+            value = _div_toward_zero(accumulator, retract_field)
+            return _finish_integer(
+                value, self._base_type, self._fail_on_overflow,
+                "long overflow: {} / {}".format(accumulator, retract_field),
+            )
 
         elif self._base_type in _FLOAT_TYPES:
             if retract_field == 0.0:
@@ -1577,7 +1588,14 @@ def _build_no_type_check(cls, identifier: str):
 def _build_numeric(cls, identifier: str):
     def _factory(field_type, field_name, options):
         _check_numeric(identifier, field_type)
-        return cls(identifier, field_type)
+        # Callers may omit options. Both Java flags default to false.
+        if options is None:
+            fail_on_overflow = False
+        elif identifier == NAME_SUM:
+            fail_on_overflow = options.field_sum_fail_on_overflow(field_name)
+        else:
+            fail_on_overflow = 
options.field_product_fail_on_overflow(field_name)
+        return cls(identifier, field_type, fail_on_overflow)
     return _factory
 
 
diff --git a/paimon-python/pypaimon/tests/test_field_aggregators.py 
b/paimon-python/pypaimon/tests/test_field_aggregators.py
index 5b3ef3b774..57e2b1fb0e 100644
--- a/paimon-python/pypaimon/tests/test_field_aggregators.py
+++ b/paimon-python/pypaimon/tests/test_field_aggregators.py
@@ -76,6 +76,13 @@ def _make(identifier, sql_type, options: CoreOptions = None):
     )
 
 
+def _fail_on_overflow(function):
+    """Java's fail-on-overflow option. The default is false (wrap)."""
+    return CoreOptions(Options({
+        "fields.field0.{}.fail-on-overflow".format(function): True,
+    }))
+
+
 class FieldPrimaryKeyAggTest(unittest.TestCase):
 
     def test_returns_input_field(self):
@@ -193,7 +200,7 @@ class FieldSumAggTest(unittest.TestCase):
         self.assertEqual(agg.retract(None, 5), -5)
 
     def test_byte_overflow(self):
-        agg = _make("sum", "TINYINT")
+        agg = _make("sum", "TINYINT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(127, 1)
@@ -202,7 +209,7 @@ class FieldSumAggTest(unittest.TestCase):
             agg.agg(-128, -1)
 
     def test_short_overflow(self):
-        agg = _make("sum", "SMALLINT")
+        agg = _make("sum", "SMALLINT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(32767, 1)
@@ -211,7 +218,7 @@ class FieldSumAggTest(unittest.TestCase):
             agg.agg(-32768, -1)
 
     def test_int_overflow(self):
-        agg = _make("sum", "INT")
+        agg = _make("sum", "INT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(2147483647, 1)
@@ -220,7 +227,7 @@ class FieldSumAggTest(unittest.TestCase):
             agg.agg(-2147483648, -1)
 
     def test_long_overflow(self):
-        agg = _make("sum", "BIGINT")
+        agg = _make("sum", "BIGINT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(9223372036854775807, 1)
@@ -229,7 +236,7 @@ class FieldSumAggTest(unittest.TestCase):
             agg.agg(-9223372036854775808, -1)
 
     def test_byte_retract_overflow(self):
-        agg = _make("sum", "TINYINT")
+        agg = _make("sum", "TINYINT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-128, 1)
@@ -241,7 +248,7 @@ class FieldSumAggTest(unittest.TestCase):
             agg.retract(None, -128)
 
     def test_short_retract_overflow(self):
-        agg = _make("sum", "SMALLINT")
+        agg = _make("sum", "SMALLINT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-32768, 1)
@@ -253,7 +260,7 @@ class FieldSumAggTest(unittest.TestCase):
             agg.retract(None, -32768)
 
     def test_int_retract_overflow(self):
-        agg = _make("sum", "INT")
+        agg = _make("sum", "INT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-2147483648, 1)
@@ -265,7 +272,7 @@ class FieldSumAggTest(unittest.TestCase):
             agg.retract(None, -2147483648)
 
     def test_long_retract_overflow(self):
-        agg = _make("sum", "BIGINT")
+        agg = _make("sum", "BIGINT", _fail_on_overflow("sum"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-9223372036854775808, 1)
@@ -276,6 +283,22 @@ class FieldSumAggTest(unittest.TestCase):
         with self.assertRaises(ArithmeticError):
             agg.retract(None, -9223372036854775808)
 
+    def test_integer_overflow_wraps_by_default(self):
+        cases = (
+            ("TINYINT", 127, 1, -128, -128, -1, 127),
+            ("SMALLINT", 32767, 1, -32768, -32768, -1, 32767),
+            ("INT", 2147483647, 1, -2147483648, -2147483648, -1, 2147483647),
+            ("BIGINT", 9223372036854775807, 1, -9223372036854775808,
+             -9223372036854775808, -1, 9223372036854775807),
+        )
+        for sql_type, high, one, wrapped_high, low, minus_one, wrapped_low in 
cases:
+            agg = _make("sum", sql_type)
+            self.assertEqual(agg.agg(high, one), wrapped_high)
+            self.assertEqual(agg.agg(low, minus_one), wrapped_low)
+            self.assertEqual(agg.retract(low, one), wrapped_low)
+            self.assertEqual(agg.retract(high, minus_one), wrapped_high)
+            self.assertEqual(agg.retract(None, low), low)
+
     def test_float_sum(self):
         agg = _make("sum", "FLOAT")
 
@@ -406,7 +429,7 @@ class FieldProductAggTest(unittest.TestCase):
         self.assertIsNone(agg.retract(None, self.to_decimal(5, 12)))
 
     def test_byte_overflow(self):
-        agg = _make("product", "TINYINT")
+        agg = _make("product", "TINYINT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(64, 2)
@@ -415,7 +438,7 @@ class FieldProductAggTest(unittest.TestCase):
             agg.agg(-64, 4)
 
     def test_short_overflow(self):
-        agg = _make("product", "SMALLINT")
+        agg = _make("product", "SMALLINT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(1000, 100)
@@ -424,7 +447,7 @@ class FieldProductAggTest(unittest.TestCase):
             agg.agg(-32768, 2)
 
     def test_int_overflow(self):
-        agg = _make("product", "INT")
+        agg = _make("product", "INT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(100000, 100000)
@@ -433,7 +456,7 @@ class FieldProductAggTest(unittest.TestCase):
             agg.agg(-2147483648, -1)
 
     def test_long_overflow(self):
-        agg = _make("product", "BIGINT")
+        agg = _make("product", "BIGINT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.agg(9223372036854775807, 2)
@@ -442,29 +465,51 @@ class FieldProductAggTest(unittest.TestCase):
             agg.agg(-9223372036854775808, -1)
 
     def test_byte_retract_overflow(self):
-        agg = _make("product", "TINYINT")
+        agg = _make("product", "TINYINT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-128, -1)
 
     def test_short_retract_overflow(self):
-        agg = _make("product", "SMALLINT")
+        agg = _make("product", "SMALLINT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-32768, -1)
 
     def test_int_retract_overflow(self):
-        agg = _make("product", "INT")
+        agg = _make("product", "INT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-2147483648, -1)
 
     def test_long_retract_overflow(self):
-        agg = _make("product", "BIGINT")
+        agg = _make("product", "BIGINT", _fail_on_overflow("product"))
 
         with self.assertRaises(ArithmeticError):
             agg.retract(-9223372036854775808, -1)
 
+    def test_integer_overflow_wraps_by_default(self):
+        agg = _make("product", "TINYINT")
+        self.assertEqual(agg.agg(64, 2), -128)
+        self.assertEqual(agg.agg(-64, 4), 0)
+        self.assertEqual(agg.retract(-128, -1), -128)
+
+        agg = _make("product", "SMALLINT")
+        self.assertEqual(agg.agg(1000, 100), -31072)
+        self.assertEqual(agg.agg(-32768, 2), 0)
+        self.assertEqual(agg.retract(-32768, -1), -32768)
+
+        agg = _make("product", "INT")
+        self.assertEqual(agg.agg(100000, 100000), 1410065408)
+        self.assertEqual(agg.agg(-2147483648, -1), -2147483648)
+        self.assertEqual(agg.retract(-2147483648, -1), -2147483648)
+        self.assertEqual(agg.retract(-7, 2), -3)
+
+        agg = _make("product", "BIGINT")
+        self.assertEqual(agg.agg(9223372036854775807, 2), -2)
+        self.assertEqual(agg.agg(-9223372036854775808, -1), 
-9223372036854775808)
+        self.assertEqual(agg.retract(-9223372036854775808, -1), 
-9223372036854775808)
+
     def test_null_inputs(self):
         agg = _make("product", "INT")
 
@@ -483,9 +528,14 @@ class FieldProductAggTest(unittest.TestCase):
             agg.agg(3037000499, 3037000499),
             9223372030926249001,
         )
+        self.assertEqual(
+            agg.agg(3037000500, 3037000500),
+            -9223372036709301616,
+        )
 
+        failing = _make("product", "BIGINT", _fail_on_overflow("product"))
         with self.assertRaises(ArithmeticError):
-            agg.agg(3037000500, 3037000500)
+            failing.agg(3037000500, 3037000500)
 
     def test_non_numeric_type_rejected_at_construction(self):
         with self.assertRaises(ValueError) as ctx:

Reply via email to