Skip to content

chore: Plan a native implementation of the DBI Arrow interface - #596

Draft
krlmlr wants to merge 1 commit into
mainfrom
claude/arrow-native-plan
Draft

krlmlr wants to merge 1 commit into
mainfrom
claude/arrow-native-plan

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

This pull request carries no code, only an empty commit. It holds the plan for implementing the DBI Arrow interface natively in RMariaDB, following RSQLite's open stack (r-dbi/RSQLite#880, from #850 to #867), and the questions about it, each posted as its own comment.

State on 2026-11-29

How RMariaDB differs from RSQLite

  • MariaDB's result columns have static types, so the schema is known after prepare. RSQLite's per-chunk typing rules (#859 to #861) shrink to a single static mapping. The types must still be read again after execute, because a ? placeholder has no type at prepare time.
  • RMariaDB has no read-ahead, so #850 and the kept errors of #830 don't apply.
  • A connection has only one current result, and a new query closes the previous one. A stream that is to outlive dbClearResult() or the next query (#865) therefore needs to be detached: mysql_stmt_store_result() buffers the remaining rows, and mysql_close() is deferred until the last stream ends.
  • The MySQL client library is not thread-safe per connection, so the check for another thread comes with the stream PR. RSQLite defers it to a later PR.
  • Binds can read Arrow buffers directly, and unsigned integer types are supported natively. The length limit for a bound value is the server's max_allowed_packet.
  • nanoarrow is vendored as in build: Vendor the nanoarrow C library RSQLite#857, under the namespace RMariaDB. src/Makevars is generated from src/Makevars.in by configure, and src/Makevars.win exists separately, so the build rule goes into both. A simpler option is to put nanoarrow.c and nanoarrow.h directly in src/.

Proposed stack (bottom to top)

Group A, no behavior change:

  1. refactor: Bind list parameters through a parameter source (like refactor: Bind list parameters through a parameter source RSQLite#855)
  2. refactor: Own results through a shared pointer, with a virtual destructor (like #856)
  3. build: Vendor the nanoarrow C library (like #857)

Group B, internal engine with unexported entry points, built on #587's chunk store:

  1. feat: Build Arrow arrays from the columns of a result (like #859 and #860 combined, since the typing is static)
  2. feat: Build Arrow date, time and timestamp arrays (like #861; see the questions on time zones and TIME)
  3. feat: Report values an Arrow column can't hold once per chunk (like #862, reduced)
  4. feat: End Arrow chunks before string and binary offsets overflow (like #863)
  5. feat: Read a result as a lazy Arrow array stream, including the thread check (like #864)
  6. feat: Let Arrow streams outlive dbClearResult(), another query and dbDisconnect() (like #865, with the detach)
  7. feat: Bind the rows of an Arrow stream to a statement (like #866). A test must show that binding an INSERT to a stream that reads the same table ends, which is RSQLite's open High.
  8. feat: Check dictionary indices, offsets, lengths and self-binding of Arrow parameters (like #867)

Group C, DBI methods (RSQLite's plan PRs 16 and 17, not started there):

  1. feat: Implement dbSendQueryArrow(), dbFetchArrow(), dbFetchArrowChunk() and dbGetQueryArrow() natively
  2. feat: Implement dbBindArrow(), dbAppendTableArrow(), dbCreateTableArrow() and dbWriteTableArrow() natively
  3. Optional, later: requested types through schema, and bulk binding with MariaDB Connector/C's STMT_ATTR_ARRAY_SIZE.

Nothing in groups A to C starts before the questions below are answered and #587's chunk store is pushed.

Open questions

Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: How far to follow RSQLite's native Arrow stack

RSQLite's native stack (r-dbi/RSQLite#880) is unmerged: from #830 up its CI is red on five expectations, a High finding on #866 is open, and its DBI methods (plan PRs 16 and 17) haven't started. #584 already gives RMariaDB the full Arrow interface through DBI's default methods, which go through data frames. The native stack proposed in this PR is about 13 PRs of C++ on top of #587's chunk store.

If nobody decides, I start group A only.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: The time zone of Arrow timestamps

RMariaDB's data frames return DATETIME and TIMESTAMP values as wall-clock times in the connection's time zone (dbConnect(timezone = , timezone_out = )). An Arrow timestamp is either an instant with a time zone or a naive time without one. RSQLite's #861 always uses an instant in UTC. This decision affects layer 5 of the plan.

  • Use an Arrow timestamp[us] with the time zone timezone_out (by default the connection's time zone), converted to an instant by the same rule dbFetch() uses, so that converting to R gives the same POSIXct values as dbFetch() (recommended)
  • Always use UTC instants, as RSQLite does; this is simplest, but displays differ from dbFetch() when the connection's time zone isn't UTC
  • Use naive timestamps (no time zone) for DATETIME and UTC instants for TIMESTAMP; this is closest to SQL semantics, but different from dbFetch()

If nobody decides, layer 5 uses the first option.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: The Arrow type of TIME columns

MariaDB's TIME is an interval from -838:59:59 to 838:59:59, not a time of day. Arrow's time64 is defined for times within a day, while duration has no such limit. dbFetch() returns hms, which nanoarrow converts to and from both. This decision affects layer 5 of the plan.

  • duration[us]: every value fits, and it converts to a difftime/hms in R (recommended)
  • time64[us], as RSQLite's #861 does; values outside 0 to 24 hours would be reported once per chunk (layer 6) and become null
  • time64[us] when every value of the chunk fits in a day, and duration[us] otherwise; a column's type would then depend on its data

If nobody decides, layer 5 uses duration[us].


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.

dbCreateTable() with a zero-row data frame makes character columns VARCHAR(1)

2 participants