2017-12-28 15:13:23 +00:00
2018-08-07 16:49:40 +00:00
# Dictionary Key and Fields
2017-12-28 15:13:23 +00:00
The `<structure>` clause describes the dictionary key and fields available for queries.
Overall structure:
```xml
< dictionary >
< structure >
< id >
< name > Id< / name >
< / id >
< attribute >
<!-- Attribute parameters -->
< / attribute >
...
< / structure >
< / dictionary >
```
Columns are described in the structure:
2018-12-12 17:28:00 +00:00
- `<id>` - [key column ](external_dicts_dict_structure.md ).
- `<attribute>` - [data column ](external_dicts_dict_structure.md ). There can be a large number of columns.
2017-12-28 15:13:23 +00:00
## Key
ClickHouse supports the following types of keys:
- Numeric key. UInt64. Defined in the tag `<id>` .
- Composite key. Set of values of different types. Defined in the tag `<key>` .
A structure can contain either `<id>` or `<key>` .
2018-09-06 18:07:25 +00:00
!!! warning
2018-07-20 17:35:34 +00:00
The key doesn't need to be defined separately in attributes.
2017-12-28 15:13:23 +00:00
2018-08-07 16:49:40 +00:00
### Numeric Key
2017-12-28 15:13:23 +00:00
Format: `UInt64` .
Configuration example:
```xml
< id >
< name > Id< / name >
< / id >
```
Configuration fields:
- name – The name of the column with keys.
2018-08-07 16:49:40 +00:00
### Composite Key
2017-12-28 15:13:23 +00:00
2018-12-12 17:28:00 +00:00
The key can be a `tuple` from any types of fields. The [layout ](external_dicts_dict_layout.md ) in this case must be `complex_key_hashed` or `complex_key_cache` .
2017-12-28 15:13:23 +00:00
2018-07-20 17:35:34 +00:00
!!! tip
A composite key can consist of a single element. This makes it possible to use a string as the key, for instance.
2017-12-28 15:13:23 +00:00
2018-12-12 17:28:00 +00:00
The key structure is set in the element `<key>` . Key fields are specified in the same format as the dictionary [attributes ](external_dicts_dict_structure.md ). Example:
2017-12-28 15:13:23 +00:00
```xml
< structure >
< key >
< attribute >
< name > field1< / name >
< type > String< / type >
< / attribute >
< attribute >
< name > field2< / name >
< type > UInt32< / type >
< / attribute >
...
< / key >
...
```
For a query to the `dictGet*` function, a tuple is passed as the key. Example: `dictGetString('dict_name', 'attr_name', tuple('string for field1', num_for_field2))` .
## Attributes
Configuration example:
```xml
< structure >
...
< attribute >
< name > Name< / name >
< type > Type< / type >
< null_value > < / null_value >
< expression > rand64()< / expression >
< hierarchical > true< / hierarchical >
< injective > true< / injective >
2018-02-06 21:34:56 +00:00
< is_object_id > true< / is_object_id >
2017-12-28 15:13:23 +00:00
< / attribute >
< / structure >
```
Configuration fields:
- `name` – The column name.
- `type` – The column type. Sets the method for interpreting data in the source. For example, for MySQL, the field might be `TEXT` , `VARCHAR` , or `BLOB` in the source table, but it can be uploaded as `String` .
- `null_value` – The default value for a non-existing element. In the example, it is an empty string.
- `expression` – The attribute can be an expression. The tag is not required.
2018-09-06 18:07:25 +00:00
- `hierarchical` – Hierarchical support. Mirrored to the parent identifier. By default, ` false` .
- `injective` – Whether the `id -> attribute` image is injective. If ` true` , then you can optimize the ` GROUP BY` clause. By default, `false` .
2018-03-25 02:04:22 +00:00
- `is_object_id` – Whether the query is executed for a MongoDB document by `ObjectID` .
2018-10-16 10:47:17 +00:00
[Original article ](https://clickhouse.yandex/docs/en/query_language/dicts/external_dicts_dict_structure/ ) <!--hide-->