2020-04-03 13:23:32 +00:00
---
toc_priority: 58
toc_title: Working with External Dictionaries
---
2017-12-28 15:13:23 +00:00
2020-04-30 18:19:18 +00:00
# Functions for Working with External Dictionaries {#ext_dict_functions}
2020-04-03 13:23:32 +00:00
2020-04-30 18:19:18 +00:00
For information on connecting and configuring external dictionaries, see [External dictionaries ](../../sql-reference/dictionaries/external-dictionaries/external-dicts.md ).
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## dictGet {#dictget}
2019-06-14 14:27:25 +00:00
Retrieves a value from an external dictionary.
2020-03-20 10:10:48 +00:00
``` sql
2019-06-14 14:27:25 +00:00
dictGet('dict_name', 'attr_name', id_expr)
dictGetOrDefault('dict_name', 'attr_name', id_expr, default_value_expr)
```
**Parameters**
2020-03-21 04:11:51 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../syntax.md#syntax-string-literal ).
- `attr_name` — Name of the column of the dictionary. [String literal ](../syntax.md#syntax-string-literal ).
2020-04-30 18:19:18 +00:00
- `id_expr` — Key value. [Expression ](../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.
2020-03-21 04:11:51 +00:00
- `default_value_expr` — Value returned if the dictionary doesn’ t contain a row with the `id_expr` key. [Expression ](../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**
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.
2019-06-14 14:27:25 +00:00
2020-03-20 10:10:48 +00:00
ClickHouse throws an exception if it cannot parse the value of the attribute or the value doesn’ t match the attribute data type.
2019-06-14 14:27:25 +00:00
2019-06-23 12:34:22 +00:00
**Example**
2019-06-14 14:27:25 +00:00
2019-06-23 12:34:22 +00:00
Create a text file `ext-dict-text.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
Configure the external dictionary:
2020-03-20 10:10:48 +00:00
``` xml
2019-06-14 14:27:25 +00:00
< yandex >
< 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 >
< / yandex >
```
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
LIMIT 3
```
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 │
└─────┴────────┘
```
**See Also**
2020-04-30 18:19:18 +00:00
- [External Dictionaries ](../../sql-reference/dictionaries/external-dictionaries/external-dicts.md )
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## dictHas {#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
2019-06-14 14:27:25 +00:00
**Parameters**
2017-12-28 15:13:23 +00:00
2020-03-21 04:11:51 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../syntax.md#syntax-string-literal ).
2020-04-30 18:19:18 +00:00
- `id_expr` — Key value. [Expression ](../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**
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` .
2020-03-18 18:43:51 +00:00
## dictGetHierarchy {#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
2019-06-14 14:27:25 +00:00
**Parameters**
2018-01-19 14:36:40 +00:00
2020-03-21 04:11:51 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../syntax.md#syntax-string-literal ).
2020-04-30 18:19:18 +00:00
- `key` — Key value. [Expression ](../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
2020-03-20 10:10:48 +00:00
## dictIsIn {#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
2019-06-14 14:27:25 +00:00
**Parameters**
2017-12-28 15:13:23 +00:00
2020-03-21 04:11:51 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../syntax.md#syntax-string-literal ).
2020-04-30 18:19:18 +00:00
- `child_id_expr` — Key to be checked. [Expression ](../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 ](../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` .
2020-04-03 13:23:32 +00:00
## Other Functions {#ext_dict_functions-other}
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`
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)
```
**Parameters**
2020-03-21 04:11:51 +00:00
- `dict_name` — Name of the dictionary. [String literal ](../syntax.md#syntax-string-literal ).
- `attr_name` — Name of the column of the dictionary. [String literal ](../syntax.md#syntax-string-literal ).
2020-04-30 18:19:18 +00:00
- `id_expr` — Key value. [Expression ](../syntax.md#syntax-expressions ) returning a [UInt64 ](../../sql-reference/data-types/int-uint.md )-type value.
2020-03-21 04:11:51 +00:00
- `default_value_expr` — Value which is returned if the dictionary doesn’ t contain a row with the `id_expr` key. [Expression ](../syntax.md#syntax-expressions ) returning a 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
2020-03-20 10:10:48 +00:00
ClickHouse throws an exception if it cannot parse the value of the attribute or the value doesn’ t match the attribute data type.
2018-10-16 10:47:17 +00:00
2020-01-30 10:34:55 +00:00
[Original article ](https://clickhouse.tech/docs/en/query_language/functions/ext_dict_functions/ ) <!--hide-->