Skip to main content

ARRAY_JOIN

Description​

Combine all elements in the array into a new string based on the separator (sep) and the string to replace NULL values (null_replace).

Syntax​

ARRAY_JOIN(<arr> , <sep> [, <null_replace>])

Parameters​

ParameterDescription
<arr>An array to join
<sep>Separator string
<null_replace>String to replace NULL values

Return Value​

Returns a new string with the following special cases:

  • If <sep> is NULL, the function returns NULL.
  • If <null_replace> is NULL, the function also returns NULL.
  • If <sep> is an empty string, no separator is applied.
  • If <null_replace> is an empty string or not specified, NULL elements in the array are discarded directly.

Example​

CREATE TABLE array_test (
k1 INT,
k2 ARRAY<INT>
)
duplicate key (k1)
distributed by hash(k1) buckets 1
properties(
'replication_num' = '1'
);

INSERT INTO array_test VALUES
(1, [1, 2, 3, 4, 5]),
(2, [6, 7, 8]),
(3, []),
(4, NULL),
(5, [1, 2, 3, 4, 5, 4, 3, 2, 1]),
(6, [1, 2, 3, NULL]),
(7, [4, 5, 6, NULL, NULL]);
select k1, k2, array_join(k2, '_', 'null') from array_test order by k1;
+------+-----------------------------+------------------------------------+
| k1 | k2 | array_join(`k2`, '_', 'null') |
+------+-----------------------------+------------------------------------+
| 1 | [1, 2, 3, 4, 5] | 1_2_3_4_5 |
| 2 | [6, 7, 8] | 6_7_8 |
| 3 | [] | |
| 4 | NULL | NULL |
| 5 | [1, 2, 3, 4, 5, 4, 3, 2, 1] | 1_2_3_4_5_4_3_2_1 |
| 6 | [1, 2, 3, NULL] | 1_2_3_null |
| 7 | [4, 5, 6, NULL, NULL] | 4_5_6_null_null |
+------+-----------------------------+------------------------------------+
select k1, k2, array_join(k2, '_') from array_test order by k1;
+------+-----------------------------+----------------------------+
| k1 | k2 | array_join(`k2`, '_') |
+------+-----------------------------+----------------------------+
| 1 | [1, 2, 3, 4, 5] | 1_2_3_4_5 |
| 2 | [6, 7, 8] | 6_7_8 |
| 3 | [] | |
| 4 | NULL | NULL |
| 5 | [1, 2, 3, 4, 5, 4, 3, 2, 1] | 1_2_3_4_5_4_3_2_1 |
| 6 | [1, 2, 3, NULL] | 1_2_3 |
| 7 | [4, 5, 6, NULL, NULL] | 4_5_6 |
+------+-----------------------------+----------------------------+
CREATE TABLE array_test01 (
k1 INT,
k2 ARRAY<STRING>
)
duplicate key (k1)
distributed by hash(k1) buckets 1
properties(
'replication_num' = '1'
);

INSERT INTO array_test01 VALUES
(1, ['a', 'b', 'c', 'd']),
(2, ['e', 'f', 'g', 'h']),
(3, [NULL, 'a', NULL, 'b', NULL, 'c']),
(4, ['d', 'e', NULL, ' ']),
(5, [' ', NULL, 'f', 'g']);
select k1, k2, array_join(k2, '_', 'null') from array_test01 order by k1;
+------+-----------------------------------+------------------------------------+
| k1 | k2 | array_join(`k2`, '_', 'null') |
+------+-----------------------------------+------------------------------------+
| 1 | ['a', 'b', 'c', 'd'] | a_b_c_d |
| 2 | ['e', 'f', 'g', 'h'] | e_f_g_h |
| 3 | [NULL, 'a', NULL, 'b', NULL, 'c'] | null_a_null_b_null_c |
| 4 | ['d', 'e', NULL, ' '] | d_e_null_ |
| 5 | [' ', NULL, 'f', 'g'] | _null_f_g |
+------+-----------------------------------+------------------------------------+
select k1, k2, array_join(k2, '_') from array_test01 order by k1;
+------+-----------------------------------+----------------------------+
| k1 | k2 | array_join(`k2`, '_') |
+------+-----------------------------------+----------------------------+
| 1 | ['a', 'b', 'c', 'd'] | a_b_c_d |
| 2 | ['e', 'f', 'g', 'h'] | e_f_g_h |
| 3 | [NULL, 'a', NULL, 'b', NULL, 'c'] | a_b_c |
| 4 | ['d', 'e', NULL, ' '] | d_e_ |
| 5 | [' ', NULL, 'f', 'g'] | _f_g |
+------+-----------------------------------+----------------------------+