Skip to content
Closed
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
15 changes: 13 additions & 2 deletions r/src/array_to_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,17 @@ bool ArraysCanFitInteger(ArrayVector arrays) {
return all_can_fit;
}

bool GetBoolOption(const std::string& name, bool default_) {
SEXP getOption = Rf_install("getOption");
cpp11::sexp call = Rf_lang2(getOption, Rf_mkString(name.c_str()));
cpp11::sexp res = Rf_eval(call, R_BaseEnv);
if (TYPEOF(res) == LGLSXP) {
return LOGICAL(res)[0] == TRUE;
} else {
return default_;
}
}

std::shared_ptr<Converter> Converter::Make(const std::shared_ptr<DataType>& type,
ArrayVector arrays) {
if (arrays.empty()) {
Expand Down Expand Up @@ -1068,8 +1079,8 @@ std::shared_ptr<Converter> Converter::Make(const std::shared_ptr<DataType>& type
return std::make_shared<arrow::r::Converter_Timestamp<int64_t>>(std::move(arrays));

case Type::INT64:
// Prefer integer if it fits
if (ArraysCanFitInteger(arrays)) {
// Prefer integer if it fits, unless option arrow.int64_downcast is `false`
if (GetBoolOption("arrow.int64_downcast", true) && ArraysCanFitInteger(arrays)) {
return std::make_shared<arrow::r::Converter_Int<arrow::Int64Type>>(
std::move(arrays));
} else {
Expand Down
2 changes: 2 additions & 0 deletions r/src/arrow_cpp11.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ std::vector<T> from_r_list(cpp11::list args) {
return vec;
}

bool GetBoolOption(const std::string& name, bool default_);

} // namespace r
} // namespace arrow

Expand Down
14 changes: 14 additions & 0 deletions r/tests/testthat/test-Array.R
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,17 @@ test_that("Array$ApproxEquals", {
expect_true(a$ApproxEquals(b))
expect_false(a$ApproxEquals(vec))
})

test_that("auto int64 conversion to int can be disabled (ARROW-10093)", {
op <- options(arrow.int64_downcast = FALSE); on.exit(options(op))

a <- Array$create(1:10, int64())
expect_true(inherits(a$as_vector(), "integer64"))

batch <- RecordBatch$create(x = a)
expect_true(inherits(as.data.frame(batch)$x, "integer64"))

tab <- Table$create(x = a)
expect_true(inherits(as.data.frame(batch)$x, "integer64"))
})