forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_best_task_test.go
274 lines (247 loc) · 9.03 KB
/
find_best_task_test.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2020 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 core
import (
"fmt"
"math"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/sessionctx"
)
var _ = Suite(&testFindBestTaskSuite{})
type testFindBestTaskSuite struct {
ctx sessionctx.Context
}
func (s *testFindBestTaskSuite) SetUpSuite(c *C) {
s.ctx = MockContext()
}
type mockDataSource struct {
baseLogicalPlan
}
func (ds mockDataSource) Init(ctx sessionctx.Context) *mockDataSource {
ds.baseLogicalPlan = newBaseLogicalPlan(ctx, "mockDS", &ds, 0)
return &ds
}
func (ds *mockDataSource) findBestTask(prop *property.PhysicalProperty) (task, error) {
// It can satisfy any of the property!
// Just use a TableDual for convenience.
p := PhysicalTableDual{}.Init(ds.ctx, &property.StatsInfo{RowCount: 1}, 0)
task := &rootTask{
p: p,
cst: 10000,
}
return task, nil
}
// mockLogicalPlan4Test is a LogicalPlan which is used for unit test.
// The basic assumption:
// 1. mockLogicalPlan4Test can generate tow kinds of physical plan: physicalPlan1 and
// physicalPlan2. physicalPlan1 can pass the property only when they are the same
// order; while physicalPlan2 cannot match any of the property(in other words, we can
// generate it only when then property is empty).
// 2. We have a hint for physicalPlan2.
// 3. If the property is empty, we still need to check `canGeneratePlan2` to decide
// whether it can generate physicalPlan2.
type mockLogicalPlan4Test struct {
baseLogicalPlan
// hasHintForPlan2 indicates whether this mockPlan contains hint.
// This hint is used to generate physicalPlan2. See the implementation
// of exhaustPhysicalPlans().
hasHintForPlan2 bool
// canGeneratePlan2 indicates whether this plan can generate physicalPlan2.
canGeneratePlan2 bool
// costOverflow indicates whether this plan will generate physical plan whose cost is overflowed.
costOverflow bool
}
func (p mockLogicalPlan4Test) Init(ctx sessionctx.Context) *mockLogicalPlan4Test {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, "mockPlan", &p, 0)
return &p
}
func (p *mockLogicalPlan4Test) getPhysicalPlan1(prop *property.PhysicalProperty) PhysicalPlan {
physicalPlan1 := mockPhysicalPlan4Test{planType: 1, costOverflow: p.costOverflow}.Init(p.ctx)
physicalPlan1.stats = &property.StatsInfo{RowCount: 1}
physicalPlan1.childrenReqProps = make([]*property.PhysicalProperty, 1)
physicalPlan1.childrenReqProps[0] = prop.Clone()
return physicalPlan1
}
func (p *mockLogicalPlan4Test) getPhysicalPlan2(prop *property.PhysicalProperty) PhysicalPlan {
physicalPlan2 := mockPhysicalPlan4Test{planType: 2, costOverflow: p.costOverflow}.Init(p.ctx)
physicalPlan2.stats = &property.StatsInfo{RowCount: 1}
physicalPlan2.childrenReqProps = make([]*property.PhysicalProperty, 1)
physicalPlan2.childrenReqProps[0] = property.NewPhysicalProperty(prop.TaskTp, nil, false, prop.ExpectedCnt, false)
return physicalPlan2
}
func (p *mockLogicalPlan4Test) exhaustPhysicalPlans(prop *property.PhysicalProperty) ([]PhysicalPlan, bool) {
plan1 := make([]PhysicalPlan, 0, 1)
plan2 := make([]PhysicalPlan, 0, 1)
if prop.IsEmpty() && p.canGeneratePlan2 {
// Generate PhysicalPlan2 when the property is empty.
plan2 = append(plan2, p.getPhysicalPlan2(prop))
if p.hasHintForPlan2 {
return plan2, true
}
}
if all, _ := prop.AllSameOrder(); all {
// Generate PhysicalPlan1 when properties are the same order.
plan1 = append(plan1, p.getPhysicalPlan1(prop))
}
if p.hasHintForPlan2 {
// The hint cannot work.
if prop.IsEmpty() {
p.ctx.GetSessionVars().StmtCtx.AppendWarning(fmt.Errorf("the hint is inapplicable for plan2"))
}
return plan1, false
}
return append(plan1, plan2...), true
}
type mockPhysicalPlan4Test struct {
basePhysicalPlan
// 1 or 2 for physicalPlan1 or physicalPlan2.
// See the comment of mockLogicalPlan4Test.
planType int
costOverflow bool
}
func (p mockPhysicalPlan4Test) Init(ctx sessionctx.Context) *mockPhysicalPlan4Test {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, "mockPlan", &p, 0)
return &p
}
func (p *mockPhysicalPlan4Test) attach2Task(tasks ...task) task {
t := tasks[0].copy()
attachPlan2Task(p, t)
if p.costOverflow {
t.addCost(math.MaxFloat64)
} else {
t.addCost(1)
}
return t
}
func (s *testFindBestTaskSuite) TestCostOverflow(c *C) {
ctx := MockContext()
// Plan Tree: mockPlan -> mockDataSource
mockPlan := mockLogicalPlan4Test{costOverflow: true}.Init(ctx)
mockDS := mockDataSource{}.Init(ctx)
mockPlan.SetChildren(mockDS)
// An empty property is enough for this test.
prop := property.NewPhysicalProperty(property.RootTaskType, nil, false, 0, false)
t, err := mockPlan.findBestTask(prop)
c.Assert(err, IsNil)
// The cost should be overflowed, but the task shouldn't be invalid.
c.Assert(t.invalid(), IsFalse)
c.Assert(t.cost(), Equals, math.MaxFloat64)
}
func (s *testFindBestTaskSuite) TestEnforcedProperty(c *C) {
ctx := MockContext()
// PlanTree : mockLogicalPlan -> mockDataSource
mockPlan := mockLogicalPlan4Test{}.Init(ctx)
mockDS := mockDataSource{}.Init(ctx)
mockPlan.SetChildren(mockDS)
col0 := &expression.Column{UniqueID: 1}
col1 := &expression.Column{UniqueID: 2}
// Use different order, so that mockLogicalPlan cannot generate any of the
// physical plans.
item0 := property.Item{Col: col0, Desc: false}
item1 := property.Item{Col: col1, Desc: true}
items := []property.Item{item0, item1}
prop0 := &property.PhysicalProperty{
Items: items,
Enforced: false,
}
// should return invalid task because no physical plan can match this property.
task, err := mockPlan.findBestTask(prop0)
c.Assert(err, IsNil)
c.Assert(task.invalid(), IsTrue)
prop1 := &property.PhysicalProperty{
Items: items,
Enforced: true,
}
// should return the valid task when the property is enforced.
task, err = mockPlan.findBestTask(prop1)
c.Assert(err, IsNil)
c.Assert(task.invalid(), IsFalse)
}
func (s *testFindBestTaskSuite) TestHintCannotFitProperty(c *C) {
ctx := MockContext()
// PlanTree : mockLogicalPlan -> mockDataSource
mockPlan0 := mockLogicalPlan4Test{
hasHintForPlan2: true,
canGeneratePlan2: true,
}.Init(ctx)
mockDS := mockDataSource{}.Init(ctx)
mockPlan0.SetChildren(mockDS)
col0 := &expression.Column{UniqueID: 1}
item0 := property.Item{Col: col0}
items := []property.Item{item0}
// case 1, The property is not empty and enforced, should enforce a sort.
prop0 := &property.PhysicalProperty{
Items: items,
Enforced: true,
}
task, err := mockPlan0.findBestTask(prop0)
c.Assert(err, IsNil)
c.Assert(task.invalid(), IsFalse)
_, enforcedSort := task.plan().(*PhysicalSort)
c.Assert(enforcedSort, IsTrue)
plan2 := task.plan().Children()[0]
mockPhysicalPlan, ok := plan2.(*mockPhysicalPlan4Test)
c.Assert(ok, IsTrue)
c.Assert(mockPhysicalPlan.planType, Equals, 2)
// case 2, The property is not empty but not enforced, still need to enforce a sort
// to ensure the hint can work
prop1 := &property.PhysicalProperty{
Items: items,
Enforced: false,
}
task, err = mockPlan0.findBestTask(prop1)
c.Assert(err, IsNil)
c.Assert(task.invalid(), IsFalse)
_, enforcedSort = task.plan().(*PhysicalSort)
c.Assert(enforcedSort, IsTrue)
plan2 = task.plan().Children()[0]
mockPhysicalPlan, ok = plan2.(*mockPhysicalPlan4Test)
c.Assert(ok, IsTrue)
c.Assert(mockPhysicalPlan.planType, Equals, 2)
// case 3, The hint cannot work even if the property is empty, should return a warning
// and generate physicalPlan1.
prop2 := &property.PhysicalProperty{
Items: items,
Enforced: false,
}
mockPlan1 := mockLogicalPlan4Test{
hasHintForPlan2: true,
canGeneratePlan2: false,
}.Init(ctx)
mockPlan1.SetChildren(mockDS)
task, err = mockPlan1.findBestTask(prop2)
c.Assert(err, IsNil)
c.Assert(task.invalid(), IsFalse)
c.Assert(ctx.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
// Because physicalPlan1 can match the property, so we should get it.
mockPhysicalPlan, ok = task.plan().(*mockPhysicalPlan4Test)
c.Assert(ok, IsTrue)
c.Assert(mockPhysicalPlan.planType, Equals, 1)
// case 4, Similar to case 3, but the property is enforced now. Ths result should be
// the same with case 3.
ctx.GetSessionVars().StmtCtx.SetWarnings(nil)
prop3 := &property.PhysicalProperty{
Items: items,
Enforced: true,
}
task, err = mockPlan1.findBestTask(prop3)
c.Assert(err, IsNil)
c.Assert(task.invalid(), IsFalse)
c.Assert(ctx.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
// Because physicalPlan1 can match the property, so we don't need to enforce a sort.
mockPhysicalPlan, ok = task.plan().(*mockPhysicalPlan4Test)
c.Assert(ok, IsTrue)
c.Assert(mockPhysicalPlan.planType, Equals, 1)
}