Skip to main content
DuckDB’s aggregate functions, exposed as free functions (defined with #[aggregate]) in better_duck_diesel::dsl::*. Use them in a grouped or aggregating .select.
Diesel core already ships count/sum/avg/min/max (in diesel::dsl) — this crate adds only what core lacks. Most aggregates return Nullable<…> because an empty group yields NULL; load into an Option.
Watch DuckDB’s result widening on the core aggregates: SUM over an INTEGER column widens to HUGEINT, which the BigInt adapter rejects. Sum a DOUBLE column, or CAST the result. See Select → counting.

General

array_agg returns a DuckList, which the typed loader cannot read — route it through diesel::sql_query + QueryableByName. See Type mapping.

Statistical

Double -> Nullable<Double> unless noted:
stddev_samp, stddev_pop, var_samp, var_pop, median, skewness, kurtosis, entropy, mad, mode.
corr(y, x), covar_samp(y, x), covar_pop(y, x), regr_slope(y, x), regr_intercept(y, x), regr_r2(y, x), regr_count(y, x) (→Nullable<BigInt>).

Approximate & quantiles

Bitwise

BigInt -> Nullable<BigInt>: bit_and(x), bit_or(x), bit_xor(x).

JSON

json_group_array(x: Text) (→Text) aggregates values into a JSON array; json_group_object(key, value) (both Text, →Text) aggregates key/value pairs into a JSON object. See JSON functions for the scalar side.

Custom aggregate UDFs

Your own aggregates registered with #[duckdb_aggregate] are callable the same way once declared with define_sql_function! + #[aggregate]. See Calling your own UDFs and the UDF macros.

See also

Select

Grouped selects and GROUP BY.

JSON functions

Scalar JSON helpers.

Calling your own UDFs

Register custom aggregates.

Type mapping

Reading DuckList aggregate results.