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

infoschema: fix sub_part in information_schema.statistics table (#55814) #56210

Open
wants to merge 1 commit into
base: release-8.1
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ func (e *memtableRetriever) setDataForStatisticsInTable(schema model.CIStr, tabl
expression = tblCol.GeneratedExprString
}

var subPart any
if key.Length != types.UnspecifiedLength {
subPart = key.Length
}

record := types.MakeDatums(
infoschema.CatalogVal, // TABLE_CATALOG
schema.O, // TABLE_SCHEMA
Expand All @@ -465,7 +470,7 @@ func (e *memtableRetriever) setDataForStatisticsInTable(schema model.CIStr, tabl
colName, // COLUMN_NAME
"A", // COLLATION
0, // CARDINALITY
nil, // SUB_PART
subPart, // SUB_PART
nil, // PACKED
nullable, // NULLABLE
"BTREE", // INDEX_TYPE
Expand Down
117 changes: 117 additions & 0 deletions tests/integrationtest/r/executor/infoschema_reader.result
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,120 @@ sleep(1)
select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t' AND TABLE_SCHEMA='executor__infoschema_reader';
table_rows avg_row_length data_length index_length
3 18 54 6
<<<<<<< HEAD
=======
drop table if exists test.t;
select * from information_schema.tidb_indexes where table_name = 't';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED IS_GLOBAL
executor__infoschema_reader t 0 PRIMARY 1 a NULL NULL 0 YES YES 0
executor__infoschema_reader t 1 idx 1 c NULL NULL 1 YES NO 0
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED IS_GLOBAL
executor__infoschema_reader t 0 PRIMARY 1 a NULL NULL 0 YES YES 0
executor__infoschema_reader t 1 idx 1 c NULL NULL 1 YES NO 0
executor__infoschema_reader t_common 0 PRIMARY 1 a NULL NULL 1 YES YES 0
executor__infoschema_reader t_implicit 0 PRIMARY 1 a NULL NULL 1 YES NO 0
executor__infoschema_reader t_int 0 PRIMARY 1 a NULL NULL 0 YES YES 0
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' and table_name = 't';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED IS_GLOBAL
executor__infoschema_reader t 0 PRIMARY 1 a NULL NULL 0 YES YES 0
executor__infoschema_reader t 1 idx 1 c NULL NULL 1 YES NO 0
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' or table_name = 't';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED IS_GLOBAL
executor__infoschema_reader t 0 PRIMARY 1 a NULL NULL 0 YES YES 0
executor__infoschema_reader t 1 idx 1 c NULL NULL 1 YES NO 0
executor__infoschema_reader t_common 0 PRIMARY 1 a NULL NULL 1 YES YES 0
executor__infoschema_reader t_implicit 0 PRIMARY 1 a NULL NULL 1 YES NO 0
executor__infoschema_reader t_int 0 PRIMARY 1 a NULL NULL 0 YES YES 0
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' and column_name = 'c';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED IS_GLOBAL
executor__infoschema_reader t 1 idx 1 c NULL NULL 1 YES NO 0
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' and table_name = 'non_exist';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED IS_GLOBAL
select * from information_schema.views where table_name = 'v1';
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION IS_UPDATABLE DEFINER SECURITY_TYPE CHARACTER_SET_CLIENT COLLATION_CONNECTION
def executor__infoschema_reader v1 SELECT 1 AS `1` CASCADED NO root@localhost DEFINER utf8mb4 utf8mb4_general_ci
select * from information_schema.views where table_name = 'non_exist';
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION IS_UPDATABLE DEFINER SECURITY_TYPE CHARACTER_SET_CLIENT COLLATION_CONNECTION
select * from information_schema.views where table_schema = 'executor__infoschema_reader';
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION IS_UPDATABLE DEFINER SECURITY_TYPE CHARACTER_SET_CLIENT COLLATION_CONNECTION
def executor__infoschema_reader v1 SELECT 1 AS `1` CASCADED NO root@localhost DEFINER utf8mb4 utf8mb4_general_ci
def executor__infoschema_reader v_test SELECT NULL AS `type` FROM `executor__infoschema_reader`.`t` AS `f` CASCADED NO root@1.1.1.1 DEFINER utf8mb4 utf8mb4_general_ci
select * from information_schema.views where table_schema = 'non_exist';
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION IS_UPDATABLE DEFINER SECURITY_TYPE CHARACTER_SET_CLIENT COLLATION_CONNECTION
select * from information_schema.views where table_schema = 'executor__infoschema_reader' and table_name = 'v1';
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION IS_UPDATABLE DEFINER SECURITY_TYPE CHARACTER_SET_CLIENT COLLATION_CONNECTION
def executor__infoschema_reader v1 SELECT 1 AS `1` CASCADED NO root@localhost DEFINER utf8mb4 utf8mb4_general_ci
select * from information_schema.views where table_schema = 'executor__infoschema_reader' or table_name = 'v1';
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION IS_UPDATABLE DEFINER SECURITY_TYPE CHARACTER_SET_CLIENT COLLATION_CONNECTION
def executor__infoschema_reader v1 SELECT 1 AS `1` CASCADED NO root@localhost DEFINER utf8mb4 utf8mb4_general_ci
def executor__infoschema_reader v_test SELECT NULL AS `type` FROM `executor__infoschema_reader`.`t` AS `f` CASCADED NO root@1.1.1.1 DEFINER utf8mb4 utf8mb4_general_ci
select * from information_schema.key_column_usage where table_name = 't';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t a 1 1 NULL NULL NULL
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t a 1 1 NULL NULL NULL
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t_common a 1 NULL NULL NULL NULL
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t_implicit a 1 NULL NULL NULL NULL
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t_int a 1 1 NULL NULL NULL
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' and table_name = 't';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t a 1 1 NULL NULL NULL
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' or table_name = 't';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t a 1 1 NULL NULL NULL
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t_common a 1 NULL NULL NULL NULL
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t_implicit a 1 NULL NULL NULL NULL
def executor__infoschema_reader PRIMARY def executor__infoschema_reader t_int a 1 1 NULL NULL NULL
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' and column_name = 'c';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' and column_name = 'non_exist';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
CREATE TABLE tc(a INT CHECK(a > 10) NOT ENFORCED, b INT, c INT, CONSTRAINT c1 CHECK (b > c));
select * from information_schema.table_constraints where table_name = 'tc';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE
def executor__infoschema_reader PRIMARY executor__infoschema_reader t PRIMARY KEY
def executor__infoschema_reader PRIMARY executor__infoschema_reader t_common PRIMARY KEY
def executor__infoschema_reader PRIMARY executor__infoschema_reader t_implicit PRIMARY KEY
def executor__infoschema_reader PRIMARY executor__infoschema_reader t_int PRIMARY KEY
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' and table_name = 'tc';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' or table_name = 'tc';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE
def executor__infoschema_reader PRIMARY executor__infoschema_reader t PRIMARY KEY
def executor__infoschema_reader PRIMARY executor__infoschema_reader t_common PRIMARY KEY
def executor__infoschema_reader PRIMARY executor__infoschema_reader t_implicit PRIMARY KEY
def executor__infoschema_reader PRIMARY executor__infoschema_reader t_int PRIMARY KEY
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' and table_name = 'non_exist';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' and CONSTRAINT_NAME = 'c1';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE
select TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,VERSION from information_schema.tables where table_name = 't';
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE ENGINE VERSION
def executor__infoschema_reader t BASE TABLE InnoDB 10
select table_name, table_schema from information_schema.tables where table_name = 't';
table_name table_schema
t executor__infoschema_reader
select table_name from information_schema.tables where table_name = 't';
table_name
t
explain format='brief' select table_name, table_schema from information_schema.tables;
id estRows task access object operator info
Projection 10000.00 root Column#3, Column#2
└─MemTableScan 10000.00 root table:TABLES
select count(*) from information_schema.tables where table_name = 't';
count(*)
1
select count(table_name) from information_schema.tables where table_name = 't';
count(table_name)
1
drop table if exists t;
create table t (c text);
alter table t add index idx_t (c(16));
select SUB_PART from information_schema.statistics where TABLE_SCHEMA = 'executor__infoschema_reader' and TABLE_NAME = 't';
SUB_PART
16
>>>>>>> 081616689db (infoschema: fix `sub_part` in `information_schema.statistics` table (#55814))
59 changes: 59 additions & 0 deletions tests/integrationtest/t/executor/infoschema_reader.test
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,62 @@ insert into t(a, b, c) values(1, 2, 'c'), (7, 3, 'd'), (12, 4, 'e');
analyze table t;
select sleep(1);
select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t' AND TABLE_SCHEMA='executor__infoschema_reader';
<<<<<<< HEAD
=======


# Cover reading from tables: Indexes, Views, KeyColumnUsage, TableConstraints
drop table if exists test.t;
select * from information_schema.tidb_indexes where table_name = 't';
--sorted_result
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader';
--sorted_result
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' and table_name = 't';
--sorted_result
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' or table_name = 't';
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' and column_name = 'c';
select * from information_schema.tidb_indexes where table_schema = 'executor__infoschema_reader' and table_name = 'non_exist';

select * from information_schema.views where table_name = 'v1';
select * from information_schema.views where table_name = 'non_exist';
--sorted_result
select * from information_schema.views where table_schema = 'executor__infoschema_reader';
select * from information_schema.views where table_schema = 'non_exist';
select * from information_schema.views where table_schema = 'executor__infoschema_reader' and table_name = 'v1';
--sorted_result
select * from information_schema.views where table_schema = 'executor__infoschema_reader' or table_name = 'v1';

select * from information_schema.key_column_usage where table_name = 't';
--sorted_result
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader';
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' and table_name = 't';
--sorted_result
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' or table_name = 't';
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' and column_name = 'c';
select * from information_schema.key_column_usage where table_schema = 'executor__infoschema_reader' and column_name = 'non_exist';

CREATE TABLE tc(a INT CHECK(a > 10) NOT ENFORCED, b INT, c INT, CONSTRAINT c1 CHECK (b > c));
--sorted_result
select * from information_schema.table_constraints where table_name = 'tc';
--sorted_result
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader';
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' and table_name = 'tc';
--sorted_result
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' or table_name = 'tc';
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' and table_name = 'non_exist';
--sorted_result
select * from information_schema.table_constraints where table_schema = 'executor__infoschema_reader' and CONSTRAINT_NAME = 'c1';

# TestTables
select TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,VERSION from information_schema.tables where table_name = 't';
select table_name, table_schema from information_schema.tables where table_name = 't';
select table_name from information_schema.tables where table_name = 't';
explain format='brief' select table_name, table_schema from information_schema.tables;
select count(*) from information_schema.tables where table_name = 't';
select count(table_name) from information_schema.tables where table_name = 't';

drop table if exists t;
create table t (c text);
alter table t add index idx_t (c(16));
select SUB_PART from information_schema.statistics where TABLE_SCHEMA = 'executor__infoschema_reader' and TABLE_NAME = 't';
>>>>>>> 081616689db (infoschema: fix `sub_part` in `information_schema.statistics` table (#55814))