ShivamSharma43 opened a new pull request, #1589:
URL: https://github.com/apache/arrow-go/pull/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.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to