# Greptime 双周精选 （7.31-8.13）｜支持 Interval 类型

Source: https://greptime.cn/blogs/2023-08-16-biweekly

## 内容概述

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

过去两周，我们所有项目中共有 **119** 个 PR 被合并，以下是这段时间较为重要的 PR 总结：

- 即使表不存在，也支持接受不带时间戳的 InfluxDB 请求

- 数据库内支持了 Interval 类型，同时 gRPC 通信层也对该类型进行了支持

- 单机模式下支持 Truncate Table

- 支持重写 RangeSelect Logical Plan

## 社区贡献者名单

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

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

- [@DevilExileSu](https://github.com/DevilExileSu) ([db#2084](https://github.com/GreptimeTeam/greptimedb/pull/2084) [db#2090](https://github.com/GreptimeTeam/greptimedb/pull/2090) [db#2097](https://github.com/GreptimeTeam/greptimedb/pull/2097))

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

- [@gongzhengyang](https://github.com/gongzhengyang) ([db#2055](https://github.com/GreptimeTeam/greptimedb/pull/2055) [db#2116](https://github.com/GreptimeTeam/greptimedb/pull/2116))

- [@niebayes](https://github.com/niebayes) ([docs#509](https://github.com/GreptimeTeam/docs/pull/509))

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

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

- [@quakewang](https://github.com/quakewang) ([docs#521](https://github.com/GreptimeTeam/docs/pull/521))

- [@QuenKar](https://github.com/QuenKar) ([db#1952](https://github.com/GreptimeTeam/greptimedb/pull/1952) [db#2043](https://github.com/GreptimeTeam/greptimedb/pull/2043) [db#2064](https://github.com/GreptimeTeam/greptimedb/pull/2064) [db#2117](https://github.com/GreptimeTeam/greptimedb/pull/2117) [db#2146](https://github.com/GreptimeTeam/greptimedb/pull/2146))

- [@sjcsjc123](https://github.com/sjcsjc123) ([gtctl#86](https://github.com/GreptimeTeam/gtctl/pull/86) [gtctl#119](https://github.com/GreptimeTeam/gtctl/pull/119))

- [@sunray-ley](https://github.com/sunray-ley) ([db#2044](https://github.com/GreptimeTeam/greptimedb/pull/2044))

- [@Taylor-lagrange](https://github.com/Taylor-lagrange) ([db#2058](https://github.com/GreptimeTeam/greptimedb/pull/2058))

- [@xie-zheng](https://github.com/xie-zheng) ([db#2143](https://github.com/GreptimeTeam/greptimedb/pull/2143))

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

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

## PR 亮点

### [db#2041](https://github.com/GreptimeTeam/greptimedb/pull/2041) 即使表不存在，也支持不带时间戳的 InfluxDB 请求

原来，当相应的表存在时，我们可以执行以下命令，但当相应的表不存在时，我们无法执行该命令：

```rust
curl -i -XPOST "http://localhost:4000/v1/influxdb/write?db=public&precision=ms" \
--data-binary \
'monitor,host=127.0.0.1 cpu=0.1,memory=0.4'
```

现在我们支持了 GreptimeDB 在相应表不存在的情况下接受不带时间戳的 InfluxDB 请求。

### [db#2064](https://github.com/GreptimeTeam/greptimedb/pull/2064) 数据库内支持了 Interval 类型，同时 gRPC 通信层也对该类型进行了支持

在 [db#1952](https://github.com/GreptimeTeam/greptimedb/pull/1952) 中，我们：

- 在 common_time 包中实现了 `Interval` 类型

- 为 IntervalMonthDayNano 实现了 `DataType`, `Vector`, `Value` 特性

同时我们修复了 [db#1886](https://github.com/GreptimeTeam/greptimedb/pull/1886) 中的 bug。现在，我们实现了基本的 Interval 类型，并且在 gRPC 通信层也支持了对应类型。

### [db#2090](https://github.com/GreptimeTeam/greptimedb/pull/2090) 单机模式下支持 Truncate Table

现在我们在单机模式下支持了 Truncate Table 的存储过程、重置版本和 Truncate region，其中包括：

- 获取 `RegionWriter` 锁
-  创建 `RegionMetaAction::Truncate` 来存储 `committed_sequence`，并将其持久化，以便从 manifest 中恢复

- 将 WAL 中的所有数据标记为 `obsolete`

- 将所有 SST 标记为已删除

-  重置版本

我们还支持从 `RegionMetaAction::Truncate` 恢复，包括：

-  将所有 SST 标记为已删除

-  重置版本，并将来自 `RegionMetaAction::Truncate` 的 `committed_sequence` 设置为版本的 `flushed_sequence`

### [db#2058](https://github.com/GreptimeTeam/greptimedb/pull/2058) 支持重写 RangeSelect Logical plan

我们现在在 GreptimeDB 中支持 RangeSelect，包括：

- 将 sqlparser 更改为可解析范围选择语法的新版本

- 使用新版 sqlparser 实现的 `Into` trait，将 sqlparser 结构转换为 datafusion 的 sqlparser 结构，因此我们仍可使用 datafusion 分析逻辑计划

实现 Logical plan rewriter 包括：

- 递归跟踪 Logical plan tree，使用标量 UDF 函数 `range_fn` 查找投影计划

- 收集 `range_fn` 中的所有参数，生成 RangeSelect 计划

- 将 RangeSelect 计划插入 Logical plan tree

- 完成 Logical plan 的重写

注意：如果 Logical plan tree 中没有找到使用标量 UDF 函数 `range_fn` 的投影计划，则不会重新生成任何计划，Logical plan rewriter 的成本很低，而且是递归的，因此可以支持任意嵌套的范围选择。

RangeSelect 计划的唯一要求是输入模式必须包含数据类型为 `Timestamp` 的时间索引列，如果没有给出 `by` 关键字，默认 `by` 将是表的行键。

## 新鲜事

### 新增官方 [js-sdk](https://github.com/GreptimeTeam/greptime-js-sdk)

我们已经发布了 greptime-js-sdk，它基于 typescript 开发，封装了常用的 SQL 建表、插入和查询语句，同时也封装了 PromQL 查询。后续我们会继续完善对不同协议和语法的支持，并不断完善文档，进一步降低使用 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/
