LCOV - code coverage report
Current view: top level - sql/server - sql_mvc.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 3 100.0 %
Date: 2021-10-13 02:24:04 Functions: 0 0 -

          Line data    Source code
       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 - 2021 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 "mapi_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             : #define ERR_NOTFOUND    070000
      32             : 
      33             : /* value vs predicate (boolean) */
      34             : #define type_value      0
      35             : #define type_relation   1
      36             : 
      37             : /* cardinality expected by enclosing operator */
      38             : #define card_none       -1      /* psm call doesn't return anything */
      39             : #define card_value      0
      40             : #define card_row        1 /* needed for subqueries on single value tables (select (select 1))*/
      41             : #define card_column     2
      42             : #define card_set        3 /* some operators require only a set (IN/EXISTS) */
      43             : #define card_exists     4
      44             : /* to be removed ie are in type (aka dimention) */
      45             : #define card_relation   5
      46             : #define card_loader     6
      47             : 
      48             : #define CARD_VALUE(card) (card == card_value || card == card_row || card == card_column || card == card_set || card == card_exists)
      49             : 
      50             : /* allowed to reduce (in the where and having parts we can reduce) */
      51             : 
      52             : /* different query execution modes (emode) */
      53             : #define m_normal        0
      54             : #define m_deallocate 1
      55             : #define m_prepare       2
      56             : #define m_plan          3
      57             : 
      58             : /* special modes for function/procedure and view instantiation and
      59             :    dependency generation */
      60             : #define m_instantiate   5
      61             : #define m_deps          6
      62             : 
      63             : /* different query execution modifiers (emod) */
      64             : #define mod_none        0
      65             : #define mod_debug       1
      66             : #define mod_trace       2
      67             : #define mod_explain     4
      68             : #define mod_exec        8
      69             : 
      70             : #define sql_shared_module_name "sql"
      71             : #define sql_private_module_name "user"
      72             : 
      73             : typedef struct sql_groupby_expression {
      74             :         symbol *sdef;
      75             :         tokens token;
      76             :         sql_exp *exp;
      77             : } sql_groupby_expression;
      78             : 
      79             : typedef struct sql_window_definition {
      80             :         char *name;
      81             :         dlist *wdef;
      82             :         bool visited; /* used for window definitions lookup */
      83             : } sql_window_definition;
      84             : 
      85             : typedef struct sql_local_table { /* declared tables during session */
      86             :         sql_table *table;
      87             : } sql_local_table;
      88             : 
      89             : typedef struct sql_rel_view { /* CTEs */
      90             :         char *name;
      91             :         sql_rel *rel_view;
      92             : } sql_rel_view;
      93             : 
      94             : typedef struct sql_var { /* Declared variables and parameters */
      95             :         char *sname; /* Global variables have a schema */
      96             :         char *name;
      97             :         atom var;
      98             : } sql_var;
      99             : 
     100             : typedef struct sql_frame {
     101             :         char *name; /* frame name */
     102             :         list *group_expressions;
     103             :         list *windows;
     104             :         list *tables;
     105             :         list *rel_views;
     106             :         list *vars;
     107             :         int frame_number;
     108             : } sql_frame;
     109             : 
     110             : typedef struct mvc {
     111             :         char errstr[ERRSIZE];
     112             : 
     113             :         sql_allocator *sa, *ta, *pa;
     114             : 
     115             :         struct scanner scanner;
     116             : 
     117             :         list *params;
     118             :         sql_func *forward;      /* forward definitions for recursive functions */
     119             :         list *global_vars; /* SQL declared variables on the global scope */
     120             :         sql_frame **frames;     /* stack of frames with variables */
     121             :         int topframes;
     122             :         int sizeframes;
     123             :         int frame;
     124             :         struct symbol *sym;
     125             : 
     126             :         int8_t use_views:1,
     127             :                    schema_path_has_sys:1, /* speed up object search */
     128             :                    schema_path_has_tmp:1;
     129             :         struct qc *qc;
     130             :         int clientid;           /* id of the owner */
     131             : 
     132             :         /* session variables */
     133             :         sqlid user_id;
     134             :         sqlid role_id;
     135             :         int timezone;           /* milliseconds west of UTC */
     136             :         int reply_size;         /* reply size */
     137             :         int debug;
     138             : 
     139             :         char emode;             /* execution mode */
     140             :         char emod;              /* execution modifier */
     141             : 
     142             :         sql_session *session;
     143             :         sql_store store;
     144             : 
     145             :         /* per query context */
     146             :         mapi_query_t type;      /* query type */
     147             : 
     148             :         /* during query needed flags */
     149             :         unsigned int label;     /* numbers for relational projection labels */
     150             :         list *cascade_action;  /* protection against recursive cascade actions */
     151             :         list *schema_path; /* schema search path for object lookup */
     152             :         uintptr_t sp;
     153             : } mvc;
     154             : 
     155             : extern sql_table *mvc_init_create_view(mvc *sql, sql_schema *s, const char *name, const char *query);
     156             : 
     157             : /* should return structure */
     158             : extern sql_store mvc_init(int debug, store_type store, int ro, int su);
     159             : extern void mvc_exit(sql_store store);
     160             : 
     161             : extern void mvc_logmanager(sql_store store);
     162             : 
     163             : extern mvc *mvc_create(sql_store *store, sql_allocator *pa, int clientid, int debug, bstream *rs, stream *ws);
     164             : extern int mvc_reset(mvc *m, bstream *rs, stream *ws, int debug);
     165             : extern void mvc_destroy(mvc *c);
     166             : 
     167             : extern int mvc_status(mvc *c);
     168             : extern int mvc_error_retry(mvc *c); // error code on errors else 0, errors AMBIGUOUS and GROUPBY will also output 0
     169             : extern int mvc_type(mvc *c);
     170             : extern int mvc_debug_on(mvc *m, int flag);
     171             : extern void mvc_cancel_session(mvc *m);
     172             : 
     173             : /* since Savepoints and transactions are related the
     174             :  * commit function includes the savepoint creation.
     175             :  * Rollbacks can be either full or until a given savepoint.
     176             :  * The special mvc_release can be used to release savepoints.
     177             :  */
     178             : #define has_snapshots(tr) ((tr) && (tr)->parent && (tr)->parent->parent)
     179             : 
     180             : extern int mvc_trans(mvc *c);
     181             : sql_export str mvc_commit(mvc *c, int chain, const char *name, bool enabling_auto_commit);
     182             : sql_export str mvc_rollback(mvc *c, int chain, const char *name, bool disabling_auto_commit);
     183             : extern str mvc_release(mvc *c, const char *name);
     184             : 
     185             : extern sql_type *mvc_bind_type(mvc *sql, const char *name);
     186             : extern sql_type *schema_bind_type(mvc *sql, sql_schema * s, const char *name);
     187             : 
     188             : sql_export sql_schema *mvc_bind_schema(mvc *c, const char *sname);
     189             : sql_export sql_table *mvc_bind_table(mvc *c, sql_schema *s, const char *tname);
     190             : extern sql_column *mvc_bind_column(mvc *c, sql_table *t, const char *cname);
     191             : extern sql_column *mvc_first_column(mvc *c, sql_table *t);
     192             : extern sql_idx *mvc_bind_idx(mvc *c, sql_schema *s, const char *iname);
     193             : extern sql_key *mvc_bind_key(mvc *c, sql_schema *s, const char *kname);
     194             : extern sql_key *mvc_bind_ukey(sql_table *t, list *cols);
     195             : extern sql_trigger *mvc_bind_trigger(mvc *c, sql_schema *s, const char *tname);
     196             : 
     197             : extern int mvc_create_type(mvc *sql, sql_schema *s, const char *sqlname, unsigned int digits, unsigned int scale, int radix, const char *impl);
     198             : extern int mvc_drop_type(mvc *sql, sql_schema *s, sql_type *t, int drop_action);
     199             : 
     200             : extern int mvc_create_func(sql_func **f, mvc *m, 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);
     201             : extern int mvc_drop_func(mvc *c, sql_schema *s, sql_func * func, int drop_action);
     202             : extern int mvc_drop_all_func(mvc *c, sql_schema *s, list *list_func, int drop_action);
     203             : 
     204             : extern int mvc_drop_schema(mvc *c, sql_schema *s, int drop_action);
     205             : extern int mvc_create_schema(mvc *m, const char *name, sqlid auth_id, sqlid owner);
     206             : extern BUN mvc_clear_table(mvc *m, sql_table *t);
     207             : extern str mvc_drop_table(mvc *c, sql_schema *s, sql_table * t, int drop_action);
     208             : extern 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);
     209             : extern int mvc_create_view(sql_table **t, mvc *m, sql_schema *s, const char *name, int persistence, const char *sql, bit system);
     210             : extern int mvc_create_remote(sql_table **t, mvc *m, sql_schema *s, const char *name, int persistence, const char *loc);
     211             : 
     212             : extern int mvc_drop_column(mvc *c, sql_table *t, sql_column *col, int drop_action);
     213             : sql_export int mvc_create_column(sql_column **col, mvc *m, sql_table *t, const char *name, sql_subtype *tpe);
     214             : extern int mvc_create_column_(sql_column **col, mvc *m, sql_table *t, const char *name, const char *type, unsigned int digits);
     215             : extern int mvc_null(mvc *c, sql_column *col, int flag);
     216             : extern int mvc_default(mvc *c, sql_column *col, char *val);
     217             : extern int mvc_drop_default(mvc *c, sql_column *col);
     218             : extern int mvc_storage(mvc *c, sql_column *col, char *storage);
     219             : extern int mvc_access(mvc *m, sql_table *t, sht access);
     220             : extern int mvc_is_sorted(mvc *c, sql_column *col);
     221             : extern int mvc_is_unique(mvc *m, sql_column *col);
     222             : extern int mvc_is_duplicate_eliminated(mvc *c, sql_column *col);
     223             : 
     224             : extern int mvc_create_ukey(sql_key **kres, mvc *m, sql_table *t, const char *name, key_type kt);
     225             : 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);
     226             : extern int mvc_create_kc(mvc *m, sql_key *k, sql_column *c);
     227             : extern int mvc_create_fkc(mvc *m, sql_fkey *fk, sql_column *c);
     228             : extern int mvc_create_key_done(mvc *m, sql_key *k);
     229             : 
     230             : extern int mvc_drop_key(mvc *c, sql_schema *s, sql_key *key, int drop_action);
     231             : 
     232             : extern int mvc_create_idx(sql_idx **i, mvc *m, sql_table *t, const char *name, idx_type it);
     233             : extern int mvc_create_ic(mvc *m, sql_idx * i, sql_column *c);
     234             : extern int mvc_create_idx_done(mvc *m, sql_idx *i);
     235             : extern int mvc_drop_idx(mvc *c, sql_schema *s, sql_idx * i);
     236             : 
     237             : 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);
     238             : extern int mvc_drop_trigger(mvc *m, sql_schema *s, sql_trigger * tri);
     239             : 
     240             : /*dependency control*/
     241             : extern int mvc_create_dependency(mvc *m, sql_base *b, sqlid depend_id, sql_dependency depend_type);
     242             : extern int mvc_create_dependencies(mvc *m, list *blist, sqlid depend_id, sql_dependency dep_type);
     243             : extern int mvc_check_dependency(mvc *m, sqlid id, sql_dependency type, list *ignore_ids);
     244             : 
     245             : /* variable management */
     246             : extern int init_global_variables(mvc *sql);
     247             : extern sql_var *find_global_var(mvc *sql, sql_schema *s, const char *name);
     248             : extern sql_var *push_global_var(mvc *sql, const char *sname, const char *name, sql_subtype *type);
     249             : extern int remove_global_var(mvc *sql, sql_schema *s, const char *name);
     250             : 
     251             : extern sql_var* frame_push_var(mvc *sql, const char *name, sql_subtype *type);
     252             : extern sql_local_table* frame_push_table(mvc *sql, sql_table *t);
     253             : extern sql_rel_view* stack_push_rel_view(mvc *sql, const char *name, sql_rel *var);
     254             : extern sql_window_definition* frame_push_window_def(mvc *sql, const char *name, dlist *wdef);
     255             : extern dlist* frame_get_window_def(mvc *sql, const char *name, int *pos);
     256             : extern sql_groupby_expression* frame_push_groupby_expression(mvc *sql, symbol *def, sql_exp *exp);
     257             : extern sql_exp* frame_get_groupby_expression(mvc *sql, symbol *def);
     258             : extern void stack_update_rel_view(mvc *sql, const char *name, sql_rel *view);
     259             : 
     260             : extern bool frame_check_var_visited(mvc *sql, int i);
     261             : extern void frame_set_var_visited(mvc *sql, int i);
     262             : extern void frame_clear_visited_flag(mvc *sql);
     263             : 
     264             : extern sql_frame *stack_push_frame(mvc *sql, const char *name);
     265             : extern void stack_pop_frame(mvc *sql);
     266             : extern void clear_frame(mvc *sql, sql_frame *frame);
     267             : extern void stack_pop_until(mvc *sql, int frame);
     268             : 
     269             : /* find variable in the stack */
     270             : extern sql_var *stack_find_var_frame(mvc *sql, const char *name, int *level);
     271             : extern sql_table *stack_find_table(mvc *sql, const char *name);
     272             : extern sql_table *frame_find_table(mvc *sq, const char *name);
     273             : extern sql_rel *stack_find_rel_view(mvc *sql, const char *name);
     274             : extern int stack_find_rel_view_projection_columns(mvc *sql, const char *name, sql_rel **res);
     275             : 
     276             : /* find variable in the current frame */
     277             : extern int frame_find_var(mvc *sql, const char *name);
     278             : extern sql_rel *frame_find_rel_view(mvc *sql, const char *name);
     279             : 
     280             : extern int stack_has_frame(mvc *sql, const char *name);
     281             : extern int stack_nr_of_declared_tables(mvc *sql);
     282             : 
     283             : extern atom *sqlvar_set(sql_var *var, ValRecord *v);
     284             : extern str sqlvar_get_string(sql_var *var);
     285             : extern str sqlvar_set_string(sql_var *var, const char *v);
     286             : #ifdef HAVE_HGE
     287             : extern hge val_get_number(ValRecord *val);
     288             : extern void sqlvar_set_number(sql_var *var, hge v);
     289             : #else
     290             : extern lng val_get_number(ValRecord *val);
     291             : extern void sqlvar_set_number(sql_var *var, lng v);
     292             : #endif
     293             : 
     294             : #define get_string_global_var(m, val) (sqlvar_get_string(find_global_var(m, mvc_bind_schema(m, "sys"), val)))
     295             : 
     296             : extern int mvc_copy_column(mvc *m, sql_table *t, sql_column *c, sql_column **cres);
     297             : extern int mvc_copy_key(mvc *m, sql_table *t, sql_key *k, sql_key **kres);
     298             : extern int mvc_copy_idx(mvc *m, sql_table *t, sql_idx *i, sql_idx **ires);
     299             : extern int mvc_copy_trigger(mvc *m, sql_table *t, sql_trigger *tr, sql_trigger **tres);
     300             : 
     301             : extern sql_rel *sql_processrelation(mvc *sql, sql_rel* rel, int value_based_opt, int storage_based_opt);
     302             : 
     303             : extern void *sql_error(mvc *sql, int error_code, _In_z_ _Printf_format_string_ char *format, ...)
     304             :         __attribute__((__format__(__printf__, 3, 4)));
     305             : 
     306             : extern int symbol_cmp(mvc* sql, symbol *s1, symbol *s2);
     307             : 
     308             : static inline int mvc_highwater(mvc *sql)
     309             : {
     310             :         int l = 0, rc = 0;
     311   799836588 :         uintptr_t c = (uintptr_t) (&l);
     312             : 
     313   799836588 :         size_t diff = c < sql->sp ? sql->sp - c : c - sql->sp;
     314   799836588 :         if (diff > THREAD_STACK_SIZE - 280 * 1024)
     315             :                 rc = 1;
     316             :         return rc;
     317             : }
     318             : 
     319             : #endif /*_SQL_MVC_H*/

Generated by: LCOV version 1.14