|
7 | 7 |
|
8 | 8 | ## Description
|
9 | 9 |
|
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: |
11 | 11 |
|
12 | 12 | - If the separator is NULL, NULL is returned.
|
13 | 13 |
|
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 |
16 | 18 | ## Syntax
|
17 | 19 |
|
18 | 20 | ```sql
|
19 | 21 | CONCAT_WS ( <sep> , <str> [ , <str> ] )
|
20 |
| -CONCAT_WS ( <sep> , <array> ) |
| 22 | +CONCAT_WS ( <sep> , <array> [ , <array> ]) |
21 | 23 | ```
|
22 | 24 |
|
23 | 25 | ## Parameters
|
24 | 26 |
|
25 | 27 | | Parameter | Description |
|
26 | 28 | |-------|-----------------|
|
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| |
30 | 32 |
|
31 | 33 | ## Return value
|
32 | 34 |
|
33 | 35 | Parameter `<sep>` or `<array>` The string concatenated with `<str>`. Special cases:
|
34 | 36 |
|
35 | 37 | - If delimiter is NULL, returns NULL.
|
| 38 | +- If parameters with mutlti arrays and it contains a null,function will return empty string. |
36 | 39 |
|
37 | 40 | ## Example
|
38 | 41 |
|
@@ -62,4 +65,107 @@ SELECT CONCAT_WS("or", ["d", "is"]),CONCAT_WS(NULL, ["d", "is"]),CONCAT_WS("or",
|
62 | 65 | +------------------------------+------------------------------+------------------------------------+
|
63 | 66 | | doris | NULL | doris |
|
64 | 67 | +------------------------------+------------------------------+------------------------------------+
|
| 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 | ++-----------------------------+ |
65 | 171 | ```
|
0 commit comments