Skip to main content

Appender

Struct Appender 

Source
pub struct Appender<'conn> { /* private fields */ }
Expand description

Appender for fast import data

§Thread Safety

Appender is neither Send nor Sync:

  • Not Send because it holds a reference to Connection, which is !Sync
  • Not Sync because DuckDB appenders don’t support concurrent access

To use an appender in another thread, move the Connection to that thread and create the appender there.

If you need to share an Appender across threads, wrap it in a Mutex.

See DuckDB concurrency documentation for more details.

§Wide Tables (Many Columns)

Array literals [value; N] are supported for tables with up to 32 columns.

appender.append_row([0; 32])?;
appender.append_row([1, 2, 3, 4, 5])?;

For tables with more than 32 columns, use one of these alternatives:

§1. Slice approach - convert values to &dyn ToSql

let values: Vec<i32> = vec![0; 100];
let params: Vec<&dyn ToSql> = values.iter().map(|v| v as &dyn ToSql).collect();
appender.append_row(params.as_slice())?;

§2. params! macro - write values explicitly

appender.append_row(params![v1, v2, v3, ..., v50])?;

§3. appender_params_from_iter - pass an iterator directly

use haybarn::appender_params_from_iter;
let values: Vec<i32> = vec![0; 100];
appender.append_row(appender_params_from_iter(values))?;

All three methods can be used interchangeably and mixed in the same appender.

Implementations§

Source§

impl Appender<'_>

Source

pub fn append_record_batch(&mut self, record_batch: RecordBatch) -> Result<()>

Append one record batch

§Example
  use arrow::record_batch::RecordBatch;
fn insert_record_batch(conn: &Connection,record_batch:RecordBatch) -> Result<()> {
    let mut app = conn.appender("foo")?;
    app.append_record_batch(record_batch)?;
    Ok(())
}
§Failure

Will return Err if append column count not the same with the table schema

Source§

impl Appender<'_>

Source

pub fn append_rows<P, I>(&mut self, rows: I) -> Result<()>
where I: IntoIterator<Item = P>, P: AppenderParams,

Append multiple rows from Iterator

§Example
fn insert_rows(conn: &Connection) -> Result<()> {
    let mut app = conn.appender("foo")?;
    app.append_rows([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])?;
    Ok(())
}
§Failure

Returns Err if a row cannot be appended.

Source

pub fn append_row<P: AppenderParams>(&mut self, params: P) -> Result<()>

Append one row

§Example
fn insert_row(conn: &Connection) -> Result<()> {
    let mut app = conn.appender("foo")?;
    app.append_row([1, 2])?;
    Ok(())
}
§Failure

Returns Err if the row cannot be appended.

Source

pub fn flush(&mut self) -> Result<()>

Flush data into DB

Source

pub fn add_column(&mut self, name: &str) -> Result<()>

Add a column to the appender’s active column list.

When columns are added, only those columns need values during append. Other columns will use their DEFAULT value (or NULL if no default).

This flushes any pending data before modifying the column list.

Source

pub fn clear_columns(&mut self) -> Result<()>

Clear the appender’s active column list.

After clearing, all columns become active again and values must be provided for every column during append.

This flushes any pending data before clearing.

Trait Implementations§

Source§

impl Debug for Appender<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Appender<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'conn> !RefUnwindSafe for Appender<'conn>

§

impl<'conn> !Send for Appender<'conn>

§

impl<'conn> !Sync for Appender<'conn>

§

impl<'conn> !UnwindSafe for Appender<'conn>

§

impl<'conn> Freeze for Appender<'conn>

§

impl<'conn> Unpin for Appender<'conn>

§

impl<'conn> UnsafeUnpin for Appender<'conn>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V