/
rule_build_key_info.go
339 lines (316 loc) · 11.3 KB
/
rule_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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// 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,
// 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 core
import (
"context"
"github.com/wuhuizuo/tidb6/expression"
"github.com/wuhuizuo/tidb6/parser/ast"
"github.com/wuhuizuo/tidb6/parser/model"
"github.com/wuhuizuo/tidb6/parser/mysql"
)
type buildKeySolver struct{}
func (*buildKeySolver) optimize(_ context.Context, p LogicalPlan, _ *logicalOptimizeOp) (LogicalPlan, error) {
buildKeyInfo(p)
return p, nil
}
// buildKeyInfo recursively calls LogicalPlan's BuildKeyInfo method.
func buildKeyInfo(lp LogicalPlan) {
for _, child := range lp.Children() {
buildKeyInfo(child)
}
childSchema := make([]*expression.Schema, len(lp.Children()))
for i, child := range lp.Children() {
childSchema[i] = child.Schema()
}
lp.BuildKeyInfo(lp.Schema(), childSchema)
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (la *LogicalAggregation) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
if la.IsPartialModeAgg() {
return
}
la.logicalSchemaProducer.BuildKeyInfo(selfSchema, childSchema)
la.buildSelfKeyInfo(selfSchema)
}
func (la *LogicalAggregation) buildSelfKeyInfo(selfSchema *expression.Schema) {
groupByCols := la.GetGroupByCols()
if len(groupByCols) == len(la.GroupByItems) && len(la.GroupByItems) > 0 {
indices := selfSchema.ColumnsIndices(groupByCols)
if indices != nil {
newKey := make([]*expression.Column, 0, len(indices))
for _, i := range indices {
newKey = append(newKey, selfSchema.Columns[i])
}
selfSchema.Keys = append(selfSchema.Keys, newKey)
}
}
if len(la.GroupByItems) == 0 {
la.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 (*LogicalSelection) checkMaxOneRowCond(eqColIDs map[int64]struct{}, childSchema *expression.Schema) bool {
if len(eqColIDs) == 0 {
return false
}
// We check `UniqueKeys` as well since the condition is `col = con | corr`, not `col <=> con | corr`.
keys := make([]expression.KeyInfo, 0, len(childSchema.Keys)+len(childSchema.UniqueKeys))
keys = append(keys, childSchema.Keys...)
keys = append(keys, childSchema.UniqueKeys...)
var maxOneRow bool
for _, cols := range keys {
maxOneRow = true
for _, c := range cols {
if _, ok := eqColIDs[c.UniqueID]; !ok {
maxOneRow = false
break
}
}
if maxOneRow {
return true
}
}
return false
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (p *LogicalSelection) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
p.baseLogicalPlan.BuildKeyInfo(selfSchema, childSchema)
if p.maxOneRow {
return
}
eqCols := make(map[int64]struct{}, len(childSchema[0].Columns))
for _, cond := range p.Conditions {
if sf, ok := cond.(*expression.ScalarFunction); ok && sf.FuncName.L == ast.EQ {
for i, arg := range sf.GetArgs() {
if col, isCol := arg.(*expression.Column); isCol {
_, isCon := sf.GetArgs()[1-i].(*expression.Constant)
_, isCorCol := sf.GetArgs()[1-i].(*expression.CorrelatedColumn)
if isCon || isCorCol {
eqCols[col.UniqueID] = struct{}{}
}
break
}
}
}
}
p.maxOneRow = p.checkMaxOneRowCond(eqCols, childSchema[0])
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (p *LogicalLimit) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
p.logicalSchemaProducer.BuildKeyInfo(selfSchema, childSchema)
if p.Count == 1 {
p.maxOneRow = true
}
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (lt *LogicalTopN) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
lt.baseLogicalPlan.BuildKeyInfo(selfSchema, childSchema)
if lt.Count == 1 {
lt.maxOneRow = true
}
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (p *LogicalTableDual) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
p.baseLogicalPlan.BuildKeyInfo(selfSchema, childSchema)
if p.RowCount == 1 {
p.maxOneRow = true
}
}
// 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 *LogicalProjection) buildSchemaByExprs(selfSchema *expression.Schema) *expression.Schema {
schema := expression.NewSchema(make([]*expression.Column, 0, selfSchema.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{
UniqueID: p.ctx.GetSessionVars().AllocPlanColumnID(),
RetType: expr.GetType(),
})
}
}
return schema
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (p *LogicalProjection) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
// `LogicalProjection` use schema from `Exprs` to build key info. See `buildSchemaByExprs`.
// So call `baseLogicalPlan.BuildKeyInfo` here to avoid duplicated building key info.
p.baseLogicalPlan.BuildKeyInfo(selfSchema, childSchema)
selfSchema.Keys = nil
schema := p.buildSchemaByExprs(selfSchema)
for _, key := range childSchema[0].Keys {
indices := schema.ColumnsIndices(key)
if indices == nil {
continue
}
newKey := make([]*expression.Column, 0, len(key))
for _, i := range indices {
newKey = append(newKey, selfSchema.Columns[i])
}
selfSchema.Keys = append(selfSchema.Keys, newKey)
}
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (p *LogicalJoin) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
p.logicalSchemaProducer.BuildKeyInfo(selfSchema, childSchema)
switch p.JoinType {
case SemiJoin, LeftOuterSemiJoin, AntiSemiJoin, AntiLeftOuterSemiJoin:
selfSchema.Keys = childSchema[0].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 childSchema[0].Keys {
if len(key) == 1 && key[0].Equal(p.ctx, ln) {
lOk = true
break
}
}
for _, key := range childSchema[1].Keys {
if len(key) == 1 && key[0].Equal(p.ctx, rn) {
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 {
selfSchema.Keys = append(selfSchema.Keys, childSchema[1].Keys...)
}
if rOk && p.JoinType != RightOuterJoin {
selfSchema.Keys = append(selfSchema.Keys, childSchema[0].Keys...)
}
}
}
// checkIndexCanBeKey checks whether an Index can be a Key in schema.
func checkIndexCanBeKey(idx *model.IndexInfo, columns []*model.ColumnInfo, schema *expression.Schema) (uniqueKey, newKey expression.KeyInfo) {
if !idx.Unique {
return nil, nil
}
newKeyOK := true
uniqueKeyOK := 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.
findUniqueKey := false
for i, col := range columns {
if idxCol.Name.L == col.Name.L {
uniqueKey = append(uniqueKey, schema.Columns[i])
findUniqueKey = true
if newKeyOK {
if !mysql.HasNotNullFlag(col.GetFlag()) {
newKeyOK = false
break
}
newKey = append(newKey, schema.Columns[i])
break
}
}
}
if !findUniqueKey {
newKeyOK = false
uniqueKeyOK = false
break
}
}
if newKeyOK {
return nil, newKey
} else if uniqueKeyOK {
return uniqueKey, nil
}
return nil, nil
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (ds *DataSource) BuildKeyInfo(selfSchema *expression.Schema, _ []*expression.Schema) {
selfSchema.Keys = nil
var latestIndexes map[int64]*model.IndexInfo
var changed bool
var err error
check := ds.ctx.GetSessionVars().IsIsolation(ast.ReadCommitted) || ds.isForUpdateRead
check = check && ds.ctx.GetSessionVars().ConnectionID > 0
// we should check index valid while forUpdateRead, see detail in https://github.com/wuhuizuo/tidb6/pull/22152
if check {
latestIndexes, changed, err = getLatestIndexInfo(ds.ctx, ds.table.Meta().ID, 0)
if err != nil {
return
}
}
for _, index := range ds.table.Meta().Indices {
if ds.isForUpdateRead && changed {
latestIndex, ok := latestIndexes[index.ID]
if !ok || latestIndex.State != model.StatePublic {
continue
}
} else if index.State != model.StatePublic {
continue
}
if uniqueKey, newKey := checkIndexCanBeKey(index, ds.Columns, selfSchema); newKey != nil {
selfSchema.Keys = append(selfSchema.Keys, newKey)
} else if uniqueKey != nil {
selfSchema.UniqueKeys = append(selfSchema.UniqueKeys, uniqueKey)
}
}
if ds.tableInfo.PKIsHandle {
for i, col := range ds.Columns {
if mysql.HasPriKeyFlag(col.GetFlag()) {
selfSchema.Keys = append(selfSchema.Keys, []*expression.Column{selfSchema.Columns[i]})
break
}
}
}
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (ts *LogicalTableScan) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
ts.Source.BuildKeyInfo(selfSchema, childSchema)
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (is *LogicalIndexScan) BuildKeyInfo(selfSchema *expression.Schema, _ []*expression.Schema) {
selfSchema.Keys = nil
for _, path := range is.Source.possibleAccessPaths {
if path.IsTablePath() {
continue
}
if uniqueKey, newKey := checkIndexCanBeKey(path.Index, is.Columns, selfSchema); newKey != nil {
selfSchema.Keys = append(selfSchema.Keys, newKey)
} else if uniqueKey != nil {
selfSchema.UniqueKeys = append(selfSchema.UniqueKeys, uniqueKey)
}
}
handle := is.getPKIsHandleCol(selfSchema)
if handle != nil {
selfSchema.Keys = append(selfSchema.Keys, []*expression.Column{handle})
}
}
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
func (*TiKVSingleGather) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
selfSchema.Keys = childSchema[0].Keys
}
func (*buildKeySolver) name() string {
return "build_keys"
}