-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
plan: get correct columns #5818
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ func (s *testStateChangeSuite) SetUpSuite(c *C) { | |
s.store, err = tikv.NewMockTikvStore() | ||
c.Assert(err, IsNil) | ||
tidb.SetSchemaLease(s.lease) | ||
tidb.SetStatsLease(0) | ||
s.dom, err = tidb.BootstrapSession(s.store) | ||
c.Assert(err, IsNil) | ||
s.se, err = tidb.CreateSession(s.store) | ||
|
@@ -281,3 +282,61 @@ func (t *testExecInfo) execSQL(idx int) error { | |
} | ||
return nil | ||
} | ||
|
||
// TestUpdateOrDelete tests whether the correct columns is used in PhysicalIndexScan's ToPB function. | ||
func (s *testStateChangeSuite) TestUpdateOrDelete(c *C) { | ||
sqls := make([]string, 2) | ||
sqls[0] = "delete from t where c2 = 'a'" | ||
sqls[1] = "update t set c2 = 'c2_update' where c2 = 'a'" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this update use index scan? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add index hint to update statement.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So how to deal with |
||
alterTableSQL := "alter table t add column a int not null default 1 first" | ||
s.runTestInWriteOnly(c, "", alterTableSQL, sqls) | ||
} | ||
|
||
func (s *testStateChangeSuite) runTestInWriteOnly(c *C, tableName, alterTableSQL string, sqls []string) { | ||
defer testleak.AfterTest(c)() | ||
_, err := s.se.Execute(`create table t ( | ||
c1 int primary key, | ||
c2 varchar(64), | ||
c3 enum('N','Y') not null default 'N', | ||
c4 timestamp on update current_timestamp, | ||
key(c2))`) | ||
c.Assert(err, IsNil) | ||
defer s.se.Execute("drop table t") | ||
_, err = s.se.Execute("insert into t values(8, 'a', 'N', '2017-07-01')") | ||
c.Assert(err, IsNil) | ||
// Make sure these sqls use the the plan of index scan. | ||
_, err = s.se.Execute("drop stats t") | ||
c.Assert(err, IsNil) | ||
|
||
callback := &ddl.TestDDLCallback{} | ||
prevState := model.StateNone | ||
var checkErr error | ||
times := 0 | ||
se, err := tidb.CreateSession(s.store) | ||
c.Assert(err, IsNil) | ||
_, err = se.Execute("use test_db_state") | ||
c.Assert(err, IsNil) | ||
callback.OnJobUpdatedExported = func(job *model.Job) { | ||
if job.SchemaState == prevState || checkErr != nil || times >= 3 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why limit the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is used for excluding the effects of other DDL statements. |
||
return | ||
} | ||
times++ | ||
if job.SchemaState != model.StateWriteOnly { | ||
return | ||
} | ||
for _, sql := range sqls { | ||
_, err = se.Execute(sql) | ||
if err != nil { | ||
checkErr = err | ||
break | ||
} | ||
} | ||
} | ||
d := s.dom.DDL() | ||
d.SetHook(callback) | ||
_, err = s.se.Execute(alterTableSQL) | ||
c.Assert(err, IsNil) | ||
c.Assert(errors.ErrorStack(checkErr), Equals, "") | ||
callback = &ddl.TestDDLCallback{} | ||
d.SetHook(callback) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,39 @@ func (t *TableInfo) GetPkColInfo() *ColumnInfo { | |
return nil | ||
} | ||
|
||
// Cols returns the columns of the table in public state. | ||
func (t *TableInfo) Cols() []*ColumnInfo { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation is not efficient. Could you do some pre-calculation? |
||
publicColumns := make([]*ColumnInfo, len(t.Columns)) | ||
maxOffset := -1 | ||
for _, col := range t.Columns { | ||
if col.State != StatePublic { | ||
continue | ||
} | ||
publicColumns[col.Offset] = col | ||
if maxOffset < col.Offset { | ||
maxOffset = col.Offset | ||
} | ||
} | ||
return publicColumns[0 : maxOffset+1] | ||
} | ||
|
||
// WritableCols returns columns of the table in writable states. | ||
// Writable states includes Public, WriteOnly, WriteOnlyReorganization. | ||
func (t *TableInfo) WritableCols() []*ColumnInfo { | ||
writableColumns := make([]*ColumnInfo, len(t.Columns)) | ||
maxOffset := -1 | ||
for _, col := range t.Columns { | ||
if col.State == StateDeleteOnly || col.State == StateDeleteReorganization { | ||
continue | ||
} | ||
writableColumns[col.Offset] = col | ||
if maxOffset < col.Offset { | ||
maxOffset = col.Offset | ||
} | ||
} | ||
return writableColumns[0 : maxOffset+1] | ||
} | ||
|
||
// NewExtraHandleColInfo mocks a column info for extra handle column. | ||
func NewExtraHandleColInfo() *ColumnInfo { | ||
colInfo := &ColumnInfo{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,14 +89,18 @@ func (p *PhysicalTableScan) ToPB(ctx context.Context) (*tipb.Executor, error) { | |
// ToPB implements PhysicalPlan ToPB interface. | ||
func (p *PhysicalIndexScan) ToPB(ctx context.Context) (*tipb.Executor, error) { | ||
columns := make([]*model.ColumnInfo, 0, p.schema.Len()) | ||
tableColumns := p.Table.Cols() | ||
if ctx.GetSessionVars().StmtCtx.InUpdateStmt { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to check it and get writable columns because all the columns in an index must be public. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, now the column in an index must be public. |
||
tableColumns = p.Table.WritableCols() | ||
} | ||
for _, col := range p.schema.Columns { | ||
if col.ID == model.ExtraHandleID { | ||
columns = append(columns, &model.ColumnInfo{ | ||
ID: model.ExtraHandleID, | ||
Name: model.NewCIStr("_rowid"), | ||
}) | ||
} else { | ||
columns = append(columns, p.Table.Columns[col.Position]) | ||
columns = append(columns, tableColumns[col.Position]) | ||
} | ||
} | ||
idxExec := &tipb.IndexScan{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove line 50?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never mind, I read it wrong.