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,infoschema: move dataForTableConstraints into executor #15240

Merged
Merged
3 changes: 2 additions & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,8 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo
strings.ToLower(infoschema.TableCharacterSets),
strings.ToLower(infoschema.TableKeyColumn),
strings.ToLower(infoschema.TableUserPrivileges),
strings.ToLower(infoschema.TableCollationCharacterSetApplicability):
strings.ToLower(infoschema.TableCollationCharacterSetApplicability),
strings.ToLower(infoschema.TableConstraints):
return &MemTableReaderExec{
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ExplainID()),
retriever: &memtableRetriever{
Expand Down
59 changes: 55 additions & 4 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
case infoschema.TableSchemata:
e.setDataFromSchemata(sctx, dbs)
case infoschema.TableTables:
err = e.dataForTables(sctx, dbs)
err = e.setDataFromTables(sctx, dbs)
case infoschema.TablePartitions:
err = e.dataForPartitions(sctx, dbs)
err = e.setDataFromPartitions(sctx, dbs)
case infoschema.TableTiDBIndexes:
e.setDataFromIndexes(sctx, dbs)
case infoschema.TableViews:
Expand All @@ -77,6 +77,8 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
e.dataForCollationCharacterSetApplicability()
case infoschema.TableUserPrivileges:
e.setDataFromUserPrivileges(sctx)
case infoschema.TableConstraints:
e.setDataFromTableConstraints(sctx, dbs)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -270,7 +272,7 @@ func (e *memtableRetriever) setDataFromSchemata(ctx sessionctx.Context, schemas
e.rows = rows
}

func (e *memtableRetriever) dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) error {
func (e *memtableRetriever) setDataFromTables(ctx sessionctx.Context, schemas []*model.DBInfo) error {
tableRowsMap, colLengthMap, err := tableStatsCache.get(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -392,7 +394,7 @@ func (e *memtableRetriever) dataForTables(ctx sessionctx.Context, schemas []*mod
return nil
}

func (e *memtableRetriever) dataForPartitions(ctx sessionctx.Context, schemas []*model.DBInfo) error {
func (e *memtableRetriever) setDataFromPartitions(ctx sessionctx.Context, schemas []*model.DBInfo) error {
tableRowsMap, colLengthMap, err := tableStatsCache.get(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -757,3 +759,52 @@ func keyColumnUsageInTable(schema *model.DBInfo, table *model.TableInfo) [][]typ
}
return rows
}

// setDataFromTableConstraints constructs data for table information_schema.constraints.See https://dev.mysql.com/doc/refman/5.7/en/table-constraints-table.html
func (e *memtableRetriever) setDataFromTableConstraints(ctx sessionctx.Context, schemas []*model.DBInfo) {
checker := privilege.GetPrivilegeManager(ctx)
var rows [][]types.Datum
for _, schema := range schemas {
for _, tbl := range schema.Tables {
if checker != nil && !checker.RequestVerification(ctx.GetSessionVars().ActiveRoles, schema.Name.L, tbl.Name.L, "", mysql.AllPrivMask) {
continue
}

if tbl.PKIsHandle {
record := types.MakeDatums(
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
mysql.PrimaryKeyName, // CONSTRAINT_NAME
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
infoschema.PrimaryKeyType, // CONSTRAINT_TYPE
)
rows = append(rows, record)
}

for _, idx := range tbl.Indices {
var cname, ctype string
if idx.Primary {
cname = mysql.PrimaryKeyName
ctype = infoschema.PrimaryKeyType
} else if idx.Unique {
cname = idx.Name.O
ctype = infoschema.UniqueKeyType
} else {
// The index has no constriant.
continue
}
record := types.MakeDatums(
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
cname, // CONSTRAINT_NAME
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
ctype, // CONSTRAINT_TYPE
)
rows = append(rows, record)
}
}
}
e.rows = rows
}
5 changes: 5 additions & 0 deletions executor/infoschema_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,8 @@ func (s *testInfoschemaTableSuite) TestPartitionsTable(c *C) {
tk.MustExec("DROP TABLE `test_partitions`;")
s.mu.Unlock()
}

func (s *testInfoschemaTableSuite) TestTableConstraintsTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery("select * from information_schema.TABLE_CONSTRAINTS where TABLE_NAME='gc_delete_range';").Check(testkit.Rows("def mysql delete_range_index mysql gc_delete_range UNIQUE"))
}
74 changes: 13 additions & 61 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ const (
// TablePartitions is the string constant of infoschema table
TablePartitions = "PARTITIONS"
// TableKeyColumn is the string constant of KEY_COLUMN_USAGE
TableKeyColumn = "KEY_COLUMN_USAGE"
tableReferConst = "REFERENTIAL_CONSTRAINTS"
tableSessionVar = "SESSION_VARIABLES"
tablePlugins = "PLUGINS"
tableConstraints = "TABLE_CONSTRAINTS"
TableKeyColumn = "KEY_COLUMN_USAGE"
tableReferConst = "REFERENTIAL_CONSTRAINTS"
tableSessionVar = "SESSION_VARIABLES"
tablePlugins = "PLUGINS"
// TableConstraints is the string constant of TABLE_CONSTRAINTS.
TableConstraints = "TABLE_CONSTRAINTS"
tableTriggers = "TRIGGERS"
// TableUserPrivileges is the string constant of infoschema user privilege table.
TableUserPrivileges = "USER_PRIVILEGES"
Expand Down Expand Up @@ -146,7 +147,7 @@ var tableIDMap = map[string]int64{
tableReferConst: autoid.InformationSchemaDBID + 13,
tableSessionVar: autoid.InformationSchemaDBID + 14,
tablePlugins: autoid.InformationSchemaDBID + 15,
tableConstraints: autoid.InformationSchemaDBID + 16,
TableConstraints: autoid.InformationSchemaDBID + 16,
tableTriggers: autoid.InformationSchemaDBID + 17,
TableUserPrivileges: autoid.InformationSchemaDBID + 18,
tableSchemaPrivileges: autoid.InformationSchemaDBID + 19,
Expand Down Expand Up @@ -1408,61 +1409,14 @@ func dataForStatisticsInTable(schema *model.DBInfo, table *model.TableInfo) [][]
}

const (
primaryKeyType = "PRIMARY KEY"
// PrimaryConstraint is the string constant of PRIMARY
// PrimaryKeyType is the string constant of PRIMARY KEY.
PrimaryKeyType = "PRIMARY KEY"
// PrimaryConstraint is the string constant of PRIMARY.
PrimaryConstraint = "PRIMARY"
uniqueKeyType = "UNIQUE"
// UniqueKeyType is the string constant of UNIQUE.
UniqueKeyType = "UNIQUE"
)

// dataForTableConstraints constructs data for table information_schema.constraints.See https://dev.mysql.com/doc/refman/5.7/en/table-constraints-table.html
func dataForTableConstraints(ctx sessionctx.Context, schemas []*model.DBInfo) [][]types.Datum {
checker := privilege.GetPrivilegeManager(ctx)
var rows [][]types.Datum
for _, schema := range schemas {
for _, tbl := range schema.Tables {
if checker != nil && !checker.RequestVerification(ctx.GetSessionVars().ActiveRoles, schema.Name.L, tbl.Name.L, "", mysql.AllPrivMask) {
continue
}

if tbl.PKIsHandle {
record := types.MakeDatums(
CatalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
mysql.PrimaryKeyName, // CONSTRAINT_NAME
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
primaryKeyType, // CONSTRAINT_TYPE
)
rows = append(rows, record)
}

for _, idx := range tbl.Indices {
var cname, ctype string
if idx.Primary {
cname = mysql.PrimaryKeyName
ctype = primaryKeyType
} else if idx.Unique {
cname = idx.Name.O
ctype = uniqueKeyType
} else {
// The index has no constriant.
continue
}
record := types.MakeDatums(
CatalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
cname, // CONSTRAINT_NAME
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
ctype, // CONSTRAINT_TYPE
)
rows = append(rows, record)
}
}
}
return rows
}

// dataForPseudoProfiling returns pseudo data for table profiling when system variable `profiling` is set to `ON`.
func dataForPseudoProfiling() [][]types.Datum {
var rows [][]types.Datum
Expand Down Expand Up @@ -1851,7 +1805,7 @@ var tableNameToColumns = map[string][]columnInfo{
tableReferConst: referConstCols,
tableSessionVar: sessionVarCols,
tablePlugins: pluginsCols,
tableConstraints: tableConstraintsCols,
TableConstraints: tableConstraintsCols,
tableTriggers: tableTriggersCols,
TableUserPrivileges: tableUserPrivilegesCols,
tableSchemaPrivileges: tableSchemaPrivilegesCols,
Expand Down Expand Up @@ -1936,8 +1890,6 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
fullRows = dataForStatistics(ctx, dbs)
case tableSessionVar:
fullRows, err = dataForSessionVar(ctx)
case tableConstraints:
fullRows = dataForTableConstraints(ctx, dbs)
case tableFiles:
case tableProfiling:
if v, ok := ctx.GetSessionVars().GetSystemVar("profiling"); ok && variable.TiDBOptOn(v) {
Expand Down
1 change: 0 additions & 1 deletion infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ func (s *testTableSuite) TestSomeTables(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustQuery("select * from information_schema.SESSION_VARIABLES where VARIABLE_NAME='tidb_retry_limit';").Check(testkit.Rows("tidb_retry_limit 10"))
tk.MustQuery("select * from information_schema.TABLE_CONSTRAINTS where TABLE_NAME='gc_delete_range';").Check(testkit.Rows("def mysql delete_range_index mysql gc_delete_range UNIQUE"))

sm := &mockSessionManager{make(map[uint64]*util.ProcessInfo, 2)}
sm.processInfoMap[1] = &util.ProcessInfo{
Expand Down