Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix query infoschema.tables table_schema/table_name with filter condition #57746

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
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 @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/integrationtest/r/infoschema/v2.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion tests/integrationtest/t/infoschema/v2.test
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ create database infoschema__v2;
show databases like 'infoschema%';


hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
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;