Skip to content

Commit 1c4f061

Browse files
authored
[fix] (function) support Concat_ws function multi arrays (#2634)
## Versions - [x] dev - [x] 3.0 - [ ] 2.1 - [ ] 2.0 ## Languages - [x] Chinese - [x] English ## Docs Checklist - [ ] Checked by AI - [ ] Test Cases Built
1 parent 35572f9 commit 1c4f061

File tree

4 files changed

+452
-34
lines changed
  • docs/sql-manual/sql-functions/scalar-functions/string-functions
  • i18n/zh-CN/docusaurus-plugin-content-docs
    • current/sql-manual/sql-functions/scalar-functions/string-functions
    • version-3.0/sql-manual/sql-functions/scalar-functions/string-functions
  • versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/string-functions

4 files changed

+452
-34
lines changed

docs/sql-manual/sql-functions/scalar-functions/string-functions/concat-ws.md

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,35 @@
77

88
## Description
99

10-
Use the first parameter sep as the connector to concatenate the second parameter and all subsequent parameters (or all strings in ARRAY) into a string. Special cases:
10+
Use the first parameter sep as the connector to concatenate the second parameter and all subsequent parameters (or all strings in one ARRAY or multi ARRAY ) into a string. Special cases:
1111

1212
- If the separator is NULL, NULL is returned.
1313

14-
The `CONCAT_WS` function does not skip empty strings, but skips NULL values.
15-
14+
- The `CONCAT_WS` function does not skip empty strings, but skips NULL values.
15+
- The `CONCAT_WS` function does not skip empty strings in any `ARRAY` parameters, but skips NULL values in `ARRAY`.
16+
- The `CONCAT_WS` function does not skip NULL parameter if input multi arrays,return empty string.
17+
- The first parameters must be a `string` type, and the others must be the same type ,belong to the `string` or `ARRAY` type
1618
## Syntax
1719

1820
```sql
1921
CONCAT_WS ( <sep> , <str> [ , <str> ] )
20-
CONCAT_WS ( <sep> , <array> )
22+
CONCAT_WS ( <sep> , <array> [ , <array> ])
2123
```
2224

2325
## Parameters
2426

2527
| Parameter | Description |
2628
|-------|-----------------|
27-
| `<sep>` | Connector for concatenating strings |
28-
| `<str>` | String to be concatenated |
29-
| `<array>` | Array to be concatenated |
29+
| `<sep>` | Connector for concatenating strings, it is `string` type or `varchar` type |
30+
| `<str>` | String to be concatenated , it is `string` or `varchar` type|
31+
| `<array>` | Array to be concatenated ,it is `ARRAY` type, and every element is `string` or `varchar` type|
3032

3133
## Return value
3234

3335
Parameter `<sep>` or `<array>` The string concatenated with `<str>`. Special cases:
3436

3537
- If delimiter is NULL, returns NULL.
38+
- If parameters with mutlti arrays and it contains a null,function will return empty string.
3639

3740
## Example
3841

@@ -62,4 +65,107 @@ SELECT CONCAT_WS("or", ["d", "is"]),CONCAT_WS(NULL, ["d", "is"]),CONCAT_WS("or",
6265
+------------------------------+------------------------------+------------------------------------+
6366
| doris | NULL | doris |
6467
+------------------------------+------------------------------+------------------------------------+
68+
```
69+
70+
Concatenating multiple arrays
71+
72+
```sql
73+
mysql> SELECT CONCAT_WS("-", ["a", "b"], ["c", NULL], ["d"]);
74+
75+
+------------------------------------------------+
76+
| CONCAT_WS("-", ["a", "b"], ["c", NULL], ["d"]) |
77+
+------------------------------------------------+
78+
| a-b-c-d |
79+
+------------------------------------------------+
80+
```
81+
82+
Handling NULL in string parameters
83+
84+
```sql
85+
mysql> SELECT CONCAT_WS("|", "hello", "", "world", NULL);
86+
87+
+--------------------------------------------+
88+
| CONCAT_WS("|", "hello", "", "world", NULL) |
89+
+--------------------------------------------+
90+
| hello||world |
91+
+--------------------------------------------+
92+
```
93+
94+
Return empty if NULL in multi arrays;
95+
96+
```sql
97+
mysql> SELECT CONCAT_WS("-", ["a", "b"], null,["c", NULL], ["d"]);
98+
+-----------------------------------------------------+
99+
| CONCAT_WS("-", ["a", "b"], null,["c", NULL], ["d"]) |
100+
+-----------------------------------------------------+
101+
| |
102+
+-----------------------------------------------------+
103+
```
104+
105+
Mixing strings and arrays (invalid)
106+
107+
```sql
108+
mysql> SELECT CONCAT_WS(",", "a", ["b", "c"]);
109+
110+
ERROR 1105 (HY000): errCode = 2, detailMessage = can not cast from origin type ARRAY<VARCHAR(1)> to target type=VARCHAR(65533)
111+
112+
```
113+
114+
All NULL inputs
115+
116+
```sql
117+
mysql> SELECT CONCAT_WS("x", NULL, NULL);
118+
119+
+----------------------------+
120+
| CONCAT_WS("x", NULL, NULL) |
121+
+----------------------------+
122+
| |
123+
+----------------------------+
124+
```
125+
126+
Chiese Charactors concat
127+
128+
```sql
129+
mysql> SELECT CONCAT_WS("x", '中文', '中文');
130+
131+
+------------------------------------+
132+
| CONCAT_WS("x", '中文', '中文') |
133+
+------------------------------------+
134+
| 中文x中文 |
135+
+------------------------------------+
136+
```
137+
138+
Chinese charactors in multi arrays
139+
140+
```sql
141+
mysql> SELECT CONCAT_WS("x", ['中文'], ['中文']);
142+
+----------------------------------------+
143+
| CONCAT_WS("x", ['中文'], ['中文']) |
144+
+----------------------------------------+
145+
| 中文x中文 |
146+
+----------------------------------------+
147+
```
148+
149+
Insert and concat them
150+
151+
```sql
152+
DROP TABLE IF EXISTS test_concat_ws_1;
153+
154+
CREATE TABLE test_concat_ws_1 (id INT, a ARRAY<VARCHAR>, b ARRAY<VARCHAR>) ENGINE=OLAP DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ('replication_num' = '1')
155+
156+
INSERT INTO test_concat_ws_1 VALUES (1, ['a','b'], ['css',null,'d']), (2, ['x',null], ['y','z']),(3,['你好','世界'],['Doris',null,'Nereids'])
157+
158+
SELECT concat_ws('-', a, b) FROM test_concat_ws_1 ORDER BY id
159+
160+
```
161+
162+
```text
163+
164+
+-----------------------------+
165+
| concat_ws('-', a, b) |
166+
+-----------------------------+
167+
| a-b-css-d |
168+
| x-y-z |
169+
| 你好-世界-Doris-Nereids |
170+
+-----------------------------+
65171
```

i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/string-functions/concat-ws.md

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,35 @@
77

88
## 描述
99

10-
使用第一个参数 sep 作为连接符,将第二个参数以及后续所有参数 (或 ARRAY 中的所有字符串) 拼接成一个字符串。特殊情况:
11-
12-
- 如果分隔符是 NULL,返回 NULL。
13-
14-
`CONCAT_WS`函数不会跳过空字符串,会跳过 NULL 值。
10+
使用第一个参数sep作为连接符,将第二个参数及后续所有参数(或一个数组、多个数组中的所有字符串)拼接成一个字符串。特殊情况:
1511

12+
- 若分隔符为 NULL,则返回 NULL。​
13+
- CONCAT_WS函数不跳过空字符串(""),但会跳过 NULL 值。​
14+
- CONCAT_WS函数不跳过任何数组参数中的空字符串,但会跳过数组中的NULL 值。​
15+
- ​CONCAT_WS函数不会跳过NULL如果输入多个数组参数,会返回空字符串。
16+
- 第一个参数必须为字符串类型(string 或 varchar),其他参数必须为相同类型,即均为字符串类型(string 或 varchar)或均为数组类型(ARRAY)。
1617
## 语法
1718

1819
```sql
1920
CONCAT_WS ( <sep> , <str> [ , <str> ] )
20-
CONCAT_WS ( <sep> , <array> )
21+
CONCAT_WS ( <sep> , <array> [ , <array> ])
2122
```
2223

2324
## 参数
2425

2526
| 参数 | 说明 |
2627
|-------|-----------------|
27-
| `<sep>` | 拼接字符串的连接符 |
28-
| `<str>` | 需要被拼接的字符串 |
29-
| `<array>` | 需要被拼接的 array 数组 |
28+
| `<sep>` | 用于拼接字符串的连接符,类型为 string 或 varchar |
29+
| `<str>` | 待拼接的字符串,类型为 string 或 varchar |
30+
| `<array>` | 待拼接的数组,类型为 ARRAY,且数组元素为 string 或 varchar |
3031

3132

3233
## 返回值
3334

3435
参数 `<sep>` 或者 `<array>` 数组使用 `<str>` 拼接后字符串。特殊情况:
3536

3637
- 如果分隔符是 NULL,返回 NULL。
37-
38+
- 如果多个数组参数中含有NULL参数,则返回空字符串
3839
## 举例
3940

4041
将字符串通过 or 拼接到一起
@@ -63,4 +64,106 @@ SELECT CONCAT_WS("or", ["d", "is"]),CONCAT_WS(NULL, ["d", "is"]),CONCAT_WS("or",
6364
+------------------------------+------------------------------+------------------------------------+
6465
| doris | NULL | doris |
6566
+------------------------------+------------------------------+------------------------------------+
67+
```
68+
拼接多个数组
69+
70+
```sql
71+
mysql> SELECT CONCAT_WS("-", ["a", "b"], ["c", NULL], ["d"]);
72+
73+
+------------------------------------------------+
74+
| CONCAT_WS("-", ["a", "b"], ["c", NULL], ["d"]) |
75+
+------------------------------------------------+
76+
| a-b-c-d |
77+
+------------------------------------------------+
78+
```
79+
80+
如果在多个数组参数中包含NULL参数,则返回空字符串;
81+
82+
```sql
83+
mysql> SELECT CONCAT_WS("-", ["a", "b"], null,["c", NULL], ["d"]);
84+
+-----------------------------------------------------+
85+
| CONCAT_WS("-", ["a", "b"], null,["c", NULL], ["d"]) |
86+
+-----------------------------------------------------+
87+
| |
88+
+-----------------------------------------------------+
89+
```
90+
91+
处理空字符串
92+
93+
```sql
94+
mysql> SELECT CONCAT_WS("|", "hello", "", "world", NULL);
95+
96+
+--------------------------------------------+
97+
| CONCAT_WS("|", "hello", "", "world", NULL) |
98+
+--------------------------------------------+
99+
| hello||world |
100+
+--------------------------------------------+
101+
```
102+
103+
混合字符串和数组(无效)
104+
105+
```sql
106+
mysql> SELECT CONCAT_WS(",", "a", ["b", "c"]);
107+
108+
ERROR 1105 (HY000): errCode = 2, detailMessage = can not cast from origin type ARRAY<VARCHAR(1)> to target type=VARCHAR(65533)
109+
110+
```
111+
112+
全为 NULL 的输入
113+
114+
```sql
115+
mysql> SELECT CONCAT_WS("x", NULL, NULL);
116+
117+
+----------------------------+
118+
| CONCAT_WS("x", NULL, NULL) |
119+
+----------------------------+
120+
| |
121+
+----------------------------+
122+
```
123+
124+
Chiese Charactors concat
125+
126+
```sql
127+
mysql> SELECT CONCAT_WS("x", '中文', '中文');
128+
129+
+------------------------------------+
130+
| CONCAT_WS("x", '中文', '中文') |
131+
+------------------------------------+
132+
| 中文x中文 |
133+
+------------------------------------+
134+
```
135+
136+
中文字符拼接
137+
138+
```sql
139+
mysql> SELECT CONCAT_WS("x", ['中文'], ['中文']);
140+
+----------------------------------------+
141+
| CONCAT_WS("x", ['中文'], ['中文']) |
142+
+----------------------------------------+
143+
| 中文x中文 |
144+
+----------------------------------------+
145+
```
146+
147+
插入数据并拼接
148+
149+
```sql
150+
DROP TABLE IF EXISTS test_concat_ws_1;
151+
152+
CREATE TABLE test_concat_ws_1 (id INT, a ARRAY<VARCHAR>, b ARRAY<VARCHAR>) ENGINE=OLAP DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ('replication_num' = '1')
153+
154+
INSERT INTO test_concat_ws_1 VALUES (1, ['a','b'], ['css',null,'d']), (2, ['x',null], ['y','z']),(3,['你好','世界'],['Doris',null,'Nereids'])
155+
156+
SELECT concat_ws('-', a, b) FROM test_concat_ws_1 ORDER BY id
157+
158+
```
159+
160+
```text
161+
162+
+-----------------------------+
163+
| concat_ws('-', a, b) |
164+
+-----------------------------+
165+
| a-b-css-d |
166+
| x-y-z |
167+
| 你好-世界-Doris-Nereids |
168+
+-----------------------------+
66169
```

0 commit comments

Comments
 (0)