-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryPlanDriver.h
206 lines (179 loc) · 6.49 KB
/
QueryPlanDriver.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
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
#ifndef QUERY_PLAN_DRIVER_H_
#define QUERY_PLAN_DRIVER_H_
#include <iostream>
#include <string>
#include <vector>
#include "DBFile.h"
#include "Schema.h"
#include "Function.h"
#include "ParseTree.h"
#include "Statistics.h"
#include "Comparison.h"
#define MAX_RELS 12
#define MAX_RELNAME 50
#define MAX_ATTS 100
class QueryNode;
class QueryPlan {
public:
QueryPlan(Statistics* st);
~QueryPlan() { if (root) delete root; }
void plan();
void print(std::ostream& os = std::cout) const;
void setOutput(char* out);
void execute();
private:
void makeLeafs();
void makeJoins();
void orderJoins();
void makeSums();
void makeProjects();
void makeDistinct();
void makeWrite();
int evalOrder(std::vector<QueryNode*> operands, Statistics st, int bestFound); // intentional copy
QueryNode* root;
std::vector<QueryNode*> nodes;
std::string outName;
FILE* outFile;
Statistics* stat;
AndList* used; // reconstruct AndList so that it can be used next time
// should be assigned to boolean after each round
void recycleList(AndList* alist) { concatList(used, alist); }
static void concatList(AndList*& left, AndList*& right);
QueryPlan(const QueryPlan&);
QueryPlan& operator=(const QueryPlan&);
};
class Pipe; class RelationalOp;
class QueryNode {
friend class QueryPlan;
friend class UnaryNode;
friend class BinaryNode; // passed as argument to binary node
friend class ProjectNode;
friend class DedupNode;
friend class JoinNode;
friend class SumNode;
friend class GroupByNode;
friend class WriteNode;
public:
virtual ~QueryNode();
protected:
QueryNode(const std::string& op, Schema* out, Statistics* st);
QueryNode(const std::string& op, Schema* out, char* rName, Statistics* st);
QueryNode(const std::string& op, Schema* out, char* rNames[], size_t num, Statistics* st);
virtual void print(std::ostream& os = std::cout, size_t level = 0) const;
virtual void printOperator(std::ostream& os = std::cout, size_t level = 0) const;
virtual void printSchema(std::ostream& os = std::cout, size_t level = 0) const;
virtual void printAnnot(std::ostream& os = std::cout, size_t level = 0) const = 0; // operator specific
virtual void printPipe(std::ostream& os, size_t level = 0) const = 0;
virtual void printChildren(std::ostream& os, size_t level = 0) const = 0;
virtual void execute(Pipe** pipes, RelationalOp** relops) = 0;
static AndList* pushSelection(AndList*& alist, Schema* target);
static bool containedIn(OrList* ors, Schema* target);
static bool containedIn(ComparisonOp* cmp, Schema* target);
std::string opName;
Schema* outSchema;
char* relNames[MAX_RELS];
size_t numRels;
int estimate, cost; // estimated number of tuples and total cost
Statistics* stat;
int pout; // output pipe
static int pipeId;
};
class LeafNode: private QueryNode { // read from file
friend class QueryPlan;
LeafNode (AndList*& boolean, AndList*& pushed,
char* relName, char* alias, Statistics* st);
~LeafNode() { if (opened) dbf.Close(); }
void printOperator(std::ostream& os = std::cout, size_t level = 0) const;
void printAnnot(std::ostream& os = std::cout, size_t level = 0) const;
void printPipe(std::ostream& os, size_t level) const;
void printChildren(std::ostream& os, size_t level) const {}
void printSchema(std::ostream& os, size_t level = 0) const {
os << "Output schema:" << endl;
outSchema->PrintToStream(os,1);
}
void execute(Pipe** pipes, RelationalOp** relops);
DBFile dbf;
bool opened;
CNF selOp;
Record literal;
};
class UnaryNode: protected QueryNode {
friend class QueryPlan;
protected:
UnaryNode(const std::string& opName, Schema* out, QueryNode* c, Statistics* st);
virtual ~UnaryNode() { delete child; }
void printPipe(std::ostream& os, size_t level) const;
void printChildren(std::ostream& os, size_t level) const { child->print(os, level+1); }
QueryNode* child;
int pin; // input pipe
};
class BinaryNode: protected QueryNode { // not including set operations.
friend class QueryPlan;
protected:
BinaryNode(const std::string& opName, QueryNode* l, QueryNode* r, Statistics* st);
virtual ~BinaryNode() { delete left; delete right; }
void printPipe(std::ostream& os, size_t level) const;
void printChildren(std::ostream& os, size_t level) const
{ left->print(os, level+1); right->print(os, level+1); }
void printSchema(std::ostream& os, size_t level = 0) const
{ QueryNode::printSchema(os, level);}
QueryNode* left;
QueryNode* right;
int pleft, pright; // input pipes
};
class ProjectNode: private UnaryNode {
friend class QueryPlan;
ProjectNode(NameList* atts, QueryNode* c);
void printAnnot(std::ostream& os = std::cout, size_t level = 0) const;
void execute(Pipe** pipes, RelationalOp** relops);
int keepMe[MAX_ATTS];
int numAttsIn, numAttsOut;
};
class DedupNode: private UnaryNode {
friend class QueryPlan;
DedupNode(QueryNode* c);
void printAnnot(std::ostream& os = std::cout, size_t level = 0) const {}
void execute(Pipe** pipes, RelationalOp** relops);
OrderMaker dedupOrder;
};
class SumNode: private UnaryNode {
friend class QueryPlan;
SumNode(FuncOperator* parseTree, QueryNode* c);
Schema* resultSchema(FuncOperator* parseTree, QueryNode* c);
void printAnnot(std::ostream& os = std::cout, size_t level = 0) const;
void execute(Pipe** pipes, RelationalOp** relops);
Function f;
Schema *inputSchema;
};
class GroupByNode: private UnaryNode {
friend class QueryPlan;
GroupByNode(NameList* gAtts, FuncOperator* parseTree, QueryNode* c);
Schema* resultSchema(NameList* gAtts, FuncOperator* parseTree, QueryNode* c);
void printAnnot(std::ostream& os = std::cout, size_t level = 0) const;
void execute(Pipe** pipes, RelationalOp** relops);
OrderMaker grpOrder;
Function f;
Schema *inputSchema;
};
class JoinNode: private BinaryNode {
friend class QueryPlan;
JoinNode(AndList*& boolean, AndList*& pushed, QueryNode* l, QueryNode* r, Statistics* st);
void printAnnot(std::ostream& os = std::cout, size_t level = 0) const;
void printSchema(std::ostream& os, size_t level = 0) const
{
BinaryNode::printSchema(os, level);
os<<"CNF :" <<endl;
selOp.PrintToStream(os, outSchema,literal);
}
void execute(Pipe** pipes, RelationalOp** relops);
CNF selOp;
Record literal;
};
class WriteNode: private UnaryNode {
friend class QueryPlan;
WriteNode(FILE*& out, QueryNode* c);
void printAnnot(std::ostream& os = std::cout, size_t level = 0) const;
void execute(Pipe** pipes, RelationalOp** relops);
FILE*& outFile;
};
#endif