Skip to main content
Edit this page

ダイナミックDictionaryに対する操作の関数

Note

DDLクエリで作成されたDictionaryについては、dict_nameパラメータを<database>.<dict_name>の形で完全に指定する必要があります。そうでない場合、現在のデータベースが使用されます。

Dictionaryの接続および設定に関する情報は、Dictionaryを参照してください。

dictGet, dictGetOrDefault, dictGetOrNull

Dictionaryから値を取得します。

dictGet('dict_name', attr_names, id_expr)
dictGetOrDefault('dict_name', attr_names, id_expr, default_value_expr)
dictGetOrNull('dict_name', attr_name, id_expr)

引数

  • dict_name — Dictionaryの名前。文字列リテラル
  • attr_names — Dictionaryのカラム名。文字列リテラル、またはカラム名のタプル。タプル(文字列リテラル)。
  • id_expr — キーの値。で、Dictionary設定に応じたキータイプの値またはタプル型の値を返します。
  • default_value_expr — Dictionaryにid_exprキーの行が含まれていない場合に返される値。またはタプル()で、attr_names属性に設定された型で値を返します。

返り値

  • ClickHouseが属性を属性のデータ型で正常に解析すると、関数はid_exprに対応するDictionary属性の値を返します。

  • Dictionaryにid_exprに対応するキーがない場合:

    • dictGetは、Dictionary設定で属性に指定された<null_value>要素の内容を返します。
    • dictGetOrDefaultは、default_value_exprパラメータとして渡された値を返します。
    • dictGetOrNullは、キーがDictionaryで見つからなかった場合、NULLを返します。

ClickHouseは、属性の値を解析できない場合や、値が属性のデータ型と一致しない場合に例外を投げます。

シンプルキーDictionaryの例

以下の内容のテキストファイルext-dict-test.csvを作成します:

1,1
2,2

最初のカラムはid、2番目のカラムはc1です。

Dictionaryを設定します:

<clickhouse>
<dictionary>
<name>ext-dict-test</name>
<source>
<file>
<path>/path-to/ext-dict-test.csv</path>
<format>CSV</format>
</file>
</source>
<layout>
<flat />
</layout>
<structure>
<id>
<name>id</name>
</id>
<attribute>
<name>c1</name>
<type>UInt32</type>
<null_value></null_value>
</attribute>
</structure>
<lifetime>0</lifetime>
</dictionary>
</clickhouse>

クエリを実行します:

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

複雑なキーDictionaryの例

以下の内容のテキストファイルext-dict-mult.csvを作成します:

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

最初のカラムはid、2番目はc1、3番目はc2です。

Dictionaryを設定します:

<clickhouse>
<dictionary>
<name>ext-dict-mult</name>
<source>
<file>
<path>/path-to/ext-dict-mult.csv</path>
<format>CSV</format>
</file>
</source>
<layout>
<flat />
</layout>
<structure>
<id>
<name>id</name>
</id>
<attribute>
<name>c1</name>
<type>UInt32</type>
<null_value></null_value>
</attribute>
<attribute>
<name>c2</name>
<type>String</type>
<null_value></null_value>
</attribute>
</structure>
<lifetime>0</lifetime>
</dictionary>
</clickhouse>

クエリを実行します:

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

範囲キーDictionaryの例

入力テーブル:

CREATE TABLE range_key_dictionary_source_table
(
key UInt64,
start_date Date,
end_date Date,
value String,
value_nullable Nullable(String)
)
ENGINE = TinyLog();

INSERT INTO range_key_dictionary_source_table VALUES(1, toDate('2019-05-20'), toDate('2019-05-20'), 'First', 'First');
INSERT INTO range_key_dictionary_source_table VALUES(2, toDate('2019-05-20'), toDate('2019-05-20'), 'Second', NULL);
INSERT INTO range_key_dictionary_source_table VALUES(3, toDate('2019-05-20'), toDate('2019-05-20'), 'Third', 'Third');

Dictionaryを作成します:

CREATE DICTIONARY range_key_dictionary
(
key UInt64,
start_date Date,
end_date Date,
value String,
value_nullable Nullable(String)
)
PRIMARY KEY key
SOURCE(CLICKHOUSE(HOST 'localhost' PORT tcpPort() TABLE 'range_key_dictionary_source_table'))
LIFETIME(MIN 1 MAX 1000)
LAYOUT(RANGE_HASHED())
RANGE(MIN start_date MAX end_date);

クエリを実行します:

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

結果:

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

関連項目

dictHas

キーがDictionaryに存在するかどうかをチェックします。

dictHas('dict_name', id_expr)

引数

  • dict_name — Dictionaryの名前。文字列リテラル
  • id_expr — キーの値。で、Dictionary設定に応じたキータイプの値またはタプル型の値を返します。

返り値

  • キーが存在しない場合は0。UInt8
  • キーが存在する場合は1。UInt8

dictGetHierarchy

階層型 Dictionary内のキーのすべての親を含む配列を作成します。

構文

dictGetHierarchy('dict_name', key)

引数

返り値

dictIsIn

Dictionary内の階層全体を通して、キーの先祖をチェックします。

dictIsIn('dict_name', child_id_expr, ancestor_id_expr)

引数

  • dict_name — Dictionaryの名前。文字列リテラル
  • child_id_expr — チェックされるキー。UInt64型の値を返します。
  • ancestor_id_exprchild_id_exprキーの仮定された先祖。UInt64型の値を返します。

返り値

  • child_id_exprancestor_id_exprの子でない場合は0。UInt8
  • child_id_exprancestor_id_exprの子またはancestor_id_expr自体である場合は1。UInt8

dictGetChildren

第一レベルの子をインデックスの配列として返します。dictGetHierarchy の逆の変換です。

構文

dictGetChildren(dict_name, key)

引数

返り値

階層型のDictionaryを考慮します:

┌─id─┬─parent_id─┐
│ 1 │ 0 │
│ 2 │ 1 │
│ 3 │ 1 │
│ 4 │ 2 │
└────┴───────────┘

第一レベルの子:

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

dictGetDescendant

全ての子孫をdictGetChildren関数がlevel回の再帰で適用されたかのように返します。

構文

dictGetDescendants(dict_name, key, level)

引数

  • dict_name — Dictionaryの名前。文字列リテラル
  • key — キーの値。UInt64型の値を返します。
  • level — 階層レベル。level = 0の場合、最後までの全ての子孫を返します。UInt8

返り値

階層型のDictionaryを考慮します:

┌─id─┬─parent_id─┐
│ 1 │ 0 │
│ 2 │ 1 │
│ 3 │ 1 │
│ 4 │ 2 │
└────┴───────────┘

全ての子孫:

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

第一レベルの子孫:

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

dictGetAll

正規表現ツリーDictionaryで各キーに一致したすべてのノードの属性値を取得します。

Array(T)型の値を返す点を除き、dictGetと同様に動作します。

構文

dictGetAll('dict_name', attr_names, id_expr[, limit])

引数

  • dict_name — Dictionaryの名前。文字列リテラル
  • attr_names — Dictionaryのカラム名。文字列リテラルまたは、カラム名のタプル。タプル(文字列リテラル)。
  • id_expr — キーの値。で、Dictionary設定に応じたキータイプの値の配列またはタプル型の値を返します。
  • limit - 返される各値配列の最大長。切り詰める場合、子ノードが親ノードより優先され、それ以外の場合は正規表現ツリーDictionaryで定義されたリストの順序が尊重されます。指定しない場合、配列の長さに制限はありません。

返り値

  • ClickHouseが属性を正常に解析し、Dictionaryで定義された属性のデータ型として、各attr_namesで指定された属性に対するid_exprに対応するDictionary属性値の配列を返します。

  • Dictionaryにid_exprに対応するキーがない場合、空の配列が返されます。

ClickHouseは、属性の値を解析できない場合や、値が属性のデータ型と一致しない場合に例外をスローします。

以下のような正規表現ツリーDictionaryを考慮します:

CREATE DICTIONARY regexp_dict
(
regexp String,
tag String
)
PRIMARY KEY(regexp)
SOURCE(YAMLRegExpTree(PATH '/var/lib/clickhouse/user_files/regexp_tree.yaml'))
LAYOUT(regexp_tree)
...
# /var/lib/clickhouse/user_files/regexp_tree.yaml
- regexp: 'foo'
tag: 'foo_attr'
- regexp: 'bar'
tag: 'bar_attr'
- regexp: 'baz'
tag: 'baz_attr'

すべての一致する値を取得:

SELECT dictGetAll('regexp_dict', 'tag', 'foobarbaz');
┌─dictGetAll('regexp_dict', 'tag', 'foobarbaz')─┐
│ ['foo_attr','bar_attr','baz_attr'] │
└───────────────────────────────────────────────┘

最大2つの一致する値を取得:

SELECT dictGetAll('regexp_dict', 'tag', 'foobarbaz', 2);
┌─dictGetAll('regexp_dict', 'tag', 'foobarbaz', 2)─┐
│ ['foo_attr','bar_attr'] │
└──────────────────────────────────────────────────┘

その他の関数

ClickHouseは、Dictionaryの設定に関係なく、Dictionaryの属性値を特定のデータ型に変換する専門の関数をサポートしています。

関数:

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

これらの関数はすべてOrDefaultの修飾が可能です。例えば、dictGetDateOrDefaultのように。

構文:

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

引数

  • dict_name — Dictionaryの名前。文字列リテラル
  • attr_name — Dictionaryのカラム名。文字列リテラル
  • id_expr — キーの値。で、UInt64またはタプル型の値を返します。
  • default_value_expr — Dictionaryにid_exprキーの行が含まれていない場合に返される値。で、attr_nameに設定されたデータ型で値を返します。

返り値

  • ClickHouseが属性を属性のデータ型で正常に解析すると、関数はid_exprに対応するDictionary属性の値を返します。

  • Dictionaryにリクエストされたid_exprがない場合:

    • dictGet[Type]は、Dictionary設定で属性に指定された<null_value>要素の内容を返します。
    • dictGet[Type]OrDefaultは、default_value_exprパラメータとして渡された値を返します。

ClickHouseは、属性の値を解析できない場合や、値が属性のデータ型と一致しない場合に例外をスローします。