JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
NumberAggregator.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 10/6/17.
3 //
4 
5 #ifndef JODA_NUMBERAGGREGATOR_H
6 #define JODA_NUMBERAGGREGATOR_H
7 
9 #include <cmath>
10 #include <unordered_map>
11 #include "IAggregator.h"
12 
13 namespace joda::query {
15  typedef double AggRep;
16  static constexpr auto name = "EXAMPLE";
17 
18  inline static void aggregateFirst(AggRep &old, double val) { old = val; };
19 
20  inline static void aggregateRep(AggRep &old, AggRep &val) { old = val; };
21 
22  inline static void aggregate(AggRep &old, double val) { old = val; };
23 
24  inline static double getDouble(AggRep &val) { return val; };
25 };
26 
32 template <class T>
33 class NumberAggregator : public IAggregator {
34  public:
35  NumberAggregator(const std::string &toPointer,
36  std::vector<std::unique_ptr<IValueProvider>> &&params)
37  : IAggregator(toPointer, std::move(params)) {
38  /*
39  * check size
40  */
41  if (this->params.empty())
42  throw WrongParameterCountException(params.size(), 1, getName());
43  if (this->params.size() == 1) {
44  enableArray = false;
45  } else if (this->params.size() == 2) {
47  if (!this->params[1]->isConst())
48  throw WrongParameterException("Parameter 1 has to be const bool");
49  RJMemoryPoolAlloc alloc;
50  enableArray =
51  this->params[1]->getAtomValue(RapidJsonDocument(), alloc).GetBool();
52  } else
53  throw WrongParameterCountException(params.size(), 2, getName());
54 
55  auto type = this->params[0]->getReturnType();
56  if (!(type == IV_Any || type == IV_Number ||
57  (enableArray && type == IV_Array)))
59  };
60 
61  void merge(IAggregator *other) override {
62  auto *o = dynamic_cast<NumberAggregator<T> *>(other);
63  assert(o != nullptr);
64  assert(getName() == o->getName());
65  assert(toPointer == o->toPointer);
66  if (o->isAggregated) {
67  if (this->isAggregated) {
68  T::aggregateRep(this->val, o->val);
69  } else {
70  this->isAggregated = true;
71  this->val = o->val;
72  }
73  }
74  };
75 
76  RJValue terminate(RJMemoryPoolAlloc &alloc) override {
77  RJValue val;
78  if (isAggregated) {
79  val.SetDouble(T::getDouble(this->val));
80  }
81  return val;
82  };
83 
84  std::unique_ptr<IAggregator> duplicate() const override {
85  return std::make_unique<NumberAggregator<T>>(toPointer,
87  };
88 
89  void accumulate(const RapidJsonDocument &json,
90  RJMemoryPoolAlloc &alloc) override {
91  const RJValue *rjVal;
92  RJValue val_;
93  if (params[0]->isAtom()) {
94  val_ = params[0]->getAtomValue(json, alloc);
95  rjVal = &val_;
96  } else {
97  rjVal = params[0]->getValue(json, alloc);
98  }
99  if (rjVal != nullptr) {
100  if (rjVal->IsNumber()) {
101  if (isAggregated) {
102  T::aggregate(this->val, rjVal->GetDouble());
103  } else {
104  isAggregated = true;
105  T::aggregateFirst(this->val, rjVal->GetDouble());
106  }
107  }
108 
109  if (enableArray && rjVal->IsArray()) {
110  for (const auto &item : rjVal->GetArray()) {
111  if (item.IsNumber()) {
112  if (isAggregated) {
113  T::aggregate(this->val, rjVal->GetDouble());
114  } else {
115  isAggregated = true;
116  T::aggregateFirst(this->val, rjVal->GetDouble());
117  }
118  }
119  }
120  }
121  }
122  };
123 
124  static constexpr auto getName_() { return T::name; }
125 
126  const std::string getName() const override { return getName_(); };
127 
128  protected:
129  typename T::AggRep val{};
130  bool isAggregated = false;
131  bool enableArray = false;
132 };
133 
134 /*
135  * Aggregators
136  */
138  typedef double AggRep;
139  static constexpr auto name = "MIN";
140 
141  inline static void aggregateFirst(AggRep &old, double val) { old = val; };
142 
143  inline static void aggregateRep(AggRep &old, AggRep &val) {
144  old = std::min(old, val);
145  };
146 
147  inline static void aggregate(AggRep &old, double val) {
148  old = std::min(old, val);
149  };
150 
151  inline static double getDouble(AggRep &val) { return val; };
152 };
153 
155 
157  typedef double AggRep;
158  static constexpr auto name = "MAX";
159  inline static void aggregateFirst(AggRep &old, double val) { old = val; };
160 
161  inline static void aggregateRep(AggRep &old, AggRep &val) {
162  old = std::max(old, val);
163  };
164 
165  inline static void aggregate(AggRep &old, double val) {
166  old = std::max(old, val);
167  };
168 
169  inline static double getDouble(AggRep &val) { return val; };
170 };
171 
173 
175  typedef double AggRep;
176  static constexpr auto name = "SUM";
177  inline static void aggregateFirst(AggRep &old, double val) { old = val; };
178 
179  inline static void aggregateRep(AggRep &old, AggRep &val) { old += val; };
180 
181  inline static void aggregate(AggRep &old, double val) { old += val; };
182 
183  inline static double getDouble(AggRep &val) { return val; };
184 };
185 
187 
189  typedef std::pair<size_t, double> AggRep;
190  static constexpr auto name = "AVG";
191  inline static void aggregateFirst(AggRep &old, double val) {
192  old = {1, val};
193  };
194 
195  inline static void aggregateRep(AggRep &old, AggRep &val) {
196  old.first += val.first;
197  old.second += val.second;
198  };
199 
200  inline static void aggregate(AggRep &old, double val) {
201  old.first += 1;
202  old.second += val;
203  };
204 
205  inline static double getDouble(AggRep &val) {
206  return val.second / val.first;
207  };
208 };
209 
211 
213 
215 
217 
219 } // namespace joda::query
220 #endif // JODA_NUMBERAGGREGATOR_H
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: IAggregator.h:25
void checkParamType(unsigned int i, IValueType expected)
Definition: IAggregator.h:139
std::vector< std::unique_ptr< IValueProvider > > params
Definition: IAggregator.h:124
std::vector< std::unique_ptr< IValueProvider > > duplicateParameters() const
Definition: IAggregator.h:154
std::string toPointer
Definition: IAggregator.h:120
Definition: NumberAggregator.h:33
void accumulate(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) override
Definition: NumberAggregator.h:89
static constexpr auto getName_()
Definition: NumberAggregator.h:124
std::unique_ptr< IAggregator > duplicate() const override
Definition: NumberAggregator.h:84
RJValue terminate(RJMemoryPoolAlloc &alloc) override
Definition: NumberAggregator.h:76
T::AggRep val
Definition: NumberAggregator.h:129
const std::string getName() const override
Definition: NumberAggregator.h:126
void merge(IAggregator *other) override
Definition: NumberAggregator.h:61
bool enableArray
Definition: NumberAggregator.h:131
bool isAggregated
Definition: NumberAggregator.h:130
NumberAggregator(const std::string &toPointer, std::vector< std::unique_ptr< IValueProvider >> &&params)
Definition: NumberAggregator.h:35
Definition: IValueProvider.h:96
Definition: IValueProvider.h:46
Definition: IValueProvider.h:60
Definition: AttributeStatAggregator.h:12
NumberAggregator< AverageAggregatorFunc > AverageAggregator
Definition: NumberAggregator.h:210
NumberAggregator< MaxAggregatorFunc > MaxAggregator
Definition: NumberAggregator.h:172
NumberAggregator< MinAggregatorFunc > MinAggregator
Definition: NumberAggregator.h:154
NumberAggregator< SumAggregatorFunc > SumAggregator
Definition: NumberAggregator.h:186
@ IV_Number
Definition: IValueProvider.h:35
@ IV_Bool
Definition: IValueProvider.h:36
@ IV_Any
Definition: IValueProvider.h:39
@ IV_Array
Definition: IValueProvider.h:38
Definition: NumberAggregator.h:188
static double getDouble(AggRep &val)
Definition: NumberAggregator.h:205
static void aggregateRep(AggRep &old, AggRep &val)
Definition: NumberAggregator.h:195
std::pair< size_t, double > AggRep
Definition: NumberAggregator.h:189
static void aggregate(AggRep &old, double val)
Definition: NumberAggregator.h:200
static void aggregateFirst(AggRep &old, double val)
Definition: NumberAggregator.h:191
static constexpr auto name
Definition: NumberAggregator.h:190
Definition: NumberAggregator.h:14
static constexpr auto name
Definition: NumberAggregator.h:16
static void aggregateFirst(AggRep &old, double val)
Definition: NumberAggregator.h:18
static void aggregateRep(AggRep &old, AggRep &val)
Definition: NumberAggregator.h:20
static double getDouble(AggRep &val)
Definition: NumberAggregator.h:24
double AggRep
Definition: NumberAggregator.h:15
static void aggregate(AggRep &old, double val)
Definition: NumberAggregator.h:22
Definition: NumberAggregator.h:156
double AggRep
Definition: NumberAggregator.h:157
static void aggregate(AggRep &old, double val)
Definition: NumberAggregator.h:165
static void aggregateRep(AggRep &old, AggRep &val)
Definition: NumberAggregator.h:161
static void aggregateFirst(AggRep &old, double val)
Definition: NumberAggregator.h:159
static double getDouble(AggRep &val)
Definition: NumberAggregator.h:169
static constexpr auto name
Definition: NumberAggregator.h:158
Definition: NumberAggregator.h:137
static void aggregateRep(AggRep &old, AggRep &val)
Definition: NumberAggregator.h:143
static void aggregate(AggRep &old, double val)
Definition: NumberAggregator.h:147
double AggRep
Definition: NumberAggregator.h:138
static double getDouble(AggRep &val)
Definition: NumberAggregator.h:151
static constexpr auto name
Definition: NumberAggregator.h:139
static void aggregateFirst(AggRep &old, double val)
Definition: NumberAggregator.h:141
Definition: NumberAggregator.h:174
static double getDouble(AggRep &val)
Definition: NumberAggregator.h:183
double AggRep
Definition: NumberAggregator.h:175
static constexpr auto name
Definition: NumberAggregator.h:176
static void aggregateFirst(AggRep &old, double val)
Definition: NumberAggregator.h:177
static void aggregate(AggRep &old, double val)
Definition: NumberAggregator.h:181
static void aggregateRep(AggRep &old, AggRep &val)
Definition: NumberAggregator.h:179