Skip to main content
Get startedGitHub

DuckDB's C types never appear in your code. You write ordinary Rust — Option, Vec, derived structs and enums — and the attribute writes the rest.

Depend on the headers, not on the library: the extension dispatches through DuckDB's API table at load time, so nothing is linked and cross-compilation works.

No unsafe functions and no raw pointers inside your function bodies. The only unsafe left is in explicit manual registration.

Annotating a function is enough. The registration items are collected for you and applied when DuckDB loads the extension.

A panic inside a function body is caught and reported as a DuckDB error instead of unwinding across the FFI boundary.

Vec, IndexMap, fixed-size arrays, STRUCTs and any nesting of them map to DuckDB's LIST, MAP, ARRAY and STRUCT.

One Rust function = one SQL function​

The attribute generates the FFI wrapper, the column readers and writers, and the registration code. Everything on the left is safe Rust that you could have written for a plain library.

src/lib.rs
use duckfn::{
duck_error, duck_scalar_function, duckfn_entrypoint, DuckOptionResult,
};

/// The SQL below is what this function becomes, doc comment and all.
#[duck_scalar_function]
pub fn double_it(v: Option<i64>) -> DuckOptionResult<i64> {
if v == Some(13) {
return Err(duck_error("unlucky input"));
}
Ok(v.map(|x| x * 2))
}

// The symbol DuckDB looks for when it loads the extension.
duckfn_entrypoint!("my_ext");
duckdb -unsigned
-- the extension loads like any other
LOAD './my_ext.duckdb_extension';

SELECT double_it(21); -- 42
SELECT double_it(NULL); -- NULL
SELECT double_it(13); -- error: unlucky input

The comments are what each call returns. Loading needs -unsigned, because the extension talks to DuckDB's C API.

Write, build and load your first extension.

Every attribute, argument and registration kind, with examples.

One extension that uses every feature duckfn offers.

From DuckDB loading the library to your function answering a query.