Skip to content

Commit

Permalink
Add support for views (#583)
Browse files Browse the repository at this point in the history
Fixes #519
hadley authored Nov 10, 2023
1 parent 25c38c7 commit c09862f
Showing 5 changed files with 28 additions and 2 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)

* `tbl()` now works with views (#519).

* dplyr joins now work correctly across any bigquery connections (#433).

* `bq_table_download()` now parses dates using the clock package. This
21 changes: 19 additions & 2 deletions R/dplyr.R
Original file line number Diff line number Diff line change
@@ -41,6 +41,17 @@ src_bigquery <- function(project, dataset, billing = project, max_pages = 10) {
dbplyr::src_dbi(con)
}

# registered onLoad
tbl.BigQueryConnection <- function(src, from, ...) {
src <- dbplyr::src_dbi(src, auto_disconnect = FALSE)
tbl <- dplyr::tbl(src, from = from)

# This is ugly, but I don't see a better way of doing this
tb <- as_bq_table(src$con, from)
tbl$lazy_query$is_view <- !inherits(from, "sql") && bq_table_meta(tb, "type")$type == "VIEW"
tbl
}

# registered onLoad
dbplyr_edition.BigQueryConnection <- function(con) 2L

@@ -155,12 +166,18 @@ op_can_download.lazy_select_query <- function(x) {
}
#' @export
op_can_download.lazy_base_query <- function(x) {
dbplyr::is.ident(x$x) || inherits(x$x, "dbplyr_table_ident")
if (isTRUE(x$is_view)) {
FALSE
} else if (inherits(x$x, "sql")) {
FALSE
} else {
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)
if (inherits(x$x$x, "sql")) return(FALSE)
if (!op_can_download(x$x)) return(FALSE)

vars_base <- dbplyr::op_vars(x$x)
if (!is_select_trivial(x$select, vars_base)) return(FALSE)
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
}

# S3 methods --------------------------------------------------------------
s3_register("dplyr::tbl", "BigQueryConnection")
s3_register("dplyr::collect", "tbl_BigQueryConnection")
s3_register("dplyr::same_src", "tbl_BigQueryConnection")

1 change: 1 addition & 0 deletions tests/testthat/test-bq-job.R
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ test_that("can control chattiness of bq_job_wait", {
})

test_that("informative errors on failure", {
withr::local_options(cli.progress_show_after = 10)
ds <- bq_test_dataset()

tb <- bq_test_table()
5 changes: 5 additions & 0 deletions tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
@@ -101,6 +101,11 @@ test_that("collect can identify directly download tables", {
bq5 <- dplyr::tbl(con, dplyr::sql("SELECT * FROM mtcars"))
expect_false(op_can_download(bq5))
expect_false(op_can_download(head(bq5)))

DBI::dbExecute(con, 'CREATE VIEW mtcars2 AS SELECT * FROM basedata.mtcars')
defer(DBI::dbExecute(con, 'DROP VIEW mtcars2'))
bq6 <- dplyr::tbl(con, "mtcars2")
expect_false(op_can_download(bq6))
})

test_that("casting uses bigquery types", {

0 comments on commit c09862f

Please sign in to comment.