Skip to content

Commit

Permalink
efine code
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 committed Sep 16, 2019
1 parent 011ff71 commit df69702
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
35 changes: 18 additions & 17 deletions expression/builtin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package expression

import (
"encoding/hex"
"fmt"
"sort"
"strconv"

Expand All @@ -28,6 +29,7 @@ import (
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/printer"
)

Expand Down Expand Up @@ -624,32 +626,31 @@ func (b *builtinTiDBDecodeKeySig) evalString(row chunk.Row) (string, bool, error
if isNull || err != nil {
return "", isNull, err
}
return decodeKey(s), false, nil
}

func decodeKey(s string) string {
key, err := hex.DecodeString(s)
if err != nil {
return "", false, err
return s
}
return decodeKey(key), false, nil
}

func decodeKey(key []byte) string {
tableID, indexID, indexValues, err := tablecodec.DecodeIndexKey(key)
// Auto decode byte if needs.
_, bs, err := codec.DecodeBytes([]byte(key), nil)
if err == nil {
str := "tableID=" + strconv.FormatInt(tableID, 10) + ", indexID=" + strconv.FormatInt(indexID, 10) + ", indexValues={"
for i, v := range indexValues {
if i > 0 {
str += ","
}
str += v
}
str += "}"
return str
key = bs
}

// Try decode as table record key.
tableID, handle, err := tablecodec.DecodeRecordKey(key)
if err == nil {
return "tableID=" + strconv.FormatInt(tableID, 10) + ", handle=" + strconv.FormatInt(handle, 10)
}
// Try decode as table index key.
tableID, indexID, indexValues, err := tablecodec.DecodeIndexKeyPrefix(key)
if err == nil {
idxValueStr := fmt.Sprintf("%X", indexValues)
return "tableID=" + strconv.FormatInt(tableID, 10) + ", indexID=" + strconv.FormatInt(indexID, 10) + ", indexValues=" + idxValueStr
}

// TODO: try to decode other type key.
return string(key)
return s
}
2 changes: 1 addition & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4130,7 +4130,7 @@ func (s *testIntegrationSuite) TestTiDBInternalFunc(c *C) {
result.Check(testkit.Rows("tableID=43, handle=42451"))

result = tk.MustQuery("select tidb_decode_key( '74800000000000019B5F698000000000000001015257303100000000FB013736383232313130FF3900000000000000F8010000000000000000F7' )")
result.Check(testkit.Rows("tableID=411, indexID=1, indexValues={RW01,768221109,}"))
result.Check(testkit.Rows("tableID=411, indexID=1, indexValues=015257303100000000FB013736383232313130FF3900000000000000F8010000000000000000F7"))
}

func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ require (
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
)

replace github.com/pingcap/parser => github.com/crazycs520/parser v0.0.0-20190916032426-3b2e7921b292
replace github.com/pingcap/parser => github.com/crazycs520/parser v0.0.0-20190916053053-b800dfc88b6d
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/crazycs520/parser v0.0.0-20190916032426-3b2e7921b292 h1:KfFtOwdW2Q1lJq4YlByDiX7nhar3iUpF5hgKIqUHXbU=
github.com/crazycs520/parser v0.0.0-20190916032426-3b2e7921b292/go.mod h1:xLjI+gnWYexq011WPMEvCNS8rFM9qe1vdojIEzSKPuc=
github.com/crazycs520/parser v0.0.0-20190916053053-b800dfc88b6d h1:UVtq5K54gpeYI5I8w4r/YYBg9XywJDZN87WEzasYAzU=
github.com/crazycs520/parser v0.0.0-20190916053053-b800dfc88b6d/go.mod h1:xLjI+gnWYexq011WPMEvCNS8rFM9qe1vdojIEzSKPuc=
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso=
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
github.com/cznic/sortutil v0.0.0-20150617083342-4c7342852e65 h1:hxuZop6tSoOi0sxFzoGGYdRqNrPubyaIf9KoBG9tPiE=
Expand Down
22 changes: 17 additions & 5 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,10 @@ func DecodeRecordKey(key kv.Key) (tableID int64, handle int64, err error) {
func DecodeIndexKey(key kv.Key) (tableID int64, indexID int64, indexValues []string, err error) {
k := key

tableID, indexID, isRecord, err := DecodeKeyHead(key)
tableID, indexID, key, err = DecodeIndexKeyPrefix(key)
if err != nil {
return 0, 0, nil, errors.Trace(err)
}
if isRecord {
return 0, 0, nil, errInvalidIndexKey.GenWithStack("invalid index key - %q", k)
}
key = key[prefixLen+idLen:]

for len(key) > 0 {
// FIXME: Without the schema information, we can only decode the raw kind of
Expand All @@ -153,6 +149,22 @@ func DecodeIndexKey(key kv.Key) (tableID int64, indexID int64, indexValues []str
return
}

// DecodeIndexKey decodes the key and gets the tableID, indexID, indexValues.
func DecodeIndexKeyPrefix(key kv.Key) (tableID int64, indexID int64, indexValues []byte, err error) {
k := key

tableID, indexID, isRecord, err := DecodeKeyHead(key)
if err != nil {
return 0, 0, nil, errors.Trace(err)
}
if isRecord {
return 0, 0, nil, errInvalidIndexKey.GenWithStack("invalid index key - %q", k)
}
indexValues = key[prefixLen+idLen:]

return tableID, indexID, indexValues, nil
}

// DecodeKeyHead decodes the key's head and gets the tableID, indexID. isRecordKey is true when is a record key.
func DecodeKeyHead(key kv.Key) (tableID int64, indexID int64, isRecordKey bool, err error) {
isRecordKey = false
Expand Down

0 comments on commit df69702

Please sign in to comment.