Transforms the keys in Map input by using func. The values in the map are not changed.
Syntax
map<K2, V> transform_keys([string <mapDupKeyPolicy>,] map<K1, V> <input>, function<K1, V, K2> <func>)
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 specify the
odps.sql.map.key.dedup.policy
parameter at the session level to configure the method that is used to process duplicate keys. For example, you can set theodps.sql.map.key.dedup.policy
parameter to exception. If you do not specify this parameter, the default value last_win is used.NoteThe 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.input: required. This parameter specifies a map.
K1
andV
inmap<K1, V>
specify the keys and values of a map.func: required. This parameter specifies the built-in function, user-defined function, or expression that is used to transform the keys. The function or expression consists of two input parameters that correspond to the keys and values in input.
K2
specifies the data type of keys in the returned map.
Return value
Data of the MAP type is returned. If one of the new keys is null, an error is returned.
Examples
-- The return value is {-10:-20, 70:50, 71:101}.
select transform_keys(map(10, -20, 20, 50, -30, 101), (k, v) -> k + v);
-- No error is returned. The returned result depends on the order of the elements in the input map.
select transform_keys("last_win", map(10, -20, 20, 50, -30, 100), (k, v) -> k + v);
-- An error is returned because duplicate keys exist.
select transform_keys("exception", map(10, -20, 20, 50, -30, 100), (k, v) -> k + v);
In this example, the combination of a hyphen and a closing angle bracket (->)
is used. For more information about how to use the combination of a hyphen and a closing angle bracket (->)
in Lambda functions, see Lambda functions.
Related functions
TRANSFORM_KEYS 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.