mbutrovich commented on issue #12426:
URL: https://github.com/apache/arrow-rs/issues/12426#issuecomment-7119576580

   `rem` can stay as it is. PostgreSQL's numeric `%` computes [`x - 
trunc(x/y)*y`](https://github.com/postgres/postgres/blob/884aca9b96989b9bc1b65d42e731e90f098ee2ee/src/backend/utils/adt/numeric.c#L9807-L9826),
 and MySQL's 
[`decimal_mod`](https://github.com/mysql/mysql-server/blob/bf5a74e48423ac51200987edcd72a678772cb2ee/mysys/decimal.cc#L2503-L2517)
 gives the remainder the sign of the dividend. Both are the remainder of a 
quotient truncated toward zero, which is what arrow-rs returns today.
   
   Three paths compute the `div` quotient, and all three need to round in the 
same change:
   
   - [Division by a 
scalar](https://github.com/apache/arrow-rs/blob/a5c9ea96a564d52471098db187720841105d6ab1/arrow-arith/src/numeric.rs#L1624-L1634).
   - [Division by an 
array](https://github.com/apache/arrow-rs/blob/a5c9ea96a564d52471098db187720841105d6ab1/arrow-arith/src/numeric.rs#L1635-L1642).
   - 
[`scaled_div`](https://github.com/apache/arrow-rs/blob/a5c9ea96a564d52471098db187720841105d6ab1/arrow-arith/src/numeric.rs#L1433-L1469),
 which both of them call when scaling the dividend overflows.
   
   The divisor is the user's value times a power of ten, so it can have either 
sign and any magnitude. The halfway test should be `|r| >= |d| - |r|`, because 
`2 * |r|` overflows when `|d|` is above `MAX / 2`. Moving the quotient one step 
away from zero can't overflow. The test can only pass when `|d| >= 2`, and then 
`|q| <= MAX / 2`.
   
   #12419 replaces the division by a scalar with a precomputed divisor and 
changes no results. Could we land it first and include it in the 59.x backport 
([asked](https://github.com/apache/arrow-rs/pull/12076#issuecomment-6659838551) 
by @timsaucer), then round on `main` for 61.0.0? The truncating closures drop 
the remainder's multiply ([`n - q * 
magnitude`](https://github.com/apache/arrow-rs/blob/a5c9ea96a564d52471098db187720841105d6ab1/arrow-buffer/src/divisor.rs#L489-L500)),
 and rounding needs it. So the rounding PR should rerun the `decimal_division` 
benchmark on the bot, including the null-fraction cases that set #12419's 
cutoffs between `unary` and `try_unary`. #12419's tests compare division by a 
scalar with division by an array, so they keep passing once both paths round. 
The rounding PR needs its own tests against a reference that rounds, with 
remainders exactly at half and odd divisors near `MAX`.


-- 
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