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
91 changes: 74 additions & 17 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import (
"github.com/pingcap/tidb/util/sqlexec"
)

const (
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
primaryKeyType = "PRIMARY KEY"
primaryConstraint = "PRIMARY"
uniqueKeyType = "UNIQUE"
)

type memtableRetriever struct {
dummyCloser
table *model.TableInfo
Expand All @@ -58,9 +64,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 +83,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 +278,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 +400,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 @@ -680,18 +688,18 @@ func keyColumnUsageInTable(schema *model.DBInfo, table *model.TableInfo) [][]typ
for _, col := range table.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
record := types.MakeDatums(
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
infoschema.PrimaryConstraint, // CONSTRAINT_NAME
infoschema.CatalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
col.Name.O, // COLUMN_NAME
1, // ORDINAL_POSITION
1, // POSITION_IN_UNIQUE_CONSTRAINT
nil, // REFERENCED_TABLE_SCHEMA
nil, // REFERENCED_TABLE_NAME
nil, // REFERENCED_COLUMN_NAME
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
primaryConstraint, // CONSTRAINT_NAME
infoschema.CatalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
col.Name.O, // COLUMN_NAME
1, // ORDINAL_POSITION
1, // POSITION_IN_UNIQUE_CONSTRAINT
nil, // REFERENCED_TABLE_SCHEMA
nil, // REFERENCED_TABLE_NAME
nil, // REFERENCED_COLUMN_NAME
)
rows = append(rows, record)
break
Expand All @@ -705,7 +713,7 @@ func keyColumnUsageInTable(schema *model.DBInfo, table *model.TableInfo) [][]typ
for _, index := range table.Indices {
var idxName string
if index.Primary {
idxName = infoschema.PrimaryConstraint
idxName = primaryConstraint
} else if index.Unique {
idxName = index.Name.O
} else {
Expand Down Expand Up @@ -757,3 +765,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
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(
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 @@ -309,3 +309,8 @@ func (s *testInfoschemaTableSuite) TestPartitionsTable(c *C) {

tk.MustExec("DROP TABLE `test_partitions`;")
}

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"))
}
73 changes: 8 additions & 65 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 @@ -1407,62 +1408,6 @@ func dataForStatisticsInTable(schema *model.DBInfo, table *model.TableInfo) [][]
return rows
}

const (
primaryKeyType = "PRIMARY KEY"
// PrimaryConstraint is the string constant of PRIMARY
PrimaryConstraint = "PRIMARY"
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 +1796,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 +1881,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