Last updated on
MAP_FROM_ARRAYS
Description
Constructs a MAP<K, V> from a key array and a value array. Elements at the same position form a key-value pair.
Syntax
MAP_FROM_ARRAYS(<keys>, <values>)
Parameters
| Parameter | Description |
|---|---|
<keys> | An ARRAY<K>. K must be a type supported by MAP keys. The array and its elements can be NULL. |
<values> | An ARRAY<V>. The array and its elements can be NULL. When both arrays are non-NULL, it must have the same number of elements as <keys>. |
Return Value
Returns a MAP<K, V>.
- If either input array is
NULL, returnsNULL. NULLelements are retained asNULLkeys or values in the result.- If both arrays are non-
NULLbut have different lengths, an error is reported. The length of the other array is not checked when either array isNULL. - If a key occurs more than once, the value from the last occurrence is retained.
- If an argument is not an array, or
Kis not a type supported by MAP keys, an analysis error is reported.
Examples
SELECT map_from_arrays([1, 2], [10, 20]) AS result;
+--------------+
| result |
+--------------+
| {1:10, 2:20} |
+--------------+
SELECT map_from_arrays([1, 1], [10, 20]) AS result;
+--------+
| result |
+--------+
| {1:20} |
+--------+
SELECT map_from_arrays(CAST(NULL AS ARRAY<INT>), [10]);
+-------------------------------------------------+
| map_from_arrays(CAST(NULL AS ARRAY<INT>), [10]) |
+-------------------------------------------------+
| NULL |
+-------------------------------------------------+
SELECT map_from_arrays(
array(CAST(NULL AS INT), 2),
array(10, CAST(NULL AS INT))) AS result;
+-------------------+
| result |
+-------------------+
| {null:10, 2:null} |
+-------------------+
SELECT map_from_arrays([1, 2], [10]);
ERROR 1105 (HY000): The key and value array offsets of function map_from_arrays must be identical