Giter VIP home page Giter VIP logo

duckdb-rs-bitstring's Introduction

crates.io docs.rs

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.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 bit_vec::BitVec;
use duckdb::{Connection, Result};
use duckdb_bitstring::Bitstring;

fn main() -> Result<()> {
    let conn = Connection::open_in_memory()?;

    conn.execute_batch(
        "CREATE TABLE t1 (d BIT);
        INSERT INTO t1 VALUES ('10110'::BIT);
        INSERT INTO t1 VALUES ('01101100010101101'::BIT);
        INSERT INTO t1 VALUES ('11111111111'::BIT);"
    )?;

    let bitvecs: Vec<Result<BitVec>> = conn.prepare("SELECT d FROM t1")?.query_map([], |row| {
        let value: Bitstring = row.get(0)?;
        Ok(value.into_bitvec())
    })?.collect();

    for bv in bitvecs {
        println!("{:?}", bv?);
    }

    // 10110
    // 01101100010101101
    // 11111111111

    Ok(())
}

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 bit_vec::BitVec;
use duckdb::{params, Connection, Result};
use duckdb_bitstring::Bitstring;

fn main() -> Result<()> {
    let conn = Connection::open_in_memory()?;

    conn.execute(
        "CREATE TABLE t1 (id INT, d BIT);",
        [],
    )?;

    let bv = BitVec::from_iter(vec![true, true, false, false, true]);
    let bs = Bitstring::from(bv);

    conn.execute(
        "INSERT INTO t1 VALUES (?, ?::BIT)",
        params![1, bs],
    )?;


    Ok(())
}

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 bit_vec::BitVec;
use duckdb::{params, Connection, Result};
use duckdb_bitstring::Bitstring;

fn main() -> Result<()> {
    let conn = Connection::open_in_memory()?;

    conn.execute(
        "CREATE TABLE t1 (id INT, d BIT);",
        [],
    )?;

    let mut appender = conn.appender("t1")?;

    let bv = BitVec::from_iter(vec![true, true, false, false, true]);
    let bs = Bitstring::from(bv);

    appender.append_row(params![1, bs])?;

    // note: if you want to try querying the `t1` table now,
    // you need to drop `appender` first!

    Ok(())
}

duckdb-rs-bitstring's People

Contributors

thomas-daniels avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.