Skip to content

Support passing lists as arguments for queries like SELECT a, b WHERE a IN ($1) #43

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

Open
slyrus opened this issue May 3, 2021 · 5 comments

Comments

@slyrus
Copy link

slyrus commented May 3, 2021

I'm trying to pass two arguments to a query. One is a list to be used in the form of SELECT FOO IN ($1) while the second parameter is used just for something like BAR = $2.

Why should these two parameters need to be of the same length? A workaround is to replicate the other arguments, but this seems wrong.

@krlmlr
Copy link
Member

krlmlr commented May 4, 2021

DBI supports passing multiple parameter sets, e.g. params = list(1:2, 3:4) runs the query with parameters (1, 3) and (2, 4). Can you use list(SQL("'foo1', 'foo2'"), "bar") ?

@slyrus
Copy link
Author

slyrus commented May 4, 2021

Right, I get that there can be multiple parameter sets, but why should they be of the same length? If I'm doing "SELECT * FROM a, b WHERE a.ID in ($!) and b.ID in ($2)", I see no reason why $1 and $2 should be of the same length. I guess I don't get what you're implying happens with, e.g., parameters (1,3) and (2,4). I would expect that this query would return rows corresponding to (1,2), (1,4), (3, 2), and (3,4) not just (1,2) and (3,4). Am I missing something?

@krlmlr
Copy link
Member

krlmlr commented Aug 24, 2021

There is currently no way to pass a list of values to be used in an IN statement. Can you use dbQuoteLiteral() and paste(collapse = ", ") to construct the query?

Regarding multiple parameter sets:

library(RPostgres)

con <- dbConnect(Postgres())

dbGetQuery(con, "SELECT $1 AS a, $2 AS b", list(1:2, 3:4))
#>   a b
#> 1 1 3
#> 2 2 4

Created on 2021-08-24 by the reprex package (v2.0.0)

@slyrus
Copy link
Author

slyrus commented Aug 24, 2021

I suppose I could, but I'd rather consider this issue to be a feature request to be able to pass lists in directly. And (forgive me as it's been a couple months), IIRC, the problem isn't that i can't pass in lists, but rather that there is a specious check that the passed in lists be of the same length.

@krlmlr krlmlr changed the title validate_params insists that all parameters be of the same length Support passing lists as arguments for queries like SELECT a, b WHERE a IN ($1) Aug 26, 2021
@krlmlr
Copy link
Member

krlmlr commented Aug 26, 2021

Thanks, I've updated the issue title. The check is good, if we support lists we would be wrapping them in an extra list, and the length requirement still would be satisfied. It might well be that the client library libpq doesn't support this at all.

@krlmlr krlmlr transferred this issue from r-dbi/RPostgres Nov 1, 2021
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