Skip to content

Commit

Permalink
executor: Add Global to SHOW INDEX output (#56028)
Browse files Browse the repository at this point in the history
ref #55452
  • Loading branch information
samba-rgb authored Sep 19, 2024
1 parent beee242 commit 66d8cdc
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 110 deletions.
16 changes: 8 additions & 8 deletions pkg/ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ func TestShowIndex(t *testing.T) {
}
rows := tk.ResultSetToResult(result, fmt.Sprintf("sql:%s", showIndexSQL))
got := fmt.Sprintf("%s", rows.Rows())
need := fmt.Sprintf("%s", testkit.Rows("t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES <nil> NO"))
need := fmt.Sprintf("%s", testkit.Rows("t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES <nil> NO NO"))
if got != need {
checkErr = fmt.Errorf("need %v, but got %v", need, got)
}
Expand All @@ -857,8 +857,8 @@ func TestShowIndex(t *testing.T) {
require.NoError(t, checkErr)

tk.MustQuery(showIndexSQL).Check(testkit.Rows(
"t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES <nil> NO",
"t 1 c2 1 c2 A 0 <nil> <nil> YES BTREE YES <nil> NO",
"t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES <nil> NO NO",
"t 1 c2 1 c2 A 0 <nil> <nil> YES BTREE YES <nil> NO NO",
))
testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/onJobUpdated")

Expand All @@ -875,26 +875,26 @@ func TestShowIndex(t *testing.T) {
partition p5 values less than (2015)
);`)
tk.MustExec("create index idx1 on tr (purchased);")
tk.MustQuery("show index from tr;").Check(testkit.Rows("tr 1 idx1 1 purchased A 0 <nil> <nil> YES BTREE YES <nil> NO"))
tk.MustQuery("show index from tr;").Check(testkit.Rows("tr 1 idx1 1 purchased A 0 <nil> <nil> YES BTREE YES <nil> NO NO"))

tk.MustExec("drop table if exists tr")
tk.MustExec("create table tr(id int primary key clustered, v int, key vv(v))")
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> YES", "tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO"))
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> YES NO", "tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO NO"))
tk.MustQuery("select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name").Check(testkit.Rows("PRIMARY YES", "vv NO"))

tk.MustExec("drop table if exists tr")
tk.MustExec("create table tr(id int primary key nonclustered, v int, key vv(v))")
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> NO"))
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> NO NO"))
tk.MustQuery("select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name").Check(testkit.Rows("PRIMARY NO", "vv NO"))

tk.MustExec("drop table if exists tr")
tk.MustExec("create table tr(id char(100) primary key clustered, v int, key vv(v))")
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> YES"))
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> YES NO"))
tk.MustQuery("select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name").Check(testkit.Rows("PRIMARY YES", "vv NO"))

tk.MustExec("drop table if exists tr")
tk.MustExec("create table tr(id char(100) primary key nonclustered, v int, key vv(v))")
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> NO"))
tk.MustQuery("show index from tr").Check(testkit.Rows("tr 1 vv 1 v A 0 <nil> <nil> YES BTREE YES <nil> NO NO", "tr 0 PRIMARY 1 id A 0 <nil> <nil> BTREE YES <nil> NO NO"))
tk.MustQuery("select key_name, clustered from information_schema.tidb_indexes where table_name = 'tr' order by key_name").Check(testkit.Rows("PRIMARY NO", "vv NO"))
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ func (e *ShowExec) fetchShowIndex() error {
"YES", // Index_visible
nil, // Expression
"YES", // Clustered
"NO", // Global_index
})
}
for _, idx := range tb.Indices() {
Expand All @@ -833,6 +834,12 @@ func (e *ShowExec) fetchShowIndex() error {
if tb.Meta().IsCommonHandle && idxInfo.Primary {
isClustered = "YES"
}

isGlobalIndex := "NO"
if idxInfo.Global {
isGlobalIndex = "YES"
}

for i, col := range idxInfo.Columns {
nonUniq := 1
if idx.Meta().Unique {
Expand Down Expand Up @@ -885,6 +892,7 @@ func (e *ShowExec) fetchShowIndex() error {
visible, // Index_visible
expression, // Expression
isClustered, // Clustered
isGlobalIndex, // Global_index
})
}
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,30 @@ func TestShow(t *testing.T) {
tk.MustExec("create global temporary table test.t2(id int) ON COMMIT DELETE ROWS;")
tk.MustQuery("show tables from test like 't2';").Check(testkit.Rows("t2"))
}

func TestShowIndex(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("create table t(id int, abclmn int);")

tk.MustExec("create index idx on t(abclmn);")
tk.MustQuery("show index from t").Check(testkit.Rows("t 1 idx 1 abclmn A 0 <nil> <nil> YES BTREE YES <nil> NO NO"))
}

func TestShowIndexWithGlobalIndex(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("set tidb_enable_global_index=true;")

defer tk.MustExec("set tidb_enable_global_index=false;")

tk.MustExec("create table test_t1 (a int, b int) partition by range (b) (partition p0 values less than (10), partition p1 values less than (maxvalue));")

tk.MustExec("insert test_t1 values (1, 1);")
tk.MustExec("alter table test_t1 add unique index p_a (a) GLOBAL;")
tk.MustQuery("show index from test_t1").Check(testkit.Rows("test_t1 0 p_a 1 a A 0 <nil> <nil> YES BTREE YES <nil> NO YES"))
}
24 changes: 12 additions & 12 deletions pkg/executor/test/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,18 @@ func TestShow(t *testing.T) {
tk.MustExec(`create index expr_idx on show_index ((id*2+1))`)
testSQL = "SHOW index from show_index;"
tk.MustQuery(testSQL).Check(testkit.RowsWithSep("|",
"show_index|0|PRIMARY|1|id|A|0|<nil>|<nil>||BTREE| |YES|<nil>|YES",
"show_index|1|cIdx|1|c|A|0|<nil>|<nil>|YES|HASH||index_comment_for_cIdx|YES|<nil>|NO",
"show_index|1|idx1|1|id|A|0|<nil>|<nil>||HASH| |YES|<nil>|NO",
"show_index|1|idx2|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|<nil>|NO",
"show_index|1|idx3|1|id|A|0|<nil>|<nil>||HASH||idx|YES|<nil>|NO",
"show_index|1|idx4|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|<nil>|NO",
"show_index|1|idx5|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|<nil>|NO",
"show_index|1|idx6|1|id|A|0|<nil>|<nil>||HASH| |YES|<nil>|NO",
"show_index|1|idx7|1|id|A|0|<nil>|<nil>||BTREE| |YES|<nil>|NO",
"show_index|1|idx8|1|id|A|0|<nil>|<nil>||BTREE| |YES|<nil>|NO",
"show_index|1|idx9|1|id|A|0|<nil>|<nil>||BTREE| |NO|<nil>|NO",
"show_index|1|expr_idx|1|NULL|A|0|<nil>|<nil>||BTREE| |YES|`id` * 2 + 1|NO",
"show_index|0|PRIMARY|1|id|A|0|<nil>|<nil>||BTREE| |YES|<nil>|YES|NO",
"show_index|1|cIdx|1|c|A|0|<nil>|<nil>|YES|HASH||index_comment_for_cIdx|YES|<nil>|NO|NO",
"show_index|1|idx1|1|id|A|0|<nil>|<nil>||HASH| |YES|<nil>|NO|NO",
"show_index|1|idx2|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|<nil>|NO|NO",
"show_index|1|idx3|1|id|A|0|<nil>|<nil>||HASH||idx|YES|<nil>|NO|NO",
"show_index|1|idx4|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|<nil>|NO|NO",
"show_index|1|idx5|1|id|A|0|<nil>|<nil>||BTREE||idx|YES|<nil>|NO|NO",
"show_index|1|idx6|1|id|A|0|<nil>|<nil>||HASH| |YES|<nil>|NO|NO",
"show_index|1|idx7|1|id|A|0|<nil>|<nil>||BTREE| |YES|<nil>|NO|NO",
"show_index|1|idx8|1|id|A|0|<nil>|<nil>||BTREE| |YES|<nil>|NO|NO",
"show_index|1|idx9|1|id|A|0|<nil>|<nil>||BTREE| |NO|<nil>|NO|NO",
"show_index|1|expr_idx|1|NULL|A|0|<nil>|<nil>||BTREE| |YES|`id` * 2 + 1|NO|NO",
))

// For show like with escape
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5497,11 +5497,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", "Clustered"}
"Null", "Index_type", "Comment", "Index_comment", "Visible", "Expression", "Clustered", "Global"}
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
6 changes: 3 additions & 3 deletions tests/integrationtest/r/ddl/multi_schema_change.result
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,22 @@ create table t (a int, b int, c int);
alter table t add index t(a), add index t(b);
Error 8200 (HY000): Unsupported operate same index 't'
show index from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
drop table if exists t;
create table t (a int, b int, c int);
alter table t add index t(a), drop column a;
Error 8200 (HY000): Unsupported operate same column 'a'
alter table t add index t(a, b), drop column a;
Error 8200 (HY000): Unsupported operate same column 'a'
show index from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
drop table if exists t;
create table t (a int, b int, c int);
insert into t values (1, 1, 1), (2, 2, 2), (3, 3, 1);
alter table t add unique index i1(a), add unique index i2(a, b), add unique index i3(c);
Error 1062 (23000): Duplicate entry '1' for key 't.i3'
show index from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
alter table t add index i1(a), add index i2(a, b), add index i3(c);
drop table if exists t;
create table t (a int, b int, c int, index t(a));
Expand Down
4 changes: 2 additions & 2 deletions tests/integrationtest/r/ddl/primary_key_handle.result
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,5 @@ a b
alter table t6 drop primary key;
Error 3522 (HY000): A primary key index cannot be invisible
show index from t6 where Key_name='PRIMARY';
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
t6 0 PRIMARY 1 b A 0 NULL NULL BTREE YES NULL NO
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
t6 0 PRIMARY 1 b A 0 NULL NULL BTREE YES NULL NO NO
8 changes: 4 additions & 4 deletions tests/integrationtest/r/executor/executor.result
Original file line number Diff line number Diff line change
Expand Up @@ -3158,11 +3158,11 @@ InnoDB DEFAULT Supports transactions, row-level locking, and foreign keys YES YE
drop table if exists t;
create table t(a int primary key);
show index in t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
t 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
t 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES NO
show index from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
t 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
t 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES NO
show master status;
File Position Binlog_Do_DB Binlog_Ignore_DB Executed_Gtid_Set
tidb-binlog 0
Expand Down
30 changes: 15 additions & 15 deletions tests/integrationtest/r/executor/show.result
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ t CREATE TABLE `t` (
drop table if exists t2;
CREATE TABLE t2(a int primary key, b int unique, c int not null, unique index (c));
SHOW INDEX IN t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
t2 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES
t2 0 c 1 c A 0 NULL NULL BTREE YES NULL NO
t2 0 b 1 b A 0 NULL NULL YES BTREE YES NULL NO
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
t2 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES NO
t2 0 c 1 c A 0 NULL NULL BTREE YES NULL NO NO
t2 0 b 1 b A 0 NULL NULL YES BTREE YES NULL NO NO
CREATE INDEX t2_b_c_index ON t2 (b, c);
CREATE INDEX t2_c_b_index ON t2 (c, b);
SHOW INDEX IN t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
t2 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES
t2 0 c 1 c A 0 NULL NULL BTREE YES NULL NO
t2 0 b 1 b A 0 NULL NULL YES BTREE YES NULL NO
t2 1 t2_b_c_index 1 b A 0 NULL NULL YES BTREE YES NULL NO
t2 1 t2_b_c_index 2 c A 0 NULL NULL BTREE YES NULL NO
t2 1 t2_c_b_index 1 c A 0 NULL NULL BTREE YES NULL NO
t2 1 t2_c_b_index 2 b A 0 NULL NULL YES BTREE YES NULL NO
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
t2 0 PRIMARY 1 a A 0 NULL NULL BTREE YES NULL YES NO
t2 0 c 1 c A 0 NULL NULL BTREE YES NULL NO NO
t2 0 b 1 b A 0 NULL NULL YES BTREE YES NULL NO NO
t2 1 t2_b_c_index 1 b A 0 NULL NULL YES BTREE YES NULL NO NO
t2 1 t2_b_c_index 2 c A 0 NULL NULL BTREE YES NULL NO NO
t2 1 t2_c_b_index 1 c A 0 NULL NULL BTREE YES NULL NO NO
t2 1 t2_c_b_index 2 b A 0 NULL NULL YES BTREE YES NULL NO NO
drop table if exists test1;
CREATE TABLE `test1` (`id` int(0) NOT NULL,`num` int(0) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
create or replace view test1_v as(select id,row_number() over (partition by num) from test1);
Expand Down Expand Up @@ -895,9 +895,9 @@ xor
year
yearweek
SHOW INDEX FROM performance_schema.events_statements_summary_by_digest;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered
events_statements_summary_by_digest 0 SCHEMA_NAME 1 SCHEMA_NAME A 0 NULL NULL YES BTREE YES NULL NO
events_statements_summary_by_digest 0 SCHEMA_NAME 2 DIGEST A 0 NULL NULL YES BTREE YES NULL NO
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered Global
events_statements_summary_by_digest 0 SCHEMA_NAME 1 SCHEMA_NAME A 0 NULL NULL YES BTREE YES NULL NO NO
events_statements_summary_by_digest 0 SCHEMA_NAME 2 DIGEST A 0 NULL NULL YES BTREE YES NULL NO NO
drop table if exists t1, t3, t4, t5, t6, t7;
create global temporary table t1 (id int) on commit delete rows;
create global temporary table t3 (i int primary key, j int) on commit delete rows;
Expand Down
Loading

0 comments on commit 66d8cdc

Please sign in to comment.