forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_builder.go
348 lines (321 loc) · 9.53 KB
/
index_builder.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
// Copyright 2021 Dolthub, 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 sql
import (
"gopkg.in/src-d/go-errors.v1"
)
var (
ErrInvalidColExpr = errors.NewKind("the expression `%s` could not be found from the index `%s`")
ErrRangeSimplification = errors.NewKind("attempting to simplify ranges has removed all ranges")
ErrInvalidRangeType = errors.NewKind("encountered the RangeType_Invalid")
)
// IndexBuilder builds ranges based on the combination of calls made for the given index, and then relies on the Index
// to return an IndexLookup from the created ranges.
type IndexBuilder struct {
idx Index
isInvalid bool
err error
colExprTypes map[string]Type
ranges map[string][]RangeColumnExpr
}
// NewIndexBuilder returns a new IndexBuilder. Used internally to construct a range that will later be passed to
// integrators through the Index function NewLookup.
func NewIndexBuilder(idx Index) *IndexBuilder {
colExprTypes := make(map[string]Type)
ranges := make(map[string][]RangeColumnExpr)
for _, cet := range idx.ColumnExpressionTypes() {
colExprTypes[cet.Expression] = cet.Type
ranges[cet.Expression] = []RangeColumnExpr{AllRangeColumnExpr(cet.Type)}
}
return &IndexBuilder{
idx: idx,
isInvalid: false,
err: nil,
colExprTypes: colExprTypes,
ranges: ranges,
}
}
// Equals represents colExpr = key. For IN expressions, pass all of them in the same Equals call.
func (b *IndexBuilder) Equals(ctx *Context, colExpr string, keys ...interface{}) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
potentialRanges := make([]RangeColumnExpr, len(keys))
for i, key := range keys {
potentialRanges[i] = ClosedRangeColumnExpr(key, key, typ)
}
b.updateCol(ctx, colExpr, potentialRanges...)
return b
}
// NotEquals represents colExpr <> key.
func (b *IndexBuilder) NotEquals(ctx *Context, colExpr string, key interface{}) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
b.updateCol(ctx, colExpr, GreaterThanRangeColumnExpr(key, typ), LessThanRangeColumnExpr(key, typ))
if !b.isInvalid {
ranges, err := SimplifyRangeColumn(b.ranges[colExpr]...)
if err != nil {
b.isInvalid = true
b.err = err
return b
}
if len(ranges) == 0 {
b.isInvalid = true
return b
}
b.ranges[colExpr] = ranges
}
return b
}
// GreaterThan represents colExpr > key.
func (b *IndexBuilder) GreaterThan(ctx *Context, colExpr string, key interface{}) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
b.updateCol(ctx, colExpr, GreaterThanRangeColumnExpr(key, typ))
return b
}
// GreaterOrEqual represents colExpr >= key.
func (b *IndexBuilder) GreaterOrEqual(ctx *Context, colExpr string, key interface{}) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
b.updateCol(ctx, colExpr, GreaterOrEqualRangeColumnExpr(key, typ))
return b
}
// LessThan represents colExpr < key.
func (b *IndexBuilder) LessThan(ctx *Context, colExpr string, key interface{}) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
b.updateCol(ctx, colExpr, LessThanRangeColumnExpr(key, typ))
return b
}
// LessOrEqual represents colExpr <= key.
func (b *IndexBuilder) LessOrEqual(ctx *Context, colExpr string, key interface{}) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
b.updateCol(ctx, colExpr, LessOrEqualRangeColumnExpr(key, typ))
return b
}
// IsNull represents colExpr = nil
func (b *IndexBuilder) IsNull(ctx *Context, colExpr string) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
b.updateCol(ctx, colExpr, NullRangeColumnExpr(typ))
return b
}
// IsNotNull represents colExpr != nil
func (b *IndexBuilder) IsNotNull(ctx *Context, colExpr string) *IndexBuilder {
if b.isInvalid {
return b
}
typ, ok := b.colExprTypes[colExpr]
if !ok {
b.isInvalid = true
b.err = ErrInvalidColExpr.New(colExpr, b.idx.ID())
return b
}
b.updateCol(ctx, colExpr, NotNullRangeColumnExpr(typ))
return b
}
// Ranges returns all ranges for this index builder. If the builder is in an error state then this returns nil.
func (b *IndexBuilder) Ranges(ctx *Context) RangeCollection {
if b.err != nil {
return nil
}
// An invalid builder that did not error got into a state where no columns will ever match, so we return an empty range
if b.isInvalid {
cets := b.idx.ColumnExpressionTypes()
emptyRange := make(Range, len(cets))
for i, cet := range cets {
emptyRange[i] = EmptyRangeColumnExpr(cet.Type)
}
return RangeCollection{emptyRange}
}
var allColumns [][]RangeColumnExpr
for _, colExpr := range b.idx.Expressions() {
ranges, ok := b.ranges[colExpr]
if !ok {
// An index builder is guaranteed to cover the first n expressions, so if we hit an expression that we do
// not have an entry for then we've hit all the ranges.
break
}
allColumns = append(allColumns, ranges)
}
// In the builder ranges map we store multiple column expressions per column, however we want all permutations to
// be their own range, so here we're creating a new range for every permutation.
colCounts := make([]int, len(allColumns))
permutation := make([]int, len(allColumns))
for i, rangeColumn := range allColumns {
colCounts[i] = len(rangeColumn)
}
var ranges []Range
exit := false
for !exit {
exit = true
currentRange := make(Range, len(allColumns))
for colIdx, exprCount := range colCounts {
permutation[colIdx] = (permutation[colIdx] + 1) % exprCount
if permutation[colIdx] != 0 {
exit = false
break
}
}
for colIdx, exprIdx := range permutation {
currentRange[colIdx] = allColumns[colIdx][exprIdx]
}
isempty, err := currentRange.IsEmpty()
if err != nil {
b.err = err
return nil
}
if !isempty {
ranges = append(ranges, currentRange)
}
}
if len(ranges) == 0 {
cets := b.idx.ColumnExpressionTypes()
emptyRange := make(Range, len(cets))
for i, cet := range cets {
emptyRange[i] = EmptyRangeColumnExpr(cet.Type)
}
return RangeCollection{emptyRange}
}
return ranges
}
// Build constructs a new IndexLookup based on the ranges that have been built internally by this builder.
func (b *IndexBuilder) Build(ctx *Context) (IndexLookup, error) {
if b.err != nil {
return emptyLookup, b.err
} else {
ranges := b.Ranges(ctx)
if len(ranges) == 0 {
return emptyLookup, nil
}
return IndexLookup{Index: b.idx, Ranges: ranges}, nil
}
}
// updateCol updates the internal columns with the given ranges by intersecting each given range with each existing
// range. That means that each given range is treated as an OR with respect to the other given ranges. If multiple
// ranges are to be intersected with respect to one another, multiple calls to updateCol should be made.
func (b *IndexBuilder) updateCol(ctx *Context, colExpr string, potentialRanges ...RangeColumnExpr) {
if len(potentialRanges) == 0 {
return
}
currentRanges, ok := b.ranges[colExpr]
if !ok {
b.ranges[colExpr] = potentialRanges
return
}
var newRanges []RangeColumnExpr
for _, currentRange := range currentRanges {
for _, potentialRange := range potentialRanges {
newRange, ok, err := currentRange.TryIntersect(potentialRange)
if err != nil {
b.isInvalid = true
if !ErrInvalidValue.Is(err) {
b.err = err
}
return
}
if ok {
isempty, err := newRange.IsEmpty()
if err != nil {
b.isInvalid = true
b.err = err
return
}
if !isempty {
newRanges = append(newRanges, newRange)
}
}
}
}
// If we end up with zero ranges then we had an impossible combination, such as (x < 1 AND x > 1)
if len(newRanges) == 0 {
b.isInvalid = true
return
}
b.ranges[colExpr] = newRanges
}
// SpatialIndexBuilder is like the IndexBuilder, but spatial
type SpatialIndexBuilder struct {
idx Index
typ Type
rng RangeColumnExpr
}
func NewSpatialIndexBuilder(idx Index) *SpatialIndexBuilder {
return &SpatialIndexBuilder{idx: idx, typ: idx.ColumnExpressionTypes()[0].Type}
}
func (b *SpatialIndexBuilder) AddRange(lower, upper interface{}) *SpatialIndexBuilder {
b.rng = RangeColumnExpr{
LowerBound: Below{Key: lower},
UpperBound: Above{Key: upper},
Typ: b.typ,
}
return b
}
func (b *SpatialIndexBuilder) Build() (IndexLookup, error) {
return IndexLookup{
Index: b.idx,
Ranges: RangeCollection{{b.rng}},
IsSpatialLookup: true,
}, nil
}