All Products
Search
Document Center

MaxCompute:MAP_FROM_ARRAYS

Last Updated:Mar 26, 2026

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

ParameterRequiredTypeDescription
mapDupKeyPolicyNoSTRINGHow 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.
aYesarray<K>The key array. K can be any data type.
bYesarray<V>The value array. V can be any data type.

Return value

Returns a value of the MAP type.

  • If a or b is null, returns null.

  • If a and b have different lengths, returns an error.

  • If a contains 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:

  1. The mapDupKeyPolicy parameter in the function call.

  2. The odps.sql.map.key.dedup.policy session-level setting, used when mapDupKeyPolicy is 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.