array_union

description

Syntax

  1. ARRAY<T> array_union(ARRAY<T> array1, ARRAY<T> array2)

Returns an array of the elements in the union of array1 and array2, without duplicates. If the input parameter is null, null is returned.

notice

Only supported in vectorized engine

example

  1. mysql> set enable_vectorized_engine=true;
  2. mysql> select k1,k2,k3,array_union(k2,k3) from array_type_table;
  3. +------+-----------------+--------------+-------------------------+
  4. | k1 | k2 | k3 | array_union(`k2`, `k3`) |
  5. +------+-----------------+--------------+-------------------------+
  6. | 1 | [1, 2, 3] | [2, 4, 5] | [1, 2, 3, 4, 5] |
  7. | 2 | [2, 3] | [1, 5] | [2, 3, 1, 5] |
  8. | 3 | [1, 1, 1] | [2, 2, 2] | [1, 2] |
  9. +------+-----------------+--------------+-------------------------+
  10. mysql> select k1,k2,k3,array_union(k2,k3) from array_type_table_nullable;
  11. +------+-----------------+--------------+-------------------------+
  12. | k1 | k2 | k3 | array_union(`k2`, `k3`) |
  13. +------+-----------------+--------------+-------------------------+
  14. | 1 | [1, NULL, 3] | [1, 3, 5] | [1, NULL, 3, 5] |
  15. | 2 | [NULL, NULL, 2] | [2, NULL, 4] | [NULL, 2, 4] |
  16. | 3 | NULL | [1, 2, 3] | NULL |
  17. +------+-----------------+--------------+-------------------------+
  18. mysql> select k1,k2,k3,array_union(k2,k3) from array_type_table_varchar;
  19. +------+----------------------------+----------------------------------+---------------------------------------------------+
  20. | k1 | k2 | k3 | array_union(`k2`, `k3`) |
  21. +------+----------------------------+----------------------------------+---------------------------------------------------+
  22. | 1 | ['hello', 'world', 'c++'] | ['I', 'am', 'c++'] | ['hello', 'world', 'c++', 'I', 'am'] |
  23. | 2 | ['a1', 'equals', 'b1'] | ['a2', 'equals', 'b2'] | ['a1', 'equals', 'b1', 'a2', 'b2'] |
  24. | 3 | ['hasnull', NULL, 'value'] | ['nohasnull', 'nonull', 'value'] | ['hasnull', NULL, 'value', 'nohasnull', 'nonull'] |
  25. | 4 | ['hasnull', NULL, 'value'] | ['hasnull', NULL, 'value'] | ['hasnull', NULL, 'value'] |
  26. +------+----------------------------+----------------------------------+---------------------------------------------------+
  27. mysql> select k1,k2,k3,array_union(k2,k3) from array_type_table_decimal;
  28. +------+------------------+-------------------+----------------------------+
  29. | k1 | k2 | k3 | array_union(`k2`, `k3`) |
  30. +------+------------------+-------------------+----------------------------+
  31. | 1 | [1.1, 2.1, 3.44] | [2.1, 3.4, 5.4] | [1.1, 2.1, 3.44, 3.4, 5.4] |
  32. | 2 | [NULL, 2, 5] | [NULL, NULL, 5.4] | [NULL, 2, 5, 5.4] |
  33. | 4 | [1, NULL, 2, 5] | [1, 3.1, 5.4] | [1, NULL, 2, 5, 3.1, 5.4] |
  34. +------+------------------+-------------------+----------------------------+

keywords

ARRAY,UNION,ARRAY_UNION