Skip to content

fix: Exact double literals in dbQuoteLiteral() - #784

Closed
krlmlr wants to merge 6 commits into
mainfrom
claude/dbquoteliteral-precision
Closed

krlmlr wants to merge 6 commits into
mainfrom
claude/dbquoteliteral-precision

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Takes over #688 by LeonidasZhak (fixes #404), with the contributor's commits and authorship kept, and current main merged in.

Change

The default dbQuoteLiteral() method keeps the as.character() output for an unclassed double when it already round-trips. For finite unclassed doubles where it does not, it uses sprintf("%.16g", x) if that round-trips in R, else sprintf("%.17g", x). #688's description mentions formatC(..., digits = 17, format = "fg", flag = "#"); the code has always used sprintf(), and this description reflects the code.

This alters the SQL that sqlInterpolate(), sqlData() and sqlAppendTable() generate for doubles that need more than 15 significant digits, e.g. 0.1 + 0.2 now gives 0.30000000000000004 instead of 0.3. Doubles that round-trip are rendered as before.

Tests

devtools::test() passes locally (0 failures) with the dev dependencies installed. New tests cover integers stored as doubles, -0, subnormal and extreme values, mixed vectors with NA and names, integer64, other classed doubles, and locale independence.

Status

Clean review done in three rounds. Open questions are posted as comments on this PR, and the remaining limitations are in the "Accepted limitations" comment (#784 (comment)). Planned for the next CRAN release, gated on the release branch's revdep run, as decided on #781 (#781 (comment)).

https://claude.ai/code/session_01Hr1rd6u3YTGmEdw8JDZsnc


Generated by Claude Code

…n `dbQuoteLiteral()`

Classed doubles such as integer64 keep the previous as.character() path,
and doubles that do not round-trip use the shortest of 15, 16, or 17
significant digits that does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6u3YTGmEdw8JDZsnc
Assign through indices, declare bit64 in Suggests for the tests,
and test that negative zero quotes as 0.

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

krlmlr commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Question: Always use 17 digits for doubles that as.character() does not round-trip?

The new branch (lines 61–67) tries %.16g first and keeps it when as.numeric() gives the value back, else uses %.17g. R's parser does not always round correctly, so the 16-digit string can come back as x in R while a database's correctly rounding parser reads the neighboring double. Example: x <- -0x1.b991d93397ab8p+5 gives "-55.19621506029711"; R reads it as x, a correctly rounding parser (checked with Python's float()) reads -0x1.b991d93397ab9p+5. The clean review found 67 such values among about 370k reformatted random doubles, at all magnitudes, and 69 where 17 digits were used though 16 would have done. The behavior of R's parser also depends on the platform (e.g. builds without long double). %.17g always identifies a double exactly for a correct parser.

  • Always emit %.17g for finite doubles whose as.character() does not round-trip in R. Correct for any correctly rounding parser, at most one digit longer (e.g. 1/3 gives "0.33333333333333331"), and faster (one sprintf(), no second parse). The values that change are the same as with the current code, so the revdep impact does not grow. (recommended)
  • Keep the shortest form as judged by R's parser, and document that "round-trips" means through R.

Who decides: Kirill. If nobody decides, the current code (second option) stays.

The as.character() path itself has the same weakness for a few values (11 in the sample, e.g. 1e-310 gives "9.99999999999997e-311"); that predates this PR and is not changed here.


Generated by Claude Code

krlmlr commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Question: Should classed doubles get the precise format too?

The precise branch runs only for unclassed doubles (line 53), because bit64::integer64 has double storage: without the guard, as.integer64("9007199254740993") was quoted as 4.4501477170144038e-308 (found in the clean review, now covered by a test). As a consequence, other classed doubles keep the lossy pre-PR output: I(1/3) and structure(1/3, class = "foo") give "0.333333333333333", and units objects would too (not tested, the package is not installed here). This is unchanged from main, not a regression.

  • Keep as.character() for every classed double, as now. Smallest change in output, so the least revdep exposure; classes with their own semantics are left to their as.character() methods. (recommended)
  • Exclude only integer64 and format all other classed doubles via unclass(x). Fixes I() and similar, but changes output for classes whose as.character() method does more than format the number.

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


Generated by Claude Code

krlmlr commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Question: Run a targeted revdep check on this branch before merging?

Per the decision on #781, this goes into the next release, gated on the release branch's revdep run. The change alters the SQL from the default dbQuoteLiteral() method, and hence sqlInterpolate(), sqlData() and sqlAppendTable(), for every unclassed double whose 15-digit as.character() form does not round-trip. That includes everyday computed values: 0.1 + 0.2 gives "0.30000000000000004" instead of "0.3", 100 * 1.1 gives "110.00000000000001" instead of "110". Values that round-trip (1, 0.1, 1e-05, integers up to 2^53) are unchanged. Snapshot tests in dbplyr and in the backends that rely on the default method can break, and per #404 an earlier fix was reverted for revdep failures.

  • Rely on the release branch's revdep run, as decided on chore: Review for the next CRAN release #781; merge once the questions here are settled and CI is green. (recommended)
  • Also run a targeted revdep check (dbplyr and the main backends) against this branch before merging, so breakage shows up before the release branch is cut. Costs one revdep run and some days.

Who decides: Kirill. I will not dispatch any revdep run myself. If nobody decides, the first option holds.


Generated by Claude Code

krlmlr commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations


Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 28, 2026 06:29
@krlmlr

krlmlr commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Not going there.

@krlmlr krlmlr closed this Nov 28, 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.

dbQuoteLiteral() for numerics has insufficient precision in some cases

3 participants