diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index 0bc237b01a086..30aa3661cfc68 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -889,7 +889,6 @@ func (e *hugeMemTableRetriever) setDataForColumnsWithOneTable( } 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] @@ -898,6 +897,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.GetPlanCtx(), is, hint.NewQBHintHandler(nil)) var err error viewLogicalPlan, err = planBuilder.BuildDataSourceFromView(ctx, schema.Name, tbl, nil, nil) diff --git a/pkg/executor/show.go b/pkg/executor/show.go index da6918f228c95..435715006545f 100644 --- a/pkg/executor/show.go +++ b/pkg/executor/show.go @@ -61,6 +61,7 @@ import ( "github.com/pingcap/tidb/pkg/sessionctx/binloginfo" "github.com/pingcap/tidb/pkg/sessionctx/sessionstates" "github.com/pingcap/tidb/pkg/sessionctx/variable" + "github.com/pingcap/tidb/pkg/sessiontxn" "github.com/pingcap/tidb/pkg/store/helper" "github.com/pingcap/tidb/pkg/table" "github.com/pingcap/tidb/pkg/table/tables" @@ -2377,5 +2378,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) } diff --git a/pkg/planner/core/logical_plan_builder.go b/pkg/planner/core/logical_plan_builder.go index 23ba5c2fe0e35..4384c17e3d31f 100644 --- a/pkg/planner/core/logical_plan_builder.go +++ b/pkg/planner/core/logical_plan_builder.go @@ -74,6 +74,7 @@ import ( "github.com/pingcap/tidb/pkg/util/set" "github.com/pingcap/tidb/pkg/util/size" "github.com/pingcap/tipb/go-tipb" + "go.uber.org/zap" ) const ( @@ -5153,6 +5154,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, plannererrors.ErrViewRecursive) && terror.ErrorNotEqual(err, plannererrors.ErrNoSuchTable) && terror.ErrorNotEqual(err, plannererrors.ErrInternal) && diff --git a/pkg/table/tables/tables_test.go b/pkg/table/tables/tables_test.go index 1cb6c3782705f..78151ac9d3628 100644 --- a/pkg/table/tables/tables_test.go +++ b/pkg/table/tables/tables_test.go @@ -690,11 +690,15 @@ 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").Sort().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.v1' 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")) }