81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
// Copyright 2018 PingCAP, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package planner
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pingcap/parser/ast"
|
|
"github.com/pingcap/tidb/infoschema"
|
|
"github.com/pingcap/tidb/planner/cascades"
|
|
plannercore "github.com/pingcap/tidb/planner/core"
|
|
"github.com/pingcap/tidb/privilege"
|
|
"github.com/pingcap/tidb/sessionctx"
|
|
)
|
|
|
|
// Optimize does optimization and creates a Plan.
|
|
// The node must be prepared first.
|
|
func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is infoschema.InfoSchema) (plannercore.Plan, error) {
|
|
fp := plannercore.TryFastPlan(sctx, node)
|
|
if fp != nil {
|
|
return fp, nil
|
|
}
|
|
|
|
// build logical plan
|
|
sctx.GetSessionVars().PlanID = 0
|
|
sctx.GetSessionVars().PlanColumnID = 0
|
|
builder := plannercore.NewPlanBuilder(sctx, is)
|
|
p, err := builder.Build(ctx, node)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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
|
|
// into the visitInfo in the logical plan builder.
|
|
if pm := privilege.GetPrivilegeManager(sctx); pm != nil {
|
|
if err := plannercore.CheckPrivilege(activeRoles, pm, builder.GetVisitInfo()); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if err := plannercore.CheckTableLock(sctx, is, builder.GetVisitInfo()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Handle the execute statement.
|
|
if execPlan, ok := p.(*plannercore.Execute); ok {
|
|
err := execPlan.OptimizePreparedPlan(ctx, sctx, is)
|
|
return p, err
|
|
}
|
|
|
|
// Handle the non-logical plan statement.
|
|
logic, isLogicalPlan := p.(plannercore.LogicalPlan)
|
|
if !isLogicalPlan {
|
|
return p, nil
|
|
}
|
|
|
|
// Handle the logical plan statement, use cascades planner if enabled.
|
|
if sctx.GetSessionVars().EnableCascadesPlanner {
|
|
return cascades.FindBestPlan(sctx, logic)
|
|
}
|
|
return plannercore.DoOptimize(ctx, builder.GetOptFlag(), logic)
|
|
}
|
|
|
|
func init() {
|
|
plannercore.OptimizeAstNode = Optimize
|
|
}
|