Skip to main content

quack_rs/aggregate/builder/
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 types for registering `DuckDB` aggregate functions.
7//!
8//! # Pitfall L6: Function set name must be set on EACH member
9//!
10//! When using `duckdb_register_aggregate_function_set`, the function name must be
11//! set on **each individual function** added to the set, not just on the set itself.
12//! If you call `duckdb_aggregate_function_set_name` only on the set (or forget to
13//! call it on an individual function), `DuckDB` silently returns `DuckDBError` at
14//! registration time, and the function is never registered.
15//!
16//! [`AggregateFunctionSetBuilder`] enforces this by calling the name-setter
17//! internally for every function added to the set.
18
19mod collision;
20mod overload;
21mod set;
22mod single;
23
24#[cfg(test)]
25mod tests;
26
27pub use overload::AggregateOverloadBuilder;
28pub use set::AggregateFunctionSetBuilder;
29pub use single::AggregateFunctionBuilder;
30
31use crate::error::ExtensionError;
32
33/// The refusal of an aggregate that takes no parameter, for `what` (the
34/// function, or one overload of a set).
35///
36/// The C API gives `update` a chunk and the state array and no row count: the
37/// rows are `chunk.size()`. `DuckDB` 2.0 stopped setting the chunk's cardinality
38/// (`CAPIAggregateUpdate`, `aggregate_function-c.cpp`), and a chunk with no
39/// column reports a size of 0, so `SELECT f() FROM t` over 100 rows answers
40/// 100 on `DuckDB` 1.5.6 and 0 on 2.0.0 (checked against both). A windowed call
41/// of such an aggregate never reaches `update` on either. A silent wrong
42/// answer is worse than a refusal.
43pub(super) fn zero_parameter_error(what: &str, name: &str) -> ExtensionError {
44    ExtensionError::new(format!(
45        "{what} takes no parameter: DuckDB 2.0 passes `update` a chunk with no columns, \
46         which reports a size of 0 and has no row count, so the aggregate would count \
47         no rows (and a windowed call never reaches `update` on any version). Give it \
48         one parameter, and for a zero-argument SQL spelling register a macro in front \
49         of it: `CREATE MACRO {name}() AS {name}_1(1)`"
50    ))
51}
52
53/// Former name of [`AggregateOverloadBuilder`].
54///
55/// Renamed for symmetry with [`ScalarOverloadBuilder`][crate::scalar::ScalarOverloadBuilder];
56/// the two now read the same way at a call site.
57#[deprecated(
58    since = "0.18.0",
59    note = "renamed to `AggregateOverloadBuilder` for symmetry with `ScalarOverloadBuilder`"
60)]
61pub type OverloadBuilder = AggregateOverloadBuilder;