-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries_generator.cpp
More file actions
203 lines (192 loc) · 4.77 KB
/
queries_generator.cpp
File metadata and controls
203 lines (192 loc) · 4.77 KB
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
#include <iostream>
#include <fstream>
#define NUM_COLUMN 3
using namespace std;
enum Return_type { INTEGER = 0,
STRING = 1,
MIX_COL_NUM = 2,
COLUMN = 3,
NUMBER = 4};
string random_arth_op()
{
string op;
switch(rand() % 5)
{
case 0:
op = "+";
break;
case 1:
op = "-";
break;
case 2:
op = "*";
break;
case 3:
op = "/";
break;
case 4:
op = "^";
break;
}
return op;
}
string random_compare_op()
{
string op;
switch(rand() % 6)
{
case 0:
op = ">";
break;
case 1:
op = "<";
break;
case 2:
op = ">=";
break;
case 3:
op = "<=";
break;
case 4:
op = "==";
break;
case 5:
op = "!=";
break;
}
return op;
}
string random_col()
{
return "int(_" + to_string(1 + (rand() % NUM_COLUMN)) + ")";
}
string random_number()
{
return "int(" + to_string(rand() % 10) + ")";
}
string random_num_expr(int depth)
{
if (depth == 0)
{
if ((rand() % 2) == 0)
{
return random_col();
}
else
{
return random_number();
}
}
return random_num_expr(depth-1) + random_arth_op() +
random_num_expr(depth-1);
}
string random_query_expr(int depth, string& input_str, int type)
{
string expr;
if (depth == 0)
{
switch (type)
{
case INTEGER:
expr = random_number();
break;
case STRING:
expr = "\"" + input_str + "\"";
break;
case MIX_COL_NUM:
expr = random_num_expr(depth);
break;
}
return expr;
}
int option;
if (type == INTEGER) //return type is int
{
switch (option = rand() % 7)
{
case 0:
expr = "avg(" + random_col() + random_arth_op() + random_num_expr(depth-1) +
") " + random_arth_op() + " " + random_num_expr(depth-1);
break;
case 1:
expr = "count("+ random_col() + ") " + random_arth_op() + " " +
random_num_expr(depth-1);
break;
case 2:
expr = "max(" + random_col() + random_arth_op() + random_num_expr(depth-1) +
") " + random_arth_op() + " " + random_num_expr(depth-1);
break;
case 3:
expr = "min(" + random_col() + random_arth_op() + random_num_expr(depth-1) +
") " + random_arth_op() + " " + random_num_expr(depth-1);
break;
case 4:
expr = "sum(" + random_col() + ") " + random_arth_op() + " " +
random_num_expr(depth-1);
break;
case 5:
expr = "charlength(" + random_query_expr(depth-1, input_str, STRING) + ")";
break;
case 6:
expr = "characterlength(" + random_query_expr(depth-1, input_str, STRING) + ")";
break;
}
}
else if (type == STRING) // return type is string
{
switch (option = rand() % 3)
{
case 0:
expr = "lower(" + random_query_expr(depth-1, input_str, STRING) + ")";
break;
case 1:
expr = "upper(" + random_query_expr(depth-1, input_str, STRING) + ")";
break;
case 2:
expr = "substr(" + random_query_expr(depth-1, input_str, STRING) + ", " +
random_query_expr(depth-1, input_str, INTEGER) + ", " +
random_query_expr(depth-1, input_str, INTEGER) + ")";
break;
}
}
else if (type == MIX_COL_NUM)
{
expr = random_num_expr(depth-1);
}
else if (type == COLUMN) // return type integer column number
{
expr = random_col();
}
else if (type == NUMBER) // return type randon number
{
expr = random_number();
}
else
{
expr = "error";
}
return expr;
}
int main()
{
srand(time(0));
int reps;
fstream query_file;
query_file.open("queries.txt", ios::out);
string input_str = " $$AbCdEfGhIjKlMnOpQrStUvWxYz## ";
cout << "Enter number of quries to be generated: ";
cin >> reps;
if(query_file.is_open()) //checking whether the file is open
{
while (reps)
{
const string input_query = "select " + random_query_expr(3, input_str, (rand() % 3))
+ " from stdin;";
query_file << input_query << endl;
//cout << input_query << endl;
reps--;
}
query_file.close();
}
return 0;
}