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

*: fix columns table query unstable #51896

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Changes from all 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
29 changes: 12 additions & 17 deletions pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package executor

import (
"bytes"
"cmp"
"context"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -780,18 +779,16 @@ func (e *hugeMemTableRetriever) setDataForColumns(ctx context.Context, sctx sess
checker := privilege.GetPrivilegeManager(sctx)
e.rows = e.rows[:0]
batch := 1024
is := sctx.GetInfoSchema().(infoschema.InfoSchema)
for ; e.dbsIdx < len(e.dbs); e.dbsIdx++ {
schema := e.dbs[e.dbsIdx]
tables := is.SchemaTables(schema)
for e.tblIdx < len(tables) {
table := tables[e.tblIdx]
for e.tblIdx < len(schema.Tables) {
table := schema.Tables[e.tblIdx]
e.tblIdx++
hasPrivs := false
var priv mysql.PrivilegeType
if checker != nil {
for _, p := range mysql.AllColumnPrivs {
if checker.RequestVerification(sctx.GetSessionVars().ActiveRoles, schema.L, table.Meta().Name.L, "", p) {
if checker.RequestVerification(sctx.GetSessionVars().ActiveRoles, schema.Name.L, table.Name.L, "", p) {
hasPrivs = true
priv |= p
}
Expand All @@ -801,7 +798,7 @@ func (e *hugeMemTableRetriever) setDataForColumns(ctx context.Context, sctx sess
}
}

e.dataForColumnsInTable(ctx, sctx, schema, table.Meta(), priv, extractor)
e.dataForColumnsInTable(ctx, sctx, schema, table, priv, extractor)
if len(e.rows) >= batch {
return nil
}
Expand All @@ -811,7 +808,7 @@ func (e *hugeMemTableRetriever) setDataForColumns(ctx context.Context, sctx sess
return nil
}

func (e *hugeMemTableRetriever) dataForColumnsInTable(ctx context.Context, sctx sessionctx.Context, schema model.CIStr, tbl *model.TableInfo, priv mysql.PrivilegeType, extractor *plannercore.ColumnsTableExtractor) {
func (e *hugeMemTableRetriever) dataForColumnsInTable(ctx context.Context, sctx sessionctx.Context, schema *model.DBInfo, tbl *model.TableInfo, priv mysql.PrivilegeType, extractor *plannercore.ColumnsTableExtractor) {
is := sessiontxn.GetTxnManager(sctx).GetTxnInfoSchema()
if tbl.IsView() {
e.viewMu.Lock()
Expand All @@ -823,7 +820,7 @@ func (e *hugeMemTableRetriever) dataForColumnsInTable(ctx context.Context, sctx
if err := runWithSystemSession(internalCtx, sctx, func(s sessionctx.Context) error {
planBuilder, _ := plannercore.NewPlanBuilder().Init(s.GetPlanCtx(), is, hint.NewQBHintHandler(nil))
var err error
viewLogicalPlan, err = planBuilder.BuildDataSourceFromView(ctx, schema, tbl, nil, nil)
viewLogicalPlan, err = planBuilder.BuildDataSourceFromView(ctx, schema.Name, tbl, nil, nil)
return errors.Trace(err)
}); err != nil {
sctx.GetSessionVars().StmtCtx.AppendWarning(err)
Expand Down Expand Up @@ -886,7 +883,7 @@ ForColumnsTag:
e.viewMu.RUnlock()
}
if !extractor.SkipRequest {
if tableSchemaFilterEnable && !extractor.TableSchema.Exist(schema.L) {
if tableSchemaFilterEnable && !extractor.TableSchema.Exist(schema.Name.L) {
continue
}
if tableNameFilterEnable && !extractor.TableName.Exist(tbl.Name.L) {
Expand All @@ -896,7 +893,7 @@ ForColumnsTag:
continue
}
for _, re := range tableSchemaRegexp {
if !re.DoMatch(schema.L) {
if !re.DoMatch(schema.Name.L) {
continue ForColumnsTag
}
}
Expand Down Expand Up @@ -987,7 +984,7 @@ ForColumnsTag:
}
record := types.MakeDatums(
infoschema.CatalogVal, // TABLE_CATALOG
schema.O, // TABLE_SCHEMA
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
col.Name.O, // COLUMN_NAME
i, // ORDINAL_POSITION
Expand Down Expand Up @@ -3072,7 +3069,7 @@ type hugeMemTableRetriever struct {
retrieved bool
initialized bool
rows [][]types.Datum
dbs []model.CIStr
dbs []*model.DBInfo
dbsIdx int
tblIdx int
viewMu syncutil.RWMutex
Expand All @@ -3088,10 +3085,8 @@ func (e *hugeMemTableRetriever) retrieve(ctx context.Context, sctx sessionctx.Co

if !e.initialized {
is := sctx.GetInfoSchema().(infoschema.InfoSchema)
dbs := is.AllSchemaNames()
slices.SortFunc(dbs, func(a, b model.CIStr) int {
return cmp.Compare(a.L, b.L)
})
dbs := is.AllSchemas()
slices.SortFunc(dbs, model.LessDBInfo)
e.dbs = dbs
e.initialized = true
e.rows = make([][]types.Datum, 0, 1024)
Expand Down