Skip to content

feat: implement dbAppendTableArrow(), dbCreateTableArrow() and dbWriteTableArrow() natively - #982

Draft
krlmlr wants to merge 10 commits into
claude/rb-dbi-arrow-fetchfrom
claude/rb-dbi-arrow-write
Draft

krlmlr wants to merge 10 commits into
claude/rb-dbi-arrow-fetchfrom
claude/rb-dbi-arrow-write

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member

For users: dbCreateTableArrow(), dbAppendTableArrow() and dbWriteTableArrow() now have RSQLite's own methods, so DBI's defaults, which converted each chunk of the stream to a data frame and appended it with dbAppendTable(), are no longer used for the usual Arrow types. The rows of the stream go into SQLite through one prepared INSERT that binds the stream directly, with the Arrow bind path of #967–#971 that dbBindArrow() uses since #981. NEWS bullet; the user documentation is the new section "Writing tables" of ?"SQLiteResultArrow-class", with an example.

dbCreateTableArrow() accepts a schema, a stream (which keeps its data), an array or a data frame, as DBI documents. The column types are those that dbCreateTable() gives the data frame that nanoarrow::infer_nanoarrow_ptype() makes of the schema, which is what DBI's default does, so extended_types = TRUE gives DATE for date32, TIME for time32/time64 and TIMESTAMP for timestamp and date64, and REAL without it. One deliberate difference: uint32, int64 and uint64 fields, also as dictionary values, are INTEGER instead of REAL, because the Arrow bind binds them as integers (#970), and a REAL column would turn integers beyond 2^53 into rounded reals. A test compares every type with dbCreateTable() of the ptype, with and without extended types.

dbAppendTableArrow() names the stream's fields as the columns of the INSERT, so that a subset or another order of the table's columns works as DBI specifies, and binds the columns by position (new internal bind_arrow_columns(), split out of bind_arrow_stream()), so that any field name works, including names that a named placeholder can't hold. It returns the number of rows inserted. A column that the table lacks fails when the statement is prepared, before the stream is read. While another result of the connection is reading, the stream is read to its end first (#967's rule), so dbAppendTableArrow(con, "t", dbReadTableArrow(con, "t")) inserts the rows that were there once. A stream with a field that the Arrow bind can't bind, such as a decimal, a string view or a nested type, goes through DBI's default method (callNextMethod()), so that it keeps working as before: decimals are written as reals through nanoarrow's data frames. The check uses the bind path's own type rule through a new internal arrow_schema_can_bind() in C++ (SqliteArrowParamSource::can_bind_column()), so the two can't drift apart.

dbWriteTableArrow() creates, overwrites or appends as dbWriteTable() does: an existing table without append or overwrite is an error, append = TRUE creates a missing table, temporary applies to a new table.

Transactions. Each method runs in a savepoint, like dbAppendTable() and dbWriteTable(): a stream that fails part-way, or a constraint violation in a later chunk, leaves the table and the database as they were, also for overwrite = TRUE and inside a user's transaction, which keeps its own rows. This is a change from DBI's default dbAppendTableArrow(), which appended each chunk in its own call and kept the chunks before a failure; DBI's specification asks that "the remote table remains unchanged" on failure.

Arguments. The methods refuse arguments in ... (rlang::check_dots_empty()), such as field.types or row.names. DBI's defaults ignored most of them, and DBItest requires dbCreateTableArrow(fields = ) to fail, which the default did only by accident (a duplicate argument for dbCreateTable()). append, overwrite and temporary must be TRUE or FALSE, and the name must quote to one identifier. Errors carry the call of the DBI method.

DBItest. The Arrow write tests (arrow_create_table_arrow_*, arrow_append_table_arrow_*, arrow_write_table_arrow_*) all run with the native methods, FAIL 0, with the same 16 skips as on the base: six "Internal: Need to enhance test_arrow_roundtrip()" in DBItest itself and ten for the date_typed, time_typed and timestamp_typed tweaks that RSQLite sets to FALSE. No new skips.

