forked from pingcap/tidb
/
index.go
353 lines (324 loc) · 7.92 KB
/
index.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2015 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 plans
import (
"github.com/pingcap/tidb/column"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/expression/expressions"
"github.com/pingcap/tidb/field"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/util/format"
"github.com/pingcap/tidb/util/types"
)
var _ plan.Plan = (*indexPlan)(nil)
type bound int
const (
minNotNullVal bound = 0
maxVal bound = 1
)
// String implements fmt.Stringer interface.
func (b bound) String() string {
if b == minNotNullVal {
return "-inf"
} else if b == maxVal {
return "+inf"
}
return ""
}
// index span is the range of value to be scanned.
type indexSpan struct {
lowVal interface{}
lowExclude bool
highVal interface{}
highExclude bool
}
// cut off the range less than val and return the new span.
// the new span may be nil if val is larger than span's high value.
func (span *indexSpan) cutOffLow(val interface{}, exclude bool) *indexSpan {
out := &indexSpan{}
*out = *span
cmp := indexCompare(span.lowVal, val)
if cmp == 0 {
if !out.lowExclude {
out.lowExclude = exclude
}
return out
} else if cmp < 0 {
out.lowVal = val
out.lowExclude = exclude
cmp2 := indexCompare(out.lowVal, out.highVal)
if cmp2 < 0 {
return out
} else if cmp2 > 0 {
return nil
} else {
if out.lowExclude || out.highExclude {
return nil
}
return out
}
} else {
return out
}
}
// cut out the range greater than val and return the new span.
// the new span may be nil if val is less than span's low value.
func (span *indexSpan) cutOffHigh(val interface{}, exclude bool) *indexSpan {
out := &indexSpan{}
*out = *span
cmp := indexCompare(span.highVal, val)
if cmp == 0 {
if !out.highExclude {
out.highExclude = exclude
}
return out
} else if cmp > 0 {
out.highVal = val
out.highExclude = exclude
cmp2 := indexCompare(out.lowVal, out.highVal)
if cmp2 < 0 {
return out
} else if cmp2 > 0 {
return nil
} else {
if out.lowExclude || out.highExclude {
return nil
}
return out
}
} else {
return out
}
}
type indexPlan struct {
src table.Table
colName string
idxName string
idx kv.Index
spans []*indexSpan // multiple spans are ordered by their values and without overlapping.
}
// comparison function that takes minNotNullVal and maxVal into account.
func indexCompare(a interface{}, b interface{}) int {
if a == nil && b == nil {
return 0
} else if b == nil {
return 1
} else if b == nil {
return -1
}
// a and b both not nil
if a == minNotNullVal && b == minNotNullVal {
return 0
} else if b == minNotNullVal {
return 1
} else if a == minNotNullVal {
return -1
}
// a and b both not min value
if a == maxVal && b == maxVal {
return 0
} else if a == maxVal {
return 1
} else if b == maxVal {
return -1
}
return types.Compare(a, b)
}
func (r *indexPlan) doSpan(ctx context.Context, txn kv.Transaction, span *indexSpan, f plan.RowIterFunc) error {
seekVal := span.lowVal
if span.lowVal == minNotNullVal {
seekVal = []byte{}
}
it, _, err := r.idx.Seek(txn, []interface{}{seekVal})
if err != nil {
return types.EOFAsNil(err)
}
defer it.Close()
var skipLowCompare bool
for {
k, h, err := it.Next()
if err != nil {
return types.EOFAsNil(err)
}
val := k[0]
if !skipLowCompare {
if span.lowExclude && indexCompare(span.lowVal, val) == 0 {
continue
}
skipLowCompare = true
}
cmp := indexCompare(span.highVal, val)
if cmp < 0 || (cmp == 0 && span.highExclude) {
return nil
}
data, err := r.src.Row(ctx, h)
if err != nil {
return err
}
if more, err := f(h, data); err != nil || !more {
return err
}
}
}
// Do implements plan.Plan Do interface.
// It scans a span from the lower bound to upper bound.
func (r *indexPlan) Do(ctx context.Context, f plan.RowIterFunc) error {
txn, err := ctx.GetTxn(false)
if err != nil {
return err
}
for _, span := range r.spans {
err := r.doSpan(ctx, txn, span, f)
if err != nil {
return err
}
}
return nil
}
// Explain implements plan.Plan Explain interface.
func (r *indexPlan) Explain(w format.Formatter) {
w.Format("┌Iterate rows of table %q using index %q where %s in ", r.src.TableName(), r.idxName, r.colName)
for _, span := range r.spans {
open := "["
close := "]"
if span.lowExclude {
open = "("
}
if span.highExclude {
close = ")"
}
w.Format("%s%v,%v%s ", open, span.lowVal, span.highVal, close)
}
w.Format("\n└Output field names %v\n", field.RFQNames(r.GetFields()))
}
// GetFields implements plan.Plan GetFields interface.
func (r *indexPlan) GetFields() []*field.ResultField {
return field.ColsToResultFields(r.src.Cols(), r.src.TableName().O)
}
// Filter implements plan.Plan Filter interface.
// Filter merges BinaryOperations, and determines the lower and upper bound.
func (r *indexPlan) Filter(ctx context.Context, expr expression.Expression) (plan.Plan, bool, error) {
switch x := expr.(type) {
case *expressions.BinaryOperation:
ok, cname, val, err := x.IsIdentRelOpVal()
if err != nil {
return nil, false, err
}
if !ok || r.colName != cname {
break
}
col := column.FindCol(r.src.Cols(), cname)
if col == nil {
break
}
if val, err = col.CastValue(ctx, val); err != nil {
return nil, false, err
}
r.spans = filterSpans(r.spans, toSpans(x.Op, val))
return r, true, nil
case *expressions.Ident:
if r.colName != x.L {
break
}
r.spans = filterSpans(r.spans, toSpans(opcode.GE, minNotNullVal))
return r, true, nil
case *expressions.UnaryOperation:
if x.Op != '!' {
break
}
operand, ok := x.V.(*expressions.Ident)
if !ok {
break
}
cname := operand.L
if r.colName != cname {
break
}
r.spans = filterSpans(r.spans, toSpans(opcode.EQ, nil))
return r, true, nil
}
return r, false, nil
}
// return the intersection range between origin and filter.
func filterSpans(origin []*indexSpan, filter []*indexSpan) []*indexSpan {
var newSpans []*indexSpan
for _, fSpan := range filter {
for _, oSpan := range origin {
newSpan := oSpan.cutOffLow(fSpan.lowVal, fSpan.lowExclude)
if newSpan == nil {
continue
}
newSpan = newSpan.cutOffHigh(fSpan.highVal, fSpan.highExclude)
if newSpan == nil {
continue
}
newSpans = append(newSpans, newSpan)
}
}
return newSpans
}
// generate a slice of span from operator and value.
func toSpans(op opcode.Op, val interface{}) []*indexSpan {
var spans []*indexSpan
switch op {
case opcode.EQ:
spans = append(spans, &indexSpan{
lowVal: val,
lowExclude: false,
highVal: val,
highExclude: false,
})
case opcode.LT:
spans = append(spans, &indexSpan{
lowVal: minNotNullVal,
highVal: val,
highExclude: true,
})
case opcode.LE:
spans = append(spans, &indexSpan{
lowVal: minNotNullVal,
highVal: val,
highExclude: false,
})
case opcode.GE:
spans = append(spans, &indexSpan{
lowVal: val,
lowExclude: false,
highVal: maxVal,
})
case opcode.GT:
spans = append(spans, &indexSpan{
lowVal: val,
lowExclude: true,
highVal: maxVal,
})
case opcode.NE:
spans = append(spans, &indexSpan{
lowVal: minNotNullVal,
highVal: val,
highExclude: true,
})
spans = append(spans, &indexSpan{
lowVal: val,
lowExclude: true,
highVal: maxVal,
})
default:
panic("should never happen")
}
return spans
}