Repository navigation
Conversation
…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.
dbFetchArrow() and dbGetQueryArrow() methods
|
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. |
|
This is a cool feature! Getting things lazily and processing by batch is a cool idiom that would be great to normalize.
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.
|
I added the 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. |
|
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. |
|
Per-query control makes sense to me. Note that this only covers DBI's default methods. I'll wait for your review of the code and the backends before changing anything. |
|
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? |
The default
dbFetchArrow()method fetches all chunks of a result before it returns a stream, anddbGetQueryArrow()anddbReadTableArrow()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 withnanoarrow::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 withoutfunction_array_stream()keep the previous eager behavior.dbGetQueryArrow()(default method): when the selecteddbFetchArrow()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 overridedbFetchArrow()are not affected.DBI.lazy_arrow(TRUEby default): setoptions(DBI.lazy_arrow = FALSE)to restore the previous eager behavior. The option must beTRUEorFALSE, and it is read once when the stream is created. It affects only DBI's default methods, not backends that provide their owndbFetchArrow().dbFetchArrow(),dbGetQueryArrow(), anddbReadTableArrow()describes the lazy behavior and the option.nanoarrow (>= 0.9.0.9000)inSuggests, with a temporaryRemotesentry 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()todbWriteTableArrow()on the same connection fails after the first chunk has been written.The previous behavior can be restored with
options(DBI.lazy_arrow = FALSE).