Tests in the new tests/testthat/test-arrow-write.R: column types of every bindable type and a decimal against dbCreateTable() of the ptype, with and without extended types; creation from a schema, a stream that keeps its data, an array and a data frame; values of each storage class round-tripping Arrow → SQLite → data frame and → Arrow, with the typeof() stored; an int64 beyond double precision round-tripping exactly from RSQLite's own read path; NULLs, a null struct row and the null type; dictionaries of utf8 and of int64 values; dates, times and timestamps with extended types on and off, stored as dbWriteTable() stores the data frame and read back as date32/time64/timestamp; insertion by name, case-insensitive, with defaults for missing columns, row counts over several chunks and an empty stream; a column the table lacks failing before the stream is read; a stream of another connection failing at its fifth row (dbAppendTableArrow(), dbWriteTableArrow() appending, overwriting and creating) and a unique constraint in a later chunk, each leaving the table unchanged and no transaction open; streams of the same table from dbReadTableArrow() and dbGetQueryArrow(), also with the first chunk read on another thread; a stream of another connection whose first chunk was read on another thread; quoting of table and column names (keywords, spaces, commas, quotes, backticks, newlines), quoted names, Id() and SQL(); temporary tables invisible to a second connection, also overwritten and appended; create/overwrite/append semantics and a zero-row stream; the decimal fallback, also failing in the savepoint; argument errors (snapshot) and a closed connection. Each test_that() block runs on its own after load_all(). Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (the zh_TW.UTF-8 locale is missing in the container), SKIP 64 (as on #981), PASS 14373 (#981: 14239). R CMD check --no-manual: 0 errors, 0 warnings, 1 NOTE (the worktree's .git file).

Decisions worth a look by the maintainer, implemented as described: INTEGER rather than DBI's REAL for 64-bit integer fields; one savepoint per call instead of per chunk; DBI's default as the fallback for types the Arrow bind can't bind, rather than an error; refusing unused arguments in ... rather than ignoring them.

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

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



Generated by Claude Code

…bWriteTableArrow()` natively

`dbAppendTableArrow()` inserts the rows of a stream with one prepared INSERT
that binds the stream as `dbBindArrow()` does, in a savepoint,
so that a failure part-way leaves the table unchanged.
`dbCreateTableArrow()` types the columns as `dbCreateTable()` types
the data frame that nanoarrow converts the schema to,
except INTEGER for uint32, int64 and uint64.
`dbWriteTableArrow()` creates, overwrites or appends as `dbWriteTable()` does,
also in a savepoint.
A stream with a type that the Arrow bind can't bind, such as a decimal,
goes through DBI's default method inside the savepoint.

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 16:32
claude and others added 7 commits December 1, 2026 16:32
`dbAppendTableArrow()` and `dbWriteTableArrow()` run in `with_savepoint()`.
After a failure, also of the release, a savepoint that began the transaction
is rolled back with ROLLBACK, which needs no lock,
and nothing is rolled back once SQLite has ended the transaction itself,
so the original error is raised.
The fallback for types that the Arrow bind can't bind converts 64-bit integers
to integer64, which keeps their values.
`dbWriteTableArrow(overwrite = TRUE)` reads the stream first
while a statement of the connection reads, before it drops the table.
Savepoint names are numbered rather than random,
the methods check that nanoarrow is installed,
and rlang 1.0.0 is required.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
… original error

The fallback for types that the Arrow bind can't bind fails for a uint64 value
beyond the range of int64 with the bind path's error, naming parameter and row,
instead of writing the wrapped integer64.
A failed rollback is attached to the original error
as `rollback_error` and a line of its message
rather than raised as a warning, which `options(warn = 2)` made the error.
The cleanup is registered before the SAVEPOINT,
the fallback releases its stream also on failure,
and savepoint ids are formatted without scientific notation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
The fallback of `dbAppendTableArrow()` checks uint64 columns for values
beyond INT64_MAX on the array itself, in C++ (`arrow_uint64_beyond_int64()`),
before nanoarrow converts them to integer64, where 2^63 became NA
and looked like a NULL.
The fallback converts each column on its own, sliced as the struct array is,
since nanoarrow 0.9.0 ignores the offset of a struct array when it converts it.
A failed rollback is attached to an rlang error as a bullet of its body,
which its message is formatted from.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: one more review round for the last fix in this pull request?

The third and last review round of this pull request found a High in the fallback that writes Arrow streams with decimal columns through data frames: a uint64 value of exactly 2^63 wrapped to the 64-bit integer that R's integer64 uses for NA, so the range check missed it and the value was stored as NULL with only an R warning. The fix is implemented: the check now runs on the Arrow array itself before any conversion (check_uint64_params(), calling arrow_uint64_beyond_int64()), so every value above the largest signed 64-bit integer is refused with the same error as the regular path, while NULLs pass. The same commit makes the fallback convert sliced struct columns one by one, because nanoarrow 0.9.0 ignores the struct's offset, and shows a failed rollback as a bullet of an rlang error. Tests cover 2^63, 2^63 + 4096, the largest unsigned value, NULLs in the same chunk and sliced input; the full suite and R CMD check pass. Your rules allow three review rounds per change and send a High that survives the third to you, so this fix has not been reviewed.

  • Run one more clean review round, limited to this commit (recommended: it adds C++ that reads Arrow buffers, and a second fix in the same commit)
  • Accept the fix without another review

Kirill decides. Until then no further round runs, and the pull request stays a draft.


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