Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/external.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,7 @@ ExternalProject_Add(ext_arrow
CMAKE_ARGS -DPARQUET_REQUIRE_ENCRYPTION:BOOL=OFF
CMAKE_ARGS -DARROW_SIMD_LEVEL:STRING=NONE
CMAKE_ARGS -DARROW_RUNTIME_SIMD_LEVEL:STRING=NONE
CMAKE_ARGS -DBUILD_WARNING_LEVEL:STRING=PRODUCTION
CMAKE_ARGS ${ARROW_EXTRA_CMAKE_ARGS}
BUILD_COMMAND
# Windows/jom: multiple parallel jom child processes share the same
Expand Down
2 changes: 1 addition & 1 deletion docs/en/05-basic/03-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ window_clause: {
When using the window clause, the following rules should be observed. These rules apply to the five window types SESSION, STATE_WINDOW, INTERVAL, EVENT_WINDOW, and COUNT_WINDOW. EXTERNAL_WINDOW has different rules; see the [External Window](#external-window) section for details.

1. The window clause is located after the data partitioning clause and cannot be used together with the GROUP BY clause.
1. The window clause partitions the data by windows and performs calculations on the expressions in the SELECT list for each window. The expressions in the SELECT list can only include: constants; pseudocolumns: \_wstart pseudo-column,\_wend pseudo-column, and \_wduration pseudo-column; aggregate functions (including selection functions, time-series specific functions whose output row count is determined by parameters, and window calculation / time-weighted statistics functions among the time-series specific functions)
1. The window clause partitions the data by windows and performs calculations on the expressions in the SELECT list for each window. The expressions in the SELECT list can only include: constants; pseudocolumns: \_wstart pseudo-column,\_wend pseudo-column, and \_wduration pseudo-column; aggregate functions (including selection functions, time-series specific functions whose output row count is determined by parameters, and window calculation / time-weighted statistics functions among the time-series specific functions). Starting from version 3.4.2.0, the SELECT list also supports column expressions and indefinite-row functions, in which case the query enters window projection mode where each window outputs all its original rows instead of one aggregated row.
1. WHERE statements can specify the start and end times of the query and other filtering conditions.

:::
Expand Down
5 changes: 4 additions & 1 deletion docs/en/14-reference/03-taos-sql/20-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Data Querying
```sql
SELECT {DATABASE() | CLIENT_VERSION() | SERVER_VERSION() | SERVER_STATUS() | NOW() | TODAY() | TIMEZONE() | CURRENT_USER() | USER() }

SELECT [hints] [DISTINCT] [TAGS] select_list
SELECT [hints] [DISTINCT] [TAGS] [SCALAR | AGG] select_list
from_clause
[WHERE condition]
[partition_by_clause]
Expand Down Expand Up @@ -117,6 +117,7 @@ true_for_expr: {
- interp_clause: Interp clause, used in conjunction with the interp function, specifying the recorded value or interpolation of the time section, can specify the time range of interpolation, output time interval, and interpolation type.
- RANGE: Specify a single or start end time value, the end time must be greater than the start time. ts_val is a standard timestamp type. surrounding_time_val is optional, specifying the time range for valid data, as a positive value. Supported time units are listed in [Time Units](./01-datatype.md#time-units) (months/quarters/years not supported). Such as ```RANGE('2023-10-01T00:00:00.000')``` or ```RANGE('2023-10-01T00:00:00.000', '2023-10-01T23:59:59.999')```.
- EVERY: Time interval range, with every_val being a positive value. Supported time units are listed in [Time Units](./01-datatype.md#time-units) (milliseconds through weeks only), such as EVERY (1s).
- SCALAR | AGG: Window query mode keywords (supported from version 3.4.2.0). When the SELECT list in a window query contains column expressions or indefinite-row functions, the query automatically enters **window projection mode**, where each window outputs all its original rows; when the SELECT list contains only aggregate functions, it enters **window aggregation mode**, where each window outputs one aggregated row. When the SELECT list contains only pseudocolumns, tag columns, tbname, constants, group keys, and state keys, INTERVAL, SESSION, STATE_WINDOW, EVENT_WINDOW, and COUNT_WINDOW default to aggregation mode, while EXTERNAL_WINDOW defaults to projection mode. Use the `SCALAR` or `AGG` keyword to explicitly specify the mode. See [TDengine Distinctive Queries](24-distinguished.md#window-projection-mode) for details.
- fill_clause: Fill clause, can be used with the interp function, INTERVAL window, or EXTERNAL_WINDOW to specify the data filling method when data is missing. The supported modes differ by context.
- group_by_expr: Specify data grouping and aggregation rules. Supports expressions, functions, positions, columns, and aliases. When using positional syntax, it must appear in the selection column, such as `select ts, current from meters order by ts desc, 2`, where 2 corresponds to the current column.
- partition_by_expr: Specify the data slicing conditions, and calculate the data independently within the slice. Supports expressions, functions, positions, columns, and aliases. When using positional syntax, it must appear in the selection column, such as `select current from meters partition by 1`, where 1 corresponds to the current column.
Expand Down Expand Up @@ -357,6 +358,8 @@ The differences between NULL, NULL_F, VALUE, VALUE_F filling modes for different

For EXTERNAL_WINDOW queries, the supported modes are `NONE`, `NULL`, `NULL_F`, `VALUE`, `VALUE_F`, `PREV`, and `NEXT`. `LINEAR`, `NEAR`, and `SURROUND` are not supported in EXTERNAL_WINDOW.

For window projection mode (when the SELECT list contains column expressions or indefinite-row functions), FILL only supports `NONE`, `NULL`, `NULL_F`, `VALUE`, and `VALUE_F`. `PREV`, `NEXT`, `LINEAR`, and `NEAR` are not supported in projection mode.

:::info

1. When using the FILL statement, a large amount of fill output may be generated, so be sure to specify the query time range. For each query, the system can return up to 10 million results with interpolation.
Expand Down
75 changes: 72 additions & 3 deletions docs/en/14-reference/03-taos-sql/24-distinguished.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ The following rules apply to the five window types SESSION, STATE_WINDOW, INTERV
- Aggregate functions (including selection functions, time-series specific functions whose output row count is determined by parameters, and window calculation / time-weighted statistics functions among the time-series specific functions).
- Expressions containing the above expressions.
- And must include at least one aggregate function(this limitation no longer exists after version 3.4.0.0).
- Column expressions and indefinite-row functions (supported from version 3.4.2.0). In this case, the query enters [window projection mode](#window-projection-mode), where each window outputs all its original rows instead of one aggregated row.
- The window clause cannot be used together with the GROUP BY clause.
- WHERE statements can specify the start and end time of the query and other filtering conditions.

Expand Down Expand Up @@ -432,9 +433,11 @@ Where:

2. **Aggregation and computation within windows:** The outer query calculates independently within each window range and supports aggregation and scalar expressions.

3. **Pseudo-column support:** `_wstart` (window start time), `_wend` (window end time), and `_wduration` (window duration) can be used in the SELECT, HAVING, and ORDER BY clauses.
3. **Window query mode:** External windows support the `SCALAR` / `AGG` keywords. Unlike other windows, external windows default to **projection mode** (each window outputs all original rows) in the ambiguous case. Use `AGG` to switch to aggregation mode (one row per window). See [Window Projection Mode](#window-projection-mode) for details.

4. **Grouping and alignment**
4. **Pseudo-column support:** `_wstart` (window start time), `_wend` (window end time), and `_wduration` (window duration) can be used in the SELECT, HAVING, and ORDER BY clauses.

5. **Grouping and alignment**

- The subquery can use `PARTITION BY` or `GROUP BY` for grouping, while the outer query can only use `PARTITION BY` for grouping.
- When both the subquery and the outer query use grouping, matching is aligned by grouping key: data from the same group only matches windows from the same group.
Expand All @@ -443,7 +446,7 @@ Where:
- When the subquery uses grouping but the outer query does not, the syntax is invalid.
- **Current limitation and caveat:** When both inner and outer queries use grouping, and the window subquery also uses `ORDER BY`, the sorting may disturb the original organization of each grouped window stream. The outer query may then operate on a merged window stream, causing the inner grouping semantics to become ineffective, as if there were no grouping, and the one-to-one alignment between inner and outer groups is lost.

5. **Nested calls support:** Multiple layers of external window nesting are supported. That is, the subquery of an external window can itself use EXTERNAL_WINDOW, enabling layered aggregation. For example, a first-level external window can define event-based time ranges and aggregate intermediate metrics, then a second-level external window can aggregate those intermediate metrics again within a new set of time ranges.
6. **Nested calls support:** Multiple layers of external window nesting are supported. That is, the subquery of an external window can itself use EXTERNAL_WINDOW, enabling layered aggregation. For example, a first-level external window can define event-based time ranges and aggregate intermediate metrics, then a second-level external window can aggregate those intermediate metrics again within a new set of time ranges.

#### Rules for Referencing Window Attribute Columns

Expand Down Expand Up @@ -561,6 +564,72 @@ The SQL above defines three one-minute external windows. If a window contains no
- If the external window, meaning the inner subquery, uses grouping, the outer query must also use PARTITION BY; otherwise, a syntax error is raised.
- Variable-row functions such as DIFF and INTERP are not supported within window scope.

### Window Projection Mode

Starting from version 3.4.2.0, window queries support projection mode. In traditional window aggregation mode, each window outputs one aggregated row. In window projection mode, each window outputs all its original rows, along with window pseudocolumns (such as `_wstart` and `_wend`).

#### Mode Detection

The system automatically detects the query mode based on the SELECT list:

- **Aggregation mode**: The SELECT list contains aggregate functions; each window outputs one row.
- **Projection mode**: The SELECT list contains column expressions or indefinite-row functions (such as DIFF, CSUM, etc.); each window outputs all original rows.
- **Ambiguous case**: When the SELECT list contains only pseudocolumns (`_wstart`, `_wend`, etc.), tag columns, tbname, constants, group keys, and/or state keys, INTERVAL, SESSION, STATE_WINDOW, EVENT_WINDOW, and COUNT_WINDOW default to aggregation mode, while EXTERNAL_WINDOW defaults to projection mode.

#### SCALAR / AGG Keywords

In ambiguous cases, the `SCALAR` or `AGG` keyword can be used to explicitly specify the mode:

- `SCALAR`: Forces projection mode.
- `AGG`: Forces aggregation mode.

Note: For EXTERNAL_WINDOW, the default mode in the ambiguous case is projection mode (opposite to other windows). Use `AGG` to switch it to aggregation mode.

These keywords are placed between `SELECT` and the select list, after `TAGS`. The syntax is:

```sql
SELECT [SCALAR | AGG] select_list FROM ... INTERVAL(...) ...
```

Examples:

```sql
-- Ambiguous case: only pseudocolumns + tags + constants, defaults to aggregation mode (1 row per window)
SELECT _wstart, _wend, tbname FROM d1001 INTERVAL(3s);

-- Use SCALAR to force projection mode (N rows per window)
SELECT SCALAR _wstart, _wend, tbname FROM d1001 INTERVAL(3s);

-- Non-ambiguous case: contains column expressions, automatically enters projection mode (all three are equivalent)
SELECT _wstart, ts, current FROM d1001 INTERVAL(3s);
SELECT SCALAR _wstart, ts, current FROM d1001 INTERVAL(3s);
SELECT AGG _wstart, ts, current FROM d1001 INTERVAL(3s);
```

EXTERNAL_WINDOW defaults to projection mode in the ambiguous case. Use `AGG` to switch to aggregation mode:

```sql
-- Ambiguous case: only pseudocolumns + tags, EXTERNAL_WINDOW defaults to projection mode (N rows per window)
SELECT _wstart, _wend, location FROM d1001
EXTERNAL_WINDOW((SELECT _wstart, _wend FROM d1001 INTERVAL(3s)) w);

-- Use AGG to force aggregation mode (1 row per window)
SELECT AGG _wstart, _wend, location FROM d1001
EXTERNAL_WINDOW((SELECT _wstart, _wend FROM d1001 INTERVAL(3s)) w);
```

#### FILL Support

Window projection mode supports the FILL clause, but only the following modes: `NONE`, `NULL`, `NULL_F`, `VALUE`, `VALUE_F`. The modes `PREV`, `NEXT`, `LINEAR`, and `NEAR` are not supported.

```sql
SELECT _wstart, ts, current FROM meters
WHERE ts >= '2024-01-01' AND ts < '2024-01-02'
PARTITION BY tbname
INTERVAL(10m)
FILL(NULL);
```

### Timestamp Pseudo Columns

In window aggregate query results, if the SQL statement does not specify the output of the timestamp column in the query results, the final results will not automatically include the window's time column information. If you need to output the time window information corresponding to the aggregate results in the results, you need to use timestamp-related pseudocolumns in the SELECT clause: window start time (\_WSTART), window end time (\_WEND), window duration (\_WDURATION), and overall query window related pseudo columns: query window start time (\_QSTART) and query window end time (\_QEND). It should be noted that, except that the end time of the INTERVAL window is an open interval, the start time and end time of other time windows are both closed intervals, and the window duration is the numerical value under the current time resolution of the data. For example, if the current database's time resolution is milliseconds, then 500 in the results represents the duration of the current time window is 500 milliseconds (500 ms).
Expand Down
2 changes: 2 additions & 0 deletions docs/en/14-reference/03-taos-sql/92-keywords.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The list of keywords is as follows:
| AES_ENCRYPT | 3.4.0.3+ |
| AFTER | |
| AGGREGATE | |
| AGG | 3.4.2.0+ |
| ALIAS | |
| ALIVE | |
| ALL | |
Expand Down Expand Up @@ -457,6 +458,7 @@ The list of keywords is as follows:
|Keyword|Description|
|----------------------|-|
| S3MIGRATE | |
| SCALAR | 3.4.2.0+ |
| SCHEMALESS | |
| SCORES | |
| SECURITY_LEVEL | 3.4.1.6+ |
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/05-basic/03-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ window_clause: {
**注意** 在使用窗口子句时应注意以下规则(以下规则适用于 SESSION、STATE_WINDOW、INTERVAL、EVENT_WINDOW、COUNT_WINDOW 五种窗口,EXTERNAL_WINDOW 的规则有所不同,详见[外部窗口](#外部窗口)章节):

1. 窗口子句位于数据切分子句之后,不可以和 GROUP BY 子句一起使用。
2. 窗口子句将数据按窗口进行切分,对每个窗口进行 SELECT 列表中的表达式的计算,SELECT 列表中的表达式只能包含:常量;伪列:_wstart、_wend 和 _wduration;聚合函数:包括选择函数、可以由参数确定输出行数的时序特有函数,以及时序函数中的窗口计算和时间加权统计函数。
2. 窗口子句将数据按窗口进行切分,对每个窗口进行 SELECT 列表中的表达式的计算,SELECT 列表中的表达式只能包含:常量;伪列:_wstart、_wend 和 _wduration;聚合函数:包括选择函数、可以由参数确定输出行数的时序特有函数,以及时序函数中的窗口计算和时间加权统计函数。从 3.4.2.0 版本开始,SELECT 列表还支持列表达式和不定行函数,此时查询将进入窗口投影模式,每个窗口输出全部原始行而非一行聚合结果。
3. WHERE 语句可以指定查询的起止时间和其他过滤条件。

### 时间戳伪列
Expand Down
5 changes: 4 additions & 1 deletion docs/zh/14-reference/03-taos-sql/20-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: 查询数据的详细语法
```sql
SELECT {DATABASE() | CLIENT_VERSION() | SERVER_VERSION() | SERVER_STATUS() | NOW() | TODAY() | TIMEZONE() | CURRENT_USER() | USER() }

SELECT [hints] [DISTINCT] [TAGS] select_list
SELECT [hints] [DISTINCT] [TAGS] [SCALAR | AGG] select_list
from_clause
[WHERE condition]
[partition_by_clause]
Expand Down Expand Up @@ -119,6 +119,7 @@ true_for_expr: {
- interp_clause: interp 子句,与 interp 函数搭配使用,指定时间截面的记录值或者插值,可以指定插值的时间范围,输出时间间隔,插值类型。
- RANGE: 指定单个或者开始结束时间值,结束时间须大于开始时间,ts_val 为标准时间戳类型,surrounding_time_val 可选,指定时间范围,为正值,时间单位参见[时间单位](./01-datatype.md#时间单位)(不支持月/季/年)。如 ```RANGE('2023-10-01T00:00:00.000')``` 、```RANGE('2023-10-01T00:00:00.000', '2023-10-01T23:59:59.999')```。
- EVERY: 时间间隔范围,every_val 为正值,时间单位参见[时间单位](./01-datatype.md#时间单位)(不支持月/季/年),如 EVERY(1s)。
- SCALAR | AGG:窗口查询模式关键字(3.4.2.0 版本开始支持)。当窗口查询的 SELECT 列表中包含列表达式或不定行函数时,自动进入**窗口投影模式**,每个窗口输出其全部原始行;当 SELECT 列表只包含聚合函数时,进入**窗口聚合模式**,每个窗口输出一行聚合结果。当 SELECT 列表仅包含伪列、标签列、tbname、常量、分组键(group key)和状态键(state key)时,INTERVAL、SESSION、STATE_WINDOW、EVENT_WINDOW、COUNT_WINDOW 默认选择聚合模式,而 EXTERNAL_WINDOW 默认选择投影模式。此时可使用 `SCALAR` 或 `AGG` 关键字显式指定模式。详见 [TDengine TSDB 特色查询](../distinguished#窗口投影模式)。
- fill_clause: fill 子句,可以与 interp 函数、INTERVAL 窗口或 EXTERNAL_WINDOW 搭配使用,用于指定数据缺失时的数据填充方法。不同上下文支持的模式有所区别。
- group_by_expr: 指定数据分组聚合规则,支持表达式、函数、位置、列、别名。使用位置语法时必须出现在选择列中,如```select ts, current from meters order by ts desc,2```,2 对应 current 列。
- partition_by_expr: 指定数据切片条件,切片内的数据独立进行计算。支持表达式、函数、位置、列、别名。使用位置语法时必须出现在选择列中,如```select current from meters partition by 1```,1 对应 current 列。
Expand Down Expand Up @@ -359,6 +360,8 @@ NULL、NULL_F、VALUE、VALUE_F 这几种填充模式针对不同场景区别如

对于 EXTERNAL_WINDOW 查询,支持的模式为 `NONE`、`NULL`、`NULL_F`、`VALUE`、`VALUE_F`、`PREV`、`NEXT`,暂不支持 `LINEAR`、`NEAR` 和 `SURROUND`。

对于窗口投影模式(SELECT 列表包含列表达式或不定行函数),FILL 仅支持 `NONE`、`NULL`、`NULL_F`、`VALUE`、`VALUE_F`,不支持 `PREV`、`NEXT`、`LINEAR` 和 `NEAR`。

:::info

1. 使用 FILL 语句的时候可能生成大量的填充输出,务必指定查询的时间区间。针对每次查询,系统可返回不超过 1 千万条具有插值的结果。
Expand Down
Loading
Loading