Skip to content

feat: Keep Arrow utf8 arrays valid for invalid UTF-8, NUL and UTF-16 databases - #959

Draft
krlmlr wants to merge 6 commits into
claude/rb-arrow-textfrom
claude/rb-arrow-utf8-checks
Draft

krlmlr wants to merge 6 commits into
claude/rb-arrow-textfrom
claude/rb-arrow-utf8-checks

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for users: the Arrow chunks are still fetched only through the unexported result_fetch_arrow_chunk(). This pull request makes the utf8 arrays of those chunks always valid, and lets every out-of-memory failure while reading a value fail its chunk at once.

What changes:

  • Text or a blob in a utf8 column that isn't valid UTF-8 or holds a NUL byte becomes NULL, checked with main's validator rsqlite_is_utf8_string() (feat: Add a UTF-8 validator for strings read from SQLite #845). This is exactly what dbFetch() does with dbConnect(check_utf8 = TRUE), including reading a blob in a text column as its bytes. A utf8 column checks this also on a connection with check_utf8 = FALSE, where dbFetch() keeps such bytes. Numbers render as ASCII and aren't checked. A binary column keeps such bytes. Like the other conversions of Arrow chunks, the NULL values are silent until the conversion reports of F4a; the header of SqliteArrowColumn says so, and the TODO that F2a left there is gone.
  • UTF-16 databases need no code of their own: sqlite3_column_text() converts text to UTF-8 and sqlite3_column_bytes() after it gives the UTF-8 byte count, while a blob keeps its bytes in a utf8 column. A lone surrogate at the end of the text converts to bytes that aren't valid UTF-8, so it is NULL. The tests now pin this.
  • Reading text or a blob as a real (a double column, or the reading as a real that an open int64 column keeps for a later widening) used sqlite3_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 by sqlite3_errcode() == SQLITE_NOMEM after the call, as feat: Build Arrow arrays from text and blob columns #860 did for blobs. sqlite3_column_double() sets that code through columnMallocFailure(), 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 as sqliteCopyDatabase() 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 with sqlite3_exec(conn, "", NULL, NULL, NULL): on empty SQL, SQLite resets the error code with sqlite3Error(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 of SqliteArrowColumn now describes the clearing instead of that caveat. The call reads UTF-16 text directly into a copy of about half its bytes, exactly as CAST(x AS REAL) does, without converting it to UTF-8 (about twice its bytes), and a zeroblob() is still read as 0 without being expanded. For a UTF-16 text of 4 million characters (8 MB) after the step to it, a double column 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 open int64 column, 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:
    • invalid UTF-8: overlong forms, surrogates, beyond U+10FFFF, and truncated sequences at and across word boundaries, as text and as blobs after text. The values are compared with dbFetch() on a check_utf8 = TRUE connection, in one chunk and in chunks that start with such values, and a binary column keeps their bytes.
    • NUL bytes in text, in text cast from blobs, and in blobs after text.
    • 500 random byte strings, alternating text and blobs, against dbFetch() with check_utf8 = TRUE.
    • UTF-16le and UTF-16be databases: non-ASCII text up to four bytes per character and 1 MB long, also in chunks of one and two rows; its UTF-8 bytes in a binary column; blobs in a utf8 column; a lone surrogate at the end and a NUL character; text and blobs of UTF-16 digits in int64 and double columns, against CAST(v AS REAL), with saturation.
    • Leading reals of UTF-16 text and blobs that a NUL character, a character beyond ASCII or a lone surrogate ends, against CAST(v AS REAL).
    • Out of memory in a UTF-16 database under local_sqlite_heap_limit(): text and a blob in a double column on the last row of a chunk of one row, and text in an open int64 column. 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 for int64), the same values read as 2.5 and 2. A zeroblob(50000000) in a double column still reads as 0 under the limit. The block skips on CRAN, like the existing out-of-memory tests.
    • A 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() gains invalid_utf8 = TRUE to muffle the warning of check_utf8 = TRUE. expect_valid_chunk() already checks that the utf8 values are valid UTF-8, and its conversion to R fails on a NUL byte.
  • Red → green: against the base (d735f90), the UTF-8, NUL, random-bytes and UTF-16 blocks fail or error (invalid UTF-8 in utf8 arrays; a NUL byte makes the conversion to R fail). The out-of-memory block fails 9 expectations: the double cases return 0 and leave the result open, and the int64 case 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 after pkgload::load_all().
  • Full suite with NOT_CRAN=true against an installed copy: FAIL 0, WARN 3 (locale zh_TW.UTF-8 not 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 NULL values for invalid UTF-8 and stays deferred to F4a. The question on the reading as a real in an open int64 column (#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

…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
@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 06:11
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
claude and others added 3 commits December 1, 2026 06:36
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

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