All Products
Search
Document Center

MaxCompute:MAP

Last Updated:Jul 24, 2023

Creates a map based on given key-value pairs.

Syntax

map(K, V) map(K <key1>, V <value1>, K <key2>, V <value2>[, ...])

Parameters

  • key: required. All keys must be of the same data type after implicit conversions. Only basic data types are supported.

  • value: required. All values must be of the same data type even after implicit conversions. Data types other than DECIMAL are supported.

Return value

A value of the MAP type is returned.

Note

You can configure the odps.sql.map.key.dedup.policy parameter at the session level to specify 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.

If you do not configure this parameter, the default value last_win is used.

Examples

  • Example 1: No duplicate keys exist. Create a map based on the data in the t_table table that contains the c1 (BIGINT), c2 (STRING), c3 (STRING), c4 (BIGINT), and c5 (BIGINT) columns. Data in the table:

    +------------+----+----+------------+------------+
    | c1         | c2 | c3 | c4         | c5         |
    +------------+----+----+------------+------------+
    | 1000       | k11 | k21 | 86         | 15         |
    | 1001       | k12 | k22 | 97         | 2          |
    | 1002       | k13 | k23 | 99         | 1          |
    +------------+----+----+------------+------------+

    Sample statement:

    -- Define a map based on the key-value pairs between the c2 and c4 columns, and between the c3 and c5 columns. 
    select map(c2,c4,c3,c5) from t_table;
    -- The following result is returned: 
    +------+
    | _c0  |
    +------+
    | {k11:86, k21:15} |
    | {k12:97, k22:2} |
    | {k13:99, k23:1} |
    +------+
  • Example 2: Duplicate keys exist. Create a map based on the data in the t_table table that contains the c1 (BIGINT), c2 (STRING), c3 (STRING), c4 (BIGINT), and c5 (BIGINT) columns. Data in the table:

    1000,'k11','k11',86,15
    1001,'k12','k22',97,2
    1002,'k13','k23',99,1
    1003,'k13','k24',100,1
    1004,'k12','k25',95,1

    Sample statement:

    -- Define a map based on the key-value pairs between the c2 and c4 columns, and between the c3 and c5 columns. 
    select map(c2,c4,c3,c5) from t_table;
    -- The following result is returned: 
    +------+
    | _c0  |
    +------+
    | {'k11':15} |
    | {'k12':97, 'k22':2} |
    | {'k13':99, 'k23':1} |
    | {'k13':100, 'k24':1} |
    | {'k12':95, 'k25':1} |
    +------+

Related functions

MAP 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.