Map Functions
map
Creates a value of type Map(key, value) from key-value pairs.
Syntax
Arguments
key_n— The keys of the map entries. Any type supported as key type of Map.value_n— The values of the map entries. Any type supported as value type of Map.
Returned value
- A map containing
key:valuepairs. Map(key, value).
Examples
Query:
Result:
mapFromArrays
Creates a map from an array or map of keys and an array or map of values.
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)'), orCAST([('aa',4), ('bb',5)], 'Map(String, UInt32)')
you can write mapFromArrays(['aa', 'bb'], [4, 5]).
Syntax
Alias: MAP_FROM_ARRAYS(keys, values)
Arguments
keys— Array or map of keys to create the map from Array or Map. Ifkeysis an array, we acceptArray(Nullable(T))orArray(LowCardinality(Nullable(T)))as its type as long as it doesn't contain NULL value.values- Array or map of values to create the map from Array or Map.
Returned value
- A map with keys and values constructed from the key array and value array/map.
Example
Query:
Result:
mapFromArrays also accepts arguments of type Map. These are cast to array of tuples during execution.
Result:
Result:
extractKeyValuePairs
Converts a string of key-value pairs to a Map(String, String). 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
Alias:
str_to_mapmapFromString
Arguments
data- String to extract key-value pairs from. String or FixedString.key_value_delimiter- Single character delimiting keys and values. Defaults to:. String or FixedString.pair_delimiters- Set of character delimiting pairs. Defaults to,,and;. String or FixedString.quoting_character- Single character used as quoting character. Defaults to". String or FixedString.unexpected_quoting_character_strategy- Strategy to handle quoting characters in unexpected places duringread_keyandread_valuephase. Possible values: "invalid", "accept" and "promote". Invalid will discard key/value and transition back toWAITING_KEYstate. Accept will treat it as a normal character. Promote will transition toREAD_QUOTED_{KEY/VALUE}state and start from next character.
Returned values
- A of key-value pairs. Type: Map(String, String)
Examples
Query
Result:
With a single quote ' as quoting character:
Result:
unexpected_quoting_character_strategy examples:
unexpected_quoting_character_strategy=invalid
unexpected_quoting_character_strategy=accept
unexpected_quoting_character_strategy=promote
Escape sequences without escape sequences support:
Result:
To restore a map string key-value pairs serialized with toString:
Result:
extractKeyValuePairsWithEscaping
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"andextractKeyValuePairswill then output:a: aaaa - Post-escaping:
extractKeyValuePairswill outputa: 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:
Result:
mapAdd
Collect all the keys and sum corresponding values.
Syntax
Arguments
Arguments are maps or tuples of two arrays, 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, UInt64 or Float64). The common promoted type is used as a type for the result array.
Returned value
- Depending on the arguments returns one map or tuple, where the first array contains the sorted keys and the second array contains values.
Example
Query with Map type:
Result:
Query with a tuple:
Result:
mapSubtract
Collect all the keys and subtract corresponding values.
Syntax
Arguments
Arguments are maps or tuples of two arrays, 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, UInt64 or Float64). The common promoted type is used as a type for the result array.
Returned value
- Depending on the arguments returns one map or tuple, where the first array contains the sorted keys and the second array contains values.
Example
Query with Map type:
Result:
Query with a tuple map:
Result:
mapPopulateSeries
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.
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.
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.
Syntax
For array arguments the number of elements in keys and values must be the same for each row.
Arguments
Arguments are Maps or two Arrays, where the first and second array contains keys and values for the each key.
Mapped arrays:
map— Map with integer keys. Map.
or
keys— Array of keys. Array(Int).values— Array of values. Array(Int).max— Maximum key value. Optional. Int8, Int16, Int32, Int64, Int128, Int256.
Returned value
- Depending on the arguments a Map or a Tuple of two Arrays: keys in sorted order, and values the corresponding keys.
Example
Query with Map type:
Result:
Query with mapped arrays:
Result:
mapKeys
Returns the keys of a given map.
This function can be optimized by enabling setting optimize_functions_to_subcolumns.
With enabled setting, the function only reads the keys subcolumn instead the whole map.
The query SELECT mapKeys(m) FROM table is transformed to SELECT m.keys FROM table.
Syntax
Arguments
map— Map. Map.
Returned value
- Array containing all keys from the
map. Array.
Example
Query:
Result:
mapContains
Returns if a given key is contained in a given map.
Syntax
Alias: mapContainsKey(map, key)
Arguments
map— Map. Map.key— Key. Type must match the key type ofmap.
Returned value
1ifmapcontainskey,0if not. UInt8.
Example
Query:
Result:
mapContainsKeyLike
Syntax
Arguments
map— Map. Map.pattern- String pattern to match.
Returned value
1ifmapcontainskeylike specified pattern,0if not.
Example
Query:
Result:
mapExtractKeyLike
Give a map with string keys and a LIKE pattern, this function returns a map with elements where the key matches the pattern.
Syntax
Arguments
map— Map. Map.pattern- String pattern to match.
Returned value
- A map containing elements the key matching the specified pattern. If no elements match the pattern, an empty map is returned.
Example
Query:
Result:
mapValues
Returns the values of a given map.
This function can be optimized by enabling setting optimize_functions_to_subcolumns.
With enabled setting, the function only reads the values subcolumn instead the whole map.
The query SELECT mapValues(m) FROM table is transformed to SELECT m.values FROM table.
Syntax
Arguments
map— Map. Map.
Returned value
- Array containing all the values from
map. Array.
Example
Query:
Result:
mapContainsValue
Returns if a given key is contained in a given map.
Syntax
Alias: mapContainsValue(map, value)
Arguments
map— Map. Map.value— Value. Type must match the value type ofmap.
Returned value
1ifmapcontainsvalue,0if not. UInt8.
Example
Query:
Result:
mapContainsValueLike
Syntax
Arguments
map— Map. Map.pattern- String pattern to match.
Returned value
1ifmapcontainsvaluelike specified pattern,0if not.
Example
Query:
Result:
mapExtractValueLike
Give a map with string values and a LIKE pattern, this function returns a map with elements where the value matches the pattern.
Syntax
Arguments
map— Map. Map.pattern- String pattern to match.
Returned value
- A map containing elements the value matching the specified pattern. If no elements match the pattern, an empty map is returned.
Example
Query:
Result:
mapApply
Applies a function to each element of a map.
Syntax
Arguments
func— Lambda function.map— Map.
Returned value
- Returns a map obtained from the original map by application of
func(map1[i], ..., mapN[i])for each element.
Example
Query:
Result:
mapFilter
Filters a map by applying a function to each map element.
Syntax
Arguments
func- Lambda function.map— Map.
Returned value
- Returns a map containing only the elements in
mapfor whichfunc(map1[i], ..., mapN[i])returns something other than 0.
Example
Query:
Result:
mapUpdate
Syntax
Arguments
Returned value
- Returns a map1 with values updated of values for the corresponding keys in map2.
Example
Query:
Result:
mapConcat
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
Arguments
maps– Arbitrarily many Maps.
Returned value
- Returns a map with concatenated maps passed as arguments.
Examples
Query:
Result:
Query:
Result:
mapExists([func,], map)
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.
mapExists is a higher-order function.
You can pass a lambda function to it as the first argument.
Example
Query:
Result:
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.
Note that the mapAll is a higher-order function.
You can pass a lambda function to it as the first argument.
Example
Query:
Result:
mapSort([func,], map)
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.
Examples
For more details see the reference 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
Arguments
func– Optional function to apply to the keys and values of the map. Lambda function.limit– Elements in range [1..limit] are sorted. (U)Int.map– Map to sort. Map.
Returned value
- Partially sorted map. Map.
Example
mapReverseSort([func,], map)
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.
Examples
For more details see function arrayReverseSort.
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
Arguments
func– Optional function to apply to the keys and values of the map. Lambda function.limit– Elements in range [1..limit] are sorted. (U)Int.map– Map to sort. Map.
Returned value
- Partially sorted map. Map.
Example
extractKeyValuePairs
Introduced in: v
Extracts key-value pairs from any string. The string does not need to be 100% structured in a key value pair format;
It can contain noise (e.g. log files). The key-value pair format to be interpreted should be specified via function arguments.
A key-value pair consists of a key followed by a key_value_delimiter and a value. Quoted keys and values are also supported. Key value pairs must be separated by pair delimiters.
Syntax
Arguments
data- String to extract key-value pairs from. String or FixedString.key_value_delimiter- Character to be used as delimiter between the key and the value. Defaults to:. String or FixedString.pair_delimiters- Set of character to be used as delimiters between pairs. Defaults to\space,,and;. String or FixedString.quoting_character- Character to be used as quoting character. Defaults to". String or FixedString.unexpected_quoting_character_strategy- Strategy to handle quoting characters in unexpected places duringread_keyandread_valuephase. Possible values:invalid,acceptandpromote. Invalid will discard key/value and transition back toWAITING_KEYstate. Accept will treat it as a normal character. Promote will transition toREAD_QUOTED_{KEY/VALUE}state and start from next character. The default value isINVALID
Returned values
- The extracted key-value pairs in a Map(String, String).
Examples
Query:
Simple case
Single quote as quoting character
unexpected_quoting_character_strategy examples:
unexpected_quoting_character_strategy=invalid
unexpected_quoting_character_strategy=accept
unexpected_quoting_character_strategy=promote
Escape sequences without escape sequences support
Syntax
Aliases: str_to_map, mapFromString
Arguments
- None.
Returned value
Examples
extractKeyValuePairsWithEscaping
Introduced in: v
Same as extractKeyValuePairs but with escaping support.
Escape sequences supported: \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"andextractKeyValuePairswill then output:a: aaaa - Post-escaping:
extractKeyValuePairswill outputa: 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.
Escape sequences with escape sequence support turned on
Syntax
Arguments
- None.
Returned value
Examples
map
Introduced in: v21.1
Creates a value of type Map(key, value) from key-value pairs.
Syntax
Arguments
Returned value
Returns a map containing key:value pairs. Map(Any, Any)
Examples
Usage example
mapAdd
Introduced in: v20.7
Collect all the keys and sum corresponding values.
Syntax
Arguments
arg1[, arg2, ...]— Maps or tuples of two arrays in which items in the first array represent keys, and the second array contains values for each key.Map(K, V)orTuple(Array(T), Array(T))
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) or Tuple(Array(T), Array(T))
Examples
With Map type
With tuple
mapAll
Introduced in: v23.4
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
Arguments
func— Lambda function.Lambda functionmap— Map to check.Map(K, V)
Returned value
Returns 1 if all key-value pairs satisfy the condition, 0 otherwise. UInt8
Examples
Usage example
mapApply
Introduced in: v22.3
Applies a function to each element of a map.
Syntax
Arguments
func— Lambda function.Lambda functionmap— Map to apply function to.Map(K, V)
Returned value
Returns a new map obtained from the original map by application of func for each element. Map(K, V)
Examples
Usage example
mapConcat
Introduced in: v23.4
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
Arguments
maps— Arbitrarily many maps.Map
Returned value
Returns a map with concatenated maps passed as arguments. Map
Examples
Usage example
mapContainsKey
Introduced in: v21.2
Determines if a key is contained in a map.
Syntax
Aliases: mapContains
Arguments
map— Map to search in.Map(K, V)key— Key to search for. Type must match the key type of the map.Any
Returned value
Returns 1 if map contains key, 0 if not. UInt8
Examples
Usage example
mapContainsKeyLike
Introduced in: v23.4
Checks whether map contains key LIKE specified pattern.
Syntax
Arguments
map— Map to search in.Map(K, V)pattern— Pattern to match keys against.const String
Returned value
Returns 1 if map contains a key matching pattern, 0 otherwise. UInt8
Examples
Usage example
mapContainsValue
Introduced in: v25.6
Determines if a value is contained in a map.
Syntax
Arguments
map— Map to search in.Map(K, V)value— Value to search for. Type must match the value type of map.Any
Returned value
Returns 1 if the map contains the value, 0 if not. UInt8
Examples
Usage example
mapContainsValueLike
Introduced in: v25.5
Checks whether a map contains a value LIKE the specified pattern.
Syntax
Arguments
map— Map to search in.Map(K, V)pattern— Pattern to match values against.const String
Returned value
Returns 1 if map contains a value matching pattern, 0 otherwise. UInt8
Examples
Usage example
mapExists
Introduced in: v23.4
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
Arguments
func— Optional. Lambda function.Lambda functionmap— Map to check.Map(K, V)
Returned value
Returns 1 if at least one key-value pair satisfies the condition, 0 otherwise. UInt8
Examples
Usage example
mapExtractKeyLike
Introduced in: v23.4
Give a map with string keys and a LIKE pattern, this function returns a map with elements where the key matches the pattern.
Syntax
Arguments
map— Map to extract from.Map(K, V)pattern— Pattern to match keys against.const 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)
Examples
Usage example
mapExtractValueLike
Introduced in: v25.5
Given a map with string values and a LIKE pattern, this function returns a map with elements where the value matches the pattern.
Syntax
Arguments
map— Map to extract from.Map(K, V)pattern— Pattern to match values against.const String
Returned value
Returns a map containing elements the value matching the specified pattern. If no elements match the pattern, an empty map is returned. Map(K, V)
Examples
Usage example
mapFilter
Introduced in: v22.3
Filters a map by applying a function to each map element.
Syntax
Arguments
func— Lambda function.Lambda functionmap— Map to filter.Map(K, V)
Returned value
Returns a map containing only the elements in the map for which func returns something other than 0. Map(K, V)
Examples
Usage example
mapFromArrays
Introduced in: v23.3
Creates a map from an array or map of keys and an array or map of values.
The function is a convenient alternative to syntax CAST([...], 'Map(key_type, value_type)').
Syntax
Aliases: MAP_FROM_ARRAYS
Arguments
keys— Array or map of keys to create the map from.ArrayorMapvalues— Array or map of values to create the map from.ArrayorMap
Returned value
Returns a map with keys and values constructed from the key array and value array/map. Map
Examples
Basic usage
With map inputs
mapKeys
Introduced in: v21.2
Returns the keys of a given map.
This function can be optimized by enabling setting optimize_functions_to_subcolumns.
With the setting enabled, the function only reads the keys subcolumn instead of the entire map.
The query SELECT mapKeys(m) FROM table is transformed to SELECT m.keys FROM table.
Syntax
Arguments
map— Map to extract keys from.Map(K, V)
Returned value
Returns array containing all keys from the map. Array(T)
Examples
Usage example
mapPartialReverseSort
Introduced in: v23.4
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
Arguments
func— Optional. Lambda function.Lambda functionlimit— Elements in the range[1..limit]are sorted.(U)Int*map— Map to sort.Map(K, V)
Returned value
Returns a partially sorted map in descending order. Map(K, V)
Examples
Usage example
mapPartialSort
Introduced in: v23.4
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
Arguments
func— Optional. Lambda function.Lambda functionlimit— Elements in the range[1..limit]are sorted.(U)Int*map— Map to sort.Map(K, V)
Returned value
Returns a partially sorted map. Map(K, V)
Examples
Usage example
mapPopulateSeries
Introduced in: v20.10
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. More specifically, the function returns a map in which the keys form a series from the smallest to the largest key (or max argument if specified) with step size of 1, and corresponding values. 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.
Syntax
Arguments
map— Map with integer keys.Map((U)Int*, V)keys— Array of keys.Array(T)values— Array of values.Array(T)max— Optional. Maximum key value.Int8orInt16orInt32orInt64orInt128orInt256
Returned value
Returns a map or a tuple of two arrays where the first has keys in sorted order, and the second values for the corresponding keys. Map(K, V) or Tuple(Array(UInt*), Array(Any))
Examples
With Map type
With mapped arrays
mapReverseSort
Introduced in: v23.4
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.
Syntax
Arguments
func— Optional. Lambda function.Lambda functionmap— Map to sort.Map(K, V)
Returned value
Returns a map sorted in descending order. Map(K, V)
Examples
Usage example
mapSort
Introduced in: v23.4
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.
Syntax
Arguments
func— Optional. Lambda function.Lambda functionmap— Map to sort.Map(K, V)
Returned value
Returns a map sorted in ascending order. Map(K, V)
Examples
Usage example
mapSubtract
Introduced in: v20.7
Collect all the keys and subtract corresponding values.
Syntax
Arguments
arg1[, arg2, ...]— Maps or tuples of two arrays in which items in the first array represent keys, and the second array contains values for each key.Map(K, V)orTuple(Array(T), Array(T))
Returned value
Returns one map or tuple, where the first array contains the sorted keys and the second array contains values. Map(K, V) or Tuple(Array(T), Array(T))
Examples
With Map type
With tuple map
mapUpdate
Introduced in: v22.3
For two maps, returns the first map with values updated on the values for the corresponding keys in the second map.
Syntax
Arguments
Returned value
Returns map1 with values updated from values for the corresponding keys in map2. Map(K, V)
Examples
Basic usage
mapValues
Introduced in: v21.2
Returns the values of a given map.
This function can be optimized by enabling setting optimize_functions_to_subcolumns.
With the setting enabled, the function only reads the values subcolumn instead of the entire map.
The query SELECT mapValues(m) FROM table is transformed to SELECT m.values FROM table.
Syntax
Arguments
map— Map to extract values from.Map(K, V)
Returned value
Returns an array containing all the values from the map. Array(T)
Examples
Usage example