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

*: refactor and fix the usages of DecodeIndexKV for clustered index #23096

Merged
merged 16 commits into from
Mar 10, 2021
Merged
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
3 changes: 2 additions & 1 deletion ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2225,7 +2225,7 @@ func (s *testIntegrationSuite7) TestAddExpressionIndex(c *C) {
}

func (s *testIntegrationSuite7) TestCreateExpressionIndexError(c *C) {
defer config.RestoreFunc()
defer config.RestoreFunc()()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is easy to make mistake to call a function of function, is there anyway we can do better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is easy to go wrong. How about this:

restoreFn := config.UpdateGlobal(func(conf *config.Config) {
	conf.Experimental.AllowsExpressionIndex = true
})
defer restoreFn()

config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
conf.AlterPrimaryKey = true
Expand Down Expand Up @@ -2640,6 +2640,7 @@ func (s *testIntegrationSuite7) TestDuplicateErrorMessage(c *C) {
restoreConfig := config.RestoreFunc()
config.UpdateGlobal(func(conf *config.Config) {
conf.EnableGlobalIndex = globalIndex
conf.AlterPrimaryKey = false
})
for _, clusteredIndex := range []bool{false, true} {
tk.Se.GetSessionVars().EnableClusteredIndex = clusteredIndex
Expand Down
28 changes: 13 additions & 15 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import (
"github.com/pingcap/tidb/util/domainutil"
"github.com/pingcap/tidb/util/israce"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/rowcodec"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)
Expand Down Expand Up @@ -1141,7 +1140,11 @@ func (s *testDBSuite4) TestAddIndex4(c *C) {
partition p4 values less than maxvalue)`, "")
}

func (s *testDBSuite5) TestAddIndex5(c *C) {
func (s *testSerialDBSuite) TestAddIndex5(c *C) {
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = false
})
testAddIndex(c, s.store, s.lease, testClusteredIndex,
`create table test_add_index (c1 bigint, c2 bigint, c3 bigint, primary key(c2, c3))`, "")
}
Expand Down Expand Up @@ -1303,7 +1306,7 @@ LOOP:

c.Assert(err, IsNil)
_, ok := handles.Get(h)
c.Assert(ok, IsTrue)
c.Assert(ok, IsTrue, Commentf("handle: %v", h.String()))
handles.Delete(h)
}
c.Assert(handles.Len(), Equals, 0)
Expand Down Expand Up @@ -1903,15 +1906,6 @@ func checkGlobalIndexRow(c *C, ctx sessionctx.Context, tblInfo *model.TableInfo,
for _, col := range tblInfo.Columns {
tblColMap[col.ID] = &col.FieldType
}
idxColInfos := make([]rowcodec.ColInfo, 0, len(indexInfo.Columns))
for _, idxCol := range indexInfo.Columns {
col := tblInfo.Columns[idxCol.Offset]
idxColInfos = append(idxColInfos, rowcodec.ColInfo{
ID: col.ID,
IsPKHandle: tblInfo.PKIsHandle && mysql.HasPriKeyFlag(col.Flag),
Ft: rowcodec.FieldTypeFromModelColumn(col),
})
}

// Check local index entry does not exist.
localPrefix := tablecodec.EncodeTableIndexPrefix(pid, indexInfo.ID)
Expand All @@ -1928,6 +1922,7 @@ func checkGlobalIndexRow(c *C, ctx sessionctx.Context, tblInfo *model.TableInfo,
c.Assert(err, IsNil)
value, err := txn.Get(context.Background(), key)
c.Assert(err, IsNil)
idxColInfos := tables.BuildRowcodecColInfoForIndexColumns(indexInfo, tblInfo)
colVals, err := tablecodec.DecodeIndexKV(key, value, len(indexInfo.Columns), tablecodec.HandleDefault, idxColInfos)
c.Assert(err, IsNil)
c.Assert(colVals, HasLen, len(idxVals)+2)
Expand Down Expand Up @@ -1955,8 +1950,10 @@ func checkGlobalIndexRow(c *C, ctx sessionctx.Context, tblInfo *model.TableInfo,
}

func (s *testSerialDBSuite) TestAddGlobalIndex(c *C) {
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.EnableGlobalIndex = true
conf.AlterPrimaryKey = true
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
Expand Down Expand Up @@ -2024,9 +2021,6 @@ func (s *testSerialDBSuite) TestAddGlobalIndex(c *C) {

err = txn.Commit(context.Background())
c.Assert(err, IsNil)
config.UpdateGlobal(func(conf *config.Config) {
conf.EnableGlobalIndex = false
})
}

func (s *testDBSuite) showColumns(tk *testkit.TestKit, c *C, tableName string) [][]interface{} {
Expand Down Expand Up @@ -2678,6 +2672,10 @@ func (s *testSerialDBSuite) TestRepairTable(c *C) {
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/infoschema/repairFetchCreateTable"), IsNil)
}()
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = true
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, other_table, origin")
Expand Down
69 changes: 16 additions & 53 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package ddl

import (
"bytes"
"context"
"strings"
"sync/atomic"
Expand All @@ -40,10 +39,8 @@ import (
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/logutil"
decoder "github.com/pingcap/tidb/util/rowDecoder"
"github.com/pingcap/tidb/util/rowcodec"
"github.com/pingcap/tidb/util/timeutil"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
Expand Down Expand Up @@ -1026,68 +1023,34 @@ func (w *addIndexWorker) initBatchCheckBufs(batchCount int) {
func (w *addIndexWorker) checkHandleExists(key kv.Key, value []byte, handle kv.Handle) error {
idxInfo := w.index.Meta()
tblInfo := w.table.Meta()
name := w.index.Meta().Name.String()

colInfo := make([]rowcodec.ColInfo, 0, len(idxInfo.Columns))
for _, idxCol := range idxInfo.Columns {
col := tblInfo.Columns[idxCol.Offset]
colInfo = append(colInfo, rowcodec.ColInfo{
ID: col.ID,
IsPKHandle: tblInfo.PKIsHandle && mysql.HasPriKeyFlag(col.Flag),
Ft: rowcodec.FieldTypeFromModelColumn(col),
})
idxColLen := len(idxInfo.Columns)
h, err := tablecodec.DecodeIndexHandle(key, value, idxColLen)
if err != nil {
return errors.Trace(err)
}

values, err := tablecodec.DecodeIndexKV(key, value, len(idxInfo.Columns), tablecodec.HandleDefault, colInfo)
hasBeenBackFilled := h.Equal(handle)
if hasBeenBackFilled {
return nil
}
colInfos := tables.BuildRowcodecColInfoForIndexColumns(idxInfo, tblInfo)
values, err := tablecodec.DecodeIndexKV(key, value, idxColLen, tablecodec.HandleNotNeeded, colInfos)
if err != nil {
return err
}

if !w.table.Meta().IsCommonHandle {
_, d, err := codec.DecodeOne(values[len(colInfo)])
if err != nil {
return errors.Trace(err)
}
if d.GetInt64() == handle.IntValue() {
return nil
}
} else {
// We expect the two handle have the same number of columns, because they come from a same table.
// But we still need to check it explicitly, otherwise we will encounter undesired index out of range panic,
// or undefined behavior if someone change the format of the value returned by tablecodec.DecodeIndexKV.
colsOfHandle := len(values) - len(colInfo)
if w.index.Meta().Global {
colsOfHandle--
}
if colsOfHandle != handle.NumCols() {
// We can claim these two handle are different, because they have different length.
// But we'd better report an error at here to detect compatibility problem introduced in other package during tests.
return errors.New("number of columns in two handle is different")
}

for i := 0; i < handle.NumCols(); i++ {
if bytes.Equal(values[i+len(colInfo)], handle.EncodedCol(i)) {
colsOfHandle--
}
}
if colsOfHandle == 0 {
return nil
}
}

valueStr := make([]string, 0, len(colInfo))
for i, val := range values[:len(colInfo)] {
d, err := tablecodec.DecodeColumnValue(val, colInfo[i].Ft, time.Local)
indexName := w.index.Meta().Name.String()
valueStr := make([]string, 0, idxColLen)
for i, val := range values[:idxColLen] {
d, err := tablecodec.DecodeColumnValue(val, colInfos[i].Ft, time.Local)
if err != nil {
return kv.ErrKeyExists.FastGenByArgs(key.String(), name)
return kv.ErrKeyExists.FastGenByArgs(key.String(), indexName)
}
str, err := d.ToString()
if err != nil {
str = string(val)
}
valueStr = append(valueStr, str)
}
return kv.ErrKeyExists.FastGenByArgs(strings.Join(valueStr, "-"), name)
return kv.ErrKeyExists.FastGenByArgs(strings.Join(valueStr, "-"), indexName)
}

func (w *addIndexWorker) batchCheckUniqueKey(txn kv.Transaction, idxRecords []*indexRecord) error {
Expand Down
11 changes: 2 additions & 9 deletions executor/mem_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,8 @@ func (m *memIndexReader) decodeIndexKeyValue(key, value []byte, tps []*types.Fie
if mysql.HasUnsignedFlag(tps[len(tps)-1].Flag) {
hdStatus = tablecodec.HandleIsUnsigned
}
colInfos := make([]rowcodec.ColInfo, 0, len(m.index.Columns))
for _, idxCol := range m.index.Columns {
col := m.table.Columns[idxCol.Offset]
colInfos = append(colInfos, rowcodec.ColInfo{
ID: col.ID,
IsPKHandle: m.table.PKIsHandle && mysql.HasPriKeyFlag(col.Flag),
Ft: rowcodec.FieldTypeFromModelColumn(col),
})
}
colInfos := tables.BuildRowcodecColInfoForIndexColumns(m.index, m.table)
colInfos = tables.TryAppendCommonHandleRowcodecColInfos(colInfos, m.table)
values, err := tablecodec.DecodeIndexKV(key, value, len(m.index.Columns), hdStatus, colInfos)
if err != nil {
return nil, errors.Trace(err)
Expand Down
17 changes: 3 additions & 14 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/hint"
"github.com/pingcap/tidb/util/rowcodec"
"github.com/pingcap/tidb/util/stringutil"
)

Expand Down Expand Up @@ -1999,28 +1998,18 @@ func decodeIndexKey(key []byte, tableID int64, tbl table.Table, loc *time.Locati
return "", errors.Trace(errors.Errorf("invalid record/index key: %X", key))
}
tblInfo := tbl.Meta()
var colInfos []rowcodec.ColInfo
var tps []*types.FieldType
var targetIndex *model.IndexInfo
for _, idx := range tblInfo.Indices {
if idx.ID == indexID {
targetIndex = idx
colInfos = make([]rowcodec.ColInfo, 0, len(idx.Columns))
tps = make([]*types.FieldType, 0, len(idx.Columns))
for _, idxCol := range idx.Columns {
col := tblInfo.Columns[idxCol.Offset]
colInfos = append(colInfos, rowcodec.ColInfo{
ID: col.ID,
Ft: rowcodec.FieldTypeFromModelColumn(col),
})
tps = append(tps, rowcodec.FieldTypeFromModelColumn(col))
}
break
}
}
if len(colInfos) == 0 || len(tps) == 0 || targetIndex == nil {
if targetIndex == nil {
return "", errors.Trace(errors.Errorf("index not found when decoding index key: %X", key))
}
colInfos := tables.BuildRowcodecColInfoForIndexColumns(targetIndex, tblInfo)
tps := tables.BuildFieldTypesForIndexColumns(targetIndex, tblInfo)
values, err := tablecodec.DecodeIndexKV(key, []byte{0}, len(colInfos), tablecodec.HandleNotNeeded, colInfos)
if err != nil {
return "", errors.Trace(err)
Expand Down
28 changes: 27 additions & 1 deletion session/clustered_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (s *testClusteredSerialSuite) TestClusteredIndexSyntax(c *C) {
tk.MustQuery(showPKType).Check(testkit.Rows(pkType))
}

defer config.RestoreFunc()
defer config.RestoreFunc()()
for _, allowAlterPK := range []bool{true, false} {
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = allowAlterPK
Expand Down Expand Up @@ -436,3 +436,29 @@ func (s *testClusteredSerialSuite) TestClusteredIndexSyntax(c *C) {
assertPkType("create table t (a int, b varchar(255), primary key(b, a) /*T![clustered_index] clustered */);", clustered)
}
}

// https://github.com/pingcap/tidb/issues/23106
func (s *testClusteredSerialSuite) TestClusteredIndexDecodeRestoredDataV5(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = false
})
defer collate.SetNewCollationEnabledForTest(false)
collate.SetNewCollationEnabledForTest(true)
tk.MustExec("use test")
tk.Se.GetSessionVars().EnableClusteredIndex = true
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (id1 int, id2 varchar(10), a1 int, primary key(id1, id2) clustered) collate utf8mb4_general_ci;")
tk.MustExec("insert into t values (1, 'asd', 1), (1, 'dsa', 1);")
tk.MustGetErrCode("alter table t add unique index t_idx(id1, a1);", errno.ErrDupEntry)

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (id1 int, id2 varchar(10), a1 int, primary key(id1, id2) clustered, unique key t_idx(id1, a1)) collate utf8mb4_general_ci;")
tk.MustExec("begin;")
tk.MustExec("insert into t values (1, 'asd', 1);")
tk.MustQuery("select * from t use index (t_idx);").Check(testkit.Rows("1 asd 1"))
tk.MustExec("commit;")
tk.MustExec("admin check table t;")
tangenta marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("drop table t;")
}
12 changes: 1 addition & 11 deletions store/driver/txn/txn_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/rowcodec"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -170,16 +169,7 @@ func extractKeyExistsErrFromIndex(key kv.Key, value []byte, tblInfo *model.Table
return genKeyExistsError(name, key.String(), errors.New("missing value"))
}

colInfo := make([]rowcodec.ColInfo, 0, len(idxInfo.Columns))
for _, idxCol := range idxInfo.Columns {
col := tblInfo.Columns[idxCol.Offset]
colInfo = append(colInfo, rowcodec.ColInfo{
ID: col.ID,
IsPKHandle: tblInfo.PKIsHandle && mysql.HasPriKeyFlag(col.Flag),
Ft: rowcodec.FieldTypeFromModelColumn(col),
})
}

colInfo := tables.BuildRowcodecColInfoForIndexColumns(idxInfo, tblInfo)
values, err := tablecodec.DecodeIndexKV(key, value, len(idxInfo.Columns), tablecodec.HandleNotNeeded, colInfo)
if err != nil {
return genKeyExistsError(name, key.String(), err)
Expand Down
Loading