ChronusQLAdvanced analysis

Pattern recognition

Detect ordered event sequences, assign semantic roles, and emit one measured result for each successful match.

Advanced analysis

12 min read

Language reference

Turn event order into a result

Execution sketchA followed by one or more B events
DEFINE AA
DEFINE BB #1
DEFINE BB #2
MEASURESMatch
Definitions classify records, the pattern controls order and repetition, and measures shape the result.
Increasing sequenceChronusQL
SELECT mr.run_id, mr.start_price, mr.end_price
INTO output
FROM input TIMESTAMP BY ts
MATCH_RECOGNIZE (
  PARTITION BY run_id
  LIMIT Duration(second, 10)
  MEASURES
    A.run_id AS run_id,
    A.price AS start_price,
    LAST(B.price) AS end_price
  ONE ROW PER MATCH
  AFTER MATCH SKIP TO NEXT ROW
  PATTERN (A B+)
  DEFINE
    A AS A.kind = 'A',
    B AS B.kind = 'B' AND B.price > PREV(B.price)
) AS mr

Clause responsibilities

  1. 01
    PARTITION BY

    Keeps pattern state independent for each key.

  2. 02
    LIMIT

    Bounds the elapsed event time of a candidate match.

  3. 03
    MEASURES

    Names values projected from a successful match.

  4. 04
    ONE ROW PER MATCH

    Emits one result object for the match.

  5. 05
    AFTER MATCH SKIP TO NEXT ROW

    Advances the next candidate start by one source row.

  6. 06
    PATTERN

    Declares variable order, alternatives, and repetition.

  7. 07
    DEFINE

    Declares the predicate for every named pattern variable.

Patterns, quantifiers, and navigation

ConstructMeaning
A BConcatenation: A followed by B.
A | BAlternation: either A or B.
A*, A+, A?Zero or more, one or more, optional.
A{n}, A{n,}, A{n,m}, A{,m}Counted repetition.
.Wildcard event with generic lineage but no DEFINE role.
PREV(B.value)Prior value assigned to B.
FIRST(B.value) / LAST(B.value)First or last value assigned to B.
COUNT(*) in MEASURESNumber of events captured by the successful match.