Skip to content

fix: dbFetch() no longer wraps integers stored as text beyond 32 bits - #958

Draft
krlmlr wants to merge 3 commits into
mainfrom
claude/fix-957-int64-text
Draft

krlmlr wants to merge 3 commits into
mainfrom
claude/fix-957-int64-text

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Closes #957.

When a column's first value is an integer, dbFetch() coerces later values of another type into it. Text (or a blob) was read with the 32-bit sqlite3_column_int(), which keeps only the low 32 bits of the 64-bit integer SQLite converts the text to, so '5000000000' came back as 705032704 and '9223372036854775808' as -1, whatever bigint said.

Now such a value is checked with sqlite3_column_int64() first: if it needs more than 32 bits, the column turns into integer64 through the same storage spillover a stored 64-bit integer uses, and bigint then applies as usual. The value is the one CAST(x AS INTEGER) gives, including SQLite's saturation beyond 64 bits: '9223372036854775808' reads as 9223372036854775807. At the low end, '-9223372036854775809' saturates to the smallest 64-bit integer, which integer64 reads as NA, the same as for that integer stored as such. Text within the 32-bit range keeps the column integer, and the 64-bit value read for the check is stored directly, so the text is converted only once: reading 2,000,000 text values into an integer column takes about 0.27 s, against 0.33 to 0.36 s with a second conversion and 0.34 s on main in the same runs. The usual "mixed type" warning still names the coerced type. I chose the promotion over NA with a warning because it is a contained change in DbColumnStorage::append_data() and matches how a stored 64-bit integer is handled; the 32-bit range check moves from SqliteColumnDataSource to DbColumnDataSource so the storage can use it.

The range check now also counts -2147483648 as needing 64 bits: it is NA_integer_ in R, so SELECT -2147483648 AS x and the text '-2147483648' in an integer column used to come back as NA silently, on main too. Such a column now turns into integer64, or as bigint asks, like any other value beyond the range of an R integer.

Tested with a new test in tests/testthat/test-basic-types.R, next to the mixed-type tests, covering positive and negative values beyond 32 bits, saturation at both ends, bigint = "numeric", and text within range. It fails on main (the column comes back as integer 1, 705032704, -705032704, -1, 0, 12) and passes with the fix. A second test covers -2147483648 stored as an integer, alone and after another integer, and as text, and checks that -2147483647 stays integer; it fails before the fix (NA in an integer column) and passes with it. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (unavailable locale), SKIP 63, PASS 10717.

This PR is independent of the Arrow chain planned in #880 and is based on main. The chain's R5 layer (#952) refactors nearby code in src/SqliteColumnDataSource.cpp; a trial merge into each layer of the chain merges the sources cleanly and conflicts only in NEWS.md, so the later merge into the chain needs a small resolution there.

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


Generated by Claude Code

A value of another type, such as text, in an integer column was read
with the 32-bit `sqlite3_column_int()`, keeping the low 32 bits
of the 64-bit integer SQLite converts it to.
Such a value now turns the column into integer64,
as a stored 64-bit integer does,
and reads the value `CAST(x AS INTEGER)` gives, saturated beyond 64 bits.
The range check moves to `DbColumnDataSource` for the column storage to use.

Closes #957.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
Text in an integer column was converted to a 64-bit integer to check its range,
and then once more by `fetch_int()` to store it.
The value from the first conversion is now stored directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
The smallest 32-bit integer is `NA_integer_` in R,
so it no longer counts as fitting in an R integer.
A column holding it, as an integer or as text, turns into integer64 or as `bigint` asks.

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.

dbFetch() wraps text beyond the 32-bit range to a wrong integer in an integer column

2 participants