Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write MAP type to DB #200

Open
DavZim opened this issue Jul 22, 2024 · 1 comment
Open

Cannot write MAP type to DB #200

DavZim opened this issue Jul 22, 2024 · 1 comment
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!

Comments

@DavZim
Copy link

DavZim commented Jul 22, 2024

I cannot write a MAP type to a duckdb using DBI::dbAppendTable().

con <- DBI::dbConnect(duckdb::duckdb())

DBI::dbExecute(con, "CREATE TABLE tbl (mp MAP(VARCHAR, VARCHAR));")
#> [1] 0
DBI::dbExecute(con, "INSERT INTO tbl VALUES (MAP {'a': 'b'})")
#> [1] 1

DBI::dbGetQuery(con, "DESCRIBE tbl")
#>   column_name           column_type null  key default extra
#> 1          mp MAP(VARCHAR, VARCHAR)  YES <NA>    <NA>  <NA>


DBI::dbReadTable(con, "tbl") |> str()
#> 'data.frame':	1 obs. of  1 variable:
#> $ mp:List of 1
#>  ..$ :'data.frame':	1 obs. of  2 variables:
#>  .. ..$ key  : chr "a"
#>  .. ..$ value: chr "b"

df <- data.frame(
  mp = I(list(data.frame(key = "page", value = "1")))
)
str(df) # same structure as the returned tbl data
#> 'data.frame':	1 obs. of  1 variable:
#>  $ mp:List of 1
#>   ..$ :'data.frame':	1 obs. of  2 variables:
#>   .. ..$ key  : chr "page"
#>   .. ..$ value: chr "1"
#>   ..- attr(*, "class")= chr "AsIs"

DBI::dbAppendTable(con, "tbl", df)
#> Error: rapi_execute: Failed to run query
#> Error: Conversion Error: Unimplemented type for cast (STRUCT("key" VARCHAR, "value" VARCHAR)[] -> MAP(VARCHAR, VARCHAR))

I am using

sapply(c("DBI", "duckdb"), packageVersion)
#> $DBI
#> [1] 1 2 3
#> 
#> $duckdb
#> [1] 1 0 0 1

(1.0.0-2 is being installed at the moment but I didnt see anything related to MAPs in the changelog from 1.0.0-1).
Update, I updated to 1.0.0-2 and I see the same error.

This is closely related to #165 and #61

@krlmlr
Copy link
Collaborator

krlmlr commented Aug 16, 2024

Thanks, confirmed.

I'm currently at a loss what needs to happen here. You can work around with map_from_entries(), see below.

@hannes: do you think duckdb could implement the cast (STRUCT("key" *, "value" *)[] -> MAP(*, *)) directly?

con <- DBI::dbConnect(duckdb::duckdb())

DBI::dbExecute(con, "CREATE TABLE tbl (mp MAP(VARCHAR, VARCHAR));")
#> [1] 0
DBI::dbExecute(con, "INSERT INTO tbl VALUES (MAP {'a': 'b'})")
#> [1] 1

DBI::dbGetQuery(con, "DESCRIBE tbl")
#>   column_name           column_type null  key default extra
#> 1          mp MAP(VARCHAR, VARCHAR)  YES <NA>    <NA>  <NA>

df <- DBI::dbReadTable(con, "tbl")
tibble::as_tibble(df)
#> # A tibble: 1 × 1
#>   mp          
#>   <list>      
#> 1 <df [1 × 2]>
df$mp
#> [[1]]
#>   key value
#> 1   a     b

duckdb::duckdb_register(con, "df", df)
DBI::dbExecute(con, "INSERT INTO tbl SELECT map_from_entries(mp) FROM df")
#> [1] 1

df <- DBI::dbReadTable(con, "tbl")
tibble::as_tibble(df)
#> # A tibble: 2 × 1
#>   mp          
#>   <list>      
#> 1 <df [1 × 2]>
#> 2 <df [1 × 2]>
df$mp
#> [[1]]
#>   key value
#> 1   a     b
#> 
#> [[2]]
#>   key value
#> 1   a     b

Created on 2024-08-16 with reprex v2.1.0

@krlmlr krlmlr added enhancement feature a feature request or enhancement help wanted ❤️ we'd love your help! and removed enhancement labels Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!
Projects
None yet
Development

No branches or pull requests

2 participants