This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris-website.git
The following commit(s) were added to refs/heads/master by this push:
new 4fae2091d7b [doc] Support more types for to_json (#2864)
4fae2091d7b is described below
commit 4fae2091d7bf7216e7f3fb16da05ba8c4dbc5210
Author: Mryange <[email protected]>
AuthorDate: Mon Sep 15 16:51:09 2025 +0800
[doc] Support more types for to_json (#2864)
## Versions
- [x] dev
- [ ] 3.0
- [ ] 2.1
- [ ] 2.0
## Languages
- [x] Chinese
- [x] English
## Docs Checklist
- [ ] Checked by AI
- [ ] Test Cases Built
---
.../scalar-functions/json-functions/to-json.md | 69 +++++++++++++++++++-
.../scalar-functions/json-functions/to-json.md | 75 +++++++++++++++++++++-
2 files changed, 139 insertions(+), 5 deletions(-)
diff --git
a/docs/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
b/docs/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
index 272dfbb6176..210a4478729 100644
--- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
+++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
@@ -36,13 +36,21 @@ TO_JSON(value)
## Parameters
-**value** - The value to be converted to JSONB type. The following Doris data
types are supported:
+**value** - The value to be converted to JSONB type.
+
+The following types have direct mapping to JSONB types and can be converted
directly:
- Numeric types: TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE,
DECIMAL
- Boolean type: BOOLEAN
- String type: STRING, VARCHAR, CHAR
- Complex types: ARRAY, STRUCT
-Types not listed above (like DATE, DATETIME, etc.) are not supported directly
and must be first converted to supported types (typically STRING).
+Additionally, the function supports converting the following types:
+- Date types: DATETIME, DATE, TIME
+- IP types: IPV4, IPV6
+- Complex types: MAP
+
+For DATETIME, DATE, TIME, IPV4, IPV6 types that don't have corresponding JSONB
types, they will be converted to STRING type.
+For MAP type, it will be converted to JSONB Object type. The Map keys must be
STRING type, as JSON standard requires Object keys to be strings.
## Return Value
@@ -66,6 +74,41 @@ SELECT to_json(1), to_json(3.14), to_json("12345");
+------------+---------------+------------------+
```
+### Date types
+
+```sql
+SELECT
+ to_json(cast('2020-01-01' as date)) ,
+ to_json(cast('2020-01-01 12:00:00' as datetime)),
+ to_json(cast('2020-01-01 12:00:00.123' as datetime(3))),
+ to_json(cast('2020-01-01 12:00:00.123456' as datetime(6))),
+ to_json(cast('8:23:45' as time));
+```
+
+```text
++-------------------------------------+--------------------------------------------------+---------------------------------------------------------+------------------------------------------------------------+----------------------------------+
+| to_json(cast('2020-01-01' as date)) | to_json(cast('2020-01-01 12:00:00' as
datetime)) | to_json(cast('2020-01-01 12:00:00.123' as datetime(3))) |
to_json(cast('2020-01-01 12:00:00.123456' as datetime(6))) |
to_json(cast('8:23:45' as time)) |
++-------------------------------------+--------------------------------------------------+---------------------------------------------------------+------------------------------------------------------------+----------------------------------+
+| "2020-01-01" | "2020-01-01 12:00:00"
| "2020-01-01 12:00:00.123" |
"2020-01-01 12:00:00.123456" | "08:23:45"
|
++-------------------------------------+--------------------------------------------------+---------------------------------------------------------+------------------------------------------------------------+----------------------------------+
+```
+
+### IP types
+
+```sql
+SELECT
+ to_json(cast('192.168.0.1' as ipv4)) ,
+ to_json(cast('2001:0db8:85a3:0000:0000:8a2e:0370:7334' as ipv6));
+```
+
+```text
++--------------------------------------+------------------------------------------------------------------+
+| to_json(cast('192.168.0.1' as ipv4)) |
to_json(cast('2001:0db8:85a3:0000:0000:8a2e:0370:7334' as ipv6)) |
++--------------------------------------+------------------------------------------------------------------+
+| "192.168.0.1" | "2001:db8:85a3::8a2e:370:7334"
|
++--------------------------------------+------------------------------------------------------------------+
+```
+
### Array conversion
```sql
@@ -134,6 +177,28 @@ SELECT
json_extract(to_json(struct(123,array(4,5,6),"789")),"$.col2");
+----------------------------------------------------------------+
```
+### MAP conversion
+
+```sql
+SELECT to_json(map("1",2,"abc",3));
+```
+
+```text
++-----------------------------+
+| to_json(map("1",2,"abc",3)) |
++-----------------------------+
+| {"1":2,"abc":3} |
++-----------------------------+
+```
+
+```sql
+SELECT to_json(map(1,2));
+```
+
+```text
+to_json only support map with string-like key type
+```
+
### Handling NULL values
```sql
diff --git
a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
index e9682b99140..4e9f1bb7f17 100644
---
a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
+++
b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/to-json.md
@@ -26,7 +26,7 @@ under the License.
## 描述
-将 Doris 内部数据类型转换为 JSONB 类型。该函数允许将兼容的 Doris 数据类型转换为 JSON 表示形式,且不会丢失精度。
+`TO_JSON` 函数将 Doris 内部数据类型转换为 JSONB 类型。该函数允许将兼容的 Doris 数据类型转换为 JSON
表示形式,并且在转换过程中不会丢失精度。
## 语法
@@ -36,13 +36,21 @@ TO_JSON(value)
## 参数
-**value** - 要转换为 JSONB 类型的值。支持以下 Doris 数据类型:
+**value** - 要转换为 JSONB 类型的值。
+
+以下类型都有一一对应的 JSONB 类型,可以直接转换成 JSONB:
- 数字类型:TINYINT、SMALLINT、INT、BIGINT、LARGEINT、FLOAT、DOUBLE、DECIMAL
- 布尔类型:BOOLEAN
- 字符串类型:STRING、VARCHAR、CHAR
- 复杂类型:ARRAY、STRUCT
-未列出的类型(如 DATE、DATETIME 等)不直接支持,必须先转换为支持的类型(通常是 STRING)。
+此外,函数还支持以下类型的转换:
+- 日期类型:DATETIME、DATE、TIME
+- IP类型:IPV4、IPV6
+- 复杂类型:MAP
+
+对于 DATETIME、DATE、TIME、IPV4、IPV6 这些没有对应 JSONB 类型的数据,它们会被转换成 STRING 类型。
+对于 MAP 类型,会被转换成 JSONB 的 Object 类型。其中 Map
的键必须为STRING类型,这是因为JSON的标准规定,Object的Key只能为字符串。
## 返回值
@@ -66,6 +74,42 @@ SELECT to_json(1), to_json(3.14), to_json("12345");
+------------+---------------+------------------+
```
+### 日期类型
+
+```sql
+SELECT
+ to_json(cast('2020-01-01' as date)) ,
+ to_json(cast('2020-01-01 12:00:00' as datetime)),
+ to_json(cast('2020-01-01 12:00:00.123' as datetime(3))),
+ to_json(cast('2020-01-01 12:00:00.123456' as datetime(6))),
+ to_json(cast('8:23:45' as time));
+```
+
+```text
++-------------------------------------+--------------------------------------------------+---------------------------------------------------------+------------------------------------------------------------+----------------------------------+
+| to_json(cast('2020-01-01' as date)) | to_json(cast('2020-01-01 12:00:00' as
datetime)) | to_json(cast('2020-01-01 12:00:00.123' as datetime(3))) |
to_json(cast('2020-01-01 12:00:00.123456' as datetime(6))) |
to_json(cast('8:23:45' as time)) |
++-------------------------------------+--------------------------------------------------+---------------------------------------------------------+------------------------------------------------------------+----------------------------------+
+| "2020-01-01" | "2020-01-01 12:00:00"
| "2020-01-01 12:00:00.123" |
"2020-01-01 12:00:00.123456" | "08:23:45"
|
++-------------------------------------+--------------------------------------------------+---------------------------------------------------------+------------------------------------------------------------+----------------------------------+
+```
+
+
+### IP类型
+
+```sql
+SELECT
+ to_json(cast('192.168.0.1' as ipv4)) ,
+ to_json(cast('2001:0db8:85a3:0000:0000:8a2e:0370:7334' as ipv6));
+```
+
+```text
++--------------------------------------+------------------------------------------------------------------+
+| to_json(cast('192.168.0.1' as ipv4)) |
to_json(cast('2001:0db8:85a3:0000:0000:8a2e:0370:7334' as ipv6)) |
++--------------------------------------+------------------------------------------------------------------+
+| "192.168.0.1" | "2001:db8:85a3::8a2e:370:7334"
|
++--------------------------------------+------------------------------------------------------------------+
+```
+
### 数组转换
```sql
@@ -134,6 +178,31 @@ SELECT
json_extract(to_json(struct(123,array(4,5,6),"789")),"$.col2");
+----------------------------------------------------------------+
```
+
+### MAP转换
+
+```sql
+SELECT to_json(map(1,2));
+```
+
+```text
+to_json only support map with string-like key type
+```
+
+
+
+```sql
+SELECT to_json(map("1",2,"abc",3));
+```
+
+```text
++-----------------------------+
+| to_json(map("1",2,"abc",3)) |
++-----------------------------+
+| {"1":2,"abc":3} |
++-----------------------------+
+```
+
### 处理 NULL 值
```sql
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]