-
Notifications
You must be signed in to change notification settings - Fork 3
/
func.go
400 lines (377 loc) · 9.71 KB
/
func.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
397
398
399
400
package kvql
import (
"fmt"
"strconv"
"strings"
)
var (
funcMap = map[string]*Function{
"lower": &Function{"lower", 1, false, TSTR, funcToLower, funcToLowerVec},
"upper": &Function{"upper", 1, false, TSTR, funcToUpper, funcToUpperVec},
"int": &Function{"int", 1, false, TNUMBER, funcToInt, funcToIntVec},
"float": &Function{"float", 1, false, TNUMBER, funcToFloat, funcToFloatVec},
"str": &Function{"str", 1, false, TSTR, funcToString, funcToStringVec},
"is_int": &Function{"is_int", 1, false, TBOOL, funcIsInt, funcIsIntVec},
"is_float": &Function{"is_float", 1, false, TBOOL, funcIsFloat, funcIsFloatVec},
"substr": &Function{"substr", 3, false, TSTR, funcSubStr, funcSubStrVec},
"json": &Function{"json", 1, false, TJSON, funcJson, funcJsonVec},
"split": &Function{"split", 2, false, TLIST, funcSplit, funcSplitVec},
"list": &Function{"list", 1, true, TLIST, funcToList, funcToListVec},
"float_list": &Function{"float_list", 1, true, TLIST, funcFloatList, funcFloatListVec},
"int_list": &Function{"int_list", 1, true, TLIST, funcIntList, funcIntListVec},
"flist": &Function{"flist", 1, true, TLIST, funcFloatList, funcFloatListVec},
"ilist": &Function{"ilist", 1, true, TLIST, funcIntList, funcIntListVec},
"len": &Function{"len", 1, false, TNUMBER, funcLen, funcLenVec},
"join": &Function{"join", 2, true, TSTR, funcJoin, funcJoinVec},
"strlen": &Function{"strlen", 1, false, TNUMBER, funcStrlen, funcStrlenVec},
"cosine_distance": &Function{"cosine_distance", 2, false, TNUMBER, funcCosineDistance, funcCosineDistanceVec},
"l2_distance": &Function{"l2_distance", 2, false, TNUMBER, funcL2Distance, funcL2DistanceVec},
}
aggrFuncMap = map[string]*AggrFunc{
"count": &AggrFunc{"count", 1, false, TNUMBER, newAggrCountFunc},
"sum": &AggrFunc{"sum", 1, false, TNUMBER, newAggrSumFunc},
"avg": &AggrFunc{"avg", 1, false, TNUMBER, newAggrAvgFunc},
"min": &AggrFunc{"min", 1, false, TNUMBER, newAggrMinFunc},
"max": &AggrFunc{"max", 1, false, TNUMBER, newAggrMaxFunc},
"quantile": &AggrFunc{"quantile", 2, false, TNUMBER, newAggrQuantileFunc},
"json_arrayagg": &AggrFunc{"json_arrayagg", 1, false, TSTR, newAggrJsonArrayAggFunc},
"group_concat": &AggrFunc{"group_concat", 2, false, TSTR, newAggrGroupConcatFunc},
}
)
type FunctionBody func(kv KVPair, args []Expression, ctx *ExecuteCtx) (any, error)
type VectorFunctionBody func(chunk []KVPair, args []Expression, ctx *ExecuteCtx) ([]any, error)
type Function struct {
Name string
NumArgs int
VarArgs bool
ReturnType Type
Body FunctionBody
BodyVec VectorFunctionBody
}
type AggrFunc struct {
Name string
NumArgs int
VarArgs bool
ReturnType Type
Body AggrFunctor
}
type AggrFunctor func(args []Expression) (AggrFunction, error)
type AggrFunction interface {
Update(kv KVPair, args []Expression, ctx *ExecuteCtx) error
Complete() (any, error)
Clone() AggrFunction
}
func GetFuncNameFromExpr(expr Expression) (string, error) {
fc, ok := expr.(*FunctionCallExpr)
if !ok {
return "", NewSyntaxError(expr.GetPos(), "Not function call expression")
}
rfname, err := fc.Name.Execute(NewKVP(nil, nil), nil)
if err != nil {
return "", err
}
fname, ok := rfname.(string)
if !ok {
return "", NewSyntaxError(expr.GetPos(), "Invalid function name")
}
return strings.ToLower(fname), nil
}
func GetScalarFunction(expr Expression) (*Function, error) {
fname, err := GetFuncNameFromExpr(expr)
if err != nil {
return nil, err
}
fobj, have := funcMap[fname]
if !have {
return nil, NewSyntaxError(expr.GetPos(), "Cannot find function %s", fname)
}
return fobj, nil
}
func GetScalarFunctionByName(name string) (*Function, bool) {
fobj, have := funcMap[name]
return fobj, have
}
func GetAggrFunctionByName(name string) (*AggrFunc, bool) {
fobj, have := aggrFuncMap[name]
return fobj, have
}
func AddScalarFunction(f *Function) {
fname := strings.ToLower(f.Name)
funcMap[fname] = f
}
func AddAggrFunction(f *AggrFunc) {
fname := strings.ToLower(f.Name)
aggrFuncMap[fname] = f
}
func IsScalarFuncExpr(expr Expression) bool {
fname, err := GetFuncNameFromExpr(expr)
if err != nil {
return false
}
if _, have := funcMap[fname]; have {
return true
}
return false
}
func IsAggrFuncExpr(expr Expression) bool {
fname, err := GetFuncNameFromExpr(expr)
if err != nil {
return false
}
if _, have := aggrFuncMap[fname]; have {
return true
}
return false
}
func IsAggrFunc(fname string) bool {
_, have := aggrFuncMap[fname]
return have
}
func toString(value any) string {
switch val := value.(type) {
case string:
return val
case []byte:
return string(val)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", val)
case float32, float64:
return fmt.Sprintf("%f", val)
case bool:
if val {
return "true"
}
return "false"
default:
if val == nil {
return "<nil>"
}
return ""
}
}
func toInt(value any, defVal int64) int64 {
switch val := value.(type) {
case string:
if ret, err := strconv.ParseInt(val, 10, 64); err == nil {
return ret
} else {
if ret, err := strconv.ParseFloat(val, 64); err == nil {
return int64(ret)
}
return defVal
}
case []byte:
if ret, err := strconv.ParseInt(string(val), 10, 64); err == nil {
return ret
} else {
if ret, err := strconv.ParseFloat(string(val), 64); err == nil {
return int64(ret)
}
return defVal
}
case int8, int16, uint8, uint16:
if ret, err := strconv.ParseInt(fmt.Sprintf("%d", val), 10, 64); err == nil {
return ret
} else {
return defVal
}
case int:
return int64(val)
case uint:
return int64(val)
case int32:
return int64(val)
case uint32:
return int64(val)
case int64:
return val
case uint64:
return int64(val)
case float32:
return int64(val)
case float64:
return int64(val)
default:
return defVal
}
}
func toFloat(value any, defVal float64) float64 {
switch val := value.(type) {
case string:
if ret, err := strconv.ParseFloat(val, 64); err == nil {
return ret
} else {
return defVal
}
case []byte:
if ret, err := strconv.ParseFloat(string(val), 64); err == nil {
return ret
} else {
return defVal
}
case int8, int16, uint8, uint16:
if ret, err := strconv.ParseFloat(fmt.Sprintf("%d", val), 64); err == nil {
return ret
} else {
return defVal
}
case int:
return float64(val)
case uint:
return float64(val)
case int32:
return float64(val)
case uint32:
return float64(val)
case int64:
return float64(val)
case uint64:
return float64(val)
case float32:
return float64(val)
case float64:
return val
default:
return defVal
}
}
func toFloatList(value any) ([]float64, error) {
switch val := value.(type) {
case []string:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
fval, err := strconv.ParseFloat(val[i], 64)
if err != nil {
return nil, err
}
ret[i] = fval
}
return ret, nil
case [][]byte:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
fval, err := strconv.ParseFloat(string(val[i]), 64)
if err != nil {
return nil, err
}
ret[i] = fval
}
return ret, nil
case []int:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = float64(val[i])
}
return ret, nil
case []uint:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = float64(val[i])
}
return ret, nil
case []int32:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = float64(val[i])
}
return ret, nil
case []uint32:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = float64(val[i])
}
return ret, nil
case []int64:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = float64(val[i])
}
return ret, nil
case []uint64:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = float64(val[i])
}
return ret, nil
case []float32:
ret := make([]float64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = float64(val[i])
}
return ret, nil
case []float64:
return val, nil
default:
return nil, fmt.Errorf("Cannot convert to float list")
}
}
func toIntList(value any) ([]int64, error) {
switch val := value.(type) {
case []string:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
fval, err := strconv.ParseInt(val[i], 10, 64)
if err != nil {
return nil, err
}
ret[i] = fval
}
return ret, nil
case [][]byte:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
fval, err := strconv.ParseInt(string(val[i]), 10, 64)
if err != nil {
return nil, err
}
ret[i] = fval
}
return ret, nil
case []int:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = int64(val[i])
}
return ret, nil
case []uint:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = int64(val[i])
}
return ret, nil
case []int32:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = int64(val[i])
}
return ret, nil
case []uint32:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = int64(val[i])
}
return ret, nil
case []int64:
return val, nil
case []uint64:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = int64(val[i])
}
return ret, nil
case []float32:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = int64(val[i])
}
return ret, nil
case []float64:
ret := make([]int64, len(val))
for i := 0; i < len(val); i++ {
ret[i] = int64(val[i])
}
return ret, nil
default:
return nil, fmt.Errorf("Cannot convert to float list")
}
}