2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/ext-dict-functions
2022-04-09 13:29:05 +00:00
sidebar_position: 58
2022-11-07 14:26:50 +00:00
sidebar_label: Dictionaries
2020-04-03 13:23:32 +00:00
---
2017-12-28 15:13:23 +00:00
2022-11-07 14:26:50 +00:00
# Functions for Working with Dictionaries
2022-04-09 13:29:05 +00:00
:::note
For dictionaries created with [DDL queries ](../../sql-reference/statements/create/dictionary.md ), the `dict_name` parameter must be fully specified, like `<database>.<dict_name>` . Otherwise, the current database is used.
:::
2020-09-09 11:33:34 +00:00
2022-11-07 14:26:50 +00:00
For information on connecting and configuring dictionaries, see [Dictionaries ](../../sql-reference/dictionaries/external-dictionaries/external-dicts.md ).
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## dictGet, dictGetOrDefault, dictGetOrNull
2019-06-14 14:27:25 +00:00
2022-11-07 14:26:50 +00:00
Retrieves values from a dictionary.
2019-06-14 14:27:25 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-05-01 17:46:02 +00:00
dictGet('dict_name', attr_names, id_expr)
dictGetOrDefault('dict_name', attr_names, id_expr, default_value_expr)
2021-05-13 18:19:28 +00:00
dictGetOrNull('dict_name', attr_name, id_expr)
2019-06-14 14:27:25 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-06-14 14:27:25 +00:00
2020-06-18 08:24:31 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
2021-05-01 17:46:02 +00:00
- `attr_names` — Name of the column of the dictionary, [String literal ](../../sql-reference/syntax.md#syntax-string-literal ), or tuple of column names, [Tuple ](../../sql-reference/data-types/tuple.md )([String literal](../../sql-reference/syntax.md#syntax-string-literal)).
2021-07-09 14:20:02 +00:00
- `id_expr` — Key value. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning dictionary key-type value or [Tuple ](../../sql-reference/data-types/tuple.md )-type value depending on the dictionary configuration.
2021-05-27 19:44:11 +00:00
- `default_value_expr` — Values returned if the dictionary does not contain a row with the `id_expr` key. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) or [Tuple ](../../sql-reference/data-types/tuple.md )([Expression](../../sql-reference/syntax.md#syntax-expressions)), returning the value (or values) in the data types configured for the `attr_names` attribute.
2019-06-14 14:27:25 +00:00
**Returned value**
2020-04-30 18:19:18 +00:00
- If ClickHouse parses the attribute successfully in the [attribute’ s data type ](../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md#ext_dict_structure-attributes ), functions return the value of the dictionary attribute that corresponds to `id_expr` .
2020-03-20 10:10:48 +00:00
2020-03-21 04:11:51 +00:00
- If there is no the key, corresponding to `id_expr` , in the dictionary, then:
2019-06-14 14:27:25 +00:00
2020-03-21 04:11:51 +00:00
- `dictGet` returns the content of the `<null_value>` element specified for the attribute in the dictionary configuration.
- `dictGetOrDefault` returns the value passed as the `default_value_expr` parameter.
2021-05-13 18:18:26 +00:00
- `dictGetOrNull` returns `NULL` in case key was not found in dictionary.
2019-06-14 14:27:25 +00:00
2021-05-27 19:44:11 +00:00
ClickHouse throws an exception if it cannot parse the value of the attribute or the value does not match the attribute data type.
2019-06-14 14:27:25 +00:00
2021-05-13 19:24:41 +00:00
**Example for simple key dictionary**
2019-06-14 14:27:25 +00:00
2021-05-13 19:24:41 +00:00
Create a text file `ext-dict-test.csv` containing the following:
2019-06-14 14:27:25 +00:00
2020-03-20 10:10:48 +00:00
``` text
2019-06-14 14:27:25 +00:00
1,1
2,2
```
2019-06-23 12:34:22 +00:00
The first column is `id` , the second column is `c1` .
2019-06-14 14:27:25 +00:00
2022-11-07 14:26:50 +00:00
Configure the dictionary:
2019-06-14 14:27:25 +00:00
2020-03-20 10:10:48 +00:00
``` xml
2021-10-26 05:50:15 +00:00
< clickhouse >
2019-06-14 14:27:25 +00:00
< dictionary >
< name > ext-dict-test< / name >
< source >
< file >
< path > /path-to/ext-dict-test.csv< / path >
< format > CSV< / format >
< / file >
< / source >
< layout >
< flat / >
< / layout >
< structure >
< id >
< name > id< / name >
< / id >
< attribute >
< name > c1< / name >
< type > UInt32< / type >
< null_value > < / null_value >
< / attribute >
< / structure >
< lifetime > 0< / lifetime >
< / dictionary >
2021-10-26 05:50:15 +00:00
< / clickhouse >
2019-06-14 14:27:25 +00:00
```
Perform the query:
2020-03-20 10:10:48 +00:00
``` sql
2019-06-14 14:27:25 +00:00
SELECT
dictGetOrDefault('ext-dict-test', 'c1', number + 1, toUInt32(number * 10)) AS val,
toTypeName(val) AS type
FROM system.numbers
2021-07-19 12:10:33 +00:00
LIMIT 3;
2019-06-14 14:27:25 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-06-14 14:27:25 +00:00
┌─val─┬─type───┐
│ 1 │ UInt32 │
│ 2 │ UInt32 │
│ 20 │ UInt32 │
└─────┴────────┘
```
2021-05-13 19:24:41 +00:00
**Example for complex key dictionary**
2021-05-01 17:46:02 +00:00
Create a text file `ext-dict-mult.csv` containing the following:
``` text
1,1,'1'
2,2,'2'
3,3,'3'
```
The first column is `id` , the second is `c1` , the third is `c2` .
2022-11-07 14:26:50 +00:00
Configure the dictionary:
2021-05-01 17:46:02 +00:00
``` xml
2021-10-26 05:50:15 +00:00
< clickhouse >
2021-05-01 17:46:02 +00:00
< dictionary >
< name > ext-dict-mult< / name >
< source >
< file >
< path > /path-to/ext-dict-mult.csv< / path >
< format > CSV< / format >
< / file >
< / source >
< layout >
< flat / >
< / layout >
< structure >
< id >
< name > id< / name >
< / id >
< attribute >
< name > c1< / name >
< type > UInt32< / type >
< null_value > < / null_value >
< / attribute >
< attribute >
< name > c2< / name >
< type > String< / type >
< null_value > < / null_value >
2021-07-09 14:20:02 +00:00
< / attribute >
2021-05-01 17:46:02 +00:00
< / structure >
< lifetime > 0< / lifetime >
< / dictionary >
2021-10-26 05:50:15 +00:00
< / clickhouse >
2021-05-01 17:46:02 +00:00
```
Perform the query:
``` sql
SELECT
2022-11-28 20:24:43 +00:00
dictGet('ext-dict-mult', ('c1','c2'), number + 1) AS val,
2021-05-01 17:46:02 +00:00
toTypeName(val) AS type
FROM system.numbers
2021-05-11 21:08:46 +00:00
LIMIT 3;
2021-05-01 17:46:02 +00:00
```
``` text
┌─val─────┬─type──────────────────┐
│ (1,'1') │ Tuple(UInt8, String) │
│ (2,'2') │ Tuple(UInt8, String) │
│ (3,'3') │ Tuple(UInt8, String) │
└─────────┴───────────────────────┘
```
2021-05-13 19:24:41 +00:00
**Example for range key dictionary**
Input table:
```sql
CREATE TABLE range_key_dictionary_source_table
(
key UInt64,
start_date Date,
end_date Date,
value String,
value_nullable Nullable(String)
)
ENGINE = TinyLog();
INSERT INTO range_key_dictionary_source_table VALUES(1, toDate('2019-05-20'), toDate('2019-05-20'), 'First', 'First');
INSERT INTO range_key_dictionary_source_table VALUES(2, toDate('2019-05-20'), toDate('2019-05-20'), 'Second', NULL);
INSERT INTO range_key_dictionary_source_table VALUES(3, toDate('2019-05-20'), toDate('2019-05-20'), 'Third', 'Third');
```
2022-11-07 14:26:50 +00:00
Create the dictionary:
2021-05-13 19:24:41 +00:00
```sql
CREATE DICTIONARY range_key_dictionary
(
key UInt64,
start_date Date,
end_date Date,
value String,
value_nullable Nullable(String)
)
PRIMARY KEY key
SOURCE(CLICKHOUSE(HOST 'localhost' PORT tcpPort() TABLE 'range_key_dictionary_source_table'))
LIFETIME(MIN 1 MAX 1000)
LAYOUT(RANGE_HASHED())
RANGE(MIN start_date MAX end_date);
```
Perform the query:
``` sql
SELECT
(number, toDate('2019-05-20')),
dictHas('range_key_dictionary', number, toDate('2019-05-20')),
dictGetOrNull('range_key_dictionary', 'value', number, toDate('2019-05-20')),
dictGetOrNull('range_key_dictionary', 'value_nullable', number, toDate('2019-05-20')),
dictGetOrNull('range_key_dictionary', ('value', 'value_nullable'), number, toDate('2019-05-20'))
FROM system.numbers LIMIT 5 FORMAT TabSeparated;
```
Result:
``` text
(0,'2019-05-20') 0 \N \N (NULL,NULL)
(1,'2019-05-20') 1 First First ('First','First')
2022-01-08 14:16:03 +00:00
(2,'2019-05-20') 1 Second \N ('Second',NULL)
(3,'2019-05-20') 1 Third Third ('Third','Third')
2021-05-13 19:24:41 +00:00
(4,'2019-05-20') 0 \N \N (NULL,NULL)
```
2019-06-14 14:27:25 +00:00
**See Also**
2022-11-07 14:26:50 +00:00
- [Dictionaries ](../../sql-reference/dictionaries/external-dictionaries/external-dicts.md )
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## dictHas
2019-06-14 14:27:25 +00:00
2019-06-23 12:34:22 +00:00
Checks whether a key is present in a dictionary.
2019-06-14 14:27:25 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-07-18 15:59:22 +00:00
dictHas('dict_name', id_expr)
2019-06-14 14:27:25 +00:00
```
2017-12-28 15:13:23 +00:00
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2020-06-18 08:24:31 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
2021-07-09 14:20:02 +00:00
- `id_expr` — Key value. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning dictionary key-type value or [Tuple ](../../sql-reference/data-types/tuple.md )-type value depending on the dictionary configuration.
2017-12-28 15:13:23 +00:00
2019-06-14 14:27:25 +00:00
**Returned value**
2018-09-04 11:18:59 +00:00
2020-03-21 04:11:51 +00:00
- 0, if there is no key.
- 1, if there is a key.
2019-06-14 14:27:25 +00:00
Type: `UInt8` .
2022-06-02 10:55:18 +00:00
## dictGetHierarchy
2017-12-28 15:13:23 +00:00
2020-04-30 18:19:18 +00:00
Creates an array, containing all the parents of a key in the [hierarchical dictionary ](../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-hierarchical.md ).
2020-02-08 11:21:22 +00:00
**Syntax**
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2020-02-08 11:21:22 +00:00
dictGetHierarchy('dict_name', key)
2019-06-14 14:27:25 +00:00
```
2017-12-28 15:13:23 +00:00
2021-02-15 21:22:10 +00:00
**Arguments**
2018-01-19 14:36:40 +00:00
2020-06-18 08:24:31 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
- `key` — Key value. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning a [UInt64 ](../../sql-reference/data-types/int-uint.md )-type value.
2019-06-14 14:27:25 +00:00
**Returned value**
2020-03-21 04:11:51 +00:00
- Parents for the key.
2020-02-08 11:21:22 +00:00
2020-04-30 18:19:18 +00:00
Type: [Array(UInt64) ](../../sql-reference/data-types/array.md ).
2019-06-14 14:27:25 +00:00
2022-06-02 10:55:18 +00:00
## dictIsIn
2017-12-28 15:13:23 +00:00
2019-07-31 13:16:29 +00:00
Checks the ancestor of a key through the whole hierarchical chain in the dictionary.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-07-12 04:50:01 +00:00
dictIsIn('dict_name', child_id_expr, ancestor_id_expr)
```
2017-12-28 15:13:23 +00:00
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2020-06-18 08:24:31 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
- `child_id_expr` — Key to be checked. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning a [UInt64 ](../../sql-reference/data-types/int-uint.md )-type value.
- `ancestor_id_expr` — Alleged ancestor of the `child_id_expr` key. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning a [UInt64 ](../../sql-reference/data-types/int-uint.md )-type value.
2017-12-28 15:13:23 +00:00
2019-06-14 14:27:25 +00:00
**Returned value**
2017-12-28 15:13:23 +00:00
2020-03-21 04:11:51 +00:00
- 0, if `child_id_expr` is not a child of `ancestor_id_expr` .
- 1, if `child_id_expr` is a child of `ancestor_id_expr` or if `child_id_expr` is an `ancestor_id_expr` .
2019-06-14 14:27:25 +00:00
Type: `UInt8` .
2022-06-02 10:55:18 +00:00
## dictGetChildren
2021-05-30 10:39:32 +00:00
2021-05-30 10:43:49 +00:00
Returns first-level children as an array of indexes. It is the inverse transformation for [dictGetHierarchy ](#dictgethierarchy ).
2021-05-30 10:39:32 +00:00
2021-07-09 14:20:02 +00:00
**Syntax**
2021-05-30 10:39:32 +00:00
``` sql
dictGetChildren(dict_name, key)
```
2021-07-09 14:20:02 +00:00
**Arguments**
2021-05-30 10:39:32 +00:00
2021-07-09 14:20:02 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
- `key` — Key value. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning a [UInt64 ](../../sql-reference/data-types/int-uint.md )-type value.
2021-05-30 10:39:32 +00:00
**Returned values**
- First-level descendants for the key.
2021-05-30 18:14:56 +00:00
Type: [Array ](../../sql-reference/data-types/array.md )([UInt64](../../sql-reference/data-types/int-uint.md)).
2021-05-30 10:39:32 +00:00
2021-05-30 18:24:14 +00:00
**Example**
Consider the hierarchic dictionary:
``` text
┌─id─┬─parent_id─┐
│ 1 │ 0 │
│ 2 │ 1 │
│ 3 │ 1 │
│ 4 │ 2 │
└────┴───────────┘
```
First-level children:
``` sql
SELECT dictGetChildren('hierarchy_flat_dictionary', number) FROM system.numbers LIMIT 4;
```
``` text
┌─dictGetChildren('hierarchy_flat_dictionary', number)─┐
│ [1] │
│ [2,3] │
│ [4] │
│ [] │
└──────────────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## dictGetDescendant
2021-05-30 10:39:32 +00:00
2021-07-09 14:20:02 +00:00
Returns all descendants as if [dictGetChildren ](#dictgetchildren ) function was applied `level` times recursively.
2021-05-30 10:39:32 +00:00
**Syntax**
``` sql
dictGetDescendants(dict_name, key, level)
```
2021-07-09 14:20:02 +00:00
**Arguments**
2021-05-30 10:39:32 +00:00
2021-07-09 14:20:02 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
2021-05-30 10:39:32 +00:00
- `key` — Key value. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning a [UInt64 ](../../sql-reference/data-types/int-uint.md )-type value.
2021-05-30 10:43:49 +00:00
- `level` — Hierarchy level. If `level = 0` returns all descendants to the end. [UInt8 ](../../sql-reference/data-types/int-uint.md ).
2021-05-30 10:39:32 +00:00
**Returned values**
- Descendants for the key.
2021-05-30 18:15:01 +00:00
Type: [Array ](../../sql-reference/data-types/array.md )([UInt64](../../sql-reference/data-types/int-uint.md)).
2021-05-30 10:39:32 +00:00
2021-05-30 12:21:43 +00:00
**Example**
2021-05-30 18:24:14 +00:00
Consider the hierarchic dictionary:
2021-05-30 12:21:43 +00:00
2021-05-30 18:24:14 +00:00
``` text
┌─id─┬─parent_id─┐
│ 1 │ 0 │
│ 2 │ 1 │
│ 3 │ 1 │
│ 4 │ 2 │
└────┴───────────┘
2021-05-30 12:21:43 +00:00
```
2021-05-30 18:24:14 +00:00
All descendants:
2021-05-30 12:21:43 +00:00
``` sql
SELECT dictGetDescendants('hierarchy_flat_dictionary', number) FROM system.numbers LIMIT 4;
```
``` text
┌─dictGetDescendants('hierarchy_flat_dictionary', number)─┐
│ [1,2,3,4] │
│ [2,3,4] │
│ [4] │
│ [] │
└─────────────────────────────────────────────────────────┘
2021-05-30 18:24:14 +00:00
```
First-level descendants:
``` sql
SELECT dictGetDescendants('hierarchy_flat_dictionary', number, 1) FROM system.numbers LIMIT 4;
```
``` text
2021-05-30 12:21:43 +00:00
┌─dictGetDescendants('hierarchy_flat_dictionary', number, 1)─┐
│ [1] │
│ [2,3] │
│ [4] │
│ [] │
└────────────────────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## Other Functions
2019-06-14 14:27:25 +00:00
2019-06-23 12:34:22 +00:00
ClickHouse supports specialized functions that convert dictionary attribute values to a specific data type regardless of the dictionary configuration.
2019-06-14 14:27:25 +00:00
Functions:
2020-03-21 04:11:51 +00:00
- `dictGetInt8` , `dictGetInt16` , `dictGetInt32` , `dictGetInt64`
- `dictGetUInt8` , `dictGetUInt16` , `dictGetUInt32` , `dictGetUInt64`
- `dictGetFloat32` , `dictGetFloat64`
- `dictGetDate`
- `dictGetDateTime`
- `dictGetUUID`
- `dictGetString`
2022-11-25 15:57:49 +00:00
- `dictGetIPv4` , `dictGetIPv6`
2019-06-14 14:27:25 +00:00
2019-07-26 06:57:17 +00:00
All these functions have the `OrDefault` modification. For example, `dictGetDateOrDefault` .
2019-06-14 14:27:25 +00:00
Syntax:
2020-03-20 10:10:48 +00:00
``` sql
2019-06-14 14:27:25 +00:00
dictGet[Type]('dict_name', 'attr_name', id_expr)
dictGet[Type]OrDefault('dict_name', 'attr_name', id_expr, default_value_expr)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-06-14 14:27:25 +00:00
2020-06-18 08:24:31 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
- `attr_name` — Name of the column of the dictionary. [String literal ](../../sql-reference/syntax.md#syntax-string-literal ).
2020-09-15 14:11:34 +00:00
- `id_expr` — Key value. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning a [UInt64 ](../../sql-reference/data-types/int-uint.md ) or [Tuple ](../../sql-reference/data-types/tuple.md )-type value depending on the dictionary configuration.
2021-05-27 19:44:11 +00:00
- `default_value_expr` — Value returned if the dictionary does not contain a row with the `id_expr` key. [Expression ](../../sql-reference/syntax.md#syntax-expressions ) returning the value in the data type configured for the `attr_name` attribute.
2019-06-14 14:27:25 +00:00
**Returned value**
2017-12-28 15:13:23 +00:00
2020-04-30 18:19:18 +00:00
- If ClickHouse parses the attribute successfully in the [attribute’ s data type ](../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md#ext_dict_structure-attributes ), functions return the value of the dictionary attribute that corresponds to `id_expr` .
2020-03-20 10:10:48 +00:00
2020-03-21 04:11:51 +00:00
- If there is no requested `id_expr` in the dictionary then:
2017-12-28 15:13:23 +00:00
2020-03-21 04:11:51 +00:00
- `dictGet[Type]` returns the content of the `<null_value>` element specified for the attribute in the dictionary configuration.
- `dictGet[Type]OrDefault` returns the value passed as the `default_value_expr` parameter.
2017-12-28 15:13:23 +00:00
2021-05-27 19:44:11 +00:00
ClickHouse throws an exception if it cannot parse the value of the attribute or the value does not match the attribute data type.