Repository navigation
fix: Parse infinity, BC dates, and years beyond 9999 in dates and timestamps - #660
Conversation
…imestamps `fetch_date()` and `convert_datetime()` read dates and timestamps at fixed character positions. `infinity`, `-infinity`, BC dates, and years above 9999 came back as garbage, and a short value such as `infinity` was read past its end. The parsers now walk the value with a bounds check at every position. `infinity` and `-infinity` become `Inf` and `-Inf`, a ` BC` suffix gives astronomical years (1 BC is year 0), and offsets from UTC with seconds, as PostgreSQL emits for local mean time, are honored. A value in any other shape gives `NA`. Closes #655. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Question: should
|
Question: what should a date or timestamp in an unexpected shape give?RPostgres sets
Generated by Claude Code |
`days_from_civil()` computes in `int` and overflows from year 5874898 on. PostgreSQL never emits such a year, the largest `date` is in 5874897, so a value beyond it now gives `NA` like any other unparseable value. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Question: warn once per column when dates or timestamps cannot be parsed?If a user changes
Generated by Claude Code |
Accepted limitations
Generated by Claude Code |
Closes #655.
PqColumnDataSource::fetch_date()andPqColumnDataSource::convert_datetime()readdate,timestampandtimestamptzvalues at fixed character positions.infinity,-infinity, BC dates and years above 9999 came back as garbage, and a value shorter than the fixed layout (infinity) was read past its end. Both parsers now walk the value with small helpers that bounds-check every position againstval + strlen(val); no newPQgetvalue()/PGresultreads were added.Behavior now:
infinityand-infinitygiveInfand-InfforDateand fortimestamptz(POSIXct).BCsuffix (which PostgreSQL emits after any UTC offset) gives astronomical years: 1 BC is year 0, 44 BC is year -43.date, 294276 for timestamps); a year beyond PostgreSQL's maximum givesNA, sincedays_from_civil()would overflowint.+HH,+HH:MMand+HH:MM:SSare honored. The seconds part is new: before,+05:53:28(local mean time before 1854 inAsia/Kolkata) was read as+05:53, giving a result 28 seconds off.strtod(), so they no longer depend on the locale.DateStylechanged by the user) givesNAinstead of garbage.time/timetz(fetch_time()) were checked and left alone: PostgreSQL always emits at leastHH:MM:SS, so its fixed positions stay inside the value, and an ASan run overtimeandtimetzvalues onmainreported nothing.timestampwithout time zone: the C++ side now returnsInf/-Inf, butfix_timezone()inR/PqResult.Rcallslubridate::force_tz(), which turns them intoNA. The result is now stable across queries, but not infinite; see the accepted limitations.Sanitizer evidence
ASan build (
-fsanitize=address, libasan preloaded into R). The probe runsSELECT repeat('x', 1000) AS a, repeat('x', n) AS b, <value> AS cfornin 0 to 1100, so that the value lands at the end of a 2048-byte libpq result block at somen.On
main(41cd4be), with'infinity'::timestamp:With
'infinity'::dateonmain:On this branch, the same probe over
infinity/-infinityfordate,timestampandtimestamptz, BC dates and timestamps, a year-10000 timestamp,timeandtimetz: no error. The full test suite under the ASan build also reports no error.Tests
New
tests/testthat/test-datetime-parsing.Rcovers infinite values, results independent of the rest of the query, BC dates and years above 9999 fordate,timestampandtimestamptz(checked against PostgreSQL's ownextract(epoch ...)and date differences), fractional seconds, and offsets with seconds. It fails onmainand passes here;devtools::test()passes locally against PostgreSQL 16.Review
A clean review found nothing above Low. Its equivalence check: 1e5 random timestamps in six time zones are bit-identical to
mainexcept for two improvements, BC and large years matchextract(epoch ...), and ASan and UBSan runs are clean. The one Low finding that needed a fix (year overflow indays_from_civil()) is fixed; the remaining limitations are listed in the Accepted limitations comment.ALTREP phase 1 (#632) will move these parsers into shared functions and carry this fix along.
Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Generated by Claude Code