Skip to content

Commit 7291829

Browse files
authored
executor: fix the issue that some extracted conditions are not used in information_schema (#55236)
close #55235
1 parent 7d0abb9 commit 7291829

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

pkg/executor/infoschema_reader.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ func (e *memtableRetriever) setDataFromKeyColumnUsage(ctx context.Context, sctx
17451745
if checker != nil && !checker.RequestVerification(sctx.GetSessionVars().ActiveRoles, schema.L, table.Name.L, "", mysql.AllPrivMask) {
17461746
continue
17471747
}
1748-
rs := keyColumnUsageInTable(schema, table)
1748+
rs := keyColumnUsageInTable(schema, table, extractor)
17491749
rows = append(rows, rs...)
17501750
}
17511751
}
@@ -1815,7 +1815,7 @@ func (e *memtableRetriever) setDataForMetricTables() {
18151815
e.rows = rows
18161816
}
18171817

1818-
func keyColumnUsageInTable(schema model.CIStr, table *model.TableInfo) [][]types.Datum {
1818+
func keyColumnUsageInTable(schema model.CIStr, table *model.TableInfo, extractor *plannercore.InfoSchemaBaseExtractor) [][]types.Datum {
18191819
var rows [][]types.Datum
18201820
if table.PKIsHandle {
18211821
for _, col := range table.Columns {
@@ -1853,6 +1853,11 @@ func keyColumnUsageInTable(schema model.CIStr, table *model.TableInfo) [][]types
18531853
// Only handle unique/primary key
18541854
continue
18551855
}
1856+
1857+
if extractor != nil && extractor.Filter("constraint_name", idxName) {
1858+
continue
1859+
}
1860+
18561861
for i, key := range index.Columns {
18571862
col := nameToCol[key.Name.L]
18581863
if col.Hidden {
@@ -2125,6 +2130,9 @@ func (e *memtableRetriever) setDataFromTableConstraints(ctx context.Context, sct
21252130
if ok && extractor.Filter("constraint_schema", schema.L) {
21262131
continue
21272132
}
2133+
if ok && extractor.Filter("table_schema", schema.L) {
2134+
continue
2135+
}
21282136
tables, err := e.is.SchemaTableInfos(ctx, schema)
21292137
if err != nil {
21302138
return errors.Trace(err)
@@ -2161,6 +2169,9 @@ func (e *memtableRetriever) setDataFromTableConstraints(ctx context.Context, sct
21612169
// The index has no constriant.
21622170
continue
21632171
}
2172+
if ok && extractor.Filter("constraint_name", cname) {
2173+
continue
2174+
}
21642175
record := types.MakeDatums(
21652176
infoschema.CatalogVal, // CONSTRAINT_CATALOG
21662177
schema.O, // CONSTRAINT_SCHEMA

pkg/executor/infoschema_reader_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,88 @@ func TestReferencedTableSchemaWithForeignKey(t *testing.T) {
627627
WHERE table_name = 't2' AND table_schema = 'test2';`).Check(testkit.Rows(
628628
"id id t1 test2 test"))
629629
}
630+
631+
func TestInfoSchemaConditionWorks(t *testing.T) {
632+
// this test creates table in different schema with different index name, and check
633+
// the condition in the following columns whether work as expected.
634+
//
635+
// - "table_schema"
636+
// - "constraint_schema"
637+
// - "table_name"
638+
// - "constraint_name"
639+
// - "partition_name"
640+
// - "schema_name"
641+
// - "index_name"
642+
store := testkit.CreateMockStore(t)
643+
tk := testkit.NewTestKit(t, store)
644+
for db := 0; db < 2; db++ {
645+
for table := 0; table < 2; table++ {
646+
tk.MustExec(fmt.Sprintf("create database if not exists db%d;", db))
647+
tk.MustExec(fmt.Sprintf(`create table db%d.table%d (id int primary key, data0 varchar(255), data1 varchar(255))
648+
partition by range (id) (
649+
partition p0 values less than (10),
650+
partition p1 values less than (20)
651+
);`, db, table))
652+
for index := 0; index < 2; index++ {
653+
tk.MustExec(fmt.Sprintf("create index idx%d on db%d.table%d (data%d);", index, db, table, index))
654+
}
655+
}
656+
}
657+
658+
testColumns := map[string]string{
659+
"table_schema": "db",
660+
"constraint_schema": "db",
661+
"table_name": "table",
662+
"constraint_name": "idx",
663+
"partition_name": "p",
664+
"schema_name": "db",
665+
"index_name": "idx",
666+
}
667+
testTables := []string{}
668+
for _, row := range tk.MustQuery("show tables in information_schema").Rows() {
669+
tableName := row[0].(string)
670+
// exclude some tables which cannot run without TiKV.
671+
if strings.HasPrefix(tableName, "CLUSTER_") ||
672+
strings.HasPrefix(tableName, "INSPECTION_") ||
673+
strings.HasPrefix(tableName, "METRICS_") ||
674+
strings.HasPrefix(tableName, "TIFLASH_") ||
675+
strings.HasPrefix(tableName, "TIKV_") ||
676+
strings.HasPrefix(tableName, "USER_") ||
677+
tableName == "TABLE_STORAGE_STATS" ||
678+
strings.Contains(tableName, "REGION") {
679+
continue
680+
}
681+
testTables = append(testTables, row[0].(string))
682+
}
683+
for _, table := range testTables {
684+
rs, err := tk.Exec(fmt.Sprintf("select * from information_schema.%s", table))
685+
require.NoError(t, err)
686+
cols := rs.Fields()
687+
688+
chk := rs.NewChunk(nil)
689+
rowCount := 0
690+
for {
691+
err := rs.Next(context.Background(), chk)
692+
require.NoError(t, err)
693+
if chk.NumRows() == 0 {
694+
break
695+
}
696+
rowCount += chk.NumRows()
697+
}
698+
if rowCount == 0 {
699+
// TODO: find a way to test the table without any rows by adding some rows to them.
700+
continue
701+
}
702+
for i := 0; i < len(cols); i++ {
703+
colName := cols[i].Column.Name.L
704+
if valPrefix, ok := testColumns[colName]; ok {
705+
for j := 0; j < 2; j++ {
706+
rows := tk.MustQuery(fmt.Sprintf("select * from information_schema.%s where %s = '%s%d';",
707+
table, colName, valPrefix, j)).Rows()
708+
rowCountWithCondition := len(rows)
709+
require.Less(t, rowCountWithCondition, rowCount, "%s has no effect on %s", colName, table)
710+
}
711+
}
712+
}
713+
}
714+
}

0 commit comments

Comments
 (0)