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

*: fix panic that upgrade from old version TiDB. #7136

Merged
merged 6 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ LOOP:
}

ctx := s.s.(sessionctx.Context)
idx := tables.NewIndex(t.Meta().ID, c3IdxInfo)
idx := tables.NewIndex(t.Meta().ID, t.Meta(), c3IdxInfo)
checkDelRangeDone(c, ctx, idx)

s.mustExec(c, "drop table t1")
Expand Down Expand Up @@ -795,7 +795,7 @@ LOOP:
}
c.Assert(nidx, IsNil)

idx := tables.NewIndex(t.Meta().ID, c3idx.Meta())
idx := tables.NewIndex(t.Meta().ID, t.Meta(), c3idx.Meta())
checkDelRangeDone(c, ctx, idx)
s.tk.MustExec("drop table test_drop_index")
}
Expand Down
4 changes: 2 additions & 2 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func buildIndexColumns(columns []*model.ColumnInfo, idxColNames []*ast.IndexColN
Name: col.Name,
Offset: col.Offset,
Length: ic.Length,
Tp: &col.FieldType,
})
}

Expand Down Expand Up @@ -488,7 +487,8 @@ type addIndexResult struct {
}

func newAddIndexWorker(sessCtx sessionctx.Context, worker *worker, id int, t table.Table, indexInfo *model.IndexInfo, colFieldMap map[int64]*types.FieldType) *addIndexWorker {
index := tables.NewIndex(t.Meta().ID, indexInfo)
tblInfo := t.Meta()
index := tables.NewIndex(tblInfo.ID, tblInfo, indexInfo)
return &addIndexWorker{
id: id,
ddlWorker: worker,
Expand Down
14 changes: 7 additions & 7 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *testSuite) TestAdminRecoverIndex(c *C) {

tblInfo := tbl.Meta()
idxInfo := findIndexByName("c2", tblInfo.Indices)
indexOpr := tables.NewIndex(tblInfo.ID, idxInfo)
indexOpr := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo)
sc := s.ctx.GetSessionVars().StmtCtx
txn, err := s.store.Begin()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -186,7 +186,7 @@ func (s *testSuite) TestAdminRecoverIndex1(c *C) {
tblInfo := tbl.Meta()
idxInfo := findIndexByName("primary", tblInfo.Indices)
c.Assert(idxInfo, NotNil)
indexOpr := tables.NewIndex(tblInfo.ID, idxInfo)
indexOpr := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo)

txn, err := s.store.Begin()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -242,9 +242,9 @@ func (s *testSuite) TestAdminCleanupIndex(c *C) {

tblInfo := tbl.Meta()
idxInfo2 := findIndexByName("c2", tblInfo.Indices)
indexOpr2 := tables.NewIndex(tblInfo.ID, idxInfo2)
indexOpr2 := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo2)
idxInfo3 := findIndexByName("c3", tblInfo.Indices)
indexOpr3 := tables.NewIndex(tblInfo.ID, idxInfo3)
indexOpr3 := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo3)

txn, err := s.store.Begin()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -309,7 +309,7 @@ func (s *testSuite) TestAdminCleanupIndexPKNotHandle(c *C) {

tblInfo := tbl.Meta()
idxInfo := findIndexByName("primary", tblInfo.Indices)
indexOpr := tables.NewIndex(tblInfo.ID, idxInfo)
indexOpr := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo)

txn, err := s.store.Begin()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -357,9 +357,9 @@ func (s *testSuite) TestAdminCleanupIndexMore(c *C) {

tblInfo := tbl.Meta()
idxInfo1 := findIndexByName("c1", tblInfo.Indices)
indexOpr1 := tables.NewIndex(tblInfo.ID, idxInfo1)
indexOpr1 := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo1)
idxInfo2 := findIndexByName("c2", tblInfo.Indices)
indexOpr2 := tables.NewIndex(tblInfo.ID, idxInfo2)
indexOpr2 := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo2)

txn, err := s.store.Begin()
c.Assert(err, IsNil)
Expand Down
2 changes: 0 additions & 2 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ type IndexColumn struct {
// for indexing;
// UnspecifedLength if not using prefix indexing
Length int `json:"length"`
// Tp is the index column field type.
Tp *types.FieldType
}

// Clone clones IndexColumn.
Expand Down
2 changes: 1 addition & 1 deletion plan/plan_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s *testDistsqlSuite) TestIndexScanToProto(c *C) {
Name: name,
State: model.StatePublic,
Columns: []*model.IndexColumn{
{Tp: tp, Name: name, Length: types.UnspecifiedLength},
{Length: types.UnspecifiedLength},
},
}
p := new(PhysicalIndexScan)
Expand Down
10 changes: 7 additions & 3 deletions table/tables/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ func (c *indexIter) Next() (val []types.Datum, h int64, err error) {
// index is the data structure for index data in the KV store.
type index struct {
idxInfo *model.IndexInfo
tblInfo *model.TableInfo
prefix kv.Key
}

// NewIndex builds a new Index object.
// id may be partition or table ID, depends on whether the table is a PartitionedTable.
func NewIndex(id int64, indexInfo *model.IndexInfo) table.Index {
func NewIndex(id int64, tblInfo *model.TableInfo, indexInfo *model.IndexInfo) table.Index {
index := &index{
idxInfo: indexInfo,
prefix: tablecodec.EncodeTableIndexPrefix(id, indexInfo.ID),
tblInfo: tblInfo,
// The prefix can't encode from tblInfo.ID, because table partition may change the id to partition id.
prefix: tablecodec.EncodeTableIndexPrefix(id, indexInfo.ID),
}
return index
}
Expand All @@ -132,7 +135,8 @@ func (c *index) truncateIndexValuesIfNeeded(indexedValues []types.Datum) []types
v := &indexedValues[i]
if v.Kind() == types.KindString || v.Kind() == types.KindBytes {
ic := c.idxInfo.Columns[i]
if ic.Tp.Charset == charset.CharsetUTF8 || ic.Tp.Charset == charset.CharsetUTF8MB4 {
colCharset := c.tblInfo.Columns[ic.Offset].Charset
if colCharset == charset.CharsetUTF8 || colCharset == charset.CharsetUTF8MB4 {
val := v.GetBytes()
if ic.Length != types.UnspecifiedLength && utf8.RuneCount(val) > ic.Length {
rs := bytes.Runes(val)
Expand Down
17 changes: 11 additions & 6 deletions table/tables/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *testIndexSuite) TestIndex(c *C) {
},
},
}
index := tables.NewIndex(tblInfo.ID, tblInfo.Indices[0])
index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0])

// Test ununiq index.
txn, err := s.s.Begin()
Expand Down Expand Up @@ -148,7 +148,7 @@ func (s *testIndexSuite) TestIndex(c *C) {
},
},
}
index = tables.NewIndex(tblInfo.ID, tblInfo.Indices[0])
index = tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0])

// Test uniq index.
txn, err = s.s.Begin()
Expand Down Expand Up @@ -210,13 +210,18 @@ func (s *testIndexSuite) TestCombineIndexSeek(c *C) {
ID: 2,
Name: model.NewCIStr("test"),
Columns: []*model.IndexColumn{
{Tp: &types.FieldType{}},
{Tp: &types.FieldType{}},
{Offset: 1},
{Offset: 2},
},
},
},
Columns: []*model.ColumnInfo{
{Offset: 0},
{Offset: 1},
{Offset: 2},
},
}
index := tables.NewIndex(tblInfo.ID, tblInfo.Indices[0])
index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0])

txn, err := s.s.Begin()
c.Assert(err, IsNil)
Expand All @@ -226,7 +231,7 @@ func (s *testIndexSuite) TestCombineIndexSeek(c *C) {
_, err = index.Create(mockCtx, txn, values, 1)
c.Assert(err, IsNil)

index2 := tables.NewIndex(tblInfo.ID, tblInfo.Indices[0])
index2 := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0])
sc := &stmtctx.StatementContext{TimeZone: time.Local}
iter, hit, err := index2.Seek(sc, txn, types.MakeDatums("abc", nil))
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func initTableIndices(t *tableCommon) error {
}

// Use partition ID for index, because tableCommon may be table or partition.
idx := NewIndex(t.partitionID, idxInfo)
idx := NewIndex(t.partitionID, tblInfo, idxInfo)
t.indices = append(t.indices, idx)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion util/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (s *testSuite) TestScan(c *C) {

idxRow1 := &RecordData{Handle: int64(1), Values: types.MakeDatums(int64(10))}
idxRow2 := &RecordData{Handle: int64(2), Values: types.MakeDatums(int64(20))}
kvIndex := tables.NewIndex(tb.Meta().ID, indices[0].Meta())
kvIndex := tables.NewIndex(tb.Meta().ID, tb.Meta(), indices[0].Meta())
sc := &stmtctx.StatementContext{TimeZone: time.Local}
idxRows, nextVals, err := ScanIndexData(sc, txn, kvIndex, idxRow1.Values, 2)
c.Assert(err, IsNil)
Expand Down