Converts a struct array of key-value pairs into a multimap, where each unique key maps to an array of all associated values.
Syntax
multimap_from_entries(array<struct<K, V>>) -> map<K, array<V>>
Parameter
| Parameter | Type | Description |
|---|---|---|
array<struct<K, V>> |
array<struct> |
A struct array of key-value pairs. Each struct must have exactly two fields: a key and a value. |
Return value
Returns map<K, array<V>>. Duplicate keys are merged: their values are collected into a single array.
Edge cases
| Condition | Result |
|---|---|
Input array is null |
Returns null |
| A struct has fewer or more than 2 fields | Returns an error |
Any key is null |
Returns an error |
Example
The following query groups values by key. Key 1 appears twice, so both values are merged into an array.
SELECT multimap_from_entries(array(struct(1, 'a'),
struct(2, 'b'),
struct(1, 'c')));
Result:
{1: ['a', 'c'], 2: ['b']}
Related functions
MULTIMAP_FROM_ENTRIES is a complex type function. For more information about functions for processing complex types such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.