Skip to main content
Skip to main content
Edit this page

Functions for Working with Embedded Dictionaries

Note

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.

As such, the examples shown in this section will throw an exception in ClickHouse Fiddle and in quick release and production deployments by default, unless first configured.

For information about creating reference lists, see the section "Dictionaries".

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.

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>

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.

All the dictionaries are re-loaded during 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 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.

Example:

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

Parameters

Returned value

  • Name of the region in the corresponding language specified by geobase. String.
  • Otherwise, an empty string.

Example

Query:

Result:

regionToCity

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

Parameters

Returned value

  • Region ID for the appropriate city, if it exists. UInt32.
  • 0, if there is none.

Example

Query:

Result:

regionToArea

Converts a region to an area (type 5 in the geobase). In every other way, this function is the same as 'regionToCity'.

Syntax

Parameters

Returned value

  • Region ID for the appropriate area, if it exists. UInt32.
  • 0, if there is none.

Example

Query:

Result:

regionToDistrict

Converts a region to a federal district (type 4 in the geobase). In every other way, this function is the same as 'regionToCity'.

Syntax

Parameters

Returned value

  • Region ID for the appropriate city, if it exists. UInt32.
  • 0, if there is none.

Example

Query:

Result:

regionToCountry

Converts a region to a country (type 3 in the geobase). In every other way, this function is the same as 'regionToCity'.

Syntax

Parameters

Returned value

  • Region ID for the appropriate country, if it exists. UInt32.
  • 0, if there is none.

Example

Query:

Result:

regionToContinent

Converts a region to a continent (type 1 in the geobase). In every other way, this function is the same as 'regionToCity'.

Syntax

Parameters

Returned value

  • Region ID for the appropriate continent, if it exists. UInt32.
  • 0, if there is none.

Example

Query:

Result:

regionToTopContinent

Finds the highest continent in the hierarchy for the region.

Syntax

Parameters

Returned value

  • Identifier of the top level continent (the latter when you climb the hierarchy of regions).UInt32.
  • 0, if there is none.

Example

Query:

Result:

regionToPopulation

Gets the population for a region. The population can be recorded in files with the geobase. See the section "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.

Syntax

Parameters

Returned value

  • Population for the region. UInt32.
  • 0, if there is none.

Example

Query:

Result:

regionIn

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.

Syntax

Parameters

Returned value

  • 1, if it belongs. UInt8.
  • 0, if it doesn't belong.

Implementation details

The relationship is reflexive – any region also belongs to itself.

Example

Query:

Result:

regionHierarchy

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.

Syntax

Parameters

Returned value

  • Array of region IDs consisting of the passed region and all parents along the chain. Array(UInt32).

Example

Query:

Result: