R语言如何向DuckDB写入原始二进制Blob数据及问题排查
我目前推测duckdb包暂不支持该操作,但不确定是否是自身操作方式不符合官方规范,以下是可复现该问题的示例代码:
con <- DBI::dbConnect(duckdb::duckdb()) # 注:使用RSQLite连接可以正常运行 # con <- DBI::dbConnect(RSQLite::SQLite()) DBI::dbCreateTable( conn = con, name = "raw_test", fields = list(file = "blob") ) DBI::dbAppendTable( conn = con, name = "raw_test", value = data.frame(file = I(list(as.raw(1:3)))), field.types = list(file = "blob") ) #> Error: rapi_execute: Unsupported column type for scan #> Error: rapi_register_df: Failed to register data frame: std::exception
- 注(1):我希望找到将任意R对象写入SQL数据库的可行方案,实现思路为将目标对象序列化为二进制格式后写入SQL,读取时再反序列化;同时要求该方案可可靠兼容尽可能多的SQL后端,因为我计划开发一款支持用户自定义传入数据库连接的工具包。
- 注(2):我已在duckdb的GitHub仓库提交了相关issue,初步怀疑该问题属于功能Bug或暂未支持的特性。
补充说明1
我进一步确认该问题大概率是duckdb包的Bug:根据DBI::dbDataType()的官方文档说明,若数据库后端需要重写该泛型函数,必须支持所有R基础数据类型作为第二个参数,包括logical、integer、numeric、character、日期(Dates)、日期时间(DateTimeClasses)、difftime类型;若数据库支持Blob类型,还必须支持原始向量列表与blob::blob对象。
duckdb明确支持Blob类型,因此理论上这类对象应当可正常写入。需要注意的是,使用blob::blob()替代I(list())构造测试数据时,仍会触发上述相同报错,测试代码如下:
DBI::dbAppendTable( conn = con, name = "raw_test", value = data.frame(file = blob::blob(as.raw(1:3))), field.types = list(file = "blob") ) #> Error: rapi_execute: Unsupported column type for scan #> Error: rapi_register_df: Failed to register data frame: std::exception
目前我暂未关闭该问题,希望有duckdb开发者可以确认该问题是否属于Bug或未覆盖的功能特性,也欢迎提供可行的临时解决方案。
内容的提问来源于stack exchange,提问作者wurli
相关产品推荐
相关产品推荐

