MATCHING clause

Detects a pattern of events with specified temporal relationships.

Syntax

MATCHING [ ONCE [ ( event [, ...] ) ] [ interval : pattern ] [ on_clause ]
Components

event

The name or alias of a stream listed as one of the query's data sources.

interval

An Interval expression.

pattern

A pattern. See pattern for complete syntax.

on_clause

An ON clause. See ON Clause: Pattern Matching Syntax for more information.

pattern

[!] { event | ( pattern ) | [ interval : pattern ] } [ {&& | || | ,} [!] { event | ( pattern ) | [ interval : pattern ] } ] [...]
Components

event

The name or alias of a stream listed as one of the query's data sources.

pattern

Another, nested pattern.

interval

An Interval expression.

Interval Expression: A valid interval literal, or an expression that evaluates to an interval.

Event Name: The name or alias of a data stream that is also listed as one of the query's data sources. If a stream is referenced more than once in a pattern definition, each reference must use its own alias.

Usage

The optional MATCHING clause follows the FROM clause in a Query statement, Database statement, or Remote Procedure statement. When used, the MATCHING clause must immediately follow the FROM data source list, which must include all data sources specified in the MATCHING clause, and cannot include any other data sources.

The MATCHING clause allows a query to monitor several data sources for a specified pattern of rows that arrive, or don't arrive, in the query's data sources within a specified time interval. If the specified pattern is detected, and other query selection conditions are met, the query publishes output to its destination.

Sybase CEP pattern matching is inclusive; a pattern is considered matched, as long as the specified events occur (or do not occur) as indicated, even if additional unspecified events also occur during the designated interval.

When used without the ONCE subclause, the MATCHING clause produces one output row for each unique combination of input events that matches the pattern (subject to other query selection conditions). Data streams can also contain overlapping occurrences of patterns, in which case all occurrences are published. This rule can be modified with the ONCE subclause.

If the MATCHING clause is used with a ONCE subclause, the query outputs no more than one pattern match for every event specified by the ONCE subclause. In this case, the published pattern match will be the first pattern match involving the specified event to be detected and to fall within the WHERE clause selection condition, if such a condition is indicated. If the ONCE subclause does not specify a list of events, the once-only rule applies to all events in the pattern.

The MATCHING clause of a query that includes multiple data sources can contain an optional ON subclause, which defines one or more multi-equalities that limit the join condition. Queries containing a MATCHING clause can also include other selection criteria, such as a WHERE clause, that further refine the matching conditions for the pattern. However, only the WHERE clause can place selection conditions on non-events. See the WHERE clause description for more information.

The outer level of a pattern definition consists of a bounded pattern definition. Bounded pattern definitions are enclosed in square brackets ([ ]) and contain:

All the components of a bounded pattern, including nested bounded patterns, must occur within the interval specified for the bounded pattern to produce a pattern match.

Event Operators

Operator

Operator Name

Description

!

Not operator

Specifies a negative condition for a pattern component. (Pattern conditions are met when the pattern component does not occur).

&&

Conjunction (And) operator

Both pattern components linked by the conjunction operator must occur for the match condition to be met, but they need not necessarily occur in the order listed.

||

Disjunction (Or) operator

One or both pattern components linked by the Disjunction operator must occur to meet the conditions of the match. Each output row produced by a Disjunction match shows the match for one of the members of the Disjunction, and NULL values for the other members. This is true even when several members of the disjunction produce events.

,

Sequence operator

Pattern components linked by the Sequence operator must both occur in the order listed, to meet the conditions of the match. The listed order refers to the rows' timestamp, not necessarily the order in which the rows appear in a stream viewer.

A sequence pattern matches a given set of events only if the events associated with the sequence occur in the specified order, and the intervals associated with the events do not overlap. Pattern interval rules are discussed below.

The default order of precedence in which pattern components are analyzed for a possible pattern match follows the order of operators, as they are listed in the table. The tightest binding between an operator and a pattern component is that of the Not operator. The bindings then get progressively looser, for events linked with a conjunction, disjunction, and sequence operators, respectively. This default order of precedence can be overridden by enclosing a pattern component in parentheses.

Pattern Definition Validity Rules

A valid pattern definition and all of its components must be properly anchored. Anchoring refers to whether or not the beginning and end of the time interval corresponding to the pattern component can be determined. Anchoring can be fixed by the row timestamp of an event within the pattern component, or can be deduced from the context of the component. Pattern components are referred to as being left-anchored (a pattern component with a fixed interval start time), right-anchored (a pattern component with a fixed interval end time), fully anchored (a pattern component that is both left-anchored and right-anchored), or unanchored (a pattern component that is neither left-anchored nor right-anchored). The following rules must be satisfied for a pattern definition to be considered valid:

  1. Every pattern component within any bounded pattern must be either left-anchored, right-anchored, or both.

  2. For any two consecutive pattern components within a sequence, the left component must be right-anchored, and/or the right component must be left-anchored.

The following table specifies the anchoring status of all pattern components.

Pattern Component Type

Anchoring Status

A single event A

Fully anchored. The event can be anchored in time by its row timestamp.

Non-event !A, where A is a single event or other pattern component

Unanchored. Since the event definition specifies an event or group of events that does not occur, no specific start or end time can be associated with it.

A conjunction A && B, where A and B are single events or other pattern components

Left-anchored if, and only if, both A and B are left-anchored. Right-anchored if, and only if, both A and B are right-anchored.

A disjunction A || B, where A and B are single events or other pattern components

Left-anchored if, and only if, both A and B are left-anchored. Right-anchored if, and only if, both A and B are right-anchored.

A sequence A, ..., B where A and B are single events or other pattern components

Left-anchored if, and only if, A is left-anchored. Right-anchored if, and only if, B is right-anchored.

A bounded-pattern [interval-expression: A] where A is a single event or other pattern component

Always fully anchored. The pattern's start and end times are determined by the specified interval-expression and A's timestamps.

Here are some examples illustrating pattern definition validity rules:

[10 SECONDS: A, B, !C]

This is a valid pattern definition. The sequence A, B, !C is left-anchored (because A is left-anchored), so the first anchoring rule is satisfied. A is right-anchored and B is left-anchored, so the second anchoring rule is satisfied for A and B. B is right-anchored, so the second anchoring rule is satisfied for B and !C.

[1 SECOND: !A, B, !C]

This pattern definition is invalid. The sequence !A, B, !C fails the first anchoring rule, because it is unanchored.

[3 SECONDS: A, !B, !C]

This pattern definition is invalid. !B is not right-anchored and !C is not left-anchored, so the pattern definition fails the second anchoring rule.

[5 MINUTES: A && (!B, C)]

This pattern definition is valid. The conjunction is right-anchored, since both A and (!B, C) are right-anchored.

[20 SECONDS: [10 SECONDS: !A, B], !C]

This pattern definition is valid. The nested bounded pattern is fully anchored, so the sequence [10 SECONDS: !A, B], !C is valid and left-anchored.

Pattern Interval Rules

A pattern component definition, together with the actual incoming events scanned for pattern matches, determine a time interval, within which the A pattern component can be said to occur. Specifically, the pattern component's interval is determined by subtracting the pattern component's starting time from its ending time. Where the starting time and ending time are the same, the interval is considered to be NULL.

The following rules specify how the starting time and ending time of each component are determined:

A single event A

Non-event !A, where A is a single event or other pattern component

A conjunction A && B, where A and B are single events or other pattern-components

A disjunction A || B, where A and B are single events or other pattern components

A sequence A, ..., B where A and B are single events or other pattern components

A bounded pattern [interval-expression: A] where A is a single event, or other pattern-component

Here are some illustrations of pattern interval rules.

[10 SECONDS: A, B, !C]

The interval for the whole pattern starts when a row arrives in stream A and ends ten seconds later. The interval associated with B starts one microsecond after the arrival of the row in A (that is, a row in stream B must arrive after the arrival of the row in A, and must not overlap A to produce a pattern match). Likewise, the interval associated with C starts one microsecond after the arrival of the row in B and extends until ten seconds have elapsed since A's arrival.

[5 MINUTES: A && (!B, C)]

The interval for the whole pattern ends when a row arrives in either stream A or C - whichever arrives later - and starts five minutes before this time. The interval associated with B starts at the same time as the interval for the whole pattern, and ends when the row arrives in C.

[20 SECONDS: [10 SECONDS: !A, B], !C]

The interval for [10 SECONDS: !A, B] ends when a row arrives in B, and starts ten seconds earlier. The interval for the whole pattern starts ten seconds before the row arrives in B and ends 20 seconds later (that is, ten seconds after the row arrives in B.) The interval associated with A starts ten seconds before the arrival of a row in B minus one microsecond. The interval associated with C starts one microsecond after a row arrives in B and ends ten seconds after the row arrives in B.

Capturing Matching Row Data

The XMLPATTERNMATCH() function can be used in the SELECT clause of the query that contains the MATCHING clause to generate an XML tree that contains data from all the rows (except non-events) that resulted in successful pattern matches. The XML tree can then be published to the query's destination.

Restrictions

The following restrictions apply to the use of pattern matching:

See Also

Examples

The following example monitors for a row arriving in the HighTemperature stream, followed by a row arriving in the HighSmoke stream, and no row in the SprinklersOn stream, all within a 120-second interval, where the RoomNumber column in the HighTemperature and HighSmoke streams contain the same value:

INSERT INTO OutStream
SELECT HighTemperature.RoomNumber
FROM HighTemperature, HighSmoke, SprinklersOn
   MATCHING [120 SECONDS: HighTemperature, HighSmoke, !SprinklersOn]
ON HighTemperature.RoomNumber = HighSmoke.RoomNumber 
   = SprinklersOn.RoomNumber;

The pattern defined in the following example is similar to the previous one, but allows the rows in HighTemperature and HighSmoke to occur in either order. The pattern interval in this example is shorter (20 seconds) than in the previous one, and each stream has an associated alias. A condition selection requires the Building column in all three streams to contain the same value:

INSERT INTO BackupAlarmSystem
SELECT 'SprinklerFailure'
FROM HighSmoke AS HS, HighTemperature AS HT, SprinklersOn AS S
   MATCHING [20 SECONDS: HS && HT, !S]
ON HS.Building = HT.Building = S.Building;

Here is another example of a pattern definition:

INSERT INTO OutStream
SELECT A.x, B.x, C.x
FROM A, B, C, D, E, F, G, H, I
   MATCHING [20 SECONDS: A && B, C && D, E, !F, G || H, !I];

The following example contains a parentheses-enclosed subpattern that overrides the default event operator order of precedence:

INSERT INTO OutStream
SELECT A.x, C.x, D.x
FROM A, C, D
   MATCHING [1 SECOND: (A || B) && C, D];

The following example contains a nested bounded pattern definition that monitors for events with a five-second interval, nested within the larger ten-second interval of the outer pattern:

INSERT INTO OutStream
SELECT A.x, D.x
FROM A, D
   MATCHING [10 SECONDS: A, !B, [5 SECONDS: !C, D]];

The following example shows a pattern-matching specification that contains a ONCE subclause, which limits the output of matches involving stream A, causing the query to output only the first pattern match involving A, where the value of B.x is greater than 100.

INSERT INTO OutStream
SELECT A.x, B.x
FROM A, B
   MATCHING ONCE (A) [10 SECONDS: A, B]
WHERE B.x > 100;