Skip to content

Commit 38f3196

Browse files
committed
Use sessions
Fixes #557
1 parent 338d66e commit 38f3196

File tree

5 files changed

+57
-61
lines changed

5 files changed

+57
-61
lines changed

R/bq-perform.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ bq_perform_upload <- function(x, values,
113113
create_disposition = "CREATE_IF_NEEDED",
114114
write_disposition = "WRITE_EMPTY",
115115
...,
116+
session = NULL,
116117
billing = x$project
117118
) {
118119

R/bq-session.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
bq_session_create <- function(project) {
2+
check_string(project)
3+
4+
url <- bq_path(project, jobs = "")
5+
body <- list(
6+
configuration = list(
7+
query = list(
8+
query = "SELECT 1;",
9+
createSession = list(
10+
value = unbox(TRUE)
11+
)
12+
)
13+
)
14+
)
15+
16+
res <- bq_post(url, body = bq_body(body))
17+
res$statistics$sessionInfo$sessionId
18+
}

R/dbi-connection.R

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ NULL
44
BigQueryConnection <- function(project,
55
dataset,
66
billing,
7+
session = NULL,
78
page_size = 1e4,
89
quiet = NA,
910
use_legacy_sql = FALSE,
@@ -15,6 +16,7 @@ BigQueryConnection <- function(project,
1516
project = project,
1617
dataset = dataset,
1718
billing = billing,
19+
session = session,
1820
page_size = as.integer(page_size),
1921
quiet = quiet,
2022
use_legacy_sql = use_legacy_sql,
@@ -31,6 +33,7 @@ setClass(
3133
project = "character",
3234
dataset = "ANY",
3335
billing = "character",
36+
session = "ANY",
3437
use_legacy_sql = "logical",
3538
page_size = "integer",
3639
quiet = "logical",
@@ -91,6 +94,7 @@ setMethod("dbExecute", c("BigQueryConnection", "character"), function(conn, stat
9194

9295
job <- bq_perform_query(statement,
9396
billing = conn@billing,
97+
session = conn@session,
9498
default_dataset = ds,
9599
quiet = conn@quiet,
96100
...
@@ -191,36 +195,16 @@ dbWriteTable_bq <- function(conn,
191195
check_bool(overwrite)
192196
check_bool(append)
193197

194-
if (!is.null(field.types)) {
195-
cli::cli_abort(
196-
"{.arg field.types} not supported by bigrquery.",
197-
call = quote(DBI::dbWriteTable())
198-
)
199-
}
200-
if (!identical(temporary, FALSE)) {
201-
cli::cli_abort(
202-
"{.code temporary = FALSE} not supported by bigrquery.",
203-
call = quote(DBI::dbWriteTable())
204-
)
205-
}
206-
207-
if (append) {
208-
create_disposition <- "CREATE_NEVER"
209-
write_disposition <- "WRITE_APPEND"
210-
} else {
211-
create_disposition <- "CREATE_IF_NEEDED"
212-
write_disposition <- if (overwrite) "WRITE_TRUNCATE" else "WRITE_EMPTY"
213-
}
214-
tb <- as_bq_table(conn, name)
215-
216-
bq_table_upload(
217-
tb,
218-
value,
219-
create_disposition = create_disposition,
220-
write_disposition = write_disposition,
221-
billing = conn@billing,
222-
...
198+
browser()
199+
sql <- DBI::sqlCreateTable(
200+
con,
201+
name,
202+
fields = field.types,
203+
temporary = temporary,
204+
row.names = row.names
223205
)
206+
DBI::dbExecute(sql)
207+
224208
invisible(TRUE)
225209
}
226210

@@ -238,26 +222,27 @@ dbWriteTable_bq <- function(conn,
238222
#' @param field.types,temporary Ignored. Included for compatibility with
239223
#' generic.
240224
#' @export
241-
setMethod(
242-
"dbWriteTable",
243-
c("BigQueryConnection", "character", "data.frame"),
244-
dbWriteTable_bq
245-
)
246-
247-
#' @rdname DBI
248-
#' @export
249-
setMethod(
250-
"dbWriteTable",
251-
c("BigQueryConnection", "Id", "data.frame"),
252-
dbWriteTable_bq
253-
)
225+
#' setMethod(
226+
#' "dbWriteTable",
227+
#' c("BigQueryConnection", "character", "data.frame"),
228+
#' dbWriteTable_bq
229+
#' )
230+
#'
231+
#' #' @rdname DBI
232+
#' #' @export
233+
#' setMethod(
234+
#' "dbWriteTable",
235+
#' c("BigQueryConnection", "Id", "data.frame"),
236+
#' dbWriteTable_bq
237+
#' )
254238

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

258242
bq_table_upload(tb, value,
259243
create_disposition = "CREATE_NEVER",
260244
write_disposition = "WRITE_APPEND",
245+
session = conn@session,
261246
...
262247
)
263248
on_connection_updated(conn)
@@ -280,15 +265,9 @@ dbCreateTable_bq <- function(conn,
280265
...,
281266
row.names = NULL,
282267
temporary = FALSE) {
283-
if (!identical(temporary, FALSE)) {
284-
cli::cli_abort(
285-
"{.code temporary = FALSE} not supported by bigrquery.",
286-
call = quote(DBI::dbCreateTable())
287-
)
288-
}
289268

290269
tb <- as_bq_table(conn, name)
291-
bq_table_create(tb, fields)
270+
bq_table_create(tb, fields, temporary = temporary, session = conn@session)
292271
on_connection_updated(conn)
293272

294273
invisible(TRUE)
@@ -305,7 +284,7 @@ setMethod("dbCreateTable", "BigQueryConnection", dbCreateTable_bq)
305284

306285
dbReadTable_bq <- function(conn, name, ...) {
307286
tb <- as_bq_table(conn, name)
308-
bq_table_download(tb, ...)
287+
bq_table_download(tb, ..., session = conn@session)
309288
}
310289

311290
#' @rdname DBI
@@ -328,7 +307,7 @@ setMethod(
328307
}
329308
ds <- bq_dataset(conn@project, conn@dataset)
330309

331-
tbs <- bq_dataset_tables(ds, ...)
310+
tbs <- bq_dataset_tables(ds, ..., session = conn@session)
332311
map_chr(tbs, function(x) x$table)
333312
})
334313

@@ -347,7 +326,7 @@ setMethod("dbExistsTable", c("BigQueryConnection", "Id"), dbExistsTable_bq)
347326

348327
dbListFields_bq <- function(conn, name, ...) {
349328
tb <- as_bq_table(conn, name)
350-
flds <- bq_table_fields(tb)
329+
flds <- bq_table_fields(tb, session = conn@session)
351330
map_chr(flds, function(x) x$name)
352331
}
353332

@@ -362,7 +341,7 @@ setMethod("dbListFields", c("BigQueryConnection", "Id"), dbListFields_bq)
362341

363342
dbRemoveTable_bq <- function(conn, name, ...) {
364343
tb <- as_bq_table(conn, name)
365-
bq_table_delete(tb)
344+
bq_table_delete(tb, session = conn@session)
366345
on_connection_updated(conn)
367346
invisible(TRUE)
368347
}
@@ -398,7 +377,7 @@ setMethod(
398377
setMethod(
399378
"dbBegin", "BigQueryConnection",
400379
function(conn, ...) {
401-
testthat::skip("Not yet implemented: dbBegin(Connection)")
380+
dbExecute(conn, "BEGIN TRANSACTION")
402381
})
403382

404383
#' @rdname DBI
@@ -407,7 +386,7 @@ setMethod(
407386
setMethod(
408387
"dbCommit", "BigQueryConnection",
409388
function(conn, ...) {
410-
testthat::skip("Not yet implemented: dbCommit(Connection)")
389+
dbExecute(conn, "COMMIT TRANSACTION")
411390
})
412391

413392
#' @rdname DBI
@@ -416,7 +395,7 @@ setMethod(
416395
setMethod(
417396
"dbRollback", "BigQueryConnection",
418397
function(conn, ...) {
419-
testthat::skip("Not yet implemented: dbRollback(Connection)")
398+
dbExecute(conn, "ROLLBACK TRANSACTION")
420399
})
421400
# nocov end
422401

@@ -428,7 +407,6 @@ as_bq_dataset.BigQueryConnection <- function(x, ..., error_arg, error_call) {
428407
bq_dataset(x@project, x@dataset)
429408
}
430409

431-
432410
#' @export
433411
as_bq_table.BigQueryConnection <- function(x, name, ...) {
434412
if (inherits(name, "dbplyr_table_ident")) {

R/dbi-driver.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ setMethod(
9191
check_bool(use_legacy_sql)
9292
bigint <- arg_match(bigint)
9393

94+
session <- bq_session_create(billing)
95+
9496
BigQueryConnection(
9597
project = project,
9698
dataset = dataset,
9799
billing = billing,
100+
session = session,
98101
page_size = page_size,
99102
quiet = quiet,
100103
use_legacy_sql = use_legacy_sql,

R/dplyr.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ db_copy_to.BigQueryConnection <- function(con,
9999
analyze = TRUE,
100100
in_transaction = TRUE) {
101101

102-
if (temporary) {
103-
cli::cli_abort("BigQuery does not support temporary tables")
104-
}
105-
106102
tb <- as_bq_table(con, table)
107103
write <- if (overwrite) "WRITE_TRUNCATE" else "WRITE_EMPTY"
108104
bq_table_upload(tb, values, fields = types, write_disposition = write)

0 commit comments

Comments
 (0)