Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Suggests:
covr,
DBItest (>= 1.7.3),
knitr,
nanoarrow,
rlang,
rmarkdown,
testthat (>= 3.0.0)
Expand Down
3 changes: 3 additions & 0 deletions R/PqResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ setClass(
#' @param params A list of query parameters to be substituted into a parameterised query.
#' Query parameters are sent as strings, and the correct type is imputed by PostgreSQL.
#' If this fails, you can manually cast the parameter with e.g. `"$1::bigint"`.
#' Parameters are matched to the placeholders `$1`, `$2`, ... by position,
#' so they must be unnamed, as must the columns of a stream
#' passed to [DBI::dbBindArrow()].
#' @param ... Other arguments needed for compatibility with generic (currently ignored).
#' @examplesIf postgresHasDefault()
#' library(DBI)
Expand Down
16 changes: 15 additions & 1 deletion R/dbBind_PqResult.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#' @rdname postgres-query
#' @usage NULL
dbBind_PqResult <- function(res, params, ...) {
# A stream passed as `params` to dbSendQueryArrow() or dbGetQueryArrow()
# reaches this method unconverted, and its names are those of its methods.
if (inherits(params, "nanoarrow_array_stream")) {
stopc(
"`params` must not be a nanoarrow stream here: ",
"bind a stream with `dbBindArrow()` after `dbSendQueryArrow()`."
)
}
if (!is.null(names(params))) {
stopc("`params` must not be named.")
stopc(
"`params` must not be named: ",
"RPostgres matches parameters to $1, $2, ... by position. ",
"Remove the names with `unname()`, ",
"for `dbBindArrow()` by calling `unname()` on the data frame ",
"before `nanoarrow::as_nanoarrow_array_stream()`."
)
}
if (!is.list(params)) {
params <- as.list(params)
Expand Down
5 changes: 4 additions & 1 deletion man/postgres-query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions tests/testthat/test-dbBind.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
test_that("dbBind() rejects named parameters with an actionable error", {
con <- postgresDefault()
on.exit(dbDisconnect(con))

res <- dbSendQuery(con, "SELECT $1::int AS a")
on.exit(dbClearResult(res), add = TRUE, after = FALSE)

expect_error(
dbBind(res, list(a = 1L)),
"must not be named.*by position.*unname"
)

dbBind(res, unname(data.frame(a = 1:2)))
expect_equal(dbFetch(res), data.frame(a = 1:2))
})

test_that("dbBindArrow() rejects streams with named columns (#642)", {
skip_if_not_installed("nanoarrow")

con <- postgresDefault()
on.exit(dbDisconnect(con))

res <- dbSendQueryArrow(con, "SELECT $1::int AS a")
on.exit(dbClearResult(res), add = TRUE, after = FALSE)

stream <- nanoarrow::as_nanoarrow_array_stream(data.frame(a = 1:2))
expect_error(
dbBindArrow(res, stream),
"must not be named.*by position.*as_nanoarrow_array_stream"
)
})

test_that("dbBindArrow() binds unnamed columns to $1, $2 by position", {
skip_if_not_installed("nanoarrow")

con <- postgresDefault()
on.exit(dbDisconnect(con))

res <- dbSendQueryArrow(con, "SELECT $1::int AS a, $2::text AS b")
on.exit(dbClearResult(res), add = TRUE, after = FALSE)

# unname() leaves the columns of the stream with empty names
data <- unname(data.frame(x = 1:2, y = c("u", "v")))
dbBindArrow(res, nanoarrow::as_nanoarrow_array_stream(data))
expect_equal(
as.data.frame(dbFetchArrow(res)),
data.frame(a = 1:2, b = c("u", "v"))
)
})

test_that("a stream passed as `params` gets its own error", {
skip_if_not_installed("nanoarrow")

con <- postgresDefault()
on.exit(dbDisconnect(con))

# dbGetQueryArrow(params = stream) passes the stream on to dbBind() unchanged
res <- dbSendQuery(con, "SELECT $1::int AS a")
on.exit(dbClearResult(res), add = TRUE, after = FALSE)

data <- unname(data.frame(a = 1:2))
stream <- nanoarrow::as_nanoarrow_array_stream(data)
expect_error(
dbBind(res, stream),
"must not be a nanoarrow stream.*dbBindArrow.*dbSendQueryArrow"
)
})