ClickHouse/docs/en/sql-reference/dictionaries/external-dictionaries/external-dicts-dict-hierarchical.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
2.1 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/dictionaries/external-dictionaries/external-dicts-dict-hierarchical
sidebar_position: 45
sidebar_label: Hierarchical dictionaries
2020-04-03 13:23:32 +00:00
---
# Hierarchical Dictionaries
ClickHouse supports hierarchical dictionaries with a [numeric key](../../dictionaries/external-dictionaries/external-dicts-dict-structure.md#numeric-key).
Look at the following hierarchical structure:
2020-03-20 10:10:48 +00:00
``` text
0 (Common parent)
├── 1 (Russia)
│ │
2020-03-20 10:10:48 +00:00
│ └── 2 (Moscow)
│ │
2020-03-20 10:10:48 +00:00
│ └── 3 (Center)
└── 4 (Great Britain)
└── 5 (London)
```
This hierarchy can be expressed as the following dictionary table.
2020-10-13 17:23:29 +00:00
| region_id | parent_region | region_name |
|------------|----------------|---------------|
2020-03-20 10:10:48 +00:00
| 1 | 0 | Russia |
| 2 | 1 | Moscow |
| 3 | 2 | Center |
| 4 | 0 | Great Britain |
| 5 | 4 | London |
This table contains a column `parent_region` that contains the key of the nearest parent for the element.
ClickHouse supports the [hierarchical](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md#hierarchical-dict-attr) property for [external dictionary](../../../sql-reference/dictionaries/external-dictionaries/) attributes. This property allows you to configure the hierarchical dictionary similar to described above.
The [dictGetHierarchy](../../../sql-reference/functions/ext-dict-functions.md#dictgethierarchy) function allows you to get the parent chain of an element.
For our example, the structure of dictionary can be the following:
2020-03-20 10:10:48 +00:00
``` xml
<dictionary>
<structure>
<id>
<name>region_id</name>
</id>
<attribute>
<name>parent_region</name>
<type>UInt64</type>
<null_value>0</null_value>
<hierarchical>true</hierarchical>
</attribute>
<attribute>
<name>region_name</name>
<type>String</type>
<null_value></null_value>
</attribute>
</structure>
</dictionary>
```