ClickHouse function reference
map
Creates a value of typeMap(key, value)
from key-value pairs.
Syntax:
key_n
(Any type supported as key type ofMap
): The keys of the map entries.value_n
(Any type supported as value type ofMap
): The values of the map entries.
- A map containing key:value pairs. Type:
Map(key, value)
.
mapFromArrays
Creates a map from an array or map of keys and an array or map of values. Syntax:- MAP_FROM_ARRAYS
keys
(Array
orMap
): Array or map of keys to create the map from.values
(Array
orMap
): Array or map of values to create the map from.
- A map with keys and values constructed from the key array and value array/map.
extractKeyValuePairs
Converts a string of key-value pairs to aMap(String, String)
. Parsing is tolerant towards noise (e.g. log files).
Syntax:
- str_to_map
- mapFromString
data
(String
orFixedString
): String to extract key-value pairs from.key_value_delimiter
(String
orFixedString
, optional): Single character delimiting keys and values. Defaults to:
.pair_delimiters
(String
orFixedString
, optional): Set of character delimiting pairs. Defaults to,
and;
.quoting_character
(String
orFixedString
, optional): Single character used as quoting character. Defaults to"
.
- A map of key-value pairs. Type:
Map(String, String)
mapFromArrays
Creates a map from an array of keys and an array of values. Syntax:- MAP_FROM_ARRAYS(keys, values)
keys
(Array
): Array of keys to create the map from.values
(Array
): Array of values to create the map from.
Map
. These are cast to array of tuples during execution.
If keys is an array, we accept
Array(Nullable(T))
or Array(LowCardinality(Nullable(T)))
as its type as long as it doesn’t contain NULL value.CAST([...], 'Map(key_type, value_type)')
. For example, instead of writing:
CAST((['salsa', 'guacamole'], [4, 5]), 'Map(String, UInt32)')
, orCAST([('salsa',4), ('guacamole',5)], 'Map(String, UInt32)')
mapFromArrays(['salsa', 'guacamole'], [4, 5])
.
extractKeyValuePairs
Converts a string of key-value pairs to aMap(String, String)
. This function is useful for parsing log files or other text-based data formats that contain key-value pairs.
Syntax
str_to_map
mapFromString
data
(String
orFixedString
): String to extract key-value pairs from.key_value_delimiter
(String
orFixedString
, optional): Single character delimiting keys and values. Default::
.pair_delimiters
(String
orFixedString
, optional): Set of characters delimiting pairs. Default:,
and;
.quoting_character
(String
orFixedString
, optional): Single character used as quoting character. Default:"
.
- A map of key-value pairs. Type:
Map(String, String)
extractKeyValuePairsWithEscaping
Converts a string of key-value pairs to aMap(String, String)
with support for escape sequences.
Syntax:
data
(String
orFixedString
): String to extract key-value pairs from.key_value_delimiter
(String
orFixedString
, optional): Single character delimiting keys and values. Default::
.pair_delimiters
(String
orFixedString
, optional): Set of characters delimiting pairs. Default:,
and;
.quoting_character
(String
orFixedString
, optional): Single character used as quoting character. Default:"
.
- A map of key-value pairs. (
Map(String, String)
)
\x
, \N
, \a
, \b
, \e
, \f
, \n
, \r
, \t
, \v
, \0
, \\
, \'
, \"
, `
, /
, =
, and ASCII control characters (c <=
31).
Non-standard escape sequences are returned as-is (including the backslash).
- Leading escape sequences are skipped in keys and considered invalid for values.
- This function is useful when pre-escaping and post-escaping are not suitable.
mapAdd
Collects all the keys and sums corresponding values in maps or tuples of arrays. Syntaxarg1
,arg2
, … (Map
orTuple
): Maps or tuples of two arrays, where:- The first array contains keys
- The second array contains values for each key
Int64
UInt64
Float64
- Depending on the arguments, returns either:
- A map
- A tuple where:
- The first array contains sorted keys
- The second array contains corresponding values
- Keys
[1, 2]
are combined - Corresponding values
[1, 1]
and[1, 1]
are summed to[2, 2]
- The result type is a tuple of arrays, where values are promoted to
UInt64
mapSubtract
Subtracts corresponding values for matching keys in maps. Syntax:map1
,map2
, … (Map
): Maps to subtract. All maps should have the same key and value types. Value types must be numeric and can be promoted to a common type (Int64
,UInt64
, orFloat64
).
- All keys from the input maps
- Values calculated by subtracting corresponding values from subsequent maps from the first map’s values
- Keys present only in the first map retain their original values
- Keys not present in the first map but present in subsequent maps will have negative values in the result
- ‘carnitas’ and ‘al pastor’ values are subtracted
- ‘barbacoa’ keeps its original value as it’s only in the first map
- ‘carne asada’ appears with a negative value as it’s only in the second map
When subtracting maps with different keys, the result will contain all unique keys from all input maps.
mapPopulateSeries
Fills missing key-value pairs in a map with integer keys, creating a series from the smallest to the largest key (or specified maximum) with a step size of 1. Syntax:map
(Map
): A map with integer keys.keys
(Array(Int)
): Array of keys.values
(Array(Int)
): Array of values.max
(Int8
,Int16
,Int32
,Int64
,Int128
,Int256
, optional): Optional maximum key value.
- A
Map
or aTuple
of twoArrays
: keys in sorted order, and corresponding values.
- Keys 2, 3, and 4 are added to fill the series.
- The value 0 is used as the default for missing keys.
- Key 6 is added because the
max
argument is set to 6.
If keys repeat in the input, only the first value (in order of appearance) is associated with the key in the result.
mapContains
Checks if a given key is present in a map. Syntaxmap
(Map
): A map.key
: The key to search for. The type must match the key type of the map.
1
if the map contains the key,0
if not. Type:UInt8
.
mapKeys
Returns an array containing all the keys from a given map. Syntax:map
(Map
): A map.
- An array containing all keys from the map. (
Array
).
The
mapKeys
function is useful for extracting and working with the keys of a map separately from their values. This can be particularly helpful when you need to perform operations or comparisons based on the keys alone.mapValues
Returns the values of a given map. Syntax:map
(Map
): Map.
- Array containing all the values from the map.
Array
.
The
mapValues
function is particularly useful when you need to extract and work with just the values from a map, such as for aggregations or further processing.mapContainsKeyLike
Checks if a map contains a key that matches a given pattern. Syntax:map
(Map
): A map.pattern
(String
): A string pattern to match against the keys.
1
if the map contains a key matching the specified pattern,0
otherwise. Type:UInt8
.
mapContainsKeyLike
checks if each items
map contains a key starting with ‘p’. Both rows return 1 because they contain keys matching the pattern (‘pollo’ and ‘pico_de_gallo’ respectively).
This function is useful for searching maps with string keys when you need to perform pattern matching on the keys.
mapExtractKeyLike
Extracts key-value pairs from a map where the keys match a given pattern. Syntax:map
(Map(String, T)
): A map with string keys.pattern
(String
): A string pattern to match against the keys. Uses the LIKE operator for matching.
mapExtractKeyLike
returns a new map containing only the key-value pairs where the key starts with ‘salsa’, matching the pattern ‘salsa%’.
This function is useful for filtering maps based on key patterns, which can be particularly handy when working with structured data like menu items, ingredient lists, or categorized information in taco-related applications.
mapApply
Applies a function to each element of a map. Syntax:func
(Function
): Lambda function to apply to each key-value pair.map
(Map
): Map to process.
- A new map obtained by applying
func(key, value)
to each element of the original map.
mapApply
function then multiplies each value by 10, simulating an increase in taco topping quantities.
The lambda function
(k, v) -> (k, v * 10)
keeps the key unchanged and multiplies the value by 10 for each element in the map.mapFilter
Filters a map by applying a function to each map element. Syntax:func
(Function
): Lambda function to apply to each key-value pair.map
(Map
): Map to filter.
- A new map containing only the elements for which
func(key, value)
returns a non-zero value.
mapFilter
returns a new map containing only the toppings with even quantities.
The
mapFilter
function is particularly useful when you need to selectively include key-value pairs from a map based on a condition. It’s often used in data processing pipelines to clean or transform map data.mapUpdate
Updates values in one map with values from another map for corresponding keys. Syntax:map1
(Map
): The base map to be updated.map2
(Map
): The map containing updates.
- Returns
map1
with values updated for keys that exist inmap2
. (Map
).
- The ‘salsa’ value is updated from ‘mild’ to ‘hot’.
- The ‘guacamole’ key-value pair remains unchanged.
- The ‘queso’ key-value pair is added from the second map.
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 the[]
operator.
Syntax:
maps
(Map
): Arbitrarily manyMap
type arguments.
taco_toppings['salsa']
returns the first value ‘mild’.
The
mapConcat
function is useful for combining multiple maps, such as merging different topping preferences for a taco order. However, be cautious when dealing with duplicate keys, as only the first occurrence will be accessible using the standard map access syntax.mapExists
Checks if a given key exists in a map. Syntax:map
(Map
): A map.key
: The key to search for. Type must match the key type of the map.
1
if the map contains the key,0
if not. (UInt8
).
The
mapExists
function is case-sensitive for string keys.mapAll
Returns 1 if the specified function returns a non-zero value for all key-value pairs in the map. Otherwise, it returns 0. Syntax:func
(Function
, optional): Lambda function to apply to each key-value pair. If not specified, the function checks if all values are non-zero.map
(Map
): The input Map.
1
if the condition is true for all elements,0
otherwise. (UInt8
)
mapAll
checks if all taco toppings have a positive quantity. Since all values are greater than 0, it returns 1.
mapAll
checks if all toppings have a quantity greater than 2. Since ‘guacamole’ and ‘sour_cream’ have quantities of 2 and 1 respectively, it returns 0.
mapAll
is a higher-order function. You can pass a lambda function to it as the first argument to define custom conditions for each key-value pair.mapSort
Sorts the elements of a map in ascending order. Syntax:func
(optional): Lambda function to determine the sorting order. If not specified, sorting is based on the map keys.map
(Map
): The map to sort.
- A new sorted map. (
Map
).
This function is similar to arraySort but operates on maps instead of arrays.
mapPartialSort
Partially sorts the elements of a map in ascending order. Syntaxfunc
— Optional lambda function to determine the sorting order. If not specified, sorting is based on map keys.limit
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
, orInt64
): Number of elements to sort. Elements in range [1..limit] are sorted.map
(Map
): Map to sort.
- A partially sorted map. (
Map
).
- The map is sorted based on the values (quantities of ingredients).
- Only the first 2 elements are guaranteed to be in the correct order.
- ‘cheese’ (1) and ‘lettuce’ (2) are sorted, while ‘salsa’ (3) remains in its original position.
This function is useful when you need to partially sort a large map, which can be more efficient than sorting the entire map.
mapReverseSort
Sorts the elements of a map in descending order. If a function is specified, the sorting order is determined by the result of that function applied to the keys and values of the map. Syntax:func
(optional): A lambda function to apply to the keys and values of the map.map
(Map
): The map to sort.
- A new map with elements sorted in descending order.
This function is similar to
arrayReverseSort
, but operates on maps instead of arrays.mapPartialReverseSort
Sorts the elements of a map in descending order with an additional limit argument allowing partial sorting. If a function is specified, the sorting order is determined by the result of that function applied to the keys and values of the map. Syntax:func
(Function
, optional): Function to apply to the keys and values of the map. Lambda function.limit
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
): Elements in range [1..limit] are sorted.map
(Map
): Map to sort.
- Partially sorted map. (
Map
).
limit
of 2 ensures that only the top 2 elements are sorted in descending order, while the least popular topping remains at the end.