Unable to find the row record with a raw key encoded by clustered index
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Enhancement
```diff
diff --git a/expression/integration_test.go b/expression/integration_test.go
index 13c7dc690..92afd13ec 100644
--- a/expression/integration_test.go
+++ b/expression/integration_test.go
@@ -3023,6 +3023,19 @@ func TestTiDBDecodeKeyFunc(t *testing.T) {
sql = fmt.Sprintf("select tidb_decode_key('%s')", hexKey)
tk.MustQuery(sql).Check(testkit.Rows(`{"_tidb_rowid":42451,"table_id":"299"}`))
+ tk.MustExec("drop table if exists t;")
+ tk.MustExec("create table t (a varchar(255), b int, primary key (a) clustered) collate = utf8mb4_bin;")
+ is = dom.InfoSchema()
+ tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
+ require.NoError(t, err)
+ data = []types.Datum{types.NewStringDatum("bbbb")}
+ hexKey = buildCommonKeyFromData(tbl.Meta().ID, data)
+ sql = fmt.Sprintf("select tidb_decode_key( '%s' )", hexKey)
+ rs = fmt.Sprintf(`{"handle":{"a":"bbbb"},"table_id":%d}`, tbl.Meta().ID)
+ collate.SetNewCollationEnabledForTest(true)
+ tk.MustQuery(sql).Check(testkit.Rows(rs))
+ collate.SetNewCollationEnabledForTest(false)
+
// Test the table with the nonclustered index.
const rowID = 10
tk.MustExec("drop table if exists t;")
```
```
Error Trace: result.go:49
integration_test.go:3036
Error: Not equal:
expected: "[{\"handle\":{\"a\":\"bbbb\"},\"table_id\":75}]\n"
actual : "[{\"handle\":{},\"table_id\":75}]\n"
```
The background is that there is no way to decode the common handle in key, if the common handle needs to be encoded to a sort key according to the collation.
It is not clear that if we should decode it into `[{"handle":{"a":"bbbb"},"table_id":75}]`, because the decoded clustered index values are not informative(we can't use it for querying).
Maybe we need to support querying the raw key with special SQL like
```sql
select * from t where _tidb_handle_raw = '7480000000000004ffb35f72XXXXX';
```
to find the actual row corresponding to the raw key.
Contributor guide
Assessment
This issue has not been assessed yet.