Skip to content

Commit 09d12f0

Browse files
authored
executor: fix query infoschema.tables table_schema/table_name with filter condition (#57746)
close #57657
1 parent 0471081 commit 09d12f0

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

pkg/executor/infoschema_reader.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,19 @@ func onlySchemaOrTableColumns(columns []*model.ColumnInfo) bool {
730730
return false
731731
}
732732

733+
func onlySchemaOrTableColPredicates(predicates map[string]set.StringSet) bool {
734+
for str := range predicates {
735+
switch str {
736+
case "table_name":
737+
case "table_schema":
738+
case "table_catalog":
739+
default:
740+
return false
741+
}
742+
}
743+
return true
744+
}
745+
733746
func (e *memtableRetriever) setDataFromTables(ctx context.Context, sctx sessionctx.Context) error {
734747
var rows [][]types.Datum
735748
checker := privilege.GetPrivilegeManager(sctx)
@@ -746,7 +759,7 @@ func (e *memtableRetriever) setDataFromTables(ctx context.Context, sctx sessionc
746759
// select count(*) from INFORMATION_SCHEMA.TABLES
747760
// select table_schema, table_name from INFORMATION_SCHEMA.TABLES
748761
// column pruning in general is not supported here.
749-
if onlySchemaOrTableColumns(e.columns) {
762+
if onlySchemaOrTableColumns(e.columns) && onlySchemaOrTableColPredicates(ex.ColPredicates) {
750763
is := e.is
751764
if raw, ok := is.(*infoschema.SessionExtendedInfoSchema); ok {
752765
is = raw.InfoSchema

pkg/executor/infoschema_reader_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,16 +1060,21 @@ func TestInfoschemaTablesSpecialOptimizationCovered(t *testing.T) {
10601060
{"select table_name, table_schema from information_schema.tables", true},
10611061
{"select table_name from information_schema.tables", true},
10621062
{"select table_name from information_schema.tables where table_schema = 'test'", true},
1063+
{"select table_name, table_schema from information_schema.tables where table_name = 't'", true},
10631064
{"select table_schema from information_schema.tables", true},
1065+
{"select table_schema from information_schema.tables where tidb_table_id = 4611686018427387967", false},
10641066
{"select count(table_schema) from information_schema.tables", true},
10651067
{"select count(table_name) from information_schema.tables", true},
10661068
{"select count(table_rows) from information_schema.tables", false},
10671069
{"select count(1) from information_schema.tables", true},
10681070
{"select count(*) from information_schema.tables", true},
1071+
{"select count(*) from information_schema.tables where tidb_table_id = 4611686018427387967", false},
10691072
{"select count(1) from (select table_name from information_schema.tables) t", true},
10701073
{"select * from information_schema.tables", false},
10711074
{"select table_name, table_catalog from information_schema.tables", true},
1075+
{"select table_name, table_catalog from information_schema.tables where table_catalog = 'normal'", true},
10721076
{"select table_name, table_rows from information_schema.tables", false},
1077+
{"select table_name, table_schema, tidb_table_id from information_schema.tables where tidb_table_id = 4611686018427387967", false},
10731078
} {
10741079
var covered bool
10751080
ctx := context.WithValue(context.Background(), "cover-check", &covered)

tests/integrationtest/r/infoschema/v2.result

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ show databases like 'infoschema%';
3636
Database (infoschema%)
3737
infoschema__v2
3838
set @@global.tidb_schema_cache_size = default;
39+
use infoschema__v2;
40+
select TABLE_SCHEMA, TABLE_NAME, TIDB_TABLE_ID from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967;
41+
TABLE_SCHEMA TABLE_NAME TIDB_TABLE_ID
42+
INFORMATION_SCHEMA CLUSTER_STATEMENTS_SUMMARY_HISTORY 4611686018427387967
43+
select TABLE_SCHEMA from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967;
44+
TABLE_SCHEMA
45+
INFORMATION_SCHEMA
46+
select TABLE_NAME, TABLE_CATALOG from information_schema.tables where TABLE_CATALOG != 'def';
47+
TABLE_NAME TABLE_CATALOG

tests/integrationtest/t/infoschema/v2.test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@ create database infoschema__v2;
4040
show databases like 'infoschema%';
4141

4242

43-
set @@global.tidb_schema_cache_size = default;
43+
set @@global.tidb_schema_cache_size = default;
44+
45+
# TestIssue57657
46+
use infoschema__v2;
47+
select TABLE_SCHEMA, TABLE_NAME, TIDB_TABLE_ID from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967;
48+
select TABLE_SCHEMA from information_schema.tables where TIDB_TABLE_ID = 4611686018427387967;
49+
select TABLE_NAME, TABLE_CATALOG from information_schema.tables where TABLE_CATALOG != 'def';

0 commit comments

Comments
 (0)