forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_plans.go
212 lines (176 loc) · 5 KB
/
logical_plans.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2016 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 plan
import (
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/plan/statistics"
"github.com/pingcap/tidb/util/types"
)
// JoinType contains CrossJoin, InnerJoin, LeftOuterJoin, RightOuterJoin, FullOuterJoin, SemiJoin.
type JoinType int
const (
// InnerJoin means inner join.
InnerJoin JoinType = iota
// LeftOuterJoin means left join.
LeftOuterJoin
// RightOuterJoin means right join.
RightOuterJoin
// SemiJoin means if row a in table A matches some rows in B, just output a.
SemiJoin
// SemiJoinWithAux means if row a in table A matches some rows in B, output (a, true), otherwise, output (a, false).
SemiJoinWithAux
)
// Join is the logical join plan.
type Join struct {
baseLogicalPlan
JoinType JoinType
anti bool
reordered bool
cartesianJoin bool
EqualConditions []*expression.ScalarFunction
LeftConditions []expression.Expression
RightConditions []expression.Expression
OtherConditions []expression.Expression
// DefaultValues is only used for outer join, which stands for the default values when the outer table cannot find join partner
// instead of null padding.
DefaultValues []types.Datum
}
// Projection represents a select fields plan.
type Projection struct {
baseLogicalPlan
Exprs []expression.Expression
}
// Aggregation represents an aggregate plan.
type Aggregation struct {
baseLogicalPlan
AggFuncs []expression.AggregationFunction
GroupByItems []expression.Expression
ctx context.Context
// groupByCols stores the columns that are group-by items.
groupByCols []*expression.Column
}
// Selection means a filter.
type Selection struct {
baseLogicalPlan
// Originally the WHERE or ON condition is parsed into a single expression,
// but after we converted to CNF(Conjunctive normal form), it can be
// split into a list of AND conditions.
Conditions []expression.Expression
// onTable means if this selection's child is a table scan or index scan.
onTable bool
}
// Apply gets one row from outer executor and gets one row from inner executor according to outer row.
type Apply struct {
baseLogicalPlan
InnerPlan LogicalPlan
OuterSchema expression.Schema
Checker *ApplyConditionChecker
// outerColumns is the columns that not belong to this plan.
outerColumns []*expression.Column
}
// Exists checks if a query returns result.
type Exists struct {
baseLogicalPlan
}
// MaxOneRow checks if a query returns no more than one row.
type MaxOneRow struct {
baseLogicalPlan
}
// TableDual represents a dual table plan.
type TableDual struct {
baseLogicalPlan
}
// DataSource represents a tablescan without condition push down.
type DataSource struct {
baseLogicalPlan
table *ast.TableName
Table *model.TableInfo
Columns []*model.ColumnInfo
DBName *model.CIStr
Desc bool
ctx context.Context
TableAsName *model.CIStr
LimitCount *int64
statisticTable *statistics.Table
}
// Trim trims extra columns in src rows.
type Trim struct {
baseLogicalPlan
}
// Union represents Union plan.
type Union struct {
baseLogicalPlan
}
// Sort stands for the order by plan.
type Sort struct {
baseLogicalPlan
ByItems []*ByItems
ExecLimit *Limit
}
// Update represents Update plan.
type Update struct {
baseLogicalPlan
OrderedList []*expression.Assignment
}
// Delete represents a delete plan.
type Delete struct {
baseLogicalPlan
Tables []*ast.TableName
IsMultiTable bool
}
// AddChild for parent.
func addChild(parent Plan, child Plan) {
if child == nil || parent == nil {
return
}
child.AddParent(parent)
parent.AddChild(child)
}
// InsertPlan means inserting plan between two plans.
func InsertPlan(parent Plan, child Plan, insert Plan) error {
err := child.ReplaceParent(parent, insert)
if err != nil {
return errors.Trace(err)
}
err = parent.ReplaceChild(child, insert)
if err != nil {
return errors.Trace(err)
}
insert.AddChild(child)
insert.AddParent(parent)
return nil
}
// RemovePlan means removing a plan.
func RemovePlan(p Plan) error {
parents := p.GetParents()
children := p.GetChildren()
if len(parents) > 1 || len(children) != 1 {
return SystemInternalErrorType.Gen("can't remove this plan")
}
if len(parents) == 0 {
child := children[0]
child.SetParents()
return nil
}
parent, child := parents[0], children[0]
err := parent.ReplaceChild(p, child)
if err != nil {
return errors.Trace(err)
}
err = child.ReplaceParent(p, parent)
return errors.Trace(err)
}