duckdb-rs-bitstring
An extension for duckdb-rs providing support for the DuckDB BIT/BITSTRING type. The corresponding Rust type is BitVec from the bit-vec crate. (duckdb-bitstring provides the Bitstring type as wrapper around BitVec in order to make this work.)
Compatibility table
duckdb-bitstring |
DuckDB | bit-vec |
|---|---|---|
| 0.7 | 1.4.X | 0.8 |
| 0.6 | 1.3.X | 0.8 |
| 0.5 | 1.2.X | 0.8 |
| 0.4 | 1.1.X | 0.8 |
| 0.3 | 1.0.X | 0.6.3 |
| 0.2 | 0.10.X | 0.6.3 |
Querying BITs from DuckDB
Similar to the example in duckdb-rust - a Bitstring can be obtained from .get on a row. A Bitstring can be consumed and turned into the underlying BitVec using .into_bitvec(), or .as_bitvec() can be used to get a reference to the underlying BitVec without consuming the Bitstring.
use BitVec;
use ;
use Bitstring;
Providing a Bitstring as query parameter
Use Bitstring::from(...) to wrap an owned or borrowed BitVec into a Bitstring. The Bitstring can then be passed as SQL parameter as usual in duckdb-rs. In the SQL query, it's still recommended to cast the parameter to BIT using ::BIT. That is because the BitVec gets necessarily converted to a string under the hood, and while DuckDB will be able to automatically recognize it as bitstring in many cases, it won't in all cases - hence the explicit cast.
use BitVec;
use ;
use Bitstring;
Important: a Bitstring (or rather its inner BitVec) can be empty (i.e. length of zero bits), but empty BITs are not supported in DuckDB. duckdb-bitstring will error if you try to pass an empty Bitstring as query parameter.
Usage in Appender
Bitstrings can also be used as parameter for an Appender:
use BitVec;
use ;
use Bitstring;