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
10 changes: 7 additions & 3 deletions r/R/compute.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(...))
}
5 changes: 3 additions & 2 deletions r/man/cast_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 13 additions & 14 deletions r/src/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,20 +591,19 @@ std::shared_ptr<arrow::compute::CastOptions> make_cast_options(cpp11::list optio
out->to_type = cpp11::as_cpp<std::shared_ptr<arrow::DataType>>(to_type);
}

SEXP allow_float_truncate = options["allow_float_truncate"];
if (!Rf_isNull(allow_float_truncate) && cpp11::as_cpp<bool>(allow_float_truncate)) {
out->allow_float_truncate = cpp11::as_cpp<bool>(allow_float_truncate);
}

SEXP allow_time_truncate = options["allow_time_truncate"];
if (!Rf_isNull(allow_time_truncate) && cpp11::as_cpp<bool>(allow_time_truncate)) {
out->allow_time_truncate = cpp11::as_cpp<bool>(allow_time_truncate);
}

SEXP allow_int_overflow = options["allow_int_overflow"];
if (!Rf_isNull(allow_int_overflow) && cpp11::as_cpp<bool>(allow_int_overflow)) {
out->allow_int_overflow = cpp11::as_cpp<bool>(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<bool>(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;
}

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