bigrquery icon indicating copy to clipboard operation
bigrquery copied to clipboard

Unexpected data type change when downloading zero row data frame

Open elalbaicin opened this issue 1 year ago • 3 comments

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()

elalbaicin avatar Jun 11 '24 15:06 elalbaicin

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!

hadley avatar Jun 11 '24 19:06 hadley

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

elalbaicin avatar Jun 11 '24 19:06 elalbaicin

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.

hadley avatar Jun 12 '24 12:06 hadley