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 | /* multi version catalog */ |
10 | #ifndef _SQL_MVC_H |
11 | #define _SQL_MVC_H |
12 | |
13 | #include "sql_mem.h" |
14 | #include "gdk.h" |
15 | #include "sql_scan.h" |
16 | #include "sql_list.h" |
17 | #include "sql_types.h" |
18 | #include "sql_backend.h" |
19 | #include "sql_catalog.h" |
20 | #include "sql_relation.h" |
21 | #include "sql_storage.h" |
22 | #include "sql_keyword.h" |
23 | #include "sql_querytype.h" |
24 | #include "sql_atom.h" |
25 | #include "sql_tokens.h" |
26 | #include "sql_symbol.h" |
27 | |
28 | #define ERRSIZE 8192 |
29 | #define ERR_AMBIGUOUS 050000 |
30 | #define ERR_GROUPBY 060000 |
31 | |
32 | /* value vs predicate (boolean) */ |
33 | #define type_value 0 |
34 | #define type_predicate 1 |
35 | |
36 | /* cardinality expected by enclosing operator */ |
37 | #define card_none -1 /* psm call doesn't return anything */ |
38 | #define card_value 0 |
39 | #define card_row 1 /* needed for subqueries on single value tables (select (select 1))*/ |
40 | #define card_column 2 |
41 | #define card_set 3 /* some operators require only a set (IN/EXISTS) */ |
42 | #define card_exists 4 |
43 | #define card_relation 5 |
44 | #define card_loader 6 |
45 | |
46 | #define CARD_VALUE(card) (card == card_value || card == card_row || card == card_column || card == card_set || card == card_exists) |
47 | |
48 | /* allowed to reduce (in the where and having parts we can reduce) */ |
49 | |
50 | /* different query execution modes (emode) */ |
51 | #define m_normal 0 |
52 | #define m_execute 2 |
53 | #define m_prepare 3 |
54 | #define m_plan 4 |
55 | |
56 | /* special modes for function/procedure and view instantiation and |
57 | dependency generation */ |
58 | #define m_instantiate 5 |
59 | #define m_deps 6 |
60 | |
61 | /* different query execution modifiers (emod) */ |
62 | #define mod_none 0 |
63 | #define mod_debug 1 |
64 | #define mod_trace 2 |
65 | #define mod_explain 4 |
66 | /* locked needs unlocking */ |
67 | #define mod_locked 16 |
68 | |
69 | typedef struct sql_groupby_expression { |
70 | symbol *sdef; |
71 | tokens token; |
72 | sql_exp *exp; |
73 | } sql_groupby_expression; |
74 | |
75 | typedef struct sql_var { |
76 | const char *name; |
77 | atom a; |
78 | sql_table *t; |
79 | sql_rel *rel; |
80 | dlist *wdef; |
81 | sql_groupby_expression *exp; |
82 | char view; |
83 | char frame; |
84 | char visited; //used for window definitions lookup |
85 | } sql_var; |
86 | |
87 | #define MAXSTATS 8 |
88 | |
89 | typedef struct mvc { |
90 | char errstr[ERRSIZE]; |
91 | |
92 | sql_allocator *sa; |
93 | struct qc *qc; |
94 | int clientid; /* id of the owner */ |
95 | struct scanner scanner; |
96 | |
97 | list *params; |
98 | sql_func *forward; /* forward definitions for recursive functions */ |
99 | sql_var *vars; /* stack of variables, frames are simply a |
100 | NULL in the var stack |
101 | (sometimes with name (label) ) */ |
102 | int topvars; |
103 | int sizevars; |
104 | int frame; |
105 | int use_views; |
106 | atom **args; |
107 | int argc; |
108 | int argmax; |
109 | struct symbol *sym; |
110 | int no_mitosis; /* run query without mitosis */ |
111 | |
112 | sqlid user_id; |
113 | sqlid role_id; |
114 | lng last_id; |
115 | lng rowcnt; |
116 | |
117 | /* current session variables */ |
118 | int timezone; /* milliseconds west of UTC */ |
119 | int cache; /* some queries should not be cached ! */ |
120 | int caching; /* cache current query ? */ |
121 | int reply_size; /* reply size */ |
122 | bool ; /* print size header in result set */ |
123 | int debug; |
124 | |
125 | lng Topt; /* timer for optimizer phase */ |
126 | char emode; /* execution mode */ |
127 | char emod; /* execution modifier */ |
128 | |
129 | sql_session *session; |
130 | |
131 | sql_query_t type; /* query type */ |
132 | int pushdown; /* AND or OR query handling */ |
133 | unsigned int label; /* numbers for relational projection labels */ |
134 | int remote; |
135 | list *cascade_action; /* protection against recursive cascade actions */ |
136 | |
137 | int opt_stats[MAXSTATS];/* keep statistics about optimizer rewrites */ |
138 | |
139 | int result_id; |
140 | res_table *results; |
141 | char *query; /* string, identify whatever we're working on */ |
142 | } mvc; |
143 | |
144 | extern sql_table *mvc_init_create_view(mvc *sql, sql_schema *s, const char *name, const char *query); |
145 | extern int mvc_init(int debug, store_type store, int ro, int su, backend_stack stk); |
146 | extern void mvc_exit(void); |
147 | extern void mvc_logmanager(void); |
148 | extern void mvc_idlemanager(void); |
149 | |
150 | extern mvc *mvc_create(int clientid, backend_stack stk, int debug, bstream *rs, stream *ws); |
151 | extern int mvc_reset(mvc *m, bstream *rs, stream *ws, int debug, int globalvars); |
152 | extern void mvc_destroy(mvc *c); |
153 | |
154 | extern int mvc_status(mvc *c); |
155 | extern int mvc_error_retry(mvc *c); // error code on errors else 0, errors AMBIGUOUS and GROUPBY will also output 0 |
156 | extern int mvc_type(mvc *c); |
157 | extern int mvc_debug_on(mvc *m, int flag); |
158 | extern void mvc_cancel_session(mvc *m); |
159 | |
160 | /* since Savepoints and transactions are related the |
161 | * commit function includes the savepoint creation. |
162 | * Rollbacks can be either full or until a given savepoint. |
163 | * The special mvc_release can be used to release savepoints. |
164 | */ |
165 | #define has_snapshots(tr) ((tr) && (tr)->parent && (tr)->parent->parent) |
166 | |
167 | extern int mvc_trans(mvc *c); |
168 | extern str mvc_commit(mvc *c, int chain, const char *name, bool enabling_auto_commit); |
169 | extern str mvc_rollback(mvc *c, int chain, const char *name, bool disabling_auto_commit); |
170 | extern str mvc_release(mvc *c, const char *name); |
171 | |
172 | extern sql_type *mvc_bind_type(mvc *sql, const char *name); |
173 | extern sql_type *schema_bind_type(mvc *sql, sql_schema * s, const char *name); |
174 | extern sql_func *mvc_bind_func(mvc *sql, const char *name); |
175 | extern list *schema_bind_func(mvc *sql, sql_schema * s, const char *name, sql_ftype type); |
176 | |
177 | extern sql_schema *mvc_bind_schema(mvc *c, const char *sname); |
178 | extern sql_table *mvc_bind_table(mvc *c, sql_schema *s, const char *tname); |
179 | extern sql_column *mvc_bind_column(mvc *c, sql_table *t, const char *cname); |
180 | extern sql_column *mvc_first_column(mvc *c, sql_table *t); |
181 | extern sql_idx *mvc_bind_idx(mvc *c, sql_schema *s, const char *iname); |
182 | extern sql_key *mvc_bind_key(mvc *c, sql_schema *s, const char *kname); |
183 | extern sql_key *mvc_bind_ukey(sql_table *t, list *cols); |
184 | extern sql_trigger *mvc_bind_trigger(mvc *c, sql_schema *s, const char *tname); |
185 | |
186 | extern sql_type *mvc_create_type(mvc *sql, sql_schema *s, const char *sqlname, int digits, int scale, int radix, const char *impl); |
187 | extern int mvc_drop_type(mvc *sql, sql_schema *s, sql_type *t, int drop_action); |
188 | |
189 | extern sql_func *mvc_create_func(mvc *sql, sql_allocator *sa, sql_schema *s, const char *name, list *args, list *res, sql_ftype type, sql_flang lang, const char *mod, const char *impl, const char *query, bit varres, bit vararg, bit system); |
190 | extern int mvc_drop_func(mvc *c, sql_schema *s, sql_func * func, int drop_action); |
191 | extern int mvc_drop_all_func(mvc *c, sql_schema *s, list *list_func, int drop_action); |
192 | |
193 | extern int mvc_drop_schema(mvc *c, sql_schema *s, int drop_action); |
194 | extern sql_schema *mvc_create_schema(mvc *m, const char *name, sqlid auth_id, sqlid owner); |
195 | extern BUN mvc_clear_table(mvc *m, sql_table *t); |
196 | extern str mvc_drop_table(mvc *c, sql_schema *s, sql_table * t, int drop_action); |
197 | extern sql_table *mvc_create_table(mvc *c, sql_schema *s, const char *name, int tt, bit system, int persistence, int commit_action, int sz, bit properties); |
198 | extern sql_table *mvc_create_view(mvc *c, sql_schema *s, const char *name, int persistence, const char *sql, bit system); |
199 | extern sql_table *mvc_create_remote(mvc *c, sql_schema *s, const char *name, int persistence, const char *loc); |
200 | |
201 | extern int mvc_drop_column(mvc *c, sql_table *t, sql_column *col, int drop_action); |
202 | extern sql_column *mvc_create_column(mvc *c, sql_table *t, const char *name, sql_subtype *type); |
203 | extern sql_column *mvc_create_column_(mvc *c, sql_table *t, const char *name, const char *type, int digits); |
204 | extern sql_column *mvc_null(mvc *c, sql_column *col, int flag); |
205 | extern sql_column *mvc_default(mvc *c, sql_column *col, char *val); |
206 | extern sql_column *mvc_drop_default(mvc *c, sql_column *col); |
207 | extern sql_column *mvc_storage(mvc *c, sql_column *col, char *storage); |
208 | extern sql_table * mvc_access(mvc *m, sql_table *t, sht access); |
209 | extern int mvc_is_sorted(mvc *c, sql_column *col); |
210 | |
211 | extern sql_ukey *mvc_create_ukey(mvc *m, sql_table *t, const char *kname, key_type kt); |
212 | extern sql_key *mvc_create_ukey_done(mvc *m, sql_key *k); |
213 | extern sql_fkey *mvc_create_fkey(mvc *m, sql_table *t, const char *kname, key_type kt, sql_key *rk, int on_delete, int on_update); |
214 | extern sql_key *mvc_create_kc(mvc *m, sql_key *k, sql_column *c); |
215 | extern sql_fkey *mvc_create_fkc(mvc *m, sql_fkey *fk, sql_column *c); |
216 | |
217 | extern int mvc_drop_key(mvc *c, sql_schema *s, sql_key *key, int drop_action); |
218 | |
219 | extern sql_idx *mvc_create_idx(mvc *m, sql_table *t, const char *iname, idx_type it); |
220 | extern sql_idx *mvc_create_ic(mvc *m, sql_idx * i, sql_column *c); |
221 | extern int mvc_drop_idx(mvc *c, sql_schema *s, sql_idx * i); |
222 | |
223 | extern sql_trigger * mvc_create_trigger(mvc *m, sql_table *t, const char *name, sht time, sht orientation, sht event, const char *old_name, const char *new_name, const char *condition, const char *statement ); |
224 | extern sql_trigger * mvc_create_tc(mvc *m, sql_trigger * i, sql_column *c /*, extra options such as trunc */ ); |
225 | extern int mvc_drop_trigger(mvc *m, sql_schema *s, sql_trigger * tri); |
226 | |
227 | /*dependency control*/ |
228 | extern void mvc_create_dependency(mvc *m, sqlid id, sqlid depend_id, sql_dependency depend_type); |
229 | extern void mvc_create_dependencies(mvc *m, list *id_l, sqlid depend_id, sql_dependency dep_type); |
230 | extern int mvc_check_dependency(mvc *m, sqlid id, sql_dependency type, list *ignore_ids); |
231 | |
232 | /* variable management */ |
233 | extern sql_var* stack_push_var(mvc *sql, const char *name, sql_subtype *type); |
234 | extern sql_var* stack_push_rel_var(mvc *sql, const char *name, sql_rel *var, sql_subtype *type); |
235 | extern sql_var* stack_push_table(mvc *sql, const char *name, sql_rel *var, sql_table *t); |
236 | extern sql_var* stack_push_rel_view(mvc *sql, const char *name, sql_rel *view); |
237 | extern sql_var* stack_push_window_def(mvc *sql, const char *name, dlist *sym); |
238 | extern dlist* stack_get_window_def(mvc *sql, const char *name, int *pos); |
239 | extern sql_var* stack_push_groupby_expression(mvc *sql, symbol *def, sql_exp *exp); |
240 | extern sql_exp* stack_get_groupby_expression(mvc *sql, symbol *def); |
241 | extern void stack_update_rel_view(mvc *sql, const char *name, sql_rel *view); |
242 | |
243 | extern char stack_check_var_visited(mvc *sql, int i); |
244 | extern void stack_set_var_visited(mvc *sql, int i); |
245 | extern void stack_clear_frame_visited_flag(mvc *sql); |
246 | |
247 | extern sql_var* stack_push_frame(mvc *sql, const char *name); |
248 | extern void stack_pop_frame(mvc *sql); |
249 | extern void stack_pop_until(mvc *sql, int top); |
250 | extern sql_subtype *stack_find_type(mvc *sql, const char *name); |
251 | extern sql_table *stack_find_table(mvc *sql, const char *name); |
252 | extern sql_rel *stack_find_rel_view(mvc *sql, const char *name); |
253 | extern int stack_find_var(mvc *sql, const char *name); |
254 | extern sql_rel *stack_find_rel_var(mvc *sql, const char *name); |
255 | /* find var in current frame */ |
256 | extern int frame_find_var(mvc *sql, const char *name); |
257 | /* find frame holding variable 'name' */ |
258 | extern int stack_find_frame(mvc *sql, const char *name); |
259 | /* find frame with given name */ |
260 | extern int stack_has_frame(mvc *sql, const char *name); |
261 | extern int stack_nr_of_declared_tables(mvc *sql); |
262 | |
263 | extern atom* stack_get_var(mvc *sql, const char *name); |
264 | extern atom* stack_set_var(mvc *sql, const char *name, ValRecord *v); |
265 | |
266 | extern str stack_get_string(mvc *sql, const char *name); |
267 | extern str stack_set_string(mvc *sql, const char *name, const char *v); |
268 | #ifdef HAVE_HGE |
269 | extern hge val_get_number(ValRecord *val); |
270 | extern hge stack_get_number(mvc *sql, const char *name); |
271 | extern void stack_set_number(mvc *sql, const char *name, hge v); |
272 | #else |
273 | extern lng val_get_number(ValRecord *val); |
274 | extern lng stack_get_number(mvc *sql, const char *name); |
275 | extern void stack_set_number(mvc *sql, const char *name, lng v); |
276 | #endif |
277 | |
278 | extern sql_column *mvc_copy_column(mvc *m, sql_table *t, sql_column *c); |
279 | extern sql_key *mvc_copy_key(mvc *m, sql_table *t, sql_key *k); |
280 | extern sql_idx *mvc_copy_idx(mvc *m, sql_table *t, sql_idx *i); |
281 | extern sql_trigger *mvc_copy_trigger(mvc *m, sql_table *t, sql_trigger *tr); |
282 | extern sql_part *mvc_copy_part(mvc *m, sql_table *t, sql_part *pt); |
283 | |
284 | extern void *sql_error(mvc *sql, int error_code, _In_z_ _Printf_format_string_ char *format, ...) |
285 | __attribute__((__format__(__printf__, 3, 4))); |
286 | |
287 | extern int symbol_cmp(mvc* sql, symbol *s1, symbol *s2); |
288 | |
289 | #endif /*_SQL_MVC_H*/ |
290 | |