Skip to main content

quack_rs/scalar/
mod.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2026 Tom F. <https://github.com/tomtom215/>
3// My way of giving something small back to the open source community
4// and encouraging more Rust development!
5
6//! Builder for registering `DuckDB` scalar functions.
7//!
8//! Scalar functions process one data chunk at a time and return a single value
9//! per row. They are the most common type of function in `DuckDB` extensions.
10//!
11//! Both [`ScalarFunctionBuilder`] and [`ScalarFunctionSetBuilder`] support:
12//! - Simple parameter types via `param(TypeId)`
13//! - Complex parameterized types via `param_logical(LogicalType)` (e.g.,
14//!   `LIST(BIGINT)`, `MAP(VARCHAR, INTEGER)`, `STRUCT(...)`)
15//! - Complex return types via `returns_logical(LogicalType)`
16//! - Per-function NULL handling via `null_handling(NullHandling)`
17//!
18//! # Example
19//!
20//! ```rust,no_run
21//! use quack_rs::scalar::ScalarFunctionBuilder;
22//! use quack_rs::types::TypeId;
23//! use libduckdb_sys::{duckdb_connection, duckdb_function_info, duckdb_data_chunk,
24//!                     duckdb_vector};
25//!
26//! unsafe extern "C" fn my_func(
27//!     _info: duckdb_function_info,
28//!     _input: duckdb_data_chunk,
29//!     _output: duckdb_vector,
30//! ) {}
31//!
32//! // fn register(con: duckdb_connection) -> Result<(), quack_rs::error::ExtensionError> {
33//! //     unsafe {
34//! //         ScalarFunctionBuilder::new("double_it")
35//! //             .param(TypeId::BigInt)
36//! //             .returns(TypeId::BigInt)
37//! //             .function(my_func)
38//! //             .register(con)
39//! //     }
40//! // }
41//! ```
42
43pub mod builder;
44pub mod info;
45/// Typed bind data and per-thread local state for scalar functions.
46#[cfg(feature = "duckdb-1-5")]
47pub mod state;
48pub mod typed;
49mod typed_builder;
50
51pub use builder::{ScalarFunctionBuilder, ScalarFunctionSetBuilder, ScalarOverloadBuilder};
52// The callback signature aliases belong at the module root alongside the
53// builders that consume them — `table` already re-exports `ScanFn` this way.
54pub use builder::ScalarFn;
55#[cfg(feature = "duckdb-1-5")]
56pub use builder::{ScalarBindFn, ScalarInitFn};
57pub use info::ScalarFunctionInfo;
58#[cfg(feature = "duckdb-1-5")]
59pub use info::{RawScalarBindInfo, RawScalarInitInfo, ScalarBindInfo, ScalarInitInfo};
60#[cfg(feature = "duckdb-1-5")]
61pub use state::{ScalarBindData, ScalarLocalState};
62pub use typed::{ScalarOut, ScalarValue};
63pub use typed_builder::TypedScalarFunctionBuilder;