All Products
Search
Document Center

MaxCompute:TRANSFORM_KEYS

Last Updated:Mar 26, 2026

Applies a function to each key in a MAP, returning a new MAP with transformed keys and unchanged values.

Syntax

map<K2, V> transform_keys([string <mapDupKeyPolicy>,] map<K1, V> <input>, function<K1, V, K2> <func>)

Type relationship: map<K1, V>map<K2, V>. The lambda receives each (K1, V) pair and returns a new key of type K2. Values pass through unchanged.

Parameters

ParameterRequiredTypeDescription
mapDupKeyPolicyNoSTRINGHow to handle duplicate keys after transformation. Valid values: exception or last_win. If omitted, the session-level setting odps.sql.map.key.dedup.policy applies (default: last_win).
inputYesmap<K1, V>The input MAP. K1 is the key type; V is the value type.
funcYesfunction<K1, V, K2>A built-in function, user-defined function (UDF), or expression that produces new keys. Takes two inputs — the key (K1) and the value (V) — and returns a new key of type K2.

Duplicate key handling

When the transformation produces duplicate keys, the behavior depends on mapDupKeyPolicy:

  • exception — returns an error.

  • last_win — the latter key overwrites the former.

MaxCompute applies mapDupKeyPolicy when specified; otherwise it falls back to odps.sql.map.key.dedup.policy. The default value of odps.sql.map.key.dedup.policy is last_win.

Return value

Returns a MAP type. If any transformed key is null, an error is returned.

Examples

All examples use the -> lambda syntax. For details, see Lambda functions.

Transform keys using key and value

-- Returns {-10:-20, 70:50, 71:101}
select transform_keys(map(10, -20, 20, 50, -30, 101), (k, v) -> k + v);

Each new key is the sum of the original key and its value.

Handle duplicate keys with last_win

-- No error. Result depends on element order in the input MAP.
select transform_keys("last_win", map(10, -20, 20, 50, -30, 100), (k, v) -> k + v);

With last_win, if two transformed keys collide, the last one wins.

Handle duplicate keys with exception

-- Returns an error because the transformation produces duplicate keys.
select transform_keys("exception", map(10, -20, 20, 50, -30, 100), (k, v) -> k + v);

Related functions

TRANSFORM_KEYS is a complex type function. For the full list of functions for ARRAY, MAP, STRUCT, and JSON types, see Complex type functions.

See also: TRANSFORM_VALUES, which applies a function to MAP values while keeping keys unchanged.