[R] arrow::write_parquet error with zero-length datetimes in R 4.5.2
Description
Using R 4.5.2 there is an error using arrow::write_parquet with zero-length datetimes:
Error: Invalid: Invalid conversion to timestamp
I believe this may be caused by the following change in the release note for R 4.5.2 (patched):
str(x, give.attr=FALSE)no longer shows attributes whenxis a zero length "Date" or "POSIXt" object.
I can't be sure if the error was present before the patched release of 4.5.2 on 5 Jan 2026.
Reprex
dplyr::tibble(
timeStamp = as.POSIXct(x = NULL), # works ok with this line commented out
otherColumn = character()
) |>
arrow::write_parquet(
sink = tempfile(fileext = ".parquet")
)
Component(s)
R
I would like to work on this issue.
thanks @shashbha14 , I've assigned you the issue.
@raulcd Yes, I have completed the analysis and solution.
Root Cause:
The crash occurs in r/src/type_infer.cpp at lines 72 and 89. The code checks if the timezone attribute is NULL, but doesn't verify it has content before reading it. In R 4.5.2, zero-length POSIXct objects can have a tzone attribute with length 0, causing an out-of-bounds memory access when the code tries to read STRING_ELT(tzone_sexp, 0).
Solution: Added a length check before accessing the timezone:
- Changed:
if (Rf_isNull(tzone_sexp)) - To:
if (Rf_isNull(tzone_sexp) || XLENGTH(tzone_sexp) == 0)
Applied to both INTSXP (line 72) and REALSXP (line 89) specializations.
Testing: Created r/tests/testthat/test-issue-48832.R with a regression test that reproduces the exact scenario from the bug report.
Preparing PR now!
Issue resolved by pull request 49619 https://github.com/apache/arrow/pull/49619