Skip to main content

Description

The MergeState combinator can be applied to the avg function to merge partial aggregate states of type AverageFunction(avg, T) and return a new intermediate aggregation state.

Example usage

The MergeState combinator is particularly useful for multi-level aggregation scenarios where you want to combine pre-aggregated states and maintain them as states (rather than finalizing them) for further processing. To illustrate, weโ€™ll look at an example in which we transform individual server performance metrics into hierarchical aggregations across multiple levels: Server level โ†’ Region level โ†’ Datacenter level. First we create a table to store the raw data:
Weโ€™ll create a server-level aggregation target table and define an Incremental materialized view acting as an insert trigger to it:
Weโ€™ll do the same for the regional and datacenter levels:
Weโ€™ll then insert sample raw data into the source table:
Weโ€™ll write three queries for each of the levels:
We can insert more data:
Letโ€™s check the datacenter-level performance again. Notice how the entire aggregation chain updated automatically:

See also

Last modified on July 3, 2026