Skip to content

feat: Build Arrow arrays from text and blob columns - #956

Draft
krlmlr wants to merge 3 commits into
claude/rb-arrow-column-typesfrom
claude/rb-arrow-text
Draft

krlmlr wants to merge 3 commits into
claude/rb-arrow-column-typesfrom
claude/rb-arrow-text

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(). With this pull request they are built from text and blob columns too, which so far failed the chunk with "Arrow arrays of text values are not supported yet."

What changes:

  • 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 whose values in the first chunk are all NULL takes utf8 for a declared TEXT affinity (TEXT, VARCHAR, CLOB, ...) and binary for BLOB, as dbFetch() types them.
  • Values of another storage class than the type of their column are read as SQLite converts them, following feat: Build Arrow arrays from text and blob columns #860: numbers in a utf8 or binary column are the text that sqlite3_column_text() renders (1.0, 1.0e+300, 1000000000000000.0, as dbFetch() reads them); text in a binary column is its UTF-8 bytes; a blob in a utf8 column keeps its bytes; text and blobs in an int64 or double column are their leading integer or real, as for CAST(), and NULL where SQLite would saturate the leading integer, as F1b does for a real beyond the range of int64. In the first chunk, text and blobs that an int64 column has read as integers take their reading as a real when a later real widens the column, so the values don't depend on the order of the rows. Any value in a column of the null type is NULL.
  • A value that SQLite can't convert for lack of memory (a NULL pointer from sqlite3_column_text(), or from sqlite3_column_blob() for a blob of nonzero size) fails the chunk with "Column `x`: out of memory while reading a value.", also on the last row of a full chunk, and completes the result like any other error of a fetch.
  • utf8 and binary values of one chunk that would exceed the 2^31 - 1 bytes of the 32-bit offsets fail the chunk with an error that names the column and the limit, rather than building an invalid array. F5 ends chunks before that limit.

Like F1b's own conversions, these are silent until the conversion reports of F4a; the header of SqliteArrowColumn says so and the tests pin the values. Left for F2b: checking that utf8 values are valid UTF-8 without NUL bytes, UTF-16 databases, and detecting running out of memory while a blob is read as a number.

Tested:

  • New tests/testthat/test-arrow-text.R: text and blob columns with empty strings, zero-length blobs (x'', zeroblob(0)) and NULL among them, and non-ASCII UTF-8 up to four bytes per character; numbers rendered as text, including the formatting of reals, integers at the limits of int64 and infinity; text and numbers in a binary column and blobs in a utf8 one; text and blobs in int64 and double columns against dbFetch() and CAST(x AS REAL), with the saturating cases (spaces, signs, leading zeros, ±2^63); text in an int64 column that a later real widens, in three orders of the rows; conversions in a later chunk into all six types; values up to 2 MB; mixed storage classes within and across chunks, with chunk boundaries up to one past the end. Where the rules agree, each chunk is compared with dbFetch() on a separate result in the same chunk sizes, with dbHasCompleted() and dbGetRowCount() after each chunk.
  • tests/testthat/helper-arrow.R: arrow_values() converts utf8 to character and binary to blob; the new expect_valid_chunk() checks every chunk that expect_same_chunks() builds: nanoarrow's full validation, which reading the chunk back from the Arrow IPC format runs (nanoarrow 0.6.0 or later), and valid UTF-8 in its utf8 columns.
  • tests/testthat/test-arrow-column-types.R: the tests that expected "not supported yet" now expect NULL for text and blobs in a column of the null type, and utf8 and binary for columns of NULL values declared as VARCHAR, CLOB, TEXT and BLOB, kept by the later chunks.
  • tests/testthat/test-arrow-chunks.R: the test of an error while a chunk is built now runs out of memory under a heap limit, with a zeroblob() in binary and utf8 columns and a large text in a UTF-16 database (the existing helper), and checks that the result is complete; it skips on CRAN.
  • Mutations: without the saturation check, or without the readings as reals, the corresponding blocks fail.
  • By hand: a chunk of 2^31 - 1 bytes in a binary column builds, one byte more fails with the new error in binary and utf8 columns, and the connection works afterwards (gigabytes of memory, so not in the suite).
  • Each test_that() block of the three Arrow test files passes on its own right after pkgload::load_all(), apart from two unchanged blocks of test-arrow-chunks.R whose expect_snapshot() needs the third edition that the harness didn't set.
  • Full suite with NOT_CRAN=true against an installed copy: FAIL 0, WARN 3 (locale zh_TW.UTF-8 not available), SKIP 63, PASS 11870.

Questions carried over from #860, for the record: reading text as a real in an open int64 column (#860 (comment)) still applies here, with its cost in time and up to 16 bytes per differing value until the chunk ends. Reporting the conversions (#860 (comment)) still applies, deferred to F4a as in F1b. Naming the column and the limit beyond 2^31 - 1 bytes (#860 (comment)) is settled here: the error names both. Running out of memory while reading a blob as a number (#860 (comment)) moves to F2b with the rest of the out-of-memory handling: here, a blob read into a double or open int64 column goes through sqlite3_column_double() without a check.

Layer F2a of the rebuilt chain, planned in #880; based on #955. Ported from #860 (draft), without its UTF-8 validation, UTF-16 tests and the out-of-memory refinements that F2b takes.

Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

The internal Arrow chunks type text columns as utf8 and blob columns as binary,
and a column of NULL values declared as TEXT or BLOB takes that type.
Values of another storage class are read as SQLite converts them:
numbers in a utf8 or binary column are the text that SQLite renders,
text in a binary column is its UTF-8 bytes, and a blob in a utf8 column keeps its bytes.
Text and blobs in an int64 column are their leading integer, or NULL where SQLite would saturate it,
and take their reading as a real when a later real in the first chunk widens the column.
Any value in a column of the null type is NULL.
The conversions stay silent until the conversion reports.

A value that SQLite can't convert for lack of memory fails the chunk,
and so do utf8 or binary values of one chunk beyond the 2^31 - 1 bytes of 32-bit offsets.
Ported from #860, without its UTF-8 checks.

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 05:46

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: drop the reading as a real of text in an open int64 column?

Carried over from #860's question, which nobody answered; this pull request ports #860's text and blob columns onto the row sink and implements the default described below. The links below point at #860's lines.

In the first chunk, every text value in an int64 column whose type is still open is also read as a real, so that a real arriving later in the chunk widens it with the same values in any order of the rows (src/SqliteArrowColumn.cpp#L235-L246). That takes time (2 million text values: 0.21 s instead of 0.15 s), and each reading that differs from the integer costs 16 bytes until the chunk ends (5 million fractions: +164 MB peak instead of +40 MB). The description states these figures.

  • Keep it: it is the price of values that don't depend on row order (recommended)
  • Go back to feat: Implement the DBI Arrow interface natively #795's rule, which keeps the integer reading after a real widens the column, so that 1, '2.5', 3.5 gives 1, 2, 3.5 and the values depend on row order

Kirill decides. If nobody decides, the reading stays, and the limitation stays checked in the "Accepted limitations".


Generated by Claude Code

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