-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathbuiltin.go
396 lines (344 loc) · 13.8 KB
/
builtin.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.
// Copyright 2015 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.
//go:generate go run generator/compare_vec.go
//go:generate go run generator/control_vec.go
//go:generate go run generator/other_vec.go
package expression
import (
"fmt"
"github.com/pingcap/log"
"sort"
"strings"
"sync"
"github.com/gogo/protobuf/proto"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tipb/go-tipb"
)
// baseBuiltinFunc will be contained in every struct that implement builtinFunc interface.
type baseBuiltinFunc struct {
bufAllocator columnBufferAllocator
args []Expression
ctx sessionctx.Context
tp *types.FieldType
pbCode tipb.ScalarFuncSig
childrenVectorizedOnce *sync.Once
childrenVectorized bool
}
func (b *baseBuiltinFunc) PbCode() tipb.ScalarFuncSig {
return b.pbCode
}
// metadata returns the metadata of a function.
// metadata means some functions contain extra inner fields which will not
// contain in `tipb.Expr.children` but must be pushed down to coprocessor
func (b *baseBuiltinFunc) metadata() proto.Message {
// We will not use a field to store them because of only
// a few functions contain implicit parameters
return nil
}
func (b *baseBuiltinFunc) setPbCode(c tipb.ScalarFuncSig) {
b.pbCode = c
}
func newBaseBuiltinFunc(ctx sessionctx.Context, args []Expression) baseBuiltinFunc {
if ctx == nil {
panic("ctx should not be nil")
}
return baseBuiltinFunc{
bufAllocator: newLocalSliceBuffer(len(args)),
childrenVectorizedOnce: new(sync.Once),
args: args,
ctx: ctx,
tp: types.NewFieldType(mysql.TypeUnspecified),
}
}
// newBaseBuiltinFuncWithTp creates a built-in function signature with specified types of arguments and the return type of the function.
// argTps indicates the types of the args, retType indicates the return type of the built-in function.
// Every built-in function needs determined argTps and retType when we create it.
func newBaseBuiltinFuncWithTp(ctx sessionctx.Context, args []Expression, retType types.EvalType, argTps ...types.EvalType) (bf baseBuiltinFunc) {
if len(args) != len(argTps) {
panic("unexpected length of args and argTps")
}
if ctx == nil {
panic("ctx should not be nil")
}
for i := range args {
if argTps[i] != args[i].GetType().EvalType() {
log.Warn(fmt.Sprintf("unmatched arg type %v with %v", argTps[i], args[i].GetType().EvalType()))
}
}
var fieldType *types.FieldType
switch retType {
case types.ETInt:
fieldType = &types.FieldType{
Tp: mysql.TypeLonglong,
Flen: mysql.MaxIntWidth,
Decimal: 0,
Flag: mysql.BinaryFlag,
}
case types.ETReal:
fieldType = &types.FieldType{
Tp: mysql.TypeDouble,
Flen: mysql.MaxRealWidth,
Decimal: types.UnspecifiedLength,
Flag: mysql.BinaryFlag,
}
case types.ETString:
fieldType = &types.FieldType{
Tp: mysql.TypeVarString,
Flen: 0,
Decimal: types.UnspecifiedLength,
}
}
if mysql.HasBinaryFlag(fieldType.Flag) {
fieldType.Charset, fieldType.Collate = charset.CharsetBin, charset.CollationBin
} else {
fieldType.Charset, fieldType.Collate = charset.GetDefaultCharsetAndCollate()
}
return baseBuiltinFunc{
bufAllocator: newLocalSliceBuffer(len(args)),
childrenVectorizedOnce: new(sync.Once),
args: args,
ctx: ctx,
tp: fieldType,
}
}
func (b *baseBuiltinFunc) getArgs() []Expression {
return b.args
}
func (b *baseBuiltinFunc) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("baseBuiltinFunc.vecEvalInt() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) vecEvalReal(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("baseBuiltinFunc.vecEvalReal() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("baseBuiltinFunc.vecEvalString() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) vecEvalDecimal(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("baseBuiltinFunc.vecEvalDecimal() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) vecEvalTime(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("baseBuiltinFunc.vecEvalTime() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) vecEvalDuration(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("baseBuiltinFunc.vecEvalDuration() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) vecEvalJSON(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("baseBuiltinFunc.vecEvalJSON() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) evalInt(row chunk.Row) (int64, bool, error) {
return 0, false, errors.Errorf("baseBuiltinFunc.evalInt() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) evalReal(row chunk.Row) (float64, bool, error) {
return 0, false, errors.Errorf("baseBuiltinFunc.evalReal() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) evalString(row chunk.Row) (string, bool, error) {
return "", false, errors.Errorf("baseBuiltinFunc.evalString() should never be called, please contact the TiDB team for help")
}
func (b *baseBuiltinFunc) vectorized() bool {
return false
}
func (b *baseBuiltinFunc) isChildrenVectorized() bool {
b.childrenVectorizedOnce.Do(func() {
b.childrenVectorized = true
for _, arg := range b.args {
if !arg.Vectorized() {
b.childrenVectorized = false
break
}
}
})
return b.childrenVectorized
}
func (b *baseBuiltinFunc) getRetTp() *types.FieldType {
switch b.tp.EvalType() {
case types.ETString:
if b.tp.Flen >= mysql.MaxBlobWidth {
b.tp.Tp = mysql.TypeLongBlob
} else if b.tp.Flen >= 65536 {
b.tp.Tp = mysql.TypeMediumBlob
}
if len(b.tp.Charset) <= 0 {
b.tp.Charset, b.tp.Collate = charset.GetDefaultCharsetAndCollate()
}
}
return b.tp
}
func (b *baseBuiltinFunc) equal(fun builtinFunc) bool {
funArgs := fun.getArgs()
if len(funArgs) != len(b.args) {
return false
}
for i := range b.args {
if !b.args[i].Equal(b.ctx, funArgs[i]) {
return false
}
}
return true
}
func (b *baseBuiltinFunc) getCtx() sessionctx.Context {
return b.ctx
}
func (b *baseBuiltinFunc) cloneFrom(from *baseBuiltinFunc) {
b.args = make([]Expression, 0, len(b.args))
for _, arg := range from.args {
b.args = append(b.args, arg.Clone())
}
b.ctx = from.ctx
b.tp = from.tp
b.pbCode = from.pbCode
b.bufAllocator = newLocalSliceBuffer(len(b.args))
b.childrenVectorizedOnce = new(sync.Once)
}
func (b *baseBuiltinFunc) Clone() builtinFunc {
panic("you should not call this method.")
}
// vecBuiltinFunc contains all vectorized methods for a builtin function.
type vecBuiltinFunc interface {
// vectorized returns if this builtin function itself supports vectorized evaluation.
vectorized() bool
// isChildrenVectorized returns if its all children support vectorized evaluation.
isChildrenVectorized() bool
// vecEvalInt evaluates this builtin function in a vectorized manner.
vecEvalInt(input *chunk.Chunk, result *chunk.Column) error
// vecEvalReal evaluates this builtin function in a vectorized manner.
vecEvalReal(input *chunk.Chunk, result *chunk.Column) error
// vecEvalString evaluates this builtin function in a vectorized manner.
vecEvalString(input *chunk.Chunk, result *chunk.Column) error
// vecEvalDecimal evaluates this builtin function in a vectorized manner.
vecEvalDecimal(input *chunk.Chunk, result *chunk.Column) error
// vecEvalTime evaluates this builtin function in a vectorized manner.
vecEvalTime(input *chunk.Chunk, result *chunk.Column) error
// vecEvalDuration evaluates this builtin function in a vectorized manner.
vecEvalDuration(input *chunk.Chunk, result *chunk.Column) error
// vecEvalJSON evaluates this builtin function in a vectorized manner.
vecEvalJSON(input *chunk.Chunk, result *chunk.Column) error
}
// builtinFunc stands for a particular function signature.
type builtinFunc interface {
vecBuiltinFunc
// evalInt evaluates int result of builtinFunc by given row.
evalInt(row chunk.Row) (val int64, isNull bool, err error)
// evalReal evaluates real representation of builtinFunc by given row.
evalReal(row chunk.Row) (val float64, isNull bool, err error)
// evalString evaluates string representation of builtinFunc by given row.
evalString(row chunk.Row) (val string, isNull bool, err error)
// getArgs returns the arguments expressions.
getArgs() []Expression
// equal check if this function equals to another function.
equal(builtinFunc) bool
// getCtx returns this function's context.
getCtx() sessionctx.Context
// getRetTp returns the return type of the built-in function.
getRetTp() *types.FieldType
// setPbCode sets pbCode for signature.
setPbCode(tipb.ScalarFuncSig)
// PbCode returns PbCode of this signature.
PbCode() tipb.ScalarFuncSig
// metadata returns the metadata of a function.
// metadata means some functions contain extra inner fields which will not
// contain in `tipb.Expr.children` but must be pushed down to coprocessor
metadata() proto.Message
// Clone returns a copy of itself.
Clone() builtinFunc
}
// baseFunctionClass will be contained in every struct that implement functionClass interface.
type baseFunctionClass struct {
funcName string
minArgs int
maxArgs int
}
func (b *baseFunctionClass) verifyArgs(args []Expression) error {
l := len(args)
if l < b.minArgs || (b.maxArgs != -1 && l > b.maxArgs) {
return ErrIncorrectParameterCount.GenWithStackByArgs(b.funcName)
}
return nil
}
// functionClass is the interface for a function which may contains multiple functions.
type functionClass interface {
// getFunction gets a function signature by the types and the counts of given arguments.
getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error)
}
// funcs holds all registered builtin functions. When new function is added,
// check expression/function_traits.go to see if it should be appended to
// any set there.
var funcs = map[string]functionClass{
// common functions
ast.IsNull: &isNullFunctionClass{baseFunctionClass{ast.IsNull, 1, 1}},
// string functions
ast.Length: &lengthFunctionClass{baseFunctionClass{ast.Length, 1, 1}},
ast.OctetLength: &lengthFunctionClass{baseFunctionClass{ast.OctetLength, 1, 1}},
ast.Strcmp: &strcmpFunctionClass{baseFunctionClass{ast.Strcmp, 2, 2}},
// control functions
ast.If: &ifFunctionClass{baseFunctionClass{ast.If, 3, 3}},
ast.Ifnull: &ifNullFunctionClass{baseFunctionClass{ast.Ifnull, 2, 2}},
ast.LogicAnd: &logicAndFunctionClass{baseFunctionClass{ast.LogicAnd, 2, 2}},
ast.LogicOr: &logicOrFunctionClass{baseFunctionClass{ast.LogicOr, 2, 2}},
ast.GE: &compareFunctionClass{baseFunctionClass{ast.GE, 2, 2}, opcode.GE},
ast.LE: &compareFunctionClass{baseFunctionClass{ast.LE, 2, 2}, opcode.LE},
ast.EQ: &compareFunctionClass{baseFunctionClass{ast.EQ, 2, 2}, opcode.EQ},
ast.NE: &compareFunctionClass{baseFunctionClass{ast.NE, 2, 2}, opcode.NE},
ast.LT: &compareFunctionClass{baseFunctionClass{ast.LT, 2, 2}, opcode.LT},
ast.GT: &compareFunctionClass{baseFunctionClass{ast.GT, 2, 2}, opcode.GT},
ast.Plus: &arithmeticPlusFunctionClass{baseFunctionClass{ast.Plus, 2, 2}},
ast.Minus: &arithmeticMinusFunctionClass{baseFunctionClass{ast.Minus, 2, 2}},
ast.Div: &arithmeticDivideFunctionClass{baseFunctionClass{ast.Div, 2, 2}},
ast.Mul: &arithmeticMultiplyFunctionClass{baseFunctionClass{ast.Mul, 2, 2}},
ast.UnaryNot: &unaryNotFunctionClass{baseFunctionClass{ast.UnaryNot, 1, 1}},
ast.UnaryMinus: &unaryMinusFunctionClass{baseFunctionClass{ast.UnaryMinus, 1, 1}},
ast.In: &inFunctionClass{baseFunctionClass{ast.In, 2, -1}},
ast.RowFunc: &rowFunctionClass{baseFunctionClass{ast.RowFunc, 2, -1}},
ast.SetVar: &setVarFunctionClass{baseFunctionClass{ast.SetVar, 2, 2}},
ast.GetVar: &getVarFunctionClass{baseFunctionClass{ast.GetVar, 1, 1}},
}
// IsFunctionSupported check if given function name is a builtin sql function.
func IsFunctionSupported(name string) bool {
_, ok := funcs[name]
return ok
}
// GetBuiltinList returns a list of builtin functions
func GetBuiltinList() []string {
res := make([]string, 0, len(funcs))
notImplementedFunctions := []string{ast.RowFunc}
for funcName := range funcs {
skipFunc := false
// Skip not implemented functions
for _, notImplFunc := range notImplementedFunctions {
if funcName == notImplFunc {
skipFunc = true
}
}
// Skip literal functions
// (their names are not readable: 'tidb`.(dateliteral, for example)
// See: https://github.com/pingcap/tidb/parser/pull/591
if strings.HasPrefix(funcName, "'tidb`.(") {
skipFunc = true
}
if skipFunc {
continue
}
res = append(res, funcName)
}
sort.Strings(res)
return res
}