Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bigrquery returns no data if there is a declared variable in the query (whether or not it is used) #635

@vkir1991

Description

@vkir1991

If you have a variable declaration in your query, the entire query returns no data, whether or not that variable is even used. These variables all work fine when run on BQ's console or via Python.

Simple reprex:

library(bigrquery)
library(DBI)

chr.query <- "
DECLARE test_date DATE DEFAULT DATE('2025-05-26');
SELECT test_date
"
tmp <- dbGetQuery(bq.con, chr.query)
print(tmp)

chr.query <- "
DECLARE test_date DATE DEFAULT DATE('2025-05-26');
SELECT DATE('2025-05-26')
"
tmp <- dbGetQuery(bq.con, chr.query)
print(tmp)

chr.query <- "
SELECT DATE('2025-05-26')
"
tmp <- dbGetQuery(bq.con, chr.query)
print(tmp)

which returns, in console:

> library(bigrquery)
> library(DBI)
> 
> chr.query <- "
+ DECLARE test_date DATE DEFAULT DATE('2025-05-26');
+ SELECT test_date
+ "
> tmp <- dbGetQuery(bq.con, chr.query)
Job complete
Billed: 0 B
> print(tmp)
# A tibble: 0 × 1
# ℹ 1 variable: test_date <chr>
> 
> chr.query <- "
+ DECLARE test_date DATE DEFAULT DATE('2025-05-26');
+ SELECT DATE('2025-05-26')
+ "
> tmp <- dbGetQuery(bq.con, chr.query)
Job complete
Billed: 0 B
> print(tmp)
# A tibble: 0 × 1
# ℹ 1 variable: f0_ <chr>
> 
> chr.query <- "
+ SELECT DATE('2025-05-26')
+ "
> tmp <- dbGetQuery(bq.con, chr.query)
Job complete
Billed: 0 B
Downloading first chunk of data.
First chunk includes all requested rows.
> print(tmp)
# A tibble: 1 × 1
  f0_       
  <date>    
1 2025-05-26

Much obliged!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions