-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
implementation_rules.go
101 lines (90 loc) · 3.55 KB
/
implementation_rules.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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 cascades
import (
plannercore "github.com/pingcap/tidb/planner/core"
impl "github.com/pingcap/tidb/planner/implementation"
"github.com/pingcap/tidb/planner/memo"
"github.com/pingcap/tidb/planner/property"
)
// ImplementationRule defines the interface for implementation rules.
type ImplementationRule interface {
// Match checks if current GroupExpr matches this rule under required physical property.
Match(expr *memo.GroupExpr, prop *property.PhysicalProperty) (matched bool)
// OnImplement generates physical plan using this rule for current GroupExpr. Note that
// childrenReqProps of generated physical plan should be set correspondingly in this function.
OnImplement(expr *memo.GroupExpr, reqProp *property.PhysicalProperty) (memo.Implementation, error)
}
// GetImplementationRules gets the all the candidate implementation rules based
// on the logical plan node.
func GetImplementationRules(node plannercore.LogicalPlan) []ImplementationRule {
operand := memo.GetOperand(node)
return implementationMap[operand]
}
var implementationMap = map[memo.Operand][]ImplementationRule{
/**
OperandSelection: []ImplementationRule{
nil,
},
OperandProjection: []ImplementationRule{
nil,
},
*/
memo.OperandTableDual: {
&ImplTableDual{},
},
memo.OperandProjection: {
&ImplProjection{},
},
}
// ImplTableDual implements LogicalTableDual as PhysicalTableDual.
type ImplTableDual struct {
}
// Match implements ImplementationRule Match interface.
func (r *ImplTableDual) Match(expr *memo.GroupExpr, prop *property.PhysicalProperty) (matched bool) {
if !prop.IsEmpty() {
return false
}
return true
}
// OnImplement implements ImplementationRule OnImplement interface.
func (r *ImplTableDual) OnImplement(expr *memo.GroupExpr, reqProp *property.PhysicalProperty) (memo.Implementation, error) {
logicProp := expr.Group.Prop
logicDual := expr.ExprNode.(*plannercore.LogicalTableDual)
dual := plannercore.PhysicalTableDual{RowCount: logicDual.RowCount}.Init(logicDual.Context(), logicProp.Stats)
dual.SetSchema(logicProp.Schema)
return impl.NewTableDualImpl(dual), nil
}
// ImplProjection implements LogicalProjection as PhysicalProjection.
type ImplProjection struct {
}
// Match implements ImplementationRule Match interface.
func (r *ImplProjection) Match(expr *memo.GroupExpr, prop *property.PhysicalProperty) (matched bool) {
return true
}
// OnImplement implements ImplementationRule OnImplement interface.
func (r *ImplProjection) OnImplement(expr *memo.GroupExpr, reqProp *property.PhysicalProperty) (memo.Implementation, error) {
logicProp := expr.Group.Prop
logicProj := expr.ExprNode.(*plannercore.LogicalProjection)
childProp, ok := logicProj.TryToGetChildProp(reqProp)
if !ok {
return nil, nil
}
proj := plannercore.PhysicalProjection{
Exprs: logicProj.Exprs,
CalculateNoDelay: logicProj.CalculateNoDelay,
AvoidColumnEvaluator: logicProj.AvoidColumnEvaluator,
}.Init(logicProj.Context(), logicProp.Stats.ScaleByExpectCnt(reqProp.ExpectedCnt), childProp)
proj.SetSchema(logicProp.Schema)
return impl.NewProjectionImpl(proj), nil
}