Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion r/R/arrow-datum.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Comment on lines +218 to +225
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
Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions r/R/dplyr-funcs-simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
35 changes: 35 additions & 0 deletions r/R/expression.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]))
}

Expand Down
46 changes: 46 additions & 0 deletions r/tests/testthat/test-dplyr-funcs-math.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Loading