Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbQuoteLiteral() for numerics has insufficient precision in some cases #404

Open
krlmlr opened this issue Dec 9, 2022 · 2 comments · Fixed by #471
Open

dbQuoteLiteral() for numerics has insufficient precision in some cases #404

krlmlr opened this issue Dec 9, 2022 · 2 comments · Fixed by #471

Comments

@krlmlr
Copy link
Member

krlmlr commented Dec 9, 2022

library(DBI)

data <- 0.0027392760273972603
sql <- dbQuoteLiteral(ANSI(), data)

sql
#> <SQL> 0.00273927602739726
as.numeric(sql) - data
#> [1] -4.336809e-19

sql_exp <- sprintf("%.17e", data)

sql_exp
#> [1] "2.73927602739726037e-03"
as.numeric(sql_exp) - data
#> [1] 0

Created on 2022-12-09 with reprex v2.0.2

We could use sprintf("%.17e") as in the reprex to always convert to exponential notation with enough decimal numbers.

@hannes: Are you aware of databases that would complain about numbers passed in exponential notation in situations when they wouldn't complain about decimal numbers?

@krlmlr krlmlr added the bug label Apr 1, 2023
@krlmlr
Copy link
Member Author

krlmlr commented Apr 1, 2024

I'll give it a try, at least one resource suggests that exponential notation is okay: https://sql-academy.org/en/guide/literals#numeric-literals.

And another resource even suggests that this is the way to indicate floating-point literals.

@krlmlr
Copy link
Member Author

krlmlr commented Jun 2, 2024

I had to revert this because reverse dependencies were failing. Need a more robust way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant