Skip to content

Commit

Permalink
*: isolate more variables for runWithSystemSession (#54791) (#56902)
Browse files Browse the repository at this point in the history
close #54343
  • Loading branch information
ti-chi-bot authored Nov 6, 2024
1 parent dc5f812 commit 2af9e79
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,6 @@ func (e *hugeMemTableRetriever) setDataForColumns(ctx context.Context, sctx sess
}

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()
_, ok := e.viewSchemaMap[tbl.ID]
Expand All @@ -843,6 +842,7 @@ func (e *hugeMemTableRetriever) dataForColumnsInTable(ctx context.Context, sctx
internalCtx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnOthers)
// Build plan is not thread safe, there will be concurrency on sessionctx.
if err := runWithSystemSession(internalCtx, sctx, func(s sessionctx.Context) error {
is := sessiontxn.GetTxnManager(s).GetTxnInfoSchema()
planBuilder, _ := plannercore.NewPlanBuilder().Init(s, is, &hint.BlockHintProcessor{})
var err error
viewLogicalPlan, err = planBuilder.BuildDataSourceFromView(ctx, schema.Name, tbl, nil, nil)
Expand Down
9 changes: 9 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/pingcap/tidb/sessionctx/sessionstates"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/store/helper"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
Expand Down Expand Up @@ -2284,5 +2285,13 @@ func runWithSystemSession(ctx context.Context, sctx sessionctx.Context, fn func(
return err
}
defer b.releaseSysSession(ctx, sysCtx)
// `fn` may use KV transaction, so initialize the txn here
if err = sessiontxn.NewTxn(ctx, sysCtx); err != nil {
return err
}
defer sysCtx.RollbackTxn(ctx)
if err = ResetContextOfStmt(sysCtx, &ast.SelectStmt{}); err != nil {
return err
}
return fn(sysCtx)
}
2 changes: 2 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (
"github.com/pingcap/tidb/util/plancodec"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/size"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -5407,6 +5408,7 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model.
}()
selectLogicalPlan, err := b.Build(ctx, selectNode)
if err != nil {
logutil.BgLogger().Error("build plan for view failed", zap.Error(err))
if terror.ErrorNotEqual(err, ErrViewRecursive) &&
terror.ErrorNotEqual(err, ErrNoSuchTable) &&
terror.ErrorNotEqual(err, ErrInternal) &&
Expand Down
6 changes: 5 additions & 1 deletion table/tables/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,12 +723,16 @@ func TestViewColumns(t *testing.T) {
for _, testCase := range testCases {
tk.MustQuery(testCase.query).Check(testkit.RowsWithSep("|", testCase.expected...))
}
tk.MustExec("create view v1 as select (select a from t) as col from dual")
tk.MustQuery("select column_name, table_name from information_schema.columns where table_name='v1'").Check(
testkit.RowsWithSep("|", "col|v1"))
tk.MustExec("drop table if exists t")
for _, testCase := range testCases {
require.Len(t, tk.MustQuery(testCase.query).Rows(), 0)
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|",
"Warning|1356|View 'test.v' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them",
"Warning|1356|View 'test.va' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them"))
"Warning|1356|View 'test.va' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them",
"Warning|1356|View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them"))
}
}

Expand Down

0 comments on commit 2af9e79

Please sign in to comment.