diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index feab4210bc358..a7e1d16d356a4 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -730,6 +730,19 @@ 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": + case "table_catalog": + default: + return false + } + } + return true +} + func (e *memtableRetriever) setDataFromTables(ctx context.Context, sctx sessionctx.Context) error { var rows [][]types.Datum checker := privilege.GetPrivilegeManager(sctx) @@ -746,7 +759,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..5b9cdfab74f3b 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'", 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}, } { 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..f6b25d3782e50 100644 --- a/tests/integrationtest/r/infoschema/v2.result +++ b/tests/integrationtest/r/infoschema/v2.result @@ -36,3 +36,12 @@ 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 +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 29fd319442226..aace2b2ee84a5 100644 --- a/tests/integrationtest/t/infoschema/v2.test +++ b/tests/integrationtest/t/infoschema/v2.test @@ -40,4 +40,10 @@ 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; +select TABLE_NAME, TABLE_CATALOG from information_schema.tables where TABLE_CATALOG != 'def';