当通过 R 从 BigQuery 查询数据时,有没有办法将大整数转换为字符串?

Is there a way around casting large integers as string when querying data from BigQuery through R?

在通过 API 通过 R 从 BigQuery 查询数据时,是否有比将它们转换为字符串更好的方法来处理大整数?

这是一个 MVE,它显示整数显示为 "NA":

的问题
> library(bigrquery)
> 
> bq_str <- "
+ SELECT 
+    206932402797274623 AS big_pk
+   ,SAFE_CAST(206932402797274623 AS string) AS string_pk
+ "
> 
> my_df <- bigrquery::query_exec(query = bq_str, 
+                                project = 'XXXXXXXXXXX', 
+                                use_legacy_sql = FALSE,
+                                bigint = "integer64")
0 bytes processed
Warning message:
In converter[[type]](data_m[i, ]) :
  NAs introduced by coercion to integer range
> head(my_df)
  big_pk          string_pk
1     NA 206932402797274623

代码如下:

library(bigrquery)

bq_str <- "
SELECT 
   206932402797274623 AS big_pk
  ,SAFE_CAST(206932402797274623 AS string) AS string_pk
"

my_df <- bigrquery::query_exec(query = bq_str, 
                               project = 'XXXXXX', 
                               use_legacy_sql = FALSE,
                               bigint = "integer64")
head(my_df)

我正在使用 bigrquery 的 1.1.1 版。

关于 query_exec,这是 deprecated, try using bq_query

如果您只是希望避免转换为字符串,我建议转换为数字。

否则,您也可以使用 bq_table_download 请记住将 bigint 映射到 "integer64"。