Skip to content

Commit

Permalink
Hack in dbplyr 2.4.0 support (#555)
Browse files Browse the repository at this point in the history
Fixes #550
  • Loading branch information
hadley authored Nov 6, 2023
1 parent cc05975 commit d901bea
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# bigrquery (development version)

* Compatible with dbplyr 2.4.0 (#550).

* `con |> tbl(sql("..."))` now works robustly once more (#540). (No more
"URL using bad/illegal format or missing URL" error).

Expand Down
26 changes: 16 additions & 10 deletions R/dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,26 @@ as_bq_dataset.BigQueryConnection <- function(x) {

#' @export
as_bq_table.BigQueryConnection <- function(x, name, ...) {
pieces <- strsplit(name, ".", fixed = TRUE)[[1]]
if (inherits(name, "dbplyr_table_ident")) {
name <- unclass(name)
pieces <- c(name$catalog, name$schema, name$table)
pieces <- pieces[!is.na(pieces)]

if (length(pieces) == 1) {
pieces <- strsplit(pieces, ".", fixed = TRUE)[[1]]
}
} else if (is.character(name) && length(name) == 1) {
pieces <- strsplit(name, ".", fixed = TRUE)[[1]]
} else {
cli::cli_abort("{.arg name} must be a string or a dbplyr_table_ident.")
}

if (length(pieces) > 3) {
stop(
"Table name, '", name, "', must have 1-3 components.",
call. = FALSE
)
cli::cli_abort("{.arg name} ({.str {name}}) must have 1-3 components.")
}
if (length(pieces) == 1 && is.null(x@dataset)) {
stop(
"Table name, '", name, "', must have 2 or 3 components ",
"when the connection has no dataset",
call. = FALSE
cli::cli_abort(
"{.arg name} ({.str {name}}) must have 2 or 3 components if the connection doesn't have a dataset."
)
}

Expand All @@ -326,4 +333,3 @@ as_bq_table.BigQueryConnection <- function(x, name, ...) {
bq_table(pieces[[1]], pieces[[2]], pieces[[3]])
)
}

6 changes: 4 additions & 2 deletions R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ db_copy_to.BigQueryConnection <- function(con, table, values,
abort("BigQuery does not support temporary tables")
}

tb <- bq_table(con@project, con@dataset, table)
tb <- as_bq_table(con, table)
write <- if (overwrite) "WRITE_TRUNCATE" else "WRITE_EMPTY"
bq_table_upload(tb, values, fields = types, write_disposition = write)

Expand Down Expand Up @@ -149,7 +149,9 @@ op_can_download.lazy_select_query <- function(x) {
query_is_head_only(x)
}
#' @export
op_can_download.lazy_base_query <- function(x) dbplyr::is.ident(x$x)
op_can_download.lazy_base_query <- function(x) {
dbplyr::is.ident(x$x) || inherits(x$x, "dbplyr_table_ident")
}

query_is_head_only <- function(x) {
if (!inherits(x$x, "lazy_base_remote_query")) return(FALSE)
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/_snaps/dbi-connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,27 @@
<BigQueryConnection>
Billing: p

# can create bq_table from connection + name

Code
as_bq_table(con1, "x")
Condition
Error in `as_bq_table()`:
! `name` ("x") must have 2 or 3 components if the connection doesn't have a dataset.

---

Code
as_bq_table(con1, "a.b.c.d")
Condition
Error in `as_bq_table()`:
! `name` ("a.b.c.d") must have 1-3 components.

# as_bq_table checks its input types

Code
as_bq_table(con1, letters)
Condition
Error in `as_bq_table()`:
! `name` must be a string or a dbplyr_table_ident.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/dplyr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# can copy_to

Code
dplyr::copy_to(con, mtcars)
Condition
Error in `db_copy_to()`:
! BigQuery does not support temporary tables

9 changes: 7 additions & 2 deletions tests/testthat/test-dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,22 @@ test_that("dataset is optional", {

test_that("can create bq_table from connection + name", {
con1 <- DBI::dbConnect(bigquery(), project = "p")
expect_error(as_bq_table(con1, "x"), "must have 2 or 3 components")
expect_snapshot(as_bq_table(con1, "x"), error = TRUE)
expect_equal(as_bq_table(con1, "x.y"), as_bq_table("p.x.y"))
expect_equal(as_bq_table(con1, "x.y.z"), as_bq_table("x.y.z"))
expect_error(as_bq_table(con1, "a.b.c.d"), "must have 1-3 components")
expect_snapshot(as_bq_table(con1, "a.b.c.d"), error = TRUE)

con2 <- DBI::dbConnect(bigquery(), project = "p", dataset = "d")
expect_equal(as_bq_table(con2, "x"), as_bq_table("p.d.x"))
expect_equal(as_bq_table(con2, "x.y"), as_bq_table("p.x.y"))
expect_equal(as_bq_table(con2, "x.y.z"), as_bq_table("x.y.z"))
})

test_that("as_bq_table checks its input types", {
con1 <- DBI::dbConnect(bigquery(), project = "p")
expect_snapshot(as_bq_table(con1, letters), error = TRUE)
})

test_that("the return type of integer columns is set by the bigint argument", {
x <- c("-2147483648", "-2147483647", "-1", "0", "1", "2147483647", "2147483648")
sql <- paste0("SELECT * FROM UNNEST ([", paste0(x, collapse = ","), "]) AS x");
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_that("can copy_to", {
ds <- bq_test_dataset()
con <- DBI::dbConnect(ds)

expect_error(dplyr::copy_to(con, mtcars), "temporary tables")
expect_snapshot(dplyr::copy_to(con, mtcars), error = TRUE)
bq_mtcars <- dplyr::copy_to(con, mtcars, temporary = FALSE)

expect_s3_class(bq_mtcars, "tbl_BigQueryConnection")
Expand Down Expand Up @@ -83,12 +83,12 @@ test_that("collect can identify directly download tables", {
bq1 <- dplyr::tbl(con, "mtcars")
expect_true(op_can_download(bq1))
expect_equal(op_rows(bq1), Inf)
expect_equal(as.character(op_table(bq1)), "mtcars")
expect_equal(format(op_table(bq1)), "`mtcars`")

bq2 <- head(bq1, 4)
expect_true(op_can_download(bq2))
expect_equal(op_rows(bq2), 4)
expect_equal(as.character(op_table(bq1)), "mtcars")
expect_equal(format(op_table(bq1)), "`mtcars`")

bq3 <- head(bq2, 2)
expect_true(op_can_download(bq3))
Expand Down

0 comments on commit d901bea

Please sign in to comment.