# Greptime 双周精选 （4 月 10 日-4 月 23 日）｜在 PromQL 中实现了时序预测函数

Source: https://greptime.cn/blogs/2023-04-26-biweekly

## 内容概述

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

以下是这段时间较为重要的 PR 总结：

- 实现了 `show create table [table]` 语句

- 在 PromQL 中实现了时序预测函数

- 支持解析本地时间戳

- 集群模式实现了 `COPY`

- 将 substrait 的实现切换到 DataFusion 的实现

## 社区贡献者名单

在过去的两周里，共有 **5 位贡献者**，累计 **7 个 PR 被成功合并**，还有很多待合并的 PR。

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

- @[DiamondMofeng](https://github.com/DiamondMofeng) ([dashboard#199](https://github.com/GreptimeTeam/dashboard/pull/199))

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

- @[haohuaijin](https://github.com/haohuaijin) ([db#1362](https://github.com/GreptimeTeam/greptimedb/pull/1362) [db#1407](https://github.com/GreptimeTeam/greptimedb/pull/1407) [db#1438](https://github.com/GreptimeTeam/greptimedb/pull/1438))

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

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

👏 欢迎 @[nearsyh](https://github.com/nearsyh) 作为新的贡献者加入到我们的社区，并都成功合并了首个 PR！

同时衷心感谢我们所有的成员和贡献者！是你们的付出让我们的项目得以成功，也是你们让 GreptimeDB 成为一个更优质的产品。让我们一起努力，建立一个更棒的社区！

## PR 亮点

### [db#1238](https://github.com/GreptimeTeam/greptimedb/pull/1238) 将 substrait 的实现切换到 DataFusion 的实现

这个 PR 涵盖了更多的查询计划并且更贴近上游。这一改动也为即将到来的分布式计划功能提供支持。

### [db#1336](https://github.com/GreptimeTeam/greptimedb/pull/1336) 实现了 `show create table [table]` 语句

```sql
mysql> show create table monitor;
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                     |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| monitor | CREATE TABLE IF NOT EXISTS monitor (
  host STRING NULL,
  ts TIMESTAMP NOT NULL,
  cpu DOUBLE NULL DEFAULT 0,
  memory DOUBLE NULL,
  TIME INDEX (ts),
  PRIMARY KEY (host)
)
ENGINE=mito
WITH(
  regions = 1
) |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
```

主要变化：‍

- `create table` 语句实现了 `Display` 接口

- 从表信息中创建 `CreateTable` 语句

- 实现了 `show create table` 处理程序

- 修复了列注释丢失的问题

### [db#1307](https://github.com/GreptimeTeam/greptimedb/pull/1307) [db#1349](https://github.com/GreptimeTeam/greptimedb/pull/1349) 在 PromQL 中实现了时序预测函数

我们在 PromQL 中实现了 `holt_winters` 和 [`predict_linear`]( https://prometheus.io/docs/prometheus/latest/querying/functions/#predict_linear) 函数，并添加了一些单元测试。将 `linear_regression` 从 `deriv.rs` 调整到 `functions.rs` 中，这样可以在 `predict_linear` 中复用。

`holt_winters` 是一种"三重指数平滑"方法，用于平滑和预测时间序列数据。与其他 range 函数不同，holt_winters支持两个额外的参数 `sf` 和 `tf`。`predict_linear` 是一个预测未来值的函数。它和 [`deriv()`](https://prometheus.io/docs/prometheus/latest/querying/functions/#deriv) 一样基于简单的[线性回归](https://en.wikipedia.org/wiki/Simple_linear_regression)。同样的，`predict_linear` 接受一个额外的参数 `t` 来指定时间。

`#[range_fn]` util 宏不支持这两种格式，因此我们需要手动实现整个结构。和 `promql/functions/idelta.rs` 下的 `idelta` 类似。

### [db#1352](https://github.com/GreptimeTeam/greptimedb/pull/1352) 支持解析本地时间戳

这个 PR 改变了根据系统时区来解析没有明确时区信息的时间戳/日期字段的行为，系统时区是 GreptimeDB 实例运行时固有的 TZ 环境变量。比如说：

- `1970-01-01 08:00:00+0000` 将被解析为 unix 时间戳 28800，因为它的时间偏移为 0

- `1970-01-01 08:00:00+0800` 将被解析为 unix 时间戳 0，因为它的时间偏移为 `+0800`

- `1970-01-01 08:00:00` 没有明确的时间偏移，因此其对应的 unix 时间戳取决于 GreptimeDB 实例的当前时区。如果运行在 `CST` 时区（UTC+8），将被解析为 0；如果运行在 `UTC` 时区，则解析为 28800。

这个 PR 还将时区标识符添加到 `DateTime` 格式的字符串中。

```sql
mysql> CREATE table demo (ts TIMESTAMP(6) TIME INDEX, cnt INT);
Query OK, 0 rows affected (0.05 sec)

# Insert timestamp without time zone info, it will be convert to local timestamp.
mysql> insert into demo(ts,cnt) values ('2023-04-04 08:00:00.52', 1);
Query OK, 1 row affected (0.00 sec)

# queries also format time into local time zone.
mysql> select * from demo;
+------------------------------+------+
| ts                           | cnt  |
+------------------------------+------+
| 2023-04-04 08:00:00.520+0800 |    1 |
+------------------------------+------+
```

### [db#1388](https://github.com/GreptimeTeam/greptimedb/pull/1388) 集群模式实现了 `COPY`

这个 PR 支持了在集群模式下的 `COPY FROM/TO` 功能。无论是在单机模式还是集群模式下，“copy”都是将数据导入和导出 GreptimeDB 最快的方式，所以 GreptimeDB 集群非常需要这个功能。我们将原始实现从 Datanode 移动到 Frontend，并同步了测试模块。

## 新鲜事

- 我们对 GreptimeDB 的文档进行了一次升级，对其中的目录结构做了微调，修复了原先用户文档中的一些问题，希望可以给开发者带来更好的使用体验。

- Greptime Play 上线了！Play 是我们的交互式文档环境，用户可以在 Play 会话上执行 SQL 语句并得到实时的运行结果。Play 构建在内测中的 GreptimeCloud 上。我们将逐步上线更多的交互式文档，帮助用户熟悉 GreptimeDB 的功能特性。官网点击首页右上方的按钮：www.greptime.com，欢迎使用！

<figure>
<img src="/zh/blogs/2023-04-26-biweekly/image1.png" alt="（图 1：Greptime Play）" style="width: 80%; margin: 0 auto"/>
<figcaption>（图 1：Greptime Play）</figcaption>
</figure>

以上是本期《双周精选》的内容，欢迎关注公众号，我们会定期更新项目有关的进展，分享社区发生的新鲜事。同时，也可以通过各种方式参与 GreptimeDB 和其他相关项目。

## 关于 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/
