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

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


The following commit(s) were added to refs/heads/main by this push:
     new acd8e44293 GH-40886: [R] Cryptic error when creating Arrow array from 
POSIXct with invalid time zones (#49714)
acd8e44293 is described below

commit acd8e442939b5fbf90d0cd65ca506514ec3851c4
Author: Nic Crane <[email protected]>
AuthorDate: Tue Apr 21 10:56:21 2026 -0400

    GH-40886: [R] Cryptic error when creating Arrow array from POSIXct with 
invalid time zones (#49714)
    
    ### Rationale for this change
    
    Horrible error with invalid timezones
    
    ### What changes are included in this PR?
    
    Do more checking of type
    
    ### Are these changes tested?
    
    Yup
    
    ### Are there any user-facing changes?
    
    No
    
    ### AI usage
    
    Done with Codex, but I went through it myself and am happy with it.
    
    * GitHub Issue: #40886
    
    Authored-by: Nic Crane <[email protected]>
    Signed-off-by: Nic Crane <[email protected]>
---
 r/src/type_infer.cpp         |  4 ++++
 r/tests/testthat/test-type.R | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/r/src/type_infer.cpp b/r/src/type_infer.cpp
index a8334387c5..76a1499305 100644
--- a/r/src/type_infer.cpp
+++ b/r/src/type_infer.cpp
@@ -72,6 +72,8 @@ std::shared_ptr<arrow::DataType> 
InferArrowTypeFromVector<INTSXP>(SEXP x) {
     if (Rf_isNull(tzone_sexp)) {
       auto systzone_sexp = cpp11::package("base")["Sys.timezone"];
       return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(systzone_sexp(), 0)));
+    } else if (TYPEOF(tzone_sexp) != STRSXP) {
+      cpp11::stop("`tzone` attribute of a `POSIXct` vector must be a character 
vector");
     } else {
       return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(tzone_sexp, 0)));
     }
@@ -89,6 +91,8 @@ std::shared_ptr<arrow::DataType> 
InferArrowTypeFromVector<REALSXP>(SEXP x) {
     if (Rf_isNull(tzone_sexp)) {
       auto systzone_sexp = cpp11::package("base")["Sys.timezone"];
       return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(systzone_sexp(), 0)));
+    } else if (TYPEOF(tzone_sexp) != STRSXP) {
+      cpp11::stop("`tzone` attribute of a `POSIXct` vector must be a character 
vector");
     } else {
       return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(tzone_sexp, 0)));
     }
diff --git a/r/tests/testthat/test-type.R b/r/tests/testthat/test-type.R
index 96968830f3..74c868faac 100644
--- a/r/tests/testthat/test-type.R
+++ b/r/tests/testthat/test-type.R
@@ -53,6 +53,25 @@ test_that("infer_type() infers from R type", {
   )
 })
 
+test_that("infer_type() errors clearly for POSIXct with invalid tzone", {
+  x <- as.POSIXct("2019-02-14 13:55:05", tz = "UTC")
+  attr(x, "tzone") <- 123
+
+  expect_error(
+    infer_type(x),
+    "`tzone` attribute of a `POSIXct` vector must be a character vector"
+  )
+
+  # Also check zero-length POSIXct
+  x <- as.POSIXct(x = NULL)
+  attr(x, "tzone") <- 123
+
+  expect_error(
+    infer_type(x),
+    "`tzone` attribute of a `POSIXct` vector must be a character vector"
+  )
+})
+
 test_that("infer_type() default method errors for unknown classes", {
   vec <- structure(list(), class = "class_not_supported")
 

Reply via email to