Oracle CQL Queries and Oracle Data Cartridges

Pattern Recognition With MATCH_RECOGNIZE 19-3 ■ Section 19.12, MATCH_RECOGNIZE Examples

19.1.1 MATCH_RECOGNIZE and the WHERE Clause

In Oracle CQL as in SQL, the FROM clause is evaluated before the WHERE clause. Consider the following Oracle CQL query: SELECT ... FROM S MATCH_RECOGNIZE .... as T WHERE ... In this query, the S MATCH_RECOGNIZE .... as T is like a subquery in the FROM clause and is evaluated first, before the WHERE clause. Consequently, you rarely use both a MATCH_RECOGNIZE clause and a WHERE clause in the same Oracle CQL query. Instead, you typically use a view to apply the required WHERE clause to a stream and then select from the view in a query that applies the MATCH_RECOGNIZE clause. Example 19–2 shows two views, e1p1 and e2p2, each applying a WHERE clause to stream S to pre-filter the stream for the required events. The query q then selects from these two views and applies the MATCH_RECOGNIZE on this filtered stream of events. Example 19–2 MATCH_RECOGNIZE and the WHERE Clause view id=e1p1 SELECT FROM S WHERE eventName = E1 and path = P1 and statName = countValue view view id=e2p2 SELECT FROM S WHERE eventName = E2 and path = P2 and statName = countValue view query id=q SELECT T.e1p1Stat as e1p1Stat, T.e2p2Stat as e2p2Stat FROM e1p1, e2p2 MATCH_RECOGNIZE ALL MATCHES PATTERNA+ DURATION 60 MINUTES DEFINE A as A.e1p1Stat 1000 and A.e2p2Stat 2000 and countA 3 as T query For more information, see opt_where_clause::= on page 20-4

19.1.2 Referencing Singleton and Group Matches

The MATCH_RECOGNIZE clause identifies the following types of matches: ■ singleton: a correlation variable is a singleton if it occurs exactly once in a pattern, is not defined by a SUBSET, is not in the scope of an alternation, and is not quantified by a pattern quantifier. References to such a correlation variable refer to this single event. ■ group: a correlation variable is a group if it occurs in more than one pattern, is defined by a SUBSET, is in the scope of an alternation, or is quantified by a pattern quantifier. References to such a correlation variable refer to this group of events. 19-4 Oracle Complex Event Processing CQL Language Reference When you reference singleton and group correlation variables in the MEASURES and DEFINE clauses, observe the following rules: ■ For singleton correlation variables, you may reference individual event attributes only, not aggregates. ■ For group correlation variables: – If you reference an individual event attribute, then the value of the last event to match the correlation variable is returned. If the correlation variable is not yet matched, NULL is returned. In the case of countA., if the correlation variable A is not yet matched, 0 is returned. If the correlation variable is being referenced in a definition of the same variable such as DEFINE A as A.balance 1000, then the value of the current event is returned. – If you reference an aggregate, then the aggregate is performed over all events that have matched the correlation variable so far. For more information, see: ■ Section 19.1.3.5, Using count With , identifier., and identifier.attr ■ Section 19.3.1, Pattern Quantifiers and Regular Expressions ■ Section 19.4.2, Referencing Attributes in the DEFINE Clause

19.1.3 Referencing Aggregates

You can use any built-in, Colt, or user-defined aggregate function in the MEASURES and DEFINE clause of a MATCH_RECOGNIZE query. When using aggregate functions, consider the following: ■ Section 19.1.3.1, Running Aggregates and Final Aggregates ■ Section 19.1.3.2, Operating on the Same Correlation Variable ■ Section 19.1.3.3, Referencing Variables That Have not Been Matched Yet ■ Section 19.1.3.4, Referencing Attributes not Qualified by Correlation Variable For more information, see: ■ Section 19.1.3.5, Using count With , identifier., and identifier.attr ■ Section 19.1.3.6, Using first and last ■ Section 9.1, Introduction to Oracle CQL Built-In Aggregate Functions ■ Section 11.1, Introduction to Oracle CQL Built-In Aggregate Colt Functions ■ Section 13.1.1.2, User-Defined Aggregate Functions ■ Section 19.2, MEASURES Clause ■ Section 19.4, DEFINE Clause

19.1.3.1 Running Aggregates and Final Aggregates

In the DEFINE clause, any aggregate function on a correlation variable X is a running aggregate: that is, the aggregate includes all preceding matches of X up to and including the current match. If the correlation variable X has been completely matched so far, then the aggregate is final, otherwise it is running. Pattern Recognition With MATCH_RECOGNIZE 19-5 In the MEASURES clause, because it is evaluated after the match has been found, all aggregates are final because they are computed over the final match. When using a SUBSET clause, be aware of the fact that you may inadvertently imply a running aggregate as Example 19–3 shows. Example 19–3 Implied Running Aggregate ... PATTERN X+ Y+ SUBSET Z = X, Y DEFINE X AS X.price 100, Y AS sumZ.price 1000 ... Because correlation variable Z involves Y, the definition of Y involves a running aggregate on Y. For more information, see: ■ Section 19.2, MEASURES Clause ■ Section 19.4, DEFINE Clause ■ Section 19.11, SUBSET Clause

19.1.3.2 Operating on the Same Correlation Variable

In both the MEASURES and DEFINE clause, you may only apply an aggregate function to attributes of the same correlation variable. For example: the use of aggregate function correlation in Example 19–4 is invalid. Example 19–4 Invalid Use of Aggregate Function ... MEASURES xycorr AS correlationX.price, Y.price PATTERN X+ Y+ DEFINE X AS X.price = 10, Y AS Y.price 10 ... The correlation aggregate function may not operate on more than one correlation variable.

19.1.3.3 Referencing Variables That Have not Been Matched Yet

In the DEFINE clause, you may reference a correlation variable that has not been matched yet. However, you should use caution when doing so. Consider Example 19–5 . Example 19–5 Referencing a Variable That has not Been Matched Yet: Invalid PATTERN X+ Y+ DEFINE X AS countY. = 3 Y AS Y.price 10,