Skip to content

refactor: Read rows through a virtual row sink - #950

Draft
krlmlr wants to merge 3 commits into
claude/rb-result-ptrfrom
claude/rb-row-sink
Draft

krlmlr wants to merge 3 commits into
claude/rb-result-ptrfrom
claude/rb-row-sink

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior. dbFetch() now reads its rows through a virtual row sink, as asked in #859 (comment), so that the Arrow layers later in this chain can fill a chunk with the same fetch loop instead of a second copy or a template.

The new src/SqliteRowSink.h declares the abstract SqliteRowSink and documents its contract: the call order, what the sink owns and reads, and what may throw. SqliteResultImpl::fetch_into(SqliteRowSink&) checks that the result is bound and runs the fetch loop that fetch_rows() had (in the private read_into(), which fetch_rows() calls after the check in fetch(), so each path checks once), with everything it did: the read-ahead when the sink is full, the kept error, the protected region that completes and resets the result on any error (#935), the rows that an SQLite error carries (#945), the busy guard, and 64-bit row counts. SqliteDataFrameSink wraps SqliteDataFrame. The interrupt check moves from DbDataFrame::advance() into the fetch loop, every 1024 rows of a fetch as before, so there is one place for it.

The design follows PqRowSink from r-dbi/RPostgres#638 (begin(), append(), remaining() with -1 for unbounded, interrupts checked by the loop) and differs where SQLite or this chain needs it. takes_row() (default true) lets a sink decline a row: the fetch ends without an error and the statement stays on that row for the next fetch. A PGresult row is cleared after append(), but an SQLite statement keeps its row, and the Arrow byte limit needs this later; data frames don't use it. finish(failed) is called inside the protected region, so building the output (the data frame and its warnings) still completes the result if it fails, and it returns the partial rows that the SQLite error carries; RPostgres builds the data frame after the loop. It runs when the rows end, the sink has enough or declines a row, or an SQLite error ends the fetch, but not when a call to the sink or an interrupt ends it, so a sink frees what it holds in its destructor. append() takes no argument, because the sink's column sources read the statement they were made for, where RPostgres hands over each PGresult. remaining() returns int64_t. When remaining() reaches 0 after an append, the loop steps past the row only to read ahead, as since #933. A sink with remaining() of -1 gets no read-ahead: the step must reach the next row for takes_row() to see it, so an SQLite error of that row ends the fetch, also if the sink would have declined the row. Keeping that error would need the sink to know before the row that it is full, and such a sink can return 0 from remaining() instead; the header says so. The warning about dbFetch() for statements stays in the data frame path, outside the protected region, so that it behaves as before; RPostgres moved it into fetch_into(). Running a writing statement sent with dbSendStatement() to the end (#948) stays in step() rather than in a discarding sink: it runs while binding, where no fetch and no sink exist.

Tested by the existing suite (FAIL 0 | WARN 3 | SKIP 63 | PASS 11391 with NOT_CRAN=true; the warnings are locale warnings of this machine). The new tests/testthat/test-row-sink.R exercises takes_row() == false through a test-only sink behind the unexported result_fetch_declining(), which runs under the busy guard through DbResult::fetch_into(): the declined row is returned by the next dbFetch(), and the row count and dbHasCompleted() follow. Two more tests pin the error cases: an SQLite error in the row after the sink's last one ends that fetch and completes the result, and an error in a row after a declined one comes from the next dbFetch().

Layer R3 of the rebuilt chain, planned in #880; based on #949. Not ported from an old PR: it replaces the fetch_loop() template of the old chain (#859).

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


Generated by Claude Code

`SqliteResultImpl::fetch_into()` reads the rows of a fetch into a `SqliteRowSink`,
modeled on RPostgres's `PqRowSink`, and keeps everything the fetch did:
the read-ahead when the sink is full, the kept error, the protected region that completes and resets the result,
the rows of an SQLite error, and 64-bit row counts.
`SqliteDataFrameSink` collects the rows of `dbFetch()` into a data frame.
The interrupt check moves from `DbDataFrame::advance()` into the fetch loop.
A sink can decline a row, which ends the fetch and leaves the row for the next one;
the unexported `result_fetch_declining()` tests that seam.

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 03:13
…w sink's contract

`dbFetch()` checked that the result is bound in `fetch()` and again in `fetch_into()`.
The fetch loop moves to the private `read_into()`,
so that `fetch()` and `fetch_into()` each check once,
before anything else, as before.

The contract in `SqliteRowSink.h` now says when `finish()` runs:
when the rows end, the sink has enough or declines a row, or an SQLite error ends the fetch,
not when a call to the sink or an interrupt ends it,
so a sink frees what it holds in its destructor.
It also says that a sink with `remaining()` of -1 gets no read-ahead:
the step must reach the next row for `takes_row()` to see it,
so an SQLite error of that row ends the fetch, also if the sink would decline the row.
New tests pin this with the declining test sink.

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