Skip to content

refactor: Bind list parameters through a parameter source - #947

Draft
krlmlr wants to merge 6 commits into
claude/rb-returning-paramsfrom
claude/rb-param-source
Draft

krlmlr wants to merge 6 commits into
claude/rb-returning-paramsfrom
claude/rb-param-source

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior. The result binds its list parameters through a parameter source, so that a later layer can add an Arrow parameter source next to it.

What changes

SqliteResultImpl bound list parameters itself: bind_row() reset the statement and bound one parameter set through bind_parameter_pos(), while group_ and groups_ counted the sets. That code moves, unchanged apart from names, into SqliteListParamSource, behind an abstract SqliteParamSource whose one method, bind_next_row(), resets the statement, binds the next set, and returns whether there was one. Like the result before, it moves on to the next set before binding it, so a set that fails to bind is never bound again. The result keeps the source of its latest bind in a boost::scoped_ptr; bind() replaces it, and a failed bind drops it, as it dropped the list before. check_bind(), the former raise_bind_exception(j, rc) from #868, is a protected helper of the base class, so that every source names the parameter and the row in the same message and throws the same SqliteError with SQLite's extended result code (#943); a value of a type that can't be bound still throws a SqliteError with SQLITE_ERROR. So read_ahead() keeps the error of a later set for the next fetch, the error of a fetch carries the rows read before it (#945), and a failed bind leaves the result unbound (#936) as before.

One line goes away: raise_bind_exception() marked the result complete before throwing, which every caller already overrode (step() completes it, read_ahead() keeps the error with the result not complete, bind() leaves it unbound), so the source doesn't touch the result's state.

A writing statement sent with dbSendStatement() runs every parameter set to the end since #948; that loop in step() takes each further set from the parameter source too, through step_done() and bind_row(), and the new result_create() argument is_statement is left as #948 has it.

On the R side, db_bind() gets is_unnamed_placeholders() and match_placeholders(), taken from #855, for the Arrow bind to share later. match_placeholders() takes the parameters rather than their names, so that an unnamed list bound to a query without placeholders still gives "Named parameters not used in query: ".

Tests

The first commit adds tests to test-dbBind.R that pass on the base and on this branch: the messages for parameters that don't match the placeholders, the binding of each type of vector with missing values, parameter sets that return several rows, none, or that are empty, rebinding, and bound vectors across fetches with gc() in between. Three pin the seams of the new source: a later set that fails to bind in dbBind() after earlier sets wrote, a later set that SQLite refuses in dbBind() after a set that returned no row (classed RSQLite_error_toobig, result left unbound), and a later set that SQLite refuses in a fetch (classed error carrying the rows before it, row count, completion, rebinding). The full suite with NOT_CRAN=true gives the same counts on the base with the new tests and on this branch: FAIL 0, WARN 3 (locale), SKIP 63, PASS 11298. Each new test_that() block passes on its own after load_all(). Binding 200k sets of five columns through an INSERT and an UPDATE takes the same time on both builds within noise, and a bind fetched row by row under gctorture() returns the right values.

Limitations carried from #855

Both findings that #855 accepted still apply, unchanged by this pull request. #874: an R error that leaves the list source by longjmp, which only an ALTREP vector whose data pointer can't be taken triggers, is now handled in a fetch (cpp11::unwind_protect() from #935) and for the first set in dbBind() (the result is unbound from the start since #936), but not for a later set in dbBind() after a set that returned no row; the interface comment says so. #873: rebinding with zero-length parameters after a partial fetch keeps the read lock until the next bind or dbClearResult().

Chain

Layer R1 of the rebuilt chain, planned in #880; based on #948. Ported from #855 (now a draft), rewritten onto today's base; nothing Arrow-specific.

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


Generated by Claude Code

The tests pin the messages for parameters that don't match the placeholders,
the binding of each type of vector,
parameter sets that return several rows, none, or that are empty,
later sets that fail to bind in `dbBind()` or in a fetch,
rebinding, and bound vectors across fetches.
They pass before and after the binding moves into a parameter source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
The result binds its parameter sets through an abstract `SqliteParamSource`,
so that a later source can bind parameters given in another form.
`SqliteListParamSource` holds the list and binds its vectors as the result did,
and throws the same `SqliteError` for a value that can't be bound.
`db_bind()` gets `is_unnamed_placeholders()` and `match_placeholders()`
for a later bind to share.
No change of behavior.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
The list parameter source moved on only after a set was bound,
so after a refused set a further `bind_next_row()` would have bound the same set again.
It now advances first, as the result did before the source,
and the contract of `bind_next_row()` says so.
The comment on `step()` notes that the run to the end of a statement
takes each set from the parameter source too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
@krlmlr
krlmlr changed the base branch from claude/rb-fetch-error-rows to claude/rb-returning-params December 1, 2026 02:52
@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 02:52

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