Map(K, V)
Data type Map(K, V)
stores key-value pairs.
Unlike other databases, maps are not unique in ClickHouse, i.e. a map can contain two elements with the same key.
(The reason for that is that maps are internally implemented as Array(Tuple(K, V))
.)
You can use use syntax m[k]
to obtain the value for key k
in map m
.
Also, m[k]
scans the map, i.e. the runtime of the operation is linear in the size of the map.
Parameters
K
— The type of the Map keys. Arbitrary type except Nullable and LowCardinality nested with Nullable types.V
— The type of the Map values. Arbitrary type.
Examples
Create a table with a column of type map:
To select key2
values:
Result:
If the requested key k
is not contained in the map, m[k]
returns the value type's default value, e.g. 0
for integer types and ''
for string types.
To check whether a key exists in a map, you can use function mapContains.
Result:
Converting Tuple to Map
Values of type Tuple()
can be cast to values of type Map()
using function CAST:
Example
Query:
Result:
Reading subcolumns of Map
To avoid reading the entire map, you can use subcolumns keys
and values
in some cases.
Example
Query:
Result:
See Also
- map() function
- CAST() function
- -Map combinator for Map datatype