Repository navigation
Conversation
Call Arrow's floored modulo kernel from Ops.ArrowDatum and Expression$op instead of rewriting %% through %/% (which casts via float64). Preserve R's NA for integer %% 0 by masking zero divisors before the kernel runs.
|
|
|
@ump45nose Can you add the AI disclosure checkbox in PR description. Thanks! |
|
@Reranko05 Thanks — updated the PR description with the required Was AI used for this PR? checkboxes (code/description: AI; reviewed before submission: Human). |
|
|
The modulo kernel only covers exact types here: an integer numerator with a double divisor has to keep returning the numerator's integer type, which the kernel does not do because it promotes to double. Restore the previous %/% rewrite for that path and extend the kernel branch to decimals. Also pin the int64 expectation to exact arithmetic instead of bit64's %%, which rounds the dividend.
There was a problem hiding this comment.
🟡 Changes recommended
Modulo handling fails for valid decimal types due to int32 promotion and unrepresentable replacement scalars, while the integer64 test has a type mismatch caused by default downcasting.
4 open issues
What changed in this PR
This PR addresses #55334 by using Arrow’s native modulo kernel for R’s %% on integer and decimal operands, avoiding precision loss from floating-point conversion.
Changes:
- Adds native modulo handling to eager and dplyr expressions, with zero-divisor masking to preserve integer
NAresults. - Updates the operator map to reference
modulo. - Adds regression tests for large int64 values, decimals, and zero divisors.
| File | Description |
|---|---|
r/tests/testthat/test-dplyr-funcs-math.R |
Adds precision and zero-divisor regression tests. |
r/R/expression.R |
Adds exact-type modulo handling for deferred expressions. |
r/R/dplyr-funcs-simple.R |
Maps %% to the native modulo kernel. |
r/R/arrow-datum.R |
Adds eager modulo handling and shared exact-type detection. |
Review effort: Balanced
| Scalar$create(1L)$cast(args[[2]]$type), | ||
| args[[2]] | ||
| ) | ||
| rem <- call_function("modulo", args = list(args[[1]], safe_divisor)) | ||
| return(eval_array_expression( | ||
| "if_else", | ||
| zero, | ||
| Scalar$create(NA_integer_)$cast(args[[1]]$type), |
| divisor_type <- int32() | ||
| } | ||
| # `modulo` errors on divide-by-zero; R returns NA for integer `%% 0`. | ||
| zero <- Expression$op("==", args[[2]], 0L) |
| safe_divisor <- Expression$create( | ||
| "if_else", | ||
| zero, | ||
| Scalar$create(1L, divisor_type), |
| }) | ||
|
|
||
| test_that("dplyr %% matches base R for int64 and zero divisor", { | ||
| skip_if_not_installed("bit64") |
|
Pushed a follow-up to the two R failures from the last run:
I could not run the R suite locally: this machine has no R toolchain and installing one is out of scope for my setup, so the change is verified only by review and by CI once it runs. Could a committer trigger the R jobs? (AI-assisted change and comment.) |

Rationale for this change
Fixes #55334.
R's
%%was implemented by rewriting through%/%, which casts both sides tofloat64. That breaks int64 values outside the exact double range (errors) and decimals with many significant digits (silently wrong answers). Arrow C++ 26.0.0 added a flooredmodulokernel with the same sign-of-divisor semantics as R's%%, so the binding can call it directly.What changes are included in this PR?
eval_array_expression()andExpression$op()callmodulofor%%.%% 0still returnsNAlike base R: zero divisors are substituted before the kernel (which errors on divide-by-zero), then those positions are set toNA..operator_mapmaps%%tomodulo.%% 0, and the dplyr binding.Are these changes tested?
Yes — new cases in
r/tests/testthat/test-dplyr-funcs-math.R.Are there any user-facing changes?
Bug fix for
%%on large int64 and high-precision decimals. Integer%% 0behaviour is unchanged (NA).Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by:
AI tooling helped draft the patch and tests; the submitter reviewed it against #55334 and the existing
%/%zero-divisor handling before opening the PR.