Skip to content

chore: Coordinate native Arrow support - #631

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

krlmlr wants to merge 1 commit into
mainfrom
claude/arrow

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

This PR records the state and the open questions of the work on native Arrow support in RPostgres. It carries no code. The implementation lands in separate PRs, linked below as they open.

Kirill's request (relayed): "Review the Arrow work in RSQLite PRs, implement in a similar fashion in this package. When implementing, file all problems discovered as issues and fix them in separate PRs."

What RSQLite does

RSQLite has not shipped native Arrow on main yet. The old all-in-one draft is r-dbi/RSQLite#795. The new stack, coordinated in r-dbi/RSQLite#880, builds Arrow arrays in C++ inside the result class, using vendored nanoarrow C (#857). It hands them to R as a lazy stream that holds its own share of the result (#856, #864, #865), and binds Arrow streams through a parameter source (#855, #866). Much of that stack deals with SQLite's dynamic typing (#862, #867). RPostgres does not need that part, because libpq fixes each column's type by OID. The step that exposes the DBI methods is still open in RSQLite.

Plan for RPostgres

One concern per PR, smallest first:

Currently, RPostgres relies on DBI's default Arrow methods, which go through data frames. Problems found in that behavior become issues with their own fix PRs.

Open questions

Constraints

Related PRs and issues

PRs for the plan, all after clean review:

Problems in the current behavior, found by probing DBI's default Arrow methods against PostgreSQL 16 ("native" means native support is expected to fix it by construction):

Build problems found on the way:

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

https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Where are the Arrow arrays built?

RSQLite (r-dbi/RSQLite#859 and the stack in r-dbi/RSQLite#880) builds the Arrow arrays in C++, inside its result class. It appends each value straight into vendored nanoarrow arrays through a shared fetch_loop(). In RPostgres, the matching place is PqResultImpl::fetch_rows() in src/PqResultImpl.cpp, whose step(), complete_ and nrows_ are private. The parallel ALTREP session is reworking the same function.

  • (recommended) Add a small row-sink seam to PqResultImpl::fetch_rows(), about 20–40 lines, agreed with the ALTREP session so that both use it. The Arrow builders go into new files (src/PqArrowColumn.*, src/PqArrowStream.*), which parse the libpq text values with the parsers already in PqColumnDataSource.cpp.
  • A separate Arrow result class with its own libpq loop. It needs no change to the fetch path, but duplicates binding, stepping and interrupt handling.
  • Fetch through COPY (query) TO STDOUT (FORMAT binary), as the ADBC PostgreSQL driver does. It is fast, but it does not work with bound parameters and is much more new code.

If nobody decides, the work proceeds with the first option. I'll ask the coordinator to settle the seam with the ALTREP session before touching PqResultImpl.cpp.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: How long does an Arrow stream live?

RSQLite (r-dbi/RSQLite#864, #865) returns a lazy stream that holds its own share of the result, so it outlives dbClearResult() and dbDisconnect(). That relies on SQLite allowing several active statements per connection. A libpq connection has only one active result, so a lazy stream from dbGetQueryArrow() cannot keep reading once the next query runs on the same connection.

  • (recommended) dbGetQueryArrow() reads all chunks into memory before it clears the result, as DBI's default method does today. dbSendQueryArrow() with dbFetchArrowChunk() stays the lazy, chunked path, and dbFetchArrow() returns a stream over the chunks still to come, valid while the result is open. A stream read after dbClearResult() fails with a clear error.
  • A lazy stream that buffers the rest of the result when another query starts or the result is cleared. Memory use then depends on what the user does next.
  • A lazy stream that fails once another query runs on the connection.

If nobody decides, the work proceeds with the first option, and this follows the lazy-fetch work in DBI where it changes the generics' contract.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Which Arrow types do int8, numeric and text map to?

Because PostgreSQL fixes the type of each column by OID, RPostgres needs none of the type-deciding and coercion-reporting code from RSQLite (r-dbi/RSQLite#862, #867). Three mappings still need a decision:

  • (recommended) int8 becomes Arrow int64 regardless of the connection's bigint setting, which keeps affecting data frames only. numeric becomes double, as dbFetch() returns it, with decimal128 left for later. Text and bytea become utf8 and binary, and a chunk ends early before its 32-bit offsets would overflow, as in feat: End Arrow chunks before string and binary offsets overflow RSQLite#863.
  • As above, but int8 follows bigint (integer → int32, numeric → double, character → utf8).
  • As the first option, but text and bytea become large_utf8 and large_binary, so no chunk needs to end early. Some consumers handle the large types less well.

If nobody decides, the work proceeds with the first option.


Generated by Claude Code

This was referenced Nov 29, 2026

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: How should the native Arrow PRs be stacked?

The next PR builds Arrow arrays from result columns as a second row sink. It needs both the row-sink seam in #638 and the vendored nanoarrow in #635. These two PRs are independent of each other, and both are based on main. A GitHub stack is linear, so the new PR can't sit on both. The ALTREP work (#632) also plans to build on #638.

If nobody decides, the native Arrow implementation waits for the first option. The fix PRs listed in the body are independent of it.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Handover

This session is being archived. Here is the state as of 2026-11-29 19:30 UTC.

Merged: #634, #647, #656, #658, #660, #662 and #665. #659 was closed: the recommendation is to cast money in queries (#654).

Open and ready for review:

On #635 and #638 the aggregate rcc commit status still reads "pending" although every job that ran has passed. The full-matrix jobs are skipped on pull requests.

The native Arrow implementation (builders, stream, binding and COPY-based writing) has not started. It waits for #635 and #638 to merge and for the four open questions above: stacking, where the arrays are built, stream lifetime and type mapping. A fresh session can pick it up from this PR's body. Two sibling sessions build on #638's interface and have agreed to it:

Issues filed without a fix yet: #639–#646, #649–#653. Most should be fixed by construction with native Arrow. #652 is a nanoarrow problem and #653 a DBItest problem; neither is reported upstream.

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


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.

Arrow generics from DBI are not reexported

2 participants