Skip to content

Commit

Permalink
Updates for dbplyr 2.5.0 (#601)
Browse files Browse the repository at this point in the history
* Don't set check_from for dbplyr 2.5.0
* Support I() in DBI wrappers
* Hide dbplyr call from R CMD check
* Conditionally wrap table name in I()
  • Loading branch information
hadley authored Mar 14, 2024
1 parent 0d434f5 commit 5cc2d14
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ S3method(print,bq_param)
S3method(print,bq_params)
S3method(print,bq_table)
S3method(print,gs_object)
S3method(toString,bq_table)
export(as_bq_dataset)
export(as_bq_field)
export(as_bq_fields)
Expand Down
7 changes: 6 additions & 1 deletion R/bq-refs.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,15 @@ setOldClass("bq_table")

#' @export
print.bq_table <- function(x, ...) {
cat_line("<bq_table> ", x$project, ".", x$dataset, ".", x$table)
cat_line("<bq_table> ", toString(x))
invisible(x)
}

#' @export
toString.bq_table <- function(x, ...) {
paste0(x$project, ".", x$dataset, ".", x$table)
}

#' @rdname bq_refs
#' @export
as_bq_table <- function(x,
Expand Down
41 changes: 37 additions & 4 deletions R/dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ setMethod(
dbWriteTable_bq
)

#' @rdname DBI
#' @export
setMethod(
"dbWriteTable",
c("BigQueryConnection", "AsIs", "data.frame"),
dbWriteTable_bq
)


dbAppendTable_bq <- function(conn, name, value, ..., row.names = NULL) {
tb <- as_bq_table(conn, name)

Expand All @@ -260,7 +269,7 @@ dbAppendTable_bq <- function(conn, name, value, ..., row.names = NULL) {
write_disposition = "WRITE_APPEND",
...
)
on_connection_updated(conn, name)
on_connection_updated(conn, toString(tb))

invisible(TRUE)
}
Expand All @@ -274,6 +283,10 @@ setMethod("dbAppendTable", c("BigQueryConnection", "character", "data.frame"), d
#' @export
setMethod("dbAppendTable", c("BigQueryConnection", "Id", "data.frame"), dbAppendTable_bq)

#' @rdname DBI
#' @export
setMethod("dbAppendTable", c("BigQueryConnection", "AsIs", "data.frame"), dbAppendTable_bq)

dbCreateTable_bq <- function(conn,
name,
fields,
Expand All @@ -289,7 +302,7 @@ dbCreateTable_bq <- function(conn,

tb <- as_bq_table(conn, name)
bq_table_create(tb, fields)
on_connection_updated(conn, name)
on_connection_updated(conn, toString(tb))

invisible(TRUE)
}
Expand Down Expand Up @@ -317,6 +330,10 @@ setMethod("dbReadTable", c("BigQueryConnection", "character"), dbReadTable_bq)
#' @export
setMethod("dbReadTable", c("BigQueryConnection", "Id"), dbReadTable_bq)

#' @rdname DBI
#' @export
setMethod("dbReadTable", c("BigQueryConnection", "AsIs"), dbReadTable_bq)

#' @rdname DBI
#' @inheritParams DBI::dbListTables
#' @export
Expand Down Expand Up @@ -345,6 +362,10 @@ setMethod("dbExistsTable", c("BigQueryConnection", "character"), dbExistsTable_b
#' @export
setMethod("dbExistsTable", c("BigQueryConnection", "Id"), dbExistsTable_bq)

#' @rdname DBI
#' @export
setMethod("dbExistsTable", c("BigQueryConnection", "AsIs"), dbExistsTable_bq)

dbListFields_bq <- function(conn, name, ...) {
tb <- as_bq_table(conn, name)
flds <- bq_table_fields(tb)
Expand All @@ -360,10 +381,14 @@ setMethod("dbListFields", c("BigQueryConnection", "character"), dbListFields_bq)
#' @export
setMethod("dbListFields", c("BigQueryConnection", "Id"), dbListFields_bq)

#' @rdname DBI
#' @export
setMethod("dbListFields", c("BigQueryConnection", "AsIs"), dbListFields_bq)

dbRemoveTable_bq <- function(conn, name, ...) {
tb <- as_bq_table(conn, name)
bq_table_delete(tb)
on_connection_updated(conn, name)
on_connection_updated(conn, toString(tb))
invisible(TRUE)
}

Expand All @@ -376,6 +401,10 @@ setMethod("dbRemoveTable", c("BigQueryConnection", "character"), dbRemoveTable_b
#' @export
setMethod("dbRemoveTable", c("BigQueryConnection", "Id"), dbRemoveTable_bq)

#' @rdname DBI
#' @export
setMethod("dbRemoveTable", c("BigQueryConnection", "AsIs"), dbRemoveTable_bq)

# nocov start
#' @rdname DBI
#' @inheritParams DBI::dbGetInfo
Expand Down Expand Up @@ -431,7 +460,9 @@ as_bq_dataset.BigQueryConnection <- function(x, ..., error_arg, error_call) {

#' @export
as_bq_table.BigQueryConnection <- function(x, name, ...) {
if (inherits(name, "dbplyr_table_ident")) {
if (inherits(name, "dbplyr_table_path")) { # dbplyr 2.5.0
pieces <- getFromNamespace("table_path_components", "dbplyr")(name, x)[[1]]
} else if (inherits(name, "dbplyr_table_ident")) { # dbplyr 2.4.0
name <- unclass(name)
pieces <- c(name$catalog, name$schema, name$table)
pieces <- pieces[!is.na(pieces)]
Expand All @@ -442,6 +473,8 @@ as_bq_table.BigQueryConnection <- function(x, name, ...) {
} else if (is(name, "Id")) {
pieces <- unname(name@name)
} else if (is.character(name) && length(name) == 1) {
# Technically incorrect according to the DBI spec, because it should
# automatically quote the name; but too high risk to change now
pieces <- strsplit(name, ".", fixed = TRUE)[[1]]
} else {
cli::cli_abort("{.arg name} must be a string or a dbplyr_table_ident.")
Expand Down
11 changes: 9 additions & 2 deletions R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ src_bigquery <- function(project, dataset, billing = project, max_pages = 10) {
# registered onLoad
tbl.BigQueryConnection <- function(src, from, ...) {
src <- dbplyr::src_dbi(src, auto_disconnect = FALSE)
tbl <- dplyr::tbl(src, from = from, check_from = FALSE)

if (utils::packageVersion("dbplyr") >= "2.4.0.9000") {
tbl <- dplyr::tbl(src, from = from)
} else {
tbl <- dplyr::tbl(src, from = from, check_from = FALSE)
}

# This is ugly, but I don't see a better way of doing this
tb <- as_bq_table(src$con, from)
Expand Down Expand Up @@ -170,7 +175,9 @@ op_can_download.lazy_base_query <- function(x) {
} else if (inherits(x$x, "sql")) {
FALSE
} else {
dbplyr::is.ident(x$x) || inherits(x$x, "dbplyr_table_ident")
dbplyr::is.ident(x$x) ||
inherits(x$x, "dbplyr_table_ident") ||
inherits(x$x, "dbplyr_table_path")
}
}

Expand Down
1 change: 0 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.onLoad <- function(libname, pkgname) {

.auth <<- gargle::init_AuthState(
package = "bigrquery",
auth_active = TRUE
Expand Down
28 changes: 28 additions & 0 deletions man/DBI.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ test_that("can collect and compute (no dataset)", {

# compute: persistent
name <- paste0("basedata.", random_name())
perm <- dplyr::compute(bq_mtcars, temporary = FALSE, name = name)
if (packageVersion("dbplyr") >= "2.4.0.9000") name <- I(name)
perm <- dplyr::compute(bq_mtcars, name = name, temporary = FALSE)
defer(DBI::dbRemoveTable(con, name))

expect_true(DBI::dbExistsTable(con, name))
Expand Down

0 comments on commit 5cc2d14

Please sign in to comment.