Skip to content

feat: Read a result as an Arrow array stream - #964

Draft
krlmlr wants to merge 3 commits into
claude/rb-arrow-byte-limitsfrom
claude/rb-arrow-stream
Draft

krlmlr wants to merge 3 commits into
claude/rb-arrow-byte-limitsfrom
claude/rb-arrow-stream

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for users: the new stream is internal. DBI's default dbSendQueryArrow() and dbFetchArrow() fetch data frames with dbFetch() and convert them with nanoarrow, so they don't reach this stream; the native methods that will use it come later in the plan (item 16).

The internal read_arrow_stream(res, n) returns a nanoarrow_array_stream over the current execution of a result. Its get_next() hands out the next Arrow chunk of at most n rows, built by the chunk fetch of #954–#963 (fetch_arrow_chunk() on the result): same column types, byte limits and errors. get_schema() reads the first chunk, which decides the types, and the result keeps it (as a kept chunk of #962) for the first get_next(), so no row counts as fetched before it is handed out. An empty result has a schema and no chunk.

The conversion warnings of #961 are raised from R when a chunk is handed out, also when nanoarrow pulls the chunks. As in fetch_arrow_chunk_warn(), the chunk is taken from the result before R allocates, and a warning made an error, a handler that exits or a failed allocation hands it back for the next get_next(), which returns it without warning again. A call to the stream from a handler of its own warning is refused, so that the chunks stay in order within the stream's calls; this ordering holds per call only: a handler that fetches from the same result, or a second stream on the same result, can take chunks in between, and no rows are lost. An SQLite error of a fetch comes through get_last_error(); R conditions that leave a callback by a jump (an interrupt, a warning made an error, the fetch-kind error) continue their jump once the C++ objects of the callback are destroyed.

The stream reads the execution that is current when it is created, using the existing executions_ count of the result: after dbBind() the next get_schema() or get_next() fails with "Can't read an Arrow stream after its result has been bound to new parameters." The stream refers to its result through a std::weak_ptr and doesn't keep it alive: after dbClearResult() (or R's garbage collector) it fails with "... after its result has been cleared." For that, DbResultPtr is now a std::shared_ptr, since the vendored Boost has no weak_ptr. A schema read before stays readable. Creating the stream checks that the result is bound and claims it for Arrow fetches, so dbFetch() fails with RSQLite_error_fetch_kind before the stream reads a row.

Left to later layers: a release() from a handler during a callback and failures that stay final (F6b; a release from a warning handler during get_next() is still unsafe here). Also left to F6b: a callback can currently continue R's longjmp through the consumer's frames under warn = 2, an exiting handler or an interrupt; nanoarrow's R glue tolerates this, but a C++ consumer would not, and a stream that outlives its result or the package (F7). The callbacks call R, so they must run on R's main thread.

Tested in the new test-arrow-stream.R: schema before the first chunk and agreeing with the chunks, all rows in order across chunks (also through nanoarrow::convert_array_stream()), empty results and results read to the end, warnings while pulling (through stream$get_next(), also with warn = 2, and through nanoarrow without it; F6b adds the warn = 2 case through nanoarrow), a refused call from a warning handler, an SQLite error mid-stream (inside a chunk and from the read-ahead step), release before the end, rebind, clearing the result while the stream is alive, the fetch-kind claim, and the n check. The new test file is clean under valgrind (0 errors, no leaks); the full suite passes with NOT_CRAN=true (FAIL 0).

Layer F6a of the rebuilt chain, planned in #880; based on #963. Ported from the basic stream of #864 (commit 2be01a8), without its clear() that freed the result's implementation and without its shared ownership of the result.

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



Generated by Claude Code

The internal `read_arrow_stream()` returns a `nanoarrow_array_stream`
whose `get_next()` hands out the next Arrow chunk of at most `n` rows of the result,
as `fetch_arrow_chunk_warn()` does:
the same column types, warnings, kept chunks and errors.
`get_schema()` reads the first chunk, which decides the types,
and the result keeps it for the first `get_next()`.
An error of a fetch reaches the consumer through `get_last_error()`.

The stream reads the execution that is current when it is created,
and fails with an error that says so once the result is bound to new parameters,
using the count of executions of the result,
or once the result is cleared:
it refers to the result through a `std::weak_ptr`,
so `DbResultPtr` is now a `std::shared_ptr`, as the vendored Boost has no `weak_ptr`.
Creating the stream claims the result for Arrow fetches.
A call from a handler of a warning that another call raised is refused,
so that the chunks stay in order.

No DBI method calls the stream yet: no change of behavior for users.

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

Takes the chunk from the result before R allocates, also in the stream.

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 08:28

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