-
Notifications
You must be signed in to change notification settings - Fork 4
/
column_evaluators_aggregate.go
404 lines (379 loc) · 10.5 KB
/
column_evaluators_aggregate.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
401
402
403
404
package compute_pipes
import (
"fmt"
"time"
)
// TransformationColumnSpec Type count
type countColumnEval struct {
inputPos int
outputPos int
where evalExpression
}
func (ctx *countColumnEval) initializeCurrentValue(currentValue *[]interface{}) {
if currentValue == nil {
return
}
(*currentValue)[ctx.outputPos] = int64(0)
}
func (ctx *countColumnEval) update(currentValue *[]interface{}, input *[]interface{}) error {
if currentValue == nil || input == nil {
return fmt.Errorf("error countColumnEval.update cannot have nil currentValue or input")
}
// if count(column_name), make sure the column is not nil
if ctx.inputPos >= 0 && (*input)[ctx.inputPos] == nil {
return nil
}
if ctx.where != nil {
w, err := ctx.where.eval(input)
if err != nil {
return fmt.Errorf("while evaluating where on count aggregate: %v", err)
}
if w == nil || w.(int) != 1 {
return nil
}
}
var count int64
c := (*currentValue)[ctx.outputPos]
if c != nil {
count = c.(int64)
}
(*currentValue)[ctx.outputPos] = count + 1
return nil
}
func (ctx *countColumnEval) done(currentValue *[]interface{}) error {
return nil
}
func (ctx *BuilderContext) buildCountEvaluator(source *InputChannel, outCh *OutputChannel, spec *TransformationColumnSpec) (*countColumnEval, error) {
if spec == nil || spec.Expr == nil {
return nil, fmt.Errorf("error: Type count must have Expr != nil")
}
inputPos := -1
var ok bool
if *spec.Expr != "*" {
inputPos, ok = source.columns[*spec.Expr]
if !ok {
return nil, fmt.Errorf("error: count needs a valid column name or * to count all rows")
}
}
var where evalExpression
var err error
if spec.Where != nil {
where, err = ctx.buildExprNodeEvaluator(source, outCh, spec.Where)
if err != nil {
return nil, fmt.Errorf("while building where expression: %v", err)
}
}
outputPos, ok := outCh.columns[spec.Name]
if !ok {
err = fmt.Errorf("error column %s not found in output source %s", spec.Name, outCh.config.Name)
}
return &countColumnEval{
inputPos: inputPos,
outputPos: outputPos,
where: where,
}, err
}
// TransformationColumnSpec Type distinct_count
type distinctCountColumnEval struct {
inputPos int
outputPos int
distinctValues map[string]bool
where evalExpression
}
func (ctx *distinctCountColumnEval) initializeCurrentValue(currentValue *[]interface{}) {
if currentValue == nil {
return
}
(*currentValue)[ctx.outputPos] = int64(0)
}
func (ctx *distinctCountColumnEval) update(currentValue *[]interface{}, input *[]interface{}) error {
if currentValue == nil || input == nil {
return fmt.Errorf("error countColumnEval.update cannot have nil currentValue or input")
}
// if count(column_name), make sure the column is not nil
value := (*input)[ctx.inputPos]
if value == nil {
return nil
}
if ctx.where != nil {
w, err := ctx.where.eval(input)
if err != nil {
return fmt.Errorf("while evaluating where on distinct_count aggregate: %v", err)
}
if w == nil || w.(int) != 1 {
return nil
}
}
//* TODO Currently distinct_count works only on string column, todo convert to string when column is not of type string
switch vv := value.(type) {
case string:
ctx.distinctValues[vv] = true
(*currentValue)[ctx.outputPos] = int64(len(ctx.distinctValues))
default:
return fmt.Errorf("error: distinct_count currently support only string columns")
}
return nil
}
func (ctx *distinctCountColumnEval) done(currentValue *[]interface{}) error {
return nil
}
func (ctx *BuilderContext) buildDistinctCountEvaluator(source *InputChannel, outCh *OutputChannel, spec *TransformationColumnSpec) (*distinctCountColumnEval, error) {
if spec == nil || spec.Expr == nil {
return nil, fmt.Errorf("error: Type distinct_count must have Expr != nil")
}
inputPos, ok := source.columns[*spec.Expr]
if !ok {
return nil, fmt.Errorf("error, count_distinct needs a valid column name")
}
var where evalExpression
var err error
if spec.Where != nil {
where, err = ctx.buildExprNodeEvaluator(source, outCh, spec.Where)
if err != nil {
return nil, fmt.Errorf("while building where expression: %v", err)
}
}
outputPos, ok := outCh.columns[spec.Name]
if !ok {
err = fmt.Errorf("error column %s not found in output source %s", spec.Name, outCh.config.Name)
}
return &distinctCountColumnEval{
inputPos: inputPos,
outputPos: outputPos,
where: where,
distinctValues: make(map[string]bool),
}, err
}
// add function used for aggregates, supports int, int64, float64
func add(lhs interface{}, rhs interface{}) (interface{}, error) {
if rhs == nil {
return lhs, nil
}
if lhs == nil {
lhs = rhs
return lhs, nil
}
switch lhsv := lhs.(type) {
// case string:
// switch rhsv := rhs.(type) {
// case string:
// case int:
// case int64:
// case float64:
// case time.Time:
// }
case int:
switch rhsv := rhs.(type) {
// case string:
// if strconv.Itoa(lhsv) == rhsv {
// return 1, nil
// }
// return 0, nil
case int:
return lhsv + rhsv, nil
case int64:
return int64(lhsv) + rhsv, nil
case float64:
return float64(lhsv) + rhsv, nil
// case time.Time:
}
case int64:
switch rhsv := rhs.(type) {
// case string:
case int:
return lhsv + int64(rhsv), nil
case int64:
return lhsv + rhsv, nil
case float64:
return float64(lhsv) + rhsv, nil
// case time.Time:
}
case float64:
switch rhsv := rhs.(type) {
// case string:
case int:
return lhsv + float64(rhsv), nil
case int64:
return lhsv + float64(rhsv), nil
case float64:
return lhsv + rhsv, nil
// case time.Time:
}
// case time.Time:
}
return nil, fmt.Errorf("add called with unsupported types: (%T, %T)",lhs, rhs)
}
// TransformationColumnSpec Type sum
type sumColumnEval struct {
inputPos int
outputPos int
where evalExpression
}
func (ctx *sumColumnEval) initializeCurrentValue(currentValue *[]interface{}) {
// by default use int64, may change to float64 based on data
(*currentValue)[ctx.outputPos] = int64(0)
}
func (ctx *sumColumnEval) update(currentValue *[]interface{}, input *[]interface{}) error {
if currentValue == nil || input == nil {
return fmt.Errorf("error sumColumnEval.update cannot have nil currentValue or input")
}
// sum(column_name), make sure the column is not nil
value := (*input)[ctx.inputPos]
if value == nil {
return nil
}
if ctx.where != nil {
w, err := ctx.where.eval(input)
if err != nil {
return fmt.Errorf("while evaluating where on sum aggregate: %v", err)
}
if w == nil || w.(int) != 1 {
return nil
}
}
//* TODO Sum start with int64 as result type, upgrades to float64 if needed - update to use data model for rdf:type
var err error
cv := (*currentValue)[ctx.outputPos]
cv, err = add(cv, (*input)[ctx.inputPos])
if err != nil {
return err
}
(*currentValue)[ctx.outputPos] = cv
return nil
}
func (ctx *sumColumnEval) done(currentValue *[]interface{}) error {
return nil
}
func (ctx *BuilderContext) buildSumEvaluator(source *InputChannel, outCh *OutputChannel, spec *TransformationColumnSpec) (*sumColumnEval, error) {
if spec == nil || spec.Expr == nil {
return nil, fmt.Errorf("error: Type sum must have Expr != nil")
}
inputPos, ok := source.columns[*spec.Expr]
if !ok {
return nil, fmt.Errorf("error, sum needs a valid column name")
}
var where evalExpression
var err error
if spec.Where != nil {
where, err = ctx.buildExprNodeEvaluator(source, outCh, spec.Where)
if err != nil {
return nil, fmt.Errorf("while building where expression for sum aggregate: %v", err)
}
}
outputPos, ok := outCh.columns[spec.Name]
if !ok {
err = fmt.Errorf("error column %s not found in output source %s", spec.Name, outCh.config.Name)
}
return &sumColumnEval{
inputPos: inputPos,
outputPos: outputPos,
where: where,
}, err
}
// min function used for aggregates, supports int, int64, float64, time
func minAgg(lhs interface{}, rhs interface{}) (interface{}, error) {
if rhs == nil {
return lhs, nil
}
if lhs == nil {
return rhs, nil
}
switch lhsv := lhs.(type) {
case int:
switch rhsv := rhs.(type) {
case int:
return min(lhsv, rhsv), nil
case int64:
return min(int64(lhsv), rhsv), nil
case float64:
return min(float64(lhsv), rhsv), nil
}
case int64:
switch rhsv := rhs.(type) {
case int:
return min(lhsv, int64(rhsv)), nil
case int64:
return min(lhsv, rhsv), nil
case float64:
return min(float64(lhsv), rhsv), nil
}
case float64:
switch rhsv := rhs.(type) {
case int:
return min(lhsv, float64(rhsv)), nil
case int64:
return min(lhsv, float64(rhsv)), nil
case float64:
return min(lhsv, rhsv), nil
}
case time.Time:
switch rhsv := rhs.(type) {
case time.Time:
if lhsv.Before(rhsv) {
return lhsv, nil
}
return rhsv, nil
}
}
return nil, fmt.Errorf("minAgg called with unsupported types: (%T, %T)",lhs, rhs)
}
// TransformationColumnSpec Type min
type minColumnEval struct {
inputPos int
outputPos int
where evalExpression
}
func (ctx *minColumnEval) initializeCurrentValue(currentValue *[]interface{}) {}
func (ctx *minColumnEval) update(currentValue *[]interface{}, input *[]interface{}) error {
if currentValue == nil || input == nil {
return fmt.Errorf("error minColumnEval.update cannot have nil currentValue or input")
}
value := (*input)[ctx.inputPos]
if value == nil {
return nil
}
if ctx.where != nil {
w, err := ctx.where.eval(input)
if err != nil {
return fmt.Errorf("while evaluating where on min aggregate: %v", err)
}
if w == nil || w.(int) != 1 {
return nil
}
}
var err error
(*currentValue)[ctx.outputPos], err = minAgg((*currentValue)[ctx.outputPos], (*input)[ctx.inputPos])
if err != nil {
return err
}
return nil
}
func (ctx *minColumnEval) done(currentValue *[]interface{}) error {
return nil
}
func (ctx *BuilderContext) buildMinEvaluator(source *InputChannel, outCh *OutputChannel, spec *TransformationColumnSpec) (*minColumnEval, error) {
if spec == nil || spec.Expr == nil {
return nil, fmt.Errorf("error: Type min must have Expr != nil")
}
inputPos, ok := source.columns[*spec.Expr]
if !ok {
return nil, fmt.Errorf("error, min needs a valid column name")
}
var where evalExpression
var err error
if spec.Where != nil {
where, err = ctx.buildExprNodeEvaluator(source, outCh, spec.Where)
if err != nil {
return nil, fmt.Errorf("while building where expression for min aggregate: %v", err)
}
}
outputPos, ok := outCh.columns[spec.Name]
if !ok {
err = fmt.Errorf("error column %s not found in output source %s", spec.Name, outCh.config.Name)
}
return &minColumnEval{
inputPos: inputPos,
outputPos: outputPos,
where: where,
}, err
}