Returns the union of multiple maps. When the same key appears in more than one map, the value from the last map containing that key takes precedence by default.
Syntax
map<K, V> map_concat([string <mapDupKeyPolicy>,] map<K, V> <a>, map<K, V> <b>[,...])
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
mapDupKeyPolicy |
No | STRING | Controls how duplicate keys are handled. Valid values: exception, last_win. If omitted, the value of the session-level parameter odps.sql.map.key.dedup.policy is used. The default is last_win. |
a, b, ... |
Yes | MAP | The maps to merge. All maps must share the same key type and the same value type. K and V in map<K, V> represent the key type and value type. |
Return value
Returns a value of the MAP type.
Usage notes
Duplicate key handling
Set mapDupKeyPolicy to control what happens when the same key appears in multiple maps:
-
last_win: The value from the last map containing that key overwrites earlier values. This is the default. -
exception: An error is returned if any duplicate keys are found.
To apply a policy across all queries in a session without specifying mapDupKeyPolicy each time, set odps.sql.map.key.dedup.policy at the session level:
set odps.sql.map.key.dedup.policy=exception;
Null and type mismatch behavior
-
If a map is null or a key within a map is null,
nullor an error is returned. -
If the maps have different key types or different value types, an error is returned.
Examples
Example 1: Merge two maps with no duplicate keys
-- Return value: {1:a, 2:b, 3:c}
select map_concat(map(1, 'a', 2, 'b'), map(3, 'c'));
The two input maps share no keys, so the result is a straightforward union of all entries.
Example 2: Merge maps with duplicate keys using `last_win`
-- Return value: {1:a, 2:d, 3:c}
select map_concat('last_win', map(1, 'a', 2, 'b'), map(3, 'c'), map(2, 'd'));
Key 2 appears in both the first map (value b) and the last map (value d). With last_win, the value from the last map wins, so the result contains 2:d instead of 2:b.
Related functions
MAP_CONCAT is a complex type function. For more information about functions for working with ARRAY, MAP, STRUCT, and JSON data types, see Complex type functions.