ACCUMULATE
functionsupportedCore SQL functionsReturns a custom aggregate value computed by four user-defined SQL lambda functions: initialize, accumulate, combine, and terminate. ACCUMULATE follows the map-reduce aggregation model and integrates with GROUP BY, HAVING, and subqueries the same way built-in aggregates do. ACCUMULATE is especially useful for prototyping and one-off aggregations that no built-in aggregate covers. For performance-sensitive workloads, prefer built-in aggregates when possible, or consider whether a combination of built-in aggregates and joins can achieve the same result. ACCUMULATE with OBJECT, ARRAY, or VARIANT state types carries additional performance overhead.
Syntax
ACCUMULATE(
<input_expr>,
<initialize_lambda>,
<accumulate_lambda>,
<combine_lambda>,
<terminate_lambda>
)
Parameters
| Name | Required | Description |
|---|---|---|
input_expr | required | Expression evaluated once per non-NULL input row. The resulting value is passed to each lambda as the input value. |
initialize_lambda | required | Lambda with signature (value) -> <state_expr>. Called once per non-NULL input row to produce the initial partial state from that row’s value. |
accumulate_lambda | required | Lambda with signature (state, value) -> <state_expr>. Folds a new input value into an existing partial state and returns the updated state. |
combine_lambda | required | Lambda with signature (state1, state2) -> <state_expr>. Merges two partial states produced by parallel workers. Must be associative. |
terminate_lambda | required | Lambda with signature (state) -> <output_expr>. Converts the final merged state into the result value returned to the query. |
Returns
Returns the value produced by terminate_lambda. The data type matches the return type of terminate_lambda. Returns NULL if all input rows are NULL or the input set is empty.
Usage notes
- NULL input rows are silently skipped before any lambda is called, consistent with standard SQL aggregate behavior. If all input rows are NULL or the input set is empty, the result is NULL.
- ACCUMULATE has no persistent form. There’s no CREATE AGGREGATE FUNCTION or equivalent DDL. To reuse an aggregation, wrap it in a view, CTE, or stored procedure.
- Lambdas may only reference their declared parameters. References to columns from the outer query aren’t allowed. To include an outer-query value, project it into the input expression or precompute it in a CTE. The following example causes a compilation error because column2 is not a lambda parameter: Copy code -- ERROR: column2 is a column reference, not a lambda parameter. SELECT ACCUMULATE( column1, (v INT) -> v + column2, ... ) FROM t;
- The following function classes aren’t allowed inside lambdas: Function class Examples Aggregate functions SUM, AVG, COUNT Window functions ROW_NUMBER() OVER (…) Non-deterministic functions RANDOM(), UUID_STRING()ExpandShow lessSee more
Type inference
Lambda argument types are optional. When omitted, types are inferred from the input expression and propagated through the lambda chain. - Explicit types aren’t coerced across lambdas. If you annotate a type on any lambda argument, that annotation is authoritative for the state type at that position. If annotations across the four lambdas are inconsistent and can’t be reconciled (for example, initialize returns ARRAY but accumulate declares the state as INT), compilation fails. - You can omit all type annotations, or annotate only some arguments; the compiler infers and widens types automatically. Mixing annotated and unannotated arguments is allowed as long as the explicit annotations are consistent. - The state type (the type flowing between initialize, accumulate, and combine) and the output type (the return type of terminate) are tracked independently and may differ. - The input expression is implicitly cast to the type expected by the initialize lambda’s value argument. For example, if the input column is INT and value is declared STRING, the cast is applied automatically.