Skip to content

feat: Bind dates, times, timestamps, durations, half floats and dictionaries from an Arrow stream - #970

Draft
krlmlr wants to merge 8 commits into
claude/rb-arrow-bind-streamfrom
claude/rb-arrow-bind-types
Draft

krlmlr wants to merge 8 commits into
claude/rb-arrow-bind-streamfrom
claude/rb-arrow-bind-types

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for users: the internal bind_arrow_stream() of #967 accepts more Arrow types, and no DBI method calls it yet.

What changes

bind_arrow_stream() now binds date32, date64, time32, time64, timestamp and duration columns of every unit, with or without a time zone, half floats, and dictionary arrays. Before, they failed with "Can't bind parameter 1 of Arrow type …".

The temporal types bind as reals, the very doubles that dbBind() binds for the R vectors nanoarrow converts them to: date32 as days (Date), date64 and timestamp as seconds since the epoch (POSIXct, whatever the time zone), time32 and time64 as seconds since midnight (hms), duration as seconds (difftime in seconds). The value is converted to a double and multiplied by the same factor nanoarrow uses (1e-3, 1e-6, 1e-9). A division by the units per second rounds differently in the last bit for values such as 9 ms, 5 us or 3 ns, and the tests catch that. dbBind() itself ignores extended_types: it binds a Date, POSIXct, hms or difftime as its bare double, so the storage class depends only on the declared column type, the same way for both paths.

Half floats bind as reals, decoded as IEEE 754 binary16 in RSQLite: subnormals as their mantissa times 2^-24, infinities as infinities, and NaN as NULL, as SQLite binds any NaN. This deliberately differs from nanoarrow's conversion to R for subnormals: ArrowHalfFloatToFloat() of nanoarrow 0.9.0 doesn't renormalize them, so 0x0001 converts to 1.1e-41 instead of 2^-24. A dictionary binds the value its index points to, with indices of any integer type, also for a sliced dictionary or a sliced index array, and also when the dictionary changes from one array of the stream to the next. A null index or a null dictionary value binds NULL. The indices of each array are checked before the full validation that #967 runs, so an index out of range fails before any row of that array runs with "Can't bind parameter 1 ($x) in row 2: the dictionary index 2 is out of range for 2 values.", with rows counted across the stream. A uint64 index beyond the range of int64 shows its unsigned value. An index in a child slot outside a sliced struct's rows, also a uint64 one of 2^63 or more, still gets nanoarrow's generic validation message. An out-of-range index in a null struct row is refused too, as nanoarrow's full validation does. When a later array of the stream fails this way, the rows of earlier arrays have already run, the same as for other errors in the middle of a stream. A dictionary of values that can't be bound, and nested struct columns, are still refused with the type error.

Where the Arrow path can't match dbBind()

  • A difftime in units other than seconds: dbBind() binds its bare number (90 for 90 minutes), while nanoarrow converts a duration to a difftime in seconds, so a duration binds as seconds (5400). dbBind() of nanoarrow's conversion gives 5400 too.
  • date64 binds as seconds since the epoch, not as days, because nanoarrow converts it to POSIXct.
  • A dictionary binds its decoded value without the "Factors converted to character" warning that dbBind() gives for a factor. nanoarrow converts a dictionary of strings to character, and dbBind() binds that without a warning.
  • Half float subnormals bind their correct value, unlike nanoarrow's conversion (see above).
  • Nanosecond values beyond 2^53, such as today's timestamps in nanoseconds, are rounded to a double without a warning. nanoarrow warns about the loss of precision when it converts them, then dbBind() binds the same rounded double.

Tests

In tests/testthat/test-arrow-bind.R, every type and unit is bound both through bind_arrow_stream() and with dbBind() of nanoarrow's conversion, with a NULL in between, negative values, extended_types off and on, and untyped and declared columns (the type dbDataType() gives). The tests compare typeof() and the values, and check the exact doubles for untyped columns. Replacing the multiplication with a division makes them fail. There are also tests for half floats by their bits (0x0001, 0x03ff, 0x0400, ±0, ±inf, NaN), a duration against a difftime in minutes, a round trip of the int64, double, utf8, binary, date32, time64 and timestamp columns of the read path with extended_types, dictionaries (a factor with an unused level, every index type, null indices and values, a sliced array over a sliced dictionary, a dictionary of timestamps in milliseconds, a dictionary that changes between arrays, indices out of range in the first and a later array including a negative one and a uint64 one beyond 2^63, values that can't be bound), and sliced struct arrays with and without their own validity bitmap and with sliced columns, which #967 supports but didn't test. arrow_int_array() in helper-arrow.R now builds small negative 64-bit values exactly, in native byte order. Two tests that used date32 as a type that can't be bound now use a nested data frame, since every type of RSQLite's read path now binds; in the test of reading a stream first, another result of the connection reads meanwhile, and the type error leaves it and the stream unread. The full suite with NOT_CRAN=true: FAIL 0, WARN 3 (locale warnings in test-encoding.R), SKIP 64, PASS 13423. valgrind (--leak-check=full) reports no errors and nothing lost for test-arrow-bind.R.

Layer F8b of the rebuilt chain, planned in #880; based on #967. Ported from #866 (temporal types, half floats, sliced struct tests) and the dictionary part of #867.

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


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

…onaries from an Arrow stream

date32, date64, time32, time64, timestamp and duration parameters of any unit bind as reals,
the very doubles that dbBind() binds for the Date, POSIXct, hms and difftime that nanoarrow converts them to:
the value is converted to a double and multiplied by the same factor as in nanoarrow.
A timestamp binds as an instant, whatever its time zone.
Half floats bind as reals.
A dictionary binds the value its index points to;
the full validation of each array already checks the indices.
Nested struct columns are still refused.

The tests bind each type and unit both ways, with extended_types on and off,
untyped and with the declared type of dbDataType(),
and compare the storage classes and values.
They also cover dictionaries, a round trip of the read path's temporal columns,
and sliced struct arrays with sliced columns.

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 10:13
claude and others added 5 commits December 1, 2026 10:13
…a dictionary index out of range

nanoarrow's ArrowHalfFloatToFloat() decodes subnormals wrongly,
so half floats are now decoded as IEEE 754 binary16 in RSQLite.
This deliberately differs from nanoarrow's conversion to R for subnormals.
Dictionary indices are checked before the full validation,
which named neither the parameter nor the row
and read a uint64 index beyond INT64_MAX as negative.

The tests add half floats by their bits, indices out of range in a later array,
a dictionary of timestamps in milliseconds, a dictionary that changes between arrays,
and restore a type that can't be bound in the test of reading a stream first.

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