From fa62a020d05ecb44c9bd3f23ce29502b22240119 Mon Sep 17 00:00:00 2001 From: mrbuho-O <355083923+mrbuho-O@users.noreply.github.com> Date: Fri, 27 Nov 2026 23:49:39 +0100 Subject: [PATCH] GH-49836: [R] Reject unrepresentable Date values before converting to Arrow --- r/src/r_to_arrow.cpp | 24 +++++++++++++----------- r/tests/testthat/test-Array.R | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/r/src/r_to_arrow.cpp b/r/src/r_to_arrow.cpp index 9a26a2c3df7a..1dec73f193de 100644 --- a/r/src/r_to_arrow.cpp +++ b/r/src/r_to_arrow.cpp @@ -32,6 +32,8 @@ #include #include +#include + #include "./r_task_group.h" namespace arrow { @@ -567,8 +569,17 @@ class RPrimitiveConverter::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(std::numeric_limits::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(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); } @@ -598,15 +609,6 @@ class RPrimitiveConverter::value>> return VisitVector(it, size, append_null, append_value); } - static int FromRDate(const Date32Type*, double from) { - return static_cast(std::floor(from)); - } - - static int64_t FromRDate(const Date64Type*, double from) { - constexpr int64_t kMilliSecondsPerDay = 86400000; - return static_cast(std::floor(from * kMilliSecondsPerDay)); - } - static int FromPosixct(const Date32Type*, double from) { constexpr int64_t kSecondsPerDay = 86400; return static_cast(from / kSecondsPerDay); diff --git a/r/tests/testthat/test-Array.R b/r/tests/testthat/test-Array.R index ea3bff782663..a8edf5a57b2c 100644 --- a/r/tests/testthat/test-Array.R +++ b/r/tests/testthat/test-Array.R @@ -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"))