Attention

For dictionaries, created with DDL queries, the dict_name parameter must be fully specified, like <database>.<dict_name>. Otherwise, the current database is used.

Functions for Working with External Dictionaries

For information on connecting and configuring external dictionaries, see External dictionaries.

dictGet, dictGetOrDefault, dictGetOrNull

Retrieves values from an external dictionary.

  1. dictGet('dict_name', attr_names, id_expr)
  2. dictGetOrDefault('dict_name', attr_names, id_expr, default_value_expr)
  3. dictGetOrNull('dict_name', attr_name, id_expr)

Arguments

  • dict_name — Name of the dictionary. String literal.
  • attr_names — Name of the column of the dictionary, String literal, or tuple of column names, Tuple(String literal).
  • id_expr — Key value. Expression returning dictionary key-type value or Tuple-type value depending on the dictionary configuration.
  • default_value_expr — Values returned if the dictionary does not contain a row with the id_expr key. Expression or Tuple(Expression), returning the value (or values) in the data types configured for the attr_names attribute.

Returned value

  • If ClickHouse parses the attribute successfully in the attribute’s data type, functions return the value of the dictionary attribute that corresponds to id_expr.

  • If there is no the key, corresponding to id_expr, in the dictionary, then:

    1. - `dictGet` returns the content of the `<null_value>` element specified for the attribute in the dictionary configuration.
    2. - `dictGetOrDefault` returns the value passed as the `default_value_expr` parameter.
    3. - `dictGetOrNull` returns `NULL` in case key was not found in dictionary.

ClickHouse throws an exception if it cannot parse the value of the attribute or the value does not match the attribute data type.

Example for simple key dictionary

Create a text file ext-dict-test.csv containing the following:

  1. 1,1
  2. 2,2

The first column is id, the second column is c1.

Configure the external dictionary:

  1. <yandex>
  2. <dictionary>
  3. <name>ext-dict-test</name>
  4. <source>
  5. <file>
  6. <path>/path-to/ext-dict-test.csv</path>
  7. <format>CSV</format>
  8. </file>
  9. </source>
  10. <layout>
  11. <flat />
  12. </layout>
  13. <structure>
  14. <id>
  15. <name>id</name>
  16. </id>
  17. <attribute>
  18. <name>c1</name>
  19. <type>UInt32</type>
  20. <null_value></null_value>
  21. </attribute>
  22. </structure>
  23. <lifetime>0</lifetime>
  24. </dictionary>
  25. </yandex>

Perform the query:

  1. SELECT
  2. dictGetOrDefault('ext-dict-test', 'c1', number + 1, toUInt32(number * 10)) AS val,
  3. toTypeName(val) AS type
  4. FROM system.numbers
  5. LIMIT 3;
  1. ┌─val─┬─type───┐
  2. 1 UInt32
  3. 2 UInt32
  4. 20 UInt32
  5. └─────┴────────┘

Example for complex key dictionary

Create a text file ext-dict-mult.csv containing the following:

  1. 1,1,'1'
  2. 2,2,'2'
  3. 3,3,'3'

The first column is id, the second is c1, the third is c2.

Configure the external dictionary:

  1. <yandex>
  2. <dictionary>
  3. <name>ext-dict-mult</name>
  4. <source>
  5. <file>
  6. <path>/path-to/ext-dict-mult.csv</path>
  7. <format>CSV</format>
  8. </file>
  9. </source>
  10. <layout>
  11. <flat />
  12. </layout>
  13. <structure>
  14. <id>
  15. <name>id</name>
  16. </id>
  17. <attribute>
  18. <name>c1</name>
  19. <type>UInt32</type>
  20. <null_value></null_value>
  21. </attribute>
  22. <attribute>
  23. <name>c2</name>
  24. <type>String</type>
  25. <null_value></null_value>
  26. </attribute>
  27. </structure>
  28. <lifetime>0</lifetime>
  29. </dictionary>
  30. </yandex>

Perform the query:

  1. SELECT
  2. dictGet('ext-dict-mult', ('c1','c2'), number) AS val,
  3. toTypeName(val) AS type
  4. FROM system.numbers
  5. LIMIT 3;
  1. ┌─val─────┬─type──────────────────┐
  2. (1,'1') Tuple(UInt8, String)
  3. (2,'2') Tuple(UInt8, String)
  4. (3,'3') Tuple(UInt8, String)
  5. └─────────┴───────────────────────┘

Example for range key dictionary

Input table:

  1. CREATE TABLE range_key_dictionary_source_table
  2. (
  3. key UInt64,
  4. start_date Date,
  5. end_date Date,
  6. value String,
  7. value_nullable Nullable(String)
  8. )
  9. ENGINE = TinyLog();
  10. INSERT INTO range_key_dictionary_source_table VALUES(1, toDate('2019-05-20'), toDate('2019-05-20'), 'First', 'First');
  11. INSERT INTO range_key_dictionary_source_table VALUES(2, toDate('2019-05-20'), toDate('2019-05-20'), 'Second', NULL);
  12. INSERT INTO range_key_dictionary_source_table VALUES(3, toDate('2019-05-20'), toDate('2019-05-20'), 'Third', 'Third');

Create the external dictionary:

  1. CREATE DICTIONARY range_key_dictionary
  2. (
  3. key UInt64,
  4. start_date Date,
  5. end_date Date,
  6. value String,
  7. value_nullable Nullable(String)
  8. )
  9. PRIMARY KEY key
  10. SOURCE(CLICKHOUSE(HOST 'localhost' PORT tcpPort() TABLE 'range_key_dictionary_source_table'))
  11. LIFETIME(MIN 1 MAX 1000)
  12. LAYOUT(RANGE_HASHED())
  13. RANGE(MIN start_date MAX end_date);

Perform the query:

  1. SELECT
  2. (number, toDate('2019-05-20')),
  3. dictHas('range_key_dictionary', number, toDate('2019-05-20')),
  4. dictGetOrNull('range_key_dictionary', 'value', number, toDate('2019-05-20')),
  5. dictGetOrNull('range_key_dictionary', 'value_nullable', number, toDate('2019-05-20')),
  6. dictGetOrNull('range_key_dictionary', ('value', 'value_nullable'), number, toDate('2019-05-20'))
  7. FROM system.numbers LIMIT 5 FORMAT TabSeparated;

Result:

  1. (0,'2019-05-20') 0 \N \N (NULL,NULL)
  2. (1,'2019-05-20') 1 First First ('First','First')
  3. (2,'2019-05-20') 0 \N \N (NULL,NULL)
  4. (3,'2019-05-20') 0 \N \N (NULL,NULL)
  5. (4,'2019-05-20') 0 \N \N (NULL,NULL)

See Also

dictHas

Checks whether a key is present in a dictionary.

  1. dictHas('dict_name', id_expr)

Arguments

  • dict_name — Name of the dictionary. String literal.
  • id_expr — Key value. Expression returning dictionary key-type value or Tuple-type value depending on the dictionary configuration.

Returned value

  • 0, if there is no key.
  • 1, if there is a key.

Type: UInt8.

dictGetHierarchy

Creates an array, containing all the parents of a key in the hierarchical dictionary.

Syntax

  1. dictGetHierarchy('dict_name', key)

Arguments

Returned value

  • Parents for the key.

Type: Array(UInt64).

dictIsIn

Checks the ancestor of a key through the whole hierarchical chain in the dictionary.

  1. dictIsIn('dict_name', child_id_expr, ancestor_id_expr)

Arguments

  • dict_name — Name of the dictionary. String literal.
  • child_id_expr — Key to be checked. Expression returning a UInt64-type value.
  • ancestor_id_expr — Alleged ancestor of the child_id_expr key. Expression returning a UInt64-type value.

Returned value

  • 0, if child_id_expr is not a child of ancestor_id_expr.
  • 1, if child_id_expr is a child of ancestor_id_expr or if child_id_expr is an ancestor_id_expr.

Type: UInt8.

dictGetChildren

Returns first-level children as an array of indexes. It is the inverse transformation for dictGetHierarchy.

Syntax

  1. dictGetChildren(dict_name, key)

Arguments

Returned values

  • First-level descendants for the key.

Type: Array(UInt64).

Example

Consider the hierarchic dictionary:

  1. ┌─id─┬─parent_id─┐
  2. 1 0
  3. 2 1
  4. 3 1
  5. 4 2
  6. └────┴───────────┘

First-level children:

  1. SELECT dictGetChildren('hierarchy_flat_dictionary', number) FROM system.numbers LIMIT 4;
  1. ┌─dictGetChildren('hierarchy_flat_dictionary', number)─┐
  2. [1]
  3. [2,3]
  4. [4]
  5. []
  6. └──────────────────────────────────────────────────────┘

dictGetDescendant

Returns all descendants as if dictGetChildren function was applied level times recursively.

Syntax

  1. dictGetDescendants(dict_name, key, level)

Arguments

  • dict_name — Name of the dictionary. String literal.
  • key — Key value. Expression returning a UInt64-type value.
  • level — Hierarchy level. If level = 0 returns all descendants to the end. UInt8.

Returned values

  • Descendants for the key.

Type: Array(UInt64).

Example

Consider the hierarchic dictionary:

  1. ┌─id─┬─parent_id─┐
  2. 1 0
  3. 2 1
  4. 3 1
  5. 4 2
  6. └────┴───────────┘

All descendants:

  1. SELECT dictGetDescendants('hierarchy_flat_dictionary', number) FROM system.numbers LIMIT 4;
  1. ┌─dictGetDescendants('hierarchy_flat_dictionary', number)─┐
  2. [1,2,3,4]
  3. [2,3,4]
  4. [4]
  5. []
  6. └─────────────────────────────────────────────────────────┘

First-level descendants:

  1. SELECT dictGetDescendants('hierarchy_flat_dictionary', number, 1) FROM system.numbers LIMIT 4;
  1. ┌─dictGetDescendants('hierarchy_flat_dictionary', number, 1)─┐
  2. [1]
  3. [2,3]
  4. [4]
  5. []
  6. └────────────────────────────────────────────────────────────┘

Other Functions

ClickHouse supports specialized functions that convert dictionary attribute values to a specific data type regardless of the dictionary configuration.

Functions:

  • dictGetInt8, dictGetInt16, dictGetInt32, dictGetInt64
  • dictGetUInt8, dictGetUInt16, dictGetUInt32, dictGetUInt64
  • dictGetFloat32, dictGetFloat64
  • dictGetDate
  • dictGetDateTime
  • dictGetUUID
  • dictGetString

All these functions have the OrDefault modification. For example, dictGetDateOrDefault.

Syntax:

  1. dictGet[Type]('dict_name', 'attr_name', id_expr)
  2. dictGet[Type]OrDefault('dict_name', 'attr_name', id_expr, default_value_expr)

Arguments

  • dict_name — Name of the dictionary. String literal.
  • attr_name — Name of the column of the dictionary. String literal.
  • id_expr — Key value. Expression returning a UInt64 or Tuple-type value depending on the dictionary configuration.
  • default_value_expr — Value returned if the dictionary does not contain a row with the id_expr key. Expression returning the value in the data type configured for the attr_name attribute.

Returned value

  • If ClickHouse parses the attribute successfully in the attribute’s data type, functions return the value of the dictionary attribute that corresponds to id_expr.

  • If there is no requested id_expr in the dictionary then:

    1. - `dictGet[Type]` returns the content of the `<null_value>` element specified for the attribute in the dictionary configuration.
    2. - `dictGet[Type]OrDefault` returns the value passed as the `default_value_expr` parameter.

ClickHouse throws an exception if it cannot parse the value of the attribute or the value does not match the attribute data type.