Class 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.

    • Method Detail

      • reset

        public void reset()
      • aggregate

        public Block aggregate​(int currentRow,
                               ArrayView matchedLabels,
                               long matchNumber,
                               ProjectingPagesWindowIndex windowIndex,
                               int partitionStart,
                               int patternStart)
        Identify the new positions for aggregation since the last time this aggregation was run, and add them to `accumulator`. Return the overall aggregation result. This method is used for: - Evaluating labels during pattern matching. In this case, the evaluated label has been appended to `matchedLabels`, - Computing row pattern measures after a non-empty match is found.
      • aggregateEmpty

        public Block aggregateEmpty()
        Aggregate over empty input. This method is used for computing row pattern measures for empty matches. According to the SQL specification, in such case: - count() aggregation should return 0, - all other aggregations should return null. In Trino, certain aggregations do not follow this pattern (e.g. count_if). This implementation is consistent with aggregations behavior in Trino.