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 const unsafe fn read_u128(&self, idx: usize) -> u128
pub const unsafe fn read_u128(&self, idx: usize) -> u128
Reads a u128 (UHUGEINT) value at row idx.
DuckDB stores UHUGEINT as { lower: u64, upper: u64 } in little-endian
layout, totalling 16 bytes per value.
§Safety
idxmust be less thanself.row_count().- The column must contain
UHUGEINTdata.
Sourcepub const unsafe fn read_timestamp_tz(&self, idx: usize) -> i64
pub const unsafe fn read_timestamp_tz(&self, idx: usize) -> i64
Reads a TIMESTAMP WITH TIME ZONE value at row idx, as microseconds
since the Unix epoch in UTC.
§Safety
idxmust be less thanself.row_count().- The column must contain
TIMESTAMPTZdata.
Sourcepub const unsafe fn read_timestamp_s(&self, idx: usize) -> i64
pub const unsafe fn read_timestamp_s(&self, idx: usize) -> i64
Reads a TIMESTAMP_S value at row idx, as seconds since the epoch.
§Safety
idxmust be less thanself.row_count().- The column must contain
TIMESTAMP_Sdata.
Sourcepub const unsafe fn read_timestamp_ms(&self, idx: usize) -> i64
pub const unsafe fn read_timestamp_ms(&self, idx: usize) -> i64
Reads a TIMESTAMP_MS value at row idx, as milliseconds since the
epoch.
§Safety
idxmust be less thanself.row_count().- The column must contain
TIMESTAMP_MSdata.
Sourcepub const unsafe fn read_timestamp_ns(&self, idx: usize) -> i64
pub const unsafe fn read_timestamp_ns(&self, idx: usize) -> i64
Reads a TIMESTAMP_NS value at row idx, as nanoseconds since the
epoch.
§Safety
idxmust be less thanself.row_count().- The column must contain
TIMESTAMP_NSdata.
Sourcepub const unsafe fn read_time_tz(&self, idx: usize) -> u64
pub const unsafe fn read_time_tz(&self, idx: usize) -> u64
Reads a TIME WITH TIME ZONE value at row idx as DuckDB’s packed
64-bit representation.
Decode it with
datetime::time_tz_from_bits.
§Safety
idxmust be less thanself.row_count().- The column must contain
TIMETZdata.
Sourcepub const unsafe fn read_decimal(&self, idx: usize, width: u8) -> i128
pub const unsafe fn read_decimal(&self, idx: usize, width: u8) -> i128
Reads a DECIMAL value at row idx as its unscaled integer.
DuckDB stores a DECIMAL in the narrowest integer that fits its
declared width — i16 up to 4 digits, i32 up to 9, i64 up to 18, and
i128 up to 38 — so width must be the column’s declared width. Get it
from LogicalType::decimal_width.
The represented number is result / 10^scale.
§Safety
idxmust be less thanself.row_count().- The column must contain
DECIMALdata with exactly thiswidth.
Sourcepub const fn contains(&self, idx: usize) -> bool
pub const fn contains(&self, idx: usize) -> bool
Returns true if idx addresses a row of this vector.
Every read_* method requires idx < row_count(); this is the check to
pair with them when the index comes from somewhere other than a
0..row_count() loop.
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.
The bytes are returned without UTF-8 validation.
§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_uuid(&self, idx: usize) -> u128
pub const unsafe fn read_uuid(&self, idx: usize) -> u128
Reads a UUID value at row idx as an i128.
Reads the UUID’s textual 128 bits — the value the column renders,
and what every Rust Uuid type holds.
A UUID column is physically a HUGEINT, but DuckDB stores it with
the top bit flipped so that signed integer ordering matches UUID string
ordering, so the raw storage of
'11111111-2222-3333-4444-555555555555' is 0x9111..., not 0x1111....
This undoes that. Use read_i128 for the raw storage,
and uuid_from_storage to convert.
§Safety
idxmust be less thanself.row_count().- The column must contain
UUIDdata.
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.