Pattern Quantifiers and Regular Expressions

Pattern Recognition With MATCH_RECOGNIZE 19-15

19.4.1 Functions Over Correlation Variables in the DEFINE Clause

You can use functions over the correlation variables while defining them. Example 19–12 applies the to_timestamp function to correlation variables. Example 19–12 Using Functions Over Correlation Variables: to_timestamp ... 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 ... Example 19–13 applies the count function to correlation variable B to count the number of times its definition was satisfied. A match is recognized when totalCountValue is less than 1000 two or more times in 30 minutes. Example 19–13 Using Functions Over Correlation Variables: count ... MATCH_RECOGNIZE ... PATTERNB DURATION 30 MINUTES DEFINE B as B.totalCountValue 1000 and countB. = 2 ... For more information, see: ■ 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

19.4.2 Referencing Attributes in the DEFINE Clause

You can refer to the attributes of a base stream: ■ Without a correlation variable: c1 20. ■ With a correlation variable: A.c1 20. When you refer to the attributes without a correlation variable, a tuple that last matched any of the correlation variables is consulted for evaluation. Consider the following definitions: ■ DEFINE A as c1 20 ■ DEFINE A as A.c1 20 Both refer to c1 in the same tuple which is the latest input tuple. This is because on receiving an input we evaluate the condition of a correlation variable assuming that the latest input matches that correlation variable. If you specify a correlation name that is not defined in the DEFINE clause, it is considered to be true for every input. 19-16 Oracle Complex Event Processing CQL Language Reference In Example 19–14 , correlation variable A appears in the PATTERN clause but is not specified in the DEFINE clause. This means the correlation name A is true for every input. It is an error to define a correlation name which is not used in a PATTERN clause. Example 19–14 Undefined Correlation Name query id=q[CDATA[ SELECT T.firstW, T.lastZ FROM S2 MATCH_RECOGNIZE MEASURES A.c1 as firstW, lastZ as lastZ PATTERNA W+ X+ Y+ Z+ DEFINE W as W.c2 prevW.c2, X as X.c2 prevX.c2, Y as Y.c2 prevY.c2, Z as Z.c2 prevZ.c2 as T ]]query For more information, see: ■ Section 19.4.3, Referencing One Correlation Variable From Another in the DEFINE Clause ■ Section 19.1.2, Referencing Singleton and Group Matches ■ 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 ■ Section 19.3, PATTERN Clause

19.4.3 Referencing One Correlation Variable From Another in the DEFINE Clause

A definition of one correlation variable can refer to another correlation variable. Consider the query that Example 19–15 shows: Example 19–15 Referencing One Correlation Variable From Another ... Select a_firsttime, d_lasttime, b_avgprice, d_avgprice FROM S MATCH_RECOGNIZE PARTITION BY symbol MEASURES firsta.time as a_firsttime, lastd.time as d_lasttime, avgb.price as b_avgprice, avgd.price as d_avgprice PATTERN A B+ C+ D DEFINE A as A.price 100, B as B.price A.price, C as C.price avgB.price, Pattern Recognition With MATCH_RECOGNIZE 19-17 D as D.price prevD.price ... Note the following: ■ Because correlation variable A defines a single attribute, B can refer to this single attribute. ■ Because B defines more than one attribute, C cannot reference a single attribute of B. In this case, C may only reference an aggregate of B. ■ D is defined in terms of itself: in this case, you may refer to a single attribute or an aggregate. In this example, the prev function is used to access the match of D prior to the current match. For more information, see: ■ Section 19.4.2, Referencing Attributes in the DEFINE Clause ■ Section 19.1.2, Referencing Singleton and Group Matches ■ 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 ■ Section 19.4.2, Referencing Attributes in the DEFINE Clause

19.5 PARTITION BY Clause

Use this optional clause to specify the stream attributes by which a MATCH_ RECOGNIZE clause should partition its results. Without a PARTITION BY clause, all stream attributes belong to the same partition. pattern_partition_clause::= non_mt_attr_list::= on page 7-21 In Example 19–1 , the pattern_partition_clause is: PARTITION BY itemId The partition by clause in pattern means the input stream is logically divided based on the attributes mentioned in the partition list and pattern matching is done within a partition. Consider a stream S with schema c1 integer, c2 integer with the input data that Example 19–16 shows. Example 19–16 Input Stream S1 c1 c2 1000 10, 1 2000 10, 2 3000 20, 2 4000 20, 1 Consider the MATCH_RECOGNIZE query that Example 19–17 shows.