This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new da00b65f fix(r): sub-day precision Date should be floored when treated 
as integer (#674)
da00b65f is described below

commit da00b65f24afbc4fd144b528993b27d0f7af7ee9
Author: eitsupi <[email protected]>
AuthorDate: Mon Nov 4 12:12:01 2024 +0900

    fix(r): sub-day precision Date should be floored when treated as integer 
(#674)
    
    Fix #665
---
 r/R/as-array.R                   |  8 +++++++-
 r/tests/testthat/test-as-array.R | 11 +++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/r/R/as-array.R b/r/R/as-array.R
index e7285d48..cf72bf73 100644
--- a/r/R/as-array.R
+++ b/r/R/as-array.R
@@ -262,8 +262,14 @@ as_nanoarrow_array.Date <- function(x, ..., schema = NULL) 
{
   switch(
     parsed$type,
     date32 = {
+      int_vec <- if (is.integer(x)) {
+        unclass(x)
+      } else {
+        as.integer(floor(as.numeric(x)))
+      }
+
       storage <- as_nanoarrow_array(
-        as.integer(x),
+        int_vec,
         schema = na_type(parsed$storage_type)
       )
       nanoarrow_array_set_schema(storage, schema)
diff --git a/r/tests/testthat/test-as-array.R b/r/tests/testthat/test-as-array.R
index 1a95b643..c9f6d0eb 100644
--- a/r/tests/testthat/test-as-array.R
+++ b/r/tests/testthat/test-as-array.R
@@ -466,6 +466,17 @@ test_that("as_nanoarrow_array() works for Date -> 
na_date32()", {
     as.raw(array$buffers[[2]]),
     as.raw(as_nanoarrow_buffer(c(10957L, 19391L, NA)))
   )
+
+  # Sub-day precision handling
+  expect_identical(
+    as.vector(as_nanoarrow_array(as.Date(c(-0.5, 0, 0.5), origin = 
as.Date("1970-01-01")))),
+    as.Date(c(-1, 0, 0), origin = as.Date("1970-01-01"))
+  )
+  # Integer based Date support
+  expect_identical(
+    as.vector(as_nanoarrow_array(as.Date(c(-1L, 0L, 1L), origin = 
as.Date("1970-01-01")))),
+    as.Date(c(-1, 0, 1), origin = as.Date("1970-01-01"))
+  )
 })
 
 test_that("as_nanoarrow_array() works for Date -> na_date64()", {

Reply via email to