Skip to main content
Requires feature = "udf". See the UDF overview for shared semantics.
#[duckdb_cast] is applied to a one-argument fn(source) -> target (or -> Result<target, E>). The source and target logical types are inferred from the signature. Casts are unnamed, so there is no name option.
Register with parse_to_int::register(&mut conn)?. Then:
The same body serves both CAST and TRY_CAST. Under TRY_CAST, a conversion error (an Err return) sets that output row to NULL; under a normal CAST, it fails the query with the error’s message.

Options

A lower non-negative implicit_cost makes DuckDB more willing to apply the cast implicitly (without an explicit CAST). implicit_cost must be an integer literal (optionally negative).

The VCast trait

The macro implements better_duck_core::udf::VCast:
Register with conn.register_cast_function::<C>() or register_cast_function_with_state::<C>(shared). The same cast_row body serves CAST (a conversion error fails the query) and TRY_CAST (a conversion error yields NULL for that row). Work over VectorRef / VectorMut.

See also