Skip to content

fix: A fetch returns its rows when the step past its last row fails - #933

Draft
krlmlr wants to merge 4 commits into
claude/rb-bind-empty-namesfrom
claude/rb-fetch-row-ahead
Draft

krlmlr wants to merge 4 commits into
claude/rb-bind-empty-namesfrom
claude/rb-fetch-row-ahead

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

What changes for users. A fetch steps one row past the last row it returns, to tell whether the result is complete and to finish the statement, which releases its read lock. For read-only queries, when that step fails, dbGetQuery(n = k) and dbFetch(n = k) now return their k rows without an error where they failed before, and the next fetch raises the error, with SQLite's original message even if later statements on the connection failed in between. Until then, dbHasCompleted() is FALSE; dbFetch(n = 0) and dbColumnInfo() leave a kept error alone, and a new dbBind() drops it. Before, the fetch that held rows 1 to k raised the error of row k + 1 and those rows were lost; with several parameter sets, the error of a later set that fails to run or to bind was raised by the fetch that held the last row of the set before. For a read-only query, a value in a later parameter set that SQLite refuses to bind completes the result only once the next fetch has raised the refusal; a statement that writes raises it at once. An error among the rows a fetch asks for is still raised by that fetch, so dbGetQuery() never returns a result cut short. dbGetQuery(n = 4) over a read-only query whose fifth row fails returns four rows, as LIMIT 4 would. Statements that write, such as INSERT ... RETURNING, still fail at once, as before: for them, the step past the last row may commit the write in autocommit mode or run the next parameter set, and an error kept there would be lost when no fetch follows, as with dbGetQuery(n = k) or dbClearResult(), so that a failed commit (a deferred foreign key, SQLITE_BUSY) or a failed later parameter set would go unnoticed.

How. A failed step or an unbindable parameter throws a C++ exception, step_error, that carries SQLite's message and result code, instead of calling cpp11::stop(), which signals the R error at once and could not be held back. read_ahead() catches it for the step past the last row of a full fetch and, if sqlite3_stmt_readonly() says the statement doesn't write, keeps it; otherwise it rethrows it at once; the next fetch_rows() raises it. Every other step_error reaches R through cpp11's entry points, which turn it into the same R error as before, with the same message and condition call. raise_bind_exception() now takes SQLite's result code.

Tests. New tests/testthat/test-read-ahead.R (fixture in helper-read-ahead.R) covers fetches of 1, 2 and 4 rows over a six-row result whose fifth row fails, several parameter sets including one that fails to bind and one SQLite refuses, a kept message surviving later failing statements, dbGetQuery(n = ), the column structure while an error is kept, a new bind dropping it, the statement releasing its lock after the last row, and statements that write raising at once: a deferred foreign key failing at commit with dbGetQuery(n = ) and with dbFetch(n = k) followed by dbClearResult(), SQLITE_BUSY at commit with a second connection holding a read, and a CHECK failing in a later parameter set. The format-string block now expects the error of its write case (INSERT ... RETURNING) from the first fetch, as statements that write raise at once; a separate read-only block checks that a kept message containing % is raised verbatim by the next fetch. test-out-of-memory.R now expects the row before an out-of-memory step to be returned. Red → green: on the base branch, 8 of the first 10 new blocks and the changed out-of-memory block fail (the other two pin existing behavior); the 3 blocks for statements that write pass on the base and failed (4 expectations) on this branch before writes were made to raise at once; with this change, the full suite with NOT_CRAN=true gives FAIL 0, WARN 3, SKIP 63, PASS 10803.

Open points carried over from #850. For read-only queries, dbGetQuery(n = ) drops a later parameter set that SQLite refuses to bind, as it drops an SQLite error after the requested rows; #850 asked whether that should be raised instead, and recommended keeping it. For read-only queries, SQLITE_BUSY from the step past a fetch is kept like any other error, so a retry does not recover (#848); the result code a retry would need is kept with the error. What happens after the kept error is raised is left to later layers of the chain.

Layer B1 of the rebuilt chain, planned in #880; based on #927. Ported from #850 (now a draft), which this pull request replaces; its squashed diff applies unchanged.

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


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

A fetch of n rows steps once more to tell whether the result is complete.
When that step fails, read_ahead() keeps the error, with SQLite's message and result code,
and the next fetch raises it instead of the fetch losing its rows.
A failed step or an unbindable parameter throws a step_error,
which cpp11's entry points turn into the same R error as before.
dbFetch(n = 0) and dbColumnInfo() leave a kept error alone, and a new dbBind() drops it.

Ported from #850.

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 November 30, 2026 23:55

krlmlr commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Question: Raise a refused parameter set from dbGetQuery(n = )?

Carried over from #850's question, which nobody answered; this pull request ports #850 unchanged, so the code links below still show the same lines. B2 of the rebuilt chain will attach the rows read before an error to the condition, which doesn't change this case: here the rows are returned and the refusal comes after them.

Since the merge of main with #868, dbGetQuery(n = 1) silently drops a later parameter set that SQLite refuses to bind, where main raises the error. The fetch of n rows returns them, read_ahead() keeps the refusal for the next fetch (src/SqliteResultImpl.cpp#L415-L429), and dbGetQuery() clears the result without fetching again. This follows this pull request's rule that dbGetQuery(n = ) drops an error after its rows, as LIMIT does. But a value the user passed in params that can't be bound is an input error rather than a row past the limit, so one could argue it should still be raised.

library(DBI)
con <- dbConnect(RSQLite::SQLite())
# Lower SQLite's length limit to 30 bytes for this connection
RSQLite:::connection_limit_length(con@ptr, 30L)
x <- c("a", strrep("a", 31))
dbGetQuery(con, "SELECT :x AS x", params = list(x = x), n = 1)
#>   x
#> 1 a
# main (53f26934): Error: Can't bind parameter 1 (:x) in row 2: string or blob too big.
dbGetQuery(con, "SELECT :x AS x", params = list(x = x))
#> Error: Can't bind parameter 1 (:x) in row 2: string or blob too big.
dbDisconnect(con)
  • Accept it as the same rule as for an SQLite error after the requested rows, and pin it with a test (recommended)
  • Raise a kept bind refusal from dbGetQuery(n = ) before clearing the result, for example by checking kept_error_completes_ there, with a test
  • Bind all parameter sets up front, so that a refusal is raised before any row is returned

If nobody decides, the recommended option stands: the behavior stays as it is and is listed under Accepted limitations.


Generated by Claude Code

The step past the last row of a fetch commits a write in autocommit mode,
or runs the next parameter set.
Keeping its error for a next fetch lost it
when no fetch followed, as in `dbGetQuery(n = k)` or `dbClearResult()`,
so that a failed commit went unnoticed.
Statements that write now raise that error at once, as before;
read-only statements keep it for the next fetch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
The format-string test now goes through a write,
so a read-only query covers the message kept for the next fetch.
The NEWS bullet says that `dbHasCompleted()` stays `FALSE` after `dbFetch()` only.

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

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