Repository navigation
Conversation
`fetch_rows()` keeps building the data frame, and hands the loop over the rows to the new member template `fetch_loop()`, which takes the rows to fill and a function that completes them. The loop is the one of a data-frame fetch: the one-row read-ahead, the kept error of the step past the last row, the completion of the result by any error that leaves the fetch, and the count of fetched rows once they are returned. The next commit fills Arrow arrays through it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
… result The internal `result_fetch_arrow_chunk()` fetches at most `n` rows of a result, all of them for -1, into a nanoarrow struct array with its schema. It runs under the busy guard of `dbFetch()`, and reads the rows through the same loop as a data-frame fetch, so the one-row read-ahead, the kept step error, the completion of the result by any error and `dbGetRowCount()` apply as they do there. No DBI method calls it yet. `SqliteArrowColumn` types a column from its first value that is not NULL, int64 for an integer and double for a real, and widens int64 to double when a real comes later in the same chunk. A column of NULL values takes the type of its declared affinity, or the null type without one. The first chunk of an execution fixes the types for the later chunks, where a real in an int64 column is truncated, or NULL beyond the range of int64, and a value in a column of the null type is NULL. A text or blob value, or a column of NULL values declared as text or blob, fails the fetch until text and blob arrays are built. The arrays are returned with the helpers of `nanoarrow/r.h`, so nanoarrow joins `LinkingTo`, and `Suggests` for the tests. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
|
Review of this pull request at 90753dc, by a Claude Code reviewer with clean context that read the description, the two commits against #857 (40678e2..90753dc), the fetch loop and busy guard around them, the new tests and helpers, and the old What was run
What holds
HighNone. MediumNone. Low
Generated by Claude Code |
…c-columns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
nanoarrow 0.3.0.1 ships only `r_nanoarrow.h`, so building against it fails in the compiler. From 0.4.0 on, `r.h` has the four helpers that `result_fetch_arrow_chunk()` uses, and the Arrow tests pass with nanoarrow 0.4.0 installed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
The header declares `SqliteArrowColumn`, `ArrowArray` and `ArrowSchema` instead of including them, so that `DbResult.cpp` and `SqliteResult.cpp` no longer compile the nanoarrow headers. `SqliteResultImpl.cpp` includes `SqliteArrowColumn.h` itself, and the dependency lists of the two other objects are back to those of the base. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…c-columns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
Superseded: Accepted limitationsSuperseded by the current copy. The clean review found nothing above Low: arrow's
Generated by Claude Code |
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
Question: keep values that arrive after the first chunk has typed their column as null?The first chunk of an execution fixes the Arrow types for its later chunks ( library(DBI)
con <- dbConnect(RSQLite::SQLite())
# A column without a declared type, NULL in the first chunk
res <- dbSendQuery(con, "
WITH t(i) AS (VALUES (1), (2), (3), (4))
SELECT CASE WHEN i > 2 THEN i END AS x FROM t
")
chunk <- RSQLite:::result_fetch_arrow_chunk(res@ptr, 2L)
nanoarrow::infer_nanoarrow_schema(chunk)$children$x$format
#> [1] "n"
# The later chunk keeps the null type: 3 and 4 become NULL without notice
# (expected: the values 3 and 4, or a warning)
nanoarrow::convert_array(RSQLite:::result_fetch_arrow_chunk(res@ptr, -1L))
#> x
#> 1 NA
#> 2 NA
dbClearResult(res)
dbDisconnect(con)
Kirill decides. If nobody decides, the rule stays as it is until #862, and the limitation stays checked in the "Accepted limitations". Generated by Claude Code |
Question: keep Arrow chunks valid after RSQLite is unloaded?An Arrow chunk kept after # Run in a separate R process because it crashes
res <- try(callr::r(function() {
library(RSQLite)
con <- DBI::dbConnect(RSQLite::SQLite())
rs <- DBI::dbSendQuery(con, "SELECT 1 AS x")
chunk <- RSQLite:::result_fetch_arrow_chunk(rs@ptr, -1L)
DBI::dbClearResult(rs)
DBI::dbDisconnect(con)
detach("package:RSQLite", unload = TRUE)
# Expected: the chunk is released without a crash
rm(chunk)
gc()
"survived"
}, libpath = .libPaths()), silent = TRUE)
cat(attr(res, "condition")$stderr)
#>
#> *** caught segfault ***
#> address 0x7ff09a075d10, cause 'memory not mapped'
#> An irrecoverable exception occurred. R is aborting now ...
Kirill decides. If nobody decides, the behavior stays, and the limitation stays checked in the "Accepted limitations". Generated by Claude Code |
Accepted limitationsThe clean review found nothing above Low: arrow's
A finding that
Generated by Claude Code |
…c-columns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
`ArrowChunk` counted its rows in an `int`, which overflowed once a chunk passed `INT_MAX` rows, as a fetch of all rows can, and fed the overflowed count to the chunk length and `finish_chunk()`. The column builders and nanoarrow already take `int64_t`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
…c-columns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
…c-columns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
…c-columns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd8MVXyvU7qoMeruD1R
The first data frame fetch or Arrow fetch of a result claims it, and a fetch of the other kind then fails with an error of class `RSQLite_error_fetch_kind`. The claim holds across `dbBind()`. `dbFetch(n = 0)`, `dbColumnInfo()` and the state of the result read no rows and stay allowed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Resolved: should the fetch loop take its rows through a virtual row sink, as RPostgres does since r-dbi/RPostgres#638?Outcome: the maintainer asked to rebuild the chain from The stack does not build on virtual methods for consuming rows. This pull request introduces A virtual
If nobody decides, the template stays. Nothing starts before the current simplification of #859 to #864 (no mixed data-frame and Arrow fetches) is pushed, since both touch the same lines. Generated by Claude Code |
Fourth step of the Arrow rework (plan: #829 (comment)), and the first piece of the internal Arrow engine: fetch up to
nrows of a result into a nanoarrow struct array, for columns whose values are integers, reals orNULL. The entry point is internal; no DBI method calls it until pull request 16 of the plan, so nothing changes for users.What changes:
result_fetch_arrow_chunk(res, n), registered through cpp11 and not exported, returns ananoarrow_arrayof a struct type, with its schema in the tag of the external pointer as the nanoarrow package expects. It holds at mostnrows, or all remaining rows forn = -1. Other values are refused: a chunk of no rows would have to fix the column types without seeing a value. The fetch runs under the busy guard of fix: Never restart a statement after an error indbFetch()ordbBind()#830, likedbFetch(), and fails like it for an inactive or unbound result.fetch_rows()moves into the member templatefetch_loop(), which takes the rows to fill and a function that completes them. Data-frame fetches and Arrow fetches both run through it, so the Arrow fetch has fix: A fetch returns its rows when the step past its last row fails #850's one-row read-ahead (the row stepped past the last row of a full chunk is the first row of the next chunk, and a chunk that returns the last row completes the result and releases its read lock), fix: Never restart a statement after an error indbFetch()ordbBind()#830's error semantics (an error of that step is kept and raised by the next fetch, and any error that leaves the fetch, including one from building the arrays, completes the result), anddbGetRowCount()counting rows once they are returned.SqliteArrowColumnbuilds the child array of one column. The type comes from the first value that is notNULL: int64 for an integer, double for a real. An int64 column widens to double when a real arrives later in the same chunk, as the default path widens integers. A column whose values in the first chunk are allNULLtakes the type of its declared affinity: int64 forINTEGER, double forREALandNUMERIC(which includes unknown declared types), and the null type without a declared type, as for an expression. The first chunk of an execution fixes the types for its later chunks: there, a real in an int64 column is truncated toward zero, orNULLbeyond the range of int64 where SQLite would saturate, and a value in a column of the null type isNULL. A newdbBind()starts a new execution, whose first chunk decides again.DbArrow.hholdscheck_arrow(), which turns a nanoarrow error code into a C++ exception, so that the arrays under construction are released on every error.SqliteResultImpl.honly declares the Arrow types, so that the nanoarrow headers are compiled only where arrays are built (SqliteArrowColumn.cpp,SqliteResultImpl.cpp,result.cpp).DESCRIPTIONaddsnanoarrow (>= 0.4.0)toLinkingTo, for the header-only helpers ofnanoarrow/r.hthat create the external pointers with the class and the finalizer the nanoarrow package expects, and toSuggests, for the tests. 0.4.0 is the first version that shipsr.h; 0.3.0.1 has onlyr_nanoarrow.h. Nothing more is needed from it: looking into the returned array from R needs the nanoarrow package loaded, which the tests do throughnanoarrow::. ThroughLinkingTo, nanoarrow is now needed to build the package.DbArrow.handSqliteArrowColumn.*come from #795 at 43db19f with its refinements up to 3436358, cut down to int64, double and the null type. The data-frame mode,ArrowTarget,CoercionLog, the byte limits,ProgressValueand theSqliteColumnDataSourcemember are left out, and so is the split ofread_row()fromappend_row()that only the byte limits need. The truncation of a real in a fixed int64 column followsinteger_value()of 3436358. The struct array is assembled in the shared fetch loop instead of a loop of its own.Mixing data-frame and Arrow fetches on one result stays undecided (#829 (comment)), and the entry point neither refuses nor supports it on purpose. What happens today: each fetch continues at the next row of the result, whichever kind came before, so no row is lost or read twice, and
dbHasCompleted()anddbGetRowCount()follow both. Each path types its columns on its own: the first Arrow chunk of an execution fixes the Arrow types,dbFetch()keeps its types as before, and neither sees the other's. Nothing is handed from one path to the other, because nothing reads ahead by more than the one row that both share; the question becomes concrete with the byte cut (pull request 8) and the schema read-ahead of streams (pull request 9).Where the values differ from
dbFetch(): integers are int64, which nanoarrow converts to double unless asked forinteger64; a column without declared type and onlyNULLvalues is of the null type rather than logical; later chunks keep the types of the first one, wheredbFetch()widens integers again in a later fetch; and an int64 column that holds a value beyond 2^31 widens to double when a real arrives, wheredbFetch()keepsinteger64and truncates the real with a warning. The tests pin each of these.Deliberately left out:
x: Arrow arrays of text and blobs are not supported yet.", which completes the result like any other error. So does a column ofNULLvalues declared asTEXTorBLOB, which would otherwise need a type that pull request 5 changes.extended_typesis not consulted until pull request 6: aDATE,TIMEorTIMESTAMPcolumn comes through as the numbers it stores, as double if it holds onlyNULL(by itsNUMERICaffinity), and text dates fail as text.main; counting in 64 bits is a separate pull request.Tested:
tests/testthat/test-arrow-columns.Rwith fixtures intests/testthat/helper-arrow.R: integer, real andNULLcolumns; the first-value rule; widening within a chunk, also from beyond 2^53; the types fixed by the first chunk; the affinity of all-NULLcolumns (INTEGER,REAL,NUMERIC, none, and an expression); int64 values beyond 2^53 and up to 2^63 - 1 kept exactly; chunks of 1, k and all rows up to past the end, and a chunk ending on the last row releasing the read lock for a writer on another connection; an empty result and a statement without columns; a step error (json()on invalid JSON, as in fix: A fetch returns its rows when the step past its last row fails #850's tests) after the rows of a chunk and among them; a busy handler that runs during the step past the last row of a chunk and can neither clear the result nor fetch from it; text and blob values and declared types failing the fetch and completing the result; a new execution deciding its types again; an unbound query and invalidn. Where the rules agree, a helper fetches the same query withdbFetch()in the same chunk sizes and compares the values,dbHasCompleted()anddbGetRowCount()after each chunk.test_that()block passes on its own in a fresh session right afterpkgload::load_all().devtools::test()withNOT_CRAN=true: no failures beyond the three errors intest-httpvfs.Rthat also happen onmain(test: Run the HTTP VFS tests for real, and make their errors fail the check #832, testthat hides a test's error when the test records another result after it #837). The refactor commit alone passestest-read-ahead.R,test-dbSendQuery.R,test-dbClearResult.R,test-error.R,test-dbBind.Randtest-basic-types.R.-O1), the new tests andtest-read-ahead.R,test-dbSendQuery.R,test-dbClearResult.R,test-error.Randtest-dbBind.Rpass without a report, and so does a script with chunks of several sizes, widening, a text error, rebinding, and arrays converted after their result was cleared and the connection closed, also undergctorture(TRUE).r.hof nanoarrow 0.4.0, and the Arrow tests pass with nanoarrow 0.4.0 installed as well as with 0.9.0.ValidateFull()accepts chunks with int64, double and null columns (checked by hand; arrow is not a test dependency here).R CMD check --no-manual: Status OK.Sits on #857, which is on #856, #855, #830 and #850.
Size
About 530 hand-written lines of C++ and 405 lines of tests, above the plan's target of 600 in all; splitting off the 16-line
fetch_loop()refactor or the later-chunk rules would save little, so the pull request is kept whole, and its commits can be read one at a time.Review
The clean review found nothing above Low; two points are fixed in 9251a64 and d912dbc, and the rest are listed in the accepted limitations.
https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
Generated by Claude Code