avgState
Description
The State combinator
can be applied to the avg
function to produce an intermediate state of AggregateFunction(avg, T) type where
T is the specified type for the average.
Example Usage
In this example, we'll look at how we can use the AggregateFunction type,
together with the avgState function to aggregate website traffic data.
First create the source table for website traffic data:
Create the aggregate table that will store average response times. Note that
avg cannot use the SimpleAggregateFunction type as it requires a complex
state (a sum and a count). We therefore use the AggregateFunction type:
Create an Incremental Materialized View that will act as an insert trigger to new data and store the intermediate state data in the target table defined above:
Insert some initial data into the source table, creating a part on disk:
Insert some more data to create a second part on disk:
Examine the target table page_performance:
Notice that the avg_response_time column is of type AggregateFunction(avg, UInt32)
and stores intermediate state information. Also notice that the row data for the
avg_response_time is not useful to us and we see strange text characters such
as �, n, F, }. This is the terminals attempt to display binary data as text.
The reason for this is that AggregateFunction types store their state in a
binary format that's optimized for efficient storage and computation, not for
human readability. This binary state contains all the information needed to
calculate the average.
To make use of it, use the Merge combinator:
Now we see the correct averages: