1 | /* |
2 | * This Source Code Form is subject to the terms of the Mozilla Public |
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
5 | * |
6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
7 | */ |
8 | |
9 | #ifndef _SQL_STATEMENT_H_ |
10 | #define _SQL_STATEMENT_H_ |
11 | |
12 | #include "sql_mem.h" |
13 | #include "sql_types.h" |
14 | #include "sql_atom.h" |
15 | #include "sql_string.h" |
16 | #include "sql_mvc.h" |
17 | #include "mal_backend.h" |
18 | |
19 | typedef union stmtdata { |
20 | struct atom *aval; |
21 | struct list *lval; |
22 | struct stmt *stval; |
23 | |
24 | struct sql_column *cval; |
25 | struct sql_idx *idxval; |
26 | struct sql_table *tval; |
27 | |
28 | sql_subtype typeval; |
29 | struct sql_subaggr *aggrval; |
30 | struct sql_subfunc *funcval; |
31 | sql_rel *rel; |
32 | } stmtdata; |
33 | |
34 | typedef enum stmt_type { |
35 | st_none, |
36 | st_var, /* use and/or declare variable */ |
37 | |
38 | st_table, /* some functions return a table */ |
39 | st_temp, /* temporal bat */ |
40 | st_single, /* single value bat */ |
41 | st_rs_column, |
42 | st_tid, |
43 | st_bat, |
44 | st_idxbat, |
45 | st_const, |
46 | st_gen_group, |
47 | st_mirror, |
48 | st_result, /* get nth result of a statement */ |
49 | |
50 | st_limit, |
51 | st_limit2, |
52 | st_sample, |
53 | st_order, |
54 | st_reorder, |
55 | |
56 | st_output, |
57 | st_affected_rows, |
58 | |
59 | st_atom, |
60 | st_uselect, |
61 | st_uselect2, |
62 | st_tunion, |
63 | st_tdiff, |
64 | st_tinter, |
65 | |
66 | st_join, |
67 | st_join2, |
68 | st_joinN, |
69 | |
70 | st_export, |
71 | st_append, |
72 | st_table_clear, |
73 | st_exception, |
74 | st_trans, |
75 | st_catalog, |
76 | |
77 | st_append_col, |
78 | st_append_idx, |
79 | st_update_col, |
80 | st_update_idx, |
81 | st_delete, |
82 | |
83 | st_group, |
84 | st_convert, |
85 | st_Nop, |
86 | st_func, |
87 | st_aggr, |
88 | |
89 | st_alias, |
90 | |
91 | /* used internally only */ |
92 | st_list, |
93 | |
94 | /* flow control statements */ |
95 | st_cond, |
96 | st_control_end, |
97 | st_return, |
98 | st_assign |
99 | } st_type; |
100 | |
101 | /* flag to indicate anti join/select */ |
102 | #define SWAPPED 16 |
103 | #define ANTI ANTISEL |
104 | #define GRP_DONE 32 |
105 | |
106 | typedef struct stmt { |
107 | st_type type; |
108 | struct stmt *op1; |
109 | struct stmt *op2; |
110 | struct stmt *op3; |
111 | stmtdata op4; /* only op4 will hold other types */ |
112 | |
113 | char nrcols; |
114 | char key; /* key (aka all values are unique) */ // TODO make this thing a bool |
115 | char aggr; /* aggregated */ |
116 | char partition; /* selected as mitosis candidate */ |
117 | |
118 | int flag; |
119 | |
120 | int nr; /* variable assignment */ |
121 | |
122 | const char *tname; |
123 | const char *cname; |
124 | InstrPtr q; |
125 | } stmt; |
126 | |
127 | extern void create_merge_partitions_accumulator(backend *be); |
128 | extern int add_to_merge_partitions_accumulator(backend *be, int nr); |
129 | |
130 | extern int stmt_key(stmt *s); |
131 | |
132 | extern stmt *stmt_none(backend *be); |
133 | |
134 | //#define VAR_DECLARE 1 |
135 | #define VAR_GLOBAL(f) ((f>>1)==1) |
136 | extern stmt *stmt_var(backend *be, const char *varname, sql_subtype *t, int declare, int level); |
137 | extern stmt *stmt_vars(backend *be, const char *varname, sql_table *t, int declare, int level); |
138 | extern stmt *stmt_varnr(backend *be, int nr, sql_subtype *t); |
139 | |
140 | extern stmt *stmt_table(backend *be, stmt *cols, int temp); |
141 | extern stmt *stmt_rs_column(backend *be, stmt *result_set, int i, sql_subtype *tpe); |
142 | |
143 | extern stmt *stmt_bat(backend *be, sql_column *c, int access, int partition); |
144 | extern stmt *stmt_idxbat(backend *be, sql_idx *i, int access, int partition); |
145 | extern stmt *stmt_tid(backend *be, sql_table *t, int partition); |
146 | |
147 | extern stmt *stmt_append_col(backend *be, sql_column *c, stmt *b, int locked); |
148 | extern stmt *stmt_append_idx(backend *be, sql_idx *i, stmt *b); |
149 | extern stmt *stmt_update_col(backend *be, sql_column *c, stmt *tids, stmt *upd); |
150 | extern stmt *stmt_update_idx(backend *be, sql_idx *i, stmt *tids, stmt *upd); |
151 | extern stmt *stmt_delete(backend *be, sql_table *t, stmt *b); |
152 | |
153 | extern stmt *stmt_append(backend *be, stmt *c, stmt *values); |
154 | extern stmt *stmt_table_clear(backend *be, sql_table *t); |
155 | |
156 | extern stmt *stmt_export(backend *be, stmt *t, const char *sep, const char *rsep, const char *ssep, const char *null_string, int onclient, stmt *file); |
157 | extern stmt *stmt_trans(backend *b, int type, stmt *chain, stmt *name); |
158 | extern stmt *stmt_catalog(backend *be, int type, stmt *args); |
159 | |
160 | extern stmt *stmt_temp(backend *be, sql_subtype *t); |
161 | extern stmt *stmt_atom(backend *be, atom *a); |
162 | extern stmt *stmt_atom_string(backend *be, const char *s); |
163 | extern stmt *stmt_atom_string_nil(backend *be); |
164 | extern stmt *stmt_atom_int(backend *be, int i); |
165 | extern stmt *stmt_atom_lng(backend *be, lng i); |
166 | extern stmt *stmt_atom_lng_nil(backend *be); |
167 | extern stmt *stmt_bool(backend *be, int b); |
168 | |
169 | extern stmt *stmt_uselect(backend *be, stmt *op1, stmt *op2, comp_type cmptype, stmt *sub, int anti); |
170 | /* cmp |
171 | 0 == l < x < h |
172 | 1 == l < x <= h |
173 | 2 == l <= x < h |
174 | 3 == l <= x <= h |
175 | */ |
176 | extern stmt *stmt_uselect2(backend *be, stmt *op1, stmt *op2, stmt *op3, int cmp, stmt *sub, int anti); |
177 | extern stmt *stmt_genselect(backend *be, stmt *lops, stmt *rops, sql_subfunc *f, stmt *sub, int anti); |
178 | |
179 | extern stmt *stmt_tunion(backend *be, stmt *op1, stmt *op2); |
180 | extern stmt *stmt_tdiff(backend *be, stmt *op1, stmt *op2); |
181 | extern stmt *stmt_tdiff2(backend *be, stmt *op1, stmt *op2); |
182 | extern stmt *stmt_tinter(backend *be, stmt *op1, stmt *op2); |
183 | |
184 | extern stmt *stmt_join(backend *be, stmt *op1, stmt *op2, int anti, comp_type cmptype); |
185 | extern stmt *stmt_join2(backend *be, stmt *l, stmt *ra, stmt *rb, int cmp, int anti, int swapped); |
186 | /* generic join operator, with a left and right statement list */ |
187 | extern stmt *stmt_genjoin(backend *be, stmt *l, stmt *r, sql_subfunc *op, int anti, int swapped); |
188 | |
189 | extern stmt *stmt_project(backend *be, stmt *op1, stmt *op2); |
190 | extern stmt *stmt_project_delta(backend *be, stmt *col, stmt *upd, stmt *ins); |
191 | extern stmt *stmt_left_project(backend *be, stmt *op1, stmt *op2, stmt *op3); |
192 | |
193 | extern stmt *stmt_list(backend *be, list *l); |
194 | extern void stmt_set_nrcols(stmt *s); |
195 | |
196 | extern stmt *stmt_group(backend *be, stmt *op1, stmt *grp, stmt *ext, stmt *cnt, int done); |
197 | |
198 | /* raise exception incase the condition (cond) holds, continue with stmt res */ |
199 | extern stmt *stmt_exception(backend *be, stmt *cond, const char *errstr, int errcode); |
200 | |
201 | extern stmt *stmt_const(backend *be, stmt *s, stmt *val); |
202 | |
203 | extern stmt *stmt_gen_group(backend *be, stmt *gids, stmt *cnts); /* given a gid,cnt blowup to full groups */ |
204 | extern stmt *stmt_mirror(backend *be, stmt *s); |
205 | extern stmt *stmt_result(backend *be, stmt *s, int nr); |
206 | |
207 | /* |
208 | * distinct: compute topn on unique groups |
209 | * dir: direction of the ordering, ie 1 Ascending, 0 decending |
210 | * last: intermediate step or last step |
211 | * order: is order important or not (firstn vs slice) |
212 | */ |
213 | extern stmt *stmt_limit(backend *sa, stmt *c, stmt *piv, stmt *gid, stmt *offset, stmt *limit, int distinct, int dir, int nullslast, int last, int order); |
214 | extern stmt *stmt_sample(backend *be, stmt *s, stmt *sample, stmt *seed); |
215 | extern stmt *stmt_order(backend *be, stmt *s, int direction, int nullslast); |
216 | extern stmt *stmt_reorder(backend *be, stmt *s, int direction, int nullslast, stmt *orderby_ids, stmt *orderby_grp); |
217 | |
218 | extern stmt *stmt_convert(backend *sa, stmt *v, sql_subtype *from, sql_subtype *to, stmt *sel); |
219 | extern stmt *stmt_unop(backend *be, stmt *op1, sql_subfunc *op); |
220 | extern stmt *stmt_binop(backend *be, stmt *op1, stmt *op2, sql_subfunc *op); |
221 | extern stmt *stmt_Nop(backend *be, stmt *ops, sql_subfunc *op); |
222 | extern stmt *stmt_func(backend *be, stmt *ops, const char *name, sql_rel *imp, int f_union); |
223 | extern stmt *stmt_aggr(backend *be, stmt *op1, stmt *grp, stmt *ext, sql_subaggr *op, int reduce, int no_nil, int nil_if_empty); |
224 | |
225 | extern stmt *stmt_alias(backend *be, stmt *op1, const char *tname, const char *name); |
226 | |
227 | extern stmt *stmt_output(backend *be, stmt *l); |
228 | extern stmt *stmt_affected_rows(backend *be, stmt *l); |
229 | |
230 | /* flow control statements */ |
231 | extern stmt *stmt_cond(backend *be, stmt *cond, stmt *outer, int loop, int anti); |
232 | extern stmt *stmt_control_end(backend *be, stmt *cond); |
233 | extern stmt *stmt_return(backend *be, stmt *val, int nr_of_declared_tables); |
234 | extern stmt *stmt_assign(backend *be, const char *varname, stmt *val, int level); |
235 | |
236 | extern sql_subtype *tail_type(stmt *st); |
237 | extern int stmt_has_null(stmt *s); |
238 | |
239 | extern const char *column_name(sql_allocator *sa, stmt *st); |
240 | extern const char *table_name(sql_allocator *sa, stmt *st); |
241 | extern const char *schema_name(sql_allocator *sa, stmt *st); |
242 | |
243 | extern stmt *const_column(backend *ba, stmt *val); |
244 | extern stmt *stmt_fetch(backend *ba, stmt *val); |
245 | |
246 | #endif /* _SQL_STATEMENT_H_ */ |
247 | |