arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[R] arrow::write_parquet error with zero-length datetimes in R 4.5.2

Open pmac0451 opened this issue 10 months ago • 4 comments

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 when x is 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

pmac0451 avatar Jan 12 '26 16:01 pmac0451

I would like to work on this issue.

shashbha14 avatar Jan 13 '26 13:01 shashbha14

thanks @shashbha14 , I've assigned you the issue.

raulcd avatar Jan 13 '26 14:01 raulcd

@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!

shashbha14 avatar Jan 13 '26 17:01 shashbha14

Issue resolved by pull request 49619 https://github.com/apache/arrow/pull/49619

thisisnic avatar Apr 02 '26 12:04 thisisnic