All Products
Search
Document Center

MaxCompute:MAP_FROM_ENTRIES

Last Updated:Jul 24, 2023

Creates a map based on given struct arrays.

Syntax

map<K, V> map_from_entries([string <mapDupKeyPolicy>,] array <struct<K, V> , struct<K, V>[,...]>)

Parameters

  • mapDupKeyPolicy: optional. A value of the STRING type. This parameter specifies the method that is used to process duplicate keys. Valid values:

    • exception: An error is returned.

    • last_win: The latter key overwrites the former key.

    You can also configure the odps.sql.map.key.dedup.policy parameter at the session level to specify the method that is used to process duplicate keys. For example, you can set odps.sql.map.key.dedup.policy to exception. If you do not configure this parameter, the default value last_win is used.

    Note

    The behavior implementation of MaxCompute is determined based on mapDupKeyPolicy. If you do not specify mapDupKeyPolicy, the value of odps.sql.map.key.dedup.policy is used.

  • Data of the STRUCT type is required. K corresponds to the keys in the generated map. V corresponds to the values in the generated map. K and V in struct<K, V> specify the keys and values of a struct array.

Return value

A value of the MAP type is returned. The return value varies based on the following rules:

  • If a struct array is null, null is returned.

  • If the number of fields in a struct array is not 2 or the key of a struct array is null, an error is returned.

Examples

-- The return value is {1:a, 2:b}. 
select map_from_entries(array(struct(1, 'a'), struct(2, 'b')));
-- The return value is {1:a, 2:c}. 
select map_from_entries(array(struct(1, 'a'), struct(2, 'b'), struct(2, 'c')));

Related functions

MAP_FROM_ENTRIES is a complex type function. For more information about the functions that are used to process data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.