Skip to content

feat: Fetch unsigned integers exactly and apply bigint from the stored values - #634

Draft
krlmlr wants to merge 9 commits into
claude/altrep-fetchfrom
claude/altrep-fetch-l2
Draft

krlmlr wants to merge 9 commits into
claude/altrep-fetchfrom
claude/altrep-fetch-l2

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Layer 2 of the ALTREP fetch design, stacked on #587 (layer 1). Closes #412, closes #593, supersedes #413.

What changes

  • Unsigned integers: each output bind takes is_unsigned from UNSIGNED_FLAG; before, every bind set it to true.

    • TINYINT, SMALLINT and MEDIUMINT UNSIGNED and YEAR come back as integer.
    • INT UNSIGNED comes back as an exact double: 4294967295 and 2147483648 are now returned as such, not as -1 and NA.
  • BIGINT UNSIGNED under the default bigint = "integer64" is a double ALTREP column with class c("mariadb_uint64", "numeric"). The double view rounds above 2^53. as.character(), format() and print() show exact values from storage. The bit64::as.integer64() method is exact, and gives NA with a warning at 2^63 and above. The exact values survive:

    • Subsets and duplicates (x[i], [.mariadb_uint64) hold a copy of only the values they select, so they do not pin the chunk.
    • Operations that request a writable data pointer (c(), range(), cumsum(), ifelse() and others) leave x exact.
    • A modified copy shows its new values. Exact stored values are used only where the double view still matches them.
    • With an integer64 operand, Ops converts exactly and returns integer64. For i64 + x, bit64's own method still runs, and the methods treat that mixed-class result as integer64.

    The class is documented as experimental in ?mariadb_uint64, which has a "Type mapping" section.

  • Signed BIGINT, as decided in Q1:

    • With bigint = "integer64", the default, it is built eagerly as plain integer64, identical to main.
    • With "integer", "numeric" and "character", it is an ALTREP column that converts from the stored int64 on access, following the DBI spec (kept): "integer" gives a silent NA on overflow, "numeric" rounds silently, and "character" is exact.
    • BIGINT UNSIGNED follows the same rules for these three values.
    • convert_bigint() is removed.
  • bigint now reaches C++, so dbColumnInfo() (types integer64, mariadb_uint64, integer, double, string) and dbFetch(n = 0) respect it.

Behavior changes to note

  • The server types some expressions as BIGINT UNSIGNED, for example LAST_INSERT_ID() and bit operations like 3 & 1. Under the default bigint, these now return mariadb_uint64 instead of integer64, and CONNECTION_ID() and CRC32() return double instead of integer. See the question below.
  • unique(), table(), x[[i]] and saveRDS() work on the rounded doubles, so 2^53 and 2^53 + 1 compare equal there. This is documented.

Testing

  • The new tests/testthat/test-fetch-integers.R covers:
    • every unsigned type at its boundaries (2^31, 2^32 − 1, 2^53, 2^63, 2^64 − 1) and NULLs;
    • every bigint value for BIGINT and BIGINT UNSIGNED;
    • exactness of uint64 subsetting, as.character() and as.integer64();
    • dbColumnInfo(), and chunked fetch with n = 2.
  • The full suite passes locally against MariaDB 10.11 on a clean build, DBItest included, with the same 10 skips as the base branch.
  • A short gctorture() run was clean.
  • Clean review: round 1 found one High (a writable data pointer dropped the exact storage) and three Mediums (mixed class with integer64, a subset pinning the whole chunk, undocumented type changes), all fixed. Round 2 is running.
  • Merged with layer 1, which includes main up to fix: Results of a closed connection fail cleanly instead of crashing #624. DbResult.cpp keeps fix: Results of a closed connection fail cleanly instead of crashing #624's check_connection() and adds the bigint argument.

mariadb_storage() is left for layer 3.

https://claude.ai/code/session_01Hr1rd7atAZVq1J42WzXJpS


Generated by Claude Code

…red values

Output bindings now take `is_unsigned` from the column's `UNSIGNED_FLAG`, instead of setting it for every column.
Unsigned TINYINT, SMALLINT, MEDIUMINT and YEAR stay integer.
INT UNSIGNED is fetched as an exact double, so 4294967295 and 2147483648 no longer come back as NA or negative (#132, #412).

BIGINT UNSIGNED with `bigint = "integer64"` is a double with class `c("mariadb_uint64", "numeric")` (#593).
Its `as.character()`, `format()`, `print()` and `bit64::as.integer64()` methods read the exact values from the fetched chunk.
`as.integer64()` gives NA with a warning for values of 2^63 and above.
Subsets and duplicates of such a column are new ALTREP columns that index into the same chunk, so `x[2:3]` stays exact.
A writable data pointer detaches the column from its storage, and `Ops` work on unclassed copies so that comparisons do not detach the original.

Signed BIGINT with `bigint = "integer64"` is built eagerly as integer64, as on `main`.
The other `bigint` values make BIGINT and BIGINT UNSIGNED columns ALTREP integer, double or character vectors that convert from the stored values,
which replaces `convert_bigint()`.
As specified by DBI, `"integer"` gives NA on overflow, `"numeric"` rounds, and `"character"` is exact.
`dbColumnInfo()` and `dbFetch(n = 0)` follow `bigint` too.

`mariadb_storage()` is left for layer 3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7atAZVq1J42WzXJpS
@krlmlr
krlmlr added this pull request to stack #635 November 29, 2026 14:41

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: LAST_INSERT_ID() and other BIGINT UNSIGNED expressions under the default bigint

The server types several common expressions as BIGINT UNSIGNED, not only columns declared that way: LAST_INSERT_ID(), ROW_COUNT() in some versions, bit operations (3 & 1, x | y), CRC32(), UUID_SHORT(). On main these come back as integer64, though values at 2^63 and above are wrong (#593). With this PR they come back as mariadb_uint64, a double with exact as.character() and as.integer64(). SELECT LAST_INSERT_ID() is a frequent idiom, so code that checks inherits(x, "integer64") or uses bit64 arithmetic on the result would notice.

  • Keep it as implemented: every BIGINT UNSIGNED value, column or expression, becomes mariadb_uint64. This is consistent and exact. NEWS gets an entry about LAST_INSERT_ID(). (recommended)
  • Return integer64 for BIGINT UNSIGNED as on main, with values at 2^63 and above as NA and a warning instead of a silent wrap, and use mariadb_uint64 only with a new opt-in. This is less breaking, but loses the upper half of the range by default.
  • Decide per chunk: integer64 if every value fits below 2^63, mariadb_uint64 otherwise. Not recommended, because the class of a column would then depend on its data.

Who decides: Kirill. If nobody decides, the first option stays.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Smoke test on 4e465af: R CMD check is OK (8839 passed, 0 failed). The two red steps share one cause. The "Commit" step merges origin/main into the branch, and that merge conflicts in src/DbResult.cpp with #624, which landed on main after this branch was cut. The "Coverage" step then built the tree with the conflict markers left in (DbResult.cpp:55: version control conflict marker). The fix is to merge main into #587 (clean) and then into this branch, resolving DbResult.cpp. That happens together with the fixes from the clean review, which are in progress.


Generated by Claude Code

claude and others added 6 commits November 29, 2026 14:53
… the values of subsets

`c(x)`, `range(x)`, `cumsum(x)`, `ifelse()`, `unlist()`, `which.max()` and `cbind()` request a writable data pointer of a `mariadb_uint64` column.
This no longer detaches the column from its stored values,
so `as.character()`, `format()`, `print()` and `bit64::as.integer64()` stay exact afterwards.

The methods use the stored value of an element only where its double still equals that value.
Elements modified through assignment use the new double.
Doubles that are not integers in the range from 0 to 2^64 - 1 give `NA` with `as.character()`,
instead of text such as "18446744073709551616".

Subsets and copies of a `mariadb_uint64` column copy the selected exact values,
instead of referencing the fetched data,
so that `df$u[1]` no longer keeps the data of all columns alive.
Modifying a copy keeps the exact values of the elements that are not modified.

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

With an integer64 operand, `Ops` converts the `mariadb_uint64` operand with `bit64::as.integer64()`,
which reads the exact values, and dispatches to bit64.
The result is integer64 or logical, never a mix of the two classes.
On R >= 4.3, a `chooseOpsMethod()` method selects this conversion for `x + y`
where `x` is the `mariadb_uint64` and `y` the integer64 operand.
For `y + x`, bit64's method is used: its result keeps the class of a longer `mariadb_uint64` operand,
and the `mariadb_uint64` methods treat such results as integer64.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7atAZVq1J42WzXJpS
The types of expressions follow the rules of columns since unsigned integers are fetched exactly.
With the default `bigint = "integer64"`, `LAST_INSERT_ID()` and `CAST(... AS UNSIGNED)` of large values,
which are `BIGINT UNSIGNED`, return `mariadb_uint64` vectors instead of integer64.
`CONNECTION_ID()`, `CRC32()` and `CAST(? AS UNSIGNED)` of small values,
which are `INT UNSIGNED`, return doubles instead of integers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7atAZVq1J42WzXJpS
Resolve the conflict in src/DbResult.cpp by keeping both sides:
fetch() and get_column_info() take layer 2's `bigint` argument and
start with the check_connection() call from #624, as on main.
cpp11 registrations are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7atAZVq1J42WzXJpS
…ary operators on integer64 results

R wraps a shared vector of 64 or more elements in a wrapper object
when setting attributes, as by `names<-`, `attr<-` and `structure()`.
The exact-value methods and `[` now look through these wrappers.
The wrapper class is taken from a wrapper created in `.onLoad()`.

Unary operators on results of bit64's methods now dispatch to bit64,
and `chooseOpsMethod()` selects this method only for integer64 operands.
The documentation states that integer64 arithmetic needs R 4.3.0
and when a modified value still shows the fetched value.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: High finding left after the third review round (wrapped mariadb_uint64 vectors)

R wraps a shared vector of 64 or more elements in its own wrapper object on names<-, attr<- and structure(). That hides our class, so as.character() and print() of such a vector showed NA for values near 2^64. Commit b12e05e unwraps wrappers in C++ through ALTREP_CLASS() and R_altrep_data1(), both experimental API, not "non-API". It learns the wrapper class from a probe in .onLoad() (R/zzz.R:3-7).

The third review found that the probe never fires in an installed package. Byte-compiled code does not create a wrapper for attr(w, "a") <- 1, so the unwrapping is a silent no-op. Against the installed package, 4 tests in test-fetch-integers.R fail (lines 204, 207, 211 and 215), so R CMD check would fail too; under load_all() they pass. The reviewer verified the fix: probing with structure(x, a = 1), which is a closure, so it wraps even in compiled code. With that change all tests pass against the installed package. The same review found a Medium: [ on a wrapped vector with dim ignores the column index. The fix is to use the unwrap branch only without dim and with one index, else NextMethod().

  • Apply the verified one-line probe fix, plus a test that the probe registered (so a silent no-op cannot ship again), plus the Medium fix. No fourth review round; the fixes are small and the reviewer verified the probe fix against the installed package. (recommended)
  • Drop the wrapper unwrapping entirely. Wrapped vectors (after names<-, attr<-, structure() on 64 or more elements) then show rounded doubles, never NA, and this is documented. Simpler, and no reliance on R's wrapper internals.
  • Apply the fixes and run a fourth review round anyway.

Who decides: Kirill. Until then, nothing is pushed to this branch.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Smoke test on b12e05e (job 141389417174): 9253 passed, 4 failed, all in test-fetch-integers.R:204-215 ("mariadb_uint64 stays exact after setting attributes"). This is the High from the third review round: the wrapper probe never fires in the byte-compiled installed package. A verified fix is ready, and it waits on the question above. Nothing is pushed until then.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Handover: state of layer 2

This session is being archived, at Kirill's request. State at b12e05e:


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.

BIGINT UNSIGNED values of 2^63 and above come back as -1 or NA Large Unsigned ints Overflow issue revised

2 participants