Skip to content

Commit

Permalink
planner: refactor infoschema predicate extractors (#55502)
Browse files Browse the repository at this point in the history
ref #50305
  • Loading branch information
tangenta authored Aug 21, 2024
1 parent 0b6e2e6 commit 99180a8
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 488 deletions.
137 changes: 64 additions & 73 deletions pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
is := sctx.GetInfoSchema().(infoschema.InfoSchema)
e.is = is

var getAllSchemas = func() []model.CIStr {
dbs := is.AllSchemaNames()
slices.SortFunc(dbs, func(a, b model.CIStr) int {
return strings.Compare(a.L, b.L)
})
return dbs
}

var err error
switch e.table.Name.O {
case infoschema.TableSchemata:
Expand Down Expand Up @@ -176,8 +168,7 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
case infoschema.TableTiDBServersInfo:
err = e.setDataForServersInfo(sctx)
case infoschema.TableTiFlashReplica:
dbs := getAllSchemas()
err = e.dataForTableTiFlashReplica(ctx, sctx, dbs)
err = e.dataForTableTiFlashReplica(ctx, sctx)
case infoschema.TableTiKVStoreStatus:
err = e.dataForTiKVStoreStatus(ctx, sctx)
case infoschema.TableClientErrorsSummaryGlobal,
Expand Down Expand Up @@ -415,34 +406,36 @@ func (e *memtableRetriever) setDataForStatistics(ctx context.Context, sctx sessi
return nil
}

func (e *memtableRetriever) setDataForStatisticsInTable(schema model.CIStr, table *model.TableInfo, extractor *plannercore.InfoSchemaStatisticsExtractor) {
func (e *memtableRetriever) setDataForStatisticsInTable(
schema model.CIStr,
table *model.TableInfo,
ex *plannercore.InfoSchemaStatisticsExtractor,
) {
var rows [][]types.Datum
if table.PKIsHandle {
if !extractor.Filter("index_name", "primary") {
for _, col := range table.Columns {
if mysql.HasPriKeyFlag(col.GetFlag()) {
record := types.MakeDatums(
infoschema.CatalogVal, // TABLE_CATALOG
schema.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
"0", // NON_UNIQUE
schema.O, // INDEX_SCHEMA
"PRIMARY", // INDEX_NAME
1, // SEQ_IN_INDEX
col.Name.O, // COLUMN_NAME
"A", // COLLATION
0, // CARDINALITY
nil, // SUB_PART
nil, // PACKED
"", // NULLABLE
"BTREE", // INDEX_TYPE
"", // COMMENT
"", // INDEX_COMMENT
"YES", // IS_VISIBLE
nil, // Expression
)
rows = append(rows, record)
}
if table.PKIsHandle && ex.HasPrimaryKey() {
for _, col := range table.Columns {
if mysql.HasPriKeyFlag(col.GetFlag()) {
record := types.MakeDatums(
infoschema.CatalogVal, // TABLE_CATALOG
schema.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
"0", // NON_UNIQUE
schema.O, // INDEX_SCHEMA
"PRIMARY", // INDEX_NAME
1, // SEQ_IN_INDEX
col.Name.O, // COLUMN_NAME
"A", // COLLATION
0, // CARDINALITY
nil, // SUB_PART
nil, // PACKED
"", // NULLABLE
"BTREE", // INDEX_TYPE
"", // COMMENT
"", // INDEX_COMMENT
"YES", // IS_VISIBLE
nil, // Expression
)
rows = append(rows, record)
}
}
}
Expand All @@ -451,7 +444,7 @@ func (e *memtableRetriever) setDataForStatisticsInTable(schema model.CIStr, tabl
nameToCol[c.Name.L] = c
}
for _, index := range table.Indices {
if extractor.Filter("index_name", index.Name.L) {
if !ex.HasIndex(index.Name.L) {
continue
}
nonUnique := "1"
Expand Down Expand Up @@ -528,7 +521,7 @@ func (e *memtableRetriever) setDataFromReferConst(ctx context.Context, sctx sess
continue
}
for _, fk := range table.ForeignKeys {
if ok && ex.Filter("constraint_name", fk.Name.L) {
if ok && !ex.HasConstraint(fk.Name.L) {
continue
}
updateRule, deleteRule := "NO ACTION", "NO ACTION"
Expand Down Expand Up @@ -753,7 +746,7 @@ func (e *memtableRetriever) setDataFromCheckConstraints(ctx context.Context, sct
if constraint.State != model.StatePublic {
continue
}
if ok && ex.Filter("constraint_name", constraint.Name.L) {
if ok && !ex.HasConstraint(constraint.Name.L) {
continue
}
record := types.MakeDatums(
Expand Down Expand Up @@ -797,7 +790,7 @@ func (e *memtableRetriever) setDataFromTiDBCheckConstraints(ctx context.Context,
if constraint.State != model.StatePublic {
continue
}
if ok && ex.Filter("constraint_name", constraint.Name.L) {
if ok && !ex.HasConstraint(constraint.Name.L) {
continue
}
record := types.MakeDatums(
Expand Down Expand Up @@ -1152,7 +1145,7 @@ func (e *memtableRetriever) setDataFromPartitions(ctx context.Context, sctx sess
rows = append(rows, record)
} else {
for i, pi := range table.GetPartitionInfo().Definitions {
if ex.Filter("partition_name", pi.Name.L) {
if !ex.HasPartition(pi.Name.L) {
continue
}
rowCount = cache.TableRowStatsCache.GetTableRows(pi.ID)
Expand Down Expand Up @@ -1676,7 +1669,7 @@ func (e *memtableRetriever) setDataFromKeyColumnUsage(ctx context.Context, sctx
if checker != nil && !checker.RequestVerification(sctx.GetSessionVars().ActiveRoles, schema.L, table.Name.L, "", mysql.AllPrivMask) {
continue
}
if ex.Filter("constraint_schema", schema.O) {
if !ex.HasConstraintSchema(schema.L) {
continue
}
rs := keyColumnUsageInTable(schema, table, ex)
Expand Down Expand Up @@ -1748,29 +1741,27 @@ func (e *memtableRetriever) setDataForMetricTables() {
e.rows = rows
}

func keyColumnUsageInTable(schema model.CIStr, table *model.TableInfo, extractor *plannercore.InfoSchemaKeyColumnUsageExtractor) [][]types.Datum {
func keyColumnUsageInTable(schema model.CIStr, table *model.TableInfo, ex *plannercore.InfoSchemaKeyColumnUsageExtractor) [][]types.Datum {
var rows [][]types.Datum
if table.PKIsHandle {
if extractor == nil || !extractor.Filter("constraint_name", lowerPrimaryKeyName) {
for _, col := range table.Columns {
if mysql.HasPriKeyFlag(col.GetFlag()) {
record := types.MakeDatums(
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.O, // CONSTRAINT_SCHEMA
infoschema.PrimaryConstraint, // CONSTRAINT_NAME
infoschema.CatalogVal, // TABLE_CATALOG
schema.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
}
if table.PKIsHandle && ex.HasPrimaryKey() {
for _, col := range table.Columns {
if mysql.HasPriKeyFlag(col.GetFlag()) {
record := types.MakeDatums(
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.O, // CONSTRAINT_SCHEMA
infoschema.PrimaryConstraint, // CONSTRAINT_NAME
infoschema.CatalogVal, // TABLE_CATALOG
schema.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 @@ -1792,7 +1783,7 @@ func keyColumnUsageInTable(schema model.CIStr, table *model.TableInfo, extractor
continue
}

if extractor != nil && extractor.Filter("constraint_name", filterIdxName) {
if !ex.HasConstraint(filterIdxName) {
continue
}

Expand All @@ -1819,7 +1810,7 @@ func keyColumnUsageInTable(schema model.CIStr, table *model.TableInfo, extractor
}
}
for _, fk := range table.ForeignKeys {
if extractor != nil && extractor.Filter("constraint_name", fk.Name.L) {
if !ex.HasConstraint(fk.Name.L) {
continue
}

Expand Down Expand Up @@ -2077,15 +2068,15 @@ func (e *memtableRetriever) setDataFromTableConstraints(ctx context.Context, sct
rows := make([][]types.Datum, 0, len(tables))
for i, tbl := range tables {
schema := schemas[i]
if ex.Filter("constraint_schema", schema.L) {
if !ex.HasConstraintSchema(schema.L) {
continue
}
if checker != nil && !checker.RequestVerification(sctx.GetSessionVars().ActiveRoles, schema.L, tbl.Name.L, "", mysql.AllPrivMask) {
continue
}

if tbl.PKIsHandle {
if !ex.Filter("constraint_name", lowerPrimaryKeyName) {
if ex.HasPrimaryKey() {
record := types.MakeDatums(
infoschema.CatalogVal, // CONSTRAINT_CATALOG
schema.O, // CONSTRAINT_SCHEMA
Expand Down Expand Up @@ -2113,7 +2104,7 @@ func (e *memtableRetriever) setDataFromTableConstraints(ctx context.Context, sct
// The index has no constriant.
continue
}
if ex.Filter("constraint_name", filterName) {
if !ex.HasConstraint(filterName) {
continue
}
record := types.MakeDatums(
Expand All @@ -2128,7 +2119,7 @@ func (e *memtableRetriever) setDataFromTableConstraints(ctx context.Context, sct
}
// TiDB includes foreign key information for compatibility but foreign keys are not yet enforced.
for _, fk := range tbl.ForeignKeys {
if ex.Filter("constraint_name", fk.Name.L) {
if !ex.HasConstraint(fk.Name.L) {
continue
}
record := types.MakeDatums(
Expand Down Expand Up @@ -2571,7 +2562,7 @@ func (e *memtableRetriever) setDataFromSequences(ctx context.Context, sctx sessi
}

// dataForTableTiFlashReplica constructs data for table tiflash replica info.
func (e *memtableRetriever) dataForTableTiFlashReplica(_ context.Context, sctx sessionctx.Context, _ []model.CIStr) error {
func (e *memtableRetriever) dataForTableTiFlashReplica(_ context.Context, sctx sessionctx.Context) error {
var (
checker = privilege.GetPrivilegeManager(sctx)
rows [][]types.Datum
Expand Down Expand Up @@ -3677,7 +3668,7 @@ func (e *memtableRetriever) setDataFromIndexUsage(ctx context.Context, sctx sess
dom := domain.GetDomain(sctx)
rows := make([][]types.Datum, 0, 100)
checker := privilege.GetPrivilegeManager(sctx)
extractor, ok := e.extractor.(*plannercore.InfoSchemaIndexUsageExtractor)
extractor, ok := e.extractor.(*plannercore.InfoSchemaTiDBIndexUsageExtractor)
if !ok {
return errors.Errorf("wrong extractor type: %T, expected InfoSchemaIndexUsageExtractor", e.extractor)
}
Expand Down
56 changes: 14 additions & 42 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4814,61 +4814,33 @@ func (b *PlanBuilder) buildMemTable(_ context.Context, dbName model.CIStr, table
case infoschema.TableTiKVRegionPeers:
p.Extractor = &TikvRegionPeersExtractor{}
case infoschema.TableColumns:
ex := &InfoSchemaColumnsExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaColumnsExtractor()
case infoschema.TableTables:
ex := &InfoSchemaTablesExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaTablesExtractor()
case infoschema.TablePartitions:
ex := &InfoSchemaPartitionsExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaPartitionsExtractor()
case infoschema.TableStatistics:
ex := &InfoSchemaStatisticsExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaStatisticsExtractor()
case infoschema.TableSchemata:
ex := &InfoSchemaSchemataExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaSchemataExtractor()
case infoschema.TableSequences:
ex := &InfoSchemaSequenceExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaSequenceExtractor()
case infoschema.TableTiDBIndexUsage:
ex := &InfoSchemaIndexUsageExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaTiDBIndexUsageExtractor()
case infoschema.TableCheckConstraints:
ex := &InfoSchemaCheckConstraintsExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaCheckConstraintsExtractor()
case infoschema.TableTiDBCheckConstraints:
ex := &InfoSchemaTiDBCheckConstraintsExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaTiDBCheckConstraintsExtractor()
case infoschema.TableReferConst:
ex := &InfoSchemaReferConstExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaReferConstExtractor()
case infoschema.TableTiDBIndexes:
ex := &InfoSchemaIndexesExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaIndexesExtractor()
case infoschema.TableViews:
ex := &InfoSchemaViewsExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaViewsExtractor()
case infoschema.TableKeyColumn:
ex := &InfoSchemaKeyColumnUsageExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaKeyColumnUsageExtractor()
case infoschema.TableConstraints:
ex := &InfoSchemaTableConstraintsExtractor{}
ex.initExtractableColNames(upTbl)
p.Extractor = ex
p.Extractor = NewInfoSchemaTableConstraintsExtractor()
case infoschema.TableTiKVRegionStatus:
p.Extractor = &TiKVRegionStatusExtractor{tablesID: make([]int64, 0)}
}
Expand Down
Loading

0 comments on commit 99180a8

Please sign in to comment.