state_agg
to create a timeline of state transitions, or to calculate the durations of states. state_agg extends the capabilities
of compact_state_agg.
state_agg is designed to work with a relatively small number of states. It might not perform well on datasets where
states are mostly distinct between rows.
Because state_agg tracks more information, it uses more memory than compact_state_agg. If you want to minimize
memory use and don’t need to query the timestamps of state transitions, consider using
compact_state_agg instead.
Two-step aggregation
This group of functions uses the two-step aggregation pattern. Rather than calculating the final result in one step, you first create an intermediate aggregate by using the aggregate function. Then, use any of the accessors on the intermediate aggregate to calculate a final result. You can also roll up multiple intermediate aggregates with the rollup functions. The two-step aggregation pattern has several advantages:- More efficient because multiple accessors can reuse the same aggregate
- Easier to reason about performance, because aggregation is separate from final computation
- Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
- Can perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result
Functions in this group
Aggregate
state_agg(): aggregate state data into an intermediate form for further computation
Accessors
state_at(): get the state at a given timeduration_in(): get the total duration in the specified statesinterpolated_duration_in(): get the total duration in the specified states, interpolating values at the boundarystate_periods(): get an array of periods for each statestate_timeline(): get a timeline of state changesinterpolated_state_periods(): get an array of periods for each state, interpolating values at the boundaryinterpolated_state_timeline(): get a timeline of state changes, interpolating values at the boundaryinto_values(): return an array of(state, duration)pairs from the aggregate
Rollup
rollup(): combine multiple intermediate aggregates