Last updated on
MAP_FROM_ENTRIES
Description
Constructs a MAP<K, V> from an array whose elements are two-field STRUCTs. The first field is used as the key and the second field as the value.
note
This function is supported since Apache Doris 4.2.
Syntax
MAP_FROM_ENTRIES(<entries>)
Parameters
| Parameter | Description |
|---|---|
<entries> | An ARRAY<STRUCT<K, V>>. Each STRUCT must contain exactly two fields. The array can be NULL, and the two fields inside a STRUCT can be NULL, but a STRUCT array element itself cannot be NULL. K must be a type supported by MAP keys. |
Return Value
Returns a MAP<K, V>.
- If
<entries>isNULL, returnsNULL. NULLfields inside a non-NULLSTRUCT are retained asNULLkeys or values.- If a STRUCT array element itself is
NULL, a runtime error is reported. - If the input is not an array of STRUCTs, or a STRUCT has fewer or more than two fields, an analysis error is reported.
- If
Kis not a type supported by MAP keys, an analysis error is reported. - If a key occurs more than once, the value from the last occurrence is retained.
Examples
SELECT map_from_entries(array(struct(1, 10), struct(2, 20))) AS result;
+--------------+
| result |
+--------------+
| {1:10, 2:20} |
+--------------+
SELECT map_from_entries(array(struct(1, 10), struct(1, 20))) AS result;
+--------+
| result |
+--------+
| {1:20} |
+--------+
SELECT map_from_entries(CAST(NULL AS ARRAY<STRUCT<k:INT,v:INT>>));
+------------------------------------------------------------+
| map_from_entries(CAST(NULL AS ARRAY<STRUCT<k:INT,v:INT>>)) |
+------------------------------------------------------------+
| NULL |
+------------------------------------------------------------+
SELECT map_from_entries(array(
struct(CAST(NULL AS INT), 10),
struct(2, CAST(NULL AS INT)))) AS result;
+-------------------+
| result |
+-------------------+
| {null:10, 2:null} |
+-------------------+
The following example fails because the array contains a NULL STRUCT element:
SELECT map_from_entries(array(CAST(NULL AS STRUCT<k:INT,v:INT>)));
ERROR 1105 (HY000): Map entry of function map_from_entries cannot be null
The following example fails during analysis because the STRUCT has three fields:
SELECT map_from_entries(array(struct(1, 2, 3)));
ERROR 1105 (HY000): map_from_entries requires an array of structs with exactly two fields