forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.go
458 lines (402 loc) · 12.3 KB
/
chunk.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
// Copyright 2021 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 chunk
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb-tools/pkg/dbutil"
"go.uber.org/zap"
)
const (
lt = "<"
lte = "<="
gt = ">"
)
type ChunkType int
const (
Bucket ChunkType = iota + 1
Random
Limit
Others
Empty
)
// Bound represents a bound for a column
type Bound struct {
Column string `json:"column"`
Lower string `json:"lower"`
Upper string `json:"upper"`
HasLower bool `json:"has-lower"`
HasUpper bool `json:"has-upper"`
}
// ChunkID is to identify the sequence of chunks
type ChunkID struct {
TableIndex int `json:"table-index"`
// we especially treat random split has only one bucket
// which is the whole table
// range is [left, right]
BucketIndexLeft int `json:"bucket-index-left"`
BucketIndexRight int `json:"bucket-index-right"`
ChunkIndex int `json:"chunk-index"`
// `ChunkCnt` is the number of chunks in this bucket
// We can compare `ChunkIndex` and `ChunkCnt` to know
// whether this chunk is the last one
ChunkCnt int `json:"chunk-count"`
}
func GetInitChunkID() *ChunkID {
return &ChunkID{
TableIndex: -1,
BucketIndexLeft: -1,
BucketIndexRight: -1,
ChunkIndex: -1,
ChunkCnt: 0,
}
}
func (c *ChunkID) Compare(o *ChunkID) int {
if c.TableIndex < o.TableIndex {
return -1
}
if c.TableIndex > o.TableIndex {
return 1
}
// c.TableIndex == o.TableIndex
if c.BucketIndexLeft < o.BucketIndexLeft {
return -1
}
if c.BucketIndexLeft > o.BucketIndexLeft {
return 1
}
// c.BucketIndexLeft == o.BucketIndexLeft
if c.ChunkIndex < o.ChunkIndex {
return -1
}
if c.ChunkIndex == o.ChunkIndex {
return 0
}
return 1
}
func (c *ChunkID) Copy() *ChunkID {
cp := *c
return &cp
}
func (c *ChunkID) ToString() string {
return fmt.Sprintf("%d:%d-%d:%d:%d", c.TableIndex, c.BucketIndexLeft, c.BucketIndexRight, c.ChunkIndex, c.ChunkCnt)
}
func (c *ChunkID) FromString(s string) error {
ids := strings.Split(s, ":")
tableIndex, err := strconv.Atoi(ids[0])
if err != nil {
return errors.Trace(err)
}
bucketIndex := strings.Split(ids[1], "-")
bucketIndexLeft, err := strconv.Atoi(bucketIndex[0])
if err != nil {
return errors.Trace(err)
}
bucketIndexRight, err := strconv.Atoi(bucketIndex[1])
if err != nil {
return errors.Trace(err)
}
chunkIndex, err := strconv.Atoi(ids[2])
if err != nil {
return errors.Trace(err)
}
chunkCnt, err := strconv.Atoi(ids[3])
if err != nil {
return errors.Trace(err)
}
c.TableIndex, c.BucketIndexLeft, c.BucketIndexRight, c.ChunkIndex, c.ChunkCnt = tableIndex, bucketIndexLeft, bucketIndexRight, chunkIndex, chunkCnt
return nil
}
// Range represents chunk range
type Range struct {
Index *ChunkID `json:"index"`
Type ChunkType `json:"type"`
Bounds []*Bound `json:"bounds"`
IsFirst bool `json:"is-first"`
IsLast bool `json:"is-last"`
Where string `json:"where"`
Args []interface{} `json:"args"`
columnOffset map[string]int
}
func (r *Range) IsFirstChunkForBucket() bool {
return r.Index.ChunkIndex == 0
}
func (r *Range) IsLastChunkForBucket() bool {
return r.Index.ChunkIndex == r.Index.ChunkCnt-1
}
// NewChunkRange return a Range.
func NewChunkRange() *Range {
return &Range{
Bounds: make([]*Bound, 0, 2),
columnOffset: make(map[string]int),
Index: &ChunkID{},
}
}
// NewChunkRangeOffset return a Range in sequence
func NewChunkRangeOffset(columnOffset map[string]int) *Range {
bounds := make([]*Bound, len(columnOffset))
for column, offset := range columnOffset {
bounds[offset] = &Bound{
Column: column,
HasLower: false,
HasUpper: false,
}
}
return &Range{
Bounds: bounds,
columnOffset: columnOffset,
}
}
func (c *Range) IsLastChunkForTable() bool {
if c.IsLast {
return true
}
// calculate from bounds
for _, b := range c.Bounds {
if b.HasUpper {
return false
}
}
return true
}
func (c *Range) IsFirstChunkForTable() bool {
if c.IsFirst {
return true
}
// calculate from bounds
for _, b := range c.Bounds {
if b.HasLower {
return false
}
}
return true
}
// String returns the string of Range, used for log.
func (c *Range) String() string {
chunkBytes, err := json.Marshal(c)
if err != nil {
log.Warn("fail to encode chunk into string", zap.Error(err))
return ""
}
return string(chunkBytes)
}
func (c *Range) ToString(collation string) (string, []interface{}) {
if collation != "" {
collation = fmt.Sprintf(" COLLATE '%s'", collation)
}
/* for example:
there is a bucket in TiDB, and the lowerbound and upperbound are (A, B1, C1), (A, B2, C2), and the columns are `a`, `b` and `c`,
this bucket's data range is (a = A) AND (b > B1 or (b == B1 and c > C1)) AND (b < B2 or (b == B2 and c <= C2))
*/
sameCondition := make([]string, 0, 1)
lowerCondition := make([]string, 0, 1)
upperCondition := make([]string, 0, 1)
sameArgs := make([]interface{}, 0, 1)
lowerArgs := make([]interface{}, 0, 1)
upperArgs := make([]interface{}, 0, 1)
preConditionForLower := make([]string, 0, 1)
preConditionForUpper := make([]string, 0, 1)
preConditionArgsForLower := make([]interface{}, 0, 1)
preConditionArgsForUpper := make([]interface{}, 0, 1)
i := 0
for ; i < len(c.Bounds); i++ {
bound := c.Bounds[i]
if !(bound.HasLower && bound.HasUpper) {
break
}
if bound.Lower != bound.Upper {
break
}
sameCondition = append(sameCondition, fmt.Sprintf("%s%s = ?", dbutil.ColumnName(bound.Column), collation))
sameArgs = append(sameArgs, bound.Lower)
}
for ; i < len(c.Bounds); i++ {
bound := c.Bounds[i]
lowerSymbol := gt
upperSymbol := lt
if i == len(c.Bounds)-1 {
upperSymbol = lte
}
if bound.HasLower {
if len(preConditionForLower) > 0 {
lowerCondition = append(lowerCondition, fmt.Sprintf("(%s AND %s%s %s ?)", strings.Join(preConditionForLower, " AND "), dbutil.ColumnName(bound.Column), collation, lowerSymbol))
lowerArgs = append(append(lowerArgs, preConditionArgsForLower...), bound.Lower)
} else {
lowerCondition = append(lowerCondition, fmt.Sprintf("(%s%s %s ?)", dbutil.ColumnName(bound.Column), collation, lowerSymbol))
lowerArgs = append(lowerArgs, bound.Lower)
}
preConditionForLower = append(preConditionForLower, fmt.Sprintf("%s%s = ?", dbutil.ColumnName(bound.Column), collation))
preConditionArgsForLower = append(preConditionArgsForLower, bound.Lower)
}
if bound.HasUpper {
if len(preConditionForUpper) > 0 {
upperCondition = append(upperCondition, fmt.Sprintf("(%s AND %s%s %s ?)", strings.Join(preConditionForUpper, " AND "), dbutil.ColumnName(bound.Column), collation, upperSymbol))
upperArgs = append(append(upperArgs, preConditionArgsForUpper...), bound.Upper)
} else {
upperCondition = append(upperCondition, fmt.Sprintf("(%s%s %s ?)", dbutil.ColumnName(bound.Column), collation, upperSymbol))
upperArgs = append(upperArgs, bound.Upper)
}
preConditionForUpper = append(preConditionForUpper, fmt.Sprintf("%s%s = ?", dbutil.ColumnName(bound.Column), collation))
preConditionArgsForUpper = append(preConditionArgsForUpper, bound.Upper)
}
}
if len(sameCondition) == 0 {
if len(upperCondition) == 0 && len(lowerCondition) == 0 {
return "TRUE", nil
}
if len(upperCondition) == 0 {
return strings.Join(lowerCondition, " OR "), lowerArgs
}
if len(lowerCondition) == 0 {
return strings.Join(upperCondition, " OR "), upperArgs
}
return fmt.Sprintf("(%s) AND (%s)", strings.Join(lowerCondition, " OR "), strings.Join(upperCondition, " OR ")), append(lowerArgs, upperArgs...)
} else {
if len(upperCondition) == 0 && len(lowerCondition) == 0 {
return strings.Join(sameCondition, " AND "), sameArgs
}
if len(upperCondition) == 0 {
return fmt.Sprintf("(%s) AND (%s)", strings.Join(sameCondition, " AND "), strings.Join(lowerCondition, " OR ")), append(sameArgs, lowerArgs...)
}
if len(lowerCondition) == 0 {
return fmt.Sprintf("(%s) AND (%s)", strings.Join(sameCondition, " AND "), strings.Join(upperCondition, " OR ")), append(sameArgs, upperArgs...)
}
return fmt.Sprintf("(%s) AND (%s) AND (%s)", strings.Join(sameCondition, " AND "), strings.Join(lowerCondition, " OR "), strings.Join(upperCondition, " OR ")), append(append(sameArgs, lowerArgs...), upperArgs...)
}
}
func (c *Range) ToMeta() string {
lowerCondition := make([]string, 0, 1)
upperCondition := make([]string, 0, 1)
columnName := make([]string, 0, 1)
for _, bound := range c.Bounds {
columnName = append(columnName, bound.Column)
if bound.HasLower {
lowerCondition = append(lowerCondition, bound.Lower)
}
if bound.HasUpper {
upperCondition = append(upperCondition, bound.Upper)
}
}
if len(upperCondition) == 0 && len(lowerCondition) == 0 {
return "range in sequence: Full"
}
if len(upperCondition) == 0 {
return fmt.Sprintf("range in sequence: (%s) < (%s)", strings.Join(lowerCondition, ","), strings.Join(columnName, ","))
}
if len(lowerCondition) == 0 {
return fmt.Sprintf("range in sequence: (%s) <= (%s)", strings.Join(columnName, ","), strings.Join(upperCondition, ","))
}
return fmt.Sprintf("range in sequence: (%s) < (%s) <= (%s)", strings.Join(lowerCondition, ","), strings.Join(columnName, ","), strings.Join(upperCondition, ","))
}
func (c *Range) addBound(bound *Bound) {
c.Bounds = append(c.Bounds, bound)
c.columnOffset[bound.Column] = len(c.Bounds) - 1
}
func (c *Range) Update(column, lower, upper string, updateLower, updateUpper bool) {
if offset, ok := c.columnOffset[column]; ok {
// update the bound
if updateLower {
c.Bounds[offset].Lower = lower
c.Bounds[offset].HasLower = true
}
if updateUpper {
c.Bounds[offset].Upper = upper
c.Bounds[offset].HasUpper = true
}
return
}
// add a new bound
c.addBound(&Bound{
Column: column,
Lower: lower,
Upper: upper,
HasLower: updateLower,
HasUpper: updateUpper,
})
}
func (c *Range) Copy() *Range {
newChunk := NewChunkRange()
for _, bound := range c.Bounds {
newChunk.addBound(&Bound{
Column: bound.Column,
Lower: bound.Lower,
Upper: bound.Upper,
HasLower: bound.HasLower,
HasUpper: bound.HasUpper,
})
}
return newChunk
}
func (c *Range) Clone() *Range {
newChunk := NewChunkRange()
for _, bound := range c.Bounds {
newChunk.addBound(&Bound{
Column: bound.Column,
Lower: bound.Lower,
Upper: bound.Upper,
HasLower: bound.HasLower,
HasUpper: bound.HasUpper,
})
}
newChunk.Type = c.Type
newChunk.Where = c.Where
newChunk.Args = c.Args
for i, v := range c.columnOffset {
newChunk.columnOffset[i] = v
}
newChunk.Index = c.Index.Copy()
newChunk.IsFirst = c.IsFirst
newChunk.IsLast = c.IsLast
return newChunk
}
func (c *Range) CopyAndUpdate(column, lower, upper string, updateLower, updateUpper bool) *Range {
newChunk := c.Copy()
newChunk.Update(column, lower, upper, updateLower, updateUpper)
return newChunk
}
// Notice: chunk may contain not only one bucket, which can be expressed as a range [3, 5],
// And `lastBucketID` means the `5` and `firstBucketID` means the `3`.
func InitChunks(chunks []*Range, t ChunkType, firstBucketID, lastBucketID int, index int, collation, limits string, chunkCnt int) {
if chunks == nil {
return
}
for _, chunk := range chunks {
conditions, args := chunk.ToString(collation)
chunk.Where = fmt.Sprintf("((%s) AND (%s))", conditions, limits)
chunk.Args = args
chunk.Index = &ChunkID{
BucketIndexLeft: firstBucketID,
BucketIndexRight: lastBucketID,
ChunkIndex: index,
ChunkCnt: chunkCnt,
}
chunk.Type = t
index++
}
}
func InitChunk(chunk *Range, t ChunkType, firstBucketID, lastBucketID int, collation, limits string) {
conditions, args := chunk.ToString(collation)
chunk.Where = fmt.Sprintf("((%s) AND (%s))", conditions, limits)
chunk.Args = args
chunk.Index = &ChunkID{
BucketIndexLeft: firstBucketID,
BucketIndexRight: lastBucketID,
ChunkIndex: 0,
ChunkCnt: 1,
}
chunk.Type = t
}