TQL

TQL 关键字在 SQL 中执行 TQL 语言。TQL 是 Time-Series Query Language 的缩写,是 GreptimeDB 中对 Prometheus 的 PromQL 的扩展。

EVAL

Syntax

sql

  1. TQL [EVAL | EVALUATE] (start, end, step) expr

start, endstep 是查询参数,就像 Prometheus Query API 一样:

  • start: <rfc3339 | unix_timestamp>: Start 时间戳, 范围中包含该值。
  • end: <rfc3339 | unix_timestamp>: End 时间戳, 范围中包含该值。
  • step: <duration | float>: 查询分辨率步长,采用 duration 格式或浮点秒数。

expr 是 TQL 表达式查询字符串。

示例

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

sql

  1. TQL eval (1677057993, 1677058993, '1m') rate(prometheus_http_requests_total{job="prometheus"}[5m]);

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

EXPLAIN

EXPLAIN 展示特定 PromQL 查询的逻辑计划和执行计划,其语法如下:

  1. TQL EXPLAIN expr;

例如,我们可以使用下方示例解释 PromQL sum by (instance) (rate(node_disk_written_bytes_total[2m])) > 50

  1. TQL EXPLAIN sum by (instance) (rate(node_disk_written_bytes_total[2m])) > 50;

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

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

结果如下:

txt

  1. +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  2. | plan_type | plan |
  3. +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  4. | logical_plan | Sort: node_disk_written_bytes_total.instance ASC NULLS LAST, node_disk_written_bytes_total.ts ASC NULLS LAST
  5. Filter: SUM(prom_rate(ts_range,field,ts)) > Float64(50)
  6. Aggregate: groupBy=[[node_disk_written_bytes_total.instance, node_disk_written_bytes_total.ts]], aggr=[[SUM(prom_rate(ts_range,field,ts))]]
  7. Projection: node_disk_written_bytes_total.ts, prom_rate(ts_range, field, node_disk_written_bytes_total.ts) AS prom_rate(ts_range,field,ts), node_disk_written_bytes_total.instance
  8. Filter: prom_rate(ts_range, field, node_disk_written_bytes_total.ts) IS NOT NULL
  9. Projection: node_disk_written_bytes_total.ts, node_disk_written_bytes_total.instance, field, ts_range
  10. PromRangeManipulate: req range=[0..0], interval=[300000], eval range=[120000], time index=[ts], values=["field"]
  11. PromSeriesNormalize: offset=[0], time index=[ts], filter NaN: [true]
  12. PromSeriesDivide: tags=["instance"]
  13. Sort: node_disk_written_bytes_total.instance DESC NULLS LAST, node_disk_written_bytes_total.ts DESC NULLS LAST
  14. TableScan: node_disk_written_bytes_total projection=[ts, instance, field], partial_filters=[ts >= TimestampMillisecond(-420000, None), ts <= TimestampMillisecond(300000, None)] |
  15. | physical_plan | SortPreservingMergeExec: [instance@0 ASC NULLS LAST,ts@1 ASC NULLS LAST]
  16. SortExec: expr=[instance@0 ASC NULLS LAST,ts@1 ASC NULLS LAST]
  17. CoalesceBatchesExec: target_batch_size=8192
  18. FilterExec: SUM(prom_rate(ts_range,field,ts))@2 > 50
  19. AggregateExec: mode=FinalPartitioned, gby=[instance@0 as instance, ts@1 as ts], aggr=[SUM(prom_rate(ts_range,field,ts))]
  20. CoalesceBatchesExec: target_batch_size=8192
  21. RepartitionExec: partitioning=Hash([Column { name: "instance", index: 0 }, Column { name: "ts", index: 1 }], 32), input_partitions=32
  22. AggregateExec: mode=Partial, gby=[instance@2 as instance, ts@0 as ts], aggr=[SUM(prom_rate(ts_range,field,ts))]
  23. ProjectionExec: expr=[ts@0 as ts, prom_rate(ts_range@3, field@2, ts@0) as prom_rate(ts_range,field,ts), instance@1 as instance]
  24. CoalesceBatchesExec: target_batch_size=8192
  25. FilterExec: prom_rate(ts_range@3, field@2, ts@0) IS NOT NULL
  26. ProjectionExec: expr=[ts@0 as ts, instance@1 as instance, field@2 as field, ts_range@3 as ts_range]
  27. PromInstantManipulateExec: req range=[0..0], interval=[300000], eval range=[120000], time index=[ts]
  28. PromSeriesNormalizeExec: offset=[0], time index=[ts], filter NaN: [true]
  29. PromSeriesDivideExec: tags=["instance"]
  30. RepartitionExec: partitioning=RoundRobinBatch(32), input_partitions=1
  31. ExecutionPlan(PlaceHolder)
  32. |
  33. +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

ANALYZE

TQL 同样支持 ANALYZE 关键词来分析给定 PromQL 查询的执行,其语法如下:

  1. TQL ANALYZE (start, end, step) expr;

例如:

  1. TQL ANALYZE (0, 10, '5s') test;

得到结果:

  1. +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  2. | plan_type | plan |
  3. +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  4. | Plan with Metrics | CoalescePartitionsExec, metrics=[output_rows=0, elapsed_compute=14.99µs]
  5. PromInstantManipulateExec: range=[0..10000], lookback=[300000], interval=[5000], time index=[j], metrics=[output_rows=0, elapsed_compute=1.08µs]
  6. PromSeriesNormalizeExec: offset=[0], time index=[j], filter NaN: [false], metrics=[output_rows=0, elapsed_compute=1.11µs]
  7. PromSeriesDivideExec: tags=["k"], metrics=[output_rows=0, elapsed_compute=1.3µs]
  8. RepartitionExec: partitioning=RoundRobinBatch(32), input_partitions=32, metrics=[send_time=32ns, repart_time=32ns, fetch_time=11.578016ms]
  9. RepartitionExec: partitioning=RoundRobinBatch(32), input_partitions=1, metrics=[send_time=1ns, repart_time=1ns, fetch_time=21.07µs]
  10. ExecutionPlan(PlaceHolder), metrics=[]
  11. |
  12. +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+