Skip to content

feat: Build Arrow date, time and timestamp arrays with extended_types - #960

Draft
krlmlr wants to merge 5 commits into
claude/rb-arrow-utf8-checksfrom
claude/rb-arrow-dates
Draft

krlmlr wants to merge 5 commits into
claude/rb-arrow-utf8-checksfrom
claude/rb-arrow-dates

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for users: Arrow chunks are fetched only through the unexported result_fetch_arrow_chunk() until the stream exists.

With dbConnect(extended_types = TRUE), a column declared DATE, TIME, DATETIME or TIMESTAMP, matched as dbFetch() matches it (datatype_from_decltype(), case-insensitive, exact names; an expression has no declared type and follows its values), now becomes date32, time64[us] or timestamp[us, UTC] in an Arrow chunk, whatever its values, also in a chunk of no rows or only NULL. Text is parsed with the parsers that the previous layer split from their warnings (parse_date(), parse_time(), parse_datetime_local()), and a UTC offset is applied in whole microseconds: a timestamp is shifted to UTC, and a time with an offset is a time of day in UTC, as SQLite's time() and dbFetch() read it. Numbers are days (truncated toward zero) or seconds (rounded to the microsecond nearest to the value the double holds, halves to even) since 1970, as dbFetch() reads them. The fraction times 1e6 is rounded to a double first, and the exact error of that rounding (from std::fma()) decides where it gives a half: 2.5e-6, a little more than 2.5 µs as a double, becomes 3 µs, where CPython's datetime.fromtimestamp() rounds the half to 2. Text that doesn't parse and blobs are NULL where dbFetch() warns, silently until the conversion reports exist (F4a), as for the other conversions of Arrow chunks.

Where the Arrow type can't hold what dbFetch() gives, the value is NULL, as #861 chose: a count of days beyond 32 bits (which dbFetch() wraps), a TIME outside the day such as 25:00:00 or a negative number however close to zero (which dbFetch() keeps as an hms duration), and microseconds beyond int64. A real TIME is checked against the day before rounding, and rounding neither takes it out of the day nor brings it in: -1e-7 is NULL as -0.1 is, and a real just before midnight that would round up to 24:00:00 stops at the last microsecond of the day. The open question from #861 about these durations carries over: should a TIME column whose values aren't times of day rather be a duration[us] array, or stay time64[us] with NULL? The values turned into NULL are to be reported by F4a.

Tests in tests/testthat/test-arrow-dates.R compare each chunk with dbFetch(extended_types = TRUE) on a separate result where the rules agree (expect_same_chunks() in helper-arrow.R, which also validates every chunk through nanoarrow's IPC round trip), and pin exact microseconds as integer64: offsets (±HH:MM, Z, surrounding whitespace, offsets SQLite rejects), times with offsets compared with SQLite's time() and timestamps with unixepoch(), fractions beyond microseconds, numbers as days or seconds including rounding of halves, of reals that hold a little more or less than a half microsecond, and of present-day timestamps, small negative TIME reals, dates and timestamps before 1970 and after 2038, values beyond the range of the Arrow types, invalid text and blobs, out-of-memory while converting UTF-16 text, NULL-only columns, mixed storage classes across chunk sizes, new executions after dbBind(), declared types such as TimeStamp, DATETIME(6), DATE_, aliases and expressions, a UTF-16 database, and the same columns without extended_types. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (locale), SKIP 63, PASS 12149.

Layer F3 of the rebuilt chain, planned in #880; based on #959. Ported from #861 (draft), without the parser split that already landed in the layer below and with #885's offset handling, so the old pins of negative offsets read as fractions of a second no longer apply.

Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

With `extended_types = TRUE`, a column declared DATE, TIME, DATETIME or TIMESTAMP,
as `dbFetch()` decides it, becomes date32, time64[us] or timestamp[us, UTC] in an Arrow chunk.
Values are read with the parsers that `dbFetch()` uses,
with a UTC offset applied in whole microseconds,
and numbers are days or seconds since 1970.
Text that doesn't parse, blobs, and values that the Arrow type can't hold are NULL,
silently until the conversion reports exist.

Ported from #861.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 06:27

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: should the Arrow arrays hold the values that dbFetch() gives where date32 and time64 can't?

Carried over from #861's question, which nobody answered; this pull request ports #861's date and time arrays and implements the default described below (NULL where date32 or time64 can't hold what dbFetch() gives, e.g. 25:00:00 or negative times, day counts beyond 32 bits). The links below point at #861's lines.

With extended_types = TRUE, the Arrow chunk has NULL where its type can't hold a value: a time outside [00:00:00, 24:00:00), which dbFetch() keeps as an hms duration, and a day count beyond 32 bits or a huge or infinite real in a DATE column, which dbFetch() wraps or saturates into a date near 1970 (#878). Numeric seconds are rounded to microseconds, which time64[us] and timestamp[us] hold. The rules are in the comments at src/SqliteArrowColumn.cpp#L455-L461 and #L489-L496, the range check of time64 at #L541-L543, and the tests pin both sides. Against this pull request's head, built from source:

library(DBI)
con <- dbConnect(RSQLite::SQLite(), extended_types = TRUE)
dbExecute(con, "CREATE TABLE t (id INTEGER, d DATE, tm TIME)")
#> [1] 0
dbExecute(con, "INSERT INTO t VALUES
  (1, 4294967297, 86400), (2, 1e300, -1), (3, 0, '25:00:00')")
#> [1] 3

# dbFetch() wraps or saturates the days (#878) and keeps the times as durations
dbGetQuery(con, "SELECT d, tm FROM t ORDER BY id")
#>            d        tm
#> 1 1970-01-02  24:00:00
#> 2 1969-12-31 -00:00:01
#> 3 1970-01-01  25:00:00

# The Arrow chunk: NULL where date32 and time64 can't hold the value
res <- dbSendQuery(con, "SELECT d, tm FROM t ORDER BY id")
chunk <- RSQLite:::result_fetch_arrow_chunk(res@ptr, -1L)
nanoarrow::convert_array(chunk)
#>            d tm
#> 1       <NA> NA
#> 2       <NA> NA
#> 3 1970-01-01 NA
dbClearResult(res)
dbDisconnect(con)

Created on 2026-11-13 with reprex v2.1.1

  • Keep NULL for these values, which feat: Report values converted to an Arrow column's type once per chunk, with their rows #862 reports once per chunk; a wrapped date isn't a valid answer, and a time beyond the day isn't one that time64 can hold (recommended)
  • Hold times outside the day in a duration[us] column instead of time64, as dbFetch() keeps them, and leave the dates as they are
  • Copy dbFetch() exactly, wrapped dates included, so that both paths always agree

Kirill decides. If nobody decides, the arrays keep NULL, and fixing #878 on main brings dbFetch() closer to them.


Generated by Claude Code

claude and others added 3 commits December 1, 2026 06:47
…imes out of the day

A TIME real is checked against the day before rounding,
so -1e-7 and -4e-7 are NULL as -0.1 is,
rather than rounding to 00:00:00.
A real just before midnight still stops at the last microsecond of the day.

The fraction of a second times 1e6 was rounded to a double before it was rounded to a microsecond,
which turned 2.5e-6, a little more than 2.5 microseconds as a double, into 2.
The error of the first rounding, exact from a fused multiply-add,
now decides where it gives a half.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants