注記
DDLクエリで作成されたディクショナリの場合、dict_name
パラメータは完全に指定されている必要があります。たとえば、<database>.<dict_name>
のように指定してください。そうでない場合は、現在のデータベースが使用されます。
ディクショナリの接続と構成に関する情報は、Dictionariesを参照してください。
dictGet, dictGetOrDefault, dictGetOrNull
ディクショナリから値を取得します。
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
— ディクショナリの名前。文字列リテラル。
attr_names
— ディクショナリのカラムの名前、文字列リテラルまたはカラム名のタプル、タプル(文字列リテラル)。
id_expr
— キー値。式はディクショナリキー型の値またはディクショナリ設定に応じてタプル型の値を返します。
default_value_expr
— id_expr
キーを持たない行がディクショナリに存在しない場合に返される値。式またはタプル(式)で、attr_names
属性のデータ型で構成された値(または値)を返します。
返される値
ClickHouseは、属性の値を解析できない場合や、それが属性のデータ型と一致しない場合に例外をスローします。
単純キーのディクショナリの例
次の内容を含んだテキストファイルext-dict-test.csv
を作成します。
最初のカラムはid
、二番目のカラムはc1
です。
ディクショナリを構成します:
<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 │
└─────┴────────┘
複雑キーのディクショナリの例
次の内容を含んだテキストファイルext-dict-mult.csv
を作成します:
最初のカラムはid
、二番目はc1
、三番目はc2
です。
ディクショナリを構成します:
<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) │
└─────────┴───────────────────────┘
範囲キーのディクショナリの例
入力テーブル:
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');
ディクショナリを作成します:
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
キーがディクショナリに存在するかどうかを確認します。
dictHas('dict_name', id_expr)
引数
dict_name
— ディクショナリの名前。文字列リテラル。
id_expr
— キー値。式はディクショナリキー型の値またはタプル型の値を返します、ディクショナリの構成によります。
返される値
dictGetHierarchy
階層的ディクショナリ内のキーのすべての親を含む配列を作成します。
構文
dictGetHierarchy('dict_name', key)
引数
返される値
dictIsIn
ディクショナリ内の階層全体を通じてキーの先祖を確認します。
dictIsIn('dict_name', child_id_expr, ancestor_id_expr)
引数
dict_name
— ディクショナリの名前。文字列リテラル。
child_id_expr
— 確認するキー。式はUInt64型の値を返します。
ancestor_id_expr
— child_id_expr
キーの推定先祖。式はUInt64型の値を返します。
返される値
child_id_expr
がancestor_id_expr
の子でない場合は0です。UInt8。
child_id_expr
がancestor_id_expr
の子であるか、またはchild_id_expr
がancestor_id_expr
である場合は1です。UInt8。
dictGetChildren
第一レベルの子をインデックスの配列として返します。これはdictGetHierarchyの逆変換です。
構文
dictGetChildren(dict_name, key)
引数
返される値
例
階層的ディクショナリを考えます:
┌─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
— ディクショナリの名前。文字列リテラル。
key
— キー値。式はUInt64型の値を返します。
level
— 階層レベル。level = 0
の場合、すべての子孫を返します。UInt8。
返される値
例
階層的ディクショナリを考えます:
┌─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
正規表現ツリーディクショナリ内の各キーに一致するノードのすべての属性値を取得します。
Array(T)
型の値を返す以外は、この関数はdictGet
と似ています。
構文
dictGetAll('dict_name', attr_names, id_expr[, limit])
引数
dict_name
— ディクショナリの名前。文字列リテラル。
attr_names
— ディクショナリのカラムの名前、文字列リテラルまたはカラム名のタプル、タプル(文字列リテラル)。
id_expr
— キー値。式はディクショナリキー型の値の配列またはタプル型の値を返します、ディクショナリの構成によります。
limit
- 各値の配列として返される最大長。切り捨て時には、子ノードが親ノードに優先され、それ以外では正規表現ツリーディクショナリの定義されたリスト順が尊重されます。指定しない場合は、配列の長さは無制限です。
返される値
ClickHouseは、属性の値を解析できない場合や、それが属性のデータ型と一致しない場合に例外をスローします。
例
次の正規表現ツリーディクショナリを考えます:
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は、ディクショナリの構成に関係なく、ディクショナリ属性値を特定のデータ型に変換する専門の関数をサポートしています。
関数:
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
— ディクショナリの名前。文字列リテラル。
attr_name
— ディクショナリのカラムの名前。文字列リテラル。
id_expr
— キー値。式はUInt64またはタプル型の値を返します、ディクショナリの構成によります。
default_value_expr
— ディクショナリにid_expr
キーを持つ行が存在しない場合に返される値。式はattr_name
属性に構成されたデータ型の値を返します。
返される値
ClickHouseは、属性の値を解析できない場合や、それが属性のデータ型と一致しない場合に例外をスローします。