forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalar.go
290 lines (278 loc) · 9.41 KB
/
scalar.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
// 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 statistics
import (
"encoding/binary"
"math"
"time"
"github.com/cznic/mathutil"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
)
// calcFraction is used to calculate the fraction of the interval [lower, upper] that lies within the [lower, value]
// using the continuous-value assumption.
func calcFraction(lower, upper, value float64) float64 {
if upper <= lower {
return 0.5
}
if value <= lower {
return 0
}
if value >= upper {
return 1
}
frac := (value - lower) / (upper - lower)
if math.IsNaN(frac) || math.IsInf(frac, 0) || frac < 0 || frac > 1 {
return 0.5
}
return frac
}
func convertDatumToScalar(value *types.Datum, commonPfxLen int) float64 {
switch value.Kind() {
case types.KindMysqlDecimal:
scalar, err := value.GetMysqlDecimal().ToFloat64()
if err != nil {
return 0
}
return scalar
case types.KindMysqlTime:
valueTime := value.GetMysqlTime()
var minTime types.Time
switch valueTime.Type() {
case mysql.TypeDate:
minTime = types.NewTime(types.MinDatetime, mysql.TypeDate, types.DefaultFsp)
case mysql.TypeDatetime:
minTime = types.NewTime(types.MinDatetime, mysql.TypeDatetime, types.DefaultFsp)
case mysql.TypeTimestamp:
minTime = types.MinTimestamp
}
sc := &stmtctx.StatementContext{TimeZone: types.BoundTimezone}
return float64(valueTime.Sub(sc, &minTime).Duration)
case types.KindString, types.KindBytes:
bytes := value.GetBytes()
if len(bytes) <= commonPfxLen {
return 0
}
return convertBytesToScalar(bytes[commonPfxLen:])
default:
// do not know how to convert
return 0
}
}
// PreCalculateScalar converts the lower and upper to scalar. When the datum type is KindString or KindBytes, we also
// calculate their common prefix length, because when a value falls between lower and upper, the common prefix
// of lower and upper equals to the common prefix of the lower, upper and the value. For some simple types like `Int64`,
// we do not convert it because we can directly infer the scalar value.
func (hg *Histogram) PreCalculateScalar() {
len := hg.Len()
if len == 0 {
return
}
switch hg.GetLower(0).Kind() {
case types.KindMysqlDecimal, types.KindMysqlTime:
hg.scalars = make([]scalar, len)
for i := 0; i < len; i++ {
hg.scalars[i] = scalar{
lower: convertDatumToScalar(hg.GetLower(i), 0),
upper: convertDatumToScalar(hg.GetUpper(i), 0),
}
}
case types.KindBytes, types.KindString:
hg.scalars = make([]scalar, len)
for i := 0; i < len; i++ {
lower, upper := hg.GetLower(i), hg.GetUpper(i)
common := commonPrefixLength(lower.GetBytes(), upper.GetBytes())
hg.scalars[i] = scalar{
commonPfxLen: common,
lower: convertDatumToScalar(lower, common),
upper: convertDatumToScalar(upper, common),
}
}
}
}
func (hg *Histogram) calcFraction(index int, value *types.Datum) float64 {
lower, upper := hg.Bounds.GetRow(2*index), hg.Bounds.GetRow(2*index+1)
switch value.Kind() {
case types.KindFloat32:
return calcFraction(float64(lower.GetFloat32(0)), float64(upper.GetFloat32(0)), float64(value.GetFloat32()))
case types.KindFloat64:
return calcFraction(lower.GetFloat64(0), upper.GetFloat64(0), value.GetFloat64())
case types.KindInt64:
return calcFraction(float64(lower.GetInt64(0)), float64(upper.GetInt64(0)), float64(value.GetInt64()))
case types.KindUint64:
return calcFraction(float64(lower.GetUint64(0)), float64(upper.GetUint64(0)), float64(value.GetUint64()))
case types.KindMysqlDuration:
return calcFraction(float64(lower.GetDuration(0, 0).Duration), float64(upper.GetDuration(0, 0).Duration), float64(value.GetMysqlDuration().Duration))
case types.KindMysqlDecimal, types.KindMysqlTime:
return calcFraction(hg.scalars[index].lower, hg.scalars[index].upper, convertDatumToScalar(value, 0))
case types.KindBytes, types.KindString:
return calcFraction(hg.scalars[index].lower, hg.scalars[index].upper, convertDatumToScalar(value, hg.scalars[index].commonPfxLen))
}
return 0.5
}
func commonPrefixLength(lower, upper []byte) int {
minLen := len(lower)
if minLen > len(upper) {
minLen = len(upper)
}
for i := 0; i < minLen; i++ {
if lower[i] != upper[i] {
return i
}
}
return minLen
}
func convertBytesToScalar(value []byte) float64 {
// Bytes type is viewed as a base-256 value, so we only consider at most 8 bytes.
var buf [8]byte
copy(buf[:], value)
return float64(binary.BigEndian.Uint64(buf[:]))
}
func calcFraction4Datums(lower, upper, value *types.Datum) float64 {
switch value.Kind() {
case types.KindFloat32:
return calcFraction(float64(lower.GetFloat32()), float64(upper.GetFloat32()), float64(value.GetFloat32()))
case types.KindFloat64:
return calcFraction(lower.GetFloat64(), upper.GetFloat64(), value.GetFloat64())
case types.KindInt64:
return calcFraction(float64(lower.GetInt64()), float64(upper.GetInt64()), float64(value.GetInt64()))
case types.KindUint64:
return calcFraction(float64(lower.GetUint64()), float64(upper.GetUint64()), float64(value.GetUint64()))
case types.KindMysqlDuration:
return calcFraction(float64(lower.GetMysqlDuration().Duration), float64(upper.GetMysqlDuration().Duration), float64(value.GetMysqlDuration().Duration))
case types.KindMysqlDecimal, types.KindMysqlTime:
return calcFraction(convertDatumToScalar(lower, 0), convertDatumToScalar(upper, 0), convertDatumToScalar(value, 0))
case types.KindBytes, types.KindString:
commonPfxLen := commonPrefixLength(lower.GetBytes(), upper.GetBytes())
return calcFraction(convertDatumToScalar(lower, commonPfxLen), convertDatumToScalar(upper, commonPfxLen), convertDatumToScalar(value, commonPfxLen))
}
return 0.5
}
const maxNumStep = 10
func enumRangeValues(low, high types.Datum, lowExclude, highExclude bool) []types.Datum {
if low.Kind() != high.Kind() {
return nil
}
exclude := 0
if lowExclude {
exclude++
}
if highExclude {
exclude++
}
switch low.Kind() {
case types.KindInt64:
// Overflow check.
lowVal, highVal := low.GetInt64(), high.GetInt64()
if lowVal <= 0 && highVal >= 0 {
if lowVal < -maxNumStep || highVal > maxNumStep {
return nil
}
}
remaining := highVal - lowVal
if remaining >= maxNumStep+1 {
return nil
}
remaining = remaining + 1 - int64(exclude)
if remaining >= maxNumStep {
return nil
}
values := make([]types.Datum, 0, remaining)
startValue := lowVal
if lowExclude {
startValue++
}
for i := int64(0); i < remaining; i++ {
values = append(values, types.NewIntDatum(startValue+i))
}
return values
case types.KindUint64:
remaining := high.GetUint64() - low.GetUint64()
if remaining >= maxNumStep+1 {
return nil
}
remaining = remaining + 1 - uint64(exclude)
if remaining >= maxNumStep {
return nil
}
values := make([]types.Datum, 0, remaining)
startValue := low.GetUint64()
if lowExclude {
startValue++
}
for i := uint64(0); i < remaining; i++ {
values = append(values, types.NewUintDatum(startValue+i))
}
return values
case types.KindMysqlDuration:
lowDur, highDur := low.GetMysqlDuration(), high.GetMysqlDuration()
fsp := mathutil.MaxInt8(lowDur.Fsp, highDur.Fsp)
stepSize := int64(math.Pow10(int(types.MaxFsp-fsp))) * int64(time.Microsecond)
lowDur.Duration = lowDur.Duration.Round(time.Duration(stepSize))
remaining := int64(highDur.Duration-lowDur.Duration)/stepSize + 1 - int64(exclude)
if remaining >= maxNumStep {
return nil
}
startValue := int64(lowDur.Duration)
if lowExclude {
startValue += stepSize
}
values := make([]types.Datum, 0, remaining)
for i := int64(0); i < remaining; i++ {
values = append(values, types.NewDurationDatum(types.Duration{Duration: time.Duration(startValue + i*stepSize), Fsp: fsp}))
}
return values
case types.KindMysqlTime:
lowTime, highTime := low.GetMysqlTime(), high.GetMysqlTime()
if lowTime.Type() != highTime.Type() {
return nil
}
fsp := mathutil.MaxInt8(lowTime.Fsp(), highTime.Fsp())
var stepSize int64
sc := &stmtctx.StatementContext{TimeZone: time.UTC}
if lowTime.Type() == mysql.TypeDate {
stepSize = 24 * int64(time.Hour)
lowTime.SetCoreTime(types.FromDate(lowTime.Year(), lowTime.Month(), lowTime.Day(), 0, 0, 0, 0))
} else {
var err error
lowTime, err = lowTime.RoundFrac(sc, fsp)
if err != nil {
return nil
}
stepSize = int64(math.Pow10(int(types.MaxFsp-fsp))) * int64(time.Microsecond)
}
remaining := int64(highTime.Sub(sc, &lowTime).Duration)/stepSize + 1 - int64(exclude)
if remaining >= maxNumStep {
return nil
}
startValue := lowTime
var err error
if lowExclude {
startValue, err = lowTime.Add(sc, types.Duration{Duration: time.Duration(stepSize), Fsp: fsp})
if err != nil {
return nil
}
}
values := make([]types.Datum, 0, remaining)
for i := int64(0); i < remaining; i++ {
value, err := startValue.Add(sc, types.Duration{Duration: time.Duration(i * stepSize), Fsp: fsp})
if err != nil {
return nil
}
values = append(values, types.NewTimeDatum(value))
}
return values
}
return nil
}