Skip to main content

VScalar

Trait VScalar 

Source
pub trait VScalar: Sized {
    type State: Send + Sync + 'static;
    type BindData: Send + Sync + 'static;

    // Required methods
    fn signatures() -> Result<Vec<ScalarSignature>>;
    fn bind(bind: &ScalarBindInfo) -> UdfResult<Self::BindData>;
    fn invoke(
        state: &Self::State,
        bind_data: &Self::BindData,
        input: &DataChunkHandle,
        output: &mut VectorMut<'_>,
    ) -> UdfResult<()>;

    // Provided methods
    fn volatile() -> bool { ... }
    fn special_handling() -> bool { ... }
}
Expand description

A DuckDB scalar function: computes one value per row.

See the callback containment contract in crate::udf.

Required Associated Types§

Source

type State: Send + Sync + 'static

State set at registration time, shared across every invocation and every worker thread. Persists for the lifetime of the catalog entry, so it must be 'static; any interior mutation must be synchronized.

Source

type BindData: Send + Sync + 'static

Per-query data produced by VScalar::bind and shared, read-only, by every VScalar::invoke call for that query. Use () if the function needs none.

Required Methods§

Source

fn signatures() -> Result<Vec<ScalarSignature>>

The possible signatures of this function. Each becomes a DuckDB overload; VScalar::invoke must be able to handle every one of them.

§Errors

Returns an error if a signature’s logical type cannot be built.

Source

fn bind(bind: &ScalarBindInfo) -> UdfResult<Self::BindData>

Runs once per query that references this function, before any invoke. Inspects the call’s argument expressions (via ScalarBindInfo — e.g. to fold a constant argument or reject an unsupported one) and produces the per-query BindData. Functions needing no bind data return Ok(()) (with type BindData = ()).

§Errors

Returns an error to reject the query at bind time with that message.

Source

fn invoke( state: &Self::State, bind_data: &Self::BindData, input: &DataChunkHandle, output: &mut VectorMut<'_>, ) -> UdfResult<()>

Computes output[row] for every row in 0..input.len().

DuckDB guarantees input and output stay live for the duration of this call, and that output’s capacity is at least input.len(). bind_data is the value bind produced for this query.

§Errors

Returns an error to fail the query with that message.

Provided Methods§

Source

fn volatile() -> bool

Whether this function is volatile — re-evaluated for every row even with no parameters, rather than optimized to a constant. Needed for functions like random-number or UUID generators.

Source

fn special_handling() -> bool

Whether this function should be invoked for rows containing NULL parameters. By default DuckDB substitutes NULL as the result for any row with a NULL argument without calling VScalar::invoke at all; returning true here disables that shortcut.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§