Skip to content

Commit

Permalink
*: add clustered index info in show index from stmt (pingcap#23329)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored Mar 16, 2021
1 parent 26ccbe9 commit 9c48b24
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 35 deletions.
10 changes: 5 additions & 5 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ HashAgg 24000.00 root group by:Column#10, funcs:firstrow(Column#11)->Column#10
└─IndexReader 10000.00 root index:IndexFullScan
└─IndexFullScan 10000.00 cop[tikv] table:t2, index:c1(c1) keep order:true, stats:pseudo
select * from information_schema.tidb_indexes where table_name='t4';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE
test t4 0 PRIMARY 1 a NULL NULL 0 YES
test t4 1 idx 1 a NULL NULL 1 YES
test t4 1 idx 2 b NULL NULL 1 YES
test t4 1 expr_idx 1 NULL NULL (`a` + `b` + 1) 2 YES
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED
test t4 0 PRIMARY 1 a NULL NULL 0 YES YES
test t4 1 idx 1 a NULL NULL 1 YES NO
test t4 1 idx 2 b NULL NULL 1 YES NO
test t4 1 expr_idx 1 NULL NULL (`a` + `b` + 1) 2 YES NO
explain format = 'brief' select count(1) from (select count(1) from (select * from t1 where c3 = 100) k) k2;
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(1)->Column#5
Expand Down
52 changes: 48 additions & 4 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,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 YES NULL"))
checkErr = checkResult(result, testkit.Rows("t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES NULL NO"))
}
}

Expand All @@ -974,8 +974,8 @@ 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 YES NULL",
"t 1 c2 1 c2 A 0 <nil> <nil> YES BTREE YES NULL",
"t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES NULL NO",
"t 1 c2 1 c2 A 0 <nil> <nil> YES BTREE YES NULL NO",
))
c.Assert(err, IsNil)
d.(ddl.DDLForTest).SetHook(originalCallback)
Expand Down Expand Up @@ -1003,8 +1003,52 @@ 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 YES NULL"))
err = checkResult(result, testkit.Rows("tr 1 idx1 1 purchased A 0 <nil> <nil> YES BTREE YES NULL NO"))
c.Assert(err, IsNil)

_, err = s.se.Execute(context.Background(), "drop table if exists tr")
c.Assert(err, IsNil)
_, err = s.se.Execute(context.Background(), "create table tr(id int primary key clustered, v int, key vv(v))")
c.Assert(err, IsNil)
result, err = s.execQuery(tk, "show index from tr")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES NULL YES", "tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES NULL NO")), IsNil)
result, err = s.execQuery(tk, "select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("PRIMARY YES", "vv NO")), IsNil)

_, err = s.se.Execute(context.Background(), "drop table if exists tr")
c.Assert(err, IsNil)
_, err = s.se.Execute(context.Background(), "create table tr(id int primary key nonclustered, v int, key vv(v))")
c.Assert(err, IsNil)
result, err = s.execQuery(tk, "show index from tr")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES NULL NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES NULL NO")), IsNil)
result, err = s.execQuery(tk, "select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("PRIMARY NO", "vv NO")), IsNil)

_, err = s.se.Execute(context.Background(), "drop table if exists tr")
c.Assert(err, IsNil)
_, err = s.se.Execute(context.Background(), "create table tr(id char(100) primary key clustered, v int, key vv(v))")
c.Assert(err, IsNil)
result, err = s.execQuery(tk, "show index from tr")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES NULL NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES NULL YES")), IsNil)
result, err = s.execQuery(tk, "select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("PRIMARY YES", "vv NO")), IsNil)

_, err = s.se.Execute(context.Background(), "drop table if exists tr")
c.Assert(err, IsNil)
_, err = s.se.Execute(context.Background(), "create table tr(id char(100) primary key nonclustered, v int, key vv(v))")
c.Assert(err, IsNil)
result, err = s.execQuery(tk, "show index from tr")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES NULL NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES NULL NO")), IsNil)
result, err = s.execQuery(tk, "select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name")
c.Assert(err, IsNil)
c.Assert(checkResult(result, testkit.Rows("PRIMARY NO", "vv NO")), IsNil)
}

func (s *testStateChangeSuite) TestParallelAlterModifyColumn(c *C) {
Expand Down
6 changes: 6 additions & 0 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,18 @@ func (e *memtableRetriever) setDataFromIndexes(ctx sessionctx.Context, schemas [
nil, // Expression
0, // INDEX_ID
"YES", // IS_VISIBLE
"YES", // CLUSTERED
)
rows = append(rows, record)
}
for _, idxInfo := range tb.Indices {
if idxInfo.State != model.StatePublic {
continue
}
isClustered := "NO"
if tb.IsCommonHandle && idxInfo.Primary {
isClustered = "YES"
}
for i, col := range idxInfo.Columns {
nonUniq := 1
if idxInfo.Unique {
Expand Down Expand Up @@ -881,6 +886,7 @@ func (e *memtableRetriever) setDataFromIndexes(ctx sessionctx.Context, schemas [
expression, // Expression
idxInfo.ID, // INDEX_ID
visible, // IS_VISIBLE
isClustered, // CLUSTERED
)
rows = append(rows, record)
}
Expand Down
24 changes: 12 additions & 12 deletions executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,18 @@ func (s *seqTestSuite) TestShow(c *C) {
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| |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>||HASH| |YES|NULL",
"show_index|1|idx2|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|NULL",
"show_index|1|idx3|1|id|A|0|<nil>|<nil>||HASH||idx|YES|NULL",
"show_index|1|idx4|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|NULL",
"show_index|1|idx5|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|NULL",
"show_index|1|idx6|1|id|A|0|<nil>|<nil>||HASH| |YES|NULL",
"show_index|1|idx7|1|id|A|0|<nil>|<nil>||BTREE| |YES|NULL",
"show_index|1|idx8|1|id|A|0|<nil>|<nil>||BTREE| |YES|NULL",
"show_index|1|idx9|1|id|A|0|<nil>|<nil>||BTREE| |NO|NULL",
"show_index|1|expr_idx|1|NULL|A|0|<nil>|<nil>|YES|BTREE| |YES|(`id` * 2 + 1)",
"show_index|0|PRIMARY|1|id|A|0|<nil>|<nil>||BTREE| |YES|NULL|YES",
"show_index|1|cIdx|1|c|A|0|<nil>|<nil>|YES|HASH||index_comment_for_cIdx|YES|NULL|NO",
"show_index|1|idx1|1|id|A|0|<nil>|<nil>||HASH| |YES|NULL|NO",
"show_index|1|idx2|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|NULL|NO",
"show_index|1|idx3|1|id|A|0|<nil>|<nil>||HASH||idx|YES|NULL|NO",
"show_index|1|idx4|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|NULL|NO",
"show_index|1|idx5|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|NULL|NO",
"show_index|1|idx6|1|id|A|0|<nil>|<nil>||HASH| |YES|NULL|NO",
"show_index|1|idx7|1|id|A|0|<nil>|<nil>||BTREE| |YES|NULL|NO",
"show_index|1|idx8|1|id|A|0|<nil>|<nil>||BTREE| |YES|NULL|NO",
"show_index|1|idx9|1|id|A|0|<nil>|<nil>||BTREE| |NO|NULL|NO",
"show_index|1|expr_idx|1|NULL|A|0|<nil>|<nil>|YES|BTREE| |YES|(`id` * 2 + 1)|NO",
))

// For show like with escape
Expand Down
6 changes: 6 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,18 @@ func (e *ShowExec) fetchShowIndex() error {
"", // Index_comment
"YES", // Index_visible
"NULL", // Expression
"YES", // Clustered
})
}
for _, idx := range tb.Indices() {
idxInfo := idx.Meta()
if idxInfo.State != model.StatePublic {
continue
}
isClustered := "NO"
if tb.Meta().IsCommonHandle && idxInfo.Primary {
isClustered = "YES"
}
for i, col := range idxInfo.Columns {
nonUniq := 1
if idx.Meta().Unique {
Expand Down Expand Up @@ -627,6 +632,7 @@ func (e *ShowExec) fetchShowIndex() error {
idx.Meta().Comment, // Index_comment
visible, // Index_visible
expression, // Expression
isClustered, // Clustered
})
}
}
Expand Down
24 changes: 12 additions & 12 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,20 +1235,20 @@ func (s *testSuite5) TestIssue19507(c *C) {
tk.MustExec("use test")
tk.MustExec("CREATE TABLE t2(a int primary key, b int unique, c int not null, unique index (c));")
tk.MustQuery("SHOW INDEX IN t2;").Check(
testutil.RowsWithSep("|", "t2|0|PRIMARY|1|a|A|0|<nil>|<nil>||BTREE|||YES|NULL",
"t2|0|c|1|c|A|0|<nil>|<nil>||BTREE|||YES|NULL",
"t2|0|b|1|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL"))
testutil.RowsWithSep("|", "t2|0|PRIMARY|1|a|A|0|<nil>|<nil>||BTREE|||YES|NULL|YES",
"t2|0|c|1|c|A|0|<nil>|<nil>||BTREE|||YES|NULL|NO",
"t2|0|b|1|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL|NO"))

tk.MustExec("CREATE INDEX t2_b_c_index ON t2 (b, c);")
tk.MustExec("CREATE INDEX t2_c_b_index ON t2 (c, b);")
tk.MustQuery("SHOW INDEX IN t2;").Check(
testutil.RowsWithSep("|", "t2|0|PRIMARY|1|a|A|0|<nil>|<nil>||BTREE|||YES|NULL",
"t2|0|c|1|c|A|0|<nil>|<nil>||BTREE|||YES|NULL",
"t2|0|b|1|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL",
"t2|1|t2_b_c_index|1|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL",
"t2|1|t2_b_c_index|2|c|A|0|<nil>|<nil>||BTREE|||YES|NULL",
"t2|1|t2_c_b_index|1|c|A|0|<nil>|<nil>||BTREE|||YES|NULL",
"t2|1|t2_c_b_index|2|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL"))
testutil.RowsWithSep("|", "t2|0|PRIMARY|1|a|A|0|<nil>|<nil>||BTREE|||YES|NULL|YES",
"t2|0|c|1|c|A|0|<nil>|<nil>||BTREE|||YES|NULL|NO",
"t2|0|b|1|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL|NO",
"t2|1|t2_b_c_index|1|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL|NO",
"t2|1|t2_b_c_index|2|c|A|0|<nil>|<nil>||BTREE|||YES|NULL|NO",
"t2|1|t2_c_b_index|1|c|A|0|<nil>|<nil>||BTREE|||YES|NULL|NO",
"t2|1|t2_c_b_index|2|b|A|0|<nil>|<nil>|YES|BTREE|||YES|NULL|NO"))
}

// TestShowPerformanceSchema tests for Issue 19231
Expand All @@ -1258,6 +1258,6 @@ func (s *testSuite5) TestShowPerformanceSchema(c *C) {
// However, its not possible to create a new performance_schema table since its a special in memory table.
// Instead the test below uses the default index on the table.
tk.MustQuery("SHOW INDEX FROM performance_schema.events_statements_summary_by_digest").Check(
testkit.Rows("events_statements_summary_by_digest 0 SCHEMA_NAME 1 SCHEMA_NAME A 0 <nil> <nil> YES BTREE YES NULL",
"events_statements_summary_by_digest 0 SCHEMA_NAME 2 DIGEST A 0 <nil> <nil> YES BTREE YES NULL"))
testkit.Rows("events_statements_summary_by_digest 0 SCHEMA_NAME 1 SCHEMA_NAME A 0 <nil> <nil> YES BTREE YES NULL NO",
"events_statements_summary_by_digest 0 SCHEMA_NAME 2 DIGEST A 0 <nil> <nil> YES BTREE YES NULL NO"))
}
1 change: 1 addition & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ var tableTiDBIndexesCols = []columnInfo{
{name: "Expression", tp: mysql.TypeVarchar, size: 64},
{name: "INDEX_ID", tp: mysql.TypeLonglong, size: 21},
{name: "IS_VISIBLE", tp: mysql.TypeVarchar, size: 64},
{name: "CLUSTERED", tp: mysql.TypeVarchar, size: 64},
}

var slowQueryCols = []columnInfo{
Expand Down
4 changes: 2 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3646,11 +3646,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", "Visible", "Expression"}
"Null", "Index_type", "Comment", "Index_comment", "Visible", "Expression", "Clustered"}
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}
case ast.ShowPlugins:
names = []string{"Name", "Status", "Type", "Library", "License", "Version"}
ftypes = []byte{
Expand Down

0 comments on commit 9c48b24

Please sign in to comment.