Hierarchical Dictionaries

ClickHouse supports hierarchical dictionaries with a numeric key.

Look at the following hierarchical structure:

  1. 0 (Common parent)
  2. ├── 1 (Russia)
  3. └── 2 (Moscow)
  4. └── 3 (Center)
  5. └── 4 (Great Britain)
  6. └── 5 (London)

This hierarchy can be expressed as the following dictionary table.

region_idparent_regionregion_name
10Russia
21Moscow
32Center
40Great Britain
54London

This table contains a column parent_region that contains the key of the nearest parent for the element.

ClickHouse supports the hierarchical property for external dictionary attributes. This property allows you to configure the hierarchical dictionary similar to described above.

The dictGetHierarchy function allows you to get the parent chain of an element.

For our example, the structure of dictionary can be the following:

  1. <dictionary>
  2. <structure>
  3. <id>
  4. <name>region_id</name>
  5. </id>
  6. <attribute>
  7. <name>parent_region</name>
  8. <type>UInt64</type>
  9. <null_value>0</null_value>
  10. <hierarchical>true</hierarchical>
  11. </attribute>
  12. <attribute>
  13. <name>region_name</name>
  14. <type>String</type>
  15. <null_value></null_value>
  16. </attribute>
  17. </structure>
  18. </dictionary>

Original article