19
Pattern Recognition With MATCH_RECOGNIZE 19-1
19
Pattern Recognition With MATCH_ RECOGNIZE
The Oracle CQL MATCH_RECOGNIZE clause and its sub-clauses perform pattern recognition in Oracle CQL queries.
19.1 Understanding Pattern Recognition With MATCH_RECOGNIZE
The MATCH_RECOGNIZE clause performs pattern recognition in an Oracle CQL query as
Example 19–1 shows. This query will export make available for inclusion in the
SELECT the MEASURES clause values for events tuples that satisfy the PATTERN clause regular expression over the DEFINE clause conditions.
Example 19–1 Pattern Matching With MATCH_RECOGNIZE
query id=detectPerish[CDATA[ select its.badItemId
from tkrfid_ItemTempStream MATCH_RECOGNIZE
PARTITION BY itemId MEASURES A.itemId as badItemId
PATTERN A B C DEFINE
A AS A.temp = 25, B AS B.temp = 25 and to_timestampB.element_time - to_timestampA.element_time INTERVAL 0
00:00:05.00 DAY TO SECOND, C AS to_timestampC.element_time - to_timestampA.element_time = INTERVAL 0 00:00:05.00 DAY TO
SECOND as its
]]query
pattern_recognition_clause::=
pattern_partition_clause::= on page 19-17,
pattern_measures_clause::= on page 19-9,
pattern_def_dur_clause::= on page 19-2
19-2 Oracle Complex Event Processing CQL Language Reference
pattern_def_dur_clause::=
pattern_clause::= on page 19-11,
pattern_skip_match_clause::= on page 19-19,
pattern_ definition_clause::=
on page 19-14, duration_clause::=
on page 19-22, subset_clause::=
on page 19-25
Using MATCH_RECOGNIZE, you define conditions on the attributes of incoming events and identify these conditions by using identifiers called correlation variables.
Example 19–1 defines correlation variables A, B, and C. A sequence of consecutive
events in the input stream satisfying these conditions constitutes a pattern. The output of a MATCH_RECOGNIZE query is always a stream.
The principle MATCH_RECOGNIZE sub-clauses are:
■
MEASURES: exports makes available for inclusion in the SELECT attribute values of events that successfully match the pattern you specify.
See Section 19.2, MEASURES Clause
.
■
PATTERN: specifies the pattern to be matched as a regular expression over one ore more correlation variables.
See Section 19.3, PATTERN Clause
.
■
DEFINE: specifies the condition for one or more correlation variables. See
Section 19.4, DEFINE Clause .
To refine pattern recognition, you may use the optional MATCH_RECOGNIZE sub-clauses, including:
■
Section 19.5, PARTITION BY Clause
■
Section 19.6, ORDER BY Clause
■
Section 19.7, ALL MATCHES Clause
■
Section 19.8, WITHIN Clause
■
Section 19.9, DURATION Clause
■
Section 19.10, INCLUDE TIMER EVENTS Clause
■
Section 19.11, SUBSET Clause For more information, see:
■
Section 19.1.1, MATCH_RECOGNIZE and the WHERE Clause
■
Section 19.1.2, Referencing Singleton and Group Matches
■
Section 19.1.3, Referencing Aggregates
– Section 19.1.3.5, Using count With , identifier., and identifier.attr
– Section 19.1.3.6, Using first and last
■
Section 19.1.4, Using prev
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.