pub struct VectorReader { /* private fields */ }Expand description
A typed reader for a single column in a DuckDB data chunk.
VectorReader wraps a pointer to a DuckDB vector’s data buffer and
provides ergonomic, type-checked access methods for common DuckDB types.
§Lifetimes
The reader borrows from the data chunk. Do not call duckdb_destroy_data_chunk
while a VectorReader that references it is live.
Implementations§
Source§impl VectorReader
impl VectorReader
Sourcepub unsafe fn new(chunk: duckdb_data_chunk, col_idx: usize) -> Self
pub unsafe fn new(chunk: duckdb_data_chunk, col_idx: usize) -> Self
Creates a new VectorReader for the given column in a data chunk.
§Safety
chunkmust be a validduckdb_data_chunkfor the duration of this reader’s lifetime.col_idxmust be a valid column index in the chunk.
Sourcepub unsafe fn from_vector(vector: duckdb_vector, row_count: usize) -> Self
pub unsafe fn from_vector(vector: duckdb_vector, row_count: usize) -> Self
Creates a VectorReader directly from a raw duckdb_vector handle.
Use this when you already have a child vector (e.g., from
StructVector::get_child or
ListVector::get_child).
§Safety
vectormust be a validduckdb_vectorfor the duration of this reader’s lifetime.row_countmust equal the number of valid rows in the vector.
Sourcepub const unsafe fn read_i16(&self, idx: usize) -> i16
pub const unsafe fn read_i16(&self, idx: usize) -> i16
Reads an i16 (SMALLINT) value at row idx.
§Safety
idxmust be less thanself.row_count().- The column must contain
SMALLINTdata.
Sourcepub const unsafe fn read_bool(&self, idx: usize) -> bool
pub const unsafe fn read_bool(&self, idx: usize) -> bool
Reads a bool (BOOLEAN) value at row idx.
§Pitfall L5: Defensive boolean reading
This method reads the underlying byte as u8 and compares with != 0,
rather than casting directly to bool. DuckDB’s C API does not guarantee
the Rust bool invariant (must be exactly 0 or 1), so a direct cast could
cause undefined behaviour.
§Safety
idxmust be less thanself.row_count().- The column must contain
BOOLEANdata.
Sourcepub unsafe fn read_str(&self, idx: usize) -> &str
pub unsafe fn read_str(&self, idx: usize) -> &str
Reads a VARCHAR value at row idx.
Returns an empty string if the data is not valid UTF-8 or if the internal string pointer is null.
§Pitfall P7
DuckDB stores strings in a 16-byte duckdb_string_t with two formats
(inline for ≤ 12 bytes, pointer otherwise). This method handles both.
§Safety
idxmust be less thanself.row_count().- The column must contain
VARCHARdata. - For pointer-format strings, the pointed-to heap memory must be valid
for the lifetime of the returned
&str.
Sourcepub unsafe fn read_blob(&self, idx: usize) -> &[u8] ⓘ
pub unsafe fn read_blob(&self, idx: usize) -> &[u8] ⓘ
Reads a BLOB (binary) value at row idx.
DuckDB stores BLOBs using the same 16-byte duckdb_string_t layout as
VARCHAR (inline for ≤12 bytes, pointer for larger values). The returned
slice borrows from the vector’s data buffer.
§Safety
idxmust be less thanself.row_count().- The column must contain
BLOBdata. - The pointed-to memory must be valid for the lifetime of the returned slice.
Sourcepub const unsafe fn read_timestamp(&self, idx: usize) -> i64
pub const unsafe fn read_timestamp(&self, idx: usize) -> i64
Sourcepub const unsafe fn read_interval(&self, idx: usize) -> DuckInterval
pub const unsafe fn read_interval(&self, idx: usize) -> DuckInterval
Reads an INTERVAL value at row idx.
Returns a DuckInterval struct.
§Pitfall P8
The INTERVAL struct is 16 bytes: { months: i32, days: i32, micros: i64 }.
This method handles the layout correctly using read_interval_at.
§Safety
idxmust be less thanself.row_count().- The column must contain
INTERVALdata.