pub struct CastFunctionBuilder { /* private fields */ }Expand description
Builder for registering a custom DuckDB cast function.
A cast function converts values from a source type to a target type.
Registering a cast lets DuckDB use it both for explicit
CAST(x AS Target) syntax and (if an implicit cost is set) for automatic
coercions.
§Example
use quack_rs::cast::{CastFunctionBuilder, CastFunctionInfo, CastMode};
use quack_rs::types::TypeId;
use libduckdb_sys::{duckdb_function_info, duckdb_vector, idx_t};
unsafe extern "C" fn my_cast(
_info: duckdb_function_info,
_count: idx_t,
_input: duckdb_vector,
_output: duckdb_vector,
) -> bool {
true // implement real conversion here
}
// fn register(con: libduckdb_sys::duckdb_connection)
// -> Result<(), quack_rs::error::ExtensionError>
// {
// unsafe {
// CastFunctionBuilder::new(TypeId::Varchar, TypeId::Integer)
// .function(my_cast)
// .register(con)
// }
// }Implementations§
Source§impl CastFunctionBuilder
impl CastFunctionBuilder
Sourcepub const fn new(source: TypeId, target: TypeId) -> Self
pub const fn new(source: TypeId, target: TypeId) -> Self
Creates a new builder that will cast source values into target values.
Sourcepub fn new_logical(source: LogicalType, target: LogicalType) -> Self
pub fn new_logical(source: LogicalType, target: LogicalType) -> Self
Creates a new builder using LogicalTypes for source and target.
Use this when the source or target types are complex (e.g.
DECIMAL(18, 3), LIST(VARCHAR), etc.) and cannot be expressed as
simple TypeId values.
Sourcepub const fn source(&self) -> Option<TypeId>
pub const fn source(&self) -> Option<TypeId>
Returns the source type this cast converts from (if set via new).
Returns None if the source was set via new_logical.
Useful for introspection and for MockRegistrar.
Sourcepub const fn target(&self) -> Option<TypeId>
pub const fn target(&self) -> Option<TypeId>
Returns the target type this cast converts to (if set via new).
Returns None if the target was set via new_logical.
Useful for introspection and for MockRegistrar.
Sourcepub const fn implicit_cost(self, cost: i64) -> Self
pub const fn implicit_cost(self, cost: i64) -> Self
Sets the implicit cast cost.
When a non-negative cost is provided, DuckDB may use this cast
automatically in expressions where an implicit coercion is needed.
Lower cost means higher priority. A negative cost or omitting this
method makes the cast explicit-only.
Sourcepub unsafe fn extra_info(
self,
ptr: *mut c_void,
destroy: duckdb_delete_callback_t,
) -> Self
pub unsafe fn extra_info( self, ptr: *mut c_void, destroy: duckdb_delete_callback_t, ) -> Self
Attaches extra data to the cast function.
The pointer is available inside the callback via
CastFunctionInfo::get_extra_info.
§Safety
ptr must remain valid until DuckDB calls destroy, or for the
lifetime of the database if destroy is None.