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

Infinite arguments require explicit type cast #361

Open
mmuurr opened this issue Dec 26, 2020 · 3 comments
Open

Infinite arguments require explicit type cast #361

mmuurr opened this issue Dec 26, 2020 · 3 comments

Comments

@mmuurr
Copy link

mmuurr commented Dec 26, 2020

Not sure what can be done about this, but if an SQL expression with parameters expects a float and Inf (or -Inf) is passed, PostgreSQL errs and complains like so:

dbGetQuery(conn, "select 0 + $1", Inf)
## Error: Failed to fetch row: ERROR:  invalid input syntax for type integer: "Inf"

With an explicit PostgreSQL type cast for the $1 parameter, however:

dbGetQuery(conn, "select 0 + $1::float", Inf)
## # A tibble: 1 x 1
##   `?column?`
##        <dbl>
## 1        Inf

This mirrors what happens in PostgreSQL:

select 0 + 'Infinity';  -- errs
select 0 + 'Infinity'::float;  -- succeeds

And so I don't think this counts as a bug, but it's sneaky in that a RPostgres user would have to protect her queries with type casts if there was any chance the R program would produce Inf as the passed value. Perhaps explicit casting should always be encouraged anyhow, though :-)

In any case, I don't know if this is known to y'all, and perhaps it's worth mentioning in docs (since PostgreSQL does support infinite values, but just requires this extra care in some situations to prevent errors).

@krlmlr
Copy link
Member

krlmlr commented Dec 26, 2020

Thanks. I think the recommendation makes sense for all databases. Can we document this in ?DBI::dbBind(), would you like to submit a pull request?

@krlmlr krlmlr transferred this issue from r-dbi/RPostgres Sep 14, 2021
@krlmlr krlmlr added the docs label Sep 14, 2021
@krlmlr krlmlr added this to the 1.1.2 milestone Sep 14, 2021
@krlmlr krlmlr removed this from the 1.1.2 milestone Dec 19, 2021
@mmuurr
Copy link
Author

mmuurr commented Nov 28, 2023

I guess this is ultimately the responsibility of each driver's dbQuoteLiteral() implementation? {RPostgres} already has conditionals based on object type, so adding tests for a few special values seems to make sense to me.

If that's a good way to go (as I've been bitten by this again recently, since min() and max() can return infinites) I can hop over to {RPostgres} and open a PR with a few such additional cases (and corresponding unit tests). For other back-ends I'm less useful, but obviously anyone can open driver-specific issues and use the Postgres solution as some sort of precedent.

Then the documentation add here would be a very small, "beware of special values -- they're handled (or not handled) by each driver differently" snippet.

@krlmlr
Copy link
Member

krlmlr commented Dec 2, 2023

I'd appreciate a tweak to RPostgres, thank you!

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

No branches or pull requests

2 participants