diff --git a/r/R/compute.R b/r/R/compute.R index d5da9024bf46..30dd4b700af8 100644 --- a/r/R/compute.R +++ b/r/R/compute.R @@ -301,8 +301,9 @@ value_counts <- function(x) { #' Cast options #' #' @param safe logical: enforce safe conversion? Default `TRUE` -#' @param ... additional cast options, such as `allow_int_overflow`, -#' `allow_time_truncate`, and `allow_float_truncate`, which are set to `!safe` +#' @param ... additional cast options: `allow_int_overflow`, +#' `allow_time_truncate`, `allow_time_overflow`, `allow_decimal_truncate`, +#' `allow_float_truncate`, and `allow_invalid_utf8`, which are set to `!safe` #' by default #' @return A list #' @export @@ -311,7 +312,10 @@ cast_options <- function(safe = TRUE, ...) { opts <- list( allow_int_overflow = !safe, allow_time_truncate = !safe, - allow_float_truncate = !safe + allow_time_overflow = !safe, + allow_decimal_truncate = !safe, + allow_float_truncate = !safe, + allow_invalid_utf8 = !safe ) modifyList(opts, list(...)) } diff --git a/r/man/cast_options.Rd b/r/man/cast_options.Rd index 40d78052c749..5b84d07d101d 100644 --- a/r/man/cast_options.Rd +++ b/r/man/cast_options.Rd @@ -9,8 +9,9 @@ cast_options(safe = TRUE, ...) \arguments{ \item{safe}{logical: enforce safe conversion? Default \code{TRUE}} -\item{...}{additional cast options, such as \code{allow_int_overflow}, -\code{allow_time_truncate}, and \code{allow_float_truncate}, which are set to \code{!safe} +\item{...}{additional cast options: \code{allow_int_overflow}, +\code{allow_time_truncate}, \code{allow_time_overflow}, \code{allow_decimal_truncate}, +\code{allow_float_truncate}, and \code{allow_invalid_utf8}, which are set to \code{!safe} by default} } \value{ diff --git a/r/src/compute.cpp b/r/src/compute.cpp index c8aa903bf063..f881b6148322 100644 --- a/r/src/compute.cpp +++ b/r/src/compute.cpp @@ -591,20 +591,19 @@ std::shared_ptr make_cast_options(cpp11::list optio out->to_type = cpp11::as_cpp>(to_type); } - SEXP allow_float_truncate = options["allow_float_truncate"]; - if (!Rf_isNull(allow_float_truncate) && cpp11::as_cpp(allow_float_truncate)) { - out->allow_float_truncate = cpp11::as_cpp(allow_float_truncate); - } - - SEXP allow_time_truncate = options["allow_time_truncate"]; - if (!Rf_isNull(allow_time_truncate) && cpp11::as_cpp(allow_time_truncate)) { - out->allow_time_truncate = cpp11::as_cpp(allow_time_truncate); - } - - SEXP allow_int_overflow = options["allow_int_overflow"]; - if (!Rf_isNull(allow_int_overflow) && cpp11::as_cpp(allow_int_overflow)) { - out->allow_int_overflow = cpp11::as_cpp(allow_int_overflow); - } + auto set_bool_option = [&options](const char* name, bool* field) { + SEXP value = options[name]; + if (!Rf_isNull(value)) { + *field = cpp11::as_cpp(value); + } + }; + + set_bool_option("allow_int_overflow", &out->allow_int_overflow); + set_bool_option("allow_time_truncate", &out->allow_time_truncate); + set_bool_option("allow_time_overflow", &out->allow_time_overflow); + set_bool_option("allow_decimal_truncate", &out->allow_decimal_truncate); + set_bool_option("allow_float_truncate", &out->allow_float_truncate); + set_bool_option("allow_invalid_utf8", &out->allow_invalid_utf8); return out; } diff --git a/r/tests/testthat/test-Array.R b/r/tests/testthat/test-Array.R index ea3bff782663..dc94eff66f29 100644 --- a/r/tests/testthat/test-Array.R +++ b/r/tests/testthat/test-Array.R @@ -432,6 +432,43 @@ test_that("integer types cast safety (ARROW-3741, ARROW-5541)", { } }) +test_that("all cast options are passed through to C++ (GH-20395)", { + # allow_decimal_truncate + dec <- arrow_array(c(1.5, 2.25))$cast(decimal128(10, 2)) + expect_error(dec$cast(int32())) + expect_equal( + as.vector(dec$cast(int32(), allow_decimal_truncate = TRUE)), + c(1L, 2L) + ) + expect_error(dec$cast(int32(), safe = FALSE), NA) + expect_error(dec$cast(int32(), safe = FALSE, allow_decimal_truncate = FALSE)) + + # allow_invalid_utf8 + bin <- arrow_array(list(as.raw(c(0xff, 0xfe)))) + expect_error(bin$cast(utf8())) + expect_error(bin$cast(utf8(), allow_invalid_utf8 = TRUE), NA) + expect_error(bin$cast(utf8(), safe = FALSE), NA) + + # allow_time_overflow + ts <- arrow_array(1e11)$cast(int64())$cast(timestamp("s")) + expect_error(ts$cast(timestamp("ns"))) + expect_error(ts$cast(timestamp("ns"), allow_time_overflow = TRUE), NA) + expect_error(ts$cast(timestamp("ns"), safe = FALSE), NA) +}) + +test_that("cast_options() sets all options from safe", { + opts <- cast_options(safe = FALSE) + expect_true(all(unlist(opts))) + expect_named( + opts, + c( + "allow_int_overflow", "allow_time_truncate", "allow_time_overflow", + "allow_decimal_truncate", "allow_float_truncate", "allow_invalid_utf8" + ) + ) + expect_false(any(unlist(cast_options(safe = TRUE)))) +}) + test_that("float types casts (ARROW-3741)", { x <- c(1, 2, 3, NA) a <- arrow_array(x) diff --git a/r/tests/testthat/test-dplyr-funcs-type.R b/r/tests/testthat/test-dplyr-funcs-type.R index 928a0e3d2412..de3d2369c262 100644 --- a/r/tests/testthat/test-dplyr-funcs-type.R +++ b/r/tests/testthat/test-dplyr-funcs-type.R @@ -206,6 +206,25 @@ test_that("explicit type conversions with as.*()", { ) }) +test_that("as.integer() and as.integer64() truncate decimals (GH-20395)", { + # These bindings pass allow_decimal_truncate = TRUE, so decimals with a + # fractional part are truncated towards zero (like base::as.integer() on + # doubles) rather than raising an error + tab <- arrow_table(dec = arrow_array(c(1.5, 2.25, -1.75, NA))$cast(decimal128(10, 2))) + expect_equal( + tab |> + transmute( + dec2int = as.integer(dec), + dec2i64 = bit64::as.integer64(dec) + ) |> + collect(), + tibble( + dec2int = c(1L, 2L, -1L, NA), + dec2i64 = bit64::as.integer64(c(1, 2, -1, NA)) + ) + ) +}) + test_that("is.finite(), is.infinite(), is.nan()", { df <- tibble( x = c(