Skip to content

feat: Build Arrow arrays of requested integer, unsigned, boolean and float types - #979

Draft
krlmlr wants to merge 12 commits into
claude/rb-arrow-stream-threadfrom
claude/rb-arrow-requested-types
Draft

krlmlr wants to merge 12 commits into
claude/rb-arrow-stream-threadfrom
claude/rb-arrow-requested-types

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for users: this adds the internal ability for an Arrow fetch to build its columns as requested types instead of the types that the first chunk infers. The internal chunk fetch (fetch_arrow_chunk_warn(), result_fetch_arrow_chunk()) and stream creation (read_arrow_stream(), result_create_arrow_stream()) take a schema, a struct nanoarrow schema whose children request the column types by position. Only the types count: the names, nullability, metadata, extension and flags of the schema and its children are ignored, so the columns keep the result's names and are always nullable. Plan item 18 exposes this as the user-facing schema argument, item 14 adds large, date64, time and timestamp types in other units and time zones.

Requested types are fixed from the first chunk of the execution on, as if an earlier chunk had decided them, whatever the values and declared types, also for columns of only NULL values and for results without rows. The result fixes them only once a chunk has been built: after a first fetch that fails partway, later fetches raise that failure, before any check of a new request, and a new execution can request other types. Besides the types that a first chunk decides (null, int64, double, utf8, binary, date32, time64[us], timestamp[us, UTC]), a fetch can request int8, int16, int32, uint8, uint16, uint32, uint64, bool and float. Half float is not supported yet: nanoarrow's encoder truncates and flushes subnormals, so it would need its own conversion.

Values that don't fit go through the warnings of #961, by column and kind, consistent with the existing rules for a fixed int64 column. Integer types truncate reals toward zero (counted as truncated), read text and blobs as their leading integer as for CAST() (counted as converted), and give NULL beyond their range, negative values in unsigned types included (counted as out of range); uint64 reads text beyond 2^63 - 1 in full where SQLite would saturate it. Bool is the integer type of the range 0 to 1: 0.5 and 1.5 are truncated, -1 and 2 are NULL, and text such as 'true' reads as 0 as for CAST(). Float takes the nearest float, counts integers beyond 2^24 that it rounds (the "rounded" warning now names the type), doesn't count reals, and gives NULL for reals beyond its range; infinities stay. A requested int64 truncates reals in the first chunk instead of widening to double.

A schema with another number of columns, a type that can't be built (half float, large types, date64, other time units or zones, dictionaries, nested and extension types), or a non-struct schema fails before any row is read and before the fetch claims the result, so a data-frame fetch still works afterwards. A request after the first chunk must request the types it has fixed; no request keeps them. A new execution infers its types anew unless they are requested again. Byte limits (#963) apply only to utf8 and binary columns, so a requested numeric column takes text of any size; a kept chunk (#962) comes back with its requested types.

Tested in the new tests/testthat/test-arrow-requested-types.R: a failed first fetch (red before the fix in db996de, which raised the error about fixed types), nullability and metadata, each type at its minimum and maximum and just beyond, reals, text, blobs (also in a UTF-16 database), NULL columns, empty results, declared types, chunks and new executions, warnings, byte limits, kept chunks with warn = 2, streams, and wrong schemas. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (the zh_TW.UTF-8 locale is missing in the container), SKIP 64, PASS 13710. Valgrind on the new test file: 0 errors, 0 bytes definitely lost.

Item 13 of the plan in #829 (comment), a layer of the rebuilt chain planned in #880; based on #978 (item 15, the stream-thread check), which sits on #977. The approach to the integer ranges and to uint64 text is ported from the first draft #801, adapted to the fixed kinds of the rebuilt chain.

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


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

…float types

The internal Arrow chunk fetch and stream take a `schema`,
a nanoarrow schema of a struct type,
whose children request the types of the result columns by position.
The requested types are fixed from the first chunk of the execution on,
as if an earlier chunk had decided them,
also for columns of only NULL values and for results without rows.
Besides the types that a first chunk decides,
a fetch can request int8 to int64, uint8 to uint64, bool and float.

Values that don't fit go through the warnings about converted values:
integer types truncate reals and read text as its leading integer,
uint64 also beyond 2^63 - 1,
and give NULL beyond their range, negative values in unsigned types too;
bool is the integer type of the range 0 to 1;
float takes the nearest float and counts integers that it rounds.
A schema with another number of columns or an unsupported type
fails before any row is read and leaves the result unclaimed.
A request after the first chunk must match the types it has fixed.

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 12:49

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: what should a requested bool column make of values other than 0 and 1?

This pull request lets an Arrow fetch build a column as a requested type, among them bool. SQLite has no boolean storage class, so a requested bool column has to decide what to do with integers other than 0 and 1, with reals, and with text. As implemented, bool is the integer type of range 0 to 1, see integer_range(): -1 and 2 become NULL with an out-of-range warning, 0.5 and 1.5 are truncated with a warning, and the text 'true' reads as 0, that is false, as SQLite's CAST('true' AS INTEGER) does. The alternative follows C and R: any nonzero number is true, and only 0 is false. Text would still follow CAST(), unless it is matched as 'true'/'false' too.

  • bool as the integers 0 and 1, with out-of-range values NULL and a warning, as implemented: no value changes meaning silently (recommended)
  • Any nonzero number is true, as in C and R's as.logical(): friendlier for columns that store flags as arbitrary integers, but 2 and -1 turn into true without a warning
  • As the previous option, and the text 'true'/'false' (any case) also read as true and false

Kirill decides. Until then the first option stays as implemented.


Generated by Claude Code

krlmlr and others added 10 commits December 1, 2026 12:56
A first fetch that failed left the requested types fixed,
so a later request for other types failed with an error about them
instead of the failure of the execution.
The chunk sink now takes the requested types,
and the result fixes them only once the chunk is built;
an Arrow stream passes its request on with each fetch.
The failure of the execution comes before the checks of the request.
The comments say that only the types of a requested schema count:
columns stay nullable and carry no metadata or flags.

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

The stream keeps the requested types in its state and passes them on
with each fetch, also on a worker thread,
whose warnings go through the queue of the main thread.

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

The `schema` arguments follow the entry points of the stream:
the R wrappers flush the warnings of worker reads first,
and the C++ functions hold the result for their whole run.

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

Creating a stream checks the requested types when it claims the result,
before it reads the rest of an Arrow stream of parameters.
The requested types add no call that can run the busy handler.

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