2020-04-03 13:23:32 +00:00
---
2022-04-09 13:29:05 +00:00
sidebar_position: 59
sidebar_label: Embedded Dictionaries
2020-04-03 13:23:32 +00:00
---
2022-03-05 10:23:52 +00:00
# Functions for Working with Embedded Dictionaries
2017-12-28 15:13:23 +00:00
2022-03-05 10:23:52 +00:00
In order for the functions below to work, the server config must specify the paths and addresses for getting all the embedded dictionaries. The dictionaries are loaded at the first call of any of these functions. If the reference lists can’ t be loaded, an exception is thrown.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
For information about creating reference lists, see the section “Dictionaries”.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Multiple Geobases
2017-12-28 15:13:23 +00:00
ClickHouse supports working with multiple alternative geobases (regional hierarchies) simultaneously, in order to support various perspectives on which countries certain regions belong to.
2020-03-20 10:10:48 +00:00
The ‘ clickhouse-server’ config specifies the file with the regional hierarchy::`< path_to_regions_hierarchy_file > /opt/geo/regions_hierarchy.txt< / path_to_regions_hierarchy_file > `
2017-12-28 15:13:23 +00:00
2020-10-13 17:23:29 +00:00
Besides this file, it also searches for files nearby that have the _ symbol and any suffix appended to the name (before the file extension).
2017-12-28 15:13:23 +00:00
For example, it will also find the file `/opt/geo/regions_hierarchy_ua.txt` , if present.
`ua` is called the dictionary key. For a dictionary without a suffix, the key is an empty string.
2020-10-13 17:23:29 +00:00
All the dictionaries are re-loaded in runtime (once every certain number of seconds, as defined in the builtin_dictionaries_reload_interval config parameter, or once an hour by default). However, the list of available dictionaries is defined one time, when the server starts.
2017-12-28 15:13:23 +00:00
All functions for working with regions have an optional argument at the end – the dictionary key. It is referred to as the geobase.
Example:
2020-03-20 10:10:48 +00:00
``` sql
2018-09-06 18:07:25 +00:00
regionToCountry(RegionID) – Uses the default dictionary: /opt/geo/regions_hierarchy.txt
regionToCountry(RegionID, '') – Uses the default dictionary: /opt/geo/regions_hierarchy.txt
regionToCountry(RegionID, 'ua') – Uses the dictionary for the 'ua' key: /opt/geo/regions_hierarchy_ua.txt
2017-12-28 15:13:23 +00:00
```
2022-06-02 10:55:18 +00:00
### regionToCity(id\[, geobase\])
2017-12-28 15:13:23 +00:00
2022-03-05 10:23:52 +00:00
Accepts a UInt32 number – the region ID from the geobase. If this region is a city or part of a city, it returns the region ID for the appropriate city. Otherwise, returns 0.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
### regionToArea(id\[, geobase\])
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Converts a region to an area (type 5 in the geobase). In every other way, this function is the same as ‘ regionToCity’ .
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-03-25 02:04:22 +00:00
SELECT DISTINCT regionToName(regionToArea(toUInt32(number), 'ua'))
FROM system.numbers
LIMIT 15
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─regionToName(regionToArea(toUInt32(number), \'ua\'))─┐
│ │
2018-09-06 18:07:25 +00:00
│ Moscow and Moscow region │
│ St. Petersburg and Leningrad region │
│ Belgorod region │
│ Ivanovsk region │
│ Kaluga region │
│ Kostroma region │
│ Kursk region │
│ Lipetsk region │
│ Orlov region │
│ Ryazan region │
│ Smolensk region │
│ Tambov region │
│ Tver region │
│ Tula region │
2017-12-28 15:13:23 +00:00
└──────────────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
### regionToDistrict(id\[, geobase\])
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Converts a region to a federal district (type 4 in the geobase). In every other way, this function is the same as ‘ regionToCity’ .
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-03-25 02:04:22 +00:00
SELECT DISTINCT regionToName(regionToDistrict(toUInt32(number), 'ua'))
FROM system.numbers
LIMIT 15
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─regionToName(regionToDistrict(toUInt32(number), \'ua\'))─┐
│ │
2018-09-06 18:07:25 +00:00
│ Central federal district │
│ Northwest federal district │
│ South federal district │
│ North Caucases federal district │
│ Privolga federal district │
│ Ural federal district │
│ Siberian federal district │
│ Far East federal district │
│ Scotland │
│ Faroe Islands │
│ Flemish region │
│ Brussels capital region │
│ Wallonia │
│ Federation of Bosnia and Herzegovina │
2017-12-28 15:13:23 +00:00
└──────────────────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
### regionToCountry(id\[, geobase\])
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Converts a region to a country. In every other way, this function is the same as ‘ regionToCity’ .
2017-12-28 15:13:23 +00:00
Example: `regionToCountry(toUInt32(213)) = 225` converts Moscow (213) to Russia (225).
2022-06-02 10:55:18 +00:00
### regionToContinent(id\[, geobase\])
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Converts a region to a continent. In every other way, this function is the same as ‘ regionToCity’ .
2017-12-28 15:13:23 +00:00
Example: `regionToContinent(toUInt32(213)) = 10001` converts Moscow (213) to Eurasia (10001).
2022-06-02 10:55:18 +00:00
### regionToTopContinent(id\[, geobase\])
2020-03-30 12:23:54 +00:00
Finds the highest continent in the hierarchy for the region.
**Syntax**
2020-04-30 18:19:18 +00:00
``` sql
2021-03-13 18:18:45 +00:00
regionToTopContinent(id[, geobase])
2020-03-30 12:23:54 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-03-30 12:23:54 +00:00
2022-03-05 10:23:52 +00:00
- `id` — Region ID from the geobase. [UInt32 ](../../sql-reference/data-types/int-uint.md ).
2020-04-30 18:19:18 +00:00
- `geobase` — Dictionary key. See [Multiple Geobases ](#multiple-geobases ). [String ](../../sql-reference/data-types/string.md ). Optional.
2020-03-30 12:23:54 +00:00
**Returned value**
2020-04-30 18:19:18 +00:00
- Identifier of the top level continent (the latter when you climb the hierarchy of regions).
- 0, if there is none.
2020-03-30 12:23:54 +00:00
Type: `UInt32` .
2022-06-02 10:55:18 +00:00
### regionToPopulation(id\[, geobase\])
2017-12-28 15:13:23 +00:00
Gets the population for a region.
2020-03-20 10:10:48 +00:00
The population can be recorded in files with the geobase. See the section “External dictionaries”.
2017-12-28 15:13:23 +00:00
If the population is not recorded for the region, it returns 0.
2022-03-05 10:23:52 +00:00
In the geobase, the population might be recorded for child regions, but not for parent regions.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
### regionIn(lhs, rhs\[, geobase\])
2017-12-28 15:13:23 +00:00
2021-05-27 19:44:11 +00:00
Checks whether a ‘ lhs’ region belongs to a ‘ rhs’ region. Returns a UInt8 number equal to 1 if it belongs, or 0 if it does not belong.
2017-12-28 15:13:23 +00:00
The relationship is reflexive – any region also belongs to itself.
2022-06-02 10:55:18 +00:00
### regionHierarchy(id\[, geobase\])
2017-12-28 15:13:23 +00:00
2022-03-05 10:23:52 +00:00
Accepts a UInt32 number – the region ID from the geobase. Returns an array of region IDs consisting of the passed region and all parents along the chain.
2017-12-28 15:13:23 +00:00
Example: `regionHierarchy(toUInt32(213)) = [213,1,3,225,10001,10000]` .
2022-06-02 10:55:18 +00:00
### regionToName(id\[, lang\])
2017-12-28 15:13:23 +00:00
2022-03-05 10:23:52 +00:00
Accepts a UInt32 number – the region ID from the geobase. A string with the name of the language can be passed as a second argument. Supported languages are: ru, en, ua, uk, by, kz, tr. If the second argument is omitted, the language ‘ ru’ is used. If the language is not supported, an exception is thrown. Returns a string – the name of the region in the corresponding language. If the region with the specified ID does not exist, an empty string is returned.
2017-12-28 15:13:23 +00:00
`ua` and `uk` both mean Ukrainian.