Skip to content

Commit

Permalink
Merge branch 'main' into fetch-row-count
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Nov 7, 2023
2 parents 5db098d + 85eab12 commit 87eb831
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export(src_bigquery)
exportClasses(BigQueryConnection)
exportClasses(BigQueryDriver)
exportClasses(BigQueryResult)
exportMethods(dbAppendTable)
exportMethods(dbBegin)
exportMethods(dbBind)
exportMethods(dbClearResult)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* `dbGetRowCount()` and `dbHasComplete()` now return correct values when you
try to fetch more rows than actually exist (#501).

* `dbWriteTable()` now correct uses the `billing` value set in the
connection (#486).

* Now supports `dbAppendTable()` (#539).

* `dbReadTable()`, `dbWriteTable()`, `dbExistsTable()`, `dbRemoveTable()`,
and `dbListFields()` now all work with `DBI::Id()` (#537).

Expand Down
34 changes: 29 additions & 5 deletions R/dbi-connection.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#' @include dbi-driver.R
NULL

BigQueryConnection <-
function(project, dataset, billing,
BigQueryConnection <- function(project,
dataset,
billing,
page_size = 1e4,
quiet = NA,
use_legacy_sql = FALSE,
bigint = c("integer", "integer64", "numeric", "character")) {
ret <- new("BigQueryConnection",
new("BigQueryConnection",
project = project,
dataset = dataset,
billing = billing,
Expand All @@ -16,7 +17,6 @@ BigQueryConnection <-
use_legacy_sql = use_legacy_sql,
bigint = match.arg(bigint)
)
ret
}

#' @rdname DBI
Expand Down Expand Up @@ -180,9 +180,12 @@ dbWriteTable_bq <- function(conn,
}
tb <- as_bq_table(conn, name)

bq_table_upload(tb, value,
bq_table_upload(
tb,
value,
create_disposition = create_disposition,
write_disposition = write_disposition,
billing = conn@billing,
...
)
invisible(TRUE)
Expand Down Expand Up @@ -216,6 +219,27 @@ setMethod(
dbWriteTable_bq
)

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

bq_table_upload(tb, value,
create_disposition = "CREATE_NEVER",
write_disposition = "WRITE_APPEND",
...
)
invisible(TRUE)
}

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

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


dbReadTable_bq <- function(conn, name, ...) {
tb <- as_bq_table(conn, name)
bq_table_download(tb, ...)
Expand Down
6 changes: 6 additions & 0 deletions man/DBI.Rd

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

11 changes: 8 additions & 3 deletions tests/testthat/test-dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ test_that("can use DBI::Id()", {
ds <- bq_test_dataset()
con <- DBI::dbConnect(ds)

id <- DBI::Id(table = "mtcars")
df <- data.frame(x = 1:10)
id <- DBI::Id(table = "mytable")

expect_no_error(DBI::dbWriteTable(con, id, mtcars))
expect_no_error(DBI::dbWriteTable(con, id, df))
expect_no_error(DBI::dbAppendTable(con, id, df))
expect_no_error(DBI::dbReadTable(con, id))
expect_true(DBI::dbExistsTable(con, id))
expect_no_error(DBI::dbListFields(con, id))
Expand All @@ -91,8 +93,11 @@ test_that("can append to an existing dataset", {
DBI::dbWriteTable(con, "df", df)
DBI::dbWriteTable(con, "df", df, append = TRUE)

# Or with dbAppend
DBI::dbAppendTable(con, "df", df)

df2 <- DBI::dbReadTable(con, "df")
expect(nrow(df2), 2L)
expect(nrow(df2), 3L)
})

test_that("dataset is optional", {
Expand Down

0 comments on commit 87eb831

Please sign in to comment.