Class MatchAggregation

java.lang.Object
io.trino.operator.window.pattern.MatchAggregation

public class MatchAggregation extends Object
This class computes an aggregate function result in row pattern recognition context.

Expressions in DEFINE and MEASURES clauses can contain aggregate functions. Each of these aggregate functions is transformed into an instance of `MatchAggregation` class.

Whenever the aggregate function needs to be evaluated , the method `aggregate()` is called. The returned value is then used to evaluate the enclosing expression.

The aggregate function needs to be evaluated in certain cases: 1. during the pattern matching phase, e.g. with a defining condition: `DEFINE A AS avg(B.x) > 0`, the aggregate function `avg` needs to be evaluated over all rows matched so far to label `B` every time the matching algorithm tries to match label `A`. 2. during row pattern measures computation, e.g. with `MEASURES M1 AS RUNNING sum(A.x)`, the running sum must be evaluated over all rows matched to label `A` up to every row included in the match; with `MEASURES M2 AS FINAL sum(A.x)`, the overall sum must be computed for rows matched to label `A` in the entire match, and the result must be propagated for every output row.

To avoid duplicate computations, `MatchAggregation` is stateful. The state consists of: - the accumulator, which holds the partial result - the setEvaluator, which determines the new positions to aggregate over since the previous call If the `MatchAggregation` instance is going to be reused for different matches, it has to be `reset` before a new match.