Skip to content

Commit 1f02695

Browse files
authored
docs: improve tql reference (#2153)
Signed-off-by: Dennis Zhuang <[email protected]>
1 parent 6883e91 commit 1f02695

File tree

2 files changed

+61
-14
lines changed
  • docs/reference/sql
  • i18n/zh/docusaurus-plugin-content-docs/current/reference/sql

2 files changed

+61
-14
lines changed

docs/reference/sql/tql.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@ description: Covers the TQL keyword for executing Time-Series Query Language in
55

66
# TQL
77

8-
The `TQL` keyword executes TQL language in SQL. The TQL is Time-Series Query Language, which is an extension for Prometheus's [PromQL](https://prometheus.io/docs/prometheus/latest/querying/basics/) in GreptimeDB.
8+
The `TQL` keyword executes TQL language in SQL. The TQL is Telemetry Query Language, which is an extension for Prometheus's [PromQL](https://prometheus.io/docs/prometheus/latest/querying/basics/) in GreptimeDB.
99

1010
## EVAL
1111

1212
### Syntax
1313

1414
```sql
15-
TQL [EVAL | EVALUATE] (start, end, step) expr
15+
TQL [EVAL | EVALUATE] (start, end, step, [lookback]) expr
1616
```
1717

1818
The `start`, `end` and `step` are the query parameters just like [Prometheus Query API](https://prometheus.io/docs/prometheus/latest/querying/api/):
1919

20-
- `start`: `<rfc3339 | unix_timestamp>`: Start timestamp, inclusive.
21-
- `end`: `<rfc3339 | unix_timestamp>`: End timestamp, inclusive.
22-
- `step`: `<duration | float>`: Query resolution step width in `duration` format or float number of seconds.
20+
- `start`: `<rfc3339 | unix_timestamp | expression>`: The start timestamp of the query; the range is inclusive of this value.
21+
- `end`: `<rfc3339 | unix_timestamp | expression>`: The end timestamp of the query; the range is inclusive of this value.
22+
- `step`: `<duration | float>`: The query resolution step, specified as a `duration` or a floating-point number of seconds.
23+
- `lookback`: `<duration | float>`: The maximum lookback duration for evaluation, default is 5 minutes and optional.
2324

24-
The `expr` is the TQL expression query string.
25+
`expr` is the TQL (PromQL) query string.
2526

2627
### Examples
2728

@@ -33,6 +34,28 @@ TQL eval (1677057993, 1677058993, '1m') rate(prometheus_http_requests_total{job=
3334

3435
will get a result just like other normal SQL queries.
3536

37+
`start` and `end` can also be time expressions that evaluate to constants. For example, to query the past 3 hours:
38+
39+
```sql
40+
TQL EVAL (now() - interval '3' hours, now(), '1m')
41+
sum by (namespace, pod) (
42+
increase(kube_pod_container_status_restarts_total[10m:30s])
43+
);
44+
```
45+
46+
To query data for the past day:
47+
48+
```sql
49+
TQL EVAL (
50+
date_trunc('day', now() - interval '1' day),
51+
date_trunc('day', now()),
52+
'1m'
53+
)
54+
sum by (namespace) (
55+
rate(http_requests_total[5m:30s])
56+
);
57+
```
58+
3659
## EXPLAIN
3760

3861
`EXPLAIN` displays both the logical plan and execution plan for a given PromQL query. The syntax is as follows:
@@ -47,7 +70,7 @@ For example, to explain the PromQL `sum by (instance) (rate(node_disk_written_by
4770
TQL EXPLAIN sum by (instance) (rate(node_disk_written_bytes_total[2m])) > 50;
4871
```
4972

50-
Notice that since the given query won't be actually executed, the triple `(start, end, step)` is not necessary. But you can still provide it like in `TQL EVAL`:
73+
Notice that since the given query won't be actually executed, the triple `(start, end, step, [lookback])` is not necessary. But you can still provide it like in `TQL EVAL`:
5174

5275
```
5376
TQL EXPLAIN (0, 100, '10s') sum by (instance) (rate(node_disk_written_bytes_total[2m])) > 50;

i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/tql.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,58 @@ description: 介绍了 `TQL` 关键字及其在 GreptimeDB 中的用法,包括
55

66
# TQL
77

8-
`TQL` 关键字在 SQL 中执行 TQL 语言。TQL 是 Time-Series Query Language 的缩写,是 GreptimeDB 中对 Prometheus 的 [PromQL](https://prometheus.io/docs/prometheus/latest/querying/basics/) 的扩展。
8+
`TQL` 关键字在 SQL 中执行 TQL 语言。TQL 是 Telemetry Query Language 的缩写,是 GreptimeDB 中对 Prometheus 的 [PromQL](https://prometheus.io/docs/prometheus/latest/querying/basics/) 的扩展。
99

1010
## EVAL
1111

1212
### Syntax
1313

1414
```sql
15-
TQL [EVAL | EVALUATE] (start, end, step) expr
15+
TQL [EVAL | EVALUATE] (start, end, step, [lookback]) expr
1616
```
1717

1818
`start`, `end``step` 是查询参数,就像 [Prometheus Query API](https://prometheus.io/docs/prometheus/latest/querying/api/) 一样:
1919

20-
- `start`: `<rfc3339 | unix_timestamp>`: Start 时间戳,范围中包含该值。
21-
- `end`: `<rfc3339 | unix_timestamp>`: End 时间戳,范围中包含该值。
20+
- `start`: `<rfc3339 | unix_timestamp | expression >`: 查询的起始时间戳,范围中包含该值。
21+
- `end`: `<rfc3339 | unix_timestamp | expression>`: 查询的截止时间戳,范围中包含该值。
2222
- `step`: `<duration | float>`: 查询分辨率步长,采用 `duration` 格式或浮点秒数。
23+
- `lookback`: `<duration | float>`: 查询评估的最大过去持续时间,默认 5 分钟,可选参数。
2324

24-
`expr` 是 TQL 表达式查询字符串
25+
`expr` 是 TQL (PromQL) 的查询字符串
2526

2627
### 示例
2728

2829
返回过去 5 分钟内 `http_requests_total` 指标的所有时间序列的每秒值:
2930

3031
```sql
31-
TQL eval (1677057993, 1677058993, '1m') rate(prometheus_http_requests_total{job="prometheus"}[5m]);
32+
TQL EVAL (1677057993, 1677058993, '1m')
33+
rate(prometheus_http_requests_total{job="prometheus"}[5m]);
3234
```
3335

3436
其查询结果和 SQL 查询结果类似。
3537

38+
`start``end` 还可以是可以被求值为常量的时间表达式,例如查询过去 3 个小时:
39+
40+
```sql
41+
TQL EVAL (now() - interval '3' hours, now(), '1m')
42+
sum by (namespace, pod) (
43+
increase(kube_pod_container_status_restarts_total[10m:30s])
44+
);
45+
```
46+
47+
查询过去一天的数据:
48+
```sql
49+
TQL EVAL (
50+
date_trunc('day', now() - interval '1' day),
51+
date_trunc('day', now()),
52+
'1m'
53+
)
54+
sum by (namespace) (
55+
rate(http_requests_total[5m:30s])
56+
);
57+
```
58+
59+
3660
## EXPLAIN
3761

3862
`EXPLAIN` 展示特定 PromQL 查询的逻辑计划和执行计划,其语法如下:
@@ -47,7 +71,7 @@ TQL EXPLAIN expr;
4771
TQL EXPLAIN sum by (instance) (rate(node_disk_written_bytes_total[2m])) > 50;
4872
```
4973

50-
注意该查询实际上没有被执行,所以 `(start, end, step)` 不是必需的,但你仍然可以像在 `TQL EVAL` 中一样提供这些参数:
74+
注意该查询实际上没有被执行,所以 `(start, end, step, [lookback])` 不是必需的,但你仍然可以像在 `TQL EVAL` 中一样提供这些参数:
5175

5276
```
5377
TQL EXPLAIN (0, 100, '10s') sum by (instance) (rate(node_disk_written_bytes_total[2m])) > 50;

0 commit comments

Comments
 (0)