forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.go
171 lines (159 loc) · 5.55 KB
/
pattern.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package memo
import (
plannercore "github.com/pingcap/tidb/planner/core"
)
// Operand is the node of a pattern tree, it represents a logical expression operator.
// Different from logical plan operator which holds the full information about an expression
// operator, Operand only stores the type information.
// An Operand may correspond to a concrete logical plan operator, or it can has special meaning,
// e.g, a placeholder for any logical plan operator.
type Operand int
const (
// OperandAny is a placeholder for any Operand.
OperandAny Operand = iota
// OperandJoin is the operand for LogicalJoin.
OperandJoin
// OperandAggregation is the operand for LogicalAggregation.
OperandAggregation
// OperandProjection is the operand for LogicalProjection.
OperandProjection
// OperandSelection is the operand for LogicalSelection.
OperandSelection
// OperandApply is the operand for LogicalApply.
OperandApply
// OperandMaxOneRow is the operand for LogicalMaxOneRow.
OperandMaxOneRow
// OperandTableDual is the operand for LogicalTableDual.
OperandTableDual
// OperandDataSource is the operand for DataSource.
OperandDataSource
// OperandUnionScan is the operand for LogicalUnionScan.
OperandUnionScan
// OperandUnionAll is the operand for LogicalUnionAll.
OperandUnionAll
// OperandSort is the operand for LogicalSort.
OperandSort
// OperandTopN is the operand for LogicalTopN.
OperandTopN
// OperandLock is the operand for LogicalLock.
OperandLock
// OperandLimit is the operand for LogicalLimit.
OperandLimit
// OperandTiKVSingleGather is the operand for TiKVSingleGather.
OperandTiKVSingleGather
// OperandMemTableScan is the operand for MemTableScan.
OperandMemTableScan
// OperandTableScan is the operand for TableScan.
OperandTableScan
// OperandIndexScan is the operand for IndexScan.
OperandIndexScan
// OperandShow is the operand for Show.
OperandShow
// OperandWindow is the operand for window function.
OperandWindow
// OperandUnsupported is the operand for unsupported operators.
OperandUnsupported
)
// GetOperand maps logical plan operator to Operand.
func GetOperand(p plannercore.LogicalPlan) Operand {
switch p.(type) {
case *plannercore.LogicalApply:
return OperandApply
case *plannercore.LogicalJoin:
return OperandJoin
case *plannercore.LogicalAggregation:
return OperandAggregation
case *plannercore.LogicalProjection:
return OperandProjection
case *plannercore.LogicalSelection:
return OperandSelection
case *plannercore.LogicalMaxOneRow:
return OperandMaxOneRow
case *plannercore.LogicalTableDual:
return OperandTableDual
case *plannercore.DataSource:
return OperandDataSource
case *plannercore.LogicalUnionScan:
return OperandUnionScan
case *plannercore.LogicalUnionAll:
return OperandUnionAll
case *plannercore.LogicalSort:
return OperandSort
case *plannercore.LogicalTopN:
return OperandTopN
case *plannercore.LogicalLock:
return OperandLock
case *plannercore.LogicalLimit:
return OperandLimit
case *plannercore.TiKVSingleGather:
return OperandTiKVSingleGather
case *plannercore.LogicalTableScan:
return OperandTableScan
case *plannercore.LogicalMemTable:
return OperandMemTableScan
case *plannercore.LogicalIndexScan:
return OperandIndexScan
case *plannercore.LogicalShow:
return OperandShow
case *plannercore.LogicalWindow:
return OperandWindow
default:
return OperandUnsupported
}
}
// Match checks if current Operand matches specified one.
func (o Operand) Match(t Operand) bool {
if o == OperandAny || t == OperandAny {
return true
}
if o == t {
return true
}
return false
}
// Pattern defines the match pattern for a rule. It's a tree-like structure
// which is a piece of a logical expression. Each node in the Pattern tree is
// defined by an Operand and EngineType pair.
type Pattern struct {
Operand
EngineTypeSet
Children []*Pattern
}
// Match checks whether the EngineTypeSet contains the given EngineType
// and whether the two Operands match.
func (p *Pattern) Match(o Operand, e EngineType) bool {
return p.EngineTypeSet.Contains(e) && p.Operand.Match(o)
}
// MatchOperandAny checks whether the pattern's Operand is OperandAny
// and the EngineTypeSet contains the given EngineType.
func (p *Pattern) MatchOperandAny(e EngineType) bool {
return p.EngineTypeSet.Contains(e) && p.Operand == OperandAny
}
// NewPattern creates a pattern node according to the Operand and EngineType.
func NewPattern(operand Operand, engineTypeSet EngineTypeSet) *Pattern {
return &Pattern{Operand: operand, EngineTypeSet: engineTypeSet}
}
// SetChildren sets the Children information for a pattern node.
func (p *Pattern) SetChildren(children ...*Pattern) {
p.Children = children
}
// BuildPattern builds a Pattern from Operand, EngineType and child Patterns.
// Used in GetPattern() of Transformation interface to generate a Pattern.
func BuildPattern(operand Operand, engineTypeSet EngineTypeSet, children ...*Pattern) *Pattern {
p := &Pattern{Operand: operand, EngineTypeSet: engineTypeSet}
p.Children = children
return p
}