Skip to content

Commit 128a8da

Browse files
Copilotkillme2008
andauthored
docs: Add regexp_extract function and Date64 support for date_format (#2192)
Signed-off-by: Dennis Zhuang <killme2008@gmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Dennis Zhuang <killme2008@gmail.com>
1 parent 8c1001a commit 128a8da

File tree

2 files changed

+70
-4
lines changed
  • docs/reference/sql/functions
  • i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions

2 files changed

+70
-4
lines changed

docs/reference/sql/functions/overview.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,40 @@ Where the `datatype` can be any valid Arrow data type in this [list](https://arr
4848
DataFusion [String Function](./df-functions.md#string-functions).
4949

5050
GreptimeDB provides:
51-
* `matches_term(expression, term)` for full text search.
51+
* `matches_term(expression, term)` for full text search. For details, read the [Fulltext Search](/user-guide/logs/fulltext-search.md).
52+
* `regexp_extract(str, regexp)` to extract the first substring in a string that matches a regular expression. Returns `NULL` if no match is found.
5253

53-
For details, read the [Fulltext Search](/user-guide/logs/fulltext-search.md).
54+
#### regexp_extract
55+
56+
Extracts the first substring in a string that matches a [regular expression](https://docs.rs/regex/latest/regex/#syntax). Returns `NULL` if no match is found.
57+
58+
```sql
59+
regexp_extract(str, regexp)
60+
```
61+
62+
**Arguments:**
63+
64+
- **str**: String expression to operate on. Can be a constant, column, or function, and any combination of operators.
65+
- **regexp**: Regular expression to match against. Can be a constant, column, or function.
66+
67+
**Note on Escaping:**
68+
69+
GreptimeDB's regex escape behavior differs between MySQL and PostgreSQL compatibility modes:
70+
- **MySQL mode**: Requires double backslashes for escape sequences (e.g., `\\d`, `\\s`)
71+
- **PostgreSQL mode**: Single backslashes work by default (e.g., `\d`, `\s`), or use `E''` prefix for consistency with MySQL (e.g., `E'\\d'`)
72+
73+
**Examples:**
74+
75+
```sql
76+
SELECT regexp_extract('version 1.2.3', '\d+\.\d+\.\d+');
77+
-- Returns: 1.2.3
78+
79+
SELECT regexp_extract('Phone: 123-456-7890', '\d{3}-\d{3}-\d{4}');
80+
-- Returns: 123-456-7890
81+
82+
SELECT regexp_extract('no match here', '\d+\.\d+\.\d+');
83+
-- Returns: NULL
84+
```
5485

5586
### Math Functions
5687

@@ -138,6 +169,8 @@ SELECT date_sub('2023-12-06 07:39:46.222'::TIMESTAMP_MS, '5 day'::INTERVAL);
138169

139170
* `date_format(expression, fmt)` to format Timestamp, Date, or DateTime into string by the format:
140171

172+
Supports `Date32`, `Date64`, and all `Timestamp` types.
173+
141174
```sql
142175
SELECT date_format('2023-12-06 07:39:46.222'::TIMESTAMP, '%Y-%m-%d %H:%M:%S:%3f');
143176
```

i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/overview.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,40 @@ arrow_cast(expression, datatype)
4747
DataFusion [字符串函数](./df-functions.md#string-functions)
4848

4949
GreptimeDB 提供:
50-
* `matches_term(expression, term)` 用于全文检索。
50+
* `matches_term(expression, term)` 用于全文检索。阅读[查询日志](/user-guide/logs/fulltext-search.md)文档获取更多详情。
51+
* `regexp_extract(str, regexp)` 提取字符串中与正则表达式匹配的第一个子串。如果没有找到匹配项则返回 `NULL`
5152

52-
阅读[查询日志](/user-guide/logs/fulltext-search.md)文档获取更多详情。
53+
#### regexp_extract
54+
55+
提取字符串中与[正则表达式](https://docs.rs/regex/latest/regex/#syntax)匹配的第一个子串。如果没有找到匹配项则返回 `NULL`
56+
57+
```sql
58+
regexp_extract(str, regexp)
59+
```
60+
61+
**参数:**
62+
63+
- **str**: 要操作的字符串表达式。可以是常量、列或函数,以及运算符的任意组合。
64+
- **regexp**: 要匹配的正则表达式。可以是常量、列或函数。
65+
66+
**关于转义的说明:**
67+
68+
GreptimeDB 在 MySQL 和 PostgreSQL 兼容模式下的正则表达式转义行为有所不同:
69+
- **MySQL 模式**:转义序列需要使用双反斜杠(例如 `\\d``\\s`
70+
- **PostgreSQL 模式**:默认情况下单反斜杠即可(例如 `\d``\s`),或者使用 `E''` 前缀以与 MySQL 保持一致(例如 `E'\\d'`
71+
72+
**示例:**
73+
74+
```sql
75+
SELECT regexp_extract('version 1.2.3', '\d+\.\d+\.\d+');
76+
-- 返回: 1.2.3
77+
78+
SELECT regexp_extract('Phone: 123-456-7890', '\d{3}-\d{3}-\d{4}');
79+
-- 返回: 123-456-7890
80+
81+
SELECT regexp_extract('no match here', '\d+\.\d+\.\d+');
82+
-- 返回: NULL
83+
```
5384

5485
### 数学函数
5586

@@ -134,6 +165,8 @@ SELECT date_sub('2023-12-06 07:39:46.222'::TIMESTAMP_MS, '5 day'::INTERVAL);
134165

135166
* `date_format(expression, fmt)` 将 Timestamp、Date 或 DateTime 格式化:
136167

168+
支持 `Date32``Date64` 和所有 `Timestamp` 类型。
169+
137170
```sql
138171
SELECT date_format('2023-12-06 07:39:46.222'::TIMESTAMP, '%Y-%m-%d %H:%M:%S:%3f');
139172
```

0 commit comments

Comments
 (0)