Use any quantity of key/value pairs to construct a Map Column
Syntax
map(key1, value1 [, key2, value2, ...])
Param
Return value
Example
SELECT map(1, 2, 3, 4), map('k1', 'v1', 'k2', 'v2');
Query results
{1:2,3:4} {'k1':'v1','k2':'v2'}
Search the data of a specific key in the Map. Rows not containing this key will be filled with the Default, equivalent to map[key]
Syntax
arrayElement(map, key)
Param
Return value
Example
WITH map('k1', 1) AS m SELECT arrayElement(m, 'k1'), m['k1'], m['k2'];
Query results
1 1 0
Caution
When using arrayElement, the entire Map Column is read first and then filtered, so for implicit Map Columns, search performance is lower. For implicit Map Columns, it is recommended to use the mapElement (or {}) method to search for keys.
Search the data of a specific key in the Map. Rows not containing this key will be filled with NULL, equivalent to map{key}
Syntax
mapElement(map, key)
Param
Return value
Example
WITH map('k1', 1) AS m SELECT mapElement(m, 'k1'), m{'k1'}, m{'k2'};
Query results
1 1 NULL
Caution
For implicit Map Columns, using this method only needs to read the corresponding implicit Column, which is more efficient compared to arrayElement;
Obtain the keys of each row in the Map Data and return them as an Array
Syntax
mapKeys(map)
Param
Return value
Example
WITH map('k1', 'v1', 'k2', 'v2') AS m SELECT mapKeys(m);
Query results
['k1','k2']
Caution
For implicit Map Columns, this method will first read the entire Map Column, then extract the keys and return them. The efficiency is lower. It is recommended to use the getMapKeys method below.
Obtain the values of each row in the Map Data and return them as an Array
Syntax
mapValues(map)
Param
Return value
Example
WITH map('k1', 'v1', 'k2', 'v2') AS m SELECT mapValues(m);
Query results
['v1','v2']
Obtain the list of keys contained in the Map Column (in the specified partition), with each key appearing only once in the result
Syntax
getMapKeys('database', 'table', 'map' [, 'partition_regex'])
Param
Return value
Example
INSERT INTO some_db.map_table (m) VALUES {'k1': 'v1', 'k2': 'v2'}; SELECT getMapKeys('some_db', 'map_table', 'm');
Query results
['k1','k2']
Caution
This function finds keys by reading the metadata of each part, so it is only applicable to implicit Map Columns.
Return whether each row of the Map Column contains the target key
Syntax
mapContains(map, key)
Param
Return value
Example
WITH map('k1', 'v1', 'k2', 'v2') AS m SELECT mapContains(m, 'k1'), mapContains(m, 'k3');
Query results
1 0
Use the specified delimiter to parse a String into a Map(String, String) type.
Syntax
str_to_map(pattern, pair_delimiter, key_value_delimiter)
Param
Return value
Example
SELECT str_to_map('k1=v1, k2=v2', ',', '=');
Query results
{'k1':'v1','k2':'v2'}
Use two Arrays to construct a Map Column, where the first Array is the key and the second Array is the value.
Syntax
mapFromArrays(keys, values)
Param
Return value
Example
SELECT mapFromArrays(['a', 'b', 'c'], [1, 2, 3]);
Query results
{'a':1,'b':2,'c':3}
Add the values of corresponding keys in multiple Map Columns
Syntax
mapAdd(map1, map2 [, ...])
Param
Return value
Example
SELECT mapAdd(map(1,1), map(1,2), map(2,2));
Query results
{1:3,2:2}
Subtract the values of corresponding keys in multiple Map Columns (the first value minus all subsequent values)
Syntax
mapAdd(map1, map2 [, ...])
Param
Return value
Example
SELECT mapSubtract(map(1, 2)::Map(Int16,Int16), map(1, 1)::Map(Int16,Int16), map(2, -2)::Map(Int16,Int16));
Query results
{1:1,2:2}
The Map key type must be Integer. Fill the Map with all key values from the minimum key to the maximum key, and fill the value with the default value of the corresponding type. The maximum key can also be specified by the second parameter.
This function can also accept two Arrays, the first as key and the second as value.
Syntax
mapPopulateSeries(map[, max]) mapPopulateSeries(keys, values[, max])
Param
Return value
Example
SELECT mapPopulateSeries(map(2, 10, 5, 20)), mapPopulateSeries(map(2, 10, 5, 20), 3); SELECT mapPopulateSeries([1, 5], [5, 20], 6);
Query results
{2:10,3:0,4:0,5:20} {2:10,3:0} ([1,2,3,4,5,6],[5,0,0,0,20,0])
Returns whether each row of the Map Column contains a key that meets the specified pattern
Syntax
mapContainsKeyLike(map, pattern)
Param
Return value
Example
WITH map('k1', 'v1', 'k2', 'v2') AS m SELECT mapContainsKeyLike(m, 'k%'), mapContainsKeyLike(m, 'm%');
Query results
1 0
Filter the keys and their corresponding values in the Map Column that meet the specified pattern
Syntax
mapExtractKeyLike(map, pattern)
Param
Return value
Example
WITH map('k1', 'v1', 'k2', 'v2') AS m SELECT mapExtractKeyLike(m, 'k%'), mapExtractKeyLike(m, 'm%');
Query results
{'k1':'v1','k2':'v2'} {}
Run the specified lambda expression on each key-value pair in the Map.
Syntax
mapApply(lambda, map)
Param
Return value
Example
WITH map(1, 1, 2, 2) AS m SELECT mapApply((k, v) -> (toString(k), v * 2), m);
Query results
{'1':2,'2':4}
Use the provided condition to filter key-value pairs in the Map.
Syntax
mapFilter(lambda, map)
Param
Return value
Example
SELECT _map, mapFilter((k, v) -> ((v % 2) = 0), _map) FROM ( SELECT map('key1', number, 'key2', number * 2) AS _map FROM numbers(3) )
Query results
{'key1':0,'key2':0} {'key1':0,'key2':0} {'key1':1,'key2':2} {'key2':2} {'key1':2,'key2':4} {'key1':2,'key2':4}
Copy map1, use the value in map2 to update the copied Map column, and return it. For keys that exist in both map1 and map2, the corresponding value in map2 will be retained.
Syntax
mapUpdate(map1, map2)
Param
Return value
Example
WITH map('k1', 1, 'k2', 1) AS m1, map('k1', 2, 'k3', 2) AS m2 SELECT mapUpdate(m1, m2), mapUpdate(m2, m1);
Query results
{'k1':2,'k2':1,'k3':2} {'k1':1,'k2':1,'k3':2}