Repository navigation
Conversation
…databases Text or a blob in a utf8 column that isn't valid UTF-8 or holds a NUL byte is NULL, as dbFetch() reads it with dbConnect(check_utf8 = TRUE), checked with the validator of #845. A binary column keeps such bytes. Numbers render as ASCII and aren't checked. Like the other conversions of Arrow chunks, this is silent until F4a. The tests compare chunks with dbFetch() on a connection with check_utf8 = TRUE, also on random bytes, and cover UTF-16le and UTF-16be databases: non-ASCII text, its UTF-8 bytes in a binary column, blobs that keep their bytes in a utf8 column, a lone surrogate at the end of the text, and text and blobs of UTF-16 digits in int64 and double columns. Ported from #860. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
sqlite3_column_double() reads text and blobs from a copy, and gives 0 if that copy runs out of memory. On the last row of a chunk, the chunk came back with that 0, and only the next fetch failed. Text is now read as UTF-8 first, which tells a failed conversion by its NULL pointer; a blob is checked by the error code of the connection, without expanding a zeroblob(). When an earlier call left that code, the next step fails instead, as #860 accepted. Ported from #860. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Reading UTF-16 text as a real converted it to UTF-8 first, which takes about four times the memory of the copy that `sqlite3_column_double()` makes. A long numeric text then failed the chunk where `CAST(x AS REAL)` succeeds. Text is now read like blobs: `sqlite3_column_double()` directly, with `sqlite3_errcode() == SQLITE_NOMEM` after the call telling a failed copy. Every step resets that code, so the caveat about a stale code is gone. The header now also says that a utf8 column checks UTF-8 with `check_utf8 = FALSE`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Reading text or a blob as a real treated `SQLITE_NOMEM` as the error code of the connection after `sqlite3_column_double()` as a failed copy. The first row of a chunk was stepped by the previous fetch, and a call on the connection in between, such as `sqliteCopyDatabase()` running out of memory, could leave that code. A valid value then failed the chunk and completed the result. The code is now read before the call as well. When it is already set, text is read as UTF-8 first, which tells a failed conversion by its NULL pointer; a blob reads as 0 if its copy fails, and the next step fails, as the header now says again. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…g a real With `SQLITE_NOMEM` left on the connection by a call between two fetches, reading a blob as a real couldn't tell a failed copy from that code. A chunk of one row then returned 0, and "out of memory" showed only on the next fetch. The code is now cleared with `sqlite3_exec()` on empty SQL, which resets it to `SQLITE_OK` without allocating or touching the statement, so text and blobs alike are checked after `sqlite3_column_double()`. The header no longer needs the caveat for blobs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No change of behavior for users: the Arrow chunks are still fetched only through the unexported
result_fetch_arrow_chunk(). This pull request makes theutf8arrays of those chunks always valid, and lets every out-of-memory failure while reading a value fail its chunk at once.What changes:
utf8column that isn't valid UTF-8 or holds a NUL byte becomesNULL, checked withmain's validatorrsqlite_is_utf8_string()(feat: Add a UTF-8 validator for strings read from SQLite #845). This is exactly whatdbFetch()does withdbConnect(check_utf8 = TRUE), including reading a blob in a text column as its bytes. Autf8column checks this also on a connection withcheck_utf8 = FALSE, wheredbFetch()keeps such bytes. Numbers render as ASCII and aren't checked. Abinarycolumn keeps such bytes. Like the other conversions of Arrow chunks, theNULLvalues are silent until the conversion reports of F4a; the header ofSqliteArrowColumnsays so, and the TODO that F2a left there is gone.sqlite3_column_text()converts text to UTF-8 andsqlite3_column_bytes()after it gives the UTF-8 byte count, while a blob keeps its bytes in autf8column. A lone surrogate at the end of the text converts to bytes that aren't valid UTF-8, so it isNULL. The tests now pin this.doublecolumn, or the reading as a real that an openint64column keeps for a later widening) usedsqlite3_column_double()unchecked. That reads from a copy and gives 0 when the copy runs out of memory, so on the last row of a chunk, whose next step only reads ahead and keeps the error, the chunk came back with a wrong 0. Text and blobs are now checked bysqlite3_errcode() == SQLITE_NOMEMafter the call, as feat: Build Arrow arrays from text and blob columns #860 did for blobs.sqlite3_column_double()sets that code throughcolumnMallocFailure(), and every step resets it. The first row of a chunk, though, was stepped by the previous fetch, and a call on the connection in between (such assqliteCopyDatabase()running out of memory) can leave the code set, so it is read before the call too. When it is already set, it is cleared first withsqlite3_exec(conn, "", NULL, NULL, NULL): on empty SQL, SQLite resets the error code withsqlite3Error(db, SQLITE_OK), allocates nothing and leaves the active statement alone, so text and blobs alike are checked after the call. An earlier revision of this branch (015449d) couldn't clear the code and read a blob as 0 in that case: with a real out-of-memory while copying the blob, a chunk of one row came back as 0 and "out of memory" showed only on the next fetch. This came up in review round 3, and the reviewer tested the fix; the header ofSqliteArrowColumnnow describes the clearing instead of that caveat. The call reads UTF-16 text directly into a copy of about half its bytes, exactly asCAST(x AS REAL)does, without converting it to UTF-8 (about twice its bytes), and azeroblob()is still read as 0 without being expanded. For a UTF-16 text of 4 million characters (8 MB) after the step to it, adoublecolumn now needs about 4.1 MB of heap above what SQLite uses instead of 16.1 MB with an earlier revision of this branch, and an openint64column, including the step, 12.1 MB instead of 24.1 MB. Reading as an integer (sqlite3_column_int64()) never allocates, and the other paths already failed at once.Tested:
tests/testthat/test-arrow-text.R, new blocks:dbFetch()on acheck_utf8 = TRUEconnection, in one chunk and in chunks that start with such values, and abinarycolumn keeps their bytes.dbFetch()withcheck_utf8 = TRUE.binarycolumn; blobs in autf8column; a lone surrogate at the end and a NUL character; text and blobs of UTF-16 digits inint64anddoublecolumns, againstCAST(v AS REAL), with saturation.CAST(v AS REAL).local_sqlite_heap_limit(): text and a blob in adoublecolumn on the last row of a chunk of one row, and text in an openint64column. Each fails the chunk with "Column `x`: out of memory while reading a value." and completes the result. With room for SQLite's copy (6 MB, and 16 MB with the step forint64), the same values read as 2.5 and 2. Azeroblob(50000000)in adoublecolumn still reads as 0 under the limit. The block skips on CRAN, like the existing out-of-memory tests.sqliteCopyDatabase()into the connection that runs out of memory between two fetches, in UTF-8 and UTF-16le databases, for text and blobs: the next chunk still reads 2.5 and 2.5 and completes the result. Skips on CRAN as well; against cfa3598 it fails with "out of memory while reading a value". The same block now also keeps the heap limit after the copy and fetches a chunk of one row whose blob of 200 kB can't be copied: it fails at once with "Column `x`: out of memory while reading a value." and completes the result, in both encodings, where 015449d returned 0 (6 failed expectations).tests/testthat/helper-arrow.R:expect_same_chunks()gainsinvalid_utf8 = TRUEto muffle the warning ofcheck_utf8 = TRUE.expect_valid_chunk()already checks that theutf8values are valid UTF-8, and its conversion to R fails on a NUL byte.utf8arrays; a NUL byte makes the conversion to R fail). The out-of-memory block fails 9 expectations: thedoublecases return 0 and leave the result open, and theint64case returns the chunk and fails only on the next fetch. All pass with this branch. The reads with room for the copy fail with "out of memory" against the earlier revision 31f0b64, which converted UTF-16 text to UTF-8 first. Each new block passes on its own right afterpkgload::load_all().NOT_CRAN=trueagainst an installed copy: FAIL 0, WARN 3 (localezh_TW.UTF-8not available), SKIP 63, PASS 12048.Questions of #860 that this settles: running out of memory while reading a blob as a number (#860 (comment)) takes the recommended option. The connection's error code detects it, for text as well. Reporting the conversions (#860 (comment)) still applies to the new
NULLvalues for invalid UTF-8 and stays deferred to F4a. The question on the reading as a real in an openint64column (#956) is unchanged.Layer F2b of the rebuilt chain, planned in #880; based on #956. Ported from #860 (draft): its UTF-8 validation, UTF-16 tests and out-of-memory refinements, with
local_sqlite_heap_limit()in place of #860's separate R process.Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Generated by Claude Code