Skip to content

Commit

Permalink
Merge pull request pingcap#985 from pingcap/shenli/index-option
Browse files Browse the repository at this point in the history
*: Add index option in IndexInfo
  • Loading branch information
shenli committed Mar 18, 2016
2 parents 6d3eaa1 + ce96978 commit 3968a09
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 52 deletions.
22 changes: 12 additions & 10 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package ast

import (
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/util/types"
)

Expand Down Expand Up @@ -198,15 +199,6 @@ func (n *ColumnOption) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IndexType is the type of index
type IndexType int

// IndexTypes
const (
IndexTypeBtree = iota + 1
IndexTypeHash
)

// IndexOption is the index options.
// KEY_BLOCK_SIZE [=] value
// | index_type
Expand All @@ -217,7 +209,7 @@ type IndexOption struct {
node

KeyBlockSize uint64
Tp IndexType
Tp model.IndexType
Comment string
}

Expand Down Expand Up @@ -259,6 +251,9 @@ type Constraint struct {

// Used for foreign key.
Refer *ReferenceDef

// Index Options
Option *IndexOption
}

// Accept implements Node Accept interface.
Expand All @@ -282,6 +277,13 @@ func (n *Constraint) Accept(v Visitor) (Node, bool) {
}
n.Refer = node.(*ReferenceDef)
}
if n.Option != nil {
node, ok := n.Option.Accept(v)
if !ok {
return n, false
}
n.Option = node.(*IndexOption)
}
return v.Leave(n)
}

Expand Down
11 changes: 9 additions & 2 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
type DDL interface {
CreateSchema(ctx context.Context, name model.CIStr, charsetInfo *ast.CharsetOpt) error
DropSchema(ctx context.Context, schema model.CIStr) error
CreateTable(ctx context.Context, ident ast.Ident, cols []*ast.ColumnDef, constrs []*ast.Constraint) error
CreateTable(ctx context.Context, ident ast.Ident, cols []*ast.ColumnDef, constrs []*ast.Constraint, options []*ast.TableOption) error
DropTable(ctx context.Context, tableIdent ast.Ident) (err error)
CreateIndex(ctx context.Context, tableIdent ast.Ident, unique bool, indexName model.CIStr, columnNames []*ast.IndexColName) error
DropIndex(ctx context.Context, tableIdent ast.Ident, indexName model.CIStr) error
Expand Down Expand Up @@ -680,6 +680,13 @@ func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*column.Col, constrai
case ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex:
idxInfo.Unique = true
}
if constr.Option != nil {
idxInfo.Comment = constr.Option.Comment
idxInfo.Tp = constr.Option.Tp
} else {
// Use btree as default index type.
idxInfo.Tp = model.IndexTypeBtree
}
idxInfo.ID, err = d.genGlobalID()
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -689,7 +696,7 @@ func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*column.Col, constrai
return
}

func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.ColumnDef, constraints []*ast.Constraint) (err error) {
func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.ColumnDef, constraints []*ast.Constraint, options []*ast.TableOption) (err error) {
is := d.GetInformationSchema()
schema, ok := is.SchemaByName(ident.Schema)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (e *DDLExec) executeCreateDatabase(s *ast.CreateDatabaseStmt) error {

func (e *DDLExec) executeCreateTable(s *ast.CreateTableStmt) error {
ident := ast.Ident{Schema: s.Table.Schema, Name: s.Table.Name}
err := sessionctx.GetDomain(e.ctx).DDL().CreateTable(e.ctx, ident, s.Cols, s.Constraints)
err := sessionctx.GetDomain(e.ctx).DDL().CreateTable(e.ctx, ident, s.Cols, s.Constraints, s.Options)
if terror.ErrorEqual(err, infoschema.TableExists) {
if s.IfNotExists {
return nil
Expand Down
4 changes: 2 additions & 2 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ func (e *ShowExec) fetchShowIndex() error {
subPart, // Sub_part
nil, // Packed
"YES", // Null
"BTREE", // Index_type
idx.Tp.String(), // Index_type
"", // Comment
"", // Index_comment
idx.Comment, // Index_comment
)
e.rows = append(e.rows, &Row{Data: data})
}
Expand Down
14 changes: 14 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,18 @@ func (s *testSuite) TestShow(c *C) {
testSQL = "SHOW VARIABLES LIKE 'character_set_results';"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)

// Test case for index type and comment
tk.MustExec(`create table show_index (c int, index cIdx using hash (c) comment "index_comment_for_cIdx");`)
testSQL = "SHOW index from show_index;"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)
expectedRow := []interface{}{
"show_index", int64(1), "cIdx", int64(1), "c", "utf8_bin",
int64(0), nil, nil, "YES", "HASH", "", "index_comment_for_cIdx"}
row := result.Rows()[0]
c.Check(row, HasLen, len(expectedRow))
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
}
23 changes: 23 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type TableInfo struct {
Indices []*IndexInfo `json:"index_info"`
State SchemaState `json:"state"`
PKIsHandle bool `json:"pk_is_handle"`
Comment string `json:"comment"`
}

// Clone clones TableInfo.
Expand Down Expand Up @@ -114,6 +115,26 @@ func (i *IndexColumn) Clone() *IndexColumn {
return &ni
}

// IndexType is the type of index
type IndexType int

// String implements Stringer interface.
func (t IndexType) String() string {
switch t {
case IndexTypeBtree:
return "BTREE"
case IndexTypeHash:
return "HASH"
}
return ""
}

// IndexTypes
const (
IndexTypeBtree IndexType = iota + 1
IndexTypeHash
)

// IndexInfo provides meta data describing a DB index.
// It corresponds to the statement `CREATE INDEX Name ON Table (Column);`
// See: https://dev.mysql.com/doc/refman/5.7/en/create-index.html
Expand All @@ -125,6 +146,8 @@ type IndexInfo struct {
Unique bool `json:"is_unique"` // Whether the index is unique.
Primary bool `json:"is_primary"` // Whether the index is primary key.
State SchemaState `json:"state"`
Comment string `json:"comment"` // Comment
Tp IndexType `json:"index_type"` // Index type: Btree or Hash
}

// Clone clones IndexInfo.
Expand Down
Loading

0 comments on commit 3968a09

Please sign in to comment.