ropensci / nodbi

Document DBI connector for R
https://docs.ropensci.org/nodbi
Other
76 stars 6 forks source link

DuckDB rapi_prepare: Failed to prepare query #45

Closed rfhb closed 1 year ago

rfhb commented 1 year ago

When package duckdb is installed from CRAN, it does not contain the JSON extension. Subsequently queries such as below or with nodbi::docdb_query() trigger the Error: rapi_prepare: Failed to prepare query. Under MS Windows and after the JSON extension is installed, loading it often crashes R such as in RStudio; in macOS loading and installing the extension works alright.

> install.packages('duckdb')
> library(duckdb)
Loading required package: DBI
> drv <- duckdb()
> con <- dbConnect(drv)
> dbExecute(con, 'CREATE TABLE tbl (j JSON);')
[1] 0
> dbExecute(con, 'INSERT INTO tbl VALUES (\'{"family": "anatidae", "species": [ "duck", "goose"] }\');')
[1] 1
> dbGetQuery(con, 'SELECT json_type(j) FROM tbl;')
Error: rapi_prepare: Failed to prepare query SELECT json_type(j) FROM example;
Error: Catalog Error: Function with name json_type is not on the catalog, but it exists in the json extension. 
To Install and Load the extension, run: INSTALL json; LOAD json;
> dbExecute(con, 'INSTALL json;')
[1] 0
> dbExecute(con, 'LOAD json;')
# Crash of R session

Solution: Install duckdb in R using this command which does not require to separately load and install the JSON extension but works out of the box (v0.6.0 as of 2022-12-03):

install.packages('duckdb', repos = 'https://duckdb.r-universe.dev')