ClickHouse/docs/en/sql-reference/functions/tuple-map-functions.md

927 lines
27 KiB
Markdown
Raw Normal View History

---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/tuple-map-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 120
sidebar_label: Maps
---
2022-06-02 10:55:18 +00:00
## map
2024-05-29 19:45:56 +00:00
Creates a value of type [Map(key, value)](../data-types/map.md) from key-value pairs.
2021-07-29 15:20:55 +00:00
**Syntax**
```sql
map(key1, value1[, key2, value2, ...])
```
2021-07-29 15:20:55 +00:00
**Arguments**
2024-05-29 19:45:56 +00:00
- `key_n` — The keys of the map entries. Any type supported as key type of [Map](../data-types/map.md).
- `value_n` — The values of the map entries. Any type supported as value type of [Map](../data-types/map.md).
**Returned value**
2024-05-29 19:45:56 +00:00
- A map containing `key:value` pairs. [Map(key, value)](../data-types/map.md).
**Examples**
Query:
```sql
SELECT map('key1', number, 'key2', number * 2) FROM numbers(3);
```
Result:
``` text
┌─map('key1', number, 'key2', multiply(number, 2))─┐
│ {'key1':0,'key2':0} │
│ {'key1':1,'key2':2} │
│ {'key1':2,'key2':4} │
└──────────────────────────────────────────────────┘
```
2023-03-01 13:15:01 +00:00
## mapFromArrays
2024-05-29 19:45:56 +00:00
Creates a map from an array of keys and an array of values.
2023-04-21 13:19:56 +00:00
2024-05-31 11:17:22 +00:00
The function is a convenient alternative to syntax `CAST([...], 'Map(key_type, value_type)')`.
For example, instead of writing
- `CAST((['aa', 'bb'], [4, 5]), 'Map(String, UInt32)')`, or
- `CAST([('aa',4), ('bb',5)], 'Map(String, UInt32)')`
you can write `mapFromArrays(['aa', 'bb'], [4, 5])`.
2023-04-21 13:19:56 +00:00
2023-03-01 13:15:01 +00:00
**Syntax**
```sql
mapFromArrays(keys, values)
2023-04-03 13:43:11 +00:00
```
2023-03-01 13:15:01 +00:00
Alias: `MAP_FROM_ARRAYS(keys, values)`
2023-04-03 13:43:11 +00:00
2023-03-03 08:35:21 +00:00
**Arguments**
2023-04-03 13:43:11 +00:00
2024-05-29 19:45:56 +00:00
- `keys` — Array of keys to create the map from. [Array(T)](../data-types/array.md) where `T` can be any type supported by [Map](../data-types/map.md) as key type.
- `values` - Array or map of values to create the map from. [Array](../data-types/array.md) or [Map](../data-types/map.md).
2023-04-21 13:19:56 +00:00
2023-03-01 13:15:01 +00:00
**Returned value**
2024-05-29 19:45:56 +00:00
- A map with keys and values constructed from the key array and value array/map.
2023-04-21 13:19:56 +00:00
2023-03-01 13:15:01 +00:00
**Example**
Query:
```sql
select mapFromArrays(['a', 'b', 'c'], [1, 2, 3])
2024-05-29 19:45:56 +00:00
```
2023-04-03 13:43:11 +00:00
2024-05-29 19:45:56 +00:00
Result:
2024-05-29 19:45:56 +00:00
```
2023-03-01 13:15:01 +00:00
┌─mapFromArrays(['a', 'b', 'c'], [1, 2, 3])─┐
│ {'a':1,'b':2,'c':3} │
└───────────────────────────────────────────┘
2024-05-29 19:45:56 +00:00
```
2023-03-30 10:18:21 +00:00
2024-05-31 11:17:22 +00:00
`mapFromArrays` also accepts arguments of type [Map](../data-types/map.md). These are casted to array of tuples during execution.
2024-05-29 19:45:56 +00:00
```sql
2023-03-30 10:18:21 +00:00
SELECT mapFromArrays([1, 2, 3], map('a', 1, 'b', 2, 'c', 3))
2024-05-29 19:45:56 +00:00
```
2023-03-30 10:18:21 +00:00
2024-05-29 19:45:56 +00:00
Result:
```
2023-03-30 10:18:21 +00:00
┌─mapFromArrays([1, 2, 3], map('a', 1, 'b', 2, 'c', 3))─┐
│ {1:('a',1),2:('b',2),3:('c',3)} │
└───────────────────────────────────────────────────────┘
2023-04-03 13:43:11 +00:00
```
2023-03-01 13:15:01 +00:00
## extractKeyValuePairs
2024-05-31 12:26:18 +00:00
Converts a string of key-value pairs to a [Map(String, String)](../data-types/map.md).
2024-05-31 11:17:22 +00:00
Parsing is tolerant towards noise (e.g. log files).
Key-value pairs in the input string consist of a key, followed by a key-value delimiter, and a value.
Key value pairs are separated by a pair delimiter.
Keys and values can be quoted.
**Syntax**
``` sql
extractKeyValuePairs(data[, key_value_delimiter[, pair_delimiter[, quoting_character]]])
```
2023-05-07 19:24:49 +00:00
Alias:
- `str_to_map`
- `mapFromString`
**Arguments**
- `data` - String to extract key-value pairs from. [String](../data-types/string.md) or [FixedString](../data-types/fixedstring.md).
2024-05-29 19:45:56 +00:00
- `key_value_delimiter` - Single character delimiting keys and values. Defaults to `:`. [String](../data-types/string.md) or [FixedString](../data-types/fixedstring.md).
- `pair_delimiters` - Set of character delimiting pairs. Defaults to ` `, `,` and `;`. [String](../data-types/string.md) or [FixedString](../data-types/fixedstring.md).
- `quoting_character` - Single character used as quoting character. Defaults to `"`. [String](../data-types/string.md) or [FixedString](../data-types/fixedstring.md).
**Returned values**
2024-05-29 19:45:56 +00:00
- A of key-value pairs. Type: [Map(String, String)](../data-types/map.md)
**Examples**
2024-05-29 19:45:56 +00:00
Query
``` sql
SELECT extractKeyValuePairs('name:neymar, age:31 team:psg,nationality:brazil') as kv
```
Result:
``` Result:
┌─kv──────────────────────────────────────────────────────────────────────┐
│ {'name':'neymar','age':'31','team':'psg','nationality':'brazil'} │
└─────────────────────────────────────────────────────────────────────────┘
```
2024-05-31 11:17:22 +00:00
With a single quote `'` as quoting character:
``` sql
SELECT extractKeyValuePairs('name:\'neymar\';\'age\':31;team:psg;nationality:brazil,last_key:last_value', ':', ';,', '\'') as kv
```
Result:
``` text
┌─kv───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ {'name':'neymar','age':'31','team':'psg','nationality':'brazil','last_key':'last_value'} │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
Escape sequences without escape sequences support:
``` sql
SELECT extractKeyValuePairs('age:a\\x0A\\n\\0') AS kv
```
Result:
``` text
┌─kv─────────────────────┐
│ {'age':'a\\x0A\\n\\0'} │
└────────────────────────┘
```
2024-05-31 11:17:22 +00:00
To restore a map string key-value pairs serialized with `toString`:
```sql
SELECT
map('John', '33', 'Paula', '31') AS m,
toString(m) as map_serialized,
extractKeyValuePairs(map_serialized, ':', ',', '\'') AS map_restored
FORMAT Vertical;
```
Result:
```
Row 1:
──────
m: {'John':'33','Paula':'31'}
map_serialized: {'John':'33','Paula':'31'}
map_restored: {'John':'33','Paula':'31'}
```
## extractKeyValuePairsWithEscaping
2024-05-29 19:45:56 +00:00
Same as `extractKeyValuePairs` but supports escaping.
Supported escape sequences: `\x`, `\N`, `\a`, `\b`, `\e`, `\f`, `\n`, `\r`, `\t`, `\v` and `\0`.
Non standard escape sequences are returned as it is (including the backslash) unless they are one of the following:
`\\`, `'`, `"`, `backtick`, `/`, `=` or ASCII control characters (c <= 31).
This function will satisfy the use case where pre-escaping and post-escaping are not suitable. For instance, consider the following
input string: `a: "aaaa\"bbb"`. The expected output is: `a: aaaa\"bbbb`.
- Pre-escaping: Pre-escaping it will output: `a: "aaaa"bbb"` and `extractKeyValuePairs` will then output: `a: aaaa`
- Post-escaping: `extractKeyValuePairs` will output `a: aaaa\` and post-escaping will keep it as it is.
Leading escape sequences will be skipped in keys and will be considered invalid for values.
**Examples**
Escape sequences with escape sequence support turned on:
``` sql
SELECT extractKeyValuePairsWithEscaping('age:a\\x0A\\n\\0') AS kv
```
Result:
``` result
┌─kv────────────────┐
│ {'age':'a\n\n\0'} │
└───────────────────┘
```
2022-06-02 10:55:18 +00:00
## mapAdd
Collect all the keys and sum corresponding values.
**Syntax**
```sql
mapAdd(arg1, arg2 [, ...])
```
**Arguments**
Arguments are [maps](../data-types/map.md) or [tuples](../data-types/tuple.md#tuplet1-t2) of two [arrays](../data-types/array.md#data-type-array), where items in the first array represent keys, and the second array contains values for the each key. All key arrays should have same type, and all value arrays should contain items which are promoted to the one type ([Int64](../data-types/int-uint.md#int-ranges), [UInt64](../data-types/int-uint.md#uint-ranges) or [Float64](../data-types/float.md#float32-float64)). The common promoted type is used as a type for the result array.
**Returned value**
- Depending on the arguments returns one [map](../data-types/map.md) or [tuple](../data-types/tuple.md#tuplet1-t2), where the first array contains the sorted keys and the second array contains values.
**Example**
2024-05-29 19:45:56 +00:00
Query with `Map` type:
```sql
2024-05-29 19:45:56 +00:00
SELECT mapAdd(map(1,1), map(1,1));
```
Result:
```text
2024-05-29 19:45:56 +00:00
┌─mapAdd(map(1, 1), map(1, 1))─┐
│ {1:2} │
└──────────────────────────────┘
```
2024-05-29 19:45:56 +00:00
Query with a tuple:
```sql
2024-05-29 19:45:56 +00:00
SELECT mapAdd(([toUInt8(1), 2], [1, 1]), ([toUInt8(1), 2], [1, 1])) as res, toTypeName(res) as type;
2021-07-21 08:08:29 +00:00
```
Result:
```text
2024-05-29 19:45:56 +00:00
┌─res───────────┬─type───────────────────────────────┐
│ ([1,2],[2,2]) │ Tuple(Array(UInt8), Array(UInt64)) │
└───────────────┴────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## mapSubtract
Collect all the keys and subtract corresponding values.
2021-07-21 08:08:29 +00:00
**Syntax**
```sql
mapSubtract(Tuple(Array, Array), Tuple(Array, Array) [, ...])
```
2021-07-21 08:08:29 +00:00
**Arguments**
Arguments are [maps](../data-types/map.md) or [tuples](../data-types/tuple.md#tuplet1-t2) of two [arrays](../data-types/array.md#data-type-array), where items in the first array represent keys, and the second array contains values for the each key. All key arrays should have same type, and all value arrays should contain items which are promote to the one type ([Int64](../data-types/int-uint.md#int-ranges), [UInt64](../data-types/int-uint.md#uint-ranges) or [Float64](../data-types/float.md#float32-float64)). The common promoted type is used as a type for the result array.
**Returned value**
- Depending on the arguments returns one [map](../data-types/map.md) or [tuple](../data-types/tuple.md#tuplet1-t2), where the first array contains the sorted keys and the second array contains values.
**Example**
2024-05-29 19:45:56 +00:00
Query with `Map` type:
```sql
2024-05-29 19:45:56 +00:00
SELECT mapSubtract(map(1,1), map(1,1));
```
Result:
```text
2024-05-29 19:45:56 +00:00
┌─mapSubtract(map(1, 1), map(1, 1))─┐
│ {1:0} │
└───────────────────────────────────┘
2020-09-23 13:12:13 +00:00
```
2024-05-29 19:45:56 +00:00
Query with a tuple map:
2021-07-21 08:08:29 +00:00
```sql
2024-05-29 19:45:56 +00:00
SELECT mapSubtract(([toUInt8(1), 2], [toInt32(1), 1]), ([toUInt8(1), 2], [toInt32(2), 1])) as res, toTypeName(res) as type;
2021-07-21 08:08:29 +00:00
```
Result:
```text
2024-05-29 19:45:56 +00:00
┌─res────────────┬─type──────────────────────────────┐
│ ([1,2],[-1,0]) │ Tuple(Array(UInt8), Array(Int64)) │
└────────────────┴───────────────────────────────────┘
2021-07-21 08:08:29 +00:00
```
2022-06-02 10:55:18 +00:00
## mapPopulateSeries
2024-05-29 19:45:56 +00:00
Fills missing key-value pairs in a map with integer keys.
To support extending the keys beyond the largest value, a maximum key can be specified.
2024-05-30 10:08:17 +00:00
More specifically, the function returns a map in which the the keys form a series from the smallest to the largest key (or `max` argument if it specified) with step size of 1, and corresponding values.
2024-05-29 19:45:56 +00:00
If no value is specified for a key, a default value is used as value.
In case keys repeat, only the first value (in order of appearance) is associated with the key.
2021-07-21 08:08:29 +00:00
**Syntax**
```sql
2021-07-21 08:08:29 +00:00
mapPopulateSeries(map[, max])
2024-05-29 19:45:56 +00:00
mapPopulateSeries(keys, values[, max])
```
For array arguments the number of elements in `keys` and `values` must be the same for each row.
**Arguments**
2024-05-29 19:45:56 +00:00
Arguments are [Maps](../data-types/map.md) or two [Arrays](../data-types/array.md#data-type-array), where the first and second array contains keys and values for the each key.
2021-07-21 08:08:29 +00:00
Mapped arrays:
2024-05-29 19:45:56 +00:00
- `map` — Map with integer keys. [Map](../data-types/map.md).
2021-07-21 08:08:29 +00:00
or
2024-05-29 19:45:56 +00:00
- `keys` — Array of keys. [Array](../data-types/array.md#data-type-array)([Int](../data-types/int-uint.md#uint-ranges)).
- `values` — Array of values. [Array](../data-types/array.md#data-type-array)([Int](../data-types/int-uint.md#uint-ranges)).
- `max` — Maximum key value. Optional. [Int8, Int16, Int32, Int64, Int128, Int256](../data-types/int-uint.md#int-ranges).
2021-07-21 08:08:29 +00:00
**Returned value**
2024-05-29 19:45:56 +00:00
- Depending on the arguments a [Map](../data-types/map.md) or a [Tuple](../data-types/tuple.md#tuplet1-t2) of two [Arrays](../data-types/array.md#data-type-array): keys in sorted order, and values the corresponding keys.
**Example**
2024-05-29 19:45:56 +00:00
Query with `Map` type:
```sql
2024-05-29 19:45:56 +00:00
SELECT mapPopulateSeries(map(1, 10, 5, 20), 6);
```
Result:
2020-09-23 13:12:13 +00:00
```text
2024-05-29 19:45:56 +00:00
┌─mapPopulateSeries(map(1, 10, 5, 20), 6)─┐
│ {1:10,2:0,3:0,4:0,5:20,6:0} │
└─────────────────────────────────────────┘
```
2024-05-29 19:45:56 +00:00
Query with mapped arrays:
2021-07-21 08:08:29 +00:00
```sql
2024-05-29 19:45:56 +00:00
SELECT mapPopulateSeries([1,2,4], [11,22,44], 5) AS res, toTypeName(res) AS type;
2021-07-21 08:08:29 +00:00
```
Result:
```text
2024-05-29 19:45:56 +00:00
┌─res──────────────────────────┬─type──────────────────────────────┐
│ ([1,2,3,4,5],[11,22,0,44,0]) │ Tuple(Array(UInt8), Array(UInt8)) │
└──────────────────────────────┴───────────────────────────────────┘
2021-07-21 08:08:29 +00:00
```
2022-06-02 10:55:18 +00:00
## mapContains
2021-02-10 12:55:18 +00:00
2024-05-29 19:45:56 +00:00
Returns if a given key is contained in a given map.
2021-02-10 12:55:18 +00:00
**Syntax**
```sql
2021-02-10 12:55:18 +00:00
mapContains(map, key)
```
2023-03-03 08:35:21 +00:00
**Arguments**
2021-02-10 12:55:18 +00:00
- `map` — Map. [Map](../data-types/map.md).
2024-05-29 19:45:56 +00:00
- `key` — Key. Type must match the key type of `map`.
2021-02-10 12:55:18 +00:00
**Returned value**
- `1` if `map` contains `key`, `0` if not. [UInt8](../data-types/int-uint.md).
2021-02-10 12:55:18 +00:00
**Example**
Query:
2021-02-11 08:46:31 +00:00
```sql
2024-05-29 19:45:56 +00:00
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
INSERT INTO tab VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
SELECT mapContains(a, 'name') FROM tab;
2021-02-11 08:46:31 +00:00
```
Result:
```text
┌─mapContains(a, 'name')─┐
│ 1 │
│ 0 │
└────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## mapKeys
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
Returns the keys of a given map.
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
This function can be optimized by enabling setting [optimize_functions_to_subcolumns](../../operations/settings/settings.md#optimize-functions-to-subcolumns).
With enabled setting, the function only reads the [keys](../data-types/map.md#map-subcolumns) subcolumn instead the whole map.
The query `SELECT mapKeys(m) FROM table` is transformed to `SELECT m.keys FROM table`.
2021-06-23 00:15:11 +00:00
2021-02-11 08:46:31 +00:00
**Syntax**
```sql
mapKeys(map)
```
2023-03-03 08:35:21 +00:00
**Arguments**
2021-02-11 08:46:31 +00:00
- `map` — Map. [Map](../data-types/map.md).
2021-02-11 08:46:31 +00:00
**Returned value**
- Array containing all keys from the `map`. [Array](../data-types/array.md).
2021-02-11 08:46:31 +00:00
**Example**
Query:
```sql
2024-05-29 19:45:56 +00:00
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
INSERT INTO tab VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
SELECT mapKeys(a) FROM tab;
2021-02-11 08:46:31 +00:00
```
Result:
2021-02-10 12:55:18 +00:00
2021-02-11 08:46:31 +00:00
```text
┌─mapKeys(a)────────────┐
│ ['name','age'] │
│ ['number','position'] │
└───────────────────────┘
```
2021-02-10 12:55:18 +00:00
2022-06-02 10:55:18 +00:00
## mapValues
2021-02-10 12:55:18 +00:00
2024-05-29 19:45:56 +00:00
Returns the values of a given map.
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
This function can be optimized by enabling setting [optimize_functions_to_subcolumns](../../operations/settings/settings.md#optimize-functions-to-subcolumns).
With enabled setting, the function only reads the [values](../data-types/map.md#map-subcolumns) subcolumn instead the whole map.
The query `SELECT mapValues(m) FROM table` is transformed to `SELECT m.values FROM table`.
2021-06-23 00:15:11 +00:00
2021-02-11 08:46:31 +00:00
**Syntax**
```sql
2021-11-18 05:03:49 +00:00
mapValues(map)
2021-02-11 08:46:31 +00:00
```
2023-03-03 08:35:21 +00:00
**Arguments**
2021-02-11 08:46:31 +00:00
- `map` — Map. [Map](../data-types/map.md).
2021-02-11 08:46:31 +00:00
**Returned value**
- Array containing all the values from `map`. [Array](../data-types/array.md).
2021-02-11 08:46:31 +00:00
**Example**
Query:
```sql
2024-05-29 19:45:56 +00:00
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
INSERT INTO tab VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
2021-02-11 08:46:31 +00:00
2024-05-29 19:45:56 +00:00
SELECT mapValues(a) FROM tab;
2021-02-11 08:46:31 +00:00
```
Result:
```text
┌─mapValues(a)─────┐
│ ['eleven','11'] │
│ ['twelve','6.0'] │
└──────────────────┘
2023-04-03 13:43:11 +00:00
```
## mapContainsKeyLike
2021-10-27 03:56:55 +00:00
**Syntax**
```sql
mapContainsKeyLike(map, pattern)
2023-04-03 13:43:11 +00:00
```
2023-03-03 08:35:21 +00:00
**Arguments**
- `map` — Map. [Map](../data-types/map.md).
2023-04-21 13:19:56 +00:00
- `pattern` - String pattern to match.
2021-10-27 03:56:55 +00:00
**Returned value**
2023-04-21 13:19:56 +00:00
- `1` if `map` contains `key` like specified pattern, `0` if not.
2021-10-27 03:56:55 +00:00
**Example**
Query:
```sql
2024-05-29 19:45:56 +00:00
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
2021-10-27 03:56:55 +00:00
2024-05-29 19:45:56 +00:00
INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});
2021-10-27 03:56:55 +00:00
2024-05-29 19:45:56 +00:00
SELECT mapContainsKeyLike(a, 'a%') FROM tab;
2023-04-03 13:43:11 +00:00
```
Result:
2021-10-27 03:56:55 +00:00
```text
┌─mapContainsKeyLike(a, 'a%')─┐
│ 1 │
│ 0 │
2023-04-03 13:43:11 +00:00
└─────────────────────────────┘
```
## mapExtractKeyLike
2024-05-29 19:45:56 +00:00
Give a map with string keys and a LIKE pattern, this function returns a map with elements where the key matches the pattern.
2021-10-28 09:15:32 +00:00
**Syntax**
```sql
mapExtractKeyLike(map, pattern)
2023-04-03 13:43:11 +00:00
```
2023-03-03 08:35:21 +00:00
**Arguments**
2023-04-21 13:19:56 +00:00
- `map` — Map. [Map](../data-types/map.md).
2023-04-21 13:19:56 +00:00
- `pattern` - String pattern to match.
2021-10-28 09:15:32 +00:00
**Returned value**
2024-05-29 19:45:56 +00:00
- A map containing elements the key matching the specified pattern. If no elements match the pattern, an empty map is returned.
2023-04-03 13:43:11 +00:00
2021-10-28 09:15:32 +00:00
**Example**
Query:
```sql
2024-05-29 19:45:56 +00:00
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
2021-10-28 09:15:32 +00:00
2024-05-29 19:45:56 +00:00
INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});
2021-10-28 09:15:32 +00:00
2024-05-29 19:45:56 +00:00
SELECT mapExtractKeyLike(a, 'a%') FROM tab;
2023-04-03 13:43:11 +00:00
```
Result:
2021-10-28 09:15:32 +00:00
```text
┌─mapExtractKeyLike(a, 'a%')─┐
│ {'abc':'abc'} │
│ {} │
└────────────────────────────┘
2023-04-03 13:43:11 +00:00
```
## mapApply
2024-05-29 19:45:56 +00:00
Applies a function to each element of a map.
**Syntax**
```sql
mapApply(func, map)
2023-04-03 13:43:11 +00:00
```
2023-03-03 08:35:21 +00:00
**Arguments**
2023-04-21 13:19:56 +00:00
- `func` — [Lambda function](../../sql-reference/functions/index.md#higher-order-functions---operator-and-lambdaparams-expr-function).
- `map` — [Map](../data-types/map.md).
**Returned value**
- Returns a map obtained from the original map by application of `func(map1[i], ..., mapN[i])` for each element.
2023-04-03 13:43:11 +00:00
**Example**
Query:
```sql
SELECT mapApply((k, v) -> (k, v * 10), _map) AS r
FROM
(
SELECT map('key1', number, 'key2', number * 2) AS _map
FROM numbers(3)
)
2023-04-03 13:43:11 +00:00
```
Result:
```text
┌─r─────────────────────┐
│ {'key1':0,'key2':0} │
│ {'key1':10,'key2':20} │
│ {'key1':20,'key2':40} │
└───────────────────────┘
2023-04-03 13:43:11 +00:00
```
## mapFilter
2024-05-29 19:45:56 +00:00
Filters a map by applying a function to each map element.
**Syntax**
2021-02-11 08:46:31 +00:00
```sql
mapFilter(func, map)
2023-04-03 13:43:11 +00:00
```
2023-03-03 08:35:21 +00:00
**Arguments**
- `func` - [Lambda function](../../sql-reference/functions/index.md#higher-order-functions---operator-and-lambdaparams-expr-function).
- `map` — [Map](../data-types/map.md).
**Returned value**
- Returns a map containing only the elements in `map` for which `func(map1[i], ..., mapN[i])` returns something other than 0.
2023-04-03 13:43:11 +00:00
**Example**
Query:
```sql
SELECT mapFilter((k, v) -> ((v % 2) = 0), _map) AS r
FROM
(
SELECT map('key1', number, 'key2', number * 2) AS _map
FROM numbers(3)
)
2023-04-03 13:43:11 +00:00
```
Result:
```text
┌─r───────────────────┐
│ {'key1':0,'key2':0} │
│ {'key2':2} │
│ {'key1':2,'key2':4} │
└─────────────────────┘
2023-04-03 13:43:11 +00:00
```
## mapUpdate
**Syntax**
```sql
mapUpdate(map1, map2)
2023-04-03 13:43:11 +00:00
```
2023-03-03 08:35:21 +00:00
**Arguments**
- `map1` [Map](../data-types/map.md).
- `map2` [Map](../data-types/map.md).
**Returned value**
- Returns a map1 with values updated of values for the corresponding keys in map2.
2023-04-03 13:43:11 +00:00
**Example**
Query:
```sql
SELECT mapUpdate(map('key1', 0, 'key3', 0), map('key1', 10, 'key2', 10)) AS map;
2023-04-03 13:43:11 +00:00
```
Result:
```text
┌─map────────────────────────────┐
│ {'key3':0,'key1':10,'key2':10} │
└────────────────────────────────┘
2023-04-03 13:43:11 +00:00
```
## mapConcat
2024-05-29 19:45:56 +00:00
Concatenates multiple maps based on the equality of their keys.
If elements with the same key exist in more than one input map, all elements are added to the result map, but only the first one is accessible via operator `[]`
2023-04-03 13:43:11 +00:00
**Syntax**
```sql
mapConcat(maps)
```
**Arguments**
2024-05-29 19:45:56 +00:00
- `maps` Arbitrarily many [Maps](../data-types/map.md).
2023-04-03 13:43:11 +00:00
**Returned value**
2024-05-29 19:45:56 +00:00
- Returns a map with concatenated maps passed as arguments.
2023-04-03 13:43:11 +00:00
2023-04-21 13:19:56 +00:00
**Examples**
2023-04-03 13:43:11 +00:00
Query:
```sql
SELECT mapConcat(map('key1', 1, 'key3', 3), map('key2', 2)) AS map;
```
Result:
```text
┌─map──────────────────────────┐
│ {'key1':1,'key3':3,'key2':2} │
└──────────────────────────────┘
```
2023-04-21 13:19:56 +00:00
Query:
```sql
SELECT mapConcat(map('key1', 1, 'key2', 2), map('key1', 3)) AS map, map['key1'];
```
Result:
```text
┌─map──────────────────────────┬─elem─┐
│ {'key1':1,'key2':2,'key1':3} │ 1 │
└──────────────────────────────┴──────┘
```
2023-04-03 13:43:11 +00:00
## mapExists(\[func,\], map)
2024-05-29 19:45:56 +00:00
Returns 1 if at least one key-value pair in `map` exists for which `func(key, value)` returns something other than 0. Otherwise, it returns 0.
2023-04-03 13:43:11 +00:00
2024-05-29 19:45:56 +00:00
:::note
`mapExists` is a [higher-order function](../../sql-reference/functions/index.md#higher-order-functions).
You can pass a lambda function to it as the first argument.
:::
2023-04-03 13:43:11 +00:00
**Example**
Query:
```sql
SELECT mapExists((k, v) -> (v = 1), map('k1', 1, 'k2', 2)) AS res
```
Result:
2024-05-29 19:45:56 +00:00
```
2023-04-03 13:43:11 +00:00
┌─res─┐
│ 1 │
└─────┘
```
## mapAll(\[func,\] map)
Returns 1 if `func(key, value)` returns something other than 0 for all key-value pairs in `map`. Otherwise, it returns 0.
2024-05-29 19:45:56 +00:00
:::note
Note that the `mapAll` is a [higher-order function](../../sql-reference/functions/index.md#higher-order-functions).
You can pass a lambda function to it as the first argument.
:::
2023-04-03 13:43:11 +00:00
**Example**
Query:
```sql
SELECT mapAll((k, v) -> (v = 1), map('k1', 1, 'k2', 2)) AS res
```
Result:
2024-05-29 19:45:56 +00:00
```
2023-04-03 13:43:11 +00:00
┌─res─┐
│ 0 │
└─────┘
```
## mapSort(\[func,\], map)
2024-05-29 19:45:56 +00:00
Sorts the elements of a map in ascending order.
If the `func` function is specified, the sorting order is determined by the result of the `func` function applied to the keys and values of the map.
2023-04-03 13:43:11 +00:00
**Examples**
``` sql
SELECT mapSort(map('key2', 2, 'key3', 1, 'key1', 3)) AS map;
```
``` text
┌─map──────────────────────────┐
│ {'key1':3,'key2':2,'key3':1} │
└──────────────────────────────┘
```
``` sql
SELECT mapSort((k, v) -> v, map('key2', 2, 'key3', 1, 'key1', 3)) AS map;
```
``` text
┌─map──────────────────────────┐
│ {'key3':1,'key2':2,'key1':3} │
└──────────────────────────────┘
```
For more details see the [reference](../../sql-reference/functions/array-functions.md#array_functions-sort) for `arraySort` function.
## mapPartialSort
Sorts the elements of a map in ascending order with additional `limit` argument allowing partial sorting.
If the `func` function is specified, the sorting order is determined by the result of the `func` function applied to the keys and values of the map.
**Syntax**
```sql
mapPartialSort([func,] limit, map)
```
**Arguments**
- `func` Optional function to apply to the keys and values of the map. [Lambda function](../../sql-reference/functions/index.md#higher-order-functions---operator-and-lambdaparams-expr-function).
- `limit` Elements in range [1..limit] are sorted. [(U)Int](../data-types/int-uint.md).
- `map` Map to sort. [Map](../data-types/map.md).
**Returned value**
- Partially sorted map. [Map](../data-types/map.md).
**Example**
``` sql
SELECT mapPartialSort((k, v) -> v, 2, map('k1', 3, 'k2', 1, 'k3', 2));
```
``` text
┌─mapPartialSort(lambda(tuple(k, v), v), 2, map('k1', 3, 'k2', 1, 'k3', 2))─┐
│ {'k2':1,'k3':2,'k1':3} │
└───────────────────────────────────────────────────────────────────────────┘
```
2023-04-03 13:43:11 +00:00
## mapReverseSort(\[func,\], map)
2024-05-29 19:45:56 +00:00
Sorts the elements of a map in descending order.
If the `func` function is specified, the sorting order is determined by the result of the `func` function applied to the keys and values of the map.
2023-04-03 13:43:11 +00:00
**Examples**
``` sql
SELECT mapReverseSort(map('key2', 2, 'key3', 1, 'key1', 3)) AS map;
```
``` text
┌─map──────────────────────────┐
│ {'key3':1,'key2':2,'key1':3} │
└──────────────────────────────┘
```
``` sql
SELECT mapReverseSort((k, v) -> v, map('key2', 2, 'key3', 1, 'key1', 3)) AS map;
```
``` text
┌─map──────────────────────────┐
│ {'key1':3,'key2':2,'key3':1} │
└──────────────────────────────┘
```
2024-05-29 19:45:56 +00:00
For more details see function [arrayReverseSort](../../sql-reference/functions/array-functions.md#array_functions-reverse-sort).
## mapPartialReverseSort
Sorts the elements of a map in descending order with additional `limit` argument allowing partial sorting.
If the `func` function is specified, the sorting order is determined by the result of the `func` function applied to the keys and values of the map.
**Syntax**
```sql
mapPartialReverseSort([func,] limit, map)
```
**Arguments**
- `func` Optional function to apply to the keys and values of the map. [Lambda function](../../sql-reference/functions/index.md#higher-order-functions---operator-and-lambdaparams-expr-function).
- `limit` Elements in range [1..limit] are sorted. [(U)Int](../data-types/int-uint.md).
- `map` Map to sort. [Map](../data-types/map.md).
**Returned value**
- Partially sorted map. [Map](../data-types/map.md).
**Example**
``` sql
SELECT mapPartialReverseSort((k, v) -> v, 2, map('k1', 3, 'k2', 1, 'k3', 2));
```
``` text
┌─mapPartialReverseSort(lambda(tuple(k, v), v), 2, map('k1', 3, 'k2', 1, 'k3', 2))─┐
│ {'k1':3,'k3':2,'k2':1} │
└──────────────────────────────────────────────────────────────────────────────────┘
```