Repository navigation
Conversation
`rsqlite_is_utf8_string()`, declared in `src/utf8.h` and defined in `src/utf8.cpp`, tells whether a byte range is well-formed UTF-8 without a NUL byte, which is what an R string can hold. It follows RFC 3629, so it rejects overlong forms, surrogates, and code points beyond U+10FFFF. ASCII text is checked eight bytes at a time. Nothing calls the validator yet, so there is no user-visible change. It is the shared infrastructure for checking text that is read from SQLite, in the data frame path and in the Arrow path. The unexported `utf8_valid()` exposes it for the tests, which compare it with `validUTF8()` from base R. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
The test hook utf8_valid() now copies each input into a buffer with three continuation bytes behind it. A validator that reads past the end then sees a truncated sequence as valid, and the truncation tests fail. Empty input is passed to the validator as NULL, which the header allows, so the empty-input test covers it. The non-raw input test now checks for cpp11's "expected 'raw'" message instead of accepting any error. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
The build regenerated it with system headers, and the previous commit picked that up by mistake. The includes did not change, so the committed file stands. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
`load_all()` sources `tests/testthat/helper*.R` but not the top level of a test file. `utf8_reference()`, `hex()`, `ascii()`, `pad()` and `expect_utf8()` move to `helper-utf8.R`. Helper files are shared by every test file, so the generic `hex()`, `ascii()` and `pad()` become `raw_hex()`, `raw_ascii()` and `raw_pad()`. Every `test_that()` block of `test-utf8.R` now runs on its own after `load_all()`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
The CI's auto-update regenerated `src/cpp11.dd` and `src/utf8.dd` with GCC, whose dependency output indents continuation lines by one space, which the filter in `src/Makevars.deps` does not drop, so the files gained hundreds of absolute and vendored paths (#836). This restores them as they were and keeps the auto-update's formatting of `src/utf8.cpp`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
`SqliteArrowColumn` types a column whose first value that is not NULL is text as `utf8`, and one whose first such value is a blob as `binary`. A column of NULL values declared with TEXT or BLOB affinity takes that type, where the fetch failed before. Values that don't fit the type of their column follow #795. Numbers in a `utf8` or `binary` column are the text SQLite renders, text in a `binary` column is its UTF-8 bytes, and text and blobs in an `int64` or `double` column are read as SQLite reads them. A blob in a `utf8` column keeps its bytes, also in a UTF-16 database. Text or a blob in a `utf8` column must be valid UTF-8 without a NUL byte, as `rsqlite_is_utf8_string()` checks it, or the value is NULL, so that the arrays pass Arrow's full validation and convert to R strings. An `int64` column takes NULL where SQLite would saturate the leading integer of text, as it already does for a real beyond the range of int64. None of these conversions is reported yet. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
|
Review of this pull request at 689a5c5, by a Claude Code reviewer with clean context that read the description, the commit's diff against #859 (90753dc), the surrounding fetch loop and read-ahead in What was run
What holds
HighNone. Medium
Low
Generated by Claude Code |
…lumns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
SQLite reports a conversion that runs out of memory as a NULL pointer from `sqlite3_column_text()` or `sqlite3_column_blob()`, and fails only the next step. After a full chunk that step only reads ahead and keeps its error for the next fetch, so the chunk came back with a wrong value: text as an empty string, a saturating integer as NULL, a real as zero. The column now fails the fetch where the conversion fails, which completes the result like any other error. A blob gives its size before `sqlite3_column_blob()`, so that a zeroblob() that can't expand is told from a blob of no bytes, and text and blobs read into a double column are read as UTF-8 text first. In the chunk that decides the types, text and blobs that an int64 column has read as integers take the readings as reals they would have in a double column when a real widens the column later in that chunk, so that their values don't depend on the order of the rows. The tests force the out-of-memory case with SQLite's hard heap limit in an R session of its own, as the limit can't be lifted again, and add whitespace and a sign before saturating integers and the digits of a blob in a UTF-16 database. A test comment claimed that a lone surrogate makes UTF-16 text NULL anywhere; it does so only at the end of the text. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…actly `append_date()` and `append_time()` read text through `value_text()` of #860, so that a conversion of text that runs out of memory fails the chunk at once, as for the other columns, instead of becoming NULL. `fetch_arrow_chunk_out_of_memory()` takes setup statements and `extended_types`, and a new test runs it on DATE, TIME and TIMESTAMP columns whose text is computed as it is read. Reals are rounded to microseconds with `std::nearbyint()`, to the nearest integer, halves to even in the default rounding mode. `std::floor(x * 1e6 + 0.5)` rounded an odd count between 2^52 and 2^53 one up, such as `5000000000.000001` seconds, where the count is exact and adding 0.5 makes a tie. `std::llround()` would round halves away from zero, but is unspecified for an infinity or a value beyond 64 bits, which the range check after rounding handles as it is. The tests pin that example, its negative, and two exact halves, and the dates that `dbFetch()` makes of huge and infinite reals in a DATE column, which the Arrow array holds as NULL. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
|
Review of the fixes in this pull request at 3ac16c6 (the commits since 689a5c5), by a Claude Code reviewer with clean context that read the diff of 3ac16c6 against its parent f6f6b3e (the merge of #859's head, taken as context), the description, the first review, the fetch loop and read-ahead in What was run
What holds
HighNone. MediumNone. Low
Generated by Claude Code |
…olumn An open `int64` column kept the reading as a real of every text or blob value for a real that may widen it later in the chunk, 16 bytes each, also when the reading equals the integer, and kept their capacity until the result was cleared. It now keeps only the readings that differ from the integer, and releases them once the first chunk fixes the type. A blob in a `double` or open `int64` column is read as a real with `sqlite3_column_double()` instead of as text first, so that a zeroblob() isn't expanded just to read 0. Running out of memory while copying a blob for that reading shows only in the error code of the connection, which is compared with its value before the call, since a code left from an earlier call can't be told apart. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
Superseded: Accepted limitationsSuperseded by the current copy. Two review rounds: the first found that running out of memory on the last row of a full chunk returned a wrong value (Medium) and that the first chunk's values depended on row order when an
Generated by Claude Code |
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
Question: drop the reading as a real of text in an open
|
Question: detect running out of memory while reading a blob as a number in every case?A blob read into a
Kirill decides. If nobody decides, the check stays as it is, and the limitation stays checked in the "Accepted limitations". Generated by Claude Code |
Question: name the column and the limit when a chunk's text or blobs exceed 2^31 − 1 bytes?
Kirill decides. If nobody decides, the message stays until pull request 8, and the limitation stays checked in the "Accepted limitations". Generated by Claude Code |
Question: report the conversions of text and blob values in this pull request?Values that don't fit the type of their column are converted or become library(DBI)
con <- dbConnect(RSQLite::SQLite())
res <- dbSendQuery(con, "
SELECT CAST(x'ff' AS TEXT) AS bad_utf8, 'a' || char(0) || 'b' AS nul,
CAST('12abc' AS TEXT) AS t
UNION ALL SELECT 'ok', 'ok', 3
")
# Invalid UTF-8 and text with a NUL byte become NULL, and the number 3
# becomes the text "3", all without notice
# (expected: a warning, or a report as #862 adds)
nanoarrow::convert_array(RSQLite:::result_fetch_arrow_chunk(res@ptr, -1L))
#> bad_utf8 nul t
#> 1 <NA> <NA> 12abc
#> 2 ok ok 3
dbClearResult(res)
dbDisconnect(con)
Kirill decides. If nobody decides, the conversions stay silent until #862, and the limitation stays checked in the "Accepted limitations". Generated by Claude Code |
Accepted limitationsTwo review rounds: the first found that running out of memory on the last row of a full chunk returned a wrong value (Medium) and that the first chunk's values depended on row order when an
Generated by Claude Code |
…lumns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
…lumns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
…lumns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
…lumns Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd8MVXyvU7qoMeruD1R
Fifth step of the Arrow rework (plan: #829 (comment)): the internal Arrow engine of #859 now builds arrays from text and blob columns. The entry point
result_fetch_arrow_chunk()stays internal, so nothing changes for users.This branch merges
claude/utf8-infra(#845) with a merge commit, for its validatorrsqlite_is_utf8_string(). Until #845 lands, its changes show in this diff:src/utf8.cpp,src/utf8.h,src/utf8.dd, the registration ofutf8_valid(),tests/testthat/helper-utf8.Randtests/testthat/test-utf8.R. The change of this pull request is in the three commits that aren't merges: the feature, and the fixes of its two reviews. A second merge commit brings in the current head of #859.What changes:
SqliteArrowColumntypes a column whose first value that is notNULLis text asutf8, and one whose first such value is a blob asbinary. A column whose values in the first chunk are allNULLtakes the type of its declared affinity, now alsoutf8for TEXT (TEXT,CHAR(n),CLOB, ...) andbinaryfor BLOB, as the data-frame path maps them. This replaces the error "Arrow arrays of text and blobs are not supported yet." of feat: Build Arrow arrays from the integer, real and NULL columns of a result #859, and its test.utf8orbinarycolumn are the text SQLite renders, as the data-frame path reads them.binarycolumn is its UTF-8 bytes, also in a UTF-16 database, as the data-frame path reads it.utf8column keeps its bytes, which must be valid UTF-8.sqlite3_column_text()would take them for text in the encoding of the database and transcode them in a UTF-16 one.int64ordoublecolumn are read as SQLite reads them: the leading integer, or the leading real. Where SQLite would saturate the leading integer of text or a blob, the value isNULL, as feat: Build Arrow arrays from the integer, real and NULL columns of a result #859 already does for a real beyond the range of int64 (22c4927). To tell a saturated integer from 2^63 - 1, the column reads the digits as SQLite does, as text in the encoding of the database: unlike feat: Implement the DBI Arrow interface natively #795, which read the raw bytes of a blob, this sees the digits of a blob in a UTF-16 database, where SQLite reads them as UTF-16.int64column has read as integers take the readings as reals they would have in adoublecolumn when a real widens the column later in that chunk, so1, '2.5', 3.5gives1, 2.5, 3.5in any order of the rows. feat: Implement the DBI Arrow interface natively #795 kept the integer reading (2), andNULLfor a saturating integer, so the values depended on the order of the rows. Once the first chunk has fixed the type, text in anint64column keeps its integer reading. While the type is open, the column keeps the reading as a real of each such value that differs from its integer reading, 16 bytes each, and releases them once the first chunk fixes the type. For one integer followed by 5 million text values in one chunk, words such as'abc1'keep nothing (peak +40 MB, as without this rule) and values such as'1.5'keep 80 MB (peak +164 MB). Reading every such value as a real also takes time: for 2 million words, the chunk takes 0.21 s instead of 0.15 s.utf8column that are not valid UTF-8 areNULL: SQLite doesn't enforce UTF-8 for text, and a blob keeps any bytes. Arrow requires valid UTF-8 inutf8arrays, and its full validation rejects a chunk that holds anything else.NULL, as in feat: Build Arrow arrays from the integer, real and NULL columns of a result #859.NULLpointer fromsqlite3_column_text()orsqlite3_column_blob()and fails only the next step, but after a full chunk that step only reads ahead, and keeps its error for the next fetch: the chunk would come back with an empty string, aNULLor a zero in place of the value. The size of a blob is read beforesqlite3_column_blob(), which tells azeroblob()that can't expand from a blob of no bytes, and text read into adoublecolumn is read as UTF-8 text first, so thatsqlite3_column_double()has nothing left to allocate. A blob read into adoubleor openint64column is read withsqlite3_column_double()alone, so that azeroblob()isn't expanded just to read 0; SQLite reports running out of memory while copying a blob for that reading only through the error code of the connection, which is compared with its value before the call, as a code left from an earlier call can't be told apart (the next step then fails, as before).rsqlite_is_utf8_string()itself. feat: Implement the DBI Arrow interface natively #795'sis_utf8_text()wrapper is not carried over, since the validator has its own ASCII fast path.NULLin autf8column, and keeps all its bytes in abinarycolumn. This is feat: Implement the DBI Arrow interface natively #795's rule, and it is what the validator implements, so the two don't conflict. Arrow itself allows NUL inutf8, but R strings can't hold it: nanoarrow fails when a converted string with a NUL is used, and arrow refuses to convert it unlessarrow.skip_nulis set. Ending the text at the first NUL, as the data-frame path does, would change the value without a trace, whileNULLtreats a NUL like the other bytes that are not text.None of these conversions is reported yet. The task left the choice between #795's simpler warning and saying what I chose: I kept them silent, as #859 keeps its own conversions (a real in a fixed int64 column, a value in a column of the null type) silent until pull request 7. One rule then holds for every conversion of a column, and pull request 7 adds the report for all of them at once instead of replacing a warning that only some of them raise. #795's warning is raised once per column from inside the fetch loop, where its handlers run while the busy guard holds the result; it is about 30 lines if reviewers prefer it in the meantime.
Where the values differ from
dbFetch():NULL, wheredbFetch()passes it through; the tests pin both.NULL, wheredbFetch()ends it at the first NUL; the tests pin both.dbFetch()transcodes them from UTF-16 on this base. The tests pin only the Arrow value, since fix: Validate blobs that are read into a text column #799 reads such blobs as bytes on the data-frame path too.dbFetch()types each fetch anew.dbFetch()keeps the integer reading after a real widens the column ('2.5'gives2), and gives-1or0for a saturating integer in a 32-bit column.Deliberately left out:
utf8andbinaryarrays are pull request 8. Until then, a chunk whose values in one column exceed 2^31 - 1 bytes fails with "Can't append to Arrow array", which completes the result like any other error of a fetch.large_utf8are pull requests 13 and 14, and the DBI methods are pull request 16.Tested:
tests/testthat/test-arrow-text.R: text and blob columns with empty strings, empty blobs andNULLamong them; numbers and blobs in autf8column and text and numbers in abinaryone; text and blobs inint64anddoublecolumns, with the saturating cases; mixed types within the first chunk and converted in a later chunk, also into columns typed by TEXT and BLOB affinity and into the null type; text that is not valid UTF-8 (cast from blobs: a stray continuation byte, a truncated sequence, an overlong form, a surrogate, beyond U+10FFFF, an invalid byte between runs of 20 ASCII bytes, and a sequence truncated after them), inutf8and inbinarycolumns; embedded NUL bytes at the start, middle and end; values up to 2 MB; chunks of 1, 2, 3 + 4 and all rows up to one past the end, with the read-ahead row starting the next chunk; a UTF-16 database with non-ASCII text, a blob in a text column, a lone surrogate at the end of the text, which makes itNULL, and one before another character, which SQLite merges into one code point (x'780000d86100'becomes"x\U00010061", the same indbFetch()), and a NUL character; the digits of blobs in a UTF-16 database read into anint64column, in the chunk that decides the type and in a later one; whitespace and a sign before saturating integers; text in anint64column that a real widens, in three orders of the rows; and values that run out of memory as the last row of a full chunk, inutf8,int64,doubleandbinarycolumns and as a blob in adoublecolumn, and azeroblob()in adoublecolumn that reads as 0 under a heap limit below its size. Where the rules agree, each chunk is compared withdbFetch()in the same chunk sizes, withdbHasCompleted()anddbGetRowCount()after each chunk.tests/testthat/test-arrow-columns.R: the test of all-NULLcolumns covers each affinity, with TEXT,CHAR(1)and BLOB added.tests/testthat/helper-arrow.R:arrow_values()convertsutf8to character andbinaryto blob, asdbFetch()returns them, and the newexpect_valid_chunk()checks every chunk that the tests build: nanoarrow's full validation, which reading the chunk back from the Arrow IPC format runs, and valid UTF-8 in itsutf8columns, which nanoarrow doesn't check.expect_same_chunks()calls it for every chunk, and takesmixed = TRUEwheredbFetch()warns of mixed types.pkgload::load_all()it skips, and it runs inR CMD checkand against an installed copy. It also skips on CRAN, as it takes about 100 MB. Against an installed copy of this branch it passes; against 689a5c5 each chunk comes back without an error, and against 3ac16c6 thezeroblob()fails with "out of memory". It is also the test of an error while a chunk is built, which feat: Build Arrow arrays from the integer, real and NULL columns of a result #859's removed "not supported yet" test was; the overflow beyond 2^31 - 1 bytes is left for the small limits of pull request 8.test_that()block of the two Arrow test files passes on its own afterpkgload::load_all()at the head, where the out-of-memory test skips; both files pass in full against an installed copy, with that test.ValidateFull()accepts all 71 chunks that the two test files build (arrow is not a test dependency here).sqlite3_column_text()fails the UTF-16 block; without the saturation check, theint64block fails; reading the raw bytes of a blob for the saturation check, as feat: Implement the DBI Arrow interface natively #795 did, fails theint64block; the previous head fails the widening and the out-of-memory blocks.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).-O1), on an installed copy of 3ac16c6, before the second review's fixes,test-arrow-text.Rwith the out-of-memory test,test-arrow-columns.R,test-utf8.R,test-read-ahead.Randtest-encoding.Rpass without a report; before the review fixes, so didtest-dbSendQuery.R,test-dbClearResult.R,test-error.R,test-dbBind.Randtest-blob.R. So does a script with chunks of several sizes over text, blobs, invalid UTF-8, NUL bytes and azeroblob(), rebinding between text, integers and blobs, and arrays converted after their result was cleared and the connection closed, also undergctorture(TRUE).R CMD check: nothing in the build,DESCRIPTION,NAMESPACEor the documentation changes, beyond the files of feat: Add a UTF-8 validator for strings read from SQLite #845.Cost of the UTF-8 check, measured on one million 50-byte values in one column, fetched in one chunk, median of nine runs in each of three rounds, against a build that skips the check: ASCII text takes 0.159 s instead of 0.148 s (about +7 %), text with 40 bytes of
é0.187 s instead of 0.149 s (about +26 %), and text with 39 bytes of日0.189 s instead of 0.153 s (about +24 %).dbFetch()takes 0.55 s to 0.60 s for the same columns. The ASCII figure is close to the 5 % that #795 measured; the cost for non-ASCII text lies in #845's validator, which checks such text sequence by sequence.The UTF-8 check is unchanged by the review fixes, which add only a test for a
NULLpointer to reading text.Sits on #859, which is on #857, #856, #855, #830 and #850, and merges #845.
Size
About 295 hand-written lines of C++ (
SqliteArrowColumn.cppand.h, about 45 of them replacing #859's lines, and 120 from the review fixes) and 510 lines of tests, not counting #845's files.Review
Two review rounds (first, second) led to 3ac16c6 and 32dea06; see the accepted limitations.
https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
Generated by Claude Code