forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheliminate_projection.go
63 lines (59 loc) · 2.1 KB
/
eliminate_projection.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
// 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/pingcap/tidb/expression"
)
// EliminateProjection eliminates projection operator to avoid the cost of memory copy in the iterator of projection.
func EliminateProjection(p PhysicalPlan) PhysicalPlan {
switch plan := p.(type) {
case *Projection:
if !projectionCanBeEliminated(plan) {
break
}
child := p.GetChildByIndex(0).(PhysicalPlan)
child.SetSchema(plan.GetSchema())
RemovePlan(p)
p = EliminateProjection(child)
}
children := make([]Plan, 0, len(p.GetChildren()))
for _, child := range p.GetChildren() {
children = append(children, EliminateProjection(child.(PhysicalPlan)))
}
p.SetChildren(children...)
return p
}
// projectionCanBeEliminated checks if a PROJECTION operator can be eliminated.
// PROJECTION operator can be eliminated when meet the following conditions at the same time:
// 1. fields of PROJECTION are all columns
// 2. fields of PROJECTION are just the same as the schema of the child operator (including order, amount, etc.).
// expressions like following cases can not be eliminated:
// "SELECT b, a FROM t",
// or "SELECT c AS a, c AS b FROM t WHERE d = 1",
// or "SELECT t1.a, t2.b, t1.b, t2.a FROM t1, t2 WHERE t1.a < 0 AND t2.b > 0".
func projectionCanBeEliminated(p *Projection) bool {
child := p.GetChildByIndex(0).(PhysicalPlan)
if len(p.GetSchema()) != len(child.GetSchema()) {
return false
}
for i, expr := range p.Exprs {
col, ok := expr.(*expression.Column)
if !ok {
return false
}
if col.FromID != child.GetSchema()[i].FromID || col.Position != child.GetSchema()[i].Position {
return false
}
}
return true
}