Repository navigation
fix: Count rows in 64 bits, so that dbGetRowCount() and dbGetRowsAffected() stay right beyond INT_MAX - #858
Conversation
…ffected()` stay right beyond `INT_MAX` The number of rows fetched from a result was an `int`, incremented once per row. A result read in chunks can pass `INT_MAX` rows in total, for example a recursive CTE that generates rows without storing them, and then the signed increment overflowed: in practice `dbGetRowCount()` returned `NA`, then negative counts. The number of rows changed was the difference of two `sqlite3_total_changes()` values, which SQLite truncates to an `int`, so that `dbGetRowsAffected()` and `dbExecute()` turned negative once one execution changed more than `INT_MAX` rows. Both counts are now `int64_t`, the changes come from `sqlite3_total_changes64()`, and both functions return an integer while the count fits, as before, and a double beyond `INT_MAX`, the only R type without bit64 that holds it exactly up to 2^53. Two unexported entry points for the tests set the counts close to the limit. Two tests that fetch and change 2^31 rows for real take minutes and run only with `RSQLITE_TEST_INT_MAX_ROWS=true`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
|
Review of this pull request at 76e4ebe, by a Claude Code reviewer with clean context that read the description, the diff against What was run
What holds
HighNone. MediumNone. Low
Generated by Claude Code |
…m 0 to 2^53 `result_set_rows_fetched()` and `result_set_rows_affected()` cast any double to `int64_t`, which is undefined behavior for `NA`, `NaN`, infinite and out-of-range values. Both now stop with "`n` must be a whole number from 0 to 2^53." unless the count fits. The hook test of `dbGetRowsAffected()` now says that it covers the R type and the 64-bit arithmetic only: the hook moves the start of the count, not SQLite's total of changes, so that only the gated test catches a return to the 32-bit `sqlite3_total_changes()`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
Accepted limitationsThe clean review found nothing above Low; it ran both gated tests (the rows-affected one in 2 min 21 s, the 2^31-row fetch in 9 min 34 s), which pass here and fail on
Generated by Claude Code |
The local counter of `fetch_rows()` was an `int`, which a single fetch with `n = -1` could overflow before `nrows_`, an `int64_t` since #858, takes its value. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
dbGetRowCount()counted the rows fetched from a result in anint, incremented once per row. A result read in chunks, such asdbFetch(res, n = 1e6)in a loop, can passINT_MAXrows in total, for example a recursive CTE that generates its rows without storing them. The signed increment then overflowed, which is undefined behavior; in practicedbGetRowCount()returnedNAat 2^31 rows and negative counts after that.dbGetRowsAffected(), and with itdbExecute(), had the same problem: it subtracted two values ofsqlite3_total_changes(), which SQLite truncates to anint, so that the count turned negative once the changes since onedbBind()ordbExecute(), summed over all its parameter rows, passedINT_MAX. Where a connection's total of changes passesINT_MAX, the subtraction itself also overflowed, even for a statement that changes a few rows, although in practice the wrapped result came out right there.Both counts are now
int64_t, and the changes come fromsqlite3_total_changes64(), which the bundled SQLite 3.54.1 has.dbGetRowCount()anddbGetRowsAffected()return an integer while the count fits, as before, and a double beyondINT_MAX, the only R type without bit64 that holds the count exactly up to 2^53.dbGetRowsAffected()still returns an integerNAbeforedbBind().The fix of the fetched rows is the one the old Arrow stack carries in 1914d20 (#795), without the Arrow parts around it, and it moves out of item 8 of the plan to rework the Arrow stack, which keeps the end of Arrow chunks before string offsets overflow. The fix of the changed rows is new: the old stack keeps the
intthere.Tests
tests/testthat/test-row-count.R, with everytest_that()block self-contained:INT_MAXthrough two unexported entry points for the tests,result_set_rows_fetched()andresult_set_rows_affected(), then fetch real rows across the limit and check the exact values on both sides: an integer up toINT_MAX, a double beyond. The changed rows go across the limit for real through anINSERT ... RETURNING, whose changes SQLite counts once its last row is fetched. The hook moves the start of the count, not SQLite's total of changes, so that only the gated test below catches a return to the 32-bitsqlite3_total_changes(). Both hooks stop with an error unless the count is a whole number from 0 to 2^53. Onmain, both fail, as the entry points do not exist there.RSQLITE_TEST_INT_MAX_ROWS=true, with the variable's name in the skip message. One fetches 2^31 + 1 rows of a recursive CTE in chunks of 10^6 rows, which took 608 s here on a machine busy with other jobs (a run of 10^7 rows suggests about six minutes when idle); onmain,dbGetRowCount()returnsNAat 2^31 rows and -2147483647 one row later. The other runsUPDATEon a table of 2^20 rows with 2^11 + 1 parameter rows, 2^31 + 2^20 changes in onedbExecute(), which took 107 s here; onmain,dbExecute()returns -2146435072.devtools::test()withNOT_CRAN=truepasses, apart from the three errors intest-httpvfs.Rthatmainhas as well (#832, #837) and "interrupt in busy handler" intest-dbConnect.R, whose callr session found no installed RSQLite afterpkgload::load_all(); #830 skips it in that case, and it passes against the installed build.Speed of the default path
A fetch of 5 million rows of an integer and a real column, as a whole and in chunks of 10^5 rows, median user CPU time of seven runs, three alternating runs per build: 1.16 s as a whole and 1.14 to 1.24 s in chunks on
main, 1.15 to 1.16 s and 1.10 to 1.17 s with this pull request. The difference is within the noise.Where this sits
This is a separate pull request from
main. The new Arrow stack, on #830 and #850, picks it up by mergingmainonce this lands.git merge-tree --write-tree origin/claude/fix-error-mid-result HEADreports conflicts in four files, in five small hunks: in each, #830 adds a line next to a declaration or asqlite3_total_changes()call that this pull request changes (busy()next ton_rows_fetched()inDbResult.cppandDbResult.h,bind_started_next to the counts inSqliteResultImpl.h, inn_rows_affected()and inbind()). Keeping both sides resolves all of them. The fetch loop that #850 and #830 rewrite merges without a conflict, as this pull request leaves the line that counts the rows alone: #830 adds anintcount of the rows of one fetch tonrows_, which is now anint64_t. #850 alone merges without any conflict. A trial merge into #830 with these resolutions builds, andtest-row-count.R,test-dbSendQuery.R,test-read-ahead.Randtest-dbClearResult.Rpass on it.For the reviewer
dbGetRowCount(),dbGetRowsAffected()anddbExecute()return a double instead of an integer beyondINT_MAX, where they returnedNAor a negative integer before. DBI's specification allows either type: "a scalar number (integer or numeric)". Code that checks for an integer withis.integer()sees the change only beyondINT_MAX.longis 32 bits: the counts useint64_tand SQLite'ssqlite3_int64, neverlong, and reach R through a cast tointordouble, never through a format string.int64_t.Reviewed in a clean review, which found nothing above Low; its points are fixed in 7ca358d or listed in the accepted limitations.
https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
Generated by Claude Code