Skip to content

fix: preserve exact double literals without forcing scientific notation - #688

Closed
LeonidasZhak wants to merge 2 commits into
r-dbi:mainfrom
LeonidasZhak:fix/dbquoteliteral-precision-404
Closed

LeonidasZhak wants to merge 2 commits into
r-dbi:mainfrom
LeonidasZhak:fix/dbquoteliteral-precision-404

Conversation

@LeonidasZhak

Copy link
Copy Markdown

Summary

Preserve exact double literals in dbQuoteLiteral() only when the default formatting would lose precision.

Thanks to maintainers

Thanks for the original issue discussion and for reverting the earlier scientific-notation change once reverse dependencies showed it was too broad.

Issue or motivation

DBI::dbQuoteLiteral() currently uses as.character() for double values. That loses precision for some values reported in #404, so quoted literals do not always round-trip back to the original number.

Root cause

The default character conversion for double values is readable, but it is not guaranteed to preserve the original binary value. The earlier fix switched all doubles to scientific notation, which fixed precision but changed unaffected literals more broadly than necessary.

Change

  • Keep the existing as.character() output when it already round-trips exactly.
  • Fall back to a longer decimal representation with formatC(..., digits = 17, format = "fg", flag = "#") only for finite double values where the default formatting loses precision.
  • Add regression tests for precision-sensitive doubles and for simple values that should keep their existing representation.

Tests

  • Rscript -e 'pkgload::load_all(\".\", quiet = TRUE); testthat::test_file(\"tests/testthat/test-quoting.R\")'
  • Rscript -e 'pkgload::load_all(\".\", quiet = TRUE); testthat::test_file(\"tests/testthat/test-sql-df.R\"); testthat::test_file(\"tests/testthat/test-interpolate.R\")'
  • R CMD build .
  • R CMD check --no-manual DBI_1.3.0.9010.tar.gz

Scope

This does not change integer or logical quoting, does not reintroduce scientific notation for all doubles, and does not include the unrelated generated vignette file changes from local checks.

@LeonidasZhak

Copy link
Copy Markdown
Author

Following up on this PR: the latest rcc run fails in rcc: ubuntu-24.04 (4.6) during the install step. GitHub has expired the job logs, and contributors cannot rerun this workflow in r-dbi/DBI. Could a maintainer rerun the job or share current failure details? The branch is unchanged since f2e3bb6.

@krlmlr

krlmlr commented Nov 28, 2026

Copy link
Copy Markdown
Member

Thank you. I'm prepared to this limitation in DBI, users who need ultra-high precision need to quote themselves. I'm willing to reconsider branch-free code that uses exactly one way to convert accurately in all or almost all cases.

@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.

2 participants