-
Notifications
You must be signed in to change notification settings - Fork 12
/
eval_expr.go
72 lines (58 loc) · 1.29 KB
/
eval_expr.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
package datatable
import "github.com/pkg/errors"
// evaluateExpressions to evaluate all columns with a binded expression
func (t *DataTable) evaluateExpressions() error {
if !t.dirty || !t.hasExpr {
return nil
}
var cols []int
var exprCols []int
for i, c := range t.cols {
if c.IsComputed() {
exprCols = append(exprCols, i)
} else {
cols = append(cols, i)
}
}
l := len(exprCols)
if l == 0 {
t.dirty = false
return nil
}
// Initialize params
params := make(map[string][]interface{}, len(t.cols))
for _, pos := range cols {
col := t.cols[pos]
params[col.name] = col.serie.All()
}
// Evaluate
for _, idx := range exprCols {
col := t.cols[idx]
res, err := col.expr.Eval(params)
if err != nil {
return err
}
name := col.Name()
if arr, ok := res.([]interface{}); ok {
// Is array
ls := col.serie.Len()
la := len(arr)
if t.nrows != ls || la != ls {
err := errors.Errorf("evaluate expr : size mismatch %d vs %d", la, ls)
return errors.Wrap(err, ErrEvaluateExprSizeMismatch.Error())
}
for i := 0; i < t.nrows; i++ {
col.serie.Set(i, arr[i])
}
} else {
// Is scalar
for i := 0; i < t.nrows; i++ {
col.serie.Set(i, res)
}
}
// update dependency
params[name] = col.serie.All()
}
t.dirty = false
return nil
}