# Range 查询中支持表达式、分布式 EXPLAIN ANALYZE | Greptime 双周精选

Source: https://greptime.cn/blogs/2024-05-22-biweekly

## 内容概述

作为一个成长中的开源项目，GreptimeDB 的进展离不开来自全球的社区贡献者们，感谢各位！

最近的内容更新如下：
- 引入 Flow Engine 提供持续聚合能力

- 可在 Range Query 中使用表达式，例如 `RANGE (INTERVAL '2' day - INTERVAL '1' day)`

- 支持在分布式下使用 `EXPLAIN ANALYZE` 分析查询执行的开销

## 社区贡献者名单

在过去的两周里，GreptimeDB 共合并了 86 个 PR，其中有 8 位独立贡献者，累计 12 个 PR 被成功合并，还有很多待合并的 PR。

祝贺以下各位在过去 2 周内成为我们活跃的贡献者：

- @[CookiePieWw](https://github.com/CookiePieWw) ([db#3932](https://github.com/GreptimeTeam/greptimedb/pull/3932))

- @[etolbakov](https://github.com/etolbakov) ([db#3924](https://github.com/GreptimeTeam/greptimedb/pull/3924))

- @[groobyming](https://github.com/groobyming) ([promql-parser#84](https://github.com/GreptimeTeam/promql-parser/pull/84))

- @[irenjj](https://github.com/irenjj) ([docs#940](https://github.com/GreptimeTeam/docs/pull/940) [docs#946](https://github.com/GreptimeTeam/docs/pull/946) [db#3910](https://github.com/GreptimeTeam/greptimedb/pull/3910))

- @[ltratt](https://github.com/ltratt) ([promql-parser#87](https://github.com/GreptimeTeam/promql-parser/pull/87))

- @[SergeTupchiy](https://github.com/SergeTupchiy) ([ingester-erl#40](https://github.com/GreptimeTeam/greptimedb-ingester-erl/pull/40))

- @[taobo](https://github.com/realtaobo) ([db#3901](https://github.com/GreptimeTeam/greptimedb/pull/3901) [db#3923](https://github.com/GreptimeTeam/greptimedb/pull/3923) [db#3995](https://github.com/GreptimeTeam/greptimedb/pull/3995))

- @[tizee](https://github.com/tizee) ([db#3751](https://github.com/GreptimeTeam/greptimedb/pull/3751))

👏 感谢各位独立贡献者持续提交并合并了多个 PR，还有更多来自其他独立贡献者的 PR 正在等待合并。

🎉 热烈欢迎 @[groobyming](https://github.com/groobyming) @[ltratt](https://github.com/ltratt) @[tizee](https://github.com/tizee)ima 作为新的贡献者加入社区并成功合并了首个 PR，还有更多来自其他独立贡献者的 PR 正在等待合并。

<img src="/zh/blogs/2024-05-22-biweekly/image1.png" alt="Contributor of GreptimeDB" style="width: 80%; margin: 0 auto"/>

## PR 亮点

### [db#3823](https://github.com/GreptimeTeam/greptimedb/pull/3823) 在 Range Query 中使用表达式

现在在 Range Query 中支持：

- 在  Range 和 Align 参数中支持 Interval 类型的计算：比如使用 `(INTERVAL '2' day - INTERVAL '1' day)`；

- 通过表达式指定查询对齐到的时间原点：比如 `(now() - INTERVAL '1' hour)` 指定了查询对齐到当前查询时间的一个小时之前。

```sql
SELECT 
    ts, 
    min(val) RANGE (INTERVAL '2' day - INTERVAL '1' day) 
FROM 
    host 
ALIGN (INTERVAL '2' day - INTERVAL '1' day)
    TO (now() - INTERVAL '1' hour) 
ORDER BY ts;
```

上述改进可以帮助用户更灵活地定义和控制时间范围，以满足特定的查询需求。

🌟 *Range Query 是 GreptimeDB 的扩展语法，更多信息：https://docs.greptime.com/reference/sql/range*

### [db#3908](https://github.com/GreptimeTeam/greptimedb/pull/3908) 支持 EXPLAIN ANALYZE 语句分析分布式查询性能

`EXPLAIN ANALYZE` 会执行对应的 SQL 语句。通过运行 `EXPLAIN ANALYZE`，用户可以详细了解分布式查询的性能，包括执行时间和资源利用率等指标。这个功能对于在分布式环境中优化和排查复杂查询至关重要。

```java
explain analyze SELECT count(*) FROM system_metrics;

+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| stage | node | plan                                                                                                                                                                            |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0     | 0    |  MergeScanExec: peers=[4402341478400(1025, 0), ] metrics=[output_rows: 1, greptime_exec_read_cost: 0, ready_time: 6352264, finish_time: 7509279, first_consume_time: 7165836, ] |
|       |      |                                                                                                                                                                                 |
| 1     | 0    |  AggregateExec: mode=Final, gby=[], aggr=[COUNT(greptime.public.system_metrics.ts)] metrics=[output_rows: 1, elapsed_compute: 108029, ]                                         |
|       |      |   CoalescePartitionsExec metrics=[output_rows: 32, elapsed_compute: 83055, ]                                                                                                    |
|       |      |     AggregateExec: mode=Partial, gby=[], aggr=[COUNT(greptime.public.system_metrics.ts)] metrics=[output_rows: 32, elapsed_compute: 334913, ]                                   |
|       |      |       RepartitionExec: partitioning=RoundRobinBatch(32), input_partitions=1 metrics=[repart_time: 1, fetch_time: 441565, send_time: 30325, ]                                    |
|       |      |         StreamScanAdapter { stream: "<SendableRecordBatchStream>" } metrics=[output_rows: 3, mem_used: 24, ]                                                                    |
|       |      |                                                                                                                                                                                 |
|       |      | Total rows: 1                                                                                                                                                                   |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```

### [db#3923](https://github.com/GreptimeTeam/greptimedb/pull/3923) 迁移依赖 orc-rs 至 datafusion-orc 

早些时候，我们将 orc-rs 捐赠至了 [datafusion-contrib 社区](https://github.com/datafusion-contrib)并与社区一同维护。最近主仓库的 [orc-rs](https://github.com/wenyxu/orc-rs) 依赖从迁移至了 [datafusion-orc](https://github.com/datafusion-contrib/datafusion-orc)。

### [db#3932](https://github.com/GreptimeTeam/greptimedb/pull/3932) 模糊测试校验已插入数据

在之前的工作中，我们为数据写入增加了模糊测试。在这个 PR 中，我们进一步增加了对已写入数据的校验，以确保写入的数据符合预期。

## Good First Issue

### [db#3997 为使用共享存储的 GreptimeDB 集群运行模糊测试](https://github.com/GreptimeTeam/greptimedb/issues/3997) 

在 [db#3967](https://github.com/GreptimeTeam/greptimedb/pull/3967) 中，我们在 CI 中运行 GreptimeDB 集群并运行模糊测试（Fuzz test）。为了进一步贴近真实使用场景，我们希望在 CI 中为使用共享存储的 GreptimeDB 集群也运行模糊测试。

关键词：CI

难度：中等

### [db#3973 为列类型变更的特性增加对应的模糊测试](https://github.com/GreptimeTeam/greptimedb/issues/3973)

在 [db#3517](https://github.com/GreptimeTeam/greptimedb/issues/3517) 中我们支持了列的数据类型变更，我们需要为特性增加对应的模糊测试。

关键词：模糊测试

难度：中等

### [db#3884 移除 query crate 中不必要的 traits 和 wrapper 类型](https://github.com/GreptimeTeam/greptimedb/issues/3884) 

大多数实现只是将请求转发到 Datafusion。由于我们与 Datafusion 高度耦合，并且没有计划支持另一个查询引擎，所以我们可以删除这些类型。

关键词：重构

难度：简单

## 关于 Greptime

[Greptime 格睿科技](https://greptime.cn/)专注于为可观测、物联网及车联网等领域提供实时、高效的数据存储和分析服务，帮助客户挖掘数据的深层价值。目前基于云原生的时序数据库 [GreptimeDB](https://github.com/GreptimeTeam/greptimedb) 已经衍生出多款适合不同用户的解决方案，更多信息或 demo 展示请联系下方小助手（微信号：greptime）。

欢迎对开源感兴趣的朋友们参与贡献和讨论，从带有 good first issue 标签的 issue 开始你的开源之旅吧～期待在开源社群里遇见你！添加小助手微信即可加入“技术交流群”与志同道合的朋友们面对面交流哦~

Star us on GitHub Now: https://github.com/GreptimeTeam/greptimedb

官网：https://greptime.cn/

文档：https://docs.greptime.cn/

Twitter: https://twitter.com/Greptime

Slack: https://greptime.com/slack

LinkedIn: https://www.linkedin.com/company/greptime/
