From 3b9d020dfe603c7baca3d67dbe98976802382536 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Tue, 3 Sep 2024 17:08:11 +0800 Subject: [PATCH] This is an automated cherry-pick of #55814 Signed-off-by: ti-chi-bot --- pkg/executor/infoschema_reader.go | 7 +- .../r/executor/infoschema_reader.result | 117 ++++++++++++++++++ .../t/executor/infoschema_reader.test | 59 +++++++++ 3 files changed, 182 insertions(+), 1 deletion(-) diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index f100600b0ad70..d497d84b02020 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -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 @@ -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 diff --git a/tests/integrationtest/r/executor/infoschema_reader.result b/tests/integrationtest/r/executor/infoschema_reader.result index 799b7b98eefde..393c367ddc1c4 100644 --- a/tests/integrationtest/r/executor/infoschema_reader.result +++ b/tests/integrationtest/r/executor/infoschema_reader.result @@ -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)) diff --git a/tests/integrationtest/t/executor/infoschema_reader.test b/tests/integrationtest/t/executor/infoschema_reader.test index 04f1318948268..66c11cd8265ab 100644 --- a/tests/integrationtest/t/executor/infoschema_reader.test +++ b/tests/integrationtest/t/executor/infoschema_reader.test @@ -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))