Functions for maps

map

Arranges key:value pairs into Map(key, value) data type.

Syntax

  1. map(key1, value1[, key2, value2, ...])

Arguments

Returned value

  • Data structure as key:value pairs.

Type: Map(key, value).

Examples

Query:

  1. SELECT map('key1', number, 'key2', number * 2) FROM numbers(3);

Result:

  1. ┌─map('key1', number, 'key2', multiply(number, 2))─┐
  2. {'key1':0,'key2':0}
  3. {'key1':1,'key2':2}
  4. {'key1':2,'key2':4}
  5. └──────────────────────────────────────────────────┘

Query:

  1. CREATE TABLE table_map (a Map(String, UInt64)) ENGINE = MergeTree() ORDER BY a;
  2. INSERT INTO table_map SELECT map('key1', number, 'key2', number * 2) FROM numbers(3);
  3. SELECT a['key2'] FROM table_map;

Result:

  1. ┌─arrayElement(a, 'key2')─┐
  2. 0
  3. 2
  4. 4
  5. └─────────────────────────┘

See Also

mapAdd

Collect all the keys and sum corresponding values.

Syntax

  1. mapAdd(arg1, arg2 [, ...])

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 a tuple:

  1. SELECT mapAdd(([toUInt8(1), 2], [1, 1]), ([toUInt8(1), 2], [1, 1])) as res, toTypeName(res) as type;

Result:

  1. ┌─res───────────┬─type───────────────────────────────┐
  2. ([1,2],[2,2]) Tuple(Array(UInt8), Array(UInt64))
  3. └───────────────┴────────────────────────────────────┘

Query with Map type:

  1. SELECT mapAdd(map(1,1), map(1,1));

Result:

  1. ┌─mapAdd(map(1, 1), map(1, 1))─┐
  2. {1:2}
  3. └──────────────────────────────┘

mapSubtract

Collect all the keys and subtract corresponding values.

Syntax

  1. mapSubtract(Tuple(Array, Array), Tuple(Array, Array) [, ...])

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 a tuple map:

  1. SELECT mapSubtract(([toUInt8(1), 2], [toInt32(1), 1]), ([toUInt8(1), 2], [toInt32(2), 1])) as res, toTypeName(res) as type;

Result:

  1. ┌─res────────────┬─type──────────────────────────────┐
  2. ([1,2],[-1,0]) Tuple(Array(UInt8), Array(Int64))
  3. └────────────────┴───────────────────────────────────┘

Query with Map type:

  1. SELECT mapSubtract(map(1,1), map(1,1));

Result:

  1. ┌─mapSubtract(map(1, 1), map(1, 1))─┐
  2. {1:0}
  3. └───────────────────────────────────┘

mapPopulateSeries

Fills missing keys in the maps (key and value array pair), where keys are integers. Also, it supports specifying the max key, which is used to extend the keys array.
Arguments are maps or two arrays, where the first array represent keys, and the second array contains values for the each key.

For array arguments the number of elements in keys and values must be the same for each row.

Syntax

  1. mapPopulateSeries(keys, values[, max])
  2. mapPopulateSeries(map[, max])

Generates a map (a tuple with two arrays or a value of Map type, depending on the arguments), where keys are a series of numbers, from minimum to maximum keys (or max argument if it specified) taken from the map with a step size of one, and corresponding values. If the value is not specified for the key, then it uses the default value in the resulting map. For repeated keys, only the first value (in order of appearing) gets associated with the key.

Arguments

Mapped arrays:

or

  • map — Map with integer keys. Map.

Returned value

  • Depending on the arguments returns a map or a tuple of two arrays: keys in sorted order, and values the corresponding keys.

Example

Query with mapped arrays:

  1. select mapPopulateSeries([1,2,4], [11,22,44], 5) as res, toTypeName(res) as type;

Result:

  1. ┌─res──────────────────────────┬─type──────────────────────────────┐
  2. ([1,2,3,4,5],[11,22,0,44,0]) Tuple(Array(UInt8), Array(UInt8))
  3. └──────────────────────────────┴───────────────────────────────────┘

Query with Map type:

  1. SELECT mapPopulateSeries(map(1, 10, 5, 20), 6);

Result:

  1. ┌─mapPopulateSeries(map(1, 10, 5, 20), 6)─┐
  2. {1:10,2:0,3:0,4:0,5:20,6:0}
  3. └─────────────────────────────────────────┘

mapContains

Determines whether the map contains the key parameter.

Syntax

  1. mapContains(map, key)

Parameters

  • map — Map. Map.
  • key — Key. Type matches the type of keys of map parameter.

Returned value

  • 1 if map contains key, 0 if not.

Type: UInt8.

Example

Query:

  1. CREATE TABLE test (a Map(String,String)) ENGINE = Memory;
  2. INSERT INTO test VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
  3. SELECT mapContains(a, 'name') FROM test;

Result:

  1. ┌─mapContains(a, 'name')─┐
  2. 1
  3. 0
  4. └────────────────────────┘

mapKeys

Returns all keys from the map parameter.

Can be optimized by enabling the optimize_functions_to_subcolumns setting. With optimize_functions_to_subcolumns = 1 the function reads only keys subcolumn instead of reading and processing the whole column data. The query SELECT mapKeys(m) FROM table transforms to SELECT m.keys FROM table.

Syntax

  1. mapKeys(map)

Parameters

  • map — Map. Map.

Returned value

  • Array containing all keys from the map.

Type: Array.

Example

Query:

  1. CREATE TABLE test (a Map(String,String)) ENGINE = Memory;
  2. INSERT INTO test VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
  3. SELECT mapKeys(a) FROM test;

Result:

  1. ┌─mapKeys(a)────────────┐
  2. ['name','age']
  3. ['number','position']
  4. └───────────────────────┘

mapValues

Returns all values from the map parameter.

Can be optimized by enabling the optimize_functions_to_subcolumns setting. With optimize_functions_to_subcolumns = 1 the function reads only values subcolumn instead of reading and processing the whole column data. The query SELECT mapValues(m) FROM table transforms to SELECT m.values FROM table.

Syntax

  1. mapKeys(map)

Parameters

  • map — Map. Map.

Returned value

  • Array containing all the values from map.

Type: Array.

Example

Query:

  1. CREATE TABLE test (a Map(String,String)) ENGINE = Memory;
  2. INSERT INTO test VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
  3. SELECT mapValues(a) FROM test;

Result:

  1. ┌─mapValues(a)─────┐
  2. ['eleven','11']
  3. ['twelve','6.0']
  4. └──────────────────┘