diff --git a/r/R/arrow-datum.R b/r/R/arrow-datum.R index 5ac72bf36856..f9b1a8df33d9 100644 --- a/r/R/arrow-datum.R +++ b/r/R/arrow-datum.R @@ -201,8 +201,34 @@ 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) 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_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) + 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 + )) + } # We can't simply do {e1 - e2 * ( e1 %/% e2 )} since Ops.Array evaluates - # eagerly, but we can build that up + # 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 @@ -225,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/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..587f5ab89094 100644 --- a/r/R/expression.R +++ b/r/R/expression.R @@ -216,6 +216,41 @@ Expression$op <- function(FUN, ..., args = list(...)) { } return(out) } else if (FUN == "%%") { + # 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% exact_type_ids) + } + + if (is_exact(args[[1]]) && is_exact(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(args[[1]] - args[[2]] * (args[[1]] %/% args[[2]])) } diff --git a/r/tests/testthat/test-dplyr-funcs-math.R b/r/tests/testthat/test-dplyr-funcs-math.R index 4db1d7441046..e4dcefe012af 100644 --- a/r/tests/testthat/test-dplyr-funcs-math.R +++ b/r/tests/testthat/test-dplyr-funcs-math.R @@ -464,6 +464,52 @@ 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") + + # 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)) + 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))