Comments (4)
I have landed on the same issue. This is a serious blocker for writing some non-trivial queries which need UNNEST
which cannot be written with a dplyr/dbplyr verb. @hadley
from bigrquery.
Here is a quick and temporary fix:
#' @name bq_tbl_from_query
#' @title Create a tbl object from a sql query for a bigquery connection
#' @description `tbl(con, sql(query))` should work, but fails on `bigquery
#' (1.4.2)`. This is a interim solution to keep my work going. See the issue
#' here: https://github.com/r-dbi/bigrquery/issues/540
#' @details
#'
#' 1. create a temporary table (expires in 1 day by default) using
#' `DBI::dbExecute`.
#'
#' 2. Create a `tbl` connection to the temporary table.
#'
#' Package dependencies: `bigrquery`, `DBI`, `dplyr`, `checkmate`, `cli`, `glue`
#'
#' @param con (object) of class 'BigQueryConnection'
#' @param query (string) query string
#' @param dataset (string) dataset where temporary table gets created
#' @param n_days_expiration (integerish) Number of days of expiry for the
#' temporary table
bq_tbl_from_query = function(con,
query,
dataset = NULL,
n_days_expiration = 1
){
checkmate::assert_class(con, "BigQueryConnection")
checkmate::assert_string(query)
checkmate::assert_string(dataset, null.ok = TRUE)
checkmate::assert_integerish(n_days_expiration, lower = 1)
# if dataset is NULL, try and extract 'dataset' from connection object
if (is.null(con_ads@dataset)){
if (is.null(dataset)) {
stop("'con' does not have dataset. Please provide 'dataset' input.")
}
} else {
if (is.null(dataset)) {
dataset = con_ads@dataset
}
}
dataset_exists_flag = bigrquery::bq_dataset_exists(
bigrquery::bq_dataset(project = con@project, dataset = dataset)
)
checkmate::assert_true(dataset_exists_flag)
# set expiration string
expiration_string =
glue::glue("OPTIONS(expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL {n_days_expiration} DAY))")
# pick a random table name and create it
table_exists_flag = TRUE
while(table_exists_flag){
table_name = paste(sample(letters, 20), collapse = "")
table_exists_flag = bigrquery::bq_table_exists(
bigrquery::bq_table(project = con@project,
dataset = dataset,
table = table_name
)
)
}
create_query_string =
glue::glue("CREATE TABLE {dataset}.{table_name} {expiration_string} AS ({query})")
exec_flag = (DBI::dbExecute(con, create_query_string) == 0)
if (!exec_flag){
stop("Query failed to run")
}
cli::cli_alert_info(glue::glue("Temporary table created at `{dataset}.{table_name}` will auto-expire in {n_days_expiration} days"))
# create a tbl object
res = dplyr::tbl(con, glue::glue("{dataset}.{table_name}"))
return(res)
}
from bigrquery.
Somewhat more minimal reprex:
library(dbplyr)
library(dplyr, warn.conflicts = FALSE)
library(bigrquery)
con <- DBI::dbConnect(
bigquery(),
project = "bigquery-public-data",
dataset = "stackoverflow",
billing = bq_test_project()
)
tbl(con, dbplyr::sql("SELECT * FROM tags LIMIT 10"))
#> Warning: <BigQueryConnection> uses an old dbplyr interface
#> ℹ Please install a newer version of the package or contact the maintainer
#> This warning is displayed once every 8 hours.
#> ! Using an auto-discovered, cached token.
#> To suppress this message, modify your code or options to clearly consent to
#> the use of a cached token.
#> See gargle's "Non-interactive auth" vignette for more details:
#> <https://gargle.r-lib.org/articles/non-interactive-auth.html>
#> ℹ The bigrquery package is using a cached token for '[email protected]'.
#> Error in curl::curl_fetch_memory(url, handle = handle): URL rejected: Malformed input to a URL function
Created on 2023-11-02 with reprex v2.0.2
from bigrquery.
Interestingly wrapping in collect()
or as.data.frame()
makes the problem go away, so this is caused by the print()
method?
Looks like the problem is caused by which branch of op_can_download(x)
you travel down.
Looks like that's caused by head()
: collect(head(tbl(con, dbplyr::sql("SELECT * FROM tags LIMIT 10")), n = 10))
gives a simpler call stack.
from bigrquery.
Related Issues (20)
- Use standard argument checkers
- Speed up tests HOT 1
- Update for dbplyr 2.4.0 quoting semantics
- Remove unprefixed deprecated functions
- Switch `expect_error()` to `expect_snapshot()`
- Use sessions HOT 2
- Switch to defer
- Add gzip to user agent? HOT 1
- update scheduled query HOT 8
- Seconds decimal dropped in new parsing HOT 4
- tbl() breaking when used with dbConnect - again? HOT 2
- Release bigrquery 1.5.0
- compute() function in dplyr is broken HOT 2
- Release bigrquery 1.5.1
- compute() permission denied even for temporary tables. HOT 2
- `dbplyr::table_path_components()` fails to parse components as table name is enclosed with backticks HOT 5
- Write a tbl_dbi as a table in bigquery HOT 8
- Improve bq_perform_upload to upload as PARQUET file HOT 1
- bigrquery::bq_auth() Doesn't work, Warning message: Unable to refresh token: invalid_grant Account has been deleted HOT 2
- tbl(con, sql("string query")) errors if the query contains 3 or more periods HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bigrquery.