Column Store
AggregatorQuery.h
Go to the documentation of this file.
1 #pragma once
2 #include <limits>
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 
8 
16 namespace ColumnStore {
26 struct Aggregator {
27  std::string name;
28 
31 
32  Aggregator(std::string name) : name(name) {}
33 
34  void initialize(Metadata metadata) {
35  columnIndex = (*metadata)[name].index;
36  type = (*metadata)[name].type;
37  }
38 
39  virtual void addRecord(DataRecord &record) = 0;
40  virtual float getValue() = 0;
41 
46  virtual ~Aggregator() {}
47  virtual std::string aggregatorType() = 0;
48  std::string getColumnName() { return aggregatorType() + "(" + name + ")"; }
49 
50  virtual std::shared_ptr<Aggregator> clone() = 0;
51 };
52 
57 typedef std::shared_ptr<Aggregator> AggregatorQuery;
58 
59 struct AverageAggregator : public Aggregator {
60  int count;
61  double sum;
62 
63  AverageAggregator(std::string name) : Aggregator(name), count(0), sum(0) {}
64 
65  std::string aggregatorType() { return "Avg"; }
66 
67  void addRecord(DataRecord &record) {
68  if (type == DataType::INT)
69  sum += record[columnIndex].as<int>();
70  else if (type == DataType::FLOAT)
71  sum += record[columnIndex].as<float>();
72  else
73  throw std::runtime_error("Unknown DataType");
74  count++;
75  }
76 
78  return AggregatorQuery(new AverageAggregator(*this));
79  }
80 
81  float getValue() {
82  assert(count);
83  return sum / count;
84  }
85 };
86 
87 struct SumAggregator : public Aggregator {
88  double sum;
89 
90  SumAggregator(std::string name) : Aggregator(name), sum(0) {}
91 
92  std::string aggregatorType() { return "Sum"; }
93 
94  void addRecord(DataRecord &record) {
95  if (type == DataType::INT)
96  sum += record[columnIndex].as<int>();
97  else if (type == DataType::FLOAT)
98  sum += record[columnIndex].as<float>();
99  else
100  throw std::runtime_error("Unknown DataType");
101  }
102 
104  return AggregatorQuery(new SumAggregator(*this));
105  }
106 
107  float getValue() { return sum; }
108 };
109 
110 struct CountAggregator : public Aggregator {
111  int count;
112  CountAggregator(std::string name) : Aggregator(name), count(0) {}
113 
114  std::string aggregatorType() { return "Count"; }
115 
116  void addRecord(DataRecord &record) { count++; }
117 
119  return AggregatorQuery(new CountAggregator(*this));
120  }
121 
122  float getValue() { return count; }
123 };
124 
125 struct MaxAggregator : public Aggregator {
126  float mx;
127 
128  MaxAggregator(std::string name)
129  : Aggregator(name), mx(std::numeric_limits<float>::min()) {}
130 
131  std::string aggregatorType() { return "Max"; }
132 
133  void addRecord(DataRecord &record) {
134  if (type == DataType::INT)
135  mx = std::max(mx, (float)record[columnIndex].as<int>());
136  else if (type == DataType::FLOAT)
137  mx = std::max(mx, record[columnIndex].as<float>());
138  else
139  throw std::runtime_error("Unknown DataType");
140  }
141 
142  float getValue() { return mx; }
143 
145  return AggregatorQuery(new MaxAggregator(*this));
146  }
147 };
148 
149 struct MinAggregator : public Aggregator {
150  float mn;
151 
152  MinAggregator(std::string name)
153  : Aggregator(name), mn(std::numeric_limits<float>::max()) {}
154 
155  std::string aggregatorType() { return "Min"; }
156 
157  void addRecord(DataRecord &record) {
158  if (type == DataType::INT)
159  mn = std::min(mn, (float)record[columnIndex].as<int>());
160  else if (type == DataType::FLOAT)
161  mn = std::min(mn, record[columnIndex].as<float>());
162  else
163  throw std::runtime_error("Unknown DataType");
164  }
165 
166  float getValue() { return mn; }
167 
169  return AggregatorQuery(new MinAggregator(*this));
170  }
171 };
172 
173 }; // namespace ColumnStore
ColumnStore::AggregatorQuery
std::shared_ptr< Aggregator > AggregatorQuery
Shared pointer to Query Structure.
Definition: AggregatorQuery.h:57
ColumnStore::CountAggregator::aggregatorType
std::string aggregatorType()
Definition: AggregatorQuery.h:114
ColumnStore::MaxAggregator::clone
AggregatorQuery clone()
Definition: AggregatorQuery.h:144
ColumnStore::Aggregator::name
std::string name
Definition: AggregatorQuery.h:27
ColumnStore::DataType
DataType
Different datatypes supported by this project.
Definition: Column.h:16
ColumnStore::AverageAggregator::sum
double sum
Definition: AggregatorQuery.h:61
ColumnStore
Definition: ColStoreDataGenerator.h:22
ColumnStore::CountAggregator::getValue
float getValue()
Definition: AggregatorQuery.h:122
ColumnStore::Aggregator::Aggregator
Aggregator(std::string name)
Definition: AggregatorQuery.h:32
ColumnStore::CountAggregator
Definition: AggregatorQuery.h:110
ColumnStore::AverageAggregator
Definition: AggregatorQuery.h:59
ColumnStore::MinAggregator::MinAggregator
MinAggregator(std::string name)
Definition: AggregatorQuery.h:152
ColumnStore::Aggregator::~Aggregator
virtual ~Aggregator()
Destroy the Condition Query object.
Definition: AggregatorQuery.h:46
ColumnStore::AverageAggregator::getValue
float getValue()
Definition: AggregatorQuery.h:81
ColumnStore::CountAggregator::count
int count
Definition: AggregatorQuery.h:111
ColumnStore::MaxAggregator
Definition: AggregatorQuery.h:125
ColumnStore::Metadata
std::shared_ptr< DataRecordMetadata > Metadata
Shared pointer to DataRecordMetadata.
Definition: DataRecord.h:208
ColumnStore::CountAggregator::CountAggregator
CountAggregator(std::string name)
Definition: AggregatorQuery.h:112
ColumnStore::AverageAggregator::addRecord
void addRecord(DataRecord &record)
Definition: AggregatorQuery.h:67
ColumnStore::MaxAggregator::addRecord
void addRecord(DataRecord &record)
Definition: AggregatorQuery.h:133
ColumnStore::AverageAggregator::count
int count
Definition: AggregatorQuery.h:60
ColumnStore::SumAggregator::getValue
float getValue()
Definition: AggregatorQuery.h:107
ColumnStore::DataRecord
Stores a row of data.
Definition: DataRecord.h:64
ColumnStore::MaxAggregator::aggregatorType
std::string aggregatorType()
Definition: AggregatorQuery.h:131
ColumnStore::AverageAggregator::aggregatorType
std::string aggregatorType()
Definition: AggregatorQuery.h:65
ColumnStore::Aggregator::initialize
void initialize(Metadata metadata)
Definition: AggregatorQuery.h:34
ColumnStore::SumAggregator::addRecord
void addRecord(DataRecord &record)
Definition: AggregatorQuery.h:94
ColumnStore::SumAggregator::clone
AggregatorQuery clone()
Definition: AggregatorQuery.h:103
ColumnStore::AverageAggregator::clone
AggregatorQuery clone()
Definition: AggregatorQuery.h:77
ColumnStore::SumAggregator::sum
double sum
Definition: AggregatorQuery.h:88
ColumnStore::MaxAggregator::mx
float mx
Definition: AggregatorQuery.h:126
ColumnStore::CountAggregator::addRecord
void addRecord(DataRecord &record)
Definition: AggregatorQuery.h:116
ColumnStore::MaxAggregator::MaxAggregator
MaxAggregator(std::string name)
Definition: AggregatorQuery.h:128
ColumnStore::MinAggregator
Definition: AggregatorQuery.h:149
ColumnStore::DataType::INT
@ INT
ColumnStore::SumAggregator::aggregatorType
std::string aggregatorType()
Definition: AggregatorQuery.h:92
ColumnStore::CountAggregator::clone
AggregatorQuery clone()
Definition: AggregatorQuery.h:118
DataGeneratorInterface.h
Data Generator Interface.
ColumnStore::Aggregator::aggregatorType
virtual std::string aggregatorType()=0
ColumnStore::MaxAggregator::getValue
float getValue()
Definition: AggregatorQuery.h:142
ColumnStore::MinAggregator::addRecord
void addRecord(DataRecord &record)
Definition: AggregatorQuery.h:157
ColumnStore::Aggregator::getColumnName
std::string getColumnName()
Definition: AggregatorQuery.h:48
ColumnStore::Aggregator::getValue
virtual float getValue()=0
ColumnStore::Aggregator
Base Condition Query Structure.
Definition: AggregatorQuery.h:26
ColumnStore::MinAggregator::mn
float mn
Definition: AggregatorQuery.h:150
ColumnStore::AverageAggregator::AverageAggregator
AverageAggregator(std::string name)
Definition: AggregatorQuery.h:63
ColumnStore::Aggregator::type
DataType type
Definition: AggregatorQuery.h:30
ColumnStore::Aggregator::addRecord
virtual void addRecord(DataRecord &record)=0
ColumnStore::Aggregator::columnIndex
int columnIndex
Definition: AggregatorQuery.h:29
ColumnStore::SumAggregator
Definition: AggregatorQuery.h:87
ColumnStore::SumAggregator::SumAggregator
SumAggregator(std::string name)
Definition: AggregatorQuery.h:90
ColumnStore::Aggregator::clone
virtual std::shared_ptr< Aggregator > clone()=0
ColumnStore::DataType::FLOAT
@ FLOAT
ColumnStore::MinAggregator::clone
AggregatorQuery clone()
Definition: AggregatorQuery.h:168
ColumnStore::MinAggregator::aggregatorType
std::string aggregatorType()
Definition: AggregatorQuery.h:155
ColumnStore::MinAggregator::getValue
float getValue()
Definition: AggregatorQuery.h:166