# TQL CTE 支持、PromQL 兼容性提升与 Remote WAL 裁剪优化｜Greptime 双周精选

Source: https://greptime.cn/blogs/2025-08-20-greptimedb-biweekly-report

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

最近的更新内容如下：

- **更灵活的查询**：为 TQL 引入 CTE，并可与 SQL CTE 混合使用
- 新增 `mysql.prepared_stmt_cache_size` 配置，用于控制缓存的 Prepared Statement 数量
- **PromQL 表达式兼容性提高**：将 `label_join` 和 `label_replace` 的行为与 Prometheus 对齐
- 为 Flat Parquet 格式实现 Last-not-null 和 Last-row 去重策略

## 社区贡献者名单

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

祝贺以下各位在过去 2 周内成为我们最突出的贡献者：
 
> 注：按照 GitHub 用户名首字母顺序排列
> - @**[kemingy](https://github.com/kemingy)** *[docs#2033](https://github.com/GreptimeTeam/docs/pull/2033)*
> - @**[sunheyi6](https://github.com/sunheyi6)** *[db#6639](https://github.com/GreptimeTeam/greptimedb/pull/6639)*
> - @**[yihong0618](https://github.com/yihong0618)** *[db#6720](https://github.com/GreptimeTeam/greptimedb/pull/6720) [db#6718](https://github.com/GreptimeTeam/greptimedb/pull/6718) [db#6714](https://github.com/GreptimeTeam/greptimedb/pull/6714) [db#6708](https://github.com/GreptimeTeam/greptimedb/pull/6708)*

👏 欢迎 @*[sunheyi6](https://github.com/sunheyi6)* 作为新的贡献者加入到社区，并成功合并了 PR，还有更多来自其他独立贡献者的 PR 正在等待合并。

<figure>
<img src="/zh/blogs/2025-08-20-greptimedb-biweekly-report/greptimedbcontributor.png" alt="（图 1：GreptimeDB 双周内新增贡献者）" style="width: 80%; margin: 0 auto"/>
<figcaption>（图 1：GreptimeDB 双周内新增贡献者）</figcaption>
</figure>

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

## PR 亮点

### [db#6639 新增 `mysql.prepared_stmt_cache_size` 配置](https://github.com/GreptimeTeam/greptimedb/pull/6639)

引入了一个新的配置项，用于控制 MySQL prepared statement 的缓存数量，默认值为 `10000`。

### [db#6645 TQL 支持 CTE (Common Table Expression)](https://github.com/GreptimeTeam/greptimedb/pull/6645)

为 TQL 引入了 CTE 支持，并允许与 SQL CTE 混合使用。例如，可先使用 TQL 查询数据，再通过 SQL 子查询进行过滤，实现更灵活的查询组合。

```sql
-- TQL CTE with column aliases
WITH tql (the_timestamp, the_value) as (
    TQL EVAL (0, 40, '10s') metric
)
SELECT * FROM tql;

+---------------------+-----------+
| the_timestamp       | the_value |
+---------------------+-----------+
| 1970-01-01T00:00:00 | 0.0       |
| 1970-01-01T00:00:10 | 8.0       |
| 1970-01-01T00:00:20 | 8.0       |
| 1970-01-01T00:00:30 | 2.0       |
| 1970-01-01T00:00:40 | 3.0       |
+---------------------+-----------+

-- Hybrid CTEs (TQL + SQL)
WITH 
    tql_data (ts, val) AS (TQL EVAL (0, 40, '10s') metric),
    filtered AS (SELECT * FROM tql_data WHERE val > 5)
SELECT count(*) FROM filtered;

+----------+
| count(*) |
+----------+
| 2        |
+----------+
```

### [db#6714](https://github.com/GreptimeTeam/greptimedb/pull/6714) [db#6720 提升 PromQL 表达式兼容性](https://github.com/GreptimeTeam/greptimedb/pull/6720)

修复了 `label_join` 与 `label_replace` 的行为，使其与 Prometheus 保持一致，从而提升 PromQL 表达式的兼容性。

### [db#6691 新增 SQL 格式化 HTTP 接口](https://github.com/GreptimeTeam/greptimedb/pull/6691)

引入 `/v1/sql/format` 接口，用于格式化 SQL 语句。

示例如下：

```bash
curl -s "http://localhost:4000/v1/sql/format?sql=select%201%20as%20x"
```

输出结果为：

```plaintext
{
  "formatted": "SELECT 1 AS x;"
}
```

### [db#6741 优化 Remote WAL 裁剪策略](https://github.com/GreptimeTeam/greptimedb/pull/6471)

通过统计信息估算数据大小进行裁剪，确保在启动 Datanode 时，即使在最坏情况下，也不会回放过多数据。

### [db#6709](https://github.com/GreptimeTeam/greptimedb/pull/6709) [db#6695 为 Flat Parquet 格式实现去重策略](https://github.com/GreptimeTeam/greptimedb/pull/6695)

支持 Last-not-null 和 Last-row 两种去重策略。


## Good First Issue

### [Issue#6287 允许用户在 `COPY TO` 时指定日期和时间戳格式](https://github.com/GreptimeTeam/greptimedb/pull/6287)

关键字：时间戳格式

难度：简单

### [Issue#6286 支持导出压缩的 CSV 或 JSON 文件](https://github.com/GreptimeTeam/greptimedb/pull/6286)

关键字：数据导出

难度：简单

### [Issue#6334 完善 `KILL` 对 `INSERT INTO SELECT` 查询的支持](https://github.com/GreptimeTeam/greptimedb/pull/6334)

关键字：查询引擎

难度：中等
