forked from gfk-ba/datateam-test-1
/
analyze.cpp
203 lines (138 loc) · 4.97 KB
/
analyze.cpp
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
#include "./analyze.h"
#include "./tokenize.h"
#include <fstream>
#include <iostream>
#include <math.h>
#include "./types.h"
typedef std::ofstream usedStreamType;
// implementation of AVG
// col1 is allways the grouping column
void doAvg(Indexed* indexed, int col1, int col2, int colType1, usedStreamType& output){
const PairIndex* index = indexed->getIndex(col1, col2);
if (!index){
std::cout << "index missing! " << col1 << " " << col2 << "\n";
return;
};
PairIndex::const_iterator it = index->begin();
while (it != index->end()){
double value = 0;
double accValue = 0;
double cnt = 0;
// groyp by name
int name = it->first;
PairIndexValue* piv = it->second;
//loop over all entries in the 2nd column
PairIndexValue::const_iterator ivIt = piv->begin();
while (ivIt != piv->end()){
accValue += (ivIt->first * ivIt->second);
cnt += ivIt->second;
ivIt++;
};
// this could trigger some problems when we have millions of records
value = round(accValue / cnt);
std::cout << typedValue(name, colType1) << ": " << value << "\n";
output << typedValue(name, colType1) << ": " << value << "\n";
it++;
};
};
// implementation of MIN
void doMin(Indexed* indexed, int col1, int col2, int colType1, usedStreamType& output){
const PairIndex* index = indexed->getIndex(col1, col2);
if (!index){
std::cout << "index missing! " << col1 << " " << col2 << "\n";
return;
};
PairIndex::const_iterator it = index->begin();
while (it != index->end()){
int name = it->first;
PairIndexValue* piv = it->second;
//get the lowest with begin
PairIndexValue::const_iterator ivIt = piv->begin();
std::cout << typedValue(name, colType1) << ": " << ivIt->first << "\n";
output << typedValue(name, colType1) << ": " << ivIt->first << "\n";
it++;
};
};
// implementation of MAX
void doMax(Indexed* indexed, int col1, int col2, int colType1, usedStreamType& output){
const PairIndex* index = indexed->getIndex(col1, col2);
if (!index){
std::cout << "index missing! " << col1 << " " << col2 << "\n";
return;
};
PairIndex::const_iterator it = index->begin();
while (it != index->end()){
int name = it->first;
PairIndexValue* piv = it->second;
//get the highest with rbegin
PairIndexValue::const_reverse_iterator ivIt = piv->rbegin();
std::cout << typedValue(name, colType1) << ": " << ivIt->first << "\n";
output << typedValue(name, colType1) << ": " << ivIt->first << "\n";
it++;
};
};
// read a file with queries
// parse the queries
// and output to file as well as stdout
void analyze(Indexed* indexed, std::string queryFilename, std::string outputFilename){
std::string line;
std::ifstream queryfile(queryFilename.c_str());
std::ofstream outputfile(outputFilename.c_str());
if (!outputfile.is_open()){
std::cout << "could not open output file\n";
return;
};
const Rows* rows = indexed->rows;
if (queryfile.is_open()){
// read all the lines
while ( getline (queryfile, line) ) {
Tokens t = tokenize(line);
bool valid = true;
if (t.size() < 3){
valid = false;
};
int colNum1 = 0;
int colType1 = 0;
int colNum2 = 0;
if (valid){
ColsByName::const_iterator colIt = rows->header.find(t[1]);
if (colIt == rows->header.end()){
std::cout << "column: " << t[1] << " not found!\n";
valid = false;
}else{
colNum2 = colIt->second.pos;
if (!typeCanAggregate(colIt->second.type)){
std::cout << "can not perform: " << t[0] << " over " << t[1] << " of type " << typeName(colIt->second.type) << "\n";
valid = false;
};
};
colIt = rows->header.find(t[2]);
if (colIt == rows->header.end()){
std::cout << "column: " << t[2] << " not found!\n";
valid = false;
}else{
colNum1 = colIt->second.pos;
colType1 = colIt->second.type;
};
};
if (valid){
// output to file and std out
std::cout << t[0] << " " << t[1] << " GROUPED BY " << t[2] << "\n";
outputfile << t[0] << " " << t[1] << " GROUPED BY " << t[2] << "\n";
if (t[0] == "AVG"){
doAvg(indexed, colNum1, colNum2, colType1, outputfile);
}else if (t[0] == "MIN"){
doMin(indexed, colNum1, colNum2, colType1, outputfile);
}else if (t[0] == "MAX"){
doMax(indexed, colNum1, colNum2, colType1, outputfile);
}else{
std::cout << "unknown aggregate function: " << t[0] << "\n";
};
// new line
std::cout << "\n";
outputfile << "\n";
};
};
};
outputfile.close();
};