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

How to insert lists, maps, and structs? #81

Open
Timmmm opened this issue Sep 10, 2022 · 11 comments
Open

How to insert lists, maps, and structs? #81

Timmmm opened this issue Sep 10, 2022 · 11 comments
Labels
feature Used for auto generate changelog

Comments

@Timmmm
Copy link

Timmmm commented Sep 10, 2022

Maybe I missed it, but I couldn't see how to insert a row if one of the columns is e.g. TEXT[]. I naively tried params![a_vec_of_str] but ToSql is only implemented for Vec<u8> (I guess for blobs).

The DuckDB docs aren't super clear but it looks like you're supposed to use list_create('a', 'b', 'c') but I'm not sure how to do that with params![].

@wangfenjin
Copy link
Collaborator

Haven’t been supported in rust yet

@wangfenjin wangfenjin added the feature Used for auto generate changelog label Sep 11, 2022
@digizeph
Copy link

@wangfenjin appreciate your work! Just want to insert here that missing insert of lists is a deal breaker for my projects. Hope to see that implemented soon. Cheers!

@Swoorup
Copy link
Contributor

Swoorup commented Nov 16, 2022

Recent support for Union types too. 👍🏼 Natural fits with Rust's enum.

@neverchanje
Copy link

Hope this feature can be supported. If you don't have time, I can write a PR for that.

@wangfenjin
Copy link
Collaborator

@neverchanje it'll be great if you can help. Thanks

@Swoorup
Copy link
Contributor

Swoorup commented Dec 4, 2022

Would be great if we could also be able to retrieve SRUCT, MAP, ENUMs and UNION along with being able to insert them. I think the insert just needs string representation vs the retrieval needs to be convert from arrow format?

Am I correct @wangfenjin ?

@wangfenjin
Copy link
Collaborator

Correct.

  1. If you want to support bind struct or other type as params, you may need to update bind_params
  2. If you want to support parse sql result as rust native datatypes like map, you may need to update get
  3. If it's a new native datatypes, you need to impl the FromSql and ToSql trait, example serde_json
  4. You may also want to add new type to enum Value when necessary, if you do that compiler will teach you multiple related places that also need to updated

@Swoorup

@wangfenjin
Copy link
Collaborator

wangfenjin commented Dec 5, 2022

The overall idea is in order to make it easier for users, we need to implement the convert logic of from/to sql. After doing this, you will be more familiar with the rust API and also the C API

@wangfenjin
Copy link
Collaborator

And please add some tests to the new features, feel free to support one type a time

@Swoorup
Copy link
Contributor

Swoorup commented Dec 5, 2022

I think struct/enums/unions would likely need some kind of derivation support via proc macros. This is an interesting example for clickhouse client where the author modified serde to enable support for struct <-> row mapping. See klickhouse_derive
https://github.com/Protryon/klickhouse

@v1gnesh
Copy link

v1gnesh commented Sep 5, 2023

Correct.

1. If you want to support bind struct or other type as params, you may need to update [bind_params](https://github.com/wangfenjin/duckdb-rs/blob/d2f611f922792ec6943a4f65301c81e247864fc0/src/statement.rs#L414)
2. If you want to support parse sql result as rust native datatypes like map, you may need to update [get](https://github.com/wangfenjin/duckdb-rs/blob/d2f611f922792ec6943a4f65301c81e247864fc0/src/row.rs#L544)
3. If it's a new native datatypes, you need to impl the FromSql and ToSql trait, example [serde_json](https://github.com/wangfenjin/duckdb-rs/blob/d2f611f922792ec6943a4f65301c81e247864fc0/src/types/serde_json.rs#L8-L27)
4. You may also want to add new type to enum [Value](https://github.com/wangfenjin/duckdb-rs/blob/d2f611f922792ec6943a4f65301c81e247864fc0/src/types/value.rs#L10) when necessary, if you do that compiler will teach you multiple related places that also need to updated

@wangfenjin If I try like in point 3, it says (edited out the error; I just had to add the relevant features).

Could you show an example, please, of how I can insert a String (valid JSON String) or a Vec<u8> containing a valid JSON String?
If I try, I only see the new row as bytes (hex bytes shown in decimal for each character, and not a string).
I created the table beforehand with create table tab (col1 STRING). Even tried the type TEXT and JSON.
All just get inserted as bytes.
Even with the serde_json example, I still see only bytes in decimal inserted into the table.

Will duckdb-rs add native support for Rust data types (struct/enum at least)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Used for auto generate changelog
Projects
None yet
Development

No branches or pull requests

6 participants