ClickHouse/docs/en/sql-reference/functions/ym-dict-functions.md

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

509 lines
23 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/functions/ym-dict-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 60
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
:::note
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 cant be loaded, an exception is thrown.
2024-04-08 15:11:18 +00:00
2024-04-08 17:09:26 +00:00
As such, the examples shown in this section will throw an exception in [ClickHouse Fiddle](https://fiddle.clickhouse.com/) and in quick release and production deployments by default, unless first configured.
:::
2024-04-08 19:51:18 +00:00
For information about creating reference lists, see the section [“Dictionaries”](../dictionaries#embedded-dictionaries).
2022-06-02 10:55:18 +00:00
## Multiple Geobases
ClickHouse supports working with multiple alternative geobases (regional hierarchies) simultaneously, in order to support various perspectives on which countries certain regions belong to.
2024-04-08 17:09:26 +00:00
The clickhouse-server config specifies the file with the regional hierarchy:
2024-04-08 17:09:26 +00:00
```<path_to_regions_hierarchy_file>/opt/geo/regions_hierarchy.txt</path_to_regions_hierarchy_file>```
2024-04-08 17:09:26 +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).
For example, it will also find the file `/opt/geo/regions_hierarchy_ua.txt`, if present. Here `ua` is called the dictionary key. For a dictionary without a suffix, the key is an empty string.
2024-04-08 17:09:26 +00:00
All the dictionaries are re-loaded during runtime (once every certain number of seconds, as defined in the [`builtin_dictionaries_reload_interval`](../../operations/server-configuration-parameters/settings#builtin-dictionaries-reload-interval) config parameter, or once an hour by default). However, the list of available dictionaries is defined once, when the server starts.
All functions for working with regions have an optional argument at the end the dictionary key. It is referred to as the geobase.
2024-04-08 15:11:18 +00:00
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
```
2024-04-08 17:09:26 +00:00
### regionToName
Accepts a region ID and geobase and returns a string of the name of the region in the corresponding language. If the region with the specified ID does not exist, an empty string is returned.
**Syntax**
``` sql
regionToName(id\[, lang\])
```
**Parameters**
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
**Returned value**
- Name of the region in the corresponding language specified by `geobase`. [String](../data-types/string).
- Otherwise, an empty string.
**Example**
Query:
``` sql
SELECT regionToName(number::UInt32,'en') FROM numbers(0,5);
```
Result:
``` text
┌─regionToName(CAST(number, 'UInt32'), 'en')─┐
│ │
│ World │
│ USA │
│ Colorado │
│ Boulder County │
└────────────────────────────────────────────┘
```
### regionToCity
2024-04-03 09:07:55 +00:00
Accepts a 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.
**Syntax**
```sql
regionToCity(id [, geobase])
```
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
**Returned value**
2024-04-08 17:09:26 +00:00
- Region ID for the appropriate city, if it exists. [UInt32](../data-types/int-uint).
2024-04-08 15:11:18 +00:00
- 0, if there is none.
**Example**
Query:
```sql
2024-04-08 15:11:18 +00:00
SELECT regionToName(number::UInt32, 'en'), regionToCity(number::UInt32) AS id, regionToName(id, 'en') FROM numbers(13);
```
Result:
```response
2024-04-08 15:11:18 +00:00
┌─regionToName(CAST(number, 'UInt32'), 'en')─┬─id─┬─regionToName(regionToCity(CAST(number, 'UInt32')), 'en')─┐
│ │ 0 │ │
│ World │ 0 │ │
│ USA │ 0 │ │
│ Colorado │ 0 │ │
│ Boulder County │ 0 │ │
│ Boulder │ 5 │ Boulder │
│ China │ 0 │ │
│ Sichuan │ 0 │ │
│ Chengdu │ 8 │ Chengdu │
│ America │ 0 │ │
│ North America │ 0 │ │
│ Eurasia │ 0 │ │
│ Asia │ 0 │ │
└────────────────────────────────────────────┴────┴──────────────────────────────────────────────────────────┘
2024-04-03 09:07:55 +00:00
```
### regionToArea
2024-04-03 09:07:55 +00:00
Converts a region to an area (type 5 in the geobase). In every other way, this function is the same as [regionToCity](#regiontocity).
**Syntax**
```sql
regionToArea(id [, geobase])
```
2024-04-03 09:07:55 +00:00
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
2024-04-03 09:07:55 +00:00
**Returned value**
2024-04-08 17:09:26 +00:00
- Region ID for the appropriate area, if it exists. [UInt32](../data-types/int-uint).
2024-04-08 15:11:18 +00:00
- 0, if there is none.
**Example**
Query:
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
```
2024-04-08 15:11:18 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
┌─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 │
└──────────────────────────────────────────────────────┘
```
2024-04-03 09:07:55 +00:00
### regionToDistrict
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.
2024-04-03 09:07:55 +00:00
**Syntax**
```sql
regionToDistrict(id [, geobase])
```
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
2024-04-03 09:07:55 +00:00
**Returned value**
2024-04-08 17:09:26 +00:00
- Region ID for the appropriate city, if it exists. [UInt32](../data-types/int-uint).
2024-04-08 15:11:18 +00:00
- 0, if there is none.
2024-04-03 09:07:55 +00:00
**Example**
Query:
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
```
2024-04-03 09:07:55 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
┌─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 │
└──────────────────────────────────────────────────────────┘
```
2024-04-03 09:07:55 +00:00
### regionToCountry
2024-04-08 17:09:26 +00:00
Converts a region to a country (type 3 in the geobase). In every other way, this function is the same as regionToCity.
2024-04-03 09:07:55 +00:00
**Syntax**
```sql
regionToCountry(id [, geobase])
```
2024-04-08 15:11:18 +00:00
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
2024-04-08 15:11:18 +00:00
**Returned value**
2024-04-08 17:09:26 +00:00
- Region ID for the appropriate country, if it exists. [UInt32](../data-types/int-uint).
2024-04-08 15:11:18 +00:00
- 0, if there is none.
**Example**
Query:
``` sql
SELECT regionToName(number::UInt32, 'en'), regionToCountry(number::UInt32) AS id, regionToName(id, 'en') FROM numbers(13);
```
Result:
``` text
┌─regionToName(CAST(number, 'UInt32'), 'en')─┬─id─┬─regionToName(regionToCountry(CAST(number, 'UInt32')), 'en')─┐
│ │ 0 │ │
│ World │ 0 │ │
│ USA │ 2 │ USA │
│ Colorado │ 2 │ USA │
│ Boulder County │ 2 │ USA │
│ Boulder │ 2 │ USA │
│ China │ 6 │ China │
│ Sichuan │ 6 │ China │
│ Chengdu │ 6 │ China │
│ America │ 0 │ │
│ North America │ 0 │ │
│ Eurasia │ 0 │ │
│ Asia │ 0 │ │
└────────────────────────────────────────────┴────┴─────────────────────────────────────────────────────────────┘
```
2024-04-03 09:07:55 +00:00
### regionToContinent
2024-04-08 17:09:26 +00:00
Converts a region to a continent (type 1 in the geobase). In every other way, this function is the same as regionToCity.
2024-04-03 09:07:55 +00:00
**Syntax**
```sql
regionToContinent(id [, geobase])
```
2024-04-08 15:11:18 +00:00
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
2024-04-08 15:11:18 +00:00
**Returned value**
2024-04-08 17:09:26 +00:00
- Region ID for the appropriate continent, if it exists. [UInt32](../data-types/int-uint).
2024-04-08 15:11:18 +00:00
- 0, if there is none.
**Example**
Query:
``` sql
SELECT regionToName(number::UInt32, 'en'), regionToContinent(number::UInt32) AS id, regionToName(id, 'en') FROM numbers(13);
2024-04-03 09:07:55 +00:00
```
2024-04-08 15:11:18 +00:00
Result:
``` text
┌─regionToName(CAST(number, 'UInt32'), 'en')─┬─id─┬─regionToName(regionToContinent(CAST(number, 'UInt32')), 'en')─┐
│ │ 0 │ │
│ World │ 0 │ │
│ USA │ 10 │ North America │
│ Colorado │ 10 │ North America │
│ Boulder County │ 10 │ North America │
│ Boulder │ 10 │ North America │
│ China │ 12 │ Asia │
│ Sichuan │ 12 │ Asia │
│ Chengdu │ 12 │ Asia │
│ America │ 9 │ America │
│ North America │ 10 │ North America │
│ Eurasia │ 11 │ Eurasia │
│ Asia │ 12 │ Asia │
└────────────────────────────────────────────┴────┴───────────────────────────────────────────────────────────────┘
```
### regionToTopContinent
Finds the highest continent in the hierarchy for the region.
**Syntax**
``` sql
regionToTopContinent(id[, geobase])
```
2024-04-03 09:07:55 +00:00
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
**Returned value**
2024-04-08 17:09:26 +00:00
- Identifier of the top level continent (the latter when you climb the hierarchy of regions).[UInt32](../data-types/int-uint).
- 0, if there is none.
2024-04-08 15:11:18 +00:00
**Example**
Query:
``` sql
SELECT regionToName(number::UInt32, 'en'), regionToTopContinent(number::UInt32) AS id, regionToName(id, 'en') FROM numbers(13);
```
Result:
``` text
┌─regionToName(CAST(number, 'UInt32'), 'en')─┬─id─┬─regionToName(regionToTopContinent(CAST(number, 'UInt32')), 'en')─┐
│ │ 0 │ │
│ World │ 0 │ │
│ USA │ 9 │ America │
│ Colorado │ 9 │ America │
│ Boulder County │ 9 │ America │
│ Boulder │ 9 │ America │
│ China │ 11 │ Eurasia │
│ Sichuan │ 11 │ Eurasia │
│ Chengdu │ 11 │ Eurasia │
│ America │ 9 │ America │
│ North America │ 9 │ America │
│ Eurasia │ 11 │ Eurasia │
│ Asia │ 11 │ Eurasia │
└────────────────────────────────────────────┴────┴──────────────────────────────────────────────────────────────────┘
```
2024-04-03 09:07:55 +00:00
### regionToPopulation
2024-04-08 19:51:18 +00:00
Gets the population for a region. The population can be recorded in files with the geobase. See the section [“Dictionaries”](../dictionaries#embedded-dictionaries). If the population is not recorded for the region, it returns 0. In the geobase, the population might be recorded for child regions, but not for parent regions.
2024-04-03 09:07:55 +00:00
**Syntax**
``` sql
regionToPopulation(id[, geobase])
```
2024-04-08 15:11:18 +00:00
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
2024-04-08 15:11:18 +00:00
**Returned value**
2024-04-08 17:09:26 +00:00
- Population for the region. [UInt32](../data-types/int-uint).
2024-04-08 15:11:18 +00:00
- 0, if there is none.
**Example**
Query:
``` sql
SELECT regionToName(number::UInt32, 'en'), regionToPopulation(number::UInt32) AS id, regionToName(id, 'en') FROM numbers(13);
```
Result:
``` text
┌─regionToName(CAST(number, 'UInt32'), 'en')─┬─population─┐
│ │ 0 │
│ World │ 4294967295 │
│ USA │ 330000000 │
│ Colorado │ 5700000 │
│ Boulder County │ 330000 │
│ Boulder │ 100000 │
│ China │ 1500000000 │
│ Sichuan │ 83000000 │
│ Chengdu │ 20000000 │
│ America │ 1000000000 │
│ North America │ 600000000 │
│ Eurasia │ 4294967295 │
│ Asia │ 4294967295 │
└────────────────────────────────────────────┴────────────┘
```
2024-04-03 09:07:55 +00:00
### regionIn
2024-04-08 17:09:26 +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.
2024-04-03 09:07:55 +00:00
**Syntax**
``` sql
regionIn(lhs, rhs\[, geobase\])
```
2024-04-08 15:11:18 +00:00
**Parameters**
2024-04-08 17:09:26 +00:00
- `lhs` — Lhs region ID from the geobase. [UInt32](../../sql-reference/data-types/int-uint).
- `rhs` — Rhs region ID from the geobase. [UInt32](../../sql-reference/data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
2024-04-08 15:11:18 +00:00
**Returned value**
2024-04-08 17:09:26 +00:00
- 1, if it belongs. [UInt8](../../sql-reference/data-types/int-uint).
2024-04-08 15:11:18 +00:00
- 0, if it doesn't belong.
**Implementation details**
The relationship is reflexive any region also belongs to itself.
**Example**
Query:
``` sql
SELECT regionToName(n1.number::UInt32, 'en') || (regionIn(n1.number::UInt32, n2.number::UInt32) ? ' is in ' : ' is not in ') || regionToName(n2.number::UInt32, 'en') FROM numbers(1,2) AS n1 CROSS JOIN numbers(1,5) AS n2;
```
Result:
``` text
World is in World
World is not in USA
World is not in Colorado
World is not in Boulder County
World is not in Boulder
USA is in World
USA is in USA
USA is not in Colorado
USA is not in Boulder County
USA is not in Boulder
```
2024-04-03 09:07:55 +00:00
### regionHierarchy
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.
2024-04-03 09:07:55 +00:00
**Syntax**
``` sql
regionHierarchy(id\[, geobase\])
```
2024-04-08 15:11:18 +00:00
**Parameters**
2024-04-08 17:09:26 +00:00
- `id` — Region ID from the geobase. [UInt32](../data-types/int-uint).
- `geobase` — Dictionary key. See [Multiple Geobases](#multiple-geobases). [String](../data-types/string). Optional.
2024-04-08 15:11:18 +00:00
**Returned value**
2024-04-08 18:16:09 +00:00
- Array of region IDs consisting of the passed region and all parents along the chain. [Array](../data-types/array)([UInt32](../data-types/int-uint)).
2024-04-08 15:11:18 +00:00
**Example**
Query:
2024-04-08 15:11:18 +00:00
``` sql
SELECT regionHierarchy(number::UInt32) AS arr, arrayMap(id -> regionToName(id, 'en'), arr) FROM numbers(5);
```
Result:
``` text
┌─arr────────────┬─arrayMap(lambda(tuple(id), regionToName(id, 'en')), regionHierarchy(CAST(number, 'UInt32')))─┐
│ [] │ [] │
│ [1] │ ['World'] │
│ [2,10,9,1] │ ['USA','North America','America','World'] │
│ [3,2,10,9,1] │ ['Colorado','USA','North America','America','World'] │
│ [4,3,2,10,9,1] │ ['Boulder County','Colorado','USA','North America','America','World'] │
└────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────┘
2024-04-08 18:16:09 +00:00
```