Skip to content

feat: request Arrow types for result columns with schema - #984

Draft
krlmlr wants to merge 8 commits into
claude/rb-dbi-arrow-writefrom
claude/rb-dbi-arrow-schema
Draft

krlmlr wants to merge 8 commits into
claude/rb-dbi-arrow-writefrom
claude/rb-dbi-arrow-schema

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

For users: dbSendQueryArrow(), dbGetQueryArrow(), dbReadTableArrow(), dbFetchArrow() and dbFetchArrowChunk() get a schema argument that requests the Arrow types of the result's columns, exposing the requested-types mechanism of #979 and #980. It takes a nanoarrow_schema of a struct type or anything that nanoarrow::as_nanoarrow_schema() converts to one, such as a schema of the arrow package. dbReadTableArrow() now has RSQLite's own method (DBI's default dropped schema), and the read methods refuse arguments they don't use, as the write methods of #982 do, except DBI's documented immediate, which they accept and ignore. A requested type replaces the inference for its column, including #981's int32 for integers that fit, bigint and the declared type; the other columns are inferred as before. Documented in the new "Requested types" section of ?"SQLiteResultArrow-class", with the argument in the Rd, an example and a NEWS bullet.

Matching. #979 left the choice between positional and by-name matching and partial schemas to this item. As implemented: fields with names take the columns of those names, in any order, and the columns that no field names are inferred (a partial schema); fields that all have empty names take all columns by position, one field for each column; a schema with no fields requests nothing. Names match exactly, case-sensitively, as the names of the result's columns come back; a column named "" can only be requested by position (documented). Errors, all of class RSQLite_error_schema with the call of the user's DBI method (also dbGetQueryArrow() and dbReadTableArrow()): a schema that isn't convertible or isn't a struct, fields with and without names mixed, a field name that occurs twice, a name that the result doesn't have (listing the result's columns), a name that the result has more than once (pointing to positional fields), a positional count mismatch, and a type that a column can't build (#979/#980's message, now naming the result's column number).

When the checks run. dbSendQueryArrow() resolves and checks the schema after preparing the statement but before it claims the result and before it binds params, so nothing is read and a refused schema leaves no open result; the column names come from the prepared statement (new internal result_column_names()), and the types are checked by the new internal result_check_arrow_request(), which builds the requested types and runs SqliteResultImpl::check_arrow_request() without claiming. The checked request is kept with the SQLiteResultArrow (new request slot, an environment) as Arrow format strings, not as the caller's schema, whose children only borrow from it (round 1 of the review found a use after free when the caller released the schema), and passed to every fetch, so it holds for later chunks, for streams, for dbColumnInfo(), for DBI's dbFetch() on the Arrow result, and for new executions after dbBind(). A schema passed to dbFetchArrow() or dbFetchArrowChunk() replaces the kept request for this and later fetches; once the first chunk of an execution has fixed the types, it must request those types for the columns it names, per #979's rule, and while a stream of the execution exists before any chunk has fixed the types, it must request exactly the types that the stream reads with, for every column (round 1 found that a different request was accepted then, and the stream and later fetches fixed different types). A refused one leaves the result and the kept request as they were; a cleared result fails as without a schema. By-name fields become positions when the types are requested, so a statement that SQLite prepares again after a schema change takes them by position on a later dbBind() (documented; the positional rule predates this PR).

Internals. The internal fetches keep their positional struct schema. They also take the resolved request, a list of class RSQLite_requested_types with one nanoarrow_schema, Arrow format string or NULL per column; NULL is AK_UNDECIDED, which the chunk sink already treats as "infer", so the int32 rule and bigint still apply to those columns. check_arrow_request() now compares only the requested columns with the types the first chunk fixed, and its error names the column ("..., as for column 1 (x)."); with the streams of the execution (DbResult's counters) it also compares the request with the one in force. Request errors are a new ArrowRequestError, which result_check_arrow_request() returns as a message for R to raise with its class; the two internal tests with that message are updated. Nullability and metadata stay ignored, as documented in #979.

Tests in the new tests/testthat/test-arrow-dbi-schema.R: by name in another order with inferred columns (chunk, dbFetchArrow(), dbGetQueryArrow()), names that aren't ASCII, by position, a schema with no fields, a schema from infer_nanoarrow_schema() and from a nanoarrow_vctr; every requested type family of #979 and #980 through the public API in one query (int8 to uint64, bool, null, float, double, utf8, large_utf8, binary, large_binary, date32, date64, time32 s/ms, time64 us/ns, timestamps in each unit with and without zones, durations) with exact values and NULLs, valid by nanoarrow's IPC round trip, and the same types from a stream; conversion warnings; a requested type overriding int32 and bigint = "character" while other columns keep them; later chunks past 65536 rows and new executions after dbBind() keeping the request; a later fetch's schema replacing the request, refused for other fixed types (snapshot) without changing the row count, accepted for the fixed types of a column it didn't request; every error in a snapshot, run on a statement whose bind would fail, which shows that nothing is read, and leaving no open result; nullability and metadata ignored; dbColumnInfo() reporting the R types of requested types and keeping the first chunk; a stream with requested types read on a worker thread through #978's hook, with its warnings at the next call, and a dbGetQueryArrow() stream on a worker with its warnings at release; a stream without and with a request refusing another request (both probes of the review) while the stream and later chunks read on, the same request passing, and a new execution after release; a schema released or moved by the caller after the request; dbReadTableArrow() with a schema and an Id; unused arguments refused (snapshot) and immediate accepted; the error classes; a cleared result failing the same way with and without a schema. Under valgrind the test file and the review's probe of a released schema: 0 errors, 0 bytes definitely lost. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (the zh_TW.UTF-8 locale is missing in the container), SKIP 64, PASS 14518 (#981: FAIL 0, WARN 3, SKIP 64, PASS 14239); the DBItest suites pass unchanged. R CMD check --no-manual on the built tarball (with vignettes, and _R_CHECK_PACKAGES_USED_IN_TESTS_USE_SUBDIRS_=true as in CI, which caught an arrow:: call in the first version of the tests): 0 errors, 0 warnings, 1 NOTE (the worktree's .git file, included by the build).

Decisions worth a look by the maintainer, implemented as described: by-name matching with partial schemas and positional matching only for schemas without any names (mixed refused); exact, case-sensitive name matching, although SQLite's identifiers are case-insensitive; a schema of a later fetch replacing the kept request for later fetches and executions rather than applying to that fetch only; and the kept request holding for new executions after dbBind(), where the internal fetches infer anew unless requested again.

Plan item 18, a layer of the rebuilt chain planned in #880; based on #982 (item 17). Not ported from an old PR: the old chain had no DBI methods.

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


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

`dbSendQueryArrow()`, `dbGetQueryArrow()`, `dbFetchArrow()` and `dbFetchArrowChunk()`
take a `schema` that requests the Arrow types of the columns,
anything that `nanoarrow::as_nanoarrow_schema()` converts to a struct schema.
Fields with names take the columns of those names, and the other columns are inferred;
fields that all have empty names take all columns by position.
The request is resolved and checked before the result is claimed and before any row is read,
kept with the result for later fetches and executions,
and replaced by a schema of a later fetch, which must match the types that the first chunk fixed.
A requested type replaces int32 for integers that fit and `bigint`.

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 17:46
claude and others added 7 commits December 1, 2026 17:46
… streams

The result kept the children of the `schema` that the caller passed,
which borrow from it, so releasing the schema left dangling pointers.
It now keeps the checked types as Arrow format strings.

A request was accepted while a stream of the execution existed
but no chunk had fixed the types yet,
and the stream and later fetches then fixed different types.
While such a stream exists, a request must now be the one the stream reads with.

Errors about `schema` have the class `RSQLite_error_schema` and the user's call,
and a cleared result fails as without a schema.
The read methods refuse unused arguments except DBI's documented `immediate`,
and `dbReadTableArrow()` has RSQLite's own method that takes `schema`.
The docs say that a column without a name is requested by position,
and that positions are fixed when the types are requested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
The arrow package isn't a declared dependency,
and R CMD check warned about its use in the tests.

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