-
Notifications
You must be signed in to change notification settings - Fork 5
/
Statistics.h
163 lines (149 loc) · 5.14 KB
/
Statistics.h
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
#ifndef STATISTICS_
#define STATISTICS_
#include "ParseTree.h"
#include <map>
#include <tr1/unordered_map>
#include <set>
#include <tr1/unordered_set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
class RelationInformation
{
typedef unsigned long long tupleCount;
tupleCount numberOfTuples;
std::map<std::string, tupleCount> attributeInformation;
// Schema sch;
std::string originalName;
public:
RelationInformation () : numberOfTuples(0), attributeInformation(), originalName() {}
explicit RelationInformation (tupleCount tuples, std::string origNme) : numberOfTuples(tuples), attributeInformation(), /* sch(), */ originalName(origNme)
{/* char * cat = "catalog"; sch = Schema(cat, origNme.c_str()); */}
std::map<std::string, tupleCount> const GetAtts()
{
return attributeInformation;
}
void CopyAtts (RelationInformation const & otherRel)
{
attributeInformation.insert(otherRel.attributeInformation.begin(), otherRel.attributeInformation.end());
}
tupleCount GetDistinct (std::string attr)
{
return attributeInformation[attr];
}
void AddAtt (std::string attr, tupleCount numDistinct)
{
attributeInformation[attr] = numDistinct;
}
tupleCount NumTuples() {return numberOfTuples;}
void print()
{
std::clog << numberOfTuples << std::endl;
for (std::map<std::string, tupleCount>::iterator it=attributeInformation.begin() ; it != attributeInformation.end(); it++ )
{std::clog << (*it).first << " => " << (*it).second << std::endl;}
}
friend std::ostream& operator<<(std::ostream& os, const RelationInformation & RI)
{
using std::endl;
os << RI.numberOfTuples << endl;
std::map<std::string, tupleCount>::const_iterator it;
os << RI.attributeInformation.size() << endl;
for (it = RI.attributeInformation.begin(); it != RI.attributeInformation.end(); it++ )
{
os << (*it).first << endl << (*it).second << endl;
}
return os;
}
friend std::istream& operator>>(std::istream& is, RelationInformation & RI)
{
using std::clog; using std::endl;
clog << "reading relation" << endl;
tupleCount numTups;
is >> numTups;
clog << "there are " << numTups << " tuples in this relation." << endl;
RI.numberOfTuples = numTups;
tupleCount numMappings;
is >> numMappings;
clog << "there are " << numMappings << " attributes in this relation." << endl;
for (unsigned i = 0; i < numMappings; i++)
{
std::string attr;
tupleCount distinct;
is >> attr;
is >> distinct;
RI.attributeInformation[attr] = distinct;
}
return is;
}
};
class Statistics
{
private:
typedef long long unsigned int tupleCount;
std::map < std::string, RelationInformation > rels;
std::map < std::string, std::string> extantAttrs; // if the attr exists, and what relation it is in. <k,v> is <attr, relation-attr-is-in>
std::map < std::string, std::string> mergedRelations; // <k,v> is <original-relation-name, merged-relation-name>
void Check (struct AndList *parseTree, char *relNames[], int numToJoin);
void CheckRelations(char *relNames[], int numToJoin);
std::vector<std::string> CheckParseTree(struct AndList *pAnd);
double CalculateEstimate(struct AndList *pAnd);
bool HasJoin(AndList *pAnd);
public:
Statistics();
Statistics(Statistics ©Me); // Performs deep copy
~Statistics();
void AddRel(char *relName, int numTuples);
void AddAtt(char *relName, char *attName, int numDistincts);
void CopyRel(char *oldName, char *newName);
void Read(char *fromWhere);
void Write(char *fromWhere);
void Apply(struct AndList *parseTree, char *relNames[], int numToJoin);
double Estimate(struct AndList *parseTree, char **relNames, int numToJoin);
std::string getAttrHomeTable(std::string a)
{
using std::clog;
using std::endl;
using std::string;
clog << "called get attr home table" << endl;
// std::string a(attr);
clog << a << a.size() << endl;
clog << "found " << extantAttrs.count(a);
if (1 == extantAttrs.count(a))
{
clog << "WTF" << endl;
clog << extantAttrs[a] << endl;
string tbl(extantAttrs[a]);
clog << tbl << endl;
return tbl;
}
return "";
}
void print()
{
using std::clog; using std::endl;
{
std::map < std::string, RelationInformation >::iterator it;
for (it = rels.begin(); it != rels.end(); it++ )
{
clog << (*it).first << " relation has information " << endl << (*it).second << endl;
}
}
{
std::map < std::string, std::string>::iterator it;
for (it = extantAttrs.begin(); it != extantAttrs.end(); it++ )
{
clog << (*it).first << " is in relation " << (*it).second << endl;
}
}
{ // merged relations
clog << "Merged relations are " << endl;
std::map < std::string, std::string>::iterator it;
for (it = mergedRelations.begin(); it != mergedRelations.end(); it++ )
{
clog << (*it).first << " is in relation " << (*it).second << endl;
}
}
}
};
#endif