MCPcopy Create free account
hub / github.com/datasweet/datatable / evaluateExpressions

Method evaluateExpressions

eval_expr.go:6–72  ·  view source on GitHub ↗

evaluateExpressions to evaluate all columns with a binded expression

()

Source from the content-addressed store, hash-verified

4
5// evaluateExpressions to evaluate all columns with a binded expression
6func (t *DataTable) evaluateExpressions() error {
7 if !t.dirty || !t.hasExpr {
8 return nil
9 }
10
11 var cols []int
12 var exprCols []int
13 for i, c := range t.cols {
14 if c.IsComputed() {
15 exprCols = append(exprCols, i)
16 } else {
17 cols = append(cols, i)
18 }
19 }
20
21 l := len(exprCols)
22 if l == 0 {
23 t.dirty = false
24 return nil
25 }
26
27 // Initialize params
28 params := make(map[string][]interface{}, len(t.cols))
29 for _, pos := range cols {
30 col := t.cols[pos]
31 params[col.name] = col.serie.All()
32 }
33
34 // Evaluate
35 for _, idx := range exprCols {
36 col := t.cols[idx]
37 res, err := col.expr.Eval(params)
38 if err != nil {
39 return err
40 }
41
42 name := col.Name()
43
44 if arr, ok := res.([]interface{}); ok {
45 // Is array
46 ls := col.serie.Len()
47 la := len(arr)
48
49 if t.nrows != ls || la != ls {
50 err := errors.Errorf("evaluate expr : size mismatch %d vs %d", la, ls)
51 return errors.Wrap(err, ErrEvaluateExprSizeMismatch.Error())
52 }
53
54 for i := 0; i < t.nrows; i++ {
55 col.serie.Set(i, arr[i])
56 }
57
58 } else {
59 // Is scalar
60 for i := 0; i < t.nrows; i++ {
61 col.serie.Set(i, res)
62 }
63 }

Callers 7

WhereMethod · 0.95
ToMapMethod · 0.95
ToTableMethod · 0.95
ToSchemaMethod · 0.95
RecordsMethod · 0.95
RowsMethod · 0.95
RowMethod · 0.95

Calls 5

IsComputedMethod · 0.65
AllMethod · 0.65
NameMethod · 0.65
SetMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected