forked from pingcap/tidb
/
range.go
241 lines (215 loc) · 6.13 KB
/
range.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
// 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 types
import (
"fmt"
"math"
"strconv"
"strings"
"github.com/juju/errors"
"github.com/pingcap/tidb/sessionctx/variable"
)
// Range is the interface of the three type of range.
type Range interface {
fmt.Stringer
Convert2IntRange() IntColumnRange
Convert2ColumnRange() *ColumnRange
Convert2IndexRange() *IndexRange
}
// IntColumnRange represents a range for a integer column, both low and high are inclusive.
type IntColumnRange struct {
LowVal int64
HighVal int64
}
// IsPoint returns if the table range is a point.
func (tr *IntColumnRange) IsPoint() bool {
return tr.HighVal == tr.LowVal
}
func (tr IntColumnRange) String() string {
var l, r string
if tr.LowVal == math.MinInt64 {
l = "(-inf"
} else {
l = "[" + strconv.FormatInt(tr.LowVal, 10)
}
if tr.HighVal == math.MaxInt64 {
r = "+inf)"
} else if tr.HighVal == math.MinInt64 {
// This branch is for nil
r = "-inf)"
} else {
r = strconv.FormatInt(tr.HighVal, 10) + "]"
}
return l + "," + r
}
// Convert2IntRange implements the Convert2IntRange interface.
func (tr IntColumnRange) Convert2IntRange() IntColumnRange {
return tr
}
// Convert2ColumnRange implements the Convert2ColumnRange interface.
func (tr IntColumnRange) Convert2ColumnRange() *ColumnRange {
panic("you shouldn't call this method.")
}
// Convert2IndexRange implements the Convert2IndexRange interface.
func (tr IntColumnRange) Convert2IndexRange() *IndexRange {
panic("you shouldn't call this method.")
}
// ColumnRange represents a range for a column.
type ColumnRange struct {
Low Datum
High Datum
LowExcl bool
HighExcl bool
}
func (cr *ColumnRange) String() string {
var l, r string
if cr.LowExcl {
l = "("
} else {
l = "["
}
if cr.HighExcl {
r = ")"
} else {
r = "]"
}
return l + formatDatum(cr.Low) + "," + formatDatum(cr.High) + r
}
// Convert2IntRange implements the Convert2IntRange interface.
func (cr *ColumnRange) Convert2IntRange() IntColumnRange {
panic("you shouldn't call this method.")
}
// Convert2ColumnRange implements the Convert2ColumnRange interface.
func (cr *ColumnRange) Convert2ColumnRange() *ColumnRange {
return cr
}
// Convert2IndexRange implements the Convert2IndexRange interface.
func (cr *ColumnRange) Convert2IndexRange() *IndexRange {
panic("you shouldn't call this method.")
}
// IndexRange represents a range for an index.
type IndexRange struct {
LowVal []Datum
HighVal []Datum
LowExclude bool // Low value is exclusive.
HighExclude bool // High value is exclusive.
}
// Clone clones a IndexRange.
func (ir *IndexRange) Clone() *IndexRange {
newRange := &IndexRange{
LowVal: make([]Datum, 0, len(ir.LowVal)),
HighVal: make([]Datum, 0, len(ir.HighVal)),
LowExclude: ir.LowExclude,
HighExclude: ir.HighExclude,
}
for i, length := 0, len(ir.LowVal); i < length; i++ {
newRange.LowVal = append(newRange.LowVal, ir.LowVal[i])
}
for i, length := 0, len(ir.HighVal); i < length; i++ {
newRange.HighVal = append(newRange.HighVal, ir.HighVal[i])
}
return newRange
}
// IsPoint returns if the index range is a point.
func (ir *IndexRange) IsPoint(sc *variable.StatementContext) bool {
if len(ir.LowVal) != len(ir.HighVal) {
return false
}
for i := range ir.LowVal {
a := ir.LowVal[i]
b := ir.HighVal[i]
if a.Kind() == KindMinNotNull || b.Kind() == KindMaxValue {
return false
}
cmp, err := a.CompareDatum(sc, &b)
if err != nil {
return false
}
if cmp != 0 {
return false
}
}
return !ir.LowExclude && !ir.HighExclude
}
// Convert2IndexRange implements the Convert2IndexRange interface.
func (ir *IndexRange) String() string {
lowStrs := make([]string, 0, len(ir.LowVal))
for _, d := range ir.LowVal {
lowStrs = append(lowStrs, formatDatum(d))
}
highStrs := make([]string, 0, len(ir.LowVal))
for _, d := range ir.HighVal {
highStrs = append(highStrs, formatDatum(d))
}
l, r := "[", "]"
if ir.LowExclude {
l = "("
}
if ir.HighExclude {
r = ")"
}
return l + strings.Join(lowStrs, " ") + "," + strings.Join(highStrs, " ") + r
}
// Convert2IntRange implements the Convert2IntRange interface.
func (ir *IndexRange) Convert2IntRange() IntColumnRange {
panic("you shouldn't call this method.")
}
// Convert2ColumnRange implements the Convert2ColumnRange interface.
func (ir *IndexRange) Convert2ColumnRange() *ColumnRange {
panic("you shouldn't call this method.")
}
// Convert2IndexRange implements the Convert2IndexRange interface.
func (ir *IndexRange) Convert2IndexRange() *IndexRange {
return ir
}
// Align appends low value and high value up to the number of columns with max value, min not null value or null value.
func (ir *IndexRange) Align(numColumns int) {
for i := len(ir.LowVal); i < numColumns; i++ {
if ir.LowExclude {
ir.LowVal = append(ir.LowVal, MaxValueDatum())
} else {
ir.LowVal = append(ir.LowVal, Datum{})
}
}
for i := len(ir.HighVal); i < numColumns; i++ {
if ir.HighExclude {
ir.HighVal = append(ir.HighVal, Datum{})
} else {
ir.HighVal = append(ir.HighVal, MaxValueDatum())
}
}
}
// PrefixEqualLen tells you how long the prefix of the range is a point.
// e.g. If this range is (1 2 3, 1 2 +inf), then the return value is 2.
func (ir *IndexRange) PrefixEqualLen(sc *variable.StatementContext) (int, error) {
// Here, len(ir.LowVal) always equal to len(ir.HighVal)
for i := 0; i < len(ir.LowVal); i++ {
cmp, err := ir.LowVal[i].CompareDatum(sc, &ir.HighVal[i])
if err != nil {
return 0, errors.Trace(err)
}
if cmp != 0 {
return i, nil
}
}
return len(ir.LowVal), nil
}
func formatDatum(d Datum) string {
if d.Kind() == KindMinNotNull {
return "-inf"
}
if d.Kind() == KindMaxValue {
return "+inf"
}
return fmt.Sprintf("%v", d.GetValue())
}