> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to look up values of one array by keys from another array

> How to translate an array of keys, or nested arrays of keys, into the matching values from a second array with mapFromArrays and arrayMap, and what happens for missing keys.

When you have an array of keys and an array of the corresponding values, build a `Map` from them with [`mapFromArrays`](https://clickhouse.com/docs/reference/functions/regular-functions/tuple-map-functions#mapFromArrays), then use [`arrayMap`](https://clickhouse.com/docs/reference/functions/regular-functions/array-functions#arrayMap) to replace every key of another array with its value. For nested arrays of keys, nest `arrayMap`.

## Example

The array `k` holds the keys, `v` holds the values at the same positions, and `r` holds nested arrays of keys to translate:

```sql theme={null}
WITH
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] AS k,
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] AS v,
    [[1, 3, 5, 7, 9], [2, 4, 6, 8], [1, 10], [2, 9]] AS r,
    mapFromArrays(k, v) AS m
SELECT arrayMap(i -> arrayMap(ii -> m[ii], i), r) AS output;
```

```text theme={null}
   ┌─output────────────────────────────────────────────────────────┐
1. │ [['a','c','e','g','i'],['b','d','f','h'],['a','j'],['b','i']] │
   └───────────────────────────────────────────────────────────────┘
```

How it works:

1. `mapFromArrays(k, v)` pairs the elements of the two arrays by position and returns a `Map`. Both arrays must have the same size.
2. The outer `arrayMap` processes each inner array `i` of `r`.
3. The inner `arrayMap` processes each key `ii` of the inner array and replaces it with `m[ii]`, the value stored in the map for that key.

For a flat array of keys, one `arrayMap` is enough: `arrayMap(x -> m[x], keys_to_translate)`.

## Use it with table columns

The arrays can be columns, so every row can have its own mapping:

```sql theme={null}
CREATE TABLE lookups
(
    id UInt32,
    keys Array(UInt32),
    labels Array(String),
    requests Array(Array(UInt32))
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO lookups VALUES
    (1, [1, 2, 3], ['a', 'b', 'c'], [[1, 3], [2]]),
    (2, [10, 20], ['x', 'y'], [[20, 10, 20], [], [10]]);

WITH mapFromArrays(keys, labels) AS m
SELECT
    id,
    arrayMap(request -> arrayMap(key -> m[key], request), requests) AS output
FROM lookups
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─output───────────────────┐
1. │  1 │ [['a','c'],['b']]        │
2. │  2 │ [['y','x','y'],[],['x']] │
   └────┴──────────────────────────┘
```

## Keys that are missing from the map

If a key is not in the map, `m[key]` returns the default value of the value type: an empty string for `String`, `0` for numbers. Use [`mapContains`](https://clickhouse.com/docs/reference/functions/regular-functions/tuple-map-functions#mapContainsKey) to return `NULL` instead, or to skip such keys:

```sql theme={null}
WITH mapFromArrays([1, 2, 3], ['a', 'b', 'c']) AS m
SELECT
    arrayMap(x -> m[x], [1, 42, 3]) AS with_default,
    arrayMap(x -> if(mapContains(m, x), m[x], NULL), [1, 42, 3]) AS with_null,
    arrayMap(x -> m[x], arrayFilter(x -> mapContains(m, x), [1, 42, 3])) AS skipped;
```

```text theme={null}
   ┌─with_default─┬─with_null──────┬─skipped───┐
1. │ ['a','','c'] │ ['a',NULL,'c'] │ ['a','c'] │
   └──────────────┴────────────────┴───────────┘
```

## Constant mappings

If the keys and values are constant arrays, the [`transform`](https://clickhouse.com/docs/reference/functions/regular-functions/other-functions#transform) function does the same lookup without a map and lets you choose the value for missing keys:

```sql theme={null}
SELECT arrayMap(x -> transform(x, [1, 2, 3], ['a', 'b', 'c'], '?'), [3, 1, 42]) AS output;
```

```text theme={null}
   ┌─output────────┐
1. │ ['c','a','?'] │
   └───────────────┘
```

`transform` requires its second and third arguments to be constants, so it does not work when the mapping comes from table columns.

## Things to keep in mind

* `m[key]` scans the map, so the time of one lookup grows linearly with the size of the map. This is fine for small mappings stored in each row. For a large mapping shared by all rows, use a [dictionary](https://clickhouse.com/docs/reference/statements/create/dictionary.md) and [`dictGet`](https://clickhouse.com/docs/reference/functions/regular-functions/ext-dict-functions#dictGet) instead.
* A `Map` can contain the same key more than once. If the keys array has duplicates, decide which value you want before building the map.

See the [`Map(K, V)` data type](https://clickhouse.com/docs/reference/data-types/map) for details.
