Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ key_column_expr ::= <column_name> | <expr>
## Parameters

- Required parameters:
- `[<db_name>.]<dict_table>`: The name of the dictionary table, which needs to be a Primary Key table. The supported data type is VARCHAR.
- `[<db_name>.]<dict_table>`: The name of the dictionary table, which needs to be a Primary Key table. The supported data type is `VARCHAR`.
- `key_column_expr_list`: The expression list for key columns in the dictionary table, including one or multiple `key_column_exprs`. The `key_column_expr` can be the name of a key column in the dictionary table, or a specific key or key expression.

This expression list needs to include all Primary Key columns of the dictionary table, which means the total number of expressions needs to match the total number of Primary Key columns in the dictionary table. So when the dictionary table uses composite primary key, the expressions in this list needs to correspond to the Primary Key columns defined in the table schema by sequence. Multiple expressions in this list are separated by commas (`,`). And if a `key_column_expr` is a specific key or key expression, its type must match the type of the corresponding Primary Key column in the dictionary table.
Expand Down Expand Up @@ -124,7 +124,7 @@ However, when the value mapped to the specified key is not found, if the `<null_
4 rows in set (0.02 sec)
```

The usage of `dict_mapping` in this example can accelerate [deduplication calculations and JOIN queries](../../../using_starrocks/query_acceleration_with_auto_increment.md). Compared to the previous solutions for building a global dictionary to accelerate precise deduplication, the solution by using `dict_mapping` is more flexible and user-friendly. Because the mapping values are directly obtained from the dictionary table at the stage "loading mapping relationships between keys and values into the table". You do not need to write statements to join the dictionary table to obtain mapping values. Additionally, this solution supports various data loading methods.<!--For detailed usage, please refer to xxx.-->
The usage of `dict_mapping` in this example can accelerate [deduplication calculations and JOIN queries](../../../using_starrocks/query_acceleration_with_auto_increment.md). Compared to the previous solutions for building a global dictionary to accelerate precise deduplication, the solution by using `dict_mapping` is more flexible and user-friendly. Because the mapping values are directly obtained from the dictionary table at the stage "loading mapping relationships between keys and values into the table". You do not need to write statements to join the dictionary table to obtain mapping values. Additionally, this solution supports various data loading methods.

**Example 3: If the mapping column in the table is not configured as a generated column, you need to explicitly configure the `dict_mapping` function for the mapping column when loading data into the table, obtain the values mapped to the keys.**

Expand Down
215 changes: 215 additions & 0 deletions docs/ja/sql-reference/sql-functions/dict-functions/dict_mapping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
---
displayed_sidebar: docs
---

# dict_mapping

辞書テーブル内の指定されたキーにマッピングされた値を返します。

この関数は、主にグローバル辞書テーブルの適用を簡素化するために使用されます。ターゲットテーブルへのデータロード中に、StarRocksはこの関数の入力パラメータを使用して、辞書テーブルから指定されたキーにマッピングされた値を自動的に取得し、その値をターゲットテーブルにロードします。

v3.2.5以降、StarRocksはこの関数をサポートしています。また、現在StarRocksの共有データモードはこの関数をサポートしていないことに注意してください。

## 構文

```SQL
dict_mapping("[<db_name>.]<dict_table>", key_column_expr_list [, <value_column> ] [, <null_if_not_exist>] )

key_column_expr_list ::= key_column_expr [, key_column_expr ... ]

key_column_expr ::= <column_name> | <expr>
```

## パラメータ

- 必須パラメータ:
- `[<db_name>.]<dict_table>`: 辞書テーブルの名前。プライマリキーテーブルである必要があります。サポートされているデータ型は`VARCHAR`です。
- `key_column_expr_list`: 辞書テーブルのキー列の式リスト。1つまたは複数の`key_column_exprs`を含みます。`key_column_expr`は、辞書テーブルのキー列の名前、または特定のキー、あるいはキー式にすることができます。

この式リストには、辞書テーブルのすべてのプライマリキー列を含める必要があります。つまり、式の総数は辞書テーブルのプライマリキー列の総数と一致する必要があります。したがって、辞書テーブルが複合プライマリキーを使用する場合、このリストの式は、テーブルスキーマで定義されたプライマリキー列に順序通りに対応する必要があります。このリスト内の複数の式はコンマ (`,`) で区切られます。また、`key_column_expr`が特定のキーまたはキー式である場合、その型は辞書テーブルの対応するプライマリキー列の型と一致する必要があります。

- オプションパラメータ:
- `<value_column>`: 値列の名前。これはマッピング列でもあります。値列が指定されていない場合、デフォルトの値列は辞書テーブルのAUTO_INCREMENT列です。値列は、自動インクリメント列とプライマリキーを除く辞書テーブル内の任意の列として定義することもできます。列のデータ型に制限はありません。
- `<null_if_not_exist>` (オプション): キーが辞書テーブルに存在しない場合に何を返すか。有効な値:
- `true`: キーが存在しない場合、Nullが返されます。
- `false` (デフォルト): キーが存在しない場合、例外がスローされます。

## 戻り値

返される値のデータ型は、値列のデータ型と一貫しています。値列が辞書テーブルの自動インクリメント列である場合、返される値のデータ型はBIGINTです。

ただし、指定されたキーにマッピングされた値が見つからない場合、`<null_if_not_exist>`パラメータが`true`に設定されていると、`NULL`が返されます。パラメータが`false`(デフォルト)に設定されている場合、エラー`query failed if record not exist in dict table`が返されます。

## 例

**例1: 辞書テーブルからキーにマッピングされた値を直接クエリします。**

1. 辞書テーブルを作成し、シミュレートされたデータをロードします。

```SQL
MySQL [test]> CREATE TABLE dict (
order_uuid STRING,
order_id_int BIGINT AUTO_INCREMENT
)
PRIMARY KEY (order_uuid)
DISTRIBUTED BY HASH (order_uuid);
Query OK, 0 rows affected (0.02 sec)

MySQL [test]> INSERT INTO dict (order_uuid) VALUES ('a1'), ('a2'), ('a3');
Query OK, 3 rows affected (0.12 sec)
{'label':'insert_9e60b0e4-89fa-11ee-a41f-b22a2c00f66b', 'status':'VISIBLE', 'txnId':'15029'}

MySQL [test]> SELECT * FROM dict;
+------------+--------------+
| order_uuid | order_id_int |
+------------+--------------+
| a1 | 1 |
| a3 | 3 |
| a2 | 2 |
+------------+--------------+
3 rows in set (0.01 sec)
```

> **注意**
>
> 現在、`INSERT INTO`ステートメントは部分更新をサポートしていません。したがって、`dict`のキー列に挿入される値が重複しないようにしてください。そうしないと、辞書テーブルに同じキー列の値を複数回挿入すると、値列のマッピングされた値が変更されます。

2. 辞書テーブル内のキー`a1`にマッピングされた値をクエリします。

```SQL
MySQL [test]> SELECT dict_mapping('dict', 'a1');
+----------------------------+
| dict_mapping('dict', 'a1') |
+----------------------------+
| 1 |
+----------------------------+
1 row in set (0.01 sec)
```

**例2: テーブル内のマッピング列が`dict_mapping`関数を使用して生成列として構成されています。これにより、StarRocksはこのテーブルにデータをロードする際に、キーにマッピングされた値を自動的に取得できます。**

1. データテーブルを作成し、`dict_mapping('dict', order_uuid)`を使用してマッピング列を生成列として構成します。

```SQL
CREATE TABLE dest_table1 (
id BIGINT,
-- この列には、STRING型の注文番号が記録されており、これは例1のdictテーブルのorder_uuid列に対応します。
order_uuid STRING,
batch int comment 'used to distinguish different batch loading',
-- この列には、order_uuid列にマッピングされたBIGINT型の注文番号が記録されます。
-- この列はdict_mappingで構成された生成列であるため、データロード中に例1のdictテーブルからこの列の値が自動的に取得されます。
-- その後、この列は重複排除およびJOINクエリに直接使用できます。
order_id_int BIGINT AS dict_mapping('dict', order_uuid)
)
DUPLICATE KEY (id, order_uuid)
DISTRIBUTED BY HASH(id);
```

2. `order_id_int`列が`dict_mapping('dict', 'order_uuid')`として構成されているこのテーブルにシミュレートされたデータをロードすると、StarRocksは`dict`テーブル内のキーと値のマッピング関係に基づいて、`order_id_int`列に値を自動的にロードします。

```SQL
MySQL [test]> INSERT INTO dest_table1(id, order_uuid, batch) VALUES (1, 'a1', 1), (2, 'a1', 1), (3, 'a3', 1), (4, 'a3', 1);
Query OK, 4 rows affected (0.05 sec)
{'label':'insert_e191b9e4-8a98-11ee-b29c-00163e03897d', 'status':'VISIBLE', 'txnId':'72'}

MySQL [test]> SELECT * FROM dest_table1;
+------+------------+-------+--------------+
| id | order_uuid | batch | order_id_int |
+------+------------+-------+--------------+
| 1 | a1 | 1 | 1 |
| 4 | a3 | 1 | 3 |
| 2 | a1 | 1 | 1 |
| 3 | a3 | 1 | 3 |
+------+------------+-------+--------------+
4 rows in set (0.02 sec)
```

この例での`dict_mapping`の使用は、[重複排除計算とJOINクエリ](../../../using_starrocks/query_acceleration_with_auto_increment.md)を高速化できます。正確な重複排除を高速化するためにグローバル辞書を構築する以前のソリューションと比較して、`dict_mapping`を使用するソリューションは、より柔軟でユーザーフレンドリーです。マッピング値は、「キーと値のマッピング関係をテーブルにロードする」段階で辞書テーブルから直接取得されるためです。マッピング値を取得するために辞書テーブルを結合するステートメントを記述する必要はありません。さらに、このソリューションはさまざまなデータロード方法をサポートしています。

**例3: テーブル内のマッピング列が生成列として構成されていない場合、テーブルにデータをロードする際に、マッピング列に対して`dict_mapping`関数を明示的に構成し、キーにマッピングされた値を取得する必要があります。**

> **注意**
>
> 例3と例2の違いは、データテーブルにインポートする際に、インポートコマンドを変更して、マッピング列に対して`dict_mapping`式を明示的に構成する必要があることです。

1. テーブルを作成します。

```SQL
CREATE TABLE dest_table2 (
id BIGINT,
order_uuid STRING,
order_id_int BIGINT NULL,
batch int comment 'used to distinguish different batch loading'
)
DUPLICATE KEY (id, order_uuid, order_id_int)
DISTRIBUTED BY HASH(id);
```

2. シミュレートされたデータがこのテーブルにロードされる際、`dict_mapping`を構成することで、辞書テーブルからマッピングされた値を取得します。

```SQL
MySQL [test]> INSERT INTO dest_table2 VALUES (1, 'a1', dict_mapping('dict', 'a1'), 1);
Query OK, 1 row affected (0.35 sec)
{'label':'insert_19872ab6-8a96-11ee-b29c-00163e03897d', 'status':'VISIBLE', 'txnId':'42'}

MySQL [test]> SELECT * FROM dest_table2;
+------+------------+--------------+-------+
| id | order_uuid | order_id_int | batch |
+------+------------+--------------+-------+
| 1 | a1 | 1 | 1 |
+------+------------+--------------+-------+
1 row in set (0.02 sec)
```

**例4: null_if_not_existモードを有効にする**

`<null_if_not_exist>`モードが無効で、辞書テーブルに存在しないキーにマッピングされた値がクエリされた場合、`NULL`ではなくエラーが返されます。これにより、データ行のキーが最初に辞書テーブルにロードされ、そのマッピングされた値(辞書ID)が生成されてから、そのデータ行がターゲットテーブルにロードされることが保証されます。

```SQL
MySQL [test]> SELECT dict_mapping('dict', 'b1', true);
ERROR 1064 (HY000): Query failed if record not exist in dict table.
```

**例5: 辞書テーブルが複合プライマリキーを使用する場合、クエリ時にはすべてのプライマリキーを指定する必要があります。**

1. 複合プライマリキーを持つ辞書テーブルを作成し、シミュレートされたデータをロードします。

```SQL
MySQL [test]> CREATE TABLE dict2 (
order_uuid STRING,
order_date DATE,
order_id_int BIGINT AUTO_INCREMENT
)
PRIMARY KEY (order_uuid,order_date) -- composite primary Key
DISTRIBUTED BY HASH (order_uuid,order_date)
;
Query OK, 0 rows affected (0.02 sec)

MySQL [test]> INSERT INTO dict2 VALUES ('a1','2023-11-22',default), ('a2','2023-11-22',default), ('a3','2023-11-22',default);
Query OK, 3 rows affected (0.12 sec)
{'label':'insert_9e60b0e4-89fa-11ee-a41f-b22a2c00f66b', 'status':'VISIBLE', 'txnId':'15029'}


MySQL [test]> select * from dict2;
+------------+------------+--------------+
| order_uuid | order_date | order_id_int |
+------------+------------+--------------+
| a1 | 2023-11-22 | 1 |
| a3 | 2023-11-22 | 3 |
| a2 | 2023-11-22 | 2 |
+------------+------------+--------------+
3 rows in set (0.01 sec)
```

2. 辞書テーブル内のキーにマッピングされた値をクエリします。辞書テーブルには複合プライマリキーがあるため、`dict_mapping`ですべてのプライマリキーを指定する必要があります。

```SQL
SELECT dict_mapping('dict2', 'a1', cast('2023-11-22' as DATE));
```

プライマリキーが1つだけ指定されている場合、エラーが発生することに注意してください。

```SQL
MySQL [test]> SELECT dict_mapping('dict2', 'a1');
ERROR 1064 (HY000): Getting analyzing error. Detail message: dict_mapping function param size should be 3 - 5.
```
Loading
Loading