Function Name | Statement | Description |
map[key] | Obtain the corresponding value for a given key from the map. | |
cardinality(x) | Return the cardinality (size) of the map. | |
element_at(map(k, v), key) | Returns the value corresponding to the given key. If the key does not exist, returns NULL. | |
map() / map(array(k), array(v)) | Returns an empty map or creates a map using the given array of key/value pairs. | |
map_from_entries(array(row(k, v))) | Create a map from a given array of entries. | |
multimap_from_entries(array(row(k, v))) | Create a multimap from a given array of entries, where each key can be associated with multiple values. | |
map_concat(map1, map2, ..., mapN) | Returns the union of all given maps. | |
map_filter(map(k, v), function(k, v, boolean)) | Filter the map to create a new map with entries where the function returns true. | |
map_keys(x) | Returns all keys in the map. | |
map_values(x) | Returns all values in the map. | |
map_zip_with(map(k, v1), map(k, v2), function) | Merges two maps into one and applies a function to the value pairs with the same keys. | |
transform_keys(map(k1, v), function(k1, v, k2)) | Applies a function to each entry and transforms the keys. | |
transform_values(map(k, v1), function(k, v1, v2)) | Applies a function to each entry and transforms the values. |
map[key]
Parameter | Description |
map | The parameter value is of MAP type. |
key | Key name to obtain. |
* | SELECT name_to_age_map['Bob'] AS bob_age
22
* | SELECT MAP(ARRAY['key1', 'key2', 'key3'], ARRAY['v1', 'v2', 'v3'])['key2']
v2
cardinality(x)
Parameter | Description |
x | The parameter value is of MAP type. |
* | SELECT cardinality(MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]))
3
element_at(map(k, v), key)
Parameter | Description |
map(k, v) | The parameter value is of MAP type, where k is the key type and v is the value type. |
key | Key name to obtain. |
* | SELECT element_at(MAP(ARRAY['a', 'b'], ARRAY['test1', 'test2']), 'c')
NULL
map()map(array(k), array(v))
Parameter | Description |
array(k) | Key array. |
array(v) | Value array, which must be the same length as the key array. |
* | SELECT map()
{}
* | SELECT MAP(ARRAY['key1', 'key2', 'key3'], ARRAY[2373, 3463, 45837])
{"key1":2373,"key2":3463,"key3":45837}
* | SELECT MAP(ARRAY['key1', 'key2', 'key3'], ARRAY['v1', 'v2', 'v3'])
{"key1":"v1","key2":"v2","key3":"v3"}
* | SELECT MAP(ARRAY['key1', 'key2', 'key3'], ARRAY[23, 34.63, 45.837])
{"key1":23,"key2":34.63,"key3":45.837}
* | SELECT MAP(ARRAY['key1', 'key2', 'key3'], ARRAY['v1', NULL, 'v3'])
{"key1":"v1","key2":null,"key3":"v3"}
map_from_entries(array(row(k, v)))
Parameter | Description |
array(row(k, v)) | An array of rows containing key-value pairs, where each element is a tuple in the form of (k, v). |
* | SELECT map_from_entries(ARRAY[(1, 'x'), (2, 'y')])
{"1":"x","2":"y"}
multimap_from_entries(array(row(k, v)))
Parameter | Description |
array(row(k, v)) | An array of rows containing key-value pairs, allowing duplicate keys. |
* | SELECT multimap_from_entries(ARRAY[(1, 'x'), (2, 'y'), (1, 'z')])
{"1":["x","z"],"2":["y"]}
map_concat(map1(k, v), map2(k, v), ..., mapN(k, v))
Parameter | Description |
map1, map2, ..., mapN | Multiple MAP-type parameters to be merged must have consistent key and value types. |
* | SELECT map_concat(MAP(ARRAY['a'], ARRAY[1]), MAP(ARRAY['a', 'b'], ARRAY[2, 3]))
{"a":2,"b":3}
map_filter(map(k, v), function(k, v, boolean))
Parameter | Description |
map(k, v) | The parameter value is of MAP type. |
function(k, v, boolean) | Lambda function that takes key k and value v as parameters and returns a Boolean value. Syntax: (k, v) -> Boolean expression. |
* | SELECT map_filter(MAP(ARRAY[10, 20, 30], ARRAY['a', NULL, 'c']), (k, v) -> v IS NOT NULL)
{"10":"a","30":"c"}
* | SELECT map_filter(MAP(ARRAY['k1', 'k2', 'k3'], ARRAY[20, 3, 15]), (k, v) -> v > 10)
{"k1":20,"k3":15}
map_keys(x)
Parameter | Description |
x | The parameter value is of MAP(k, v) type. |
* | SELECT map_keys(MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]))
["a","b","c"]
map_values(x)
Parameter | Description |
x | The parameter value is of MAP(k, v) type. |
* | SELECT map_values(MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]))
[1, 2, 3]
map_zip_with(map(k, v1), map(k, v2), function(k, v1, v2, v3))
Parameter | Description |
map(k, v1) | The first parameter of MAP type. |
map(k, v2) | The second MAP-type parameter must have keys of the same type as the first map. |
function(k, v1, v2, v3) | Lambda function that takes key k, value v1 from the first map, and value v2 from the second map as parameters, returning a new value v3. Syntax: (k, v1, v2) -> expression. |
* | SELECT map_zip_with(MAP(ARRAY[1, 2, 3], ARRAY['a', 'b', 'c']),MAP(ARRAY[1, 2, 3], ARRAY['d', 'e', 'f']),(k, v1, v2) -> concat(v1, v2))
{"1":"ad","2":"be","3":"cf"}
* | SELECT map_zip_with(MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 8, 27]),MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]),(k, v1, v2) -> k || CAST(v1 / v2 AS VARCHAR))
{"a":"a1","b":"b4","c":"c9"}
transform_keys(map(k1, v), function(k1, v, k2))
Parameter | Description |
map(k1, v) | The parameter value is of MAP type, where k1 is the original key type. |
function(k1, v, k2) | Lambda function that takes the original key k1 and value v as parameters, returning a new key k2. Syntax: (k, v) -> new key expression. |
* | SELECT transform_keys(MAP(ARRAY[1, 2, 3], ARRAY['a', 'b', 'c']), (k, v) -> k + 1)
{"2":"a","3":"b","4":"c"}
* | SELECT transform_keys(MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]), (k, v) -> v * v)
{"1":1,"4":2,"9":3}
* | SELECT transform_keys(MAP(ARRAY['a', 'b'], ARRAY[1, 2]), (k, v) -> k || CAST(v AS VARCHAR))
{"a1":1,"b2":2}
* | SELECT transform_keys(MAP(ARRAY[1, 2], ARRAY[1.0, 1.4]),(k, v) -> MAP(ARRAY[1, 2], ARRAY['one', 'two'])[k])
{"one":1,"two":1.4}
transform_values(map(k, v1), function(k, v1, v2))
Parameter | Description |
map(k, v1) | The parameter value is of MAP type, and v1 is of primitive type. |
function(k, v1, v2) | Lambda function that takes key k and original value v1 as parameters, returning a new value v2. Syntax: (k, v) -> new value expression. |
* | SELECT transform_values(MAP(ARRAY[1, 2, 3], ARRAY[10, 20, 30]), (k, v) -> v + k)
{"1":11,"2":22,"3":33}
* | SELECT transform_values(MAP(ARRAY[1, 2, 3], ARRAY['a', 'b', 'c']), (k, v) -> k * k)
{"1":1,"2":4,"3":9}
* | SELECT transform_values(MAP(ARRAY['a', 'b'], ARRAY[1, 2]), (k, v) -> k || CAST(v AS VARCHAR))
{"a":"a1","b":"b2"}
* | SELECT transform_values(MAP(ARRAY[1, 2], ARRAY[1.0, 1.4]),(k, v) -> MAP(ARRAY[1, 2], ARRAY['one', 'two'])[k] || '_' || CAST(v AS VARCHAR))
{"1":"one_1.0","2":"two_1.4"}
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback