sumSimpleState
Description
The SimpleState
combinator can be applied to the sum
function to return the sum across all input values. It returns the result with
type SimpleAggregateFunction
.
Example Usage
Tracking upvotes and downvotes
Let's look at a practical example using a table that tracks votes on posts.
For each post, we want to maintain running totals of upvotes, downvotes, and an
overall score. Using the SimpleAggregateFunction
type with sum is suited for
this use case as we only need to store the running totals, not the entire state
of the aggregation. As a result, it will be faster and will not require merging
of partial aggregate states.
First, we create a table for the raw data:
Next, we create a target table which will store the aggregated data:
We then create a Materialized View with SimpleAggregateFunction
type columns:
Insert sample data:
Query the Materialized View using the SimpleState
combinator: