Skip to content

Commit

Permalink
ddl: basic support for invisible index (#15366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deardrops authored Mar 17, 2020
1 parent 8aad651 commit 12aabbd
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 22 deletions.
9 changes: 6 additions & 3 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func (s *testStateChangeSuite) TestShowIndex(c *C) {
checkErr = err1
break
}
checkErr = checkResult(result, testkit.Rows("t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE NULL"))
checkErr = checkResult(result, testkit.Rows("t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES NULL"))
}
}

Expand All @@ -668,7 +668,10 @@ func (s *testStateChangeSuite) TestShowIndex(c *C) {

result, err := s.execQuery(tk, showIndexSQL)
c.Assert(err, IsNil)
err = checkResult(result, testkit.Rows("t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE NULL", "t 1 c2 1 c2 A 0 <nil> <nil> YES BTREE NULL"))
err = checkResult(result, testkit.Rows(
"t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES NULL",
"t 1 c2 1 c2 A 0 <nil> <nil> YES BTREE YES NULL",
))
c.Assert(err, IsNil)
d.(ddl.DDLForTest).SetHook(originalCallback)

Expand All @@ -692,7 +695,7 @@ func (s *testStateChangeSuite) TestShowIndex(c *C) {
c.Assert(err, IsNil)
result, err = s.execQuery(tk, "show index from tr;")
c.Assert(err, IsNil)
err = checkResult(result, testkit.Rows("tr 1 idx1 1 purchased A 0 <nil> <nil> YES BTREE NULL"))
err = checkResult(result, testkit.Rows("tr 1 idx1 1 purchased A 0 <nil> <nil> YES BTREE YES NULL"))
c.Assert(err, IsNil)
}

Expand Down
3 changes: 3 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,9 @@ func buildTableInfo(ctx sessionctx.Context, tableName model.CIStr, cols []*table
if err != nil {
return nil, errors.Trace(err)
}
if constr.Option.Visibility == ast.IndexVisibilityInvisible {
idxInfo.Invisible = true
}
if constr.Option.Tp == model.IndexTypeInvalid {
// Use btree as default index type.
idxInfo.Tp = model.IndexTypeBtree
Expand Down
3 changes: 3 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ func (w *worker) onCreateIndex(d *ddlCtx, t *meta.Meta, job *model.Job, isPK boo
}
if indexOption != nil {
indexInfo.Comment = indexOption.Comment
if indexOption.Visibility == ast.IndexVisibilityInvisible {
indexInfo.Invisible = true
}
if indexOption.Tp == model.IndexTypeInvalid {
// Use btree as default index type.
indexInfo.Tp = model.IndexTypeBtree
Expand Down
13 changes: 11 additions & 2 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ func (e *memtableRetriever) setDataForStatisticsInTable(schema *model.DBInfo, ta
"", // NULLABLE
"BTREE", // INDEX_TYPE
"", // COMMENT
"NULL", // Expression
"", // INDEX_COMMENT
"YES", // IS_VISIBLE
"NULL", // Expression
)
rows = append(rows, record)
}
Expand All @@ -344,13 +345,20 @@ func (e *memtableRetriever) setDataForStatisticsInTable(schema *model.DBInfo, ta
if mysql.HasNotNullFlag(col.Flag) {
nullable = ""
}

visible := "YES"
if index.Invisible {
visible = "NO"
}

colName := col.Name.O
expression := "NULL"
tblCol := table.Columns[col.Offset]
if tblCol.Hidden {
colName = "NULL"
expression = fmt.Sprintf("(%s)", tblCol.GeneratedExprString)
}

record := types.MakeDatums(
infoschema.CatalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
Expand All @@ -367,8 +375,9 @@ func (e *memtableRetriever) setDataForStatisticsInTable(schema *model.DBInfo, ta
nullable, // NULLABLE
"BTREE", // INDEX_TYPE
"", // COMMENT
expression, // Expression
"", // INDEX_COMMENT
visible, // IS_VISIBLE
expression, // Expression
)
rows = append(rows, record)
}
Expand Down
46 changes: 36 additions & 10 deletions executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,28 @@ func (s *seqTestSuite) TestShow(c *C) {
c.Check(r, Equals, expectedRow[i])
}

// test SHOW CREATE TABLE with invisible index
tk.MustExec("drop table if exists t")
tk.MustExec(`create table t (
a int,
b int,
c int UNIQUE KEY,
d int UNIQUE KEY,
index (b) invisible,
index (d) invisible)`)
excepted :=
"t CREATE TABLE `t` (\n" +
" `a` int(11) DEFAULT NULL,\n" +
" `b` int(11) DEFAULT NULL,\n" +
" `c` int(11) DEFAULT NULL,\n" +
" `d` int(11) DEFAULT NULL,\n" +
" KEY `b` (`b`) /*!80000 INVISIBLE */,\n" +
" KEY `d` (`d`) /*!80000 INVISIBLE */,\n" +
" UNIQUE KEY `c` (`c`),\n" +
" UNIQUE KEY `d_2` (`d`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
tk.MustQuery("show create table t").Check(testkit.Rows(excepted))

testSQL = "SHOW VARIABLES LIKE 'character_set_results';"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)
Expand All @@ -292,19 +314,23 @@ func (s *seqTestSuite) TestShow(c *C) {
tk.MustExec(`create index idx5 using hash on show_index (id) using btree comment 'idx';`)
tk.MustExec(`create index idx6 using hash on show_index (id);`)
tk.MustExec(`create index idx7 on show_index (id);`)
tk.MustExec(`create index idx8 on show_index (id) visible;`)
tk.MustExec(`create index idx9 on show_index (id) invisible;`)
tk.MustExec(`create index expr_idx on show_index ((id*2+1))`)
testSQL = "SHOW index from show_index;"
tk.MustQuery(testSQL).Check(testutil.RowsWithSep("|",
"show_index|0|PRIMARY|1|id|A|0|<nil>|<nil>||BTREE| |NULL",
"show_index|1|cIdx|1|c|A|0|<nil>|<nil>|YES|HASH||index_comment_for_cIdx|NULL",
"show_index|1|idx1|1|id|A|0|<nil>|<nil>|YES|HASH| |NULL",
"show_index|1|idx2|1|id|A|0|<nil>|<nil>|YES|BTREE||idx|NULL",
"show_index|1|idx3|1|id|A|0|<nil>|<nil>|YES|HASH||idx|NULL",
"show_index|1|idx4|1|id|A|0|<nil>|<nil>|YES|BTREE||idx|NULL",
"show_index|1|idx5|1|id|A|0|<nil>|<nil>|YES|BTREE||idx|NULL",
"show_index|1|idx6|1|id|A|0|<nil>|<nil>|YES|HASH| |NULL",
"show_index|1|idx7|1|id|A|0|<nil>|<nil>|YES|BTREE| |NULL",
"show_index|1|expr_idx|1|NULL|A|0|<nil>|<nil>|YES|BTREE| |(`id` * 2 + 1)",
"show_index|0|PRIMARY|1|id|A|0|<nil>|<nil>||BTREE| |YES|NULL",
"show_index|1|cIdx|1|c|A|0|<nil>|<nil>|YES|HASH||index_comment_for_cIdx|YES|NULL",
"show_index|1|idx1|1|id|A|0|<nil>|<nil>|YES|HASH| |YES|NULL",
"show_index|1|idx2|1|id|A|0|<nil>|<nil>|YES|BTREE||idx|YES|NULL",
"show_index|1|idx3|1|id|A|0|<nil>|<nil>|YES|HASH||idx|YES|NULL",
"show_index|1|idx4|1|id|A|0|<nil>|<nil>|YES|BTREE||idx|YES|NULL",
"show_index|1|idx5|1|id|A|0|<nil>|<nil>|YES|BTREE||idx|YES|NULL",
"show_index|1|idx6|1|id|A|0|<nil>|<nil>|YES|HASH| |YES|NULL",
"show_index|1|idx7|1|id|A|0|<nil>|<nil>|YES|BTREE| |YES|NULL",
"show_index|1|idx8|1|id|A|0|<nil>|<nil>|YES|BTREE| |YES|NULL",
"show_index|1|idx9|1|id|A|0|<nil>|<nil>|YES|BTREE| |NO|NULL",
"show_index|1|expr_idx|1|NULL|A|0|<nil>|<nil>|YES|BTREE| |YES|(`id` * 2 + 1)",
))

// For show like with escape
Expand Down
14 changes: 14 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ func (e *ShowExec) fetchShowIndex() error {
"BTREE", // Index_type
"", // Comment
"", // Index_comment
"YES", // Index_visible
"NULL", // Expression
})
}
Expand All @@ -562,21 +563,30 @@ func (e *ShowExec) fetchShowIndex() error {
if idx.Meta().Unique {
nonUniq = 0
}

var subPart interface{}
if col.Length != types.UnspecifiedLength {
subPart = col.Length
}

nullVal := "YES"
if idx.Meta().Name.O == mysql.PrimaryKeyName {
nullVal = ""
}

visible := "YES"
if idx.Meta().Invisible {
visible = "NO"
}

colName := col.Name.O
expression := "NULL"
tblCol := tb.Meta().Columns[col.Offset]
if tblCol.Hidden {
colName = "NULL"
expression = fmt.Sprintf("(%s)", tblCol.GeneratedExprString)
}

e.appendRow([]interface{}{
tb.Meta().Name.O, // Table
nonUniq, // Non_unique
Expand All @@ -591,6 +601,7 @@ func (e *ShowExec) fetchShowIndex() error {
idx.Meta().Tp.String(), // Index_type
"", // Comment
idx.Meta().Comment, // Index_comment
visible, // Index_visible
expression, // Expression
})
}
Expand Down Expand Up @@ -852,6 +863,9 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
cols = append(cols, colInfo)
}
fmt.Fprintf(buf, "(%s)", strings.Join(cols, ","))
if idxInfo.Invisible {
fmt.Fprintf(buf, ` /*!80000 INVISIBLE */`)
}
if i != len(publicIndices)-1 {
buf.WriteString(",\n")
}
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ github.com/pingcap/kvproto v0.0.0-20200228095611-2cf9a243b8d5/go.mod h1:IOdRDPLy
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd h1:CV3VsP3Z02MVtdpTMfEgRJ4T9NGgGTxdHpJerent7rM=
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/parser v0.0.0-20200305120128-bde9faa0df84 h1:u5FOwUw9muF8mBTZVV1dQhoAKiEo2Ci54CxN9XchEEY=
github.com/pingcap/parser v0.0.0-20200305120128-bde9faa0df84/go.mod h1:9v0Edh8IbgjGYW2ArJr19E+bvL8zKahsFp+ixWeId+4=
github.com/pingcap/parser v0.0.0-20200315112143-3dc646cd6b52 h1:L8N5zWQOrQyDdpX8n/xYkIe/U8IuL/HYCVK9maD1EsQ=
github.com/pingcap/parser v0.0.0-20200315112143-3dc646cd6b52/go.mod h1:9v0Edh8IbgjGYW2ArJr19E+bvL8zKahsFp+ixWeId+4=
github.com/pingcap/pd/v4 v4.0.0-beta.1.0.20200305072537-61d9f9cc35d3 h1:Yrp99FnjHAEuDrSBql2l0IqCtJX7KwJbTsD5hIArkvk=
Expand Down Expand Up @@ -490,8 +488,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200301222351-066e0c02454c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb h1:iKlO7ROJc6SttHKlxzwGytRtBUqX4VARrNTgP2YLX5M=
golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/tools v0.0.0-20200313205530-4303120df7d8 h1:gkI/wGGwpcG5W4hLCzZNGxA4wzWBGGDStRI1MrjDl2Q=
golang.org/x/tools v0.0.0-20200313205530-4303120df7d8/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 2 additions & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,9 @@ var statisticsCols = []columnInfo{
{name: "NULLABLE", tp: mysql.TypeVarchar, size: 3},
{name: "INDEX_TYPE", tp: mysql.TypeVarchar, size: 16},
{name: "COMMENT", tp: mysql.TypeVarchar, size: 16},
{name: "Expression", tp: mysql.TypeVarchar, size: 64},
{name: "INDEX_COMMENT", tp: mysql.TypeVarchar, size: 1024},
{name: "IS_VISIBLE", tp: mysql.TypeVarchar, size: 3},
{name: "Expression", tp: mysql.TypeVarchar, size: 64},
}

var profilingCols = []columnInfo{
Expand Down
5 changes: 3 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2917,10 +2917,11 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp
case ast.ShowIndex:
names = []string{"Table", "Non_unique", "Key_name", "Seq_in_index",
"Column_name", "Collation", "Cardinality", "Sub_part", "Packed",
"Null", "Index_type", "Comment", "Index_comment", "Expression"}
"Null", "Index_type", "Comment", "Index_comment", "Visible", "Expression"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeLonglong,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong, mysql.TypeLonglong,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar}
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar}
case ast.ShowPlugins:
names = []string{"Name", "Status", "Type", "Library", "License", "Version"}
ftypes = []byte{
Expand Down

0 comments on commit 12aabbd

Please sign in to comment.