The COMBINE function

The COMBINE function merges one partial result with another partial result and returns the updated partial result. For example, for the AVG aggregate, the COMBINE function adds the two partial results and adds the two partial counts.

If the aggregate is derived from a simple binary operator whose result type is the same as the state type and the column type, the COMBINE function can be the same as the ITER function. For example, for the AVG aggregate, the COMBINE function adds the current sum and the row count of one partial result to the same values for another partial result and returns the new values.

The database server uses the COMBINE function for parallel execution. When a query includes an aggregate, the database server uses parallel execution when the query includes only aggregates. However, the COMBINE function might also be used even when a query is not parallelized. For example, when a query contains both distinct and nondistinct aggregates, the database server can decompose the computation of the nondistinct aggregate into subaggregates based on the distinct column values. Therefore, you must provide a COMBINE function for each user-defined aggregate.

Parallel aggregation must give the same results as an aggregate that is not computed in parallel. You must write the COMBINE function so that the result of aggregating over a set of rows is the same as aggregating over two partitions of the set separately and then combining the results.

In C and Java™, the COMBINE function can perform cleanup work to release resources that the INIT function allocated. However, it must not free the state arguments.