Builds a map from an array of structs, where each struct provides one key-value pair.
Syntax
map<K, V> map_from_entries([string <mapDupKeyPolicy>,] array<struct<K, V>>)Parameters
mapDupKeyPolicy (optional)
Type: STRING. Controls how duplicate keys are handled. Valid values:
exception— Returns an error when a duplicate key is detected.last_win— The later entry overwrites the earlier one.
If mapDupKeyPolicy is not specified, the value of odps.sql.map.key.dedup.policy is used. You can configure this session-level parameter to set the default behavior. If the session parameter is not set, last_win applies.
Struct array (required)
Type: array<struct<K, V>>. Each struct must have exactly two fields: K maps to the output map's key, and V maps to the output map's value.
Return value
Returns a value of the MAP type.
If the input array is null, returns null.
If any struct does not have exactly two fields, or if any key is null, returns an error.
Examples
Basic usage — build a map from two key-value pairs:
SELECT map_from_entries(array(struct(1, 'a'), struct(2, 'b')));
-- {1:a, 2:b}Duplicate key with last_win (default) — the last value for key 2 wins:
SELECT map_from_entries(array(struct(1, 'a'), struct(2, 'b'), struct(2, 'c')));
-- {1:a, 2:c}Related functions
MAP_FROM_ENTRIES is a complex type function. For the full list of functions that work with ARRAY, MAP, STRUCT, and JSON types, see Complex type functions.