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
69typedef struct sql_groupby_expression {
70 symbol *sdef;
71 tokens token;
72 sql_exp *exp;
73} sql_groupby_expression;
74
75typedef 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
89typedef 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 sizeheader; /* 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
144extern sql_table *mvc_init_create_view(mvc *sql, sql_schema *s, const char *name, const char *query);
145extern int mvc_init(int debug, store_type store, int ro, int su, backend_stack stk);
146extern void mvc_exit(void);
147extern void mvc_logmanager(void);
148extern void mvc_idlemanager(void);
149
150extern mvc *mvc_create(int clientid, backend_stack stk, int debug, bstream *rs, stream *ws);
151extern int mvc_reset(mvc *m, bstream *rs, stream *ws, int debug, int globalvars);
152extern void mvc_destroy(mvc *c);
153
154extern int mvc_status(mvc *c);
155extern int mvc_error_retry(mvc *c); // error code on errors else 0, errors AMBIGUOUS and GROUPBY will also output 0
156extern int mvc_type(mvc *c);
157extern int mvc_debug_on(mvc *m, int flag);
158extern 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
167extern int mvc_trans(mvc *c);
168extern str mvc_commit(mvc *c, int chain, const char *name, bool enabling_auto_commit);
169extern str mvc_rollback(mvc *c, int chain, const char *name, bool disabling_auto_commit);
170extern str mvc_release(mvc *c, const char *name);
171
172extern sql_type *mvc_bind_type(mvc *sql, const char *name);
173extern sql_type *schema_bind_type(mvc *sql, sql_schema * s, const char *name);
174extern sql_func *mvc_bind_func(mvc *sql, const char *name);
175extern list *schema_bind_func(mvc *sql, sql_schema * s, const char *name, sql_ftype type);
176
177extern sql_schema *mvc_bind_schema(mvc *c, const char *sname);
178extern sql_table *mvc_bind_table(mvc *c, sql_schema *s, const char *tname);
179extern sql_column *mvc_bind_column(mvc *c, sql_table *t, const char *cname);
180extern sql_column *mvc_first_column(mvc *c, sql_table *t);
181extern sql_idx *mvc_bind_idx(mvc *c, sql_schema *s, const char *iname);
182extern sql_key *mvc_bind_key(mvc *c, sql_schema *s, const char *kname);
183extern sql_key *mvc_bind_ukey(sql_table *t, list *cols);
184extern sql_trigger *mvc_bind_trigger(mvc *c, sql_schema *s, const char *tname);
185
186extern sql_type *mvc_create_type(mvc *sql, sql_schema *s, const char *sqlname, int digits, int scale, int radix, const char *impl);
187extern int mvc_drop_type(mvc *sql, sql_schema *s, sql_type *t, int drop_action);
188
189extern 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);
190extern int mvc_drop_func(mvc *c, sql_schema *s, sql_func * func, int drop_action);
191extern int mvc_drop_all_func(mvc *c, sql_schema *s, list *list_func, int drop_action);
192
193extern int mvc_drop_schema(mvc *c, sql_schema *s, int drop_action);
194extern sql_schema *mvc_create_schema(mvc *m, const char *name, sqlid auth_id, sqlid owner);
195extern BUN mvc_clear_table(mvc *m, sql_table *t);
196extern str mvc_drop_table(mvc *c, sql_schema *s, sql_table * t, int drop_action);
197extern 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);
198extern sql_table *mvc_create_view(mvc *c, sql_schema *s, const char *name, int persistence, const char *sql, bit system);
199extern sql_table *mvc_create_remote(mvc *c, sql_schema *s, const char *name, int persistence, const char *loc);
200
201extern int mvc_drop_column(mvc *c, sql_table *t, sql_column *col, int drop_action);
202extern sql_column *mvc_create_column(mvc *c, sql_table *t, const char *name, sql_subtype *type);
203extern sql_column *mvc_create_column_(mvc *c, sql_table *t, const char *name, const char *type, int digits);
204extern sql_column *mvc_null(mvc *c, sql_column *col, int flag);
205extern sql_column *mvc_default(mvc *c, sql_column *col, char *val);
206extern sql_column *mvc_drop_default(mvc *c, sql_column *col);
207extern sql_column *mvc_storage(mvc *c, sql_column *col, char *storage);
208extern sql_table * mvc_access(mvc *m, sql_table *t, sht access);
209extern int mvc_is_sorted(mvc *c, sql_column *col);
210
211extern sql_ukey *mvc_create_ukey(mvc *m, sql_table *t, const char *kname, key_type kt);
212extern sql_key *mvc_create_ukey_done(mvc *m, sql_key *k);
213extern 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);
214extern sql_key *mvc_create_kc(mvc *m, sql_key *k, sql_column *c);
215extern sql_fkey *mvc_create_fkc(mvc *m, sql_fkey *fk, sql_column *c);
216
217extern int mvc_drop_key(mvc *c, sql_schema *s, sql_key *key, int drop_action);
218
219extern sql_idx *mvc_create_idx(mvc *m, sql_table *t, const char *iname, idx_type it);
220extern sql_idx *mvc_create_ic(mvc *m, sql_idx * i, sql_column *c);
221extern int mvc_drop_idx(mvc *c, sql_schema *s, sql_idx * i);
222
223extern 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 );
224extern sql_trigger * mvc_create_tc(mvc *m, sql_trigger * i, sql_column *c /*, extra options such as trunc */ );
225extern int mvc_drop_trigger(mvc *m, sql_schema *s, sql_trigger * tri);
226
227/*dependency control*/
228extern void mvc_create_dependency(mvc *m, sqlid id, sqlid depend_id, sql_dependency depend_type);
229extern void mvc_create_dependencies(mvc *m, list *id_l, sqlid depend_id, sql_dependency dep_type);
230extern int mvc_check_dependency(mvc *m, sqlid id, sql_dependency type, list *ignore_ids);
231
232/* variable management */
233extern sql_var* stack_push_var(mvc *sql, const char *name, sql_subtype *type);
234extern sql_var* stack_push_rel_var(mvc *sql, const char *name, sql_rel *var, sql_subtype *type);
235extern sql_var* stack_push_table(mvc *sql, const char *name, sql_rel *var, sql_table *t);
236extern sql_var* stack_push_rel_view(mvc *sql, const char *name, sql_rel *view);
237extern sql_var* stack_push_window_def(mvc *sql, const char *name, dlist *sym);
238extern dlist* stack_get_window_def(mvc *sql, const char *name, int *pos);
239extern sql_var* stack_push_groupby_expression(mvc *sql, symbol *def, sql_exp *exp);
240extern sql_exp* stack_get_groupby_expression(mvc *sql, symbol *def);
241extern void stack_update_rel_view(mvc *sql, const char *name, sql_rel *view);
242
243extern char stack_check_var_visited(mvc *sql, int i);
244extern void stack_set_var_visited(mvc *sql, int i);
245extern void stack_clear_frame_visited_flag(mvc *sql);
246
247extern sql_var* stack_push_frame(mvc *sql, const char *name);
248extern void stack_pop_frame(mvc *sql);
249extern void stack_pop_until(mvc *sql, int top);
250extern sql_subtype *stack_find_type(mvc *sql, const char *name);
251extern sql_table *stack_find_table(mvc *sql, const char *name);
252extern sql_rel *stack_find_rel_view(mvc *sql, const char *name);
253extern int stack_find_var(mvc *sql, const char *name);
254extern sql_rel *stack_find_rel_var(mvc *sql, const char *name);
255/* find var in current frame */
256extern int frame_find_var(mvc *sql, const char *name);
257/* find frame holding variable 'name' */
258extern int stack_find_frame(mvc *sql, const char *name);
259/* find frame with given name */
260extern int stack_has_frame(mvc *sql, const char *name);
261extern int stack_nr_of_declared_tables(mvc *sql);
262
263extern atom* stack_get_var(mvc *sql, const char *name);
264extern atom* stack_set_var(mvc *sql, const char *name, ValRecord *v);
265
266extern str stack_get_string(mvc *sql, const char *name);
267extern str stack_set_string(mvc *sql, const char *name, const char *v);
268#ifdef HAVE_HGE
269extern hge val_get_number(ValRecord *val);
270extern hge stack_get_number(mvc *sql, const char *name);
271extern void stack_set_number(mvc *sql, const char *name, hge v);
272#else
273extern lng val_get_number(ValRecord *val);
274extern lng stack_get_number(mvc *sql, const char *name);
275extern void stack_set_number(mvc *sql, const char *name, lng v);
276#endif
277
278extern sql_column *mvc_copy_column(mvc *m, sql_table *t, sql_column *c);
279extern sql_key *mvc_copy_key(mvc *m, sql_table *t, sql_key *k);
280extern sql_idx *mvc_copy_idx(mvc *m, sql_table *t, sql_idx *i);
281extern sql_trigger *mvc_copy_trigger(mvc *m, sql_table *t, sql_trigger *tr);
282extern sql_part *mvc_copy_part(mvc *m, sql_table *t, sql_part *pt);
283
284extern void *sql_error(mvc *sql, int error_code, _In_z_ _Printf_format_string_ char *format, ...)
285 __attribute__((__format__(__printf__, 3, 4)));
286
287extern int symbol_cmp(mvc* sql, symbol *s1, symbol *s2);
288
289#endif /*_SQL_MVC_H*/
290