-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateQueries.cpp
321 lines (277 loc) · 9.98 KB
/
GenerateQueries.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
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
#ifndef GENERATE_QUERIES_H_
#define GENERATE_QUERIES_H_
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Number of optimized queries (ugly and hardcoded since the queries are hardcoded - sadface )
const int numOptimized = 15;
const char * getOperator(int index) {
switch(index) {
case 0:
return "<";
case 1:
return ">";
case 3: // Skip 2 so that != is default. EQ will only be returned if index is >= 3
return "=";
default:
return "!=";
}
}
// ----------------------------------------------------------
// Queries Optimized for Partition Scheme
// ----------------------------------------------------------
// Template: sscan(T1:t; t.a, t.b, t.n, t.o; )
void query0(std::stringstream & text) {
text << "sscan(T1:t; t.a, t.b, t.n, t.o; )" << std::endl;
}
// Template: sscan(T1:t; t.a, t.b, t.n, t.o; t.a < 500 | t.a > 4800 & t.a < 5300 | t.a > 97000)
void query1(std::stringstream & text) {
int number1 = rand() % 500 + 1;
int number2 = 2000 + (rand() % 3000 + 1);
int number3 = number2 + (rand() % 1000 + 1);
int number4 = 70000 + (rand() % 20000 + 1);
text << "sscan(T1:t; t.a, t.b, t.n, t.o; t.a < " << number1
<< " | t.a > " << number2 << " & t.a < " << number3 << " | t.a > " << number4 << ")" << std::endl;
}
// Template: sscan(T1:t; t.l, t.m, t.q; t.l < t.m)
void query2(std::stringstream & text) {
int random = rand() % 3;
const char * op = getOperator(random);
text << "sscan(T1:t; t.l, t.m, t.q; t.l " << op << " t.m)" << std::endl;
}
// Template: sscan(T2:t; t.b, t.l, t.m; t.b < 500 | t.b > 3300 & t.b < 4573 | t.b > 98000)
void query3(std::stringstream & text) {
const char * op1 = getOperator(rand() % 4);
const char * op2 = getOperator(rand() % 4);
int number1 = rand() % 500 + 1;
int number2 = 80000 + (rand() % 20000 + 1);
text << "sscan(T2:t; t.b, t.l, t.m; t.b " << op1 << " " << number1 << " | t.b > "
<< "3300 & t.b < 4573 | t.b " << op2 << " " << number2 << ")" << std::endl;
}
// Template: sscan(T2:t; t.b, t.m, t.l; t.l > t.m)
void query4(std::stringstream & text) {
int random = rand() % 4;
const char * op = getOperator(random);
text << "sscan(T2:t; t.b, t.m, t.l; t.l " << op << " t.m)" << std::endl;
}
// Template: sscan(T3:t; t.b, t.m, t.q; t.m = ‘A’ | t.m = ‘E’ | t.m = ‘I’ | t.m = ‘O’ | t.m = ‘U’)
void query5(std::stringstream & text) {
char char1 = (char)(rand() % 26 + 65);
char char2 = (char)(rand() % 26 + 65);
char char3 = (char)(rand() % 26 + 65);
char char4 = (char)(rand() % 26 + 65);
char char5 = (char)(rand() % 26 + 65);
text << "sscan(T3:t; t.b, t.m, t.q; t.m = '" << char1 << "' | t.m = '" << char2
<< "' | t.m = '" << char3 << "' | t.m = '" << char4 << "' | t.m = '" << char5 << "')" << std::endl;
}
// Template: sscan(T3:t; t.a, t.h; t.a < 3800 | t.a > 50000 & t.h = 'H')
void query6(std::stringstream & text) {
int number1 = rand() % 4000 + 1;
int number2 = 40000 + (rand() % 1000 + 1);
char char1 = (char)(rand() % 26 + 65);
text << "sscan(T3:t; t.a, t.h; t.a < " << number1 << " | t.a > " << number2 << " & t.h = '" << char1 << "')" << std::endl;
}
// Template: sscan(T4:t; t.a, t.b, t.c, t.l, t.m, t.p, t.n, t.o; )
void query7(std::stringstream & text) {
text << "sscan(T4:t; t.a, t.b, t.c, t.l, t.m, t.p, t.n, t.o; )" << std::endl;
}
// Template: sscan(T4:t; t.a, t.f, t.g, t.i, t.m, t.n, t.n, t.o, t.q, t.r; t.m < t.n)
void query8(std::stringstream & text) {
const char * op1 = getOperator(rand() % 3);
text << "sscan(T4:t; t.a, t.f, t.g, t.i, t.m, t.n, t.n, t.o, t.q; t.m " << op1 << " t.n)" << std::endl;
}
// Template: sscan(T5:t; t.l, t.m, t.n, t.q; )
void query9(std::stringstream & text) {
text << "sscan(T5:t; t.l, t.m, t.n, t.q; )" << std::endl;
}
// Template: sscan(T5:t; t.l, t.m, t.n, t.q; t.m > t.n & t.l = 'Z')
void query10(std::stringstream & text) {
char char1 = (char)(rand() % 26 + 65);
const char * op1 = getOperator(rand() % 3);
text << "sscan(T5:t; t.l, t.m, t.n, t.q; t.m " << op1 << " t.n & t.l = '" << char1 << "')" << std::endl;
}
// Template: merge-join(sscan(T1:R; R.a, R.b, T.n, R.o; ), sscan(T3:S; S.a, S.h, S.r; S.h = 'A' | S.h = 'B'); R.a = S.a; )
void query11(std::stringstream & text) {
char char1 = (char)(rand() % 26 + 65);
char char2 = (char)(rand() % 26 + 65);
text << "merge-join(sscan(T1:R; R.a, R.b, T.n, R.o; ), sscan(T3:S; S.a, S.h, S.r; S.h = '"
<< char1 << "' | S.h = '" << char2 <<"'); R.a = S.a; )" << std::endl;
}
// Template: merge-join(sscan(T1:R; R.a, R.b, T.n, R.o; R.a < 5500), sscan(T3:S; S.a, S.h, S.r; S.h = 'A' | S.h = 'B'); R.a = S.a; )
void query12(std::stringstream & text) {
int number = rand() % 10000 + 1;
char char1 = (char)(rand() % 26 + 65);
char char2 = (char)(rand() % 26 + 65);
text << "merge-join(sscan(T1:R; R.a, R.b, T.n, R.o; R.a < " << number
<< "), sscan(T3:S; S.a, S.h, S.r; S.h = '" << char1 << "' | S.h = '" << char2 << "'); R.a = S.a; )" << std::endl;
}
// Template: merge-join(sscan(T3:R; R.a, R.r; ), sscan(T5:S; S.f, S.e, S.p; S.e < 5); R.a = S.f; )
void query13(std::stringstream & text)
{
const char * op = getOperator(rand() % 4);
int number = rand() % 10 + 1;
text << "merge-join(sscan(T3:R; R.a, R.r; ), sscan(T5:S; S.f, S.e, S.p; S.e " << op << " " << number << "); R.a = S.f; )" << std::endl;
}
// Template: merge-join(merge-join(sscan(T1:S; S.a, S.b, S.o; ), sscan(T3:R; R.b, R.m, R.q; R.b >= 4000 & R.m < 'F'); R.b = S.a;), sscan(T5:T; T.d, T.f; ); T.f = R.a; )
void query14(std::stringstream & text)
{
int number = rand() % 10000 + 1;
char char1 = (char)(rand() % 26 + 65);
const char * op = getOperator(rand() % 4);
text << "merge-join(merge-join(sscan(T1:S; S.a, S.b, S.o; ), "
<< "sscan(T3:R; R.b, R.m, R.q; R.b >= " << number << " & R.m " << op << " '" << char1 << "'); R.b = S.a;),"
<< " sscan(T5:T; T.d, T.f; ); T.f = R.a; )" << std::endl;
}
// ----------------------------------------------------------
// Queries Not Optimized for Partition Scheme
// ----------------------------------------------------------
// Template: sscan(T1:t; t.a, t.n, t.c, t.e, t.m, t.q; )
void query15(std::stringstream & text) {
text << "sscan(T1:t; t.a, t.n, t.c, t.e, t.m, t.q; )" << std::endl;
}
// Template: sscan(T3:t; t.a, t.h, t.f, t.c, t.q; T3.m = 'B')
void query16(std::stringstream & text) {
const char * op = getOperator(rand() % 4);
char char1 = (char)(rand() % 26 + 65);
text << "sscan(T3:t; t.a, t.h, t.f, t.c, t.q, t.m; t.m " << op << " '" << char1 << "')" << std::endl;
}
void query17(std::stringstream & text) {
const char * op = getOperator(rand() % 4);
int number = rand() % 10 + 1;
text << "merge-join(sscan(T5:R; R.a, R.o, R.f; ), sscan(T3:S; S.a, S.b, S.c, S.f, S.i, S.q, S.e; S.e "
<< op << " " << number << "); R.a = S.f; )" << std::endl;
}
void query18(std::stringstream & text) {
const char * op1 = getOperator(rand() % 4);
const char * op2 = getOperator(rand() % 4);
char char1 = (char)(rand() % 26 + 65);
int number = rand() % 10 + 1;
text << "merge-join(sscan(T4:R; R.a, R.o, R.f, R.l; R.l " << op1 << " '" << char1 << "'), sscan(T5:S; S.a, S.b, S.c, S.f, S.i, S.q, S.e; S.e "
<< op2 << " " << number << "); R.a = S.f; )" << std::endl;
}
void genQuery(std::stringstream & text, int query) {
switch(query) {
case 0:
query0(text);
break;
case 1:
query1(text);
break;
case 2:
query2(text);
break;
case 3:
query3(text);
break;
case 4:
query4(text);
break;
case 5:
query5(text);
break;
case 6:
query6(text);
break;
case 7:
query7(text);
break;
case 8:
query8(text);
break;
case 9:
query9(text);
break;
case 10:
query10(text);
break;
case 11:
query11(text);
break;
case 12:
query12(text);
break;
case 13:
query13(text);
break;
case 14:
query14(text);
break;
// Non partition-optimized queries
case 15:
query15(text);
break;
case 16:
query16(text);
break;
case 17:
query17(text);
break;
case 18:
query18(text);
break;
}
}
// entry point
int main(int argc, char ** argv)
{
// check arguments
if(argc > numOptimized + 2) {
std::cout << "Supply at most " << (numOptimized + 1) << " arguments: [number non-optimized queries], [numQ1], [numQ2], ..." << std::endl;
exit(1);
}
if(!strcmp(argv[1], "--help") || !strcmp(argv[1], "help")) {
std::cout << "Supply at most " << (numOptimized + 1) << " arguments: [number non-optimized queries], [numQ1], [numQ2], ..." << std::endl;
exit(0);
}
int totalOptQueries = 0; // Total number of optimized queries
int * counts = new int[argc - 1]; // Counts of each type of query
int totalNotOptQueries = atoi(argv[1]); // Ratio of non-optimized queries to optimized
int totalQueries = 0;
// Loop through number of arguments (number of queries to run)
for(int i = 2; i < argc; i++) {
int count = atoi(argv[i]);
counts[i - 2] = count;
totalOptQueries += count;
}
// Calculate total number of queries
totalQueries = totalOptQueries + totalNotOptQueries;
// Vector to store queries
std::vector<std::stringstream*> * queries = new std::vector<std::stringstream*>();
// initialize random seed
srand ( time(NULL) );
// Generate partition-optimized queries
for(int i = 0; i < argc - 2; i++) {
for(int j = 0; j < counts[i]; j++) {
std::stringstream * text = new std::stringstream();
genQuery(*text, i);
queries->push_back(text);
}
}
// Generate non optimized queries
for(int i = 0; i < totalNotOptQueries; i++) {
int queryNum = rand() % 4 + 15; // Non-optimized queries between 15 & 18
std::stringstream * text = new std::stringstream();
genQuery(*text, queryNum);
queries->push_back(text);
}
// Randomize queries
random_shuffle(queries->begin(), queries->end());
std::stringstream * text = new std::stringstream();
*text << "end" << std::endl;
queries->push_back(text);
// print queries to standard out
for(int i = 0; i < totalQueries + 1; i++) {
std::cout << queries->at(i)->str();
// clean up memory while we're at it
delete queries->at(i);
}
// Memory cleanup
delete[] counts;
delete queries;
return 0;
}
#endif