Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /* multi version catalog */
14 : #ifndef _SQL_MVC_H
15 : #define _SQL_MVC_H
16 :
17 : #include "sql_mem.h"
18 : #include "gdk.h"
19 : #include "sql_scan.h"
20 : #include "sql_list.h"
21 : #include "sql_types.h"
22 : #include "sql_backend.h"
23 : #include "sql_catalog.h"
24 : #include "sql_relation.h"
25 : #include "sql_storage.h"
26 : #include "sql_keyword.h"
27 : #include "mapi_querytype.h"
28 : #include "sql_atom.h"
29 : #include "sql_tokens.h"
30 : #include "sql_symbol.h"
31 :
32 : #define REMOTE_USER_INFO "remote_user_info"
33 :
34 : #define ERRSIZE 8192
35 : #define ERR_AMBIGUOUS 050000
36 : #define ERR_GROUPBY 060000
37 : #define ERR_NOTFOUND 070000
38 :
39 : /* value vs predicate (boolean) */
40 : #define type_value 0
41 : #define type_relation 1
42 :
43 : /* cardinality expected by enclosing operator */
44 : #define card_none -1 /* psm call doesn't return anything */
45 : #define card_value 0
46 : #define card_row 1 /* needed for subqueries on single value tables (select (select 1))*/
47 : #define card_column 2
48 : #define card_set 3 /* some operators require only a set (IN/EXISTS) */
49 : #define card_exists 4
50 : /* to be removed ie are in type (aka dimension) */
51 : #define card_relation 5
52 : #define card_loader 6
53 :
54 : #define CARD_VALUE(card) (card == card_value || card == card_row || card == card_column || card == card_set || card == card_exists)
55 :
56 : /* allowed to reduce (in the where and having parts we can reduce) */
57 :
58 : /* different query execution modes (emode) */
59 : #define m_normal 0
60 : #define m_deallocate 1
61 : #define m_prepare 2
62 : #define m_plan 3
63 :
64 : /* special modes for function/procedure and view instantiation and
65 : dependency generation */
66 : #define m_instantiate 5
67 : #define m_deps 6
68 :
69 : /* different query execution modifiers (emod) */
70 : #define mod_none 0
71 : #define mod_trace 1
72 : #define mod_explain 2
73 : #define mod_exec 4
74 :
75 : typedef struct sql_groupby_expression {
76 : symbol *sdef;
77 : tokens token;
78 : sql_exp *exp;
79 : } sql_groupby_expression;
80 :
81 : typedef struct sql_window_definition {
82 : char *name;
83 : dlist *wdef;
84 : bool visited; /* used for window definitions lookup */
85 : } sql_window_definition;
86 :
87 : typedef struct sql_local_table { /* declared tables during session */
88 : sql_table *table;
89 : } sql_local_table;
90 :
91 : typedef struct sql_rel_view { /* CTEs */
92 : char *name;
93 : sql_rel *rel_view;
94 : } sql_rel_view;
95 :
96 : typedef struct sql_var { /* Declared variables and parameters */
97 : char *sname; /* Global variables have a schema */
98 : char *name;
99 : atom var;
100 : } sql_var;
101 :
102 : typedef struct sql_frame {
103 : char *name; /* frame name */
104 : list *group_expressions;
105 : list *windows;
106 : list *tables;
107 : list *rel_views;
108 : list *vars;
109 : int frame_number;
110 : } sql_frame;
111 :
112 : /* a single SQL optimizer run */
113 : typedef struct {
114 : const char *name; /* the optimizer name itself */
115 : int nchanges; /* how many changes it did */
116 : lng time; /* how long it did take (all runs) */
117 : } sql_optimizer_run;
118 :
119 : typedef struct mvc {
120 : char errstr[ERRSIZE];
121 :
122 : allocator *sa, *ta, *pa;
123 :
124 : struct scanner scanner;
125 :
126 : list *params;
127 : sqlid objid; /* when replacing an existing view, it can't be seen */
128 : sql_func *forward; /* forward definitions for recursive functions */
129 : list *global_vars; /* SQL declared variables on the global scope */
130 : sql_frame **frames; /* stack of frames with variables */
131 : int topframes;
132 : int sizeframes;
133 : int frame;
134 : struct symbol *sym;
135 :
136 : bool use_views:1,
137 : schema_path_has_sys:1, /* speed up object search */
138 : schema_path_has_tmp:1,
139 : no_int128:1;
140 : struct qc *qc;
141 : int clientid; /* id of the owner */
142 :
143 : /* session variables */
144 : sqlid user_id;
145 : sqlid role_id;
146 : int timezone; /* milliseconds west of UTC */
147 : unsigned int div_min_scale; /* minimum scale for division op*/
148 : int reply_size; /* reply size */
149 : int debug;
150 : int sql_optimizer; /* SQL optimizer mask */
151 : sql_optimizer_run *runs; /* Information about SQL optimizer runs */
152 : char emode; /* execution mode */
153 : char emod; /* execution modifier */
154 : sql_session *session;
155 : sql_store store;
156 :
157 : /* per query context */
158 : mapi_query_t type; /* query type */
159 :
160 : /* during query needed flags */
161 : unsigned int label; /* numbers for relational projection labels */
162 : int nid; /* numbers for relational names */
163 : list *cascade_action; /* protection against recursive cascade actions */
164 : list *schema_path; /* schema search path for object lookup */
165 : uintptr_t sp;
166 : } mvc;
167 :
168 : /* should return structure */
169 : extern sql_store mvc_init(int debug, store_type store, int ro, int su, const char *initpasswd);
170 : extern void mvc_exit(sql_store store);
171 :
172 : extern void mvc_logmanager(sql_store store);
173 :
174 : extern mvc *mvc_create(sql_store *store, allocator *pa, int clientid, int debug, bstream *rs, stream *ws);
175 : extern void mvc_destroy(mvc *c);
176 :
177 : extern int mvc_status(mvc *c);
178 : extern int mvc_error_retry(mvc *c); // error code on errors else 0, errors AMBIGUOUS and GROUPBY will also output 0
179 : extern int mvc_type(mvc *c);
180 : extern int mvc_debug_on(mvc *m, int flag);
181 : extern void mvc_cancel_session(mvc *m);
182 :
183 : /* since Savepoints and transactions are related the
184 : * commit function includes the savepoint creation.
185 : * Rollbacks can be either full or until a given savepoint.
186 : * The special mvc_release can be used to release savepoints.
187 : */
188 : #define has_snapshots(tr) ((tr) && (tr)->parent && (tr)->parent->parent)
189 :
190 : extern int mvc_trans(mvc *c);
191 : sql_export str mvc_commit(mvc *c, int chain, const char *name, bool enabling_auto_commit);
192 : sql_export str mvc_rollback(mvc *c, int chain, const char *name, bool disabling_auto_commit);
193 : extern str mvc_release(mvc *c, const char *name);
194 :
195 : extern sql_type *mvc_bind_type(mvc *sql, const char *name);
196 : extern sql_type *schema_bind_type(mvc *sql, sql_schema * s, const char *name);
197 :
198 : sql_export sql_schema *mvc_bind_schema(mvc *c, const char *sname);
199 : sql_export sql_table *mvc_bind_table(mvc *c, sql_schema *s, const char *tname);
200 : extern sql_column *mvc_bind_column(mvc *c, sql_table *t, const char *cname);
201 : extern sql_column *mvc_first_column(mvc *c, sql_table *t);
202 : extern sql_idx *mvc_bind_idx(mvc *c, sql_schema *s, const char *iname);
203 : extern sql_key *mvc_bind_key(mvc *c, sql_schema *s, const char *kname);
204 : extern sql_key *mvc_bind_ukey(sql_table *t, list *cols);
205 : extern sql_trigger *mvc_bind_trigger(mvc *c, sql_schema *s, const char *tname);
206 :
207 : extern int mvc_create_type(mvc *sql, sql_schema *s, const char *sqlname, unsigned int digits, unsigned int scale, int radix, const char *impl);
208 : extern int mvc_drop_type(mvc *sql, sql_schema *s, sql_type *t, int drop_action);
209 :
210 : extern int mvc_create_func(sql_func **f, mvc *m, allocator *sa, sql_schema *s, const char *name, list *args, list *res, sql_ftype type, sql_flang lang,
211 : const char *mod, const char *impl, const char *query, bit varres, bit vararg, bit system, bit side_effect, bit order_required, bit opt_order);
212 : extern int mvc_drop_func(mvc *c, sql_schema *s, sql_func * func, int drop_action);
213 : extern int mvc_drop_all_func(mvc *c, sql_schema *s, list *list_func, int drop_action);
214 :
215 : extern int mvc_drop_schema(mvc *c, sql_schema *s, int drop_action);
216 : extern int mvc_create_schema(mvc *m, const char *name, sqlid auth_id, sqlid owner);
217 : extern BUN mvc_clear_table(mvc *m, sql_table *t);
218 : extern str mvc_drop_table(mvc *c, sql_schema *s, sql_table * t, int drop_action);
219 : sql_export int mvc_create_table(sql_table **t, mvc *m, sql_schema *s, const char *name, int tt, bit system, int persistence, int commit_action, int sz, bit properties);
220 : extern int mvc_create_view(sql_table **t, mvc *m, sql_schema *s, const char *name, int persistence, const char *sql, bit system);
221 : extern int mvc_create_remote(sql_table **t, mvc *m, sql_schema *s, const char *name, int persistence, const char *loc);
222 :
223 : extern int mvc_drop_column(mvc *c, sql_table *t, sql_column *col, int drop_action);
224 : sql_export int mvc_create_column(sql_column **col, mvc *m, sql_table *t, const char *name, sql_subtype *tpe);
225 : extern int mvc_create_column_(sql_column **col, mvc *m, sql_table *t, const char *name, const char *type, unsigned int digits);
226 : extern int mvc_null(mvc *c, sql_column *col, int flag);
227 : extern int mvc_default(mvc *c, sql_column *col, char *val);
228 : extern int mvc_check(mvc *m, sql_column *col, char *check);
229 : extern int mvc_drop_default(mvc *c, sql_column *col);
230 : extern int mvc_storage(mvc *c, sql_column *col, char *storage);
231 : extern int mvc_access(mvc *m, sql_table *t, sht access);
232 : extern int mvc_is_sorted(mvc *c, sql_column *col);
233 : extern int mvc_is_unique(mvc *m, sql_column *col);
234 : extern int mvc_is_duplicate_eliminated(mvc *c, sql_column *col);
235 : extern int mvc_col_stats(mvc *m, sql_column *col, bool *nonil, bool *unique, double *unique_est, ValPtr min, ValPtr max);
236 :
237 : extern int mvc_create_ukey(sql_key **kres, mvc *m, sql_table *t, const char *name, key_type kt, const char* check);
238 : extern int mvc_create_fkey(sql_fkey **kres, mvc *m, sql_table *t, const char *name, key_type kt, sql_key *rkey, int on_delete, int on_update);
239 : extern int mvc_create_kc(mvc *m, sql_key *k, sql_column *c);
240 : extern int mvc_create_fkc(mvc *m, sql_fkey *fk, sql_column *c);
241 : extern int mvc_create_key_done(mvc *m, sql_key *k);
242 :
243 : extern int mvc_drop_key(mvc *c, sql_schema *s, sql_key *key, int drop_action);
244 :
245 : extern int mvc_create_idx(sql_idx **i, mvc *m, sql_table *t, const char *name, idx_type it);
246 : extern int mvc_create_ic(mvc *m, sql_idx * i, sql_column *c);
247 : extern int mvc_create_idx_done(mvc *m, sql_idx *i);
248 : extern int mvc_drop_idx(mvc *c, sql_schema *s, sql_idx * i);
249 :
250 : extern int mvc_create_trigger(sql_trigger **tri, 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);
251 : extern int mvc_drop_trigger(mvc *m, sql_schema *s, sql_trigger * tri);
252 :
253 : /*dependency control*/
254 : extern int mvc_create_dependency(mvc *m, sql_base *b, sqlid depend_id, sql_dependency depend_type);
255 : extern int mvc_create_dependencies(mvc *m, list *blist, sqlid depend_id, sql_dependency dep_type);
256 : extern int mvc_check_dependency(mvc *m, sqlid id, sql_dependency type, list *ignore_ids);
257 :
258 : /* variable management */
259 : extern int init_global_variables(mvc *sql);
260 : extern sql_var *find_global_var(mvc *sql, sql_schema *s, const char *name);
261 : extern sql_var *push_global_var(mvc *sql, const char *sname, const char *name, sql_subtype *type);
262 : extern int remove_global_var(mvc *sql, sql_schema *s, const char *name);
263 :
264 : extern sql_var* frame_push_var(mvc *sql, const char *name, sql_subtype *type);
265 : extern sql_local_table* frame_push_table(mvc *sql, sql_table *t);
266 : extern sql_rel_view* stack_push_rel_view(mvc *sql, const char *name, sql_rel *var);
267 : extern sql_window_definition* frame_push_window_def(mvc *sql, const char *name, dlist *wdef);
268 : extern dlist* frame_get_window_def(mvc *sql, const char *name, int *pos);
269 : extern sql_groupby_expression* frame_push_groupby_expression(mvc *sql, symbol *def, sql_exp *exp);
270 : extern sql_exp* frame_get_groupby_expression(mvc *sql, symbol *def);
271 : extern void stack_update_rel_view(mvc *sql, const char *name, sql_rel *view);
272 :
273 : extern bool frame_check_var_visited(mvc *sql, int i);
274 : extern void frame_set_var_visited(mvc *sql, int i);
275 : extern void frame_clear_visited_flag(mvc *sql);
276 :
277 : extern sql_frame *stack_push_frame(mvc *sql, const char *name);
278 : extern void stack_pop_frame(mvc *sql);
279 : extern void clear_frame(mvc *sql, sql_frame *frame);
280 : extern void stack_pop_until(mvc *sql, int frame);
281 :
282 : /* find variable in the stack */
283 : extern sql_var *stack_find_var_frame(mvc *sql, const char *name, int *level);
284 : extern sql_var *stack_find_var_at_level(mvc *sql, const char *name, int level);
285 : extern sql_table *stack_find_table(mvc *sql, const char *name);
286 : extern sql_table *frame_find_table(mvc *sq, const char *name);
287 : extern sql_rel *stack_find_rel_view(mvc *sql, const char *name);
288 : extern int stack_find_rel_view_projection_columns(mvc *sql, const char *name, sql_rel **res);
289 :
290 : /* find variable in the current frame */
291 : extern int frame_find_var(mvc *sql, const char *name);
292 : extern sql_rel *frame_find_rel_view(mvc *sql, const char *name);
293 :
294 : extern int stack_has_frame(mvc *sql, const char *name);
295 : extern int stack_nr_of_declared_tables(mvc *sql);
296 :
297 : extern atom *sqlvar_set(sql_var *var, ValRecord *v);
298 : extern str sqlvar_get_string(sql_var *var);
299 : extern str sqlvar_set_string(sql_var *var, const char *v);
300 : #ifdef HAVE_HGE
301 : extern hge val_get_number(const ValRecord *val);
302 : extern void sqlvar_set_number(sql_var *var, hge v);
303 : #else
304 : extern lng val_get_number(const ValRecord *val);
305 : extern void sqlvar_set_number(sql_var *var, lng v);
306 : #endif
307 :
308 : #define get_string_global_var(m, val) (sqlvar_get_string(find_global_var(m, mvc_bind_schema(m, "sys"), val)))
309 :
310 : extern int mvc_copy_column(mvc *m, sql_table *t, sql_column *c, sql_column **cres);
311 : extern int mvc_copy_key(mvc *m, sql_table *t, sql_key *k, sql_key **kres);
312 : extern int mvc_copy_idx(mvc *m, sql_table *t, sql_idx *i, sql_idx **ires);
313 : extern int mvc_copy_trigger(mvc *m, sql_table *t, sql_trigger *tr, sql_trigger **tres);
314 :
315 : extern sql_rel *sql_processrelation(mvc *sql, sql_rel *rel, int profile, int instantiate, int value_based_opt, int storage_based_opt);
316 :
317 : extern void *sql_error(mvc *sql, int error_code, _In_z_ _Printf_format_string_ char *format, ...)
318 : __attribute__((__format__(__printf__, 3, 4)));
319 :
320 : extern int symbol_cmp(mvc* sql, symbol *s1, symbol *s2);
321 :
322 541539616 : static inline int mvc_highwater(mvc *sql)
323 : {
324 541539616 : int rc = 0;
325 : #ifdef __has_builtin
326 : #if __has_builtin(__builtin_frame_address)
327 : /* we can improve on the previous assignment */
328 541539616 : uintptr_t c = (uintptr_t) __builtin_frame_address(0);
329 : #define BUILTIN_USED
330 : #endif
331 : #endif
332 : #ifndef BUILTIN_USED
333 : uintptr_t c = (uintptr_t) (&rc);
334 : #endif
335 : #undef BUILTIN_USED
336 :
337 541553673 : size_t diff = c < sql->sp ? sql->sp - c : c - sql->sp;
338 541553673 : if (diff > THREAD_STACK_SIZE - 280 * 1024)
339 183 : rc = 1;
340 541553707 : return rc;
341 : }
342 :
343 : #endif /*_SQL_MVC_H*/
|