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
24 changes: 13 additions & 11 deletions r/src/r_to_arrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <arrow/util/float16.h>
#include <arrow/util/logging.h>

#include <cmath>

#include "./r_task_group.h"

namespace arrow {
Expand Down Expand Up @@ -567,8 +569,17 @@ class RPrimitiveConverter<T, enable_if_t<is_date_type<T>::value>>
return Status::OK();
};
auto append_value = [this](r_value_type value) {
this->primitive_builder_->UnsafeAppend(FromRDate(this->primitive_type_, value));
return Status::OK();
using CType = typename T::c_type;
constexpr double kUnitsPerDay = T::UNIT == DateUnit::DAY ? 1 : 86400000;
constexpr double kMin = static_cast<double>(std::numeric_limits<CType>::min());
double converted = std::floor(value * kUnitsPerDay);
// The exclusive upper bound is exact in double even for int64_t.
if (converted >= kMin && converted < -kMin) {
this->primitive_builder_->UnsafeAppend(static_cast<CType>(converted));
return Status::OK();
}
return Status::Invalid("R Date value ", value, " cannot be represented as ",
this->primitive_type_->ToString());
};
return VisitVector(it, size, append_null, append_value);
}
Expand Down Expand Up @@ -598,15 +609,6 @@ class RPrimitiveConverter<T, enable_if_t<is_date_type<T>::value>>
return VisitVector(it, size, append_null, append_value);
}

static int FromRDate(const Date32Type*, double from) {
return static_cast<int>(std::floor(from));
}

static int64_t FromRDate(const Date64Type*, double from) {
constexpr int64_t kMilliSecondsPerDay = 86400000;
return static_cast<int64_t>(std::floor(from * kMilliSecondsPerDay));
}

static int FromPosixct(const Date32Type*, double from) {
constexpr int64_t kSecondsPerDay = 86400;
return static_cast<int>(from / kSecondsPerDay);
Expand Down
21 changes: 21 additions & 0 deletions r/tests/testthat/test-Array.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,27 @@ test_that("array supports Date (ARROW-3340)", {
expect_array_roundtrip(d, date32())
})

test_that("R Date conversion only accepts values representable in the Arrow date type", {
date <- function(days) as.Date(days, origin = "1970-01-01")
dates <- date(c(-1, 0, NA_real_, 1))
expect_equal(as.vector(arrow_array(dates, type = date32())), dates)
timestamps <- as.vector(arrow_array(dates, type = date64()))
expect_equal(as.numeric(timestamps), c(-86400, 0, NA, 86400))

for (type in list(date32(), date64())) {
for (value in c(Inf, -Inf, NaN)) {
expect_error(arrow_array(date(c(1, value)), type = type), "cannot be represented as date")
}
}

date32_limits <- date(c(-2^31, 2^31 - 1))
expect_equal(as.vector(arrow_array(date32_limits, type = date32())), date32_limits)
for (value in c(-2^31 - 1, 2^31)) {
expect_error(arrow_array(date(value)), "cannot be represented as date32")
}
expect_error(arrow_array(date(2^37), type = date64()), "cannot be represented as date64")
})

test_that("array supports POSIXct (ARROW-3340)", {
times <- lubridate::ymd_hms("2018-10-07 19:04:05") + 1:10
expect_array_roundtrip(times, timestamp("us", "UTC"))
Expand Down