/better-duck-skills:better-duck-macrosfrom better-duck-skills
Proc-macros for DuckDB user-defined functions and data derives in Rust. Use when writing scalar/table/aggregate UDFs or deriving FromRow/ToRow/DuckEnum/DuckStruct for DuckDB row mapping.
Ships in better-duck-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
better-duck-macros is the proc-macro engine behind two better-duck capabilities: user-defined
functions (register plain Rust fns as DuckDB scalar / table / aggregate / cast functions) and
data derives (map Rust structs and enums to DuckDB rows, STRUCTs, and ENUMs). Neither needs
unsafe or manual vector handling.
You do not depend on
better-duck-macrosdirectly. The macros are re-exported through better-duck-core'sudfandderivefeatures (and better-duck-diesel'sderivefeature for ORMFromSql/ToSqlemission). Import everything frombetter_duck_core::….
[dependencies]
better-duck-core = { version = "0.1.0-beta.8", features = ["udf", "derive"] }
For the DuckDB engine semantics these compile against see the duckdb skill; for the driver API
(connections, DuckValue, appender, columnar) see better-duck-core; this skill is the overview
and router for the macros themselves.
A scalar UDF (feature udf) — parameter/return types inferred from the signature, registered on a
Connection:
use better_duck_core::{connection::Connection, duckdb_scalar};
#[duckdb_scalar]
fn repeat_str(s: &str, n: i32) -> String {
s.repeat(n.max(0) as usize)
}
# fn main() -> better_duck_core::error::Result<()> {
let mut conn = Connection::open_in_memory()?;
repeat_str::register(&mut conn)?; // generated associated fn
conn.execute("SELECT repeat_str('ab', 3)")?; // "ababab"
# Ok(())
# }
A row-mapping derive (feature derive) — read a result straight into a Vec<T>:
use better_duck_core::FromRow;
#[derive(FromRow, Debug)]
struct Task { id: i32, name: String }
# fn demo(conn: &mut better_duck_core::connection::Connection) -> better_duck_core::error::Result<()> {
let tasks: Vec<Task> = conn.execute("SELECT id, name FROM tasks")?.materialize()?.to_structs()?;
# Ok(())
# }
User-defined functions (feature udf) — details in references/udf-macros.md:
| Macro | Applies to | Becomes |
|---|---|---|
#[duckdb_scalar] | fn(args) -> T | a scalar function (one value per row, for SELECT/WHERE) |
#[duckdb_table_function] | fn(args) -> impl Iterator<Item = T> + Send | a table function (rows + columns, usable in FROM) |
#[duckdb_aggregate] | mod { init, update, combine, finalize } | a custom aggregate |
#[duckdb_cast] | fn(source) -> target | a CAST / TRY_CAST implementation |
#[duckdb_entrypoint] | fn(&mut Connection) -> Result<…> | a loadable-extension C entrypoint (experimental) |
Each UDF macro generates a register(&mut Connection) associated item. Option<T> propagates
NULL, Result<T, E> fails the query with E's message, and panics are contained only under
panic = "unwind" — see the panic caveat in the reference.
Data derives + helper macros (feature derive) — details in references/derives.md:
| Item | Maps / does | Direction |
|---|---|---|
#[derive(FromRow)] | struct ← query row (by column name) | read |
#[derive(ToRow)] | struct → appender row / bound params (declaration order) | write |
#[derive(DuckEnum)] | unit enum ↔ DuckDB ENUM | read + write |
#[derive(DuckStruct)] | struct ↔ DuckDB STRUCT value | read + write |
transaction!(conn, { … }) | commit-on-Ok / rollback-on-Err-or-panic scope | — |
params![a, b, …] | builds the &mut [&mut dyn AppendAble] slice for execute_with | — |
Field/variant naming is controlled by #[duck(rename / rename_all)] (and #[duck_enum(rename / rename_all)] for DuckEnum); a #[duck(crate = ::path)] / #[duck_enum(crate = ::path)] escape
hatch retargets the generated ::better_duck_core path.
name, columns,
named_params, projection_pushdown, state/duck_state!, extra_info/duck_extra_info!,
volatile, implicit_cost), the VAggregate model, custom casts, loadable extensions, or the
panic / panic = "abort" caveat → references/udf-macros.md.#[duck(...)] / #[duck_enum(...)] attributes, the
rename_all casings, Diesel emission, or the transaction! / params! helpers →
references/derives.md.SELECT/WHERE → #[duckdb_scalar].FROM clause → #[duckdb_table_function].#[duckdb_aggregate].CAST / TRY_CAST between types → #[duckdb_cast]..duckdb_extension loaded into another DuckDB → #[duckdb_entrypoint].#[derive(FromRow)]; bulk-insert / bind structs → #[derive(ToRow)].ENUM, or a struct as a STRUCT value → #[derive(DuckEnum)] /
#[derive(DuckStruct)].What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
duckdb, user-defined-functions, rust-macros, data-derives, proc-macro
Other plugins' skills for the same job
npx claudepluginhub nimdeveloper/better-duck --plugin better-duck-skills6 skills in this plugin
/better-duckOverview and task router for the better-duck DuckDB driver for Rust. Use when working with the better-duck / better-duck-core / better-duck-macros / better-duck-diesel / better-duck-tauri crates, when choosing a Cargo feature flag, or when deciding which API or crate fits a task (opening a connection, querying, parameter binding, bulk insert, columnar access, user-defined functions, Diesel ORM, or a Tauri plugin). Routes to the per-crate skills for depth./better-duck-coreRust API guide for the better-duck-core crate (the embedded DuckDB driver published as better-duck): opening connections, executing SQL, iterating rows and values, parameter binding, bulk insert, columnar chunks, transactions, pooling, async, and Arrow/Polars interop. Use when writing Rust against better-duck-core — Connection, execute, execute_with, execute_with_named, DuckResult / DuckValue, Appender, fetch_chunk, TableDescription, Database — or when choosing a Cargo flag feature = "chrono|decimal|arrow|polars|columnar|udf|async|pool|time|uuid" (plus json / parquet / icu for bundled extensions, and derive). For DuckDB SQL and engine behavior use the duckdb skill; for the UDF and data-derive macros use better-duck-macros./better-duck-dieselThe Diesel 2.3 ORM backend for DuckDB, built on better-duck. Use when working with the `better-duck-diesel` crate: establishing a `DuckDbConnection`, defining a `table!` schema, running INSERT/SELECT/UPDATE/DELETE/RETURNING or transactions through Diesel, mapping Rust types to DuckDB column types, or reaching DuckDB-only SQL that Diesel core omits via `use better_duck_diesel::dsl::*;` (ILIKE/SIMILAR TO/GLOB/regex operators; string, numeric, regex, date/time, JSON, list and map functions; general/statistical/approximate/bit aggregates). Also covers calling your `#[duckdb_*]` UDFs through the DSL with no raw SQL (`feature = "udf"`), spatial `ST_*` functions (`feature = "spatial"`), ORM-emitting data derives (`feature = "derive"`), and r2d2 pooling. Triggers: `better-duck-diesel`, Diesel + DuckDB, `DuckDbConnection`, `better_duck_diesel::dsl`, `feature = "udf"|"derive"|"spatial"`./better-duck-tauriThe better-duck-tauri crate: a Tauri v2 plugin that exposes better-duck (embedded DuckDB) to the webview as a local analytics engine. Use when registering the plugin in `tauri::Builder`, choosing a backend Cargo feature (`feature = "backend-core"` default, `feature = "backend-diesel"`, or `feature = "arrow"`), calling the webview commands (`load`/`close`/`select`/`execute`, plus import/export/append/stream/checkpoint/revert) from JS/TS via `@better-duck/tauri`, wiring Tauri capabilities/permissions and the scope + policy security model, defining startup migrations, or configuring WAL checkpoint strategies. Triggers: "Tauri", "webview", the crate name, the command names, or the backend/arrow feature flags./duckdbDuckDB engine knowledge for driving it from Rust through better-duck: the SQL dialect, the DuckDB type system and how each type maps to driver values, extensions (json, parquet, icu, spatial), and runtime behaviors (transactions, appender semantics, named/positional parameters, replacement scans). Use when writing SQL for DuckDB, choosing or reasoning about a DuckDB column type, loading an extension, or diagnosing DuckDB-specific behavior seen through the better-duck driver.