Map functions

**Returned value**

Returns a map or returns a tuple, where the first array contains the sorted keys and the second array contains values. [`Map(K, V)`](/docs/sql-reference/data-types/map) or [`Tuple(Array(T), Array(T))`](/docs/sql-reference/data-types/tuple)

**Examples**

**With Map type**

```
SELECT mapAdd(map(1, 1), map(1, 1))
```

```
{1:2}
```

**With tuple**

```
SELECT mapAdd(([toUInt8(1), 2], [1, 1]), ([toUInt8(1), 2], [1, 1]))
```

```
([1, 2], [2, 2])
```

## mapAll[​](#mapAll "Direct link to mapAll")

Introduced in: v23.4.0

Tests whether a condition holds for all key-value pairs in a map.
`mapAll` is a higher-order function.
You can pass a lambda function to it as the first argument.

**Syntax**

```
mapAll([func,] map)
```

**Arguments**

- `func` — Lambda function. [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `map` — Map to check. [`Map(K, V)`](/docs/sql-reference/data-types/map)

**Returned value**

Returns `1` if all key-value pairs satisfy the condition, `0` otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT mapAll((k, v) -> v = 1, map('k1', 1, 'k2', 2))
```

```
0
```

## mapApply[​](#mapApply "Direct link to mapApply")

Introduced in: v22.3.0

Applies a function to each element of a map.

**Syntax**

```
mapApply(func, map)
```

**Arguments**

- `func` — Lambda function. [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `map` — Map to apply function to. [`Map(K, V)`](/docs/sql-reference/data-types/map)

**Returned value**

Returns a new map obtained from the original map by application of `func` for each element. [`Map(K, V)`](/docs/sql-reference/data-types/map)

**Examples**

**Usage example**

```
SELECT mapApply((k, v) -> (k, v * 2), map('k1', 1, 'k2', 2))
```

```
{'k1':2,'k2':4}
```

## mapConcat[​](#mapConcat "Direct link to mapConcat")

Introduced in: v23.4.0

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 [].

**Syntax**

```
mapConcat(maps)
```

**Arguments**

- `maps` — Arbitrarily many maps. [`Map`](/docs/sql-reference/data-types/map)

**Returned value**

Returns a map with concatenated maps passed as arguments. [`Map`](/docs/sql-reference/data-types/map)

**Examples**

**Usage example**

```
SELECT mapConcat(map('k1', 'v1'), map('k2', 'v2'))
```

```
{'k1':'v1','k2':'v2'}
```

## mapContainsKey[​](#mapContainsKey "Direct link to mapContainsKey")

Introduced in: v21.2.0

Determines if a key is contained in a map.

**Syntax**

```
mapContainsKey(map, key)
```

**Aliases**: `mapContains`

**Arguments**

- `map` — Map to search in. [`Map(K, V)`](/docs/sql-reference/data-types/map)
- `key` — Key to search for. Type must match the key type of the map. [`Any`](/docs/sql-reference/data-types)

**Returned value**

Returns 1 if map contains key, 0 if not. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT mapContainsKey(map('k1', 'v1', 'k2', 'v2'), 'k1')
```

```
1
```

## mapContainsKeyLike[​](#mapContainsKeyLike "Direct link to mapContainsKeyLike")

Introduced in: v23.4.0

Checks whether map contains key `LIKE` specified pattern.

**Syntax**

```
mapContainsKeyLike(map, pattern)
```

**Arguments**

- `map` — Map to search in. [`Map(K, V)`](/docs/sql-reference/data-types/map)
- `pattern` — Pattern to match keys against. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `map` contains a key matching `pattern`, `0` otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
CREATE TABLE tab (a Map(String, String))
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});

SELECT mapContainsKeyLike(a, 'a%') FROM tab;
```

```
┌─mapContainsKeyLike(a, 'a%')─┐
│                           1 │
│                           0 │
└─────────────────────────────┘
```

## mapContainsValue[​](#mapContainsValue "Direct link to mapContainsValue")

Introduced in: v25.6.0

Determines if a value is contained in a map.

**Syntax**

```
mapContainsValue(map, value)
```

**Arguments**

- `map` — Map to search in. [`Map(K, V)`](/docs/sql-reference/data-types/map)
- `value` — Value to search for. Type must match the value type of map. [`Any`](/docs/sql-reference/data-types)

**Returned value**

Returns `1` if the map contains the value, `0` if not. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT mapContainsValue(map('k1', 'v1', 'k2', 'v2'), 'v1')
```

```
1
```

## mapContainsValueLike[​](#mapContainsValueLike "Direct link to mapContainsValueLike")

Introduced in: v25.5.0

Checks whether a map contains a value `LIKE` the specified pattern.

**Syntax**

```
mapContainsValueLike(map, pattern)
```

**Arguments**

- `map` — Map to search in. [`Map(K, V)`](/docs/sql-reference/data-types/map)
- `pattern` — Pattern to match values against. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `map` contains a value matching `pattern`, `0` otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
CREATE TABLE tab (a Map(String, String))
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});

SELECT mapContainsValueLike(a, 'a%') FROM tab;
```

```
┌─mapContainsV⋯ke(a, 'a%')─┐
│                        1 │
│                        0 │
└──────────────────────────┘
```

## mapExists[​](#mapExists "Direct link to mapExists")

Introduced in: v23.4.0

Tests whether a condition holds for at least one key-value pair in a map.
`mapExists` is a higher-order function.
You can pass a lambda function to it as the first argument.

**Syntax**

```
mapExists([func,] map)
```

**Arguments**

- `func` — Optional. Lambda function. [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `map` — Map to check. [`Map(K, V)`](/docs/sql-reference/data-types/map)

**Returned value**

Returns `1` if at least one key-value pair satisfies the condition, `0` otherwise. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT mapExists((k, v) -> v = 1, map('k1', 1, 'k2', 2))
```

```
1
```

## mapExtractKeyLike[​](#mapExtractKeyLike "Direct link to mapExtractKeyLike")

Introduced in: v23.4.0

Give a map with string keys and a `LIKE` pattern, this function returns a map with elements where the key matches the pattern.

**Syntax**

```
mapExtractKeyLike(map, pattern)
```

**Arguments**

- `map` — Map to extract from. [`Map(K, V)`](/docs/sql-reference/data-types/map)
- `pattern` — Pattern to match keys against. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a map containing elements the key matching the specified pattern. If no elements match the pattern, an empty map is returned. [`Map(K, V)`](/docs/sql-reference/data-types/map)

**Examples**

**Usage example**

```
CREATE TABLE tab (a Map(String, String))
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});

SELECT mapExtractKeyLike(a, 'a%') FROM tab;
```

```
┌─mapExtractKeyLike(a, 'a%')─┐
│ {'abc':'abc'}              │
│ {}                         │
└────────────────────────────┘
```

## mapExtractValueLike[​](#mapExtractValueLike "Direct link to mapExtractValueLike")

Introduced in: v25.5.0

Given a map with string values and a `LIKE` pattern, this function returns a map with elements where the value matches the pattern.

**Syntax**

```
mapExtractValueLike(map, pattern)
```

**Arguments**

- `map` — Map to extract from. [`Map(K, V)`](/docs/sql-reference/data-types/map)
- `pattern` — Pattern to match values against. [`const String`](/docs/sql-reference/data-types/string)

**Returned value**