forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
344 lines (323 loc) · 9.62 KB
/
util.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
// Copyright 2016 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 expression
import (
"strconv"
"strings"
"time"
"unicode"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mvmap"
"github.com/pingcap/tidb/util/types"
)
// ExtractColumns extracts all columns from an expression.
func ExtractColumns(expr Expression) (cols []*Column) {
switch v := expr.(type) {
case *Column:
return []*Column{v}
case *ScalarFunction:
for _, arg := range v.GetArgs() {
cols = append(cols, ExtractColumns(arg)...)
}
}
return
}
// ColumnSubstitute substitutes the columns in filter to expressions in select fields.
// e.g. select * from (select b as a from t) k where a < 10 => select * from (select b as a from t where b < 10) k.
func ColumnSubstitute(expr Expression, schema *Schema, newExprs []Expression) Expression {
switch v := expr.(type) {
case *Column:
id := schema.ColumnIndex(v)
if id == -1 {
return v
}
return newExprs[id].Clone()
case *ScalarFunction:
if v.FuncName.L == ast.Cast {
newFunc := v.Clone().(*ScalarFunction)
newFunc.GetArgs()[0] = ColumnSubstitute(newFunc.GetArgs()[0], schema, newExprs)
return newFunc
}
newArgs := make([]Expression, 0, len(v.GetArgs()))
for _, arg := range v.GetArgs() {
newArgs = append(newArgs, ColumnSubstitute(arg, schema, newExprs))
}
fun, _ := NewFunction(v.GetCtx(), v.FuncName.L, v.RetType, newArgs...)
return fun
}
return expr
}
func datumsToConstants(datums []types.Datum) []Expression {
constants := make([]Expression, 0, len(datums))
for _, d := range datums {
constants = append(constants, &Constant{Value: d, RetType: types.NewFieldType(kindToMysqlType[d.Kind()])})
}
return constants
}
func primitiveValsToConstants(args []interface{}) []Expression {
return datumsToConstants(types.MakeDatums(args...))
}
var kindToMysqlType = map[byte]byte{
types.KindNull: mysql.TypeNull,
types.KindInt64: mysql.TypeLonglong,
types.KindUint64: mysql.TypeLonglong,
types.KindMysqlHex: mysql.TypeLonglong,
types.KindMinNotNull: mysql.TypeLonglong,
types.KindMaxValue: mysql.TypeLonglong,
types.KindFloat32: mysql.TypeDouble,
types.KindFloat64: mysql.TypeDouble,
types.KindString: mysql.TypeVarString,
types.KindBytes: mysql.TypeVarString,
types.KindMysqlBit: mysql.TypeBit,
types.KindMysqlEnum: mysql.TypeEnum,
types.KindMysqlSet: mysql.TypeSet,
types.KindRow: mysql.TypeVarString,
types.KindInterface: mysql.TypeVarString,
types.KindMysqlDecimal: mysql.TypeNewDecimal,
types.KindMysqlDuration: mysql.TypeDuration,
types.KindMysqlTime: mysql.TypeDatetime,
}
// calculateSum adds v to sum.
func calculateSum(sc *variable.StatementContext, sum, v types.Datum) (data types.Datum, err error) {
// for avg and sum calculation
// avg and sum use decimal for integer and decimal type, use float for others
// see https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
switch v.Kind() {
case types.KindNull:
case types.KindInt64, types.KindUint64:
var d *types.MyDecimal
d, err = v.ToDecimal(sc)
if err == nil {
data = types.NewDecimalDatum(d)
}
case types.KindMysqlDecimal:
data = v
default:
var f float64
f, err = v.ToFloat64(sc)
if err == nil {
data = types.NewFloat64Datum(f)
}
}
if err != nil {
return data, errors.Trace(err)
}
if data.IsNull() {
return sum, nil
}
switch sum.Kind() {
case types.KindNull:
return data, nil
case types.KindFloat64, types.KindMysqlDecimal:
return types.ComputePlus(sum, data)
default:
return data, errors.Errorf("invalid value %v for aggregate", sum.Kind())
}
}
// getValidPrefix gets a prefix of string which can parsed to a number with base. the minimum base is 2 and the maximum is 36.
func getValidPrefix(s string, base int64) string {
var (
validLen int
upper rune
)
switch {
case base >= 2 && base <= 9:
upper = rune('0' + base)
case base <= 36:
upper = rune('A' + base - 10)
default:
return ""
}
Loop:
for i := 0; i < len(s); i++ {
c := rune(s[i])
switch {
case unicode.IsDigit(c) || unicode.IsLower(c) || unicode.IsUpper(c):
c = unicode.ToUpper(c)
if c < upper {
validLen = i + 1
} else {
break Loop
}
case c == '+' || c == '-':
if i != 0 {
break Loop
}
default:
break Loop
}
}
if validLen > 1 && s[0] == '+' {
return s[1:validLen]
}
return s[:validLen]
}
// createDistinctChecker creates a new distinct checker.
func createDistinctChecker() *distinctChecker {
return &distinctChecker{
existingKeys: mvmap.NewMVMap(),
}
}
// distinctChecker stores existing keys and checks if given data is distinct.
type distinctChecker struct {
existingKeys *mvmap.MVMap
buf []byte
}
// Check checks if values is distinct.
func (d *distinctChecker) Check(values []types.Datum) (bool, error) {
d.buf = d.buf[:0]
var err error
d.buf, err = codec.EncodeValue(d.buf, values...)
if err != nil {
return false, errors.Trace(err)
}
v := d.existingKeys.Get(d.buf)
if v != nil {
return false, nil
}
d.existingKeys.Put(d.buf, []byte{})
return true, nil
}
// SubstituteCorCol2Constant will substitute correlated column to constant value which it contains.
// If the args of one scalar function are all constant, we will substitute it to constant.
func SubstituteCorCol2Constant(expr Expression) (Expression, error) {
switch x := expr.(type) {
case *ScalarFunction:
allConstant := true
newArgs := make([]Expression, 0, len(x.GetArgs()))
for _, arg := range x.GetArgs() {
newArg, err := SubstituteCorCol2Constant(arg)
if err != nil {
return nil, errors.Trace(err)
}
_, ok := newArg.(*Constant)
newArgs = append(newArgs, newArg)
allConstant = allConstant && ok
}
if allConstant {
val, err := x.Eval(nil)
if err != nil {
return nil, errors.Trace(err)
}
return &Constant{Value: val, RetType: x.GetType()}, nil
}
var newSf Expression
if x.FuncName.L == ast.Cast {
newSf = NewCastFunc(x.RetType, newArgs[0], x.GetCtx())
} else {
newSf, _ = NewFunction(x.GetCtx(), x.FuncName.L, x.GetType(), newArgs...)
}
return newSf, nil
case *CorrelatedColumn:
return &Constant{Value: *x.Data, RetType: x.GetType()}, nil
default:
return x.Clone(), nil
}
}
// ConvertCol2CorCol will convert the column in the condition which can be found in outerSchema to a correlated column whose
// Column is this column. And please make sure the outerSchema.Columns[i].Equal(corCols[i].Column)) holds when you call this.
func ConvertCol2CorCol(cond Expression, corCols []*CorrelatedColumn, outerSchema *Schema) Expression {
switch x := cond.(type) {
case *ScalarFunction:
newArgs := make([]Expression, 0, len(x.GetArgs()))
for _, arg := range x.GetArgs() {
newArg := ConvertCol2CorCol(arg, corCols, outerSchema)
newArgs = append(newArgs, newArg)
}
var newSf Expression
if x.FuncName.L == ast.Cast {
newSf = NewCastFunc(x.RetType, newArgs[0], x.GetCtx())
} else {
newSf, _ = NewFunction(x.GetCtx(), x.FuncName.L, x.GetType(), newArgs...)
}
return newSf
case *Column:
if pos := outerSchema.ColumnIndex(x); pos >= 0 {
return corCols[pos]
}
}
return cond
}
// timeZone2Duration converts timezone whose format should satisfy the regular condition
// `(^(+|-)(0?[0-9]|1[0-2]):[0-5]?\d$)|(^+13:00$)` to time.Duration.
func timeZone2Duration(tz string) time.Duration {
sign := 1
if strings.HasPrefix(tz, "-") {
sign = -1
}
i := strings.Index(tz, ":")
h, _ := strconv.Atoi(tz[1:i])
m, _ := strconv.Atoi(tz[i+1:])
return time.Duration(sign) * (time.Duration(h)*time.Hour + time.Duration(m)*time.Minute)
}
var oppositeOp = map[string]string{
ast.LT: ast.GE,
ast.GE: ast.LT,
ast.GT: ast.LE,
ast.LE: ast.GT,
ast.EQ: ast.NE,
ast.NE: ast.EQ,
}
// PushDownNot pushes the `not` function down to the expression's arguments.
func PushDownNot(expr Expression, not bool, ctx context.Context) Expression {
if f, ok := expr.(*ScalarFunction); ok {
switch f.FuncName.L {
case ast.UnaryNot:
return PushDownNot(f.GetArgs()[0], !not, f.GetCtx())
case ast.LT, ast.GE, ast.GT, ast.LE, ast.EQ, ast.NE:
if not {
nf, _ := NewFunction(f.GetCtx(), oppositeOp[f.FuncName.L], f.GetType(), f.GetArgs()...)
return nf
}
for i, arg := range f.GetArgs() {
f.GetArgs()[i] = PushDownNot(arg, false, f.GetCtx())
}
return f
case ast.AndAnd:
if not {
args := f.GetArgs()
for i, a := range args {
args[i] = PushDownNot(a, true, f.GetCtx())
}
nf, _ := NewFunction(f.GetCtx(), ast.OrOr, f.GetType(), args...)
return nf
}
for i, arg := range f.GetArgs() {
f.GetArgs()[i] = PushDownNot(arg, false, f.GetCtx())
}
return f
case ast.OrOr:
if not {
args := f.GetArgs()
for i, a := range args {
args[i] = PushDownNot(a, true, f.GetCtx())
}
nf, _ := NewFunction(f.GetCtx(), ast.AndAnd, f.GetType(), args...)
return nf
}
for i, arg := range f.GetArgs() {
f.GetArgs()[i] = PushDownNot(arg, false, f.GetCtx())
}
return f
}
}
if not {
expr, _ = NewFunction(ctx, ast.UnaryNot, types.NewFieldType(mysql.TypeTiny), expr)
}
return expr
}