Skip to content

GH-55334: [R] Use modulo kernel for %% on int64 and decimals - #55338

Open
ump45nose wants to merge 2 commits into
apache:mainfrom
ump45nose:fix/55334-r-modulo-kernel
Open

ump45nose wants to merge 2 commits into
apache:mainfrom
ump45nose:fix/55334-r-modulo-kernel

Conversation

@ump45nose

@ump45nose ump45nose commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Fixes #55334.

R's %% was implemented by rewriting through %/%, which casts both sides to float64. 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 floored modulo kernel 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() and Expression$op() call modulo for %%.
  • Integer %% 0 still returns NA like base R: zero divisors are substituted before the kernel (which errors on divide-by-zero), then those positions are set to NA.
  • .operator_map maps %% to modulo.
  • Tests for large int64, decimal, integer %% 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 %% 0 behaviour 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:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

AI tooling helped draft the patch and tests; the submitter reviewed it against #55334 and the existing %/% zero-divisor handling before opening the PR.

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.
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #55334 has been automatically assigned in GitHub to PR creator.

@Reranko05

Copy link
Copy Markdown
Collaborator

@ump45nose Can you add the AI disclosure checkbox in PR description. Thanks!

@ump45nose

Copy link
Copy Markdown
Contributor Author

@Reranko05 Thanks — updated the PR description with the required Was AI used for this PR? checkboxes (code/description: AI; reviewed before submission: Human).

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #55334 has been automatically assigned in GitHub to PR creator.

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.
Copilot AI balanced review requested due to automatic review settings November 13, 2026 23:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 NA results.
  • 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

Comment thread r/R/arrow-datum.R
Comment on lines +218 to +225
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),
Comment thread r/R/expression.R
divisor_type <- int32()
}
# `modulo` errors on divide-by-zero; R returns NA for integer `%% 0`.
zero <- Expression$op("==", args[[2]], 0L)
Comment thread r/R/expression.R
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")
@ump45nose

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up to the two R failures from the last run:

  • test-compute-arith.R:93 (a %% 2 with an integer array): the modulo kernel promotes an integer numerator with a double divisor to double, so the result no longer had the numerator's integer type. Floating-point divisors now keep the previous %/% rewrite; the kernel branch is used for exact types only, and now also covers decimals.
  • test-dplyr-funcs-math.R:472 was my test comparing against bit64's %%, which rounds the dividend. It now pins the exact remainder (2^60 + 5 mod 7 = 6) and asserts the result type is int64, plus a negative-dividend case for R's floor semantics.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[R] %% returns wrong results for decimals and errors for large int64; use the modulo kernel

4 participants