avgMergeState
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:
- Service level
- Regional level
- Datacenter level
We can insert more data:
Let's check the datacenter-level performance again. Notice how the entire aggregation chain updated automatically: