-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
new_refiner.go
284 lines (267 loc) · 9.38 KB
/
new_refiner.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
// 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 (
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/types"
)
func buildIndexRange(sc *variable.StatementContext, cols []*expression.Column, lengths []int,
accessCondition []expression.Expression) ([]*types.IndexRange, error) {
rb := builder{sc: sc}
var (
ranges []*types.IndexRange
eqAndInCount int
)
for eqAndInCount = 0; eqAndInCount < len(accessCondition) && eqAndInCount < len(cols); eqAndInCount++ {
if sf, ok := accessCondition[eqAndInCount].(*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[eqAndInCount])
if eqAndInCount == 0 {
ranges = rb.buildIndexRanges(point, cols[eqAndInCount].RetType)
} else {
ranges = rb.appendIndexRanges(ranges, point, cols[eqAndInCount].RetType)
}
}
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 eqAndInCount == 0 {
ranges = rb.buildIndexRanges(rangePoints, cols[0].RetType)
} else if eqAndInCount < len(accessCondition) {
ranges = rb.appendIndexRanges(ranges, rangePoints, cols[eqAndInCount].RetType)
}
// Take prefix index into consideration.
if hasPrefix(lengths) {
fixPrefixColRange(ranges, lengths)
}
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, errors.Trace(rb.err)
}
func hasPrefix(lengths []int) bool {
for _, l := range lengths {
if l != types.UnspecifiedLength {
return true
}
}
return false
}
func fixPrefixColRange(ranges []*types.IndexRange, lengths []int) {
for _, ran := range ranges {
for i := 0; i < len(ran.LowVal); i++ {
fixRangeDatum(&ran.LowVal[i], lengths[i])
}
ran.LowExclude = false
for i := 0; i < len(ran.HighVal); i++ {
fixRangeDatum(&ran.HighVal[i], lengths[i])
}
ran.HighExclude = false
}
}
func fixRangeDatum(v *types.Datum, length int) {
// If this column is prefix and the prefix length is smaller than the range, cut it.
if length != types.UnspecifiedLength && length < len(v.GetBytes()) {
v.SetBytes(v.GetBytes()[:length])
}
}
// getEQColOffset judge if the expression is a eq function that one side is constant and another is column.
// If so, it will return the offset of this column in the slice.
func getEQColOffset(expr expression.Expression, cols []*expression.Column) int {
f, ok := expr.(*expression.ScalarFunction)
if !ok || f.FuncName.L != ast.EQ {
return -1
}
if c, ok := f.GetArgs()[0].(*expression.Column); ok {
if _, ok := f.GetArgs()[1].(*expression.Constant); ok {
for i, col := range cols {
if col.Equal(c, nil) {
return i
}
}
}
}
if c, ok := f.GetArgs()[1].(*expression.Column); ok {
if _, ok := f.GetArgs()[0].(*expression.Constant); ok {
for i, col := range cols {
if col.Equal(c, nil) {
return i
}
}
}
}
return -1
}
// DetachIndexConditions will detach the index filters from table filters.
func DetachIndexConditions(conditions []expression.Expression, cols []*expression.Column, lengths []int) (accessConds []expression.Expression,
filterConds []expression.Expression) {
accessConds = make([]expression.Expression, len(cols))
// PushDownNot here can convert query 'not (a != 1)' to 'a = 1'.
for i, cond := range conditions {
conditions[i] = expression.PushDownNot(cond, false, nil)
}
var accessEqualCount int
for _, cond := range conditions {
offset := getEQColOffset(cond, cols)
if offset != -1 {
accessConds[offset] = cond
}
}
for i, cond := range accessConds {
if cond == nil {
accessConds = accessConds[:i]
accessEqualCount = i
break
}
if lengths[i] != types.UnspecifiedLength {
filterConds = append(filterConds, cond)
}
if i == len(accessConds)-1 {
accessEqualCount = len(accessConds)
}
}
// We should remove all accessConds, so that they will not be added to filter conditions.
conditions = removeAccessConditions(conditions, accessConds)
var curIndex int
for curIndex = accessEqualCount; curIndex < len(cols); curIndex++ {
checker := &conditionChecker{
cols: cols,
columnOffset: curIndex,
length: lengths[curIndex],
}
// First of all, we should extract all of in/eq expressions from rest conditions for every continuous index column.
// e.g. For index (a,b,c) and conditions a in (1,2) and b < 1 and c in (3,4), we should only extract column a in (1,2).
accessIdx := checker.findEqOrInFunc(conditions)
// If we fail to find any in or eq expression, we should consider all of other conditions for the next column.
if accessIdx == -1 {
accessConds, filterConds = checker.extractAccessAndFilterConds(conditions, accessConds, filterConds)
break
}
accessConds = append(accessConds, conditions[accessIdx])
if lengths[curIndex] != types.UnspecifiedLength {
filterConds = append(filterConds, conditions[accessIdx])
}
conditions = append(conditions[:accessIdx], conditions[accessIdx+1:]...)
}
// If curIndex equals to len of index columns, it means the rest conditions haven't been appended to filter conditions.
if curIndex == len(cols) {
filterConds = append(filterConds, conditions...)
}
return accessConds, filterConds
}
// buildColumnRange builds the range for sampling histogram to calculate the row count.
func buildColumnRange(conds []expression.Expression, sc *variable.StatementContext, tp *types.FieldType) ([]*types.ColumnRange, error) {
if len(conds) == 0 {
return []*types.ColumnRange{{Low: types.Datum{}, High: 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)
}
}
ranges := rb.buildColumnRanges(rangePoints, tp)
if rb.err != nil {
return nil, errors.Trace(rb.err)
}
return ranges, nil
}
// DetachCondsForSelectivity detaches the conditions used for range calculation from other useless conditions.
func DetachCondsForSelectivity(conds []expression.Expression, rangeType int, cols []*expression.Column,
lengths []int) (accessConditions, otherConditions []expression.Expression) {
if rangeType == IntRangeType || rangeType == ColumnRangeType {
return DetachColumnConditions(conds, cols[0].ColName)
} else if rangeType == IndexRangeType {
return DetachIndexConditions(conds, cols, lengths)
}
return nil, conds
}
// BuildRange is a method which can calculate IntColumnRange, ColumnRange, IndexRange.
func BuildRange(sc *variable.StatementContext, conds []expression.Expression, rangeType int, cols []*expression.Column,
lengths []int) (retRanges []types.Range, _ error) {
if rangeType == IntRangeType {
ranges, err := BuildTableRange(conds, sc)
if err != nil {
return nil, errors.Trace(err)
}
retRanges = make([]types.Range, 0, len(ranges))
for _, ran := range ranges {
retRanges = append(retRanges, ran)
}
} else if rangeType == ColumnRangeType {
ranges, err := buildColumnRange(conds, sc, cols[0].RetType)
if err != nil {
return nil, errors.Trace(err)
}
retRanges = make([]types.Range, 0, len(ranges))
for _, ran := range ranges {
retRanges = append(retRanges, ran)
}
} else if rangeType == IndexRangeType {
ranges, err := buildIndexRange(sc, cols, lengths, conds)
if err != nil {
return nil, errors.Trace(err)
}
retRanges = make([]types.Range, 0, len(ranges))
for _, ran := range ranges {
retRanges = append(retRanges, ran)
}
}
return
}
// Ranges2IntRanges changes []types.Range to []types.IntColumnRange
func Ranges2IntRanges(ranges []types.Range) []types.IntColumnRange {
retRanges := make([]types.IntColumnRange, 0, len(ranges))
for _, ran := range ranges {
retRanges = append(retRanges, ran.Convert2IntRange())
}
return retRanges
}
// Ranges2ColumnRanges changes []types.Range to []*types.ColumnRange
func Ranges2ColumnRanges(ranges []types.Range) []*types.ColumnRange {
retRanges := make([]*types.ColumnRange, 0, len(ranges))
for _, ran := range ranges {
retRanges = append(retRanges, ran.Convert2ColumnRange())
}
return retRanges
}
// Ranges2IndexRanges changes []types.Range to []*types.IndexRange
func Ranges2IndexRanges(ranges []types.Range) []*types.IndexRange {
retRanges := make([]*types.IndexRange, 0, len(ranges))
for _, ran := range ranges {
retRanges = append(retRanges, ran.Convert2IndexRange())
}
return retRanges
}