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

*: add session variable "tidb_enable_cascades_planner" #7879

Merged
merged 6 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
Expand Down Expand Up @@ -174,7 +175,7 @@ func (a *ExecStmt) RebuildPlan() (int64, error) {
if err := plannercore.Preprocess(a.Ctx, a.StmtNode, is, false); err != nil {
return 0, errors.Trace(err)
}
p, err := plannercore.Optimize(a.Ctx, a.StmtNode, is)
p, err := planner.Optimize(a.Ctx, a.StmtNode, is)
if err != nil {
return 0, errors.Trace(err)
}
Expand Down
3 changes: 2 additions & 1 deletion executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pkg/errors"
Expand All @@ -45,7 +46,7 @@ func (c *Compiler) Compile(ctx context.Context, stmtNode ast.StmtNode) (*ExecStm
return nil, errors.Trace(err)
}

finalPlan, err := plannercore.Optimize(c.Ctx, stmtNode, infoSchema)
finalPlan, err := planner.Optimize(c.Ctx, stmtNode, infoSchema)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
3 changes: 2 additions & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
Expand Down Expand Up @@ -1738,7 +1739,7 @@ func (s *testSuite) TestIsPointGet(c *C) {
c.Check(err, IsNil)
err = plannercore.Preprocess(ctx, stmtNode, infoSchema, false)
c.Check(err, IsNil)
p, err := plannercore.Optimize(ctx, stmtNode, infoSchema)
p, err := planner.Optimize(ctx, stmtNode, infoSchema)
c.Check(err, IsNil)
ret := executor.IsPointGetWithPKOrUniqueKeyByAutoCommit(ctx, p)
c.Assert(ret, Equals, result)
Expand Down
3 changes: 2 additions & 1 deletion executor/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/testkit"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (s *testSuite) TestStmtLabel(c *C) {
is := executor.GetInfoSchema(tk.Se)
err = plannercore.Preprocess(tk.Se.(sessionctx.Context), stmtNode, is, false)
c.Assert(err, IsNil)
_, err = plannercore.Optimize(tk.Se, stmtNode, is)
_, err = planner.Optimize(tk.Se, stmtNode, is)
c.Assert(err, IsNil)
c.Assert(executor.GetStmtLabel(stmtNode), Equals, tt.label)
}
Expand Down
3 changes: 2 additions & 1 deletion executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
Expand Down Expand Up @@ -248,7 +249,7 @@ func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args ...inter
execStmt.UsingVars[i] = ast.NewValueExpr(val)
}
is := GetInfoSchema(ctx)
execPlan, err := plannercore.Optimize(ctx, execStmt, is)
execPlan, err := planner.Optimize(ctx, execStmt, is)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
3 changes: 2 additions & 1 deletion executor/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/opentracing/basictracer-go"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/tracing"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (e *TraceExec) Next(ctx context.Context, chk *chunk.Chunk) error {

// record how much time was spent for optimizeing plan
optimizeSp := e.rootTrace.Tracer().StartSpan("plan_optimize", opentracing.FollowsFrom(e.rootTrace.Context()))
stmtPlan, err := plannercore.Optimize(e.builder.ctx, e.stmtNode, e.builder.is)
stmtPlan, err := planner.Optimize(e.builder.ctx, e.stmtNode, e.builder.is)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/planner"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
Expand Down Expand Up @@ -323,7 +324,7 @@ func (s *testAnalyzeSuite) TestIndexRead(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is, false)
c.Assert(err, IsNil)
p, err := core.Optimize(ctx, stmt, is)
p, err := planner.Optimize(ctx, stmt, is)
c.Assert(err, IsNil)
c.Assert(core.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
Expand Down Expand Up @@ -373,7 +374,7 @@ func (s *testAnalyzeSuite) TestEmptyTable(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is, false)
c.Assert(err, IsNil)
p, err := core.Optimize(ctx, stmt, is)
p, err := planner.Optimize(ctx, stmt, is)
c.Assert(err, IsNil)
c.Assert(core.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
Expand Down Expand Up @@ -485,7 +486,7 @@ func (s *testAnalyzeSuite) TestAnalyze(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is, false)
c.Assert(err, IsNil)
p, err := core.Optimize(ctx, stmt, is)
p, err := planner.Optimize(ctx, stmt, is)
c.Assert(err, IsNil)
c.Assert(core.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
Expand Down Expand Up @@ -561,7 +562,7 @@ func (s *testAnalyzeSuite) TestPreparedNullParam(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is, true)
c.Assert(err, IsNil)
p, err := core.Optimize(ctx, stmt, is)
p, err := planner.Optimize(ctx, stmt, is)
c.Assert(err, IsNil)

c.Assert(core.ToString(p), Equals, best, Commentf("for %s", sql))
Expand Down Expand Up @@ -812,7 +813,7 @@ func BenchmarkOptimize(b *testing.B) {
b.Run(tt.sql, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := core.Optimize(ctx, stmt, is)
_, err := planner.Optimize(ctx, stmt, is)
c.Assert(err, IsNil)
}
b.ReportAllocs()
Expand Down
5 changes: 3 additions & 2 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ type Execute struct {
Plan Plan
}

func (e *Execute) optimizePreparedPlan(ctx sessionctx.Context, is infoschema.InfoSchema) error {
// OptimizePreparedPlan optimizes the prepared statement.
func (e *Execute) OptimizePreparedPlan(ctx sessionctx.Context, is infoschema.InfoSchema) error {
vars := ctx.GetSessionVars()
if e.Name != "" {
e.ExecID = vars.PreparedStmtNameToID[e.Name]
Expand Down Expand Up @@ -191,7 +192,7 @@ func (e *Execute) getPhysicalPlan(ctx sessionctx.Context, is infoschema.InfoSche
return plan, nil
}
}
p, err := Optimize(ctx, prepared.Stmt, is)
p, err := OptimizeAstNode(ctx, prepared.Stmt, is)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
20 changes: 10 additions & 10 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func evalAstExpr(ctx sessionctx.Context, expr ast.ExprNode) (types.Datum, error)
if val, ok := expr.(*ast.ValueExpr); ok {
return val.Datum, nil
}
b := &planBuilder{
b := &PlanBuilder{
ctx: ctx,
colMapper: make(map[*ast.ColumnNameExpr]int),
}
Expand All @@ -53,7 +53,7 @@ func evalAstExpr(ctx sessionctx.Context, expr ast.ExprNode) (types.Datum, error)
return newExpr.Eval(chunk.Row{})
}

func (b *planBuilder) rewriteInsertOnDuplicateUpdate(exprNode ast.ExprNode, mockPlan LogicalPlan, insertPlan *Insert) (expression.Expression, error) {
func (b *PlanBuilder) rewriteInsertOnDuplicateUpdate(exprNode ast.ExprNode, mockPlan LogicalPlan, insertPlan *Insert) (expression.Expression, error) {
b.rewriterCounter++
defer func() { b.rewriterCounter-- }()

Expand All @@ -77,15 +77,15 @@ func (b *planBuilder) rewriteInsertOnDuplicateUpdate(exprNode ast.ExprNode, mock
// aggMapper maps ast.AggregateFuncExpr to the columns offset in p's output schema.
// asScalar means whether this expression must be treated as a scalar expression.
// And this function returns a result expression, a new plan that may have apply or semi-join.
func (b *planBuilder) rewrite(exprNode ast.ExprNode, p LogicalPlan, aggMapper map[*ast.AggregateFuncExpr]int, asScalar bool) (expression.Expression, LogicalPlan, error) {
func (b *PlanBuilder) rewrite(exprNode ast.ExprNode, p LogicalPlan, aggMapper map[*ast.AggregateFuncExpr]int, asScalar bool) (expression.Expression, LogicalPlan, error) {
expr, resultPlan, err := b.rewriteWithPreprocess(exprNode, p, aggMapper, asScalar, nil)
return expr, resultPlan, errors.Trace(err)
}

// rewriteWithPreprocess is for handling the situation that we need to adjust the input ast tree
// before really using its node in `expressionRewriter.Leave`. In that case, we first call
// er.preprocess(expr), which returns a new expr. Then we use the new expr in `Leave`.
func (b *planBuilder) rewriteWithPreprocess(exprNode ast.ExprNode, p LogicalPlan, aggMapper map[*ast.AggregateFuncExpr]int, asScalar bool, preprocess func(ast.Node) ast.Node) (expression.Expression, LogicalPlan, error) {
func (b *PlanBuilder) rewriteWithPreprocess(exprNode ast.ExprNode, p LogicalPlan, aggMapper map[*ast.AggregateFuncExpr]int, asScalar bool, preprocess func(ast.Node) ast.Node) (expression.Expression, LogicalPlan, error) {
b.rewriterCounter++
defer func() { b.rewriterCounter-- }()

Expand All @@ -106,7 +106,7 @@ func (b *planBuilder) rewriteWithPreprocess(exprNode ast.ExprNode, p LogicalPlan
return expr, resultPlan, errors.Trace(err)
}

func (b *planBuilder) getExpressionRewriter(p LogicalPlan) (rewriter *expressionRewriter) {
func (b *PlanBuilder) getExpressionRewriter(p LogicalPlan) (rewriter *expressionRewriter) {
defer func() {
if p != nil {
rewriter.schema = p.Schema()
Expand All @@ -129,7 +129,7 @@ func (b *planBuilder) getExpressionRewriter(p LogicalPlan) (rewriter *expression
return
}

func (b *planBuilder) rewriteExprNode(rewriter *expressionRewriter, exprNode ast.ExprNode, asScalar bool) (expression.Expression, LogicalPlan, error) {
func (b *PlanBuilder) rewriteExprNode(rewriter *expressionRewriter, exprNode ast.ExprNode, asScalar bool) (expression.Expression, LogicalPlan, error) {
exprNode.Accept(rewriter)
if rewriter.err != nil {
return nil, nil, errors.Trace(rewriter.err)
Expand All @@ -153,7 +153,7 @@ type expressionRewriter struct {
schema *expression.Schema
err error
aggrMap map[*ast.AggregateFuncExpr]int
b *planBuilder
b *PlanBuilder
ctx sessionctx.Context

// asScalar indicates the return value must be a scalar value.
Expand Down Expand Up @@ -552,7 +552,7 @@ func (er *expressionRewriter) handleExistSubquery(v *ast.ExistsSubqueryExpr) (as
}
er.ctxStack = append(er.ctxStack, er.p.Schema().Columns[er.p.Schema().Len()-1])
} else {
physicalPlan, err := doOptimize(er.b.optFlag, np)
physicalPlan, err := DoOptimize(er.b.optFlag, np)
if err != nil {
er.err = errors.Trace(err)
return v, true
Expand Down Expand Up @@ -621,7 +621,7 @@ func (er *expressionRewriter) handleInSubquery(v *ast.PatternInExpr) (ast.Node,
// TODO: Now we cannot add it to CBO framework. Instead, user can set a session variable to open this optimization.
// We will improve our CBO framework in future.
if lLen == 1 && er.ctx.GetSessionVars().AllowInSubqueryUnFolding && len(np.extractCorrelatedCols()) == 0 {
physicalPlan, err1 := doOptimize(er.b.optFlag, np)
physicalPlan, err1 := DoOptimize(er.b.optFlag, np)
if err1 != nil {
er.err = errors.Trace(err1)
return v, true
Expand Down Expand Up @@ -709,7 +709,7 @@ func (er *expressionRewriter) handleScalarSubquery(v *ast.SubqueryExpr) (ast.Nod
}
return v, true
}
physicalPlan, err := doOptimize(er.b.optFlag, np)
physicalPlan, err := DoOptimize(er.b.optFlag, np)
if err != nil {
er.err = errors.Trace(err)
return v, true
Expand Down
Loading