You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "wForget (via GitHub)" <gi...@apache.org> on 2025/02/20 05:07:59 UTC
[PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
wForget opened a new pull request, #7159:
URL: https://github.com/apache/arrow-rs/pull/7159
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #7158.
# Rationale for this change
The `div/rem` operations may also overflow, so I want to add `div_wrapping/rem_wrapping` operations to wrapping on overflow. Make it consistent with the logic of `add/sub/mul` so that we can implement `fail_on_overflow` feature for `div/rem` in downstream `datafusion`.
# What changes are included in this PR?
add `div_wrapping/rem_wrapping` functions
# Are there any user-facing changes?
yes, newly added `div_wrapping/rem_wrapping` functions
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "kazuyukitanimura (via GitHub)" <gi...@apache.org>.
kazuyukitanimura commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2685832182
> I don't think we should proceed with this change
@tustvold Sorry, what was the reason for not proceeding with this change? I think we can remove `div_wrapping` from this PR, but `rem_wrapping` is still a valid case?
Or did you mean we should fix this in DataFusion as @wForget mentioned?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2684234086
> add and add_wrapping also have different behaviors in arrow.
This is for performance reasons, see above comments and linked issues.
I don't think we should proceed with this change
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "kazuyukitanimura (via GitHub)" <gi...@apache.org>.
kazuyukitanimura commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2683046331
I see I think the only issue is `rem_wrapping`
```
SELECT -2147483648 % -1;
Output:
?column?
----------
0
(1 row)
```
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2683801031
>Is it possible to return a different error? (divide by zero error)
I have returned error for `divide by zero`
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2684245027
> > add and add_wrapping also have different behaviors in arrow.
>
> This is for performance reasons, see above comments and linked issues.
>
> I don't think we should proceed with this change
I see, thank you. @kazuyukitanimura Should we go back to downstream DataFusion to handle this case?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "kazuyukitanimura (via GitHub)" <gi...@apache.org>.
kazuyukitanimura commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2685971074
> Given this what do you think of changing the behaviour of Operator::Rem
I assume `Operator::Modulo` in DataFusion? WDYT @wForget
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid overflow of remainder [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2671078903
If I'm not mistaken, these new kernels will panic on divide by zero?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "kazuyukitanimura (via GitHub)" <gi...@apache.org>.
kazuyukitanimura commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2680214658
> Yes, should divide by zero be considered as overflow?
Is it possible to return a different error? (divide by zero error)
On DataFusion side we can always use `div_wrapping/rem_wrapping` regardless `fail_on_overflow `. Not sure if any other engines expect overflow for signed_integer::MIN / -1
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2674011556
> Yes, should divide by zero be considered as overflow?
At the very least it shouldn't panic :sweat_smile:
I personally feel it would be better to simply document that `fail_on_overflow` doesn't impact division operators. It seems a pretty tough pill to effectively double the codegen in order to handle a single set of arguments that overflows....
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2681598890
FWIW the current behaviour is consistent with postgres
```
postgres=# select -2147483648 -1;
ERROR: integer out of range
postgres=# select -2147483648 / -1;
ERROR: integer out of range
```
Where -2147483648 is sourced from https://www.postgresql.org/docs/current/datatype-numeric.html
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2673036866
> If I'm not mistaken, these new kernels will panic on divide by zero?
Yes, should `divide by zero` be considered as overflow?

--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2670817405
> The absence of these kernels is intentional - #2647
Thank you for the information, if I understand correctly, it is to avoid inconsistent behavior caused by unchecked divide by zero returning NaN. This PR uses `mod_wrapping` method directly which keeps panics on divide by zero, only wrapping on overflow for `signed_integer::MIN / -1`.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2671013223
> The motivation for the wrapping variants is solely performance, as they vectorise better and are therefore orders of magnitude faster. This does not apply to the division operators, I'm therefore unclear on why one would want an unchecked version, when it will be no faster
Thank you for your explanation. My motivation was just to provide `div/rem` implementation similar to `add_wrapping/sub_wrapping` that does not fail when overflow occurs. That way we can easily provide consistent behavior for binary operators in Datafusion. `BinaryExpr` in Datafusion defines a `fail_on_overflow` property which when `false` means it will not fail on overflow, and there is a similar behavior in Spark.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2689778475
> > Given this what do you think of changing the behaviour of Operator::Rem
>
> I assume `Operator::Modulo` in DataFusion? WDYT @wForget
This sounds good to me, `signed_integer::MIN % -1` with either ansi or non-ansi mode always returns `0` in spark.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2685883823
> @tustvold Sorry, what was the reason for not proceeding with this change? I think we can remove div_wrapping from this PR, but rem_wrapping is still a valid case?
I am trying to be pragmatic and avoid adding codegen for what is, I hope you will concede, an exceptionally niche edge case.
That being said unless I am mistaken the only distinction between wrapping_rem and checked_rem is that for signed integers, dividing the smallest representable quantity by -1 results in "overflow", this is really an implementation detail of rem and is because the quotient overflows, but Rust doesn't distinguish this case.
Given this what do you think of changing the behaviour of `Operator::Rem` to use the WrappingRem logic added in this PR, AFAICT the only difference is it now returns an arguably more correct result for this single case. I think this could reasonably be filed as a bug fix
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2685975698
> I assume Operator::Modulo in DataFusion
Minor nit, but DF (and most databases) don't actually implement modulo, they instead implement remainder
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2683729216
> FWIW the current behaviour is consistent with postgres
`add` and `add_wrapping` also have different behaviors in arrow.
```
#[test]
fn test_integer_add() {
let a = Int32Array::from(vec![-2147483648]);
let b = Int32Array::from(vec![-1]);
let err = add(&a, &b).unwrap_err().to_string();
assert_eq!(err, "Arithmetic overflow: Overflow happened on: -2147483648 + -1");
let result_on_add_wrapping = add_wrapping(&a, &b).unwrap();
assert_eq!(
result_on_add_wrapping.as_ref(),
&Int32Array::from(vec![2147483647])
);
}
```
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2670882872
The motivation for the wrapping variants is solely performance, as they vectorise better and are therefore orders of magnitude faster. This does not apply to the division operators, I'm therefore unclear on why one would want an unchecked version, when it will be no faster
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support div_wrapping/rem_wrapping for numeric arithmetic kernels [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #7159:
URL: https://github.com/apache/arrow-rs/pull/7159#issuecomment-2670672147
The absence of these kernels is intentional - https://github.com/apache/arrow-rs/issues/2647
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org