This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 7addfc71 fix(arrow/compute): report overflow for checked signed min /
-1 (#1680)
7addfc71 is described below
commit 7addfc7175d464e509d2f478b0af4e53ad39cbff
Author: Guillaume Marty <[email protected]>
AuthorDate: Wed Nov 25 19:39:10 2026 +0100
fix(arrow/compute): report overflow for checked signed min / -1 (#1680)
### Rationale for this change
`compute.Divide` with overflow checking on (the default,
`ArithmeticOptions{}`) returns the minimum value for `min / -1` on
signed integers. For example, int32 `-2147483648 / -1` returns
`-2147483648` with no error. The true result does not fit in the type,
so the checked kernel should report an overflow. The other checked
kernels (`add`, `subtract`, `multiply`, `negate`, `abs`) already do, and
Arrow C++ `divide_checked` returns "overflow" for this case.
### What changes are included in this PR?
The checked integer division kernel now returns the existing `overflow`
error when a signed dividend is the type's minimum and the divisor is
-1. Unchecked division keeps Go's wrapping result.
### Are these changes tested?
Yes. `TestDivideOverflow` in `BinaryIntegralArithmeticSuite` covers
int8, int16, int32 and int64 with overflow checks on (error expected)
and off (wrapped value expected). It fails on main: "An error is
expected but got nil". With the change, `go test -run
'TestBinaryArithmetic$' ./arrow/compute` passes on linux/arm64.
### Are there any user-facing changes?
Yes. Checked `divide` now returns an error for signed `min / -1`, where
it used to return a wrong value silently.
An AI assistant was used to find and write this change. The added test
fails without the fix and passes with it.
---
arrow/compute/arithmetic_test.go | 12 ++++++++++++
arrow/compute/internal/kernels/base_arithmetic.go | 7 +++++++
2 files changed, 19 insertions(+)
diff --git a/arrow/compute/arithmetic_test.go b/arrow/compute/arithmetic_test.go
index a588ce07..f8341546 100644
--- a/arrow/compute/arithmetic_test.go
+++ b/arrow/compute/arithmetic_test.go
@@ -568,6 +568,18 @@ type BinaryIntegralArithmeticSuite[T arrow.IntType |
arrow.UintType] struct {
BinaryArithmeticSuite[T]
}
+func (b *BinaryIntegralArithmeticSuite[T]) TestDivideOverflow() {
+ if !arrow.IsSignedInteger(b.DataType().ID()) {
+ b.T().SkipNow()
+ }
+
+ b.setOverflowCheck(true)
+ b.assertBinopErr(compute.Divide, fmt.Sprintf("[%v]", b.min), `[-1]`,
"overflow")
+
+ b.setOverflowCheck(false)
+ b.assertBinop(compute.Divide, fmt.Sprintf("[%v]", b.min), `[-1]`,
fmt.Sprintf("[%v]", b.min))
+}
+
func (b *BinaryIntegralArithmeticSuite[T]) TestShiftLeft() {
b.Run(b.DataType().String(), func() {
for _, overflow := range []bool{false, true} {
diff --git a/arrow/compute/internal/kernels/base_arithmetic.go
b/arrow/compute/internal/kernels/base_arithmetic.go
index 0a711ec8..c26ef7aa 100644
--- a/arrow/compute/internal/kernels/base_arithmetic.go
+++ b/arrow/compute/internal/kernels/base_arithmetic.go
@@ -285,11 +285,18 @@ func getGoArithmeticOpIntegral[InT, OutT arrow.UintType |
arrow.IntType](op Arit
return OutT(o)
}))
case OpDivChecked:
+ // for signed types, ^InT(0) is -1 and min / -1 overflows
+ minusOne, min := ^InT(0), MinOf[InT]()
+ signed := minusOne < 0
return ScalarBinaryNotNull(func(_ *exec.KernelCtx, a, b InT, e
*error) (out OutT) {
if b == 0 {
*e = errDivByZero
return
}
+ if signed && a == min && b == minusOne {
+ *e = errOverflow
+ return
+ }
return OutT(a / b)
})
case OpAbsoluteValueChecked: