r-dbi / bigrquery

An interface to Google's BigQuery from R.
https://bigrquery.r-dbi.org
Other
512 stars 180 forks source link

Unexpected data type change when downloading zero row data frame #615

Open elalbaicin opened 3 months ago

elalbaicin commented 3 months ago

Whenever a query result has zero rows, some columns of the downloaded data frame have their data type changed.

In particular, the downloaded data frame converts date columns to string columns:

example

This type conversion has caused some unexpected behavior. Is it possible to ensure that the column types from downloaded data frames always match the schema?

# Download zero rows table (error)
queryData0 <- glue("SELECT {dateCol} FROM {dataset}.{table} LIMIT 0")

bq_project_query(x = projectId, query = queryData0) |>
  bq_table_download()

# Download non-zero rows table (correct)
queryData1 <- glue("SELECT {dateCol} FROM {dataset}.{table} LIMIT 1")

bq_project_query(x = projectId, query = queryData1) |>
  bq_table_download()
hadley commented 3 months ago

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session. Thanks!

elalbaicin commented 3 months ago

Yes, of course.

library(bigrquery)

# Auth
bq_auth_configure(path = "client_key_googlecloud.json")
bq_auth(path = "marine-booth-200313-xxxxxxxxxxx.json")

# Project ID
projectId <- "marine-booth-200313"

# Download one row table
queryData1 <- "SELECT date FROM RData.adsRaw LIMIT 1"

bq_project_query(x = projectId, query = queryData1) |>
  bq_table_download()
#> # A tibble: 1 × 1
#>   date      
#>   <date>    
#> 1 2020-12-22

# Download zero rows table (error)
queryData0 <- "SELECT date FROM RData.adsRaw LIMIT 0"

bq_project_query(x = projectId, query = queryData0) |>
  bq_table_download()
#> # A tibble: 0 × 1
#> # ℹ 1 variable: date <chr>

Created on 2024-06-11 with reprex v2.1.0

hadley commented 3 months ago

Thanks! It'll probably be a couple of weeks before I get to this, but I'm hoping to put some more time into bigrquery soon.