Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: TIDB_DECODE_KEY(): Decode keys which only have a TableID #29522

Merged
merged 6 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4996,16 +4996,23 @@ func (s *testIntegrationSuite) TestTiDBInternalFunc(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer s.cleanEnv(c)
var result *testkit.Result

// Row Keys
result = tk.MustQuery("select tidb_decode_key( '74800000000000002B5F72800000000000A5D3' )")
result.Check(testkit.Rows(`{"_tidb_rowid":42451,"table_id":"43"}`))
result = tk.MustQuery("select tidb_decode_key( '7480000000000000325f7205bff199999999999a013131000000000000f9' )")
result.Check(testkit.Rows(`{"handle":"{1.1, 11}","table_id":50}`))

// Index Keys
result = tk.MustQuery("select tidb_decode_key( '74800000000000019B5F698000000000000001015257303100000000FB013736383232313130FF3900000000000000F8010000000000000000F7' )")
result.Check(testkit.Rows(`{"index_id":1,"index_vals":"RW01, 768221109, ","table_id":411}`))
result = tk.MustQuery("select tidb_decode_key( '7480000000000000695F698000000000000001038000000000004E20' )")
result.Check(testkit.Rows(`{"index_id":1,"index_vals":"20000","table_id":105}`))

// Table keys
result = tk.MustQuery("select tidb_decode_key( '7480000000000000FF4700000000000000F8' )")
result.Check(testkit.Rows(`{"table_id":71}`))

// Test invalid record/index key.
result = tk.MustQuery("select tidb_decode_key( '7480000000000000FF2E5F728000000011FFE1A3000000000000' )")
result.Check(testkit.Rows("7480000000000000FF2E5F728000000011FFE1A3000000000000"))
Expand Down
17 changes: 17 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,13 @@ func decodeKeyFromString(ctx sessionctx.Context, s string) string {
return s
}
return ret
} else if tablecodec.IsTableKey(key) {
ret, err := decodeTableKey(key, tableID, tbl, loc)
if err != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(err)
return s
}
return ret
}
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("invalid record/index key: %X", key))
return s
Expand Down Expand Up @@ -2150,6 +2157,16 @@ func decodeIndexKey(key []byte, tableID int64, tbl table.Table, loc *time.Locati
return string(retStr), nil
}

func decodeTableKey(key []byte, tableID int64, tbl table.Table, loc *time.Location) (string, error) {
ret := make(map[string]interface{})
ret["table_id"] = tableID
dveeden marked this conversation as resolved.
Show resolved Hide resolved
retStr, err := json.Marshal(ret)
if err != nil {
return "", errors.Trace(err)
}
return string(retStr), nil
}

func datumToJSONObject(d *types.Datum) (interface{}, error) {
if d.IsNull() {
return nil, nil
Expand Down
5 changes: 5 additions & 0 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,11 @@ func IsIndexKey(k []byte) bool {
return len(k) > 11 && k[0] == 't' && k[10] == 'i'
}

// IsTableKey is used to check whether the key is a table key.
func IsTableKey(k []byte) bool {
return len(k) == 9 && k[0] == 't'
}

// IsUntouchedIndexKValue uses to check whether the key is index key, and the value is untouched,
// since the untouched index key/value is no need to commit.
func IsUntouchedIndexKValue(k, v []byte) bool {
Expand Down