Repository navigation
Conversation
The internal bind_arrow_stream() binds the rows of a nanoarrow_array_stream of struct arrays, or of anything nanoarrow converts to one, and runs the statement once per row, as dbBind() does with a data frame. Columns match placeholders by name or by position, with the rules and errors of dbBind(). A new SqliteArrowParamSource reads the arrays as the rows are bound. It binds null, boolean, integer, float, double, utf8 and binary columns, also large ones; other types are refused before any row runs, which keeps the previous bind. A value that fails to bind, a uint64 beyond int64 or a failing stream leaves the result unbound, as a failed dbBind() does. An Arrow stream of RSQLite that reads a result on the same connection, also the result itself, is read to the end before the first row runs: read lazily, an INSERT into the table it reads would see its own rows and might never end. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
… into claude/rb-arrow-bind-stream Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…ection `result_stream_on_connection()` keeps the table of entry points in `src/cpp11.cpp` aligned as it was. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Question: how should binding treat an Arrow stream that reads from the same connection?Carried over from the High in the review of #866. This pull request binds a result's parameters from an Arrow stream, one execution per row. If a statement on the same connection is still reading while the bind starts, for example the source of an
Kirill decides. Until then the first option stays as implemented. Generated by Claude Code |
… into claude/rb-arrow-bind-stream Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…ecide that by the connection bind_arrow_stream() checks the number and the types of the parameters from the stream's schema before it reads the stream to its end, so that a refused stream stays readable and the previous bind goes on, also for a stream of the result itself. A stream is read to its end first whenever a statement of the connection is reading its rows, whoever made the stream, so that a wrapped stream of RSQLite is covered too. A type that can't be bound names the placeholder and is an RSQLite_error. Every offset of utf8 and binary arrays is validated, and text that isn't valid UTF-8 or holds a NUL byte fails at its row. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
… into claude/rb-arrow-bind-stream Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…its own statement A stream is read to its end before the bind when an Arrow stream holds the result, or it keeps a chunk, whoever wraps the stream: the bind starts a new execution, which such a stream can't read on from, also with the result's statement done. The result's own statement no longer counts as reading, so that a stream of another connection stays lazy after a bind whose rows weren't all read. Recognizing a stream of RSQLite by its release callback adds nothing more and is dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
… into claude/rb-arrow-bind-stream Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…ecution A stream of an earlier execution of the result reads no more, and a chunk the result keeps doesn't read the connection either: neither makes a bind read an unrelated stream to its end any longer, which depended on when the garbage collector released the old stream. The result counts its streams per execution for that. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…o claude/rb-arrow-bind-stream Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
No change of behavior: this adds an internal entry point that no DBI method calls yet.
bind_arrow_stream(res, stream)binds the rows of a nanoarrow_array_stream of struct arrays, or of anythingnanoarrow::as_nanoarrow_array_stream()accepts such as a data frame, and runs the statement once per row, asdbBind()does with a data frame.dbBindArrow()(plan item 17) will need it.Parameter source. A new
SqliteArrowParamSourcesits next to the list source behindSqliteParamSource(#947's design): the bind loop pulls rows from it, and it reads the stream's arrays only as the rows are bound, skipping empty arrays.SqliteResultImpl::bind()is split intocheck_param_count()andstart_bind(), which both sources share, with the same messages and the same state handling as before. A null row of the struct array binds NULL to every placeholder.Placeholders. Named placeholders take the columns of the same name, anonymous and numbered ones take unnamed columns by position, through
is_unnamed_placeholders()andmatch_placeholders(), with the rules and messages ofdbBind()for a data frame. A stream that isn't of struct arrays fails with "Parameters must be an Arrow stream of struct arrays, with a column per placeholder."Types. null binds NULL; bool and int8 to int64, uint8 to uint32 and uint64 bind as integers; float and double as reals; utf8 and large utf8 as text, binary and large binary as blobs, where an empty value stays an empty text or blob. A uint64 beyond int64 fails at its row: "Can't bind parameter 1 ($a) in row 2: the value 9223372036854775808 is beyond the range of a signed 64-bit integer." Text that isn't valid UTF-8 or holds a NUL byte fails at its row ("Can't bind parameter 1 ($x) in row 1: the text contains a NUL byte."), since SQLite would store it as it is and a NUL would cut it short when read. Every array is validated at nanoarrow's full level, which also checks every offset of utf8 and binary arrays ("Invalid Arrow parameters: …"). Every other type (temporal, duration, dictionary, nested, also half float) is refused before any row runs with "Can't bind parameter 1 ($a) of Arrow type date32.", an
RSQLite_error; those types come with the next layers. The result code of everysqlite3_bind_*()call goes through the sharedcheck_bind().Errors and state. Input refused before the first row (counts, names, types, a stream that isn't of struct arrays) keeps the previous bind and leaves the stream with its owner, as #936 does for
dbBind(). These checks need only the schema and run before any array is read, also before a stream is read to its end (below). A value that fails to bind, an error of SQLite, or a stream whoseget_next()fails ("Can't read the Arrow parameters: …") leaves the result in the one unbound state of #936:dbHasCompleted()isFALSE,dbGetRowsAffected()counts the rows before the failure, anddbFetch()asks for a new bind. These errors areRSQLite_errorconditions withSQLITE_ERRORunless SQLite reports another code. The stream's callbacks run undercpp11::unwind_protect(), so an R error that leaves a stream implemented in R by a jump unwinds the C++ frames.A stream that reads the same connection (the High from #866's review, question there). Read lazily, a stream that reads the connection would see the rows the statement writes: an
INSERTinto the table it reads can read its own rows and never end. The rule inDbResult::must_read_arrow_params_first()doesn't depend on who made the stream, since any producer can wrap a stream of RSQLite.read_stream_first()inR/db_bind.Rreads the stream to its end before the first row runs if another statement of the connection is in the middle of reading its rows (sqlite3_next_stmt()andsqlite3_stmt_busy()), or if the result itself has an Arrow stream of its current execution that hasn't ended, failed or been released: such a stream couldn't read on once the bind starts a new execution, also when its statement is done with all rows in the chunk the schema read. Streams of earlier executions read no more and don't count, so the decision doesn't depend on when the garbage collector releases them. The rule is conservative: any other open result that is reading triggers it, also one the stream doesn't read. The result's own statement doesn't count, so a stream of another connection or producer stays lazy also after a bind whose rows weren't all read. Two limits remain: a lazy producer that fetches from the result being bound fails on the bind's busy guard instead of being read first, and a stream that starts statements on the connection only during the bind isn't detected. Read first, the arrays stay in memory until the bind has read them, and an error of the stream is raised before the bind, which keeps the previous bind and leaves the caller's stream read up to the error. This also makes binding a result to a stream of its own rows work: the stream reads the current execution, and the bind starts the next. With today's R API, sending theINSERTclears another result's stream, which then fails before the bind instead of looping; the loop comes back if a stream may read on afterdbClearResult()(#966's open question), or through a result made with the internalresult_create(), which the tests use. The alternatives are to refuse such a stream with an error, or to bind lazily and document the hazard for plan item 17; a question follows on this pull request.Accepted limitation. A stream of another connection to the same database file is read lazily. With a rollback journal, its open read keeps a shared lock, so the bind's write on this connection fails with SQLITE_BUSY ("database is locked") once the busy timeout runs out; with WAL, the reader sees its snapshot and the write goes through.
Tests.
tests/testthat/test-arrow-bind.R, through nanoarrow, with helpers inhelper-arrow.Rthat build integer arrays from their bytes, since nanoarrow converts R vectors to most widths only with the arrow package: every type above with nulls and storage classes, a null struct row, named ($,:,@), anonymous and numbered placeholders, the messages of mismatched parameters compared with those ofdbBind(), the previous bind going on after a refusal with the refused stream still readable, a uint64 out of range in the second row and a constraint in the second array (state and rows affected afterwards, then a working bind), a stream of RSQLite on another connection that fails in its third chunk, an empty stream for a statement and a query, a stream of several arrays with an empty one fetched in two steps, the same-connection cases: a second result reading the table withLIMIT 100inserts exactly its 3 rows (100 when read lazily), also when the stream is wrapped withnanoarrow::array_stream_set_finalizer(), a result bound to its own stream, plain and wrapped, in chunks of one row, of all rows and of more, a stream of another connection that stays lazy after a partly read bind and with a stream of an earlier execution alive, and is read first while another result of the connection reads, a stream that fails while it is read first and keeps the previous bind, and a cleared stream that fails before the bind; a type and a count refused for a stream of the same connection, which leave the stream readable and its result unread, and for a stream of the result itself, which keeps the previous bind; and text with a NUL byte, invalid UTF-8 and a middle offset beyond the data. Everytest_that()block passes on its own afterload_all(). The full suite withNOT_CRAN=truegives FAIL 0, WARN 3 (the knowntest-encoding.Rwarnings), SKIP 64, PASS 12998. valgrind (--leak-check=full) reports no errors and nothing lost fortest-arrow-bind.R.Layer F8a of the rebuilt chain, planned in #880; based on #966. Ported from #866 (the parameter source, without temporal and duration types and without the tests of sliced arrays, which come with F8b) and from #867 for the question of a stream of the result itself (its checks come with F9).
Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Generated by Claude Code