From 246e745163064ebdab53f4f2eb289cd4ff71fe61 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 27 Nov 2024 10:28:50 +0800 Subject: [PATCH 1/3] executor: fix query infoschema.tables table_schema/table_name with filter condition --- pkg/executor/infoschema_reader.go | 14 +++++++++++++- pkg/executor/infoschema_reader_test.go | 5 +++++ tests/integrationtest/r/infoschema/v2.result | 7 +++++++ tests/integrationtest/t/infoschema/v2.test | 7 ++++++- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index dbdf25dd03980..e9bd716d8d33c 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -727,6 +727,18 @@ func onlySchemaOrTableColumns(columns []*model.ColumnInfo) bool { return false } +func onlySchemaOrTableColPredicates(predicates map[string]set.StringSet) bool { + for str, _ := range predicates { + switch str { + case "table_name": + case "table_schema": + default: + return false + } + } + return true +} + func (e *memtableRetriever) setDataFromTables(ctx context.Context, sctx sessionctx.Context) error { var rows [][]types.Datum checker := privilege.GetPrivilegeManager(sctx) @@ -743,7 +755,7 @@ func (e *memtableRetriever) setDataFromTables(ctx context.Context, sctx sessionc // select count(*) from INFORMATION_SCHEMA.TABLES // select table_schema, table_name from INFORMATION_SCHEMA.TABLES // column pruning in general is not supported here. - if onlySchemaOrTableColumns(e.columns) { + if onlySchemaOrTableColumns(e.columns) && onlySchemaOrTableColPredicates(ex.ColPredicates) { is := e.is if raw, ok := is.(*infoschema.SessionExtendedInfoSchema); ok { is = raw.InfoSchema diff --git a/pkg/executor/infoschema_reader_test.go b/pkg/executor/infoschema_reader_test.go index 668b6e2a752d3..b9ea8be6eb2db 100644 --- a/pkg/executor/infoschema_reader_test.go +++ b/pkg/executor/infoschema_reader_test.go @@ -1060,16 +1060,21 @@ func TestInfoschemaTablesSpecialOptimizationCovered(t *testing.T) { {"select table_name, table_schema from information_schema.tables", true}, {"select table_name from information_schema.tables", true}, {"select table_name from information_schema.tables where table_schema = 'test'", true}, + {"select table_name, table_schema from information_schema.tables where table_name = 't'", true}, {"select table_schema from information_schema.tables", true}, + {"select table_schema from information_schema.tables where tidb_table_id = 4611686018427387967", false}, {"select count(table_schema) from information_schema.tables", true}, {"select count(table_name) from information_schema.tables", true}, {"select count(table_rows) from information_schema.tables", false}, {"select count(1) from information_schema.tables", true}, {"select count(*) from information_schema.tables", true}, + {"select count(*) from information_schema.tables where tidb_table_id = 4611686018427387967", false}, {"select count(1) from (select table_name from information_schema.tables) t", true}, {"select * from information_schema.tables", false}, {"select table_name, table_catalog from information_schema.tables", true}, + {"select table_name, table_catalog from information_schema.tables where table_catalog = 'normal'", false}, {"select table_name, table_rows from information_schema.tables", false}, + {"select table_name, table_schema, tidb_table_id from information_schema.tables where tidb_table_id = 4611686018427387967", false}, } { var covered bool ctx := context.WithValue(context.Background(), "cover-check", &covered) diff --git a/tests/integrationtest/r/infoschema/v2.result b/tests/integrationtest/r/infoschema/v2.result index 1917ae90eda73..08a912a52087c 100644 --- a/tests/integrationtest/r/infoschema/v2.result +++ b/tests/integrationtest/r/infoschema/v2.result @@ -36,3 +36,10 @@ show databases like 'infoschema%'; Database (infoschema%) infoschema__v2 set @@global.tidb_schema_cache_size = default; +use infoschema__v2; +select TABLE_SCHEMA, TABLE_NAME, TIDB_TABLE_ID from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967; +TABLE_SCHEMA TABLE_NAME TIDB_TABLE_ID +INFORMATION_SCHEMA CLUSTER_STATEMENTS_SUMMARY_HISTORY 4611686018427387967 +select TABLE_SCHEMA from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967; +TABLE_SCHEMA +INFORMATION_SCHEMA diff --git a/tests/integrationtest/t/infoschema/v2.test b/tests/integrationtest/t/infoschema/v2.test index 29fd319442226..6d253e5feea3d 100644 --- a/tests/integrationtest/t/infoschema/v2.test +++ b/tests/integrationtest/t/infoschema/v2.test @@ -40,4 +40,9 @@ create database infoschema__v2; show databases like 'infoschema%'; -set @@global.tidb_schema_cache_size = default; \ No newline at end of file +set @@global.tidb_schema_cache_size = default; + +# TestIssue57657 +use infoschema__v2; +select TABLE_SCHEMA, TABLE_NAME, TIDB_TABLE_ID from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967; +select TABLE_SCHEMA from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967; From 21099b2ad07cebedc24f671927ab8df48ebe6564 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 27 Nov 2024 10:51:26 +0800 Subject: [PATCH 2/3] make lint happy --- pkg/executor/infoschema_reader.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index e9bd716d8d33c..5a70b9d04de33 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -728,7 +728,7 @@ func onlySchemaOrTableColumns(columns []*model.ColumnInfo) bool { } func onlySchemaOrTableColPredicates(predicates map[string]set.StringSet) bool { - for str, _ := range predicates { + for str := range predicates { switch str { case "table_name": case "table_schema": From 012e2404bdbe16d5edf7c2f3e0efa09c39ace378 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 27 Nov 2024 16:03:58 +0800 Subject: [PATCH 3/3] fix CI --- pkg/executor/infoschema_reader.go | 1 + pkg/executor/infoschema_reader_test.go | 2 +- tests/integrationtest/r/infoschema/v2.result | 2 ++ tests/integrationtest/t/infoschema/v2.test | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index 5a70b9d04de33..1819455cc806e 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -732,6 +732,7 @@ func onlySchemaOrTableColPredicates(predicates map[string]set.StringSet) bool { switch str { case "table_name": case "table_schema": + case "table_catalog": default: return false } diff --git a/pkg/executor/infoschema_reader_test.go b/pkg/executor/infoschema_reader_test.go index b9ea8be6eb2db..5b9cdfab74f3b 100644 --- a/pkg/executor/infoschema_reader_test.go +++ b/pkg/executor/infoschema_reader_test.go @@ -1072,7 +1072,7 @@ func TestInfoschemaTablesSpecialOptimizationCovered(t *testing.T) { {"select count(1) from (select table_name from information_schema.tables) t", true}, {"select * from information_schema.tables", false}, {"select table_name, table_catalog from information_schema.tables", true}, - {"select table_name, table_catalog from information_schema.tables where table_catalog = 'normal'", false}, + {"select table_name, table_catalog from information_schema.tables where table_catalog = 'normal'", true}, {"select table_name, table_rows from information_schema.tables", false}, {"select table_name, table_schema, tidb_table_id from information_schema.tables where tidb_table_id = 4611686018427387967", false}, } { diff --git a/tests/integrationtest/r/infoschema/v2.result b/tests/integrationtest/r/infoschema/v2.result index 08a912a52087c..f6b25d3782e50 100644 --- a/tests/integrationtest/r/infoschema/v2.result +++ b/tests/integrationtest/r/infoschema/v2.result @@ -43,3 +43,5 @@ INFORMATION_SCHEMA CLUSTER_STATEMENTS_SUMMARY_HISTORY 4611686018427387967 select TABLE_SCHEMA from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967; TABLE_SCHEMA INFORMATION_SCHEMA +select TABLE_NAME, TABLE_CATALOG from information_schema.tables where TABLE_CATALOG != 'def'; +TABLE_NAME TABLE_CATALOG diff --git a/tests/integrationtest/t/infoschema/v2.test b/tests/integrationtest/t/infoschema/v2.test index 6d253e5feea3d..aace2b2ee84a5 100644 --- a/tests/integrationtest/t/infoschema/v2.test +++ b/tests/integrationtest/t/infoschema/v2.test @@ -46,3 +46,4 @@ set @@global.tidb_schema_cache_size = default; use infoschema__v2; select TABLE_SCHEMA, TABLE_NAME, TIDB_TABLE_ID from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967; select TABLE_SCHEMA from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967; +select TABLE_NAME, TABLE_CATALOG from information_schema.tables where TABLE_CATALOG != 'def';