Skip to content

feat: Request R types for result columns with ptype - #802

Closed
krlmlr wants to merge 19 commits into
claude/elegant-tesla-mja103-schemafrom
claude/elegant-tesla-mja103-ptype
Closed

krlmlr wants to merge 19 commits into
claude/elegant-tesla-mja103-schemafrom
claude/elegant-tesla-mja103-ptype

Conversation

@krlmlr

@krlmlr krlmlr commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

Stacked on #801 (base branch claude/elegant-tesla-mja103-schema), which is stacked on #800, #796 and #795.

dbSendQuery(), dbGetQuery() and dbReadTable() gain a ptype argument for connections opened with arrow = TRUE, the data frame counterpart of the schema argument of #801:

con <- dbConnect(RSQLite::SQLite(), arrow = TRUE)
dbGetQuery(con, "SELECT * FROM t", ptype = list(id = bit64::integer64(), when = as.POSIXct(character(), tz = "UTC")))
  • ptype is a data frame or a named list of vectors whose values are ignored, so a zero-row data frame and a list of zero-length vectors describe the same thing. Entries are matched to the result columns by name, exactly: a name not in the result, one the result carries twice, an unnamed entry, a duplicate name, a data frame column, a raw vector (the message points to blob::blob()), and a nanoarrow schema (the message points to lists and data frames) are errors raised when the query is sent, with no result left open. Without arrow = TRUE the argument is an error too.
  • Each named column is requested from SQLite as the Arrow type nanoarrow::infer_nanoarrow_schema() gives for the prototype, through the machinery of feat: Request Arrow types for result columns with schema #801, and converted back to the prototype's type: integer as int32 (values outside the range become NA with the warning of feat: Report coerced values once per chunk or dbFetch(), with their rows #800), integer64 as int64 and it stays an integer64 whatever the values and whatever bigint says, logical as bool, Date as date32, blob as binary. POSIXct (and POSIXlt) and hms are requested as timestamp[us] in the prototype's time zone and time64[us], which fixes their type, but data frame fetches read them like the times and timestamps decided from extended_types: as doubles in seconds with the default path's precision and range (fractions below a microsecond, times outside a day), whatever the declared type, with the prototype's class and time zone (the session's for an empty one); an Arrow stream of the result has them in microseconds. A factor is read as text and converted to its levels, values outside them becoming NA as with factor(), or to the levels found in the values of each fetch when it has none; an ordered factor stays ordered. A prototype nanoarrow finds no Arrow type for (a complex vector, NULL), one of a type RSQLite cannot fill (difftime, which nanoarrow maps to duration), and one nanoarrow cannot convert back to (an AsIs vector, a list) are rejected with the column name before the query is sent, so that an open result is left alone.
  • A column with a prototype keeps its type in every fetch, whatever the values. The other columns are typed for each fetch like on the default path (feat: Add arrow argument to dbConnect() to route data frames through Arrow #796), and bigint applies only to them. The prototype is stored on the result, so it holds across chunked fetches including n = 0, across dbBind() and dbBindArrow(), for empty results, and for the rows kept when an Arrow schema was requested first.
  • The error messages of the shared schema request now name the argument they check (schema or ptype), and result_set_arrow_schema() takes a logical seconds per requested column for the times and timestamps that data frame fetches store in seconds.

Tests in test-arrow-ptype.R cover the type table, data frame and list prototypes, matching, validation, chunking, binding, empty results, rows kept for a schema, coercion, bigint, factors and dbReadTable(), compare the columns without a prototype with the default path over sequences of dbFetch(n), and compare POSIXct and hms prototypes with the default path for fractions below a microsecond and times beyond a day. The Arrow tests (with and without the arrow package), DBItest and DBItest-arrow pass on the branch.

Review

A clean-context review (2026-09-27) had no findings of its own; it noted that finding 1 of the review on #796 (a small first fetch fixing NULL-first columns) applied to ptype fetches too, which holds after merging the reworked layers below and is tested. Problems found while checking every promise above against the merged code are fixed (an ordered prototype without levels came back unordered; prototypes that cannot be requested or converted back failed late or without naming the column; POSIXct and hms prototypes lost precision below a microsecond and times outside a day). #801 is merged up to 4614fa2; the remaining limitations will be listed in an "Accepted limitations" comment after a final clean review of the whole stack.

Since the integration review: dbSendQueryArrow() and dbGetQueryArrow() refuse ptype together with schema (9f385a3), and with arrow = TRUE, dbColumnInfo() reports the class each prototype produces, as dbFetch(rs, n = 0) does (96cc328); a blob or integer64 prototype is named by its class, while the same types without a prototype keep the default path's names (list, double).

What remains is listed under Accepted limitations.

https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE


Generated by Claude Code

krlmlr and others added 2 commits September 26, 2026 11:47
`dbSendQuery()`, `dbGetQuery()` and `dbReadTable()` gain a `ptype` argument
for connections opened with `arrow = TRUE`:
a data frame, or a named list of vectors whose values are ignored,
matched to the result columns by name, exactly.
Each named column is requested from SQLite as the Arrow type
that nanoarrow infers for the prototype and converted back to the prototype's type,
so its type depends neither on the values, nor on the chunk size, nor on `bigint`.
Factors are read as text and converted to their levels,
`hms` columns are requested as `time64`, and `POSIXlt` stands for `POSIXct`.
The other columns are converted as before.

The prototype is validated when the query is sent, with no result left open on an error,
and it outlives `dbBind()`.
The error messages of the shared schema request name the argument they check.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EpBkzNMeuMBGy4nxTafYH6

krlmlr commented Sep 27, 2026

Copy link
Copy Markdown
Member Author

Review of this PR at 90a302d, by a Claude Code reviewer with clean context that read the diff against claude/elegant-tesla-mja103-schema and ran targeted R snippets against a build of the whole stack. No findings of its own; finding 1 of the review on #796 (a small first fetch freezes NULL-first columns) applies to ptype fetches too, and a ptype entry for such a column is the workaround until it is fixed.

Checked and found sound: the prototype is validated and turned into an Arrow schema before dbBind(), stored on the result so that it holds across chunked fetches, rebinding and empty results, the factor levels come from the values of each fetch when none are given, bigint applies only to columns without a prototype, and a validation error leaves no result open. dbReadTable(con, Id(...), ptype = ) reaches the character method through DBI's delegation. test-arrow-ptype.R passes on the build of the whole stack.


Generated by Claude Code

…-tesla-mja103-ptype

Data frame fetches through Arrow now go through `result_fetch_arrow_df()`, which types each fetch like the default path and reports which columns needed 64-bit integers.
`arrow_fetch_df()` reads the prototype from the result and passes it to `arrow_to_df()`, which converts the stream with the prototype's types.
`arrow_df_int64()` takes both `wide_ints` and the columns to keep, so that a column with a prototype is neither narrowed to `integer` nor touched by `bigint`.
The documentation of `ptype` moves next to the new rules for typing data frame fetches.
The dependency files of #801 replace the ones with system headers that the auto-update had committed.

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

The columns without a prototype are compared with the default path over sequences of `dbFetch(n)` that start with a NULL row, including `n = 0`.
The columns with a prototype keep their types in each fetch, in the rows kept for a schema, and in empty fetches.
An `integer64` prototype stays `integer64` for every `bigint` setting, whether the values fit an `integer` or not.

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

A factor prototype without levels takes the levels found in the values of each fetch, and an ordered one lost its class on the way.
The documentation now says that values outside the levels of a prototype become `NA`, as with `factor()`.
The tests cover logical prototypes and factors with and without levels, ordered or not, across fetches.

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

A prototype that nanoarrow finds no Arrow type for, such as a complex vector or `NULL`, failed with nanoarrow's message, which does not say which column it was.
A prototype that nanoarrow infers a type for but can't convert back to, such as an `AsIs` vector, failed only in `dbFetch()`, after the rows were read.
Both are now errors that name the column, raised before the query is sent, so that an open result is left alone.
The documentation lists these errors and says that a `POSIXlt` prototype stands for `POSIXct`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…-tesla-mja103-ptype

`arrow_to_df()` now converts the list that `result_fetch_arrow_df()` returns, with the formats, names and arrays of a fetch.
It takes the prototype as its third argument, puts its columns into the conversion target by position, and turns the factors without levels into factors after the conversion.
The columns of the prototype stay out of the narrowing of 64-bit integers and out of `bigint`, which `arrow_df_int64()` used to do and `arrow_to_df()` now does itself.
The coercion warning stays where #801 raises it, after the conversion in `arrow_fetch_df()`.

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

A `ptype` entry for `hms` or `POSIXct` requested `time64[us]` or `timestamp[us]`, which a data frame fetch built in microseconds and limited to a time of day.
Such entries now mark their requested type with `seconds`, which `result_set_arrow_schema()` passes to the column: the type stays fixed, but a data frame fetch stores the values as doubles in seconds, like a time or timestamp decided from `extended_types`.
`arrow_to_df()` then gives these columns the attributes of the prototype, its class and time zone.
Times outside a day and fractions below a microsecond now come back as on the default path, also without declared types, across chunked fetches and across arrays.
An Arrow stream of such a result still has `time64[us]` and `timestamp[us]` in the prototype's time zone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
krlmlr pushed a commit that referenced this pull request Nov 10, 2026
Measured on top of #802, converting every fetch of several arrays column by column took as long as the conversion of the layers below and peaked higher: 11 MB more for all five columns of five million rows and 89 MB more for the two text columns.
`nanoarrow::convert_array_stream()` keeps each array until the next garbage collection, where the layers below release it as soon as it is copied.
A fetch without a lazy column now takes the path of the layers below unchanged.
Only a fetch with a column that stays a view splits its arrays by column, in `arrow_lazy_to_df()`, so that the view holds the strings of its own column alone.
The data frames section of `?sqlite-arrow` describes the eager path as before and the split for lazy strings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
krlmlr pushed a commit that referenced this pull request Nov 10, 2026
The benchmark ran again on #802's head, on the merge that converted every fetch of several arrays column by column, and on the commit that keeps that conversion for lazy strings only.
The harness now reads the peak RSS before `object.size()`, which allocates a hash table for the strings of a column and materializes a lazy one: it added up to 170 MB to the peaks of the cells with text.
The README replaces the claim that the column-by-column conversion made the eager path faster and leaner, which no longer holds on the reworked layers, and updates the numbers for `lazy_strings`.
A full fetch of five million rows takes 3.6 s with lazy strings, 9.4 s eager and 16.4 s on the default path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
krlmlr pushed a commit that referenced this pull request Nov 10, 2026
…tesla-mja103-lazy

#802 now carries the final layers below it: a data frame fetch hands R a list of struct arrays with their formats, the columns of times in seconds, and the coercion warning, which `arrow_fetch_df()` raises after the conversion.
`arrow_to_df()` takes `lazy_strings` from the connection as a fourth argument and marks the text columns without a prototype or with a plain `character()` one; factors without levels stay out, since they are built from copies.
A fetch with such a column and at least one array converts its columns in `arrow_lazy_columns()`, which replaces only the conversion step: factors, times in seconds, the prototypes' attributes, the narrowing of integers and `bigint` follow as for the other fetches.
`arrow_split_columns()` now splits the list of arrays instead of a stream.
Eager fetches and fetches of no rows take the path of the layers below unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
`ptype` reaches `dbSendQuery()` through the dots of `dbSendQueryArrow()`, which stored it on the result before the `schema` replaced its types.
`dbFetch()` then converted the columns of the schema to the prototype, and failed or gave the wrong type.
The combination is now an error raised before the query is sent, also for `dbGetQueryArrow()`, and the documentation of `ptype` says so.
Each argument alone works as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…-tesla-mja103-ptype

Brings in `dbColumnInfo()` answered from the Arrow columns, `POSIXlt` written like `POSIXct`,
the CI hook for the large Arrow test, and the comparison helper that can ask `dbColumnInfo()` before each fetch.

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

krlmlr commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

Superseded: Accepted limitations

Superseded by this comment.

One clean review of this pull request, at 90a302d, had no findings of its own, and the problems found while checking the description against the merged layers below are fixed up to 04fa3fc (b13570d, 844b42f, 04fa3fc). The integration review of the whole stack at 071560ec found two issues in this layer: dbColumnInfo() ignores ptype (the ptype part of its Medium finding 1), and dbSendQueryArrow() accepts ptype together with schema (Low finding 6). The combination with schema is refused since 9f385a3; ptype in dbColumnInfo() follows once the fix of dbColumnInfo() in #796 is merged up, and this list will be posted again after it. These remain:

  • R/arrow.R#L209: a value outside the levels of a factor prototype becomes NA without a warning, as with factor(): with ptype = list(f = factor(levels = c("a", "b"))), the values 'c' and 1 come back as NA silently. Relevance: medium; reviewer's confidence: high. (recorded)
  • R/arrow.R#L210: a factor prototype without levels takes the levels found in each fetch, so two fetches of one result can carry different levels (x, y, then x, z). Relevance: low; reviewer's confidence: high. (recorded)
  • R/arrow.R#L211: a difftime prototype cannot be requested, since nanoarrow maps it to duration, which RSQLite does not fill; the error names the column, and an hms prototype is the alternative. Relevance: low; reviewer's confidence: high. (recorded)
  • R/dbSendQuery_SQLiteConnection_character.R#L6: ptype works only on connections with arrow = TRUE; on the default path, dbSendQuery(), dbGetQuery() and dbReadTable() refuse it with an error. Relevance: low; reviewer's confidence: high. (recorded)

Generated by Claude Code

…fo()`

With `arrow = TRUE`, `dbColumnInfo()` named every column after its Arrow format, as the default path names its types.
For a column of `ptype` that was wrong or failed: an `integer` prototype, requested as `int32`, failed with an internal error, a factor was named `character`, and an `integer64` was named `integer`.
These columns are now named after their prototype, as `dbFetch()` returns them: the class of an object such as a factor, a `Date` or an `integer64`, and the storage type of the others.
The columns without a prototype are named as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…-tesla-mja103-ptype

Brings in the busy mark reset through `R_ExecWithCleanup()`, the checks of Arrow parameters,
the 64-bit row counter, the UTF-16 text in binary columns,
and `dbColumnInfo()` for the formats requested with `schema`.

`set_arrow_schema()` runs through `run_guarded()` and passes the seconds of `ptype` on.
`arrow_column_info()` names the columns of `ptype` after their prototype with `arrow_type_name()`,
and the other columns of a requested format with `arrow_column_type_name()`, so both helpers stay.
`src/cpp11.cpp` is regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…-tesla-mja103-ptype

Brings in the bytes of a blob in a `utf8` column kept as they are, also in a UTF-16 database.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…-tesla-mja103-ptype

Brings in the state of the Arrow test hooks kept in each result,
with the helpers `set_bytes_limits()` and `fetch_with_bytes_limits()` in `helper-arrow.R`.
`src/cpp11.cpp` is regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…-tesla-mja103-ptype

Brings in Arrow streams read safely on worker threads, with the "Threads" section of `?"sqlite-arrow"`,
the coercion warning reported as the stream's error off R's main thread,
the scanner tests, the memory cleanup of the large tests with `helper-memory.R`, and the longjmp tests.
The requested R types add no R call to the stream callbacks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd3gjmufoKCWVDBPCgE
…-tesla-mja103-ptype

Brings in the atomic busy mark that records the thread holding a result, the atomic row count and completion,
the parked results closed from the main-thread entry points, NaN tested without R,
and the tests that read streams on a thread of their own.
`result_set_arrow_schema()` drops the parked results before it passes the seconds of `ptype` on.

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

krlmlr commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

One clean review of this pull request, at 90a302d, the problems found while checking the description against the merged layers below, and both integration reviews of the stack (first integration review: finding 6 fixed in 9f385a3, the ptype part of finding 1 in 96cc328; second integration review, which found nothing new in this layer) are addressed up to af97a3c, which closes the review rounds for the stack. Every High and Medium finding is fixed. These remain:

  • R/arrow.R#L209: a value outside the levels of a factor prototype becomes NA without a warning, as with factor(): with ptype = list(f = factor(levels = c("a", "b"))), the values 'c' and 1 come back as NA silently. Relevance: medium; reviewer's confidence: high. (recorded)
  • R/arrow.R#L210: a factor prototype without levels takes the levels found in each fetch, so two fetches of one result can carry different levels (x, y, then x, z). Relevance: low; reviewer's confidence: high. (recorded)
  • R/arrow.R#L211: a difftime prototype cannot be requested, since nanoarrow maps it to duration, which RSQLite does not fill; the error names the column, and an hms prototype is the alternative. Relevance: low; reviewer's confidence: high. (recorded)
  • R/dbSendQuery_SQLiteConnection_character.R#L6: ptype works only on connections with arrow = TRUE; on the default path, dbSendQuery(), dbGetQuery() and dbReadTable() refuse it with an error. Relevance: low; reviewer's confidence: high. (recorded)
  • R/arrow-df.R#L289-L294: with arrow = TRUE, dbColumnInfo() names a column with a prototype by the class the prototype produces, so a blob prototype reads blob and an integer64 prototype integer64, while the same output without a prototype keeps the default path's names, list and double. Relevance: low; confidence: high.

Generated by Claude Code

…-tesla-mja103-ptype

Brings in the Arrow test fixtures moved into `helper-arrow.R`,
with the wrappers of the tests of `schema` renamed to `validated_dbGetQueryArrow()`, `validated_dbReadTableArrow()` and `validated_dbFetchArrowChunk()`.

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

krlmlr commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Marked as a draft, as decided: this pull request is the reference for the reworked Arrow stack, which carries its changes in smaller, separately reviewed pull requests (see the plan). So far: #855, #856, #857, #859 to #867 (GitHub stack 851 on #830 and #850), and #852, #853, #854 and #858 from main. It will be closed once its replacements are merged.


Generated by Claude Code

krlmlr commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Closed as superseded, as decided: the Arrow work is rebuilt from main as a chain of smaller pull requests, planned on #880. The data-frame path through Arrow that this stack carries (group D) is tracked in #881. The branch stays as a reference.


Generated by Claude Code

@krlmlr krlmlr closed this Nov 30, 2026
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