Skip to main content
Requires feature = "udf". See the UDF overview for shared semantics.
#[duckdb_table_function] turns a Rust function into a DuckDB table function — used in a FROM clause. It returns impl Iterator<Item = T> + Send: T for one column, or a tuple (A, B, …) for several. DuckDB pulls rows in chunks, so the iterator is driven lazily and may be scanned across several calls.
The return type must be impl Iterator<Item = T> + Send (optionally wrapped in Result<_, E>). Missing the Send bound, or returning something other than an impl Iterator, is a compile error. The Send bound is required because DuckDB may scan across worker threads.

Options

schema = "…" is intentionally rejected — DuckDB’s C API cannot register a function into a schema. Register it, then move it with SQL.

Named parameters

A parameter listed in named_params is bound as name := … in SQL. A non-Option named parameter is required; an Option<T> one is optional:

Projection pushdown and extra info — body macros

Inside the body:
  • duck_projection!() -> Vec<usize> — the 0-based column indices the current query needs (empty if no pushdown was requested; only meaningful under projection_pushdown).
  • duck_extra_info!(Type) -> Type — a Clone of the registered extra-info value.
Both panic if called in the wrong context, but that panic is contained by the callback machinery and surfaces as a normal query error, never UB. See helper macros.

The VTab trait

The macro implements better_duck_core::udf::VTab. Implement it by hand for stateful scans:
bind defines the output schema via BindInfo::add_result_column and may set_cardinality; init produces per-query state and may set_max_threads and read column_indices (projection); func writes up to output.capacity() rows then calls output.set_len(k), signalling completion with set_len(0). Register with conn.register_table_function::<T>(name), register_table_function_ext::<T>(name) (adds a per-worker VTabLocalInit::local_init), or register_table_function_with_extra_info::<T, E>(name, extra).

See also