Skip to content

Commit

Permalink
executor: fix query infoschema.tables table_schema/table_name with fi…
Browse files Browse the repository at this point in the history
…lter condition (#57746) (#57771)

close #57657
  • Loading branch information
ti-chi-bot authored Nov 29, 2024
1 parent e49a151 commit bfa61d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
15 changes: 14 additions & 1 deletion pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,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)
Expand All @@ -745,7 +758,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
Expand Down
5 changes: 5 additions & 0 deletions pkg/executor/infoschema_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,16 +1058,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)
Expand Down
9 changes: 9 additions & 0 deletions tests/integrationtest/r/infoschema/v2.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion tests/integrationtest/t/infoschema/v2.test
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ create database infoschema__v2;
show databases like 'infoschema%';


set @@global.tidb_schema_cache_size = default;
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';

0 comments on commit bfa61d2

Please sign in to comment.