forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_key_info.go
207 lines (196 loc) · 6.41 KB
/
build_key_info.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
// Copyright 2017 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/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/mysql"
)
type buildKeySolver struct{}
func (s *buildKeySolver) optimize(lp LogicalPlan, _ context.Context, _ *idAllocator) (LogicalPlan, error) {
lp.buildKeyInfo()
return lp, nil
}
func (p *LogicalAggregation) buildKeyInfo() {
p.baseLogicalPlan.buildKeyInfo()
for _, key := range p.Children()[0].Schema().Keys {
indices := p.schema.ColumnsIndices(key)
if indices == nil {
continue
}
newKey := make([]*expression.Column, 0, len(key))
for _, i := range indices {
newKey = append(newKey, p.schema.Columns[i])
}
p.schema.Keys = append(p.schema.Keys, newKey)
}
if len(p.groupByCols) == len(p.GroupByItems) && len(p.GroupByItems) > 0 {
indices := p.schema.ColumnsIndices(p.groupByCols)
if indices != nil {
newKey := make([]*expression.Column, 0, len(indices))
for _, i := range indices {
newKey = append(newKey, p.schema.Columns[i])
}
p.schema.Keys = append(p.schema.Keys, newKey)
}
}
if len(p.GroupByItems) == 0 {
p.schema.MaxOneRow = true
}
}
// If a condition is the form of (uniqueKey = constant) or (uniqueKey = Correlated column), it returns at most one row.
// This function will check it.
func (p *Selection) checkMaxOneRowCond(unique expression.Expression, constOrCorCol expression.Expression) bool {
col, ok := unique.(*expression.Column)
if !ok {
return false
}
if !p.children[0].Schema().IsUniqueKey(col) {
return false
}
_, okCon := constOrCorCol.(*expression.Constant)
if okCon {
return true
}
_, okCorCol := constOrCorCol.(*expression.CorrelatedColumn)
return okCorCol
}
func (p *Selection) buildKeyInfo() {
p.baseLogicalPlan.buildKeyInfo()
p.schema.MaxOneRow = p.children[0].Schema().MaxOneRow
for _, cond := range p.Conditions {
if sf, ok := cond.(*expression.ScalarFunction); ok && sf.FuncName.L == ast.EQ {
if p.checkMaxOneRowCond(sf.GetArgs()[0], sf.GetArgs()[1]) || p.checkMaxOneRowCond(sf.GetArgs()[1], sf.GetArgs()[0]) {
p.schema.MaxOneRow = true
break
}
}
}
}
// A bijection exists between columns of a projection's schema and this projection's Exprs.
// Sometimes we need a schema made by expr of Exprs to convert a column in child's schema to a column in this projection's Schema.
func (p *Projection) buildSchemaByExprs() *expression.Schema {
schema := expression.NewSchema(make([]*expression.Column, 0, p.schema.Len())...)
for _, expr := range p.Exprs {
if col, isCol := expr.(*expression.Column); isCol {
schema.Append(col)
} else {
// If the expression is not a column, we add a column to occupy the position.
schema.Append(&expression.Column{
Position: -1, RetType: expr.GetType()})
}
}
return schema
}
func (p *Projection) buildKeyInfo() {
p.baseLogicalPlan.buildKeyInfo()
p.schema.MaxOneRow = p.children[0].Schema().MaxOneRow
schema := p.buildSchemaByExprs()
for _, key := range p.Children()[0].Schema().Keys {
indices := schema.ColumnsIndices(key)
if indices == nil {
continue
}
newKey := make([]*expression.Column, 0, len(key))
for _, i := range indices {
newKey = append(newKey, p.schema.Columns[i])
}
p.schema.Keys = append(p.schema.Keys, newKey)
}
}
func (p *LogicalJoin) buildKeyInfo() {
p.baseLogicalPlan.buildKeyInfo()
p.schema.MaxOneRow = p.children[0].Schema().MaxOneRow && p.children[1].Schema().MaxOneRow
switch p.JoinType {
case SemiJoin, LeftOuterSemiJoin:
p.schema.Keys = p.children[0].Schema().Clone().Keys
case InnerJoin, LeftOuterJoin, RightOuterJoin:
// If there is no equal conditions, then cartesian product can't be prevented and unique key information will destroy.
if len(p.EqualConditions) == 0 {
return
}
lOk := false
rOk := false
// Such as 'select * from t1 join t2 where t1.a = t2.a and t1.b = t2.b'.
// If one sides (a, b) is a unique key, then the unique key information is remained.
// But we don't consider this situation currently.
// Only key made by one column is considered now.
for _, expr := range p.EqualConditions {
ln := expr.GetArgs()[0].(*expression.Column)
rn := expr.GetArgs()[1].(*expression.Column)
for _, key := range p.children[0].Schema().Keys {
if len(key) == 1 && key[0].Equal(ln, p.ctx) {
lOk = true
break
}
}
for _, key := range p.children[1].Schema().Keys {
if len(key) == 1 && key[0].Equal(rn, p.ctx) {
rOk = true
break
}
}
}
// For inner join, if one side of one equal condition is unique key,
// another side's unique key information will all be reserved.
// If it's an outer join, NULL value will fill some position, which will destroy the unique key information.
if lOk && p.JoinType != LeftOuterJoin {
p.schema.Keys = append(p.schema.Keys, p.children[1].Schema().Keys...)
}
if rOk && p.JoinType != RightOuterJoin {
p.schema.Keys = append(p.schema.Keys, p.children[0].Schema().Keys...)
}
}
}
func (p *DataSource) buildKeyInfo() {
p.baseLogicalPlan.buildKeyInfo()
indices, _ := availableIndices(p.indexHints, p.tableInfo)
for _, idx := range indices {
if !idx.Unique {
continue
}
newKey := make([]*expression.Column, 0, len(idx.Columns))
ok := true
for _, idxCol := range idx.Columns {
// The columns of this index should all occur in column schema.
// Since null value could be duplicate in unique key. So we check NotNull flag of every column.
find := false
for i, col := range p.schema.Columns {
if idxCol.Name.L == col.ColName.L {
if !mysql.HasNotNullFlag(p.Columns[i].Flag) {
break
}
newKey = append(newKey, p.schema.Columns[i])
find = true
break
}
}
if !find {
ok = false
break
}
}
if ok {
p.schema.Keys = append(p.schema.Keys, newKey)
}
}
if p.tableInfo.PKIsHandle {
for i, col := range p.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
p.schema.Keys = append(p.schema.Keys, []*expression.Column{p.schema.Columns[i]})
break
}
}
}
}