Skip to content

fix: The error of a fetch carries the rows it read before the error - #945

Draft
krlmlr wants to merge 5 commits into
claude/rb-rlang-conditionsfrom
claude/rb-fetch-error-rows
Draft

krlmlr wants to merge 5 commits into
claude/rb-rlang-conditionsfrom
claude/rb-fetch-error-rows

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

An SQLite error in dbFetch() or dbGetQuery() still raises at once, and its condition now carries the rows that the fetch read before the error in the field rows, instead of losing them. This mirrors gh, which hands the records it fetched before an interrupt to the caller in the gh_result field of the condition; the field here is named rows, next to the unprefixed code, extended_code, code_name and extended_code_name that #943 added. The rows are a data frame typed as that fetch would have returned it: the same column types, converted for bigint and row.names as dbFetch() converts its result. If that conversion fails, as with row.names = "missing_column", the SQLite error is still raised and the field keeps the rows as RSQLite read them, so that no row is lost. Building and converting the rows of a failed fetch raises no warnings (such as "mixed type" or invalid UTF-8): with options(warn = 2) or an exiting warning = handler, such a warning would otherwise replace the SQLite error and its rows. When the fetch read rows, the message adds a line such as "ℹ 4 rows read before the error are in the condition's rows field." (singular for one row); the message field stays SQLite's message, and errors without rows keep SQLite's message unchanged. The result is complete after the error, as before (#935).

Which errors carry rows: every SQLite error that a fetch raises, that is, a failed step in the fetch loop (a malformed JSON value in a later row, SQLITE_BUSY, a constraint in a later parameter set of a RETURNING statement), a later parameter set that SQLite refuses to bind, and the error of a writing statement's step past the last row of a full fetch, which is raised at once. The error kept from the step past the rows of the previous fetch (#933) is raised by the next fetch, which has read no row, so its field is a zero-row data frame with the columns of the result. Errors while converting values, warnings made errors, and interrupts are not SQLite conditions and carry no rows: their rows are lost as before. SQLite errors outside of a fetch (in dbSendQuery(), dbBind(), dbExecute(), dbConnect()) have no rows field.

The rows in the condition count in dbGetRowCount(): #935 counts rows "once they are returned", and the condition returns them to the caller. Before, the rows of a fetch that failed did not count, so this changes some of #935's expectations in test-fetch-error.R.

Implementation: the fetch loop catches a SqliteError of the step, leaves the loop, builds the data frame of the rows read so far inside the unwind protection and outside of any catch block, and throws a SqliteError that takes it over by a move, which calls no R API. result_fetch() moves it out of the exception in the catch block and returns the error with its rows to R as a list of class RSQLite_fetch_error; check_fetch_error() in R raises it through signal_sqlite_error() inside rethrow(), which sets the call and converts the rows as before. Raised from C++ through cpp11::function, the condition and its rows stayed referenced by cpp11's unwind token until the next error that unwound through it: with a 2M-row fetch, 24.6 MB stayed in use after rm() and gc() instead of 9.4 MB; raised from R, it's 9.4 MB. Other SQLite errors are still raised from C++ by with_sqlite_errors(). Nothing longjmps over C++ objects. The loop now advances the data frame before the step, so that the row before a failed step is part of it; the interrupt check moves before the step accordingly.

The field is documented in ?"RSQLite-conditions", with an example that recovers the rows.

Tested with a new test-fetch-error-rows.R: rows of a plain query, of dbGetQuery() with and without parameters, row names, all four bigint settings, the kept error with zero rows, a constraint in a later parameter set, no rows for conversion errors, and no field outside of a fetch; the info line in the message (plural, singular, none for zero rows), the SQLite error with its rows under options(warn = 2) and with an exiting warning = handler, and that the rows are released once the handled condition is dropped (Vcells after rm() and gc()). Red on the base (#943): the first six tests fail because the field is NULL; green here. The tests of the message, the warnings and the memory fail on the previous head of this pull request and pass now. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (locale), SKIP 63, PASS 11200.

How an uncaught fetch error points to its rows was an open question (#945 (comment)); its recommended option is implemented: the info line in the message, raised with stop() as before, without recording it for rlang::last_error(). Ticking another option there changes it.

Closes #872.

Layer B2 of the rebuilt chain, planned in #880; based on #943. New code, not ported from an old PR; it follows the maintainer's decision in #880.

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


Generated by Claude Code

An SQLite error in `dbFetch()` or `dbGetQuery()` still raises at once,
and the condition now carries the rows that the fetch read before the error
in the field `rows`, as gh carries partial results in its conditions.
The rows are converted as `dbFetch()` converts its result,
for `bigint` and `row.names`, and count in `dbGetRowCount()`.
The error kept from the step past the rows of the previous fetch carries zero rows.
Errors while converting values and interrupts carry no rows.

Closes #872.

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 December 1, 2026 02:17

krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member Author

Question: how should an uncaught fetch error point users to the rows it carries?

This pull request puts the rows read before an SQLite error into the condition's rows field, as #872 decided. gh does the same with gh_result, and also makes the partial result reachable after the fact through rlang::last_error() and names it in its message. Here, rlang::last_error() doesn't record these errors, because #943 raises them with stop(rlang::error_cnd(...)) rather than rlang::abort(), which costs about 1.3 ms per caught error for its backtrace; and the message is SQLite's, unchanged. So a user who didn't wrap the call in tryCatch() loses the rows.

  • Add an info line to the message when rows were read, e.g. "ℹ 4 rows read before the error are in the condition's rows field.", and keep stop(); users who see the error once can rerun with tryCatch() (recommended: cheap, discoverable, and errors without rows keep SQLite's message unchanged)
  • Record uncaught fetch errors for rlang::last_error() as well, e.g. by raising with rlang::abort() only when rows were read, so that rlang::last_error()$rows works after the fact; costs the backtrace only in that case
  • Both
  • Neither: document the field only, as now

Implemented on 2026-12-01: the recommended option is in 372f456; no box is ticked, and ticking another option changes it.

If nobody decides, the recommended option goes in with the fixes of this pull request's review.


Generated by Claude Code

claude and others added 3 commits December 1, 2026 02:32
Building the rows of a failed fetch raises no warnings about their values,
and the conversion in R muffles its warnings,
so that a warning made an error or an exiting warning handler no longer replaces the SQLite error.
`result_fetch()` returns the error with its rows to R,
which raises it after the C++ frames have returned:
raised through cpp11, the condition and its rows stayed referenced by cpp11's unwind token
until the next error that unwound through it.
The rows are taken over by the error without calling R's API outside of the unwind protection.
The message adds a line that points to the `rows` field when the fetch read rows;
SQLite's message stays in the `message` field.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
The first error of a fetch in a fresh R process
costs about 160k Vcells for compiling code,
a large share of the threshold that varies across R versions.
A failing fetch before the measurement leaves that cost out.

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.

A dbFetch() that hits an SQLite error returns none of the rows it read before the error

2 participants