From 1bbc0e9fb304d7500173c37e9a4c3dc08e54ef94 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Tue, 23 Jul 2024 18:36:04 +0800 Subject: [PATCH] This is an automated cherry-pick of #54791 Signed-off-by: ti-chi-bot --- executor/infoschema_reader.go | 6 ++- executor/show.go | 71 ++++++++++++++++++++++++++++ planner/core/logical_plan_builder.go | 63 ++++++++++++++++++++++++ table/tables/tables_test.go | 4 ++ 4 files changed, 143 insertions(+), 1 deletion(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 208d0be40cacd..79150091284b0 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -828,7 +828,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] @@ -837,7 +836,12 @@ 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 { +<<<<<<< HEAD:executor/infoschema_reader.go planBuilder, _ := plannercore.NewPlanBuilder().Init(s, is, &hint.BlockHintProcessor{}) +======= + is := sessiontxn.GetTxnManager(s).GetTxnInfoSchema() + planBuilder, _ := plannercore.NewPlanBuilder().Init(s.GetPlanCtx(), is, hint.NewQBHintHandler(nil)) +>>>>>>> b19a91817c5 (*: isolate more variables for `runWithSystemSession` (#54791)):pkg/executor/infoschema_reader.go var err error viewLogicalPlan, err = planBuilder.BuildDataSourceFromView(ctx, schema.Name, tbl, nil, nil) return errors.Trace(err) diff --git a/executor/show.go b/executor/show.go index 7c0976c5268c9..ff2995831a804 100644 --- a/executor/show.go +++ b/executor/show.go @@ -26,6 +26,7 @@ import ( "time" "github.com/pingcap/errors" +<<<<<<< HEAD:executor/show.go "github.com/pingcap/tidb/bindinfo" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" @@ -72,6 +73,64 @@ import ( "github.com/pingcap/tidb/util/sqlexec" "github.com/pingcap/tidb/util/stringutil" "golang.org/x/exp/slices" +======= + "github.com/pingcap/tidb/br/pkg/utils" + "github.com/pingcap/tidb/pkg/bindinfo" + "github.com/pingcap/tidb/pkg/config" + "github.com/pingcap/tidb/pkg/ddl" + fstorage "github.com/pingcap/tidb/pkg/disttask/framework/storage" + "github.com/pingcap/tidb/pkg/disttask/importinto" + "github.com/pingcap/tidb/pkg/domain" + "github.com/pingcap/tidb/pkg/domain/infosync" + "github.com/pingcap/tidb/pkg/executor/importer" + "github.com/pingcap/tidb/pkg/executor/internal/exec" + "github.com/pingcap/tidb/pkg/expression" + "github.com/pingcap/tidb/pkg/infoschema" + "github.com/pingcap/tidb/pkg/kv" + "github.com/pingcap/tidb/pkg/meta/autoid" + "github.com/pingcap/tidb/pkg/parser" + "github.com/pingcap/tidb/pkg/parser/ast" + "github.com/pingcap/tidb/pkg/parser/auth" + "github.com/pingcap/tidb/pkg/parser/charset" + parserformat "github.com/pingcap/tidb/pkg/parser/format" + "github.com/pingcap/tidb/pkg/parser/model" + "github.com/pingcap/tidb/pkg/parser/mysql" + "github.com/pingcap/tidb/pkg/parser/terror" + "github.com/pingcap/tidb/pkg/parser/tidb" + field_types "github.com/pingcap/tidb/pkg/parser/types" + plannercore "github.com/pingcap/tidb/pkg/planner/core" + "github.com/pingcap/tidb/pkg/planner/core/base" + "github.com/pingcap/tidb/pkg/plugin" + "github.com/pingcap/tidb/pkg/privilege" + "github.com/pingcap/tidb/pkg/privilege/privileges" + "github.com/pingcap/tidb/pkg/sessionctx" + "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" + "github.com/pingcap/tidb/pkg/tidb-binlog/node" + "github.com/pingcap/tidb/pkg/types" + "github.com/pingcap/tidb/pkg/util" + "github.com/pingcap/tidb/pkg/util/chunk" + "github.com/pingcap/tidb/pkg/util/collate" + contextutil "github.com/pingcap/tidb/pkg/util/context" + "github.com/pingcap/tidb/pkg/util/dbterror/exeerrors" + "github.com/pingcap/tidb/pkg/util/dbterror/plannererrors" + "github.com/pingcap/tidb/pkg/util/etcd" + "github.com/pingcap/tidb/pkg/util/filter" + "github.com/pingcap/tidb/pkg/util/format" + "github.com/pingcap/tidb/pkg/util/hack" + "github.com/pingcap/tidb/pkg/util/hint" + "github.com/pingcap/tidb/pkg/util/memory" + "github.com/pingcap/tidb/pkg/util/sem" + "github.com/pingcap/tidb/pkg/util/set" + "github.com/pingcap/tidb/pkg/util/sqlexec" + "github.com/pingcap/tidb/pkg/util/stringutil" + "github.com/tikv/client-go/v2/oracle" +>>>>>>> b19a91817c5 (*: isolate more variables for `runWithSystemSession` (#54791)):pkg/executor/show.go ) var etcdDialTimeout = 5 * time.Second @@ -2140,6 +2199,18 @@ func runWithSystemSession(ctx context.Context, sctx sessionctx.Context, fn func( if err != nil { return err } +<<<<<<< HEAD:executor/show.go defer b.releaseSysSession(ctx, sysCtx) +======= + 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 + } +>>>>>>> b19a91817c5 (*: isolate more variables for `runWithSystemSession` (#54791)):pkg/executor/show.go return fn(sysCtx) } diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 5871dac96962f..0107b8730adde 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -27,6 +27,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/failpoint" +<<<<<<< HEAD:planner/core/logical_plan_builder.go "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/expression/aggregation" @@ -151,6 +152,56 @@ const ( HintSemiJoinRewrite = "semi_join_rewrite" // HintNoDecorrelate indicates a LogicalApply not to be decorrelated. HintNoDecorrelate = "no_decorrelate" +======= + "github.com/pingcap/tidb/pkg/domain" + "github.com/pingcap/tidb/pkg/errctx" + "github.com/pingcap/tidb/pkg/expression" + "github.com/pingcap/tidb/pkg/expression/aggregation" + exprctx "github.com/pingcap/tidb/pkg/expression/context" + "github.com/pingcap/tidb/pkg/infoschema" + "github.com/pingcap/tidb/pkg/kv" + "github.com/pingcap/tidb/pkg/parser" + "github.com/pingcap/tidb/pkg/parser/ast" + "github.com/pingcap/tidb/pkg/parser/charset" + "github.com/pingcap/tidb/pkg/parser/format" + "github.com/pingcap/tidb/pkg/parser/model" + "github.com/pingcap/tidb/pkg/parser/mysql" + "github.com/pingcap/tidb/pkg/parser/opcode" + "github.com/pingcap/tidb/pkg/parser/terror" + "github.com/pingcap/tidb/pkg/planner/core/base" + core_metrics "github.com/pingcap/tidb/pkg/planner/core/metrics" + fd "github.com/pingcap/tidb/pkg/planner/funcdep" + "github.com/pingcap/tidb/pkg/planner/property" + "github.com/pingcap/tidb/pkg/planner/util" + "github.com/pingcap/tidb/pkg/planner/util/coreusage" + "github.com/pingcap/tidb/pkg/planner/util/debugtrace" + "github.com/pingcap/tidb/pkg/planner/util/fixcontrol" + "github.com/pingcap/tidb/pkg/planner/util/tablesampler" + "github.com/pingcap/tidb/pkg/privilege" + "github.com/pingcap/tidb/pkg/sessionctx" + "github.com/pingcap/tidb/pkg/sessionctx/variable" + "github.com/pingcap/tidb/pkg/statistics" + "github.com/pingcap/tidb/pkg/table" + "github.com/pingcap/tidb/pkg/table/tables" + "github.com/pingcap/tidb/pkg/table/temptable" + "github.com/pingcap/tidb/pkg/types" + driver "github.com/pingcap/tidb/pkg/types/parser_driver" + util2 "github.com/pingcap/tidb/pkg/util" + "github.com/pingcap/tidb/pkg/util/chunk" + "github.com/pingcap/tidb/pkg/util/collate" + "github.com/pingcap/tidb/pkg/util/dbterror" + "github.com/pingcap/tidb/pkg/util/dbterror/plannererrors" + "github.com/pingcap/tidb/pkg/util/hack" + h "github.com/pingcap/tidb/pkg/util/hint" + "github.com/pingcap/tidb/pkg/util/intest" + "github.com/pingcap/tidb/pkg/util/intset" + "github.com/pingcap/tidb/pkg/util/logutil" + "github.com/pingcap/tidb/pkg/util/plancodec" + "github.com/pingcap/tidb/pkg/util/set" + "github.com/pingcap/tidb/pkg/util/size" + "github.com/pingcap/tipb/go-tipb" + "go.uber.org/zap" +>>>>>>> b19a91817c5 (*: isolate more variables for `runWithSystemSession` (#54791)):pkg/planner/core/logical_plan_builder.go ) const ( @@ -5304,6 +5355,7 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model. }() selectLogicalPlan, err := b.Build(ctx, selectNode) if err != nil { +<<<<<<< HEAD:planner/core/logical_plan_builder.go if terror.ErrorNotEqual(err, ErrViewRecursive) && terror.ErrorNotEqual(err, ErrNoSuchTable) && terror.ErrorNotEqual(err, ErrInternal) && @@ -5312,6 +5364,17 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model. terror.ErrorNotEqual(err, ErrViewNoExplain) && terror.ErrorNotEqual(err, ErrNotSupportedYet) { err = ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O) +======= + 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) && + terror.ErrorNotEqual(err, plannererrors.ErrFieldNotInGroupBy) && + terror.ErrorNotEqual(err, plannererrors.ErrMixOfGroupFuncAndFields) && + terror.ErrorNotEqual(err, plannererrors.ErrViewNoExplain) && + terror.ErrorNotEqual(err, plannererrors.ErrNotSupportedYet) { + err = plannererrors.ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O) +>>>>>>> b19a91817c5 (*: isolate more variables for `runWithSystemSession` (#54791)):pkg/planner/core/logical_plan_builder.go } return nil, err } diff --git a/table/tables/tables_test.go b/table/tables/tables_test.go index 28502e75e57e8..6d39ad55dd637 100644 --- a/table/tables/tables_test.go +++ b/table/tables/tables_test.go @@ -717,11 +717,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").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")) } }