-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistics.cc
379 lines (303 loc) · 11.6 KB
/
Statistics.cc
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "Statistics.h"
// #include "QueryPlanUtils.h"
// using namespace std;
void RelStats::UpdateNumTuples(int num_tuples) {
this->num_tuples += num_tuples;
}
RelStats::RelStats() {
this->num_tuples = 0;
}
void RelStats::AddAtt(char *attName, int numDistincts) {
string att(attName);
if (numDistincts == -1) {
numDistincts = this->num_tuples;
}
if (umap.find(att) == umap.end()) {
umap.insert({att, numDistincts});
} else {
int temp = umap[att];
temp += numDistincts;
umap[att] = temp;
}
}
void RelStats::Copy(RelStats &toMe, char *newName) {
toMe.num_tuples = this->num_tuples;
for (pair<string, int> element : umap) {
string temp = newName != NULL ? string(newName) + "." + element.first : element.first;
toMe.umap.insert({temp, element.second});
}
}
void RelStats::Write(FILE *file) {
for (pair<string, int> element : umap) {
fprintf(file, "%s %d\n", element.first.c_str(), element.second);
}
}
Statistics::Statistics() {
}
Statistics::Statistics(Statistics ©Me) {
for (pair<string, RelStats *> element : copyMe.umap) {
RelStats *relstat = new RelStats();
element.second->Copy(*relstat, NULL);
this->umap.insert({element.first, relstat});
}
}
Statistics::~Statistics() {
//empty
for (pair<string, RelStats *> element : this->umap) {
delete element.second;
}
umap.clear();
}
void Statistics::AddRel(char *relName, int numTuples) {
string rel = string(relName);
if (umap.find(rel) == umap.end()) {
RelStats *temp = new RelStats();
temp->UpdateNumTuples(numTuples);
umap.insert({rel, temp});
} else {
RelStats *relstats = umap[rel];
relstats->UpdateNumTuples(numTuples);
}
}
void Statistics::AddAtt(char *relName, char *attName, int numDistincts) {
string rel(relName);
RelStats *temp = umap[rel];
temp->AddAtt(attName, numDistincts);
//update distinct_lookup
distinct_lookup.insert({string(attName), numDistincts});
//update numtuples_lookup
numtuples_lookup.insert({string(attName), umap[rel]->num_tuples});
// update the reverse_lookup
reverse_lookup.insert({string(attName), rel});
}
void Statistics::CopyRel(char *oldName, char *newName) {
RelStats *old = umap[string(oldName)];
RelStats *newRel = new RelStats();
old->Copy(*newRel, newName);
umap.insert({string(newName), newRel});
// update all lookups
for (pair<string, int> element : newRel->umap) {
distinct_lookup.insert(element);
numtuples_lookup.insert({element.first, newRel->num_tuples});
reverse_lookup.insert({element.first, string(newName)});
}
}
void Statistics::Read(char *fromWhere) {
FILE *stat_file = fopen(fromWhere, "r");
char space[200];
while (fscanf(stat_file, "%s", space) != EOF) {
if (strcmp(space, "BEGIN")) {
cout << "Error! Not a stat file!" << endl;
exit(1);
}
fscanf(stat_file, "%s", space);
string relName(space);
int *numtuples = new int;
fscanf(stat_file, "%d", numtuples);
RelStats *relStats = new RelStats();
relStats->num_tuples = *numtuples;
fscanf(stat_file, "%s", space);
while (strcmp(space, "END")) {
int *numDistinct = new int;
fscanf(stat_file, "%d", numDistinct);
relStats->AddAtt(space, *numDistinct);
fscanf(stat_file, "%s", space);
}
umap.insert({relName, relStats});
for (pair<string, int> element : relStats->umap) {
distinct_lookup.insert(element);
numtuples_lookup.insert({element.first, relStats->num_tuples});
reverse_lookup.insert({element.first, relName});
}
}
}
void Statistics::Write(char *toWhere) {
FILE *stat_file = fopen(toWhere, "w");
for (pair<string, RelStats *> element : umap) {
fprintf(stat_file, "%s\n", "BEGIN");
fprintf(stat_file, "%s %g\n", element.first.c_str(), element.second->num_tuples);
element.second->Write(stat_file);
fprintf(stat_file, "%s\n\n", "END");
}
fclose(stat_file);
}
void Statistics::JoinRels(string relNames[], double join_result) {
RelStats *left_rel = umap[string(relNames[0])];
RelStats *right_rel = umap[string(relNames[1])];
left_rel->num_tuples = join_result;
string newRelName = string(relNames[0]) + "_" + string(relNames[1]);
for (pair<string, int> element : right_rel->umap) {
left_rel->umap.insert(element);
}
for (pair<string, int> element : left_rel->umap) {
auto it1 = reverse_lookup.find(element.first);
it1->second = newRelName;
auto it2 = numtuples_lookup.find(element.first);
it2->second = join_result;
}
auto it = umap.find(string(relNames[0]));
swap(umap[newRelName], it->second);
umap.erase(it);
umap.erase(string(relNames[1]));
}
void Statistics::Apply(struct AndList *parseTree, char *relNames[], int numToJoin) {
bool isJoin = false;
double factor = 1;
double result = 1;
int count = 0;
while (parseTree != NULL) {
struct OrList *left = parseTree->left;
struct ComparisonOp *cop = left->left;
struct Operand *right_cop = cop->right;
string right_key = string(right_cop->value);
struct Operand *left_cop = cop->left;
string left_key = string(left_cop->value);
double left_tuple = numtuples_lookup[left_key];
result = left_tuple;
factor = 1;
// current relation on which selection is done
string sel_relname;
while (left != NULL) {
// left
cop = left->left;
left_cop = cop->left;
left_key = string(left_cop->value);
double left_distinct = distinct_lookup[left_key];
// right
right_cop = cop->right;
right_key = string(right_cop->value);
if (right_cop->code == NAME) {
isJoin = true;
double right_tuple = numtuples_lookup[right_key];
double right_distinct = distinct_lookup[right_key];
double join_result = left_tuple * right_tuple / max(left_distinct, right_distinct);
string rel_names[] = {reverse_lookup[left_key], reverse_lookup[right_key]};
JoinRels(rel_names, join_result);
} else {
// it is a selection
isJoin = false;
sel_relname = reverse_lookup[left_key];
if (cop->code == EQUALS) {
// means EQUALS operator
factor *= (1 - ((double)1 / left_distinct));
} else {
// means <, > operators
factor *= ((double)2 / 3);
}
}
left = left->rightOr;
//counting for every disjunction (AND)
count++;
}
parseTree = parseTree->rightAnd;
if (!isJoin) {
result = result * (1 - factor);
RelStats *temp = umap[sel_relname];
temp->num_tuples = result;
for (pair<string, int> element : umap[sel_relname]->umap) {
numtuples_lookup[element.first] = result;
}
}
}
}
double Statistics::Estimate(struct AndList *parseTree, char **relNames, int numToJoin) {
unordered_map<string, RelStats *> temp_umap;
unordered_map<string, int> temp_distinct_lookup;
unordered_map<string, int> *temp_numtuples_lookup = new unordered_map<string, int>;
unordered_map<string, string> temp_reverse_lookup;
// // printing the ANDLIST DEBUG
// printAndList(parseTree);
for (pair<string, RelStats *> element : umap) {
RelStats *temprel = new RelStats();
element.second->Copy(*temprel, NULL);
temp_umap.insert({element.first, temprel});
}
for (pair<string, int> element : distinct_lookup) {
temp_distinct_lookup.insert(element);
}
for (pair<string, int> element : numtuples_lookup) {
temp_numtuples_lookup->insert(element);
}
for (pair<string, string> element : reverse_lookup) {
temp_reverse_lookup.insert(element);
}
bool isJoin = false;
double factor = 1;
double result = 1;
double join_result = 0;
int count = 0;
while (parseTree != NULL) {
struct OrList *left = parseTree->left;
struct ComparisonOp *cop = left->left;
struct Operand *right_cop = cop->right;
string right_key = string(right_cop->value);
struct Operand *left_cop = cop->left;
string left_key = string(left_cop->value);
double left_tuple = temp_numtuples_lookup->at(left_key);
result = left_tuple;
factor = 1;
// current relation on which selection is done
string sel_relname;
while (left != NULL) {
// left
cop = left->left;
left_cop = cop->left;
left_key = string(left_cop->value);
double left_distinct = temp_distinct_lookup[left_key];
// right
right_cop = cop->right;
right_key = string(right_cop->value);
if (right_cop->code == NAME) {
isJoin = true;
double right_tuple = temp_numtuples_lookup->at(right_key);
double right_distinct = temp_distinct_lookup[right_key];
join_result = left_tuple * right_tuple / max(left_distinct, right_distinct);
string rel_names[] = {temp_reverse_lookup[left_key], temp_reverse_lookup[right_key]};
RelStats *left_rel = temp_umap[string(rel_names[0])];
RelStats *right_rel = temp_umap[string(rel_names[1])];
left_rel->num_tuples = join_result;
string newRelName = string(rel_names[0]) + "_" + string(rel_names[1]);
for (pair<string, int> element : right_rel->umap) {
left_rel->umap.insert(element);
}
for (pair<string, int> element : left_rel->umap) {
auto it1 = temp_reverse_lookup.find(element.first);
it1->second = newRelName;
auto it2 = temp_numtuples_lookup->find(element.first);
it2->second = join_result;
}
auto it = temp_umap.find(string(rel_names[0]));
swap(temp_umap[newRelName], it->second);
temp_umap.erase(it);
temp_umap.erase(string(rel_names[1]));
} else {
// it is a selection
isJoin = false;
sel_relname = temp_reverse_lookup[left_key];
if (cop->code == EQUALS) {
// means EQUALS operator
factor *= (1 - ((double)1 / left_distinct));
} else {
// means <, > operators
factor *= ((double)2 / 3);
}
}
left = left->rightOr;
//counting for every disjunction (AND)
count++;
}
parseTree = parseTree->rightAnd;
if (!isJoin) {
result = result * (1 - factor);
RelStats *temp = temp_umap[sel_relname];
temp->num_tuples = result;
for (pair<string, int> element : temp_umap[sel_relname]->umap) {
temp_numtuples_lookup->at(element.first) = result;
}
}
}
temp_distinct_lookup.clear();
temp_numtuples_lookup->clear();
temp_reverse_lookup.clear();
return isJoin ? join_result : result;
}