Skip to content

Commit fafc39d

Browse files
zimulalashenli
authored andcommitted
plan: get correct columns (#5818)
Use the correct columns in PhysicalIndexScan's ToPB function.
1 parent b43c41c commit fafc39d

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

ddl/ddl_db_change_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (s *testStateChangeSuite) SetUpSuite(c *C) {
4848
s.store, err = tikv.NewMockTikvStore()
4949
c.Assert(err, IsNil)
5050
tidb.SetSchemaLease(s.lease)
51+
tidb.SetStatsLease(0)
5152
s.dom, err = tidb.BootstrapSession(s.store)
5253
c.Assert(err, IsNil)
5354
s.se, err = tidb.CreateSession(s.store)
@@ -281,3 +282,61 @@ func (t *testExecInfo) execSQL(idx int) error {
281282
}
282283
return nil
283284
}
285+
286+
// TestUpdateOrDelete tests whether the correct columns is used in PhysicalIndexScan's ToPB function.
287+
func (s *testStateChangeSuite) TestUpdateOrDelete(c *C) {
288+
sqls := make([]string, 2)
289+
sqls[0] = "delete from t where c2 = 'a'"
290+
sqls[1] = "update t use index(c2) set c2 = 'c2_update' where c2 = 'a'"
291+
alterTableSQL := "alter table t add column a int not null default 1 first"
292+
s.runTestInWriteOnly(c, "", alterTableSQL, sqls)
293+
}
294+
295+
func (s *testStateChangeSuite) runTestInWriteOnly(c *C, tableName, alterTableSQL string, sqls []string) {
296+
defer testleak.AfterTest(c)()
297+
_, err := s.se.Execute(`create table t (
298+
c1 int primary key,
299+
c2 varchar(64),
300+
c3 enum('N','Y') not null default 'N',
301+
c4 timestamp on update current_timestamp,
302+
key(c2))`)
303+
c.Assert(err, IsNil)
304+
defer s.se.Execute("drop table t")
305+
_, err = s.se.Execute("insert into t values(8, 'a', 'N', '2017-07-01')")
306+
c.Assert(err, IsNil)
307+
// Make sure these sqls use the the plan of index scan.
308+
_, err = s.se.Execute("drop stats t")
309+
c.Assert(err, IsNil)
310+
311+
callback := &ddl.TestDDLCallback{}
312+
prevState := model.StateNone
313+
var checkErr error
314+
times := 0
315+
se, err := tidb.CreateSession(s.store)
316+
c.Assert(err, IsNil)
317+
_, err = se.Execute("use test_db_state")
318+
c.Assert(err, IsNil)
319+
callback.OnJobUpdatedExported = func(job *model.Job) {
320+
if job.SchemaState == prevState || checkErr != nil || times >= 3 {
321+
return
322+
}
323+
times++
324+
if job.SchemaState != model.StateWriteOnly {
325+
return
326+
}
327+
for _, sql := range sqls {
328+
_, err = se.Execute(sql)
329+
if err != nil {
330+
checkErr = err
331+
break
332+
}
333+
}
334+
}
335+
d := s.dom.DDL()
336+
d.SetHook(callback)
337+
_, err = s.se.Execute(alterTableSQL)
338+
c.Assert(err, IsNil)
339+
c.Assert(errors.ErrorStack(checkErr), Equals, "")
340+
callback = &ddl.TestDDLCallback{}
341+
d.SetHook(callback)
342+
}

model/model.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ func (t *TableInfo) GetPkColInfo() *ColumnInfo {
172172
return nil
173173
}
174174

175+
// Cols returns the columns of the table in public state.
176+
func (t *TableInfo) Cols() []*ColumnInfo {
177+
publicColumns := make([]*ColumnInfo, len(t.Columns))
178+
maxOffset := -1
179+
for _, col := range t.Columns {
180+
if col.State != StatePublic {
181+
continue
182+
}
183+
publicColumns[col.Offset] = col
184+
if maxOffset < col.Offset {
185+
maxOffset = col.Offset
186+
}
187+
}
188+
return publicColumns[0 : maxOffset+1]
189+
}
190+
175191
// NewExtraHandleColInfo mocks a column info for extra handle column.
176192
func NewExtraHandleColInfo() *ColumnInfo {
177193
colInfo := &ColumnInfo{

plan/plan_to_pb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@ func (p *PhysicalTableScan) ToPB(ctx context.Context) (*tipb.Executor, error) {
8989
// ToPB implements PhysicalPlan ToPB interface.
9090
func (p *PhysicalIndexScan) ToPB(ctx context.Context) (*tipb.Executor, error) {
9191
columns := make([]*model.ColumnInfo, 0, p.schema.Len())
92+
tableColumns := p.Table.Cols()
9293
for _, col := range p.schema.Columns {
9394
if col.ID == model.ExtraHandleID {
9495
columns = append(columns, &model.ColumnInfo{
9596
ID: model.ExtraHandleID,
9697
Name: model.NewCIStr("_rowid"),
9798
})
9899
} else {
99-
columns = append(columns, p.Table.Columns[col.Position])
100+
columns = append(columns, tableColumns[col.Position])
100101
}
101102
}
102103
idxExec := &tipb.IndexScan{

0 commit comments

Comments
 (0)