forked from def-/sqlsmith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.hh
More file actions
425 lines (383 loc) · 11.6 KB
/
Copy pathgrammar.hh
File metadata and controls
425 lines (383 loc) · 11.6 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/// @file
/// @brief grammar: Top-level and unsorted grammar productions
#ifndef GRAMMAR_HH
#define GRAMMAR_HH
#include <ostream>
#include "relmodel.hh"
#include <memory>
#include "schema.hh"
#include "prod.hh"
#include "expr.hh"
using std::shared_ptr;
static int g_joins = 1;
struct table_ref : prod {
vector<shared_ptr<named_relation> > refs;
static shared_ptr<table_ref> factory(prod *p);
table_ref(prod *p) : prod(p) { }
virtual ~table_ref() { }
};
struct table_or_query_name : table_ref {
virtual void out(std::ostream &out);
table_or_query_name(prod *p);
virtual ~table_or_query_name() { }
named_relation *t;
};
struct target_table : table_ref {
virtual void out(std::ostream &out);
target_table(prod *p, table *victim = 0);
virtual ~target_table() { }
table *victim_;
};
struct table_sample : table_ref {
virtual void out(std::ostream &out);
table_sample(prod *p);
virtual ~table_sample() { }
struct table *t;
private:
string method;
double percent;
};
struct table_subquery : table_ref {
bool is_lateral;
virtual void out(std::ostream &out);
shared_ptr<struct query_spec> query;
table_subquery(prod *p, bool lateral = false);
virtual ~table_subquery();
virtual void accept(prod_visitor *v);
};
struct lateral_subquery : table_subquery {
lateral_subquery(prod *p)
: table_subquery(p, true) { }
};
/// Table function call in a FROM clause, e.g. unnest(...) with ordinality.
struct table_function_ref : table_ref {
virtual void out(std::ostream &out);
table_function_ref(prod *p);
virtual ~table_function_ref() { }
virtual void accept(prod_visitor *v) {
v->visit(this);
for (auto p : args)
p->accept(v);
}
string funcname;
vector<shared_ptr<value_expr> > args;
/// Literal arguments emitted before the value_expr args.
string literal_args;
bool with_ordinality;
relation derived_table;
};
struct join_cond : prod {
static shared_ptr<join_cond> factory(prod *p, table_ref &lhs, table_ref &rhs);
join_cond(prod *p, table_ref &lhs, table_ref &rhs)
: prod(p) { (void) lhs; (void) rhs;}
};
struct simple_join_cond : join_cond {
std::string condition;
simple_join_cond(prod *p, table_ref &lhs, table_ref &rhs);
virtual void out(std::ostream &out);
};
struct expr_join_cond : join_cond {
struct scope joinscope;
shared_ptr<bool_expr> search;
expr_join_cond(prod *p, table_ref &lhs, table_ref &rhs);
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v) {
search->accept(v);
v->visit(this);
}
};
struct joined_table : table_ref {
virtual void out(std::ostream &out);
joined_table(prod *p);
std::string type;
std::string alias;
virtual std::string ident() { return alias; }
shared_ptr<table_ref> lhs;
shared_ptr<table_ref> rhs;
shared_ptr<join_cond> condition;
virtual ~joined_table() {
}
virtual void accept(prod_visitor *v) {
lhs->accept(v);
rhs->accept(v);
condition->accept(v);
v->visit(this);
}
};
struct from_clause : prod {
std::vector<shared_ptr<table_ref> > reflist;
virtual void out(std::ostream &out);
from_clause(prod *p);
~from_clause() { }
virtual void accept(prod_visitor *v) {
v->visit(this);
for (auto p : reflist)
p->accept(v);
}
};
struct select_list : prod {
std::vector<shared_ptr<value_expr> > value_exprs;
relation derived_table;
int columns = 0;
/// Without a template, generates a random number of columns of random
/// types. With a template, generates exactly one column per type.
select_list(prod *p, vector<sqltype *> *templ = 0);
virtual void out(std::ostream &out);
~select_list() { }
virtual void accept(prod_visitor *v) {
v->visit(this);
for (auto p : value_exprs)
p->accept(v);
}
};
struct query_spec : prod {
std::string set_quantifier;
shared_ptr<struct from_clause> from_clause;
shared_ptr<struct select_list> select_list;
shared_ptr<bool_expr> search;
/// When set, the plain select list items become grouping keys
/// (referenced by ordinal) and aggregates are appended to the select
/// list. Checked by window_function::allowed() during generation.
bool has_group_by = false;
size_t group_by_cols = 0;
shared_ptr<value_expr> having_agg;
shared_ptr<value_expr> having_rhs;
const char *having_op = 0;
std::string order_clause;
std::string limit_clause;
struct scope myscope;
virtual void out(std::ostream &out);
query_spec(prod *p, struct scope *s, bool lateral = 0,
vector<sqltype *> *templ = 0);
virtual void accept(prod_visitor *v) {
v->visit(this);
select_list->accept(v);
from_clause->accept(v);
search->accept(v);
if (having_agg)
having_agg->accept(v);
if (having_rhs)
having_rhs->accept(v);
}
};
struct select_for_update : query_spec {
const char *lockmode;
virtual void out(std::ostream &out);
select_for_update(prod *p, struct scope *s, bool lateral = 0);
};
struct prepare_stmt : prod {
query_spec q;
static long seq;
long id;
virtual void out(std::ostream &out) {
out << "prepare prep" << id << " as " << q;
}
prepare_stmt(prod *p) : prod(p), q(p, scope) {
id = seq++;
}
virtual void accept(prod_visitor *v) {
v->visit(this);
q.accept(v);
}
};
struct modifying_stmt : prod {
table *victim;
struct scope myscope;
modifying_stmt(prod *p, struct scope *s, struct table *victim = 0);
// shared_ptr<modifying_stmt> modifying_stmt::factory(prod *p, struct scope *s);
virtual void pick_victim();
};
struct delete_stmt : modifying_stmt {
shared_ptr<bool_expr> search;
delete_stmt(prod *p, struct scope *s, table *v);
virtual ~delete_stmt() { }
virtual void out(std::ostream &out) {
out << "delete from " << victim->ident();
indent(out);
out << "where " << std::endl << *search;
}
virtual void accept(prod_visitor *v) {
v->visit(this);
search->accept(v);
}
};
struct delete_returning : delete_stmt {
shared_ptr<struct select_list> select_list;
delete_returning(prod *p, struct scope *s, table *victim = 0);
virtual void out(std::ostream &out) {
delete_stmt::out(out);
// Syntax: ERROR: Expected end of statement, found RETURNING
//out << std::endl << "returning " << *select_list;
}
virtual void accept(prod_visitor *v) {
v->visit(this);
search->accept(v);
select_list->accept(v);
}
};
struct comment_stmt : modifying_stmt {
shared_ptr<column> victim_column;
comment_stmt(prod *p, struct scope *s, table *victim = 0);
virtual ~comment_stmt() { }
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v) {
v->visit(this);
}
};
struct insert_stmt : modifying_stmt {
vector<shared_ptr<value_expr> > value_exprs;
insert_stmt(prod *p, struct scope *s, table *victim = 0);
virtual ~insert_stmt() { }
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v) {
v->visit(this);
for (auto p : value_exprs) p->accept(v);
}
};
struct set_list : prod {
vector<shared_ptr<value_expr> > value_exprs;
vector<string> names;
set_list(prod *p, table *target);
virtual ~set_list() { }
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v) {
v->visit(this);
for (auto p : value_exprs) p->accept(v);
}
};
struct upsert_stmt : insert_stmt {
shared_ptr<struct set_list> set_list;
string constraint;
shared_ptr<bool_expr> search;
upsert_stmt(prod *p, struct scope *s, table *v = 0);
virtual void out(std::ostream &out) {
insert_stmt::out(out);
out << " on conflict on constraint " << constraint << " do update ";
out << *set_list << " where " << *search;
}
virtual void accept(prod_visitor *v) {
insert_stmt::accept(v);
set_list->accept(v);
search->accept(v);
}
virtual ~upsert_stmt() { }
};
struct update_stmt : modifying_stmt {
shared_ptr<bool_expr> search;
shared_ptr<struct set_list> set_list;
update_stmt(prod *p, struct scope *s, table *victim = 0);
virtual ~update_stmt() { }
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v) {
v->visit(this);
search->accept(v);
}
};
struct when_clause : prod {
bool matched;
shared_ptr<bool_expr> condition;
// shared_ptr<prod> merge_action;
when_clause(struct merge_stmt *p);
virtual ~when_clause() { }
static shared_ptr<when_clause> factory(struct merge_stmt *p);
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v);
};
struct when_clause_update : when_clause {
shared_ptr<struct set_list> set_list;
struct scope myscope;
when_clause_update(struct merge_stmt *p);
virtual ~when_clause_update() { }
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v);
};
struct when_clause_insert : when_clause {
vector<shared_ptr<value_expr> > exprs;
when_clause_insert(struct merge_stmt *p);
virtual ~when_clause_insert() { }
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v);
};
struct merge_stmt : modifying_stmt {
merge_stmt(prod *p, struct scope *s, table *victim = 0);
shared_ptr<table_ref> target_table_;
shared_ptr<table_ref> data_source;
shared_ptr<join_cond> join_condition;
vector<shared_ptr<when_clause> > clauselist;
virtual ~merge_stmt() { }
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v);
};
struct update_returning : update_stmt {
shared_ptr<struct select_list> select_list;
update_returning(prod *p, struct scope *s, table *victim = 0);
virtual void out(std::ostream &out) {
update_stmt::out(out);
// Syntax: ERROR: Expected end of statement, found RETURNING
//out << std::endl << "returning " << *select_list;
}
virtual void accept(prod_visitor *v) {
v->visit(this);
search->accept(v);
set_list->accept(v);
select_list->accept(v);
}
};
/// Set operation over two or more type-compatible SELECTs, e.g.
/// (q1) union all (q2) except (q3).
struct set_op_query : prod {
struct scope myscope;
vector<shared_ptr<query_spec> > operands;
vector<string> setops;
set_op_query(prod *p, struct scope *s);
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v) {
v->visit(this);
for (auto q : operands)
q->accept(v);
}
};
/// WITH MUTUALLY RECURSIVE query. Always emits RETURN AT RECURSION
/// LIMIT so non-converging bindings terminate instead of timing out.
struct wmr_query : prod {
struct scope myscope;
long recursion_limit;
vector<shared_ptr<named_relation> > bindings;
vector<shared_ptr<relation> > binding_rels;
vector<vector<sqltype *> > binding_types;
vector<shared_ptr<query_spec> > binding_queries;
shared_ptr<query_spec> query;
wmr_query(prod *p, struct scope *s);
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v) {
v->visit(this);
for (auto q : binding_queries)
q->accept(v);
query->accept(v);
}
};
/// Whether a result type is concrete enough to be used as a select list
/// column without drawing "cannot reference pseudo type" errors.
bool concrete_result_type(sqltype *t);
shared_ptr<prod> statement_factory(struct scope *s, long max_joins=1, struct prod *parent = 0);
struct explain_stmt : prod {
shared_ptr<prod> q;
virtual void out(std::ostream &out);
explain_stmt(struct prod* p, shared_ptr<prod> query) : prod(p), q(query) {
}
virtual void accept(prod_visitor *v) {
v->visit(this);
q->accept(v);
}
};
shared_ptr<prod> explain_factory(struct scope *s, long max_joins=1);
struct common_table_expression : prod {
vector<shared_ptr<prod> > with_queries;
shared_ptr<prod> query;
vector<shared_ptr<named_relation> > refs;
struct scope myscope;
virtual void out(std::ostream &out);
virtual void accept(prod_visitor *v);
common_table_expression(prod *parent, struct scope *s);
};
#endif