Skip to content
Merged
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
62 changes: 55 additions & 7 deletions functions-and-operators/tidb-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,45 @@ SELECT VITESS_HASH(123);

## TIDB_ENCODE_INDEX_KEY

Encodes an index key.
The `TIDB_ENCODE_INDEX_KEY()` function encodes a specified index key into a hexadecimal string. The syntax is as follows:

```sql
TIDB_ENCODE_INDEX_KEY(<db_name>, <table_name>, <index_name>, <index_columns>..., <handle_columns>...)
```

Parameter descriptions:

* `<db_name>`: the name of the database that contains the target index.
* `<table_name>`: the name of the table that contains the target index. For a partitioned table, you can specify the partition name, for example, `'t(p0)'`.
* `<index_name>`: the name of the target index.
* `<index_columns>...`: the values of the index columns. You must specify the values in the same order as defined in the index. For a composite index, you must specify values for all index columns.
* `<handle_columns>...`: the handle values for the row. The required handle values depend on the primary key type of the table:

* If the table has no primary key, or the primary key is `NONCLUSTERED`, the handle value is the value of the hidden column `_tidb_rowid`.
* If the primary key is `CLUSTERED` and is a single-column integer, the handle value is the value of the primary key column.
* If the primary key is `CLUSTERED` and is a composite primary key or a non-integer type (common handle), the handle value consists of the values of all primary key columns in order.

The following examples show how to call this function for the composite secondary index `idx(c1, c2)` under different primary key types.

```sql
-- For tables without a primary key or with a NONCLUSTERED primary key, use the _tidb_rowid column.
SELECT TIDB_ENCODE_INDEX_KEY(
'<db_name>', '<table_name>', '<index_name>',
<c1>, <c2>, <_tidb_rowid>
);

-- For tables with a CLUSTERED integer primary key (the primary key column is id), use the id column.
SELECT TIDB_ENCODE_INDEX_KEY(
'<db_name>', '<table_name>', '<index_name>',
<c1>, <c2>, <id>
);

-- For tables with a CLUSTERED composite primary key (the primary key columns are p1, p2), provide the values of p1 and p2 in their defined order.
SELECT TIDB_ENCODE_INDEX_KEY(
'<db_name>', '<table_name>', '<index_name>',
<c1>, <c2>, <p1>, <p2>
);
```

```sql
CREATE TABLE t(id int PRIMARY KEY, a int, KEY `idx` (a));
Expand All @@ -594,29 +632,39 @@ Query OK, 0 rows affected (0.00 sec)
```

```sql
INSERT INTO t VALUES(1,1);
INSERT INTO t VALUES(1,2);
```

```
Query OK, 1 row affected (0.00 sec)
```

```sql
SELECT TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 1, 1);
SELECT TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 2, 1);
```

```
+----------------------------------------------------------------------------+
| TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 1, 1) |
| TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 2, 1) |
+----------------------------------------------------------------------------+
| 74800000000000007f5f698000000000000001038000000000000001038000000000000001 |
| 7480000000000000b45f698000000000000001038000000000000002038000000000000001 |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

## TIDB_ENCODE_RECORD_KEY

Encodes a record key.
The `TIDB_ENCODE_RECORD_KEY()` function encodes a specified row record key into a hexadecimal string. The function syntax is as follows:

```sql
TIDB_ENCODE_RECORD_KEY(<db_name>, <table_name>, <handle_columns>...)
```

Parameter descriptions:

* `<db_name>`: the name of the database that contains the target table.
* `<table_name>`: the name of the target table. For a partitioned table, you can specify the partition name in `<table_name>`, for example, `'t(p0)'`.
* `<handle_columns>...`: the handle (row key) values for the corresponding row. The exact composition of the handle depends on the primary key type of the table, such as whether the primary key is `CLUSTERED`, a common handle, or uses the hidden column `_tidb_rowid`. For more information, see the description of `<handle_columns>...` in [`TIDB_ENCODE_INDEX_KEY()`](#tidb_encode_index_key).

```sql
CREATE TABLE t(id int PRIMARY KEY, a int, KEY `idx` (a));
Expand All @@ -627,7 +675,7 @@ Query OK, 0 rows affected (0.00 sec)
```

```sql
INSERT INTO t VALUES(1,1);
INSERT INTO t VALUES(1,2);
```

```
Expand Down