Skip to content

Commit 4554448

Browse files
JinqingKuangcodex
andcommitted
feat(stream): support nested event window sub-events
Add recursive EVENT_WINDOW START WITH parsing, runtime conditionPath propagation, and _event_condition_path placeholder support. Co-authored-by: OpenAI Codex GPT-5 <noreply@openai.com>
1 parent 0599a1f commit 4554448

File tree

19 files changed

+1112
-192
lines changed

19 files changed

+1112
-192
lines changed

docs/en/06-advanced/03-stream.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ trigger_type: {
3333
| INTERVAL(interval_val[, interval_offset]) SLIDING(sliding_val[, offset_time])
3434
| SESSION(ts_col, session_val)
3535
| STATE_WINDOW(expr [, extend[, zeroth_state]]) [TRUE_FOR(true_for_expr)]
36-
| EVENT_WINDOW(START WITH start_condition END WITH end_condition) [TRUE_FOR(true_for_expr)]
36+
| EVENT_WINDOW(START WITH start_event_item [END WITH end_condition]) [TRUE_FOR(true_for_expr)]
3737
| COUNT_WINDOW(count_val[, sliding_val][, col1[, ...]])
3838
}
3939

40+
start_event_item: start_condition | (start_event_item, start_event_item [, ...])
41+
4042
true_for_expr: {
4143
duration_time
4244
| COUNT count_val
@@ -60,6 +62,8 @@ tag_definition:
6062
tag_name type_name [COMMENT 'string_value'] AS expr
6163
```
6264

65+
For `EVENT_WINDOW`, `start_event_item` supports multi-level nested sub-events. For example, `START WITH ((cond_1a, cond_1b), cond_2)`. When a stream calculation uses a sub-event structure, the `_event_condition_path` placeholder can be used in the query part to obtain the static path of the currently matched node in the start-condition tree.
66+
6367
### Trigger Methods
6468

6569
- Periodic trigger: drives execution by fixed intervals of system time. The baseline is midnight of the day the stream is created, and subsequent trigger times are determined by the specified interval. A time offset can be applied to adjust the baseline.

docs/en/14-reference/03-taos-sql/41-stream.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ trigger_type: {
3030
| INTERVAL(interval_val[, interval_offset]) SLIDING(sliding_val[, offset_time])
3131
| SESSION(ts_col, session_val)
3232
| STATE_WINDOW(expr[, extend[, zeroth_state]]) [TRUE_FOR(true_for_expr)]
33-
| EVENT_WINDOW(START WITH start_condition END WITH end_condition) [TRUE_FOR(true_for_expr)]
34-
| EVENT_WINDOW(START WITH (start_condition_1, start_condition_2 [,...]) [END WITH end_condition]) [TRUE_FOR(true_for_expr)]
33+
| EVENT_WINDOW(START WITH start_event_item [END WITH end_condition]) [TRUE_FOR(true_for_expr)]
3534
| COUNT_WINDOW(count_val[, sliding_val][, col1[, ...]])
3635
}
3736

37+
start_event_item: start_condition | (start_event_item, start_event_item [, ...])
38+
3839
true_for_expr: {
3940
duration_time
4041
| COUNT count_val
@@ -192,13 +193,13 @@ Applicable Scenarios: Suitable for use cases where computations and/or notificat
192193
##### Event Window Trigger
193194

194195
```sql
195-
EVENT_WINDOW(START WITH start_condition END WITH end_condition) [TRUE_FOR(true_for_expr)]
196+
EVENT_WINDOW(START WITH start_condition [END WITH end_condition]) [TRUE_FOR(true_for_expr)]
196197
```
197198

198199
An event window trigger partitions the incoming data of the trigger table into windows based on defined event start and end conditions, and triggers when the window opens and/or closes. Parameter definitions are as follows:
199200

200201
- start_condition: Definition of the event start condition. It can be any valid conditional expression.
201-
- end_condition: Definition of the event end condition. It can be any valid conditional expression.
202+
- end_condition: Optional. Definition of the event end condition. It can be any valid conditional expression.
202203
- true_for_expr (optional): Specifies the filtering condition for windows. Only windows that meet the condition will generate a trigger. Supports the following four modes:
203204
- `TRUE_FOR(duration_time)`: Filters based on duration only. The window duration must be greater than or equal to `duration_time`.
204205
- `TRUE_FOR(COUNT n)`: Filters based on row count only. The window row count must be greater than or equal to `n`.
@@ -225,16 +226,18 @@ CREATE STREAM s_tag_event
225226

226227
Applicable Scenarios: Suitable for use cases where computations and/or notifications need to be driven by event windows.
227228

228-
##### Event Window Trigger (with Sub-Event Window Support)
229+
##### Event Window Trigger (with Multi-Level Sub-Event Support)
229230

230231
```sql
231-
EVENT_WINDOW(START WITH (start_condition_1, start_condition_2 [,...] [END WITH end_condition]) [TRUE_FOR(true_for_expr)]
232+
EVENT_WINDOW(START WITH start_event_item [END WITH end_condition]) [TRUE_FOR(true_for_expr)]
233+
234+
start_event_item: start_condition | (start_event_item, start_event_item [, ...])
232235
```
233236

234-
An event window trigger partitions the incoming data of the trigger table into windows based on event windows. It now supports specifying multiple start conditions and can further subdivide and manage sub-event windows within the original event window based on changes in the effective trigger condition, while introducing the concept of a parent event window to aggregate related sub-event windows. Parameter definitions are as follows:
237+
An event window trigger now supports recursively nested start-event groups. A start item can be a single valid condition expression or a group made of multiple `start_event_item` entries, and groups can be nested further. The engine evaluates the entire start-condition tree in SQL order and preserves the hierarchy of parent groups and leaf conditions in notifications and stream calculations.
235238

236-
- start_condition_1, start_condition_2 [, ...]: Defines multiple event start conditions. The event window opens when any one of these conditions is satisfied. The system evaluates these conditions in order from first to last, and the first satisfied condition becomes the "effective trigger condition". When all start_conditions are not satisfied, both the parent window and the last sub-window close.
237-
- end_condition: Definition of the event end condition. When this condition is satisfied, both the current parent window and the last sub-window close. This parameter is now optional.
239+
- start_event_item: A start-event item. It can be a single start condition or a grouped list. Group items can themselves contain nested groups.
240+
- end_condition: Optional. When it is satisfied, the active leaf window and all affected ancestor group windows are closed in hierarchical order.
238241
- true_for_expr (optional): Specifies the filtering condition for windows. Only windows that meet the condition will generate a trigger. Supports the following four modes:
239242
- `TRUE_FOR(duration_time)`: Filters based on duration only. The window duration must be greater than or equal to `duration_time`.
240243
- `TRUE_FOR(COUNT n)`: Filters based on row count only. The window row count must be greater than or equal to `n`.
@@ -248,17 +251,23 @@ Usage Notes:
248251
- A trigger table must be specified. When the trigger table is a supertable, grouping by tags or subtables is supported, as well as no grouping.
249252
- When used with a supertable, it must be combined with PARTITION BY tbname.
250253
- Supports conditional window triggering after filtering the written data.
251-
- The multiple `start_condition` expressions and the optional `end_condition` can also reference tag columns visible in the trigger-table context.
252-
- Parent and sub-window behavior:
253-
- No parent/sub-windows: During the event window opening period, if the effective trigger condition does not change, only one window is produced. The system treats it as a regular event window, without generating the concept of parent/sub-windows.
254-
- Sub-windows: When a specific start_condition becomes the effective trigger condition, a sub-window opens. If the effective trigger condition changes, or when the end_condition is satisfied, the current sub-window closes. Sub-windows do not overlap with each other.
255-
- Parent window: A parent window only opens when the second sub-window opens. The parent window's start time is the start time of the first sub-window, and its end time is the end time of the last sub-window. It closes when all start_conditions are not satisfied, or when the end_condition is satisfied.
256-
- Notification message extensions: In the window open (WINDOW_OPEN) notification message, two new fields are added:
257-
- conditionIndex: The index number of the start condition that triggered the current window opening, counting from 0. For a parent window, its value is the same as the first sub-window's value.
258-
- windowIndex: The index number of the sub-event window within the parent window, counting from 0. If it is not a sub-window (i.e., a regular event window or parent window), this field value is -1.
254+
- `start_event_item` and the optional `end_condition` can reference tag columns visible in the trigger-table context.
255+
- Each node in the start-condition tree is assigned a stable static path named `conditionPath`. Paths are generated in SQL order, and sibling nodes are numbered from `0`. For `START WITH ((a, b, c), d)`:
256+
- Group `(a, b, c)` has `conditionPath = "0"`
257+
- `a` has `conditionPath = "0.0"`
258+
- `b` has `conditionPath = "0.1"`
259+
- `c` has `conditionPath = "0.2"`
260+
- `d` has `conditionPath = "1"`
261+
- `conditionIndex` is redefined as the local index of the current node under its parent, which is always the last segment of `conditionPath`.
262+
- When the active branch switches from one subtree to another, the engine closes the current leaf window first, then closes the affected ancestor group windows, and finally opens the windows for the new branch.
263+
- Notification payloads no longer include `windowIndex`. Event-window node identification is now expressed with `conditionPath + conditionIndex`.
264+
- A new placeholder, `_event_condition_path`, is available in stream calculations:
265+
- It is only valid in `EVENT_WINDOW` stream calculations.
266+
- `START WITH` must use a sub-event structure, either single-level or nested.
267+
- The value is the static path string of the currently triggered node, such as `0` or `0.1`.
259268
- The TRUE_FOR option applies to both sub-windows and parent windows, meaning windows (whether sub-windows or parent windows) shorter than the duration limit will be directly ignored. When some sub-windows under a parent window do not meet the TRUE_FOR condition, the valid sub-windows may not be consecutive. If only 1 sub-window under a parent window meets the TRUE_FOR condition, the parent/sub-window structure is still retained and triggers notifications and computations.
260269

261-
Applicable Scenarios: Suitable for use cases where computations and/or notifications need to be driven by event windows, especially in IoT and industrial data management fields where fine-grained monitoring and analysis of events based on multiple dynamically changing conditions is required. For example, in equipment fault alarms, multiple alarm level conditions (such as "load above 90" and "load above 60") can be defined, and when alarm levels change, the escalation or de-escalation of alarm states can be clearly tracked.
270+
Applicable Scenarios: Suitable for use cases where computations and/or notifications need to be driven by event windows, especially when multiple dynamic conditions must be modeled with explicit hierarchy. For example, an alarm stream can first define a high-level alarm group and then subdivide it into nested severity levels, while `conditionPath` or `_event_condition_path` identifies the exact branch that matched.
262271

263272
##### Count Window Trigger
264273

@@ -373,6 +382,7 @@ When performing calculations, you may need to use contextual information from th
373382
| Window Trigger | _twrownum | Number of rows in currently open window. Used only with WINDOW_CLOSE trigger. |
374383
| Idle Trigger | _tidlestart | The time (processing time) of the last data received by the group before it entered idle state. Nanosecond precision Unix epoch. Applicable only for IDLE/RESUME triggers. Cannot be mixed with `_twstart/_twend`. Since output tables are usually millisecond-precision, use `cast(_tidlestart/1000000 as timestamp)` to convert. |
375384
| Idle Trigger | _tidleend | The trigger time of the IDLE or RESUME event. Nanosecond precision Unix epoch. Applicable only for IDLE/RESUME triggers. Cannot be mixed with `_twstart/_twend`. Since output tables are usually millisecond-precision, use `cast(_tidleend/1000000 as timestamp)` to convert.|
385+
| Event Window Trigger | _event_condition_path | Static path of the currently triggered node in the event-window start-condition tree. Valid only for `EVENT_WINDOW` stream calculations that use a sub-event structure. Returns a string such as `0` or `0.1`. |
376386
| All | _tgrpid | ID of trigger group (data type BIGINT) |
377387
| All | _tlocaltime | System time of current trigger (nanosecond precision) |
378388
| All | %%n | Reference to trigger group column<br/>n is the column number in `[PARTITION BY col1[, ...]]`, starting with 1 |
@@ -384,6 +394,7 @@ Usage Restrictions:
384394
- %%trows: Can only be used in the FROM clause. Queries that use %%trows do not support WHERE condition filtering or join operations on %%trows.
385395
- %%tbname: Can be used in the FROM, SELECT, and WHERE clauses.
386396
- Other placeholders: Can only be used in the SELECT and WHERE clauses.
397+
- `_event_condition_path`: Only valid in `EVENT_WINDOW` stream calculations where `START WITH` uses a sub-event structure. It is illegal for a single plain start condition.
387398

388399
### Stream Processing Control Options
389400

@@ -504,8 +515,9 @@ An example structure of a notification message is shown below:
504515
"groupId": "7533998559487590581",
505516
"windowStart": 1733284800000,
506517
"triggerCondition": {
518+
"conditionPath": "0.0",
507519
"conditionIndex": 0,
508-
"fieldValue": {
520+
"fieldValues": {
509521
"c1": 10,
510522
"c2": 15
511523
}
@@ -521,8 +533,9 @@ An example structure of a notification message is shown below:
521533
"windowStart": 1733284800000,
522534
"windowEnd": 1733284810000,
523535
"triggerCondition": {
536+
"conditionPath": "0.1",
524537
"conditionIndex": 1,
525-
"fieldValue": {
538+
"fieldValues": {
526539
"c1": 20,
527540
"c2": 3
528541
}
@@ -621,14 +634,16 @@ These fields apply only when triggerType is Event.
621634
- If eventType = WINDOW_OPEN, the event object includes:
622635
- windowStart: Long integer timestamp indicating the window’s start time. Precision matches the time precision of the result table.
623636
- triggerCondition: Information about the condition that opened the window, including:
624-
- conditionIndex: Integer. The index of the condition that triggered the window open, starting from 0.
625-
- fieldValue: Key–value pairs containing the condition column names and their corresponding values.
637+
- conditionPath: String. The static path of the current node in the start-condition tree.
638+
- conditionIndex: Integer. The local index of the current node under its parent, equal to the last segment of `conditionPath`.
639+
- fieldValues: Key–value pairs containing the columns referenced by the event-window start-condition tree and their values.
626640
- If eventType = WINDOW_CLOSE, the event object includes:
627641
- windowStart: Long integer timestamp indicating the window’s start time. Precision matches the time precision of the result table.
628642
- windowEnd: Long integer timestamp indicating the window’s end time. Precision matches the time precision of the result table.
629643
- triggerCondition: Information about the condition that closed the window, including:
630-
- conditionIndex: Integer. The index of the condition that triggered the window close, starting from 0.
631-
- fieldValue: Key–value pairs containing the condition column names and their corresponding values.
644+
- conditionPath: String. The static path of the current node in the start-condition tree.
645+
- conditionIndex: Integer. The local index of the current node under its parent, equal to the last segment of `conditionPath`.
646+
- fieldValues: Key–value pairs containing the condition-related column values carried by the close notification.
632647
- result: The computation result, expressed as key–value pairs containing the names of the result columns and their corresponding values.
633648

634649
##### Fields for Count Windows

docs/zh/06-advanced/03-stream.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ options: {
3232
trigger_type: {
3333
PERIOD(period_time[, offset_time])
3434
| SLIDING(sliding_val[, offset_time])
35-
| INTERVAL(interval_val[, interval_offset]) SLIDING(sliding_val[, offset_time])
36-
| SESSION(ts_col, session_val)
37-
| STATE_WINDOW(expr [, extend[, zeroth_state]]) [TRUE_FOR(true_for_expr)]
38-
| EVENT_WINDOW(START WITH start_condition END WITH end_condition) [TRUE_FOR(true_for_expr)]
39-
| COUNT_WINDOW(count_val[, sliding_val][, col1[, ...]])
40-
}
35+
| INTERVAL(interval_val[, interval_offset]) SLIDING(sliding_val[, offset_time])
36+
| SESSION(ts_col, session_val)
37+
| STATE_WINDOW(expr [, extend[, zeroth_state]]) [TRUE_FOR(true_for_expr)]
38+
| EVENT_WINDOW(START WITH start_event_item [END WITH end_condition]) [TRUE_FOR(true_for_expr)]
39+
| COUNT_WINDOW(count_val[, sliding_val][, col1[, ...]])
40+
}
41+
42+
start_event_item: start_condition | (start_event_item, start_event_item [, ...])
4143

4244
true_for_expr: {
4345
duration_time
@@ -56,11 +58,13 @@ notify_option: [NOTIFY_HISTORY | ON_FAILURE_PAUSE]
5658
event_types:
5759
event_type [|event_type]
5860

59-
event_type: {WINDOW_OPEN | WINDOW_CLOSE | IDLE | RESUME}
61+
event_type: {WINDOW_OPEN | WINDOW_CLOSE | IDLE | RESUME}
6062

6163
tag_definition:
62-
tag_name type_name [COMMENT 'string_value'] AS expr
63-
```
64+
tag_name type_name [COMMENT 'string_value'] AS expr
65+
```
66+
67+
其中 `EVENT_WINDOW``start_event_item` 支持多级子事件嵌套。例如 `START WITH ((cond_1a, cond_1b), cond_2)`。当流计算使用了子事件结构时,可在查询部分使用 `_event_condition_path` 获取当前命中节点在开始条件树中的静态路径。
6468

6569
### 触发方式
6670

0 commit comments

Comments
 (0)