Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#54791
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
lance6716 authored and ti-chi-bot committed Jul 26, 2024
1 parent e07d004 commit 1bbc0e9
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
6 changes: 5 additions & 1 deletion executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down
71 changes: 71 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
63 changes: 63 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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) &&
Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions table/tables/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
Expand Down

0 comments on commit 1bbc0e9

Please sign in to comment.