This repository was archived by the owner on Nov 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy patheval.cpp
More file actions
208 lines (186 loc) · 6.3 KB
/
eval.cpp
File metadata and controls
208 lines (186 loc) · 6.3 KB
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
#include "eval.h"
#include <sstream>
#include <unordered_map>
#include <functional>
#include <set>
#if defined(__APPLE__)
#include <array>
#endif
#include <boost/algorithm/string.hpp>
#include "muParser.h"
namespace Akumuli {
namespace QP {
static std::unordered_map<std::string, int> buildNameToIndexMapping(const QP::ReshapeRequest& req)
{
std::unordered_map<std::string, int> result;
const int ncol = static_cast<int>(req.select.columns.size());
const SeriesMatcherBase* matcher = req.select.global_matcher;
for(int ix = 0; ix < ncol; ix++) {
if (req.select.columns[ix].ids.empty()) {
continue;
}
auto idcol = req.select.columns[ix].ids.front();
auto rawstr = matcher->id2str(idcol);
// copy metric name from the begining until the ' ' or ':'
std::string sname(rawstr.first, rawstr.first + rawstr.second);
auto it = std::find_if(sname.begin(), sname.end(), [](char c) {
return std::isspace(c) || c == ':';
});
result[std::string(sname.begin(), it)] = ix;
}
return result;
}
// Muparser based implementation
struct MuparserEvalImpl : Node {
enum {
MAX_VALUES = MutableSample::MAX_PAYLOAD_SIZE / sizeof(double)
};
u32 nfields_;
std::array<u32, MAX_VALUES> indexes_;
std::array<double, MAX_VALUES> values_;
mu::Parser parser_;
std::shared_ptr<Node> next_;
MuparserEvalImpl(const MuparserEvalImpl&) = delete;
MuparserEvalImpl& operator = (const MuparserEvalImpl&) = delete;
static std::string preProcessExpression(std::string input,
const ReshapeRequest& req,
std::map<std::string, std::string>* varmap)
{
std::vector<std::string> vars;
for (auto const& col: req.select.columns) {
if (col.ids.empty()) {
continue;
}
auto id = col.ids.front();
auto st = req.select.global_matcher->id2str(id);
// extract series name
std::string metric(st.first, st.first + st.second);
auto pos = metric.find(' ');
if (pos != std::string::npos) {
metric.resize(pos);
}
vars.push_back(metric);
}
for (u32 i = 0; i < vars.size(); i++) {
std::string varname = "_var_" + std::to_string(i);
varmap->insert(std::make_pair(vars[i], varname));
boost::algorithm::replace_all(input, vars[i], varname);
}
return input;
}
//! Bild eval node using 'expr' field of the ptree object.
MuparserEvalImpl(const boost::property_tree::ptree& ptree,
const ReshapeRequest& req,
std::shared_ptr<Node> next)
: next_(next)
{
std::unordered_map<std::string, int> fields = buildNameToIndexMapping(req);
std::map<std::string, std::string> varmap;
auto const& expr = ptree.get_child_optional("expr");
std::map<std::string, double*> used;
if (expr) {
try {
parser_.EnableOptimizer(true);
auto str = expr->get_value<std::string>("");
auto pstr = preProcessExpression(str, req, &varmap);
parser_.SetExpr(pstr);
used = parser_.GetUsedVar();
} catch (mu::ParserError const& error) {
std::stringstream msg;
msg << "Expression parsing error at: " << static_cast<int>(error.GetPos())
<< " token: " << error.GetToken()
<< " message: " << error.GetMsg();
QueryParserError qerr(msg.str());
}
}
else {
QueryParserError err("'expr' field required");
BOOST_THROW_EXCEPTION(err);
}
nfields_ = static_cast<u32>(used.size());
auto ix = indexes_.begin();
auto vx = values_.begin();
std::set<std::string> defined;
// The indexes in indexes_ array should be sorted to
// improve runtime performance.
for (auto const& kv: fields) {
auto varname = varmap[kv.first];
if (used.count(varname)) {
parser_.DefineVar(varname, vx);
*vx++ = 0.;
*ix++ = static_cast<u32>(kv.second);
defined.insert(varname);
}
}
std::stringstream msg;
msg << "Unknown variable [";
bool error = false;
for (auto kv: used) {
if (!defined.count(kv.first)) {
if (!error) {
msg << kv.first;
}
else {
msg << ", " << kv.first;
}
error = true;
}
}
if (error) {
msg << "]";
QueryParserError parsererr(msg.str());
BOOST_THROW_EXCEPTION(parsererr);
}
if (parser_.GetNumResults() != 1) {
QueryParserError parsererr("Mutiple results are not supported");
BOOST_THROW_EXCEPTION(parsererr);
}
}
virtual void complete() {
next_->complete();
}
virtual bool put(MutableSample& mut) {
for (u32 i = 0; i < nfields_; i++) {
auto xs = mut[indexes_[i]];
values_[i] = xs ? *xs : 0.0;
}
double val = parser_.Eval();
mut.collapse();
if (!std::isnan(val)) {
*mut[0] = val;
return next_->put(mut);
}
return true;
}
virtual void set_error(aku_Status status) {
next_->set_error(status);
}
virtual int get_requirements() const {
return TERMINAL;
}
};
// ExprEval
ExprEval::ExprEval(const boost::property_tree::ptree& expr,
const ReshapeRequest& req,
std::shared_ptr<Node> next)
: impl_(std::make_shared<MuparserEvalImpl>(expr, req, next))
{
}
void ExprEval::complete()
{
impl_->complete();
}
bool ExprEval::put(MutableSample& sample)
{
return impl_->put(sample);
}
void ExprEval::set_error(aku_Status status)
{
impl_->set_error(status);
}
int ExprEval::get_requirements() const
{
return impl_->get_requirements();
}
static QueryParserToken<ExprEval> eval_token("eval");
}} // namespace