Skip to main content

read_duck_string

Function read_duck_string 

Source
pub unsafe fn read_duck_string<'a>(data: *const u8, idx: usize) -> &'a str
Expand description

Reads a DuckDB VARCHAR value from a raw vector data pointer at a given row index.

Returns the string as a &str slice, or an empty string if the data is not valid UTF-8 or if the pointer is null.

§Pitfall P7

DuckDB strings have two storage formats:

  • Inline (≤ 12 bytes): stored directly in the 16-byte struct
  • Pointer (> 12 bytes): struct contains a pointer to heap-allocated data

This function handles both transparently.

§Safety

  • data must point to a DuckDB VARCHAR vector’s data buffer.
  • idx must be within bounds of the vector.
  • For pointer-format strings, the heap data pointed to must be valid for the duration of this function call and the returned &str slice.
  • The returned &str borrows from the DuckDB vector — do not destroy the data chunk while the returned reference is live.

§Example

use quack_rs::vector::string::read_duck_string;

// Inside a DuckDB aggregate callback:
// let data = libduckdb_sys::duckdb_vector_get_data(vec) as *const u8;
// let s = unsafe { read_duck_string(data, row_idx) };