forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize.go
72 lines (63 loc) · 2.2 KB
/
optimize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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 (
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/infoschema"
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 sessionctx.Context, node ast.Node, is infoschema.InfoSchema) (plannercore.Plan, error) {
fp := plannercore.TryFastPlan(ctx, node)
if fp != nil {
return fp, nil
}
// build logical plan
ctx.GetSessionVars().PlanID = 0
ctx.GetSessionVars().PlanColumnID = 0
builder := plannercore.NewPlanBuilder(ctx, is)
p, err := builder.Build(node)
if err != nil {
return nil, err
}
// 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(ctx); pm != nil {
if !plannercore.CheckPrivilege(pm, builder.GetVisitInfo()) {
return nil, errors.New("privilege check fail")
}
}
// Handle the execute statement.
if execPlan, ok := p.(*plannercore.Execute); ok {
err := execPlan.OptimizePreparedPlan(ctx, is)
return p, errors.Trace(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 ctx.GetSessionVars().EnableCascadesPlanner {
return nil, errors.New("the cascades planner is not implemented yet")
}
return plannercore.DoOptimize(builder.GetOptFlag(), logic)
}
func init() {
plannercore.OptimizeAstNode = Optimize
}