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

expression: move FindIndexByName to parser #9951

Merged
merged 7 commits into from
Apr 1, 2019
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: 1 addition & 2 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/pingcap/tidb/ddl"
testddlutil "github.com/pingcap/tidb/ddl/testutil"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
Expand Down Expand Up @@ -450,7 +449,7 @@ func (s *testDBSuite) TestCancelDropIndex(c *C) {
rs.Close()
}
t := s.testGetTable(c, "t")
indexInfo := expression.FindIndexByName("idx_c2", t.Meta().Indices)
indexInfo := t.Meta().FindIndexByName("idx_c2")
if testCase.cancelSucc {
c.Assert(checkErr, IsNil)
c.Assert(err, NotNil)
Expand Down
4 changes: 2 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2752,7 +2752,7 @@ func (d *ddl) CreateIndex(ctx sessionctx.Context, ti ast.Ident, unique bool, ind
indexName = getAnonymousIndex(t, idxColNames[0].Column.Name)
}

if indexInfo := expression.FindIndexByName(indexName.L, t.Meta().Indices); indexInfo != nil {
if indexInfo := t.Meta().FindIndexByName(indexName.L); indexInfo != nil {
return ErrDupKeyName.GenWithStack("index already exist %s", indexName)
}

Expand Down Expand Up @@ -2893,7 +2893,7 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
return errors.Trace(infoschema.ErrTableNotExists.GenWithStackByArgs(ti.Schema, ti.Name))
}

if indexInfo := expression.FindIndexByName(indexName.L, t.Meta().Indices); indexInfo == nil {
if indexInfo := t.Meta().FindIndexByName(indexName.L); indexInfo == nil {
return ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
}

Expand Down
10 changes: 5 additions & 5 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func dropIndexColumnFlag(tblInfo *model.TableInfo, indexInfo *model.IndexInfo) {
}

func validateRenameIndex(from, to model.CIStr, tbl *model.TableInfo) (ignore bool, err error) {
if fromIdx := expression.FindIndexByName(from.L, tbl.Indices); fromIdx == nil {
if fromIdx := tbl.FindIndexByName(from.L); fromIdx == nil {
return false, errors.Trace(infoschema.ErrKeyNotExists.GenWithStackByArgs(from.O, tbl.Name))
}
// Take case-sensitivity into account, if `FromKey` and `ToKey` are the same, nothing need to be changed
Expand All @@ -195,7 +195,7 @@ func validateRenameIndex(from, to model.CIStr, tbl *model.TableInfo) (ignore boo
// If spec.FromKey.L == spec.ToKey.L, we operate on the same index(case-insensitive) and change its name (case-sensitive)
// e.g: from `inDex` to `IndEX`. Otherwise, we try to rename an index to another different index which already exists,
// that's illegal by rule.
if toIdx := expression.FindIndexByName(to.L, tbl.Indices); toIdx != nil && from.L != to.L {
if toIdx := tbl.FindIndexByName(to.L); toIdx != nil && from.L != to.L {
return false, errors.Trace(infoschema.ErrKeyNameDuplicate.GenWithStackByArgs(toIdx.Name.O))
}
return false, nil
Expand All @@ -207,7 +207,7 @@ func onRenameIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) {
return ver, errors.Trace(err)
}

idx := expression.FindIndexByName(from.L, tblInfo.Indices)
idx := tblInfo.FindIndexByName(from.L)
idx.Name = to
if ver, err = updateVersionAndTableInfo(t, job, tblInfo, true); err != nil {
job.State = model.JobStateCancelled
Expand Down Expand Up @@ -246,7 +246,7 @@ func (w *worker) onCreateIndex(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int
return ver, errors.Trace(err)
}

indexInfo := expression.FindIndexByName(indexName.L, tblInfo.Indices)
indexInfo := tblInfo.FindIndexByName(indexName.L)
if indexInfo != nil && indexInfo.State == model.StatePublic {
job.State = model.JobStateCancelled
return ver, ErrDupKeyName.GenWithStack("index already exist %s", indexName)
Expand Down Expand Up @@ -415,7 +415,7 @@ func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.Inde
return nil, nil, errors.Trace(err)
}

indexInfo := expression.FindIndexByName(indexName.L, tblInfo.Indices)
indexInfo := tblInfo.FindIndexByName(indexName.L)
if indexInfo == nil {
job.State = model.JobStateCancelled
return nil, nil, ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
Expand Down
5 changes: 2 additions & 3 deletions ddl/rollingback.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/util/logutil"
Expand Down Expand Up @@ -68,7 +67,7 @@ func convertNotStartAddIdxJob2RollbackJob(t *meta.Meta, job *model.Job, occuredE
return ver, errors.Trace(err)
}

indexInfo := expression.FindIndexByName(indexName.L, tblInfo.Indices)
indexInfo := tblInfo.FindIndexByName(indexName.L)
if indexInfo == nil {
job.State = model.JobStateCancelled
return ver, errCancelledDDLJob
Expand Down Expand Up @@ -213,7 +212,7 @@ func rollingbackRenameIndex(t *meta.Meta, job *model.Job) (ver int64, err error)
return ver, errors.Trace(err)
}
// Here rename index is done in a transaction, if the job is not completed, it can be canceled.
idx := expression.FindIndexByName(from.L, tblInfo.Indices)
idx := tblInfo.FindIndexByName(from.L)
if idx.State == model.StatePublic {
job.State = model.JobStateCancelled
return ver, errCancelledDDLJob
Expand Down
25 changes: 8 additions & 17 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ func (s *testSuite1) TestAdminCheckIndexRange(c *C) {
result.Check(testkit.Rows("-1 hi 4", "2 cd 2"))
}

func findIndexByName(idxName string, indices []*model.IndexInfo) *model.IndexInfo {
for _, idx := range indices {
if idx.Name.L == idxName {
return idx
}
}
return nil
}

func (s *testSuite2) TestAdminRecoverIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -91,7 +82,7 @@ func (s *testSuite2) TestAdminRecoverIndex(c *C) {
c.Assert(err, IsNil)

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

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

Expand Down Expand Up @@ -244,9 +235,9 @@ func (s *testSuite2) TestAdminCleanupIndex(c *C) {
c.Assert(err, IsNil)

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

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

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

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

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

txn, err := s.store.Begin()
Expand Down Expand Up @@ -544,7 +535,7 @@ func (s *testSuite2) TestAdminCheckWithSnapshot(c *C) {
c.Assert(err, IsNil)

tblInfo := tbl.Meta()
idxInfo := findIndexByName("a", tblInfo.Indices)
idxInfo := tblInfo.FindIndexByName("a")
idxOpr := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo)
txn, err := s.store.Begin()
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@ func (s *testSuite2) TestReplaceLog(c *C) {
tbl, err := is.TableByName(dbName, tblName)
c.Assert(err, IsNil)
tblInfo := tbl.Meta()
idxInfo := findIndexByName("b", tblInfo.Indices)
idxInfo := tblInfo.FindIndexByName("b")
indexOpr := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo)

txn, err := s.store.Begin()
Expand Down
11 changes: 0 additions & 11 deletions expression/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
)

// KeyInfo stores the columns of one unique key or primary key.
Expand Down Expand Up @@ -260,13 +259,3 @@ func MergeSchema(lSchema, rSchema *Schema) *Schema {
func NewSchema(cols ...*Column) *Schema {
return &Schema{Columns: cols, TblID2Handle: make(map[int64][]*Column)}
}

// FindIndexByName finds index by name.
func FindIndexByName(idxName string, indices []*model.IndexInfo) *model.IndexInfo {
for _, idx := range indices {
if idx.Name.L == idxName {
return idx
}
}
return nil
}
13 changes: 0 additions & 13 deletions expression/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,3 @@ func (s *testEvalSuite) TestSchemaMergeSchema(c *C) {
c.Assert(schema.Columns[i+len(lSchema.Columns)].ColName, Equals, rSchema.Columns[i].ColName)
}
}

func (s *testEvalSuite) TestFindIndexByName(c *C) {
indexs := make([]*model.IndexInfo, 3)
indexs[0] = &model.IndexInfo{Name: model.NewCIStr("idx1")}
indexs[1] = &model.IndexInfo{Name: model.NewCIStr("idx2")}
indexs[2] = &model.IndexInfo{Name: model.NewCIStr("idx3")}
idx := FindIndexByName("idx1", indexs)
c.Assert(idx, NotNil)
c.Assert(idx.Name.L, Equals, "idx1")
indexs = indexs[:0]
idx = FindIndexByName("idx1", indexs)
c.Assert(idx, IsNil)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e
github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596
github.com/pingcap/parser v0.0.0-20190331024200-2f120db0a482
github.com/pingcap/parser v0.0.0-20190401032248-1f037b1a2fa9
github.com/pingcap/pd v2.1.0-rc.4+incompatible
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible
github.com/pingcap/tipb v0.0.0-20190107072121-abbec73437b7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562 h1:32oF1/8lVnBR2JV
github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562/go.mod h1:QMdbTAXCHzzygQzqcG9uVUgU2fKeSN1GmfMiykdSzzY=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw=
github.com/pingcap/parser v0.0.0-20190331024200-2f120db0a482 h1:7WrNaktGabxJ/FUtML+wHEGPszCA1fXjWe82gt5Q/Eo=
github.com/pingcap/parser v0.0.0-20190331024200-2f120db0a482/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/parser v0.0.0-20190401032248-1f037b1a2fa9 h1:E87GLoLGO05akeyNnRugcm7m5fCdIk0lBn2xrmnN5iY=
github.com/pingcap/parser v0.0.0-20190401032248-1f037b1a2fa9/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/pd v2.1.0-rc.4+incompatible h1:/buwGk04aHO5odk/+O8ZOXGs4qkUjYTJ2UpCJXna8NE=
github.com/pingcap/pd v2.1.0-rc.4+incompatible/go.mod h1:nD3+EoYes4+aNNODO99ES59V83MZSI+dFbhyr667a0E=
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible h1:MkWCxgZpJBgY2f4HtwWMMFzSBb3+JPzeJgF3VrXE/bU=
Expand Down
2 changes: 1 addition & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ func (b *PlanBuilder) buildAnalyzeIndex(as *ast.AnalyzeTableStmt) (Plan, error)
return nil, err
}
for _, idxName := range as.IndexNames {
idx := expression.FindIndexByName(idxName.L, tblInfo.Indices)
idx := tblInfo.FindIndexByName(idxName.L)
if idx == nil || idx.State != model.StatePublic {
return nil, ErrAnalyzeMissIndex.GenWithStackByArgs(idxName.O, tblInfo.Name.O)
}
Expand Down