-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
ranger.go
468 lines (443 loc) · 14 KB
/
ranger.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// 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 ranger
import (
"bytes"
"math"
"sort"
"unicode/utf8"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/codec"
)
func validInterval(sc *stmtctx.StatementContext, low, high point) (bool, error) {
l, err := codec.EncodeKey(sc, nil, low.value)
if err != nil {
return false, errors.Trace(err)
}
if low.excl {
l = []byte(kv.Key(l).PrefixNext())
}
r, err := codec.EncodeKey(sc, nil, high.value)
if err != nil {
return false, errors.Trace(err)
}
if !high.excl {
r = []byte(kv.Key(r).PrefixNext())
}
return bytes.Compare(l, r) < 0, nil
}
// points2Ranges build index ranges from range points.
// Only one column is built there. If there're multiple columns, use appendPoints2Ranges.
func points2Ranges(sc *stmtctx.StatementContext, rangePoints []point, tp *types.FieldType) ([]*Range, error) {
ranges := make([]*Range, 0, len(rangePoints)/2)
for i := 0; i < len(rangePoints); i += 2 {
startPoint, err := convertPoint(sc, rangePoints[i], tp)
if err != nil {
return nil, errors.Trace(err)
}
endPoint, err := convertPoint(sc, rangePoints[i+1], tp)
if err != nil {
return nil, errors.Trace(err)
}
less, err := validInterval(sc, startPoint, endPoint)
if err != nil {
return nil, errors.Trace(err)
}
if !less {
continue
}
// If column has not null flag, [null, null] should be removed.
if mysql.HasNotNullFlag(tp.Flag) && endPoint.value.Kind() == types.KindNull {
continue
}
ran := &Range{
LowVal: []types.Datum{startPoint.value},
LowExclude: startPoint.excl,
HighVal: []types.Datum{endPoint.value},
HighExclude: endPoint.excl,
}
ranges = append(ranges, ran)
}
return ranges, nil
}
func convertPoint(sc *stmtctx.StatementContext, point point, tp *types.FieldType) (point, error) {
switch point.value.Kind() {
case types.KindMaxValue, types.KindMinNotNull:
return point, nil
}
casted, err := point.value.ConvertTo(sc, tp)
if err != nil {
return point, errors.Trace(err)
}
valCmpCasted, err := point.value.CompareDatum(sc, &casted)
if err != nil {
return point, errors.Trace(err)
}
point.value = casted
if valCmpCasted == 0 {
return point, nil
}
if point.start {
if point.excl {
if valCmpCasted < 0 {
// e.g. "a > 1.9" convert to "a >= 2".
point.excl = false
}
} else {
if valCmpCasted > 0 {
// e.g. "a >= 1.1 convert to "a > 1"
point.excl = true
}
}
} else {
if point.excl {
if valCmpCasted > 0 {
// e.g. "a < 1.1" convert to "a <= 1"
point.excl = false
}
} else {
if valCmpCasted < 0 {
// e.g. "a <= 1.9" convert to "a < 2"
point.excl = true
}
}
}
return point, nil
}
// appendPoints2Ranges appends additional column ranges for multi-column index.
// The additional column ranges can only be appended to point ranges.
// for example we have an index (a, b), if the condition is (a > 1 and b = 2)
// then we can not build a conjunctive ranges for this index.
func appendPoints2Ranges(sc *stmtctx.StatementContext, origin []*Range, rangePoints []point,
ft *types.FieldType) ([]*Range, error) {
var newIndexRanges []*Range
for i := 0; i < len(origin); i++ {
oRange := origin[i]
if !oRange.IsPoint(sc) {
newIndexRanges = append(newIndexRanges, oRange)
} else {
newRanges, err := appendPoints2IndexRange(sc, oRange, rangePoints, ft)
if err != nil {
return nil, errors.Trace(err)
}
newIndexRanges = append(newIndexRanges, newRanges...)
}
}
return newIndexRanges, nil
}
func appendPoints2IndexRange(sc *stmtctx.StatementContext, origin *Range, rangePoints []point,
ft *types.FieldType) ([]*Range, error) {
newRanges := make([]*Range, 0, len(rangePoints)/2)
for i := 0; i < len(rangePoints); i += 2 {
startPoint, err := convertPoint(sc, rangePoints[i], ft)
if err != nil {
return nil, errors.Trace(err)
}
endPoint, err := convertPoint(sc, rangePoints[i+1], ft)
if err != nil {
return nil, errors.Trace(err)
}
less, err := validInterval(sc, startPoint, endPoint)
if err != nil {
return nil, errors.Trace(err)
}
if !less {
continue
}
lowVal := make([]types.Datum, len(origin.LowVal)+1)
copy(lowVal, origin.LowVal)
lowVal[len(origin.LowVal)] = startPoint.value
highVal := make([]types.Datum, len(origin.HighVal)+1)
copy(highVal, origin.HighVal)
highVal[len(origin.HighVal)] = endPoint.value
ir := &Range{
LowVal: lowVal,
LowExclude: startPoint.excl,
HighVal: highVal,
HighExclude: endPoint.excl,
}
newRanges = append(newRanges, ir)
}
return newRanges, nil
}
// points2TableRanges build ranges for table scan from range points.
// It will remove the nil and convert MinNotNull and MaxValue to MinInt64 or MinUint64 and MaxInt64 or MaxUint64.
func points2TableRanges(sc *stmtctx.StatementContext, rangePoints []point, tp *types.FieldType) ([]*Range, error) {
ranges := make([]*Range, 0, len(rangePoints)/2)
var minValueDatum, maxValueDatum types.Datum
// Currently, table's kv range cannot accept encoded value of MaxValueDatum. we need to convert it.
if mysql.HasUnsignedFlag(tp.Flag) {
minValueDatum.SetUint64(0)
maxValueDatum.SetUint64(math.MaxUint64)
} else {
minValueDatum.SetInt64(math.MinInt64)
maxValueDatum.SetInt64(math.MaxInt64)
}
for i := 0; i < len(rangePoints); i += 2 {
startPoint, err := convertPoint(sc, rangePoints[i], tp)
if err != nil {
return nil, errors.Trace(err)
}
if startPoint.value.Kind() == types.KindNull {
startPoint.value = minValueDatum
startPoint.excl = false
} else if startPoint.value.Kind() == types.KindMinNotNull {
startPoint.value = minValueDatum
}
endPoint, err := convertPoint(sc, rangePoints[i+1], tp)
if err != nil {
return nil, errors.Trace(err)
}
if endPoint.value.Kind() == types.KindMaxValue {
endPoint.value = maxValueDatum
} else if endPoint.value.Kind() == types.KindNull {
continue
}
less, err := validInterval(sc, startPoint, endPoint)
if err != nil {
return nil, errors.Trace(err)
}
if !less {
continue
}
ran := &Range{
LowVal: []types.Datum{startPoint.value},
LowExclude: startPoint.excl,
HighVal: []types.Datum{endPoint.value},
HighExclude: endPoint.excl,
}
ranges = append(ranges, ran)
}
return ranges, nil
}
// BuildTableRange will build range of pk for PhysicalTableScan
func BuildTableRange(accessConditions []expression.Expression, sc *stmtctx.StatementContext, tp *types.FieldType) ([]*Range, error) {
rb := builder{sc: sc}
rangePoints := fullRange
for _, cond := range accessConditions {
rangePoints = rb.intersection(rangePoints, rb.build(cond))
if rb.err != nil {
return nil, errors.Trace(rb.err)
}
}
newTp := newFieldType(tp)
ranges, err := points2TableRanges(sc, rangePoints, newTp)
if err != nil {
return nil, errors.Trace(err)
}
return ranges, nil
}
// BuildColumnRange builds the range for sampling histogram to calculate the row count.
func BuildColumnRange(conds []expression.Expression, sc *stmtctx.StatementContext, tp *types.FieldType) ([]*Range, error) {
if len(conds) == 0 {
return []*Range{{LowVal: []types.Datum{{}}, HighVal: []types.Datum{types.MaxValueDatum()}}}, nil
}
rb := builder{sc: sc}
rangePoints := fullRange
for _, cond := range conds {
rangePoints = rb.intersection(rangePoints, rb.build(cond))
if rb.err != nil {
return nil, errors.Trace(rb.err)
}
}
newTp := newFieldType(tp)
ranges, err := points2Ranges(sc, rangePoints, newTp)
if err != nil {
return nil, errors.Trace(err)
}
return ranges, nil
}
// buildCNFIndexRange builds the range for index where the top layer is CNF.
func buildCNFIndexRange(sc *stmtctx.StatementContext, cols []*expression.Column, newTp []*types.FieldType, lengths []int,
eqAndInCount int, accessCondition []expression.Expression) ([]*Range, error) {
rb := builder{sc: sc}
var (
ranges []*Range
err error
)
for _, col := range cols {
newTp = append(newTp, newFieldType(col.RetType))
}
for i := 0; i < eqAndInCount; i++ {
if sf, ok := accessCondition[i].(*expression.ScalarFunction); !ok || (sf.FuncName.L != ast.EQ && sf.FuncName.L != ast.In) {
break
}
// Build ranges for equal or in access conditions.
point := rb.build(accessCondition[i])
if rb.err != nil {
return nil, errors.Trace(rb.err)
}
if i == 0 {
ranges, err = points2Ranges(sc, point, newTp[i])
} else {
ranges, err = appendPoints2Ranges(sc, ranges, point, newTp[i])
}
if err != nil {
return nil, errors.Trace(err)
}
}
rangePoints := fullRange
// Build rangePoints for non-equal access conditions.
for i := eqAndInCount; i < len(accessCondition); i++ {
rangePoints = rb.intersection(rangePoints, rb.build(accessCondition[i]))
if rb.err != nil {
return nil, errors.Trace(rb.err)
}
}
if eqAndInCount == 0 {
ranges, err = points2Ranges(sc, rangePoints, newTp[0])
} else if eqAndInCount < len(accessCondition) {
ranges, err = appendPoints2Ranges(sc, ranges, rangePoints, newTp[eqAndInCount])
}
if err != nil {
return nil, errors.Trace(err)
}
// Take prefix index into consideration.
if hasPrefix(lengths) {
fixPrefixColRange(ranges, lengths, newTp)
}
if len(ranges) > 0 && len(ranges[0].LowVal) < len(cols) {
for _, ran := range ranges {
if ran.HighExclude || ran.LowExclude {
if ran.HighExclude {
ran.HighVal = append(ran.HighVal, types.NewDatum(nil))
} else {
ran.HighVal = append(ran.HighVal, types.MaxValueDatum())
}
if ran.LowExclude {
ran.LowVal = append(ran.LowVal, types.MaxValueDatum())
} else {
ran.LowVal = append(ran.LowVal, types.NewDatum(nil))
}
}
}
}
return ranges, nil
}
type sortRange struct {
originalValue *Range
encodedStart []byte
encodedEnd []byte
}
func unionRanges(sc *stmtctx.StatementContext, ranges []*Range) ([]*Range, error) {
if len(ranges) == 0 {
return nil, nil
}
objects := make([]*sortRange, 0, len(ranges))
for _, ran := range ranges {
left, err := codec.EncodeKey(sc, nil, ran.LowVal...)
if err != nil {
return nil, errors.Trace(err)
}
if ran.LowExclude {
left = kv.Key(left).PrefixNext()
}
right, err := codec.EncodeKey(sc, nil, ran.HighVal...)
if err != nil {
return nil, errors.Trace(err)
}
if !ran.HighExclude {
right = kv.Key(right).PrefixNext()
}
objects = append(objects, &sortRange{originalValue: ran, encodedStart: left, encodedEnd: right})
}
sort.Slice(objects, func(i, j int) bool {
return bytes.Compare(objects[i].encodedStart, objects[j].encodedStart) < 0
})
ranges = ranges[:0]
lastRange := objects[0]
for i := 1; i < len(objects); i++ {
// For two intervals [a, b], [c, d], we have guaranteed that a >= c. If b >= c. Then two intervals are overlapped.
// And this two can be merged as [a, max(b, d)].
// Otherwise they aren't overlapped.
if bytes.Compare(lastRange.encodedEnd, objects[i].encodedStart) >= 0 {
if bytes.Compare(lastRange.encodedEnd, objects[i].encodedEnd) < 0 {
lastRange.encodedEnd = objects[i].encodedEnd
lastRange.originalValue.HighVal = objects[i].originalValue.HighVal
lastRange.originalValue.HighExclude = objects[i].originalValue.HighExclude
}
} else {
ranges = append(ranges, lastRange.originalValue)
lastRange = objects[i]
}
}
ranges = append(ranges, lastRange.originalValue)
return ranges, nil
}
func hasPrefix(lengths []int) bool {
for _, l := range lengths {
if l != types.UnspecifiedLength {
return true
}
}
return false
}
func fixPrefixColRange(ranges []*Range, lengths []int, tp []*types.FieldType) {
for _, ran := range ranges {
for i := 0; i < len(ran.LowVal); i++ {
fixRangeDatum(&ran.LowVal[i], lengths[i], tp[i])
}
ran.LowExclude = false
for i := 0; i < len(ran.HighVal); i++ {
fixRangeDatum(&ran.HighVal[i], lengths[i], tp[i])
}
ran.HighExclude = false
}
}
func fixRangeDatum(v *types.Datum, length int, tp *types.FieldType) {
// If this column is prefix and the prefix length is smaller than the range, cut it.
// In case of UTF8, prefix should be cut by characters rather than bytes
if v.Kind() == types.KindString || v.Kind() == types.KindBytes {
colCharset := tp.Charset
colValue := v.GetBytes()
isUTF8Charset := colCharset == charset.CharsetUTF8 || colCharset == charset.CharsetUTF8MB4
if isUTF8Charset {
if length != types.UnspecifiedLength && utf8.RuneCount(colValue) > length {
rs := bytes.Runes(colValue)
truncateStr := string(rs[:length])
// truncate value and limit its length
v.SetString(truncateStr)
}
} else if length != types.UnspecifiedLength && len(colValue) > length {
// truncate value and limit its length
v.SetBytes(colValue[:length])
}
}
}
// We cannot use the FieldType of column directly. e.g. the column a is int32 and we have a > 1111111111111111111.
// Obviously the constant is bigger than MaxInt32, so we will get overflow error if we use the FieldType of column a.
func newFieldType(tp *types.FieldType) *types.FieldType {
switch tp.Tp {
// To avoid overflow error.
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
newTp := types.NewFieldType(mysql.TypeLonglong)
newTp.Flag = tp.Flag
newTp.Charset = tp.Charset
return newTp
// To avoid data truncate error.
case mysql.TypeFloat, mysql.TypeDouble, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob,
mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString:
newTp := types.NewFieldType(tp.Tp)
newTp.Charset = tp.Charset
return newTp
default:
return tp
}
}