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 4e345140 fix(arrow/compute): round decimals between -1 and 1 in the 
right direction (#1589)
4e345140 is described below

commit 4e3451401282c972a07a722137a328b1620b213a
Author: Shivam Sharma <[email protected]>
AuthorDate: Tue Nov 17 22:44:13 2026 +0530

    fix(arrow/compute): round decimals between -1 and 1 in the right direction 
(#1589)
    
    ### Rationale for this change
    
    For decimal inputs, `round` with `DOWN`, `UP`, `HALF_DOWN` or `HALF_UP` 
(and so `floor` and `ceil`) returns the wrong result when the value's magnitude 
is below the rounding unit:
    
    ```go
    in, _, _ := array.FromJSON(mem, &arrow.Decimal128Type{Precision: 4, Scale: 
2},
            strings.NewReader(`["0.40", "-0.40", "0.50", "-0.50"]`))
    compute.CallFunction(ctx, "floor", nil, compute.NewDatum(in)) // [0.00, 
0.00, 0.00, 0.00], want [0.00, -1.00, 0.00, -1.00]
    compute.CallFunction(ctx, "ceil", nil, compute.NewDatum(in))  // [0.00, 
0.00, 0.00, 0.00], want [1.00, 0.00, 1.00, 0.00]
    // round HALF_UP:   0.50 -> 0.00, want 1.00
    // round HALF_DOWN: -0.50 -> 0.00, want -1.00
    // round(-0.04, ndigits=1, DOWN) -> 0.00, want -0.10
    ```
    
    The float64 kernels already return the expected values for the same inputs.
    
    The kernel subtracts the remainder and then decides whether to step down or 
up from the sign of the truncated value. For an input between -1 and 1 that 
value is zero, so the kernel never takes the step. Arrow C++ uses the sign of 
the remainder instead ([`RoundImpl<T, RoundMode::DOWN>` / 
`UP`](https://github.com/apache/arrow/blob/main/cpp/src/arrow/compute/kernels/scalar_round.cc)).
    
    ### What changes are included in this PR?
    
    In `getDecRounding`, the `RoundDown`/`HalfDown` and `RoundUp`/`HalfUp` 
closures now use the sign of the remainder, as Arrow C++ does. The other modes 
already did this, or don't depend on the sign. `round_to_multiple` already used 
the remainder and is unchanged.
    
    ### Are these changes tested?
    
    Yes. `TestRoundMagnitudeBelowOne` rounds `0.40, 0.50, 0.60` and their 
negatives with each rounding mode, and also through `floor` and `ceil`, for 
Decimal128 and Decimal256. It also covers `ndigits=1` with `±0.04`. On `main`, 
the `DOWN`, `UP`, `HALF_DOWN`, `HALF_UP`, `floor`, `ceil` and `ndigits=1` cases 
fail. They pass with this change.
    
    `go test ./arrow/compute/...` passes, as do runs with `-tags assert,test 
-short` and with `-tags assert,test,noasm -short`. `go vet`, gofmt and 
golangci-lint (staticcheck) report nothing for the changed packages.
    
    ### Are there any user-facing changes?
    
    Yes, a bug fix. For decimal values between -1 and 1, `floor`, `ceil` and 
`round` with the `DOWN`, `UP`, `HALF_DOWN` and `HALF_UP` modes now return the 
correctly rounded value. They used to return 0.
    
    AI assistance: I used Claude Code (Anthropic) to find this bug and write 
the fix and the test. I reviewed the code and ran the tests locally.
---
 arrow/compute/arithmetic_test.go           | 63 ++++++++++++++++++++++++++++++
 arrow/compute/internal/kernels/rounding.go |  8 ++--
 2 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/arrow/compute/arithmetic_test.go b/arrow/compute/arithmetic_test.go
index bf319740..1ca688cf 100644
--- a/arrow/compute/arithmetic_test.go
+++ b/arrow/compute/arithmetic_test.go
@@ -1726,6 +1726,69 @@ func (ds *DecimalUnaryArithmeticSuite) TestRound() {
        }
 }
 
+func (ds *DecimalUnaryArithmeticSuite) TestRoundMagnitudeBelowOne() {
+       // rounding a value between -1 and 1 to a whole number truncates it to
+       // zero first, so the direction must come from the sign of the input
+       cases := []struct {
+               fn   string
+               mode compute.RoundMode
+               exp  string
+       }{
+               {"round", compute.RoundDown, `["0.00", "0.00", "0.00", "-1.00", 
"-1.00", "-1.00", null]`},
+               {"round", compute.RoundUp, `["1.00", "1.00", "1.00", "0.00", 
"0.00", "0.00", null]`},
+               {"round", compute.RoundTowardsZero, `["0.00", "0.00", "0.00", 
"0.00", "0.00", "0.00", null]`},
+               {"round", compute.RoundTowardsInfinity, `["1.00", "1.00", 
"1.00", "-1.00", "-1.00", "-1.00", null]`},
+               {"round", compute.RoundHalfDown, `["0.00", "0.00", "1.00", 
"0.00", "-1.00", "-1.00", null]`},
+               {"round", compute.RoundHalfUp, `["0.00", "1.00", "1.00", 
"0.00", "0.00", "-1.00", null]`},
+               {"round", compute.RoundHalfTowardsZero, `["0.00", "0.00", 
"1.00", "0.00", "0.00", "-1.00", null]`},
+               {"round", compute.RoundHalfTowardsInfinity, `["0.00", "1.00", 
"1.00", "0.00", "-1.00", "-1.00", null]`},
+               {"round", compute.RoundHalfToEven, `["0.00", "0.00", "1.00", 
"0.00", "0.00", "-1.00", null]`},
+               {"round", compute.RoundHalfToOdd, `["0.00", "1.00", "1.00", 
"0.00", "-1.00", "-1.00", null]`},
+               {"floor", 0, `["0.00", "0.00", "0.00", "-1.00", "-1.00", 
"-1.00", null]`},
+               {"ceil", 0, `["1.00", "1.00", "1.00", "0.00", "0.00", "0.00", 
null]`},
+       }
+
+       for _, ty := range []arrow.DataType{&arrow.Decimal128Type{Precision: 4, 
Scale: 2}, &arrow.Decimal256Type{Precision: 4, Scale: 2}} {
+               ds.Run(ty.String(), func() {
+                       values := ds.getArr(ty, `["0.40", "0.50", "0.60", 
"-0.40", "-0.50", "-0.60", null]`)
+                       defer values.Release()
+
+                       for _, tt := range cases {
+                               name := tt.fn
+                               var opts compute.FunctionOptions
+                               if tt.fn == "round" {
+                                       name = tt.mode.String()
+                                       opts = compute.RoundOptions{NDigits: 0, 
Mode: tt.mode}
+                               }
+                               ds.Run(name, func() {
+                                       exp := ds.getArr(ty, tt.exp)
+                                       defer exp.Release()
+                                       checkScalar(ds.T(), tt.fn, 
[]compute.Datum{&compute.ArrayDatum{Value: values.Data()}},
+                                               &compute.ArrayDatum{Value: 
exp.Data()}, opts)
+                               })
+                       }
+
+                       // the same applies when rounding to a non-zero number 
of digits
+                       small := ds.getArr(ty, `["0.04", "-0.04"]`)
+                       defer small.Release()
+                       for _, tt := range []struct {
+                               mode compute.RoundMode
+                               exp  string
+                       }{
+                               {compute.RoundDown, `["0.00", "-0.10"]`},
+                               {compute.RoundUp, `["0.10", "0.00"]`},
+                       } {
+                               ds.Run("ndigits=1/"+tt.mode.String(), func() {
+                                       exp := ds.getArr(ty, tt.exp)
+                                       defer exp.Release()
+                                       checkScalar(ds.T(), "round", 
[]compute.Datum{&compute.ArrayDatum{Value: small.Data()}},
+                                               &compute.ArrayDatum{Value: 
exp.Data()}, compute.RoundOptions{NDigits: 1, Mode: tt.mode})
+                               })
+                       }
+               })
+       }
+}
+
 func (ds *DecimalUnaryArithmeticSuite) TestRoundHalfToOddTies() {
        for _, ty := range []arrow.DataType{
                &arrow.Decimal128Type{Precision: 5, Scale: 2},
diff --git a/arrow/compute/internal/kernels/rounding.go 
b/arrow/compute/internal/kernels/rounding.go
index 9e46573c..493dc8e1 100644
--- a/arrow/compute/internal/kernels/rounding.go
+++ b/arrow/compute/internal/kernels/rounding.go
@@ -232,7 +232,7 @@ func getDecRounding[T decimal128.Num | decimal256.Num](mode 
RoundMode, opsImpl *
        case RoundDown:
                return func(val, remainder, pow10 T, _ int32) T {
                        val = opsImpl.Sub(val, remainder)
-                       if opsImpl.Sign(val) < 0 {
+                       if opsImpl.Sign(remainder) < 0 {
                                val = opsImpl.Sub(val, pow10)
                        }
                        return val
@@ -240,7 +240,7 @@ func getDecRounding[T decimal128.Num | decimal256.Num](mode 
RoundMode, opsImpl *
        case RoundUp:
                return func(val, remainder, pow10 T, _ int32) T {
                        val = opsImpl.Sub(val, remainder)
-                       if opsImpl.Sign(val) > 0 && remainder != z {
+                       if opsImpl.Sign(remainder) > 0 {
                                val = opsImpl.Add(val, pow10)
                        }
                        return val
@@ -264,7 +264,7 @@ func getDecRounding[T decimal128.Num | decimal256.Num](mode 
RoundMode, opsImpl *
        case HalfDown:
                return func(val, remainder, pow10 T, _ int32) T {
                        val = opsImpl.Sub(val, remainder)
-                       if opsImpl.Sign(val) < 0 {
+                       if opsImpl.Sign(remainder) < 0 {
                                val = opsImpl.Sub(val, pow10)
                        }
                        return val
@@ -272,7 +272,7 @@ func getDecRounding[T decimal128.Num | decimal256.Num](mode 
RoundMode, opsImpl *
        case HalfUp:
                return func(val, remainder, pow10 T, _ int32) T {
                        val = opsImpl.Sub(val, remainder)
-                       if opsImpl.Sign(val) > 0 && remainder != z {
+                       if opsImpl.Sign(remainder) > 0 {
                                val = opsImpl.Add(val, pow10)
                        }
                        return val

Reply via email to