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

planner: skip the optimizer for the execute statement #36612

Merged
merged 28 commits into from
Aug 8, 2022
Merged
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
36bf288
planner: skip the optimizer for the execute statement
Reminiscent Jul 27, 2022
547d9ca
refactor the code
Reminiscent Jul 27, 2022
6d1f37b
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Jul 27, 2022
f97dba5
fix ut
Reminiscent Jul 28, 2022
a66a837
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Jul 28, 2022
48d416d
fixut
Reminiscent Jul 28, 2022
3fbbfa2
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Jul 28, 2022
3f8dcd0
Merge branch 'master' into refactor-PC-optimize
qw4990 Jul 29, 2022
13e754f
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Jul 29, 2022
9751479
address comments
Reminiscent Aug 3, 2022
0942abb
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Aug 3, 2022
4ed462b
Merge remote-tracking branch 'origin/refactor-PC-optimize' into refac…
Reminiscent Aug 3, 2022
58ef07f
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Aug 4, 2022
a40f780
fix ut
Reminiscent Aug 4, 2022
41810fd
Merge branch 'master' into refactor-PC-optimize
Reminiscent Aug 4, 2022
ece4f7d
fix ut
Reminiscent Aug 5, 2022
2c554b6
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Aug 5, 2022
91f5574
Merge remote-tracking branch 'origin/refactor-PC-optimize' into refac…
Reminiscent Aug 5, 2022
03a537d
address comments
Reminiscent Aug 5, 2022
ecdeb4a
address comments
Reminiscent Aug 5, 2022
08357a1
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Aug 5, 2022
73dcf12
address comments
Reminiscent Aug 5, 2022
bfdf902
Merge branch 'master' into refactor-PC-optimize
qw4990 Aug 8, 2022
2292bf6
Merge branch 'master' into refactor-PC-optimize
qw4990 Aug 8, 2022
0788988
address comments
Reminiscent Aug 8, 2022
886d793
Merge branch 'master' of https://github.com/pingcap/tidb into refacto…
Reminiscent Aug 8, 2022
047e375
Merge remote-tracking branch 'origin/refactor-PC-optimize' into refac…
Reminiscent Aug 8, 2022
1e16078
Merge branch 'master' into refactor-PC-optimize
qw4990 Aug 8, 2022
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
69 changes: 47 additions & 22 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
}()
}

// handle the execute statement
if execAST, ok := node.(*ast.ExecuteStmt); ok {
p, names, err := OptimizeExecStmt(ctx, sctx, execAST, is)
return p, names, err
}

tableHints := hint.ExtractTableHintsFromStmtNode(node, sctx)
originStmtHints, originStmtHintsOffs, warns := handleStmtHints(tableHints)
sessVars.StmtCtx.StmtHints = originStmtHints
Expand Down Expand Up @@ -305,31 +311,16 @@ func optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
})

// build logical plan
sctx.GetSessionVars().PlanID = 0
sctx.GetSessionVars().PlanColumnID = 0
sctx.GetSessionVars().MapHashCode2UniqueID4ExtendedCol = nil
hintProcessor := &hint.BlockHintProcessor{Ctx: sctx}
node.Accept(hintProcessor)

failpoint.Inject("mockRandomPlanID", func() {
sctx.GetSessionVars().PlanID = rand.Intn(1000) // nolint:gosec
})

builder := planBuilderPool.Get().(*core.PlanBuilder)
defer planBuilderPool.Put(builder.ResetForReuse())

builder.Init(sctx, is, hintProcessor)

// reset fields about rewrite
sctx.GetSessionVars().RewritePhaseInfo.Reset()
beginRewrite := time.Now()
p, err := builder.Build(ctx, node)
p, err := buildLogicalPlan(ctx, sctx, node, builder)
if err != nil {
return nil, nil, 0, err
}
sctx.GetSessionVars().RewritePhaseInfo.DurationRewrite = time.Since(beginRewrite)

sctx.GetSessionVars().StmtCtx.Tables = builder.GetDBTableInfo()
activeRoles := sctx.GetSessionVars().ActiveRoles
// Check privilege. Maybe it's better to move this to the Preprocess, but
// we need the table information to check privilege, which is collected
Expand All @@ -345,12 +336,6 @@ func optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
return nil, nil, 0, err
}

// Handle the execute statement.
if execPlan, ok := p.(*core.Execute); ok {
err := execPlan.OptimizePreparedPlan(ctx, sctx, is)
return p, p.OutputNames(), 0, err
}

names := p.OutputNames()

// Handle the non-logical plan statement.
Expand All @@ -371,6 +356,46 @@ func optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
return finalPlan, names, cost, err
}

// OptimizeExecStmt to handle the "execute" statement
func OptimizeExecStmt(ctx context.Context, sctx sessionctx.Context,
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
execAst *ast.ExecuteStmt, is infoschema.InfoSchema) (core.Plan, types.NameSlice, error) {
builder := planBuilderPool.Get().(*core.PlanBuilder)
defer planBuilderPool.Put(builder.ResetForReuse())
builder.Init(sctx, is, nil)

p, err := buildLogicalPlan(ctx, sctx, execAst, builder)
if err != nil {
return nil, nil, err
}
if execPlan, ok := p.(*core.Execute); ok {
err = execPlan.OptimizePreparedPlan(ctx, sctx, is)
return execPlan, execPlan.OutputNames(), err
}
err = errors.Errorf("invalid result plan type, should be Execute")
return nil, nil, err
}

func buildLogicalPlan(ctx context.Context, sctx sessionctx.Context, node ast.Node, builder *core.PlanBuilder) (core.Plan, error) {
sctx.GetSessionVars().PlanID = 0
sctx.GetSessionVars().PlanColumnID = 0
sctx.GetSessionVars().MapHashCode2UniqueID4ExtendedCol = nil

failpoint.Inject("mockRandomPlanID", func() {
sctx.GetSessionVars().PlanID = rand.Intn(1000) // nolint:gosec
})

// reset fields about rewrite
sctx.GetSessionVars().RewritePhaseInfo.Reset()
beginRewrite := time.Now()
p, err := builder.Build(ctx, node)
if err != nil {
return nil, err
}
sctx.GetSessionVars().RewritePhaseInfo.DurationRewrite = time.Since(beginRewrite)
sctx.GetSessionVars().StmtCtx.Tables = builder.GetDBTableInfo()
return p, nil
}

// ExtractSelectAndNormalizeDigest extract the select statement and normalize it.
func ExtractSelectAndNormalizeDigest(stmtNode ast.StmtNode, specifiledDB string) (ast.StmtNode, string, string, error) {
switch x := stmtNode.(type) {
Expand Down