JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
Agg_Actions.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 08/05/2019.
3 //
4 
5 #ifndef JODA_AGG_ACTIONS_H
6 #define JODA_AGG_ACTIONS_H
14 
15 #include "../grammar/Grammar.h"
16 #include "../states/States.h"
17 
19 
20 template <>
22  static void apply0(aggState &state) {
23  assert(state.aggfun == NOAGG);
24  state.aggfun = DISTINCT;
25  }
26 };
27 template <>
29  static void apply0(aggState &state) {
30  assert(state.aggfun == NOAGG);
31  state.aggfun = ATTSTAT;
32  }
33 };
34 template <>
36  static void apply0(aggState &state) {
37  assert(state.aggfun == NOAGG);
38  state.aggfun = AVG;
39  }
40 };
41 template <>
43  static void apply0(aggState &state) {
44  assert(state.aggfun == NOAGG);
45  state.aggfun = COUNT;
46  }
47 };
48 template <>
50  static void apply0(aggState &state) {
51  assert(state.aggfun == NOAGG);
52  state.aggfun = SUM;
53  }
54 };
55 template <>
57  static void apply0(aggState &state) {
58  assert(state.aggfun == NOAGG);
59  state.aggfun = MIN;
60  }
61 };
62 template <>
64  static void apply0(aggState &state) {
65  assert(state.aggfun == NOAGG);
66  state.aggfun = COLLECT;
67  }
68 };
69 template <>
71  static void apply0(aggState &state) {
72  assert(state.aggfun == NOAGG);
73  state.aggfun = MAX;
74  }
75 };
76 
77 template <>
79  static void apply0(aggState &state) {
80  assert(state.aggfun == NOAGG);
81  state.aggfun = HISTOGRAM;
82  }
83 };
84 
85 template <>
87  template <typename Input>
88  static void apply(const Input &in, aggState &state) {
89  assert(state.toPointer.empty());
90  std::string pointer = in.string();
91  state.toPointer = pointer.substr(1, pointer.size() - 2);
92  }
93 };
94 
95 template <>
97  static void apply0(aggState &state) {
98  DCHECK(!state.valprov.empty());
99  DCHECK(state.groupedByValue == nullptr);
100  auto beforeSize = state.valprov.size();
101  state.groupedByValue = std::move(state.valprov.back());
102  state.valprov.pop_back();
103  DCHECK_EQ(beforeSize - 1, state.valprov.size());
104  DCHECK(state.groupedByValue != nullptr);
105  }
106 };
107 
108 template <>
110  template <typename Input>
111  static void apply(const Input &in, aggState &state) {
112  DCHECK(state.groupAs.empty());
113  std::string as = in.string();
114  state.groupAs = as;
115  }
116 };
117 
118 template <>
120  template <typename Input>
121  static void apply(const Input &in, aggState &state) {
122  assert(state.aggfun != NOAGG);
123  std::unique_ptr<joda::query::IAggregator> agg = nullptr;
124  try {
125  switch (state.aggfun) {
126  case NOAGG:
127  assert(false);
128  break;
129  case AVG:
130  agg = std::make_unique<joda::query::AverageAggregator>(
131  state.toPointer, std::move(state.valprov));
132  break;
133  case COUNT:
134  agg = std::make_unique<joda::query::CountAggregator>(
135  state.toPointer, std::move(state.valprov));
136  break;
137  case SUM:
138  agg = std::make_unique<joda::query::SumAggregator>(
139  state.toPointer, std::move(state.valprov));
140  break;
141  case ATTSTAT:
142  agg = std::make_unique<joda::query::AttributeStatAggregator>(
143  state.toPointer, std::move(state.valprov));
144  break;
145  case DISTINCT:
146  agg = std::make_unique<joda::query::DistinctAggregator>(
147  state.toPointer, std::move(state.valprov));
148  break;
149  case MIN:
150  agg = std::make_unique<joda::query::MinAggregator>(
151  state.toPointer, std::move(state.valprov));
152  break;
153  case MAX:
154  agg = std::make_unique<joda::query::MaxAggregator>(
155  state.toPointer, std::move(state.valprov));
156  break;
157  case COLLECT:
158  agg = std::make_unique<joda::query::CollectAggregator>(
159  state.toPointer, std::move(state.valprov));
160  break;
161  case HISTOGRAM:
162  agg = std::make_unique<joda::query::HistogramAggregator>(
163  state.toPointer, std::move(state.valprov));
164  }
166  throw tao::pegtl::parse_error(e.what(), in);
167  }
168  DCHECK(agg != nullptr);
169  if (state.groupedByValue == nullptr) { // Normal aggregation
170  state.aggs.push_back(std::move(agg));
171  } else { // Group by aggregation
172  auto groupAgg = std::make_unique<joda::query::GroupAggregator>(
173  state.toPointer, std::move(state.groupedByValue), std::move(agg));
174  if (!state.groupAs.empty()) {
175  groupAgg->setGroupAs(state.groupAs);
176  state.groupAs.clear();
177  }
178  state.aggs.push_back(std::move(groupAgg));
179  }
180  DCHECK(agg == nullptr);
181  DCHECK(state.groupedByValue == nullptr);
182  state.valprov.clear();
183  state.toPointer.clear();
184  state.aggfun = NOAGG;
185  assert(state.valprov.empty());
186  assert(state.toPointer.empty());
187  assert(state.aggfun == NOAGG);
188  }
189 };
190 } // namespace joda::queryparsing::grammar
191 #endif // JODA_AGG_ACTIONS_H
Definition: IValueProvider.h:46
virtual const char * what() const
Definition: IValueProvider.h:49
@ COLLECT
Definition: Agg_State.h:20
@ MAX
Definition: Agg_State.h:19
@ HISTOGRAM
Definition: Agg_State.h:21
@ AVG
Definition: Agg_State.h:13
@ MIN
Definition: Agg_State.h:18
@ SUM
Definition: Agg_State.h:15
@ DISTINCT
Definition: Agg_State.h:17
@ COUNT
Definition: Agg_State.h:14
@ ATTSTAT
Definition: Agg_State.h:16
@ NOAGG
Definition: Agg_State.h:12
static void apply(const Input &in, aggState &state)
Definition: Agg_Actions.h:111
static void apply0(aggState &state)
Definition: Agg_Actions.h:97
static void apply0(aggState &state)
Definition: Agg_Actions.h:29
static void apply0(aggState &state)
Definition: Agg_Actions.h:36
static void apply0(aggState &state)
Definition: Agg_Actions.h:64
static void apply0(aggState &state)
Definition: Agg_Actions.h:43
static void apply0(aggState &state)
Definition: Agg_Actions.h:22
static void apply0(aggState &state)
Definition: Agg_Actions.h:79
static void apply0(aggState &state)
Definition: Agg_Actions.h:71
static void apply0(aggState &state)
Definition: Agg_Actions.h:57
static void apply0(aggState &state)
Definition: Agg_Actions.h:50
static void apply(const Input &in, aggState &state)
Definition: Agg_Actions.h:121
static void apply(const Input &in, aggState &state)
Definition: Agg_Actions.h:88
Definition: Agg_State.h:24
std::string groupAs
Definition: Agg_State.h:50
std::vector< std::unique_ptr< joda::query::IValueProvider > > valprov
Definition: Agg_State.h:44
std::vector< std::unique_ptr< joda::query::IAggregator > > aggs
Definition: Agg_State.h:47
std::string toPointer
Definition: Agg_State.h:45
std::unique_ptr< joda::query::IValueProvider > groupedByValue
Definition: Agg_State.h:49
aggFunction aggfun
Definition: Agg_State.h:46
Definition: Literals.h:27