evaluateExpressions to evaluate all columns with a binded expression
()
| 4 | |
| 5 | // evaluateExpressions to evaluate all columns with a binded expression |
| 6 | func (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 | } |