Implementing the list, struct, map types fully
CGenie opened this issue · comments
Hello,
I wanted to use duckdb-rs
to insert a list of items. However, it turns out that this isn't implemented in duckdb-rs
. See:
- https://github.com/duckdb/duckdb-rs/blob/main/crates/duckdb/src/types/value.rs#L237
- https://github.com/duckdb/duckdb-rs/blob/main/crates/duckdb/src/types/value_ref.rs#L355
It seems to me that the main reason this is the case, is that the underlying duckdb type (https://github.com/duckdb/duckdb-rs/blob/main/crates/duckdb/src/types/mod.rs#L89) expects to pass the type of the underlying elements (e.g.List<Type>
). I think we need some kind of compiler introspection of the underlyingValue::List(Vec<Value>)
type, i.e. if we want to continue implementingValue::data_type
from https://github.com/duckdb/duckdb-rs/blob/main/crates/duckdb/src/types/value.rs#L237 we would do:
Value::List(vec_of_x) => Type::List(Value::data_type(x))
I'm not that advanced in rust. Maybe macros can help here? Anyways, the lack of such collection types in duckdb-rs is a big drawback for me.
BTW, I'm able to hack this with the following:
CREATE TABLE x (model STRUCT(mx DOUBLE, my DOUBLE, mz DOUBLE))
and then calling:
conn.execute("INSERT INTO x (model) values(?)", params![serde_json::json!(model_struct).to_string()])
i.e. I pass a Value::Text
as a serialized JSON string, and duckdb is able to reimplement that transparently as a struct/list etc.