Skip to content

Conversation

@ozars
Copy link

@ozars ozars commented Dec 5, 2025

This PR enables implementing ToSql for types that represent lists. Below example starts working:

#[derive(Debug, PartialEq, Eq)]
struct MyList(Vec<i32>);

impl ToSql for MyList {
    fn to_sql(&self) -> crate::Result<ToSqlOutput<'_>> {
        Ok(ToSqlOutput::Owned(Value::List(
            self.0.iter().map(|&x| Value::Int(x)).collect(),
        )))
    }
}

let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo (numbers INT[]);")?;

let list = MyList(vec![1, 2, 3, 4, 5]);
db.execute("INSERT INTO foo (numbers) VALUES (?)", params![&list])?;

This code works by inferring inner data type of a list from the first element of the list.

This means below code doesn't work since this method doesn't work for empty lists unfortunately:

let list = MyList(vec![]);

// This should panic because the list is empty and DuckDB cannot determine the type of the list.
_ = db.execute("INSERT INTO foo (numbers) VALUES (?)", params![&list]);

There are also a breaking change in ValueRef::List so that conversion from Value::List is possible. Without this change, ValueRef::List assumes referee is backed by an Arrow array, while Value::List is backed by Vec<Value>, preventing a conversion.

ozars added 5 commits December 4, 2025 19:31
The end goal is to make binding a list to a prepared statement work.
This will be useful later for converting duckdb-rs types to FFI logical
type handles, in particular, for arguments of duckdb_create_list_value.
This will be useful later when calling FFI, in particular, for
duckdb_create_list_value and duckdb_bind_value.
`Value::List` is backed by a `Vec<Value>`, however, `ValueRef::List`
can't refer to it because `ValueRef` assumes referee is backed by Arrow
list arrays.

This commit extends `ValueRef::List` to allow refering to native Rust
values that are not backed by Arrow list arrays.

Note that this is a user-facing braking change in `ValueRef::List` and
`ListType` API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant