ACCUMULATE

functionsupportedCore SQL functions

docs.snowflake.com ↗

Aggregate functions

Returns 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

NameRequiredDescription
input_exprrequiredExpression evaluated once per non-NULL input row. The resulting value is passed to each lambda as the input value.
initialize_lambdarequiredLambda with signature (value) -> <state_expr>. Called once per non-NULL input row to produce the initial partial state from that row’s value.
accumulate_lambdarequiredLambda with signature (state, value) -> <state_expr>. Folds a new input value into an existing partial state and returns the updated state.
combine_lambdarequiredLambda with signature (state1, state2) -> <state_expr>. Merges two partial states produced by parallel workers. Must be associative.
terminate_lambdarequiredLambda 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

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.