From acff6f3c772fde6f21c259e0866d77b83831c3dc Mon Sep 17 00:00:00 2001 From: Vincent <1729065730@qq.com> Date: Fri, 30 Oct 2026 19:11:41 +0800 Subject: [PATCH 1/2] GH-55334: [R] Use modulo kernel for %% on int64 and decimals 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. --- r/R/arrow-datum.R | 34 +++++++++++++++----- r/R/dplyr-funcs-simple.R | 6 ++-- r/R/expression.R | 36 ++++++++++++++++++++- r/tests/testthat/test-dplyr-funcs-math.R | 40 ++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 12 deletions(-) diff --git a/r/R/arrow-datum.R b/r/R/arrow-datum.R index 5ac72bf36856..7c0ed4537058 100644 --- a/r/R/arrow-datum.R +++ b/r/R/arrow-datum.R @@ -201,14 +201,32 @@ eval_array_expression <- function(FUN, ..., args = list(...), options = empty_na return(eval_array_expression("floor", out)) } } else if (FUN == "%%") { - # We can't simply do {e1 - e2 * ( e1 %/% e2 )} since Ops.Array evaluates - # eagerly, but we can build that up - quotient <- eval_array_expression("%/%", args = args) - base <- eval_array_expression("*", quotient, args[[2]]) - # this cast is to ensure that the result of this and e1 are the same - # (autocasting only applies to scalars) - base <- base$cast(args[[1]]$type) - return(eval_array_expression("-", args[[1]], base)) + # Use Arrow's floored `modulo` kernel (R `%%` / Python `%` semantics) so + # int64 and decimal keep their native precision. The previous rewrite via + # `%/%` cast both sides through float64. + int_type_ids <- Type[toupper(INTEGER_TYPES)] + numerator_is_int <- args[[1]]$type_id() %in% int_type_ids + denominator_is_int <- args[[2]]$type_id() %in% int_type_ids + + if (numerator_is_int && denominator_is_int) { + # `modulo` errors on divide-by-zero; R returns NA for integer `%% 0`. + # Replace zero divisors before calling the kernel, then restore NA. + zero <- eval_array_expression("equal", args[[2]], 0L) + safe_divisor <- eval_array_expression( + "if_else", + zero, + 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), + rem + )) + } + return(call_function("modulo", args = args)) } call_function( diff --git a/r/R/dplyr-funcs-simple.R b/r/R/dplyr-funcs-simple.R index e556c7418de1..23ec712f5155 100644 --- a/r/R/dplyr-funcs-simple.R +++ b/r/R/dplyr-funcs-simple.R @@ -101,9 +101,9 @@ "*" = "multiply_checked", "/" = "divide", "%/%" = "divide_checked", - # we don't actually use divide_checked with `%%`, rather it is rewritten to - # use `%/%` in Expression$op(). - "%%" = "divide_checked", + # `%%` is handled in Expression$op() / eval_array_expression() so integer + # `%% 0` can return NA; the map entry documents the underlying kernel. + "%%" = "modulo", "^" = "power_checked" ) diff --git a/r/R/expression.R b/r/R/expression.R index 78272323c709..9f3790710cb7 100644 --- a/r/R/expression.R +++ b/r/R/expression.R @@ -216,7 +216,41 @@ Expression$op <- function(FUN, ..., args = list(...)) { } return(out) } else if (FUN == "%%") { - return(args[[1]] - args[[2]] * (args[[1]] %/% args[[2]])) + # Prefer `modulo` over rewriting through `%/%` (which goes via float64). + int_type_ids <- Type[toupper(INTEGER_TYPES)] + is_int <- function(x) { + is.integer(x) || + (inherits(x, "ArrowObject") && x$type_id() %in% int_type_ids) + } + + if (is_int(args[[1]]) && is_int(args[[2]])) { + if (inherits(args[[1]], "ArrowObject")) { + out_type <- args[[1]]$type() + } else { + out_type <- int32() + } + if (inherits(args[[2]], "ArrowObject")) { + divisor_type <- args[[2]]$type() + } else { + 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), + args[[2]] + ) + rem <- Expression$create("modulo", args = list(args[[1]], safe_divisor)) + return(Expression$create( + "if_else", + zero, + Scalar$create(NA_integer_, out_type), + rem + )) + } + return(Expression$create("modulo", args = args)) } Expression$create(.operator_map[[FUN]], args = args) diff --git a/r/tests/testthat/test-dplyr-funcs-math.R b/r/tests/testthat/test-dplyr-funcs-math.R index 4db1d7441046..be3eb3abc79e 100644 --- a/r/tests/testthat/test-dplyr-funcs-math.R +++ b/r/tests/testthat/test-dplyr-funcs-math.R @@ -464,6 +464,46 @@ test_that("floor division maintains type consistency with R", { ) }) +test_that("%% uses the modulo kernel for large int64 and decimals", { + skip_if_not_installed("bit64") + + # Beyond float64 exact integer range: the old %% rewrite via %/% errored. + big <- bit64::as.integer64("9007199254740993") + expect_equal(as.vector(Array$create(big) %% 7L), as.vector(big %% 7L)) + + # Decimal with many significant digits: the old rewrite was silently wrong. + d <- Scalar$create("12345678901234567890.12", string())$cast(decimal128(25, 2)) + expect_equal(as.vector(d %% 7L), 1.12) +}) + +test_that("integer %% 0 returns NA like base R", { + expect_true(is.na(as.vector(Array$create(5L) %% 0L))) + expect_equal( + as.vector(Array$create(c(5L, 0L, NA_integer_)) %% 0L), + as.vector(c(5L, 0L, NA_integer_) %% 0L) + ) +}) + +test_that("dplyr %% matches base R for int64 and zero divisor", { + skip_if_not_installed("bit64") + + df <- tibble( + integers = c(1:4, NA_integer_), + big = bit64::as.integer64(c("9007199254740993", "10", "0", "-3", NA)) + ) + + compare_dplyr_binding( + .input |> + transmute( + mod_int = integers %% 3L, + mod_zero = integers %% 0L, + mod_big = big %% 7L + ) |> + collect(), + df + ) +}) + test_that("exp()", { df <- tibble(x = c(1:5, NA)) From ef1e18975195e7743b9a63581cc4409f563af4e1 Mon Sep 17 00:00:00 2001 From: yuwk <1729065730@qq.com> Date: Sat, 14 Nov 2026 07:07:36 +0800 Subject: [PATCH 2/2] GH-55334: [R] Keep floating-point %% on the existing rewrite 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. --- r/R/arrow-datum.R | 36 ++++++++++++++++++------ r/R/expression.R | 13 +++++---- r/tests/testthat/test-dplyr-funcs-math.R | 12 ++++++-- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/r/R/arrow-datum.R b/r/R/arrow-datum.R index 7c0ed4537058..f9b1a8df33d9 100644 --- a/r/R/arrow-datum.R +++ b/r/R/arrow-datum.R @@ -201,14 +201,14 @@ eval_array_expression <- function(FUN, ..., args = list(...), options = empty_na return(eval_array_expression("floor", out)) } } else if (FUN == "%%") { - # Use Arrow's floored `modulo` kernel (R `%%` / Python `%` semantics) so - # int64 and decimal keep their native precision. The previous rewrite via - # `%/%` cast both sides through float64. - int_type_ids <- Type[toupper(INTEGER_TYPES)] - numerator_is_int <- args[[1]]$type_id() %in% int_type_ids - denominator_is_int <- args[[2]]$type_id() %in% int_type_ids + # Use Arrow's floored `modulo` kernel (R `%%` / Python `%` semantics) for + # exact types so int64 and decimal keep their native precision. The rewrite + # below casts both sides through float64, which loses that precision. + exact_type_ids <- .exact_arithmetic_type_ids() + numerator_is_exact <- args[[1]]$type_id() %in% exact_type_ids + denominator_is_exact <- args[[2]]$type_id() %in% exact_type_ids - if (numerator_is_int && denominator_is_int) { + if (numerator_is_exact && denominator_is_exact) { # `modulo` errors on divide-by-zero; R returns NA for integer `%% 0`. # Replace zero divisors before calling the kernel, then restore NA. zero <- eval_array_expression("equal", args[[2]], 0L) @@ -226,7 +226,15 @@ eval_array_expression <- function(FUN, ..., args = list(...), options = empty_na rem )) } - return(call_function("modulo", args = args)) + # We can't simply do {e1 - e2 * ( e1 %/% e2 )} since Ops.Array evaluates + # eagerly, but we can build that up. Floating-point `%%` therefore keeps + # R's float division semantics, and the result keeps the numerator's type. + quotient <- eval_array_expression("%/%", args = args) + base <- eval_array_expression("*", quotient, args[[2]]) + # this cast is to ensure that the result of this and e1 are the same + # (autocasting only applies to scalars) + base <- base$cast(args[[1]]$type) + return(eval_array_expression("-", args[[1]], base)) } call_function( @@ -243,6 +251,18 @@ eval_array_expression <- function(FUN, ..., args = list(...), options = empty_na arg } +# Type ids whose values are exact integers, so `%/%` and `%%` can use Arrow's +# integer/decimal kernels instead of dividing through float64. +.exact_arithmetic_type_ids <- function() { + Type[c( + toupper(INTEGER_TYPES), + "DECIMAL32", + "DECIMAL64", + "DECIMAL128", + "DECIMAL256" + )] +} + #' @export na.omit.ArrowDatum <- function(object, ...) { object$Filter(!is.na(object)) diff --git a/r/R/expression.R b/r/R/expression.R index 9f3790710cb7..587f5ab89094 100644 --- a/r/R/expression.R +++ b/r/R/expression.R @@ -216,14 +216,15 @@ Expression$op <- function(FUN, ..., args = list(...)) { } return(out) } else if (FUN == "%%") { - # Prefer `modulo` over rewriting through `%/%` (which goes via float64). - int_type_ids <- Type[toupper(INTEGER_TYPES)] - is_int <- function(x) { + # Prefer `modulo` for exact types over rewriting through `%/%`, which goes + # via float64 and loses int64/decimal precision. + exact_type_ids <- .exact_arithmetic_type_ids() + is_exact <- function(x) { is.integer(x) || - (inherits(x, "ArrowObject") && x$type_id() %in% int_type_ids) + (inherits(x, "ArrowObject") && x$type_id() %in% exact_type_ids) } - if (is_int(args[[1]]) && is_int(args[[2]])) { + if (is_exact(args[[1]]) && is_exact(args[[2]])) { if (inherits(args[[1]], "ArrowObject")) { out_type <- args[[1]]$type() } else { @@ -250,7 +251,7 @@ Expression$op <- function(FUN, ..., args = list(...)) { rem )) } - return(Expression$create("modulo", args = args)) + return(args[[1]] - args[[2]] * (args[[1]] %/% args[[2]])) } Expression$create(.operator_map[[FUN]], args = args) diff --git a/r/tests/testthat/test-dplyr-funcs-math.R b/r/tests/testthat/test-dplyr-funcs-math.R index be3eb3abc79e..e4dcefe012af 100644 --- a/r/tests/testthat/test-dplyr-funcs-math.R +++ b/r/tests/testthat/test-dplyr-funcs-math.R @@ -467,9 +467,15 @@ test_that("floor division maintains type consistency with R", { test_that("%% uses the modulo kernel for large int64 and decimals", { skip_if_not_installed("bit64") - # Beyond float64 exact integer range: the old %% rewrite via %/% errored. - big <- bit64::as.integer64("9007199254740993") - expect_equal(as.vector(Array$create(big) %% 7L), as.vector(big %% 7L)) + # 2^60 + 5 needs 61 bits, so the old %% rewrite, which divided through + # float64, could not return its exact remainder. 2^60 = (2^3)^20 = 1 mod 7, + # so the exact remainder is 6. + big <- Array$create(bit64::as.integer64("1152921504606846981")) + expect_equal(as.double(as.vector(big %% 7L)), 6) + expect_equal((big %% 7L)$type, int64()) + # R rounds towards negative infinity for %%, which the kernel also does. + negative <- Array$create(bit64::as.integer64("-7")) + expect_equal(as.double(as.vector(negative %% 3L)), 2) # Decimal with many significant digits: the old rewrite was silently wrong. d <- Scalar$create("12345678901234567890.12", string())$cast(decimal128(25, 2))