Builds a MAP from two arrays: one for keys and one for values.
Syntax
map<K, V> map_from_arrays([string <mapDupKeyPolicy>,] array<K> <a>, array<V> <b>))Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
mapDupKeyPolicy | No | STRING | How to handle duplicate keys. Valid values: exception (returns an error) and last_win (the last occurrence overwrites earlier ones). Defaults to last_win if neither this parameter nor odps.sql.map.key.dedup.policy is set. |
a | Yes | array<K> | The key array. K can be any data type. |
b | Yes | array<V> | The value array. V can be any data type. |
Return value
Returns a value of the MAP type.
If
aorbis null, returns null.If
aandbhave different lengths, returns an error.If
acontains a null element, returns an error.
Usage notes
Duplicate key policy
Two mechanisms control how MAP_FROM_ARRAYS handles duplicate keys, applied in this order:
The
mapDupKeyPolicyparameter in the function call.The
odps.sql.map.key.dedup.policysession-level setting, used whenmapDupKeyPolicyis not specified.
If neither is set, the default policy is last_win.
To set the session-level policy:
set odps.sql.map.key.dedup.policy=exception;Examples
Basic usage
-- Returns {1:2, 3:4}
SELECT map_from_arrays(array(1.0, 3.0), array('2', '4'));Handling duplicate keys with `last_win`
When the key array contains duplicates, last_win keeps the value from the last occurrence.
-- Returns {1:2, 3:6}
-- The key 3 appears twice; last_win keeps '6' and discards '4'
SELECT map_from_arrays('last_win', array(1.0, 3.0, 3), array('2', '4', '6'));Related functions
MAP_FROM_ARRAYS is a complex type function. For more information about functions that process ARRAY, MAP, STRUCT, and JSON data, see Complex type functions.