Skip to content

feat: fetch Arrow results lazily in the default dbFetchArrow() and dbGetQueryArrow() methods - #769

Open
eitsupi wants to merge 3 commits into
r-dbi:mainfrom
eitsupi:feat/lazy-arrow-fetch
Open

eitsupi wants to merge 3 commits into
r-dbi:mainfrom
eitsupi:feat/lazy-arrow-fetch

Conversation

@eitsupi

@eitsupi eitsupi commented Oct 22, 2026 •

Copy link
Copy Markdown

The default dbFetchArrow() method fetches all chunks of a result before it returns a stream, and dbGetQueryArrow() and dbReadTableArrow() inherit this behavior. This PR makes the default methods lazy: the remaining chunks are fetched only when the returned stream is consumed. A consumer that reads only the first rows of a large result, such as a data viewer, then no longer has to wait for and hold the whole result in memory.

Changes

  • dbFetchArrow() (default method): fetches the first chunk to determine the schema. If more rows remain, it returns a stream built with nanoarrow::function_array_stream() (feat(r): add a function for lazy R callbacks apache/arrow-nanoarrow#975) that fetches the remaining chunks on demand. Results that fit into one chunk, zero-row results, and installations of nanoarrow without function_array_stream() keep the previous eager behavior.
  • dbGetQueryArrow() (default method): when the selected dbFetchArrow() method is DBI's default one, the result is owned by the returned stream and cleared when the stream reaches the end, when fetching fails, or when the stream is released. Otherwise, the previous behavior (clearing the result on exit) is kept, so backends that override dbFetchArrow() are not affected.
  • New option DBI.lazy_arrow (TRUE by default): set options(DBI.lazy_arrow = FALSE) to restore the previous eager behavior. The option must be TRUE or FALSE, and it is read once when the stream is created. It affects only DBI's default methods, not backends that provide their own dbFetchArrow().
  • The documentation of dbFetchArrow(), dbGetQueryArrow(), and dbReadTableArrow() describes the lazy behavior and the option.
  • nanoarrow (>= 0.9.0.9000) in Suggests, with a temporary Remotes entry for the development version of nanoarrow, as done before in a091ecc.

Behavior change

For results larger than one chunk (256 rows by default), the result stays open on the connection until the stream has been consumed or released. With backends that support only one active result per connection, such as RSQLite, issuing another query on the same connection before consuming the stream invalidates it. For example, passing the stream from dbGetQueryArrow() to dbWriteTableArrow() on the same connection fails after the first chunk has been written.

The previous behavior can be restored with options(DBI.lazy_arrow = FALSE).

…GetQueryArrow()

The default dbFetchArrow() method used to fetch all chunks of a result before
returning a stream. It now fetches the first chunk to determine the schema and,
if more rows remain, returns a stream that fetches the remaining chunks only
when it is consumed. This uses nanoarrow::function_array_stream() and falls back
to the previous eager behavior if the installed nanoarrow does not provide it.

The default dbGetQueryArrow() method transfers the ownership of the result to
the returned stream when the default dbFetchArrow() method is used. The result
is cleared when the stream reaches the end, when fetching fails, or when the
stream is released. Results that fit into one chunk are still cleared right
away, and backends with their own dbFetchArrow() method keep the previous
behavior.

The development version of nanoarrow is required for the new behavior, so it is
temporarily added to Remotes.
@eitsupi eitsupi changed the title feat: Fetch Arrow results lazily in the default dbFetchArrow() and dbGetQueryArrow() feat: fetch Arrow results lazily in the default dbFetchArrow() and dbGetQueryArrow() methods Oct 22, 2026
@eitsupi
eitsupi marked this pull request as ready for review October 22, 2026 15:20
@eitsupi

eitsupi commented Oct 22, 2026

Copy link
Copy Markdown
Author

Making the default methods lazy means that, for results larger than one chunk, the result stays open on the connection until the stream is consumed.
With single-result backends such as RSQLite, issuing another query on the same connection before consuming the stream invalidates it (for example dbWriteTableArrow(con, ..., dbGetQueryArrow(con, ...))).
Would you prefer this to be the default, or should the lazy behavior be opt-in (for example through an argument or an option)?

@paleolimbot

Copy link
Copy Markdown

This is a cool feature! Getting things lazily and processing by batch is a cool idiom that would be great to normalize.

should the lazy behavior be opt-in

I think it should probably be controlled by an option (i.e., starts as opt-in, moves to opt-out in case it causes issues, or perhaps we can go straight to opt-out).

The "stream depends on the connection" gotcha we solve in adbcdrivermanager's read_adbc by two things: most drivers try to be defensive about keeping a weak reference to the connection state so that they can at least error instead of crash when the connection is closed, and we also have an option to "attach" a connection to a stream so that it can live independently in the wild ether of FFI stream consumption.

The other gotcha here is passing these streams to things like polars, datafusion (e.g., SedonaDB), or DuckDB, where they might get consumed on another thread. In SedonaDB I'm defensive about this (I materialize by default before scanning), which is perhaps a better place to apply materialization since most consumers won't be processing results on multiple threads and the consumers that do usually know this.

The default dbFetchArrow() and dbGetQueryArrow() methods fetch the remaining
chunks lazily by default. Setting options(DBI.lazy_arrow = FALSE) restores the
previous behavior: all chunks are fetched before the stream is returned, and
dbGetQueryArrow() frees the result right away.

The option must be TRUE or FALSE; other values are an error. It is read once
when the stream is created, so changing it later does not affect existing
streams. Backends with their own dbFetchArrow() method are not affected.
@eitsupi

eitsupi commented Oct 24, 2026

Copy link
Copy Markdown
Author

I added the DBI.lazy_arrow option.
For now it is opt-out (TRUE by default), so that the lazy behavior gets exercised; if this causes trouble, we can switch the default to FALSE.
With options(DBI.lazy_arrow = FALSE), all chunks are fetched before the stream is returned, and dbGetQueryArrow() frees the result right away, as before.

Attaching the connection to the stream and consuming the stream from other threads seem out of scope for the default methods in DBI, so I left them out of this PR.
The documentation notes that the result stays open until the stream is consumed or released.
Happy to follow up on either of them separately if useful.

@krlmlr

krlmlr commented Oct 24, 2026

Copy link
Copy Markdown
Member

Thank you for working on this.

Should the option perhaps be per connection or even per query? I will also take a look at the cose and at the behavior across backends.

@eitsupi

eitsupi commented Oct 25, 2026

Copy link
Copy Markdown
Author

Per-query control makes sense to me.
I could keep the option as the global default and add a named lazy argument (after ...) to the default dbFetchArrow(), dbGetQueryArrow(), and dbReadTableArrow() methods, without changing the generics, similar to n in the default dbGetQuery() method.
Since the option is read only once when the stream is created, withr::with_options(list(DBI.lazy_arrow = FALSE), dbGetQueryArrow(...)) already works for a single query.

Note that this only covers DBI's default methods.
Backends with their own dbFetchArrow(), such as adbi and duckdb, already decide on their own whether to collect the chunks or return a native stream, and they would ignore lazy.
For per-connection defaults, I would leave it to backends (e.g. an argument to dbConnect()) rather than adding a common interface in DBI.

I'll wait for your review of the code and the backends before changing anything.

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member

Update: #794 is experimenting with slots provided and populated by DBI's generics that will allow capturing arguments at connection level and looking them up later.

@paleolimbot: When do you plan a nanoarrow update so that this PR can avoid the remote?

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.

3 participants