LCOV - code coverage report
Current view: top level - sql/server - sql_mvc.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 7 100.0 %
Date: 2024-04-26 00:35:57 Functions: 0 0 -

          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 dimention) */
      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             :         int reply_size;         /* reply size */
     148             :         int debug;
     149             :         int sql_optimizer; /* SQL optimizer mask */
     150             :         sql_optimizer_run *runs; /* Information about SQL optimizer runs */
     151             : 
     152             :         char emode;             /* execution mode */
     153             :         char emod;              /* execution modifier */
     154             : 
     155             :         sql_session *session;
     156             :         sql_store store;
     157             : 
     158             :         /* per query context */
     159             :         mapi_query_t type;      /* query type */
     160             : 
     161             :         /* during query needed flags */
     162             :         unsigned int label;     /* numbers for relational projection labels */
     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);
     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_drop_default(mvc *c, sql_column *col);
     229             : extern int mvc_storage(mvc *c, sql_column *col, char *storage);
     230             : extern int mvc_access(mvc *m, sql_table *t, sht access);
     231             : extern int mvc_is_sorted(mvc *c, sql_column *col);
     232             : extern int mvc_is_unique(mvc *m, sql_column *col);
     233             : extern int mvc_is_duplicate_eliminated(mvc *c, sql_column *col);
     234             : extern int mvc_col_stats(mvc *m, sql_column *col, bool *nonil, bool *unique, double *unique_est, ValPtr min, ValPtr max);
     235             : 
     236             : extern int mvc_create_ukey(sql_key **kres, mvc *m, sql_table *t, const char *name, key_type kt);
     237             : 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);
     238             : extern int mvc_create_kc(mvc *m, sql_key *k, sql_column *c);
     239             : extern int mvc_create_fkc(mvc *m, sql_fkey *fk, sql_column *c);
     240             : extern int mvc_create_key_done(mvc *m, sql_key *k);
     241             : 
     242             : extern int mvc_drop_key(mvc *c, sql_schema *s, sql_key *key, int drop_action);
     243             : 
     244             : extern int mvc_create_idx(sql_idx **i, mvc *m, sql_table *t, const char *name, idx_type it);
     245             : extern int mvc_create_ic(mvc *m, sql_idx * i, sql_column *c);
     246             : extern int mvc_create_idx_done(mvc *m, sql_idx *i);
     247             : extern int mvc_drop_idx(mvc *c, sql_schema *s, sql_idx * i);
     248             : 
     249             : 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);
     250             : extern int mvc_drop_trigger(mvc *m, sql_schema *s, sql_trigger * tri);
     251             : 
     252             : /*dependency control*/
     253             : extern int mvc_create_dependency(mvc *m, sql_base *b, sqlid depend_id, sql_dependency depend_type);
     254             : extern int mvc_create_dependencies(mvc *m, list *blist, sqlid depend_id, sql_dependency dep_type);
     255             : extern int mvc_check_dependency(mvc *m, sqlid id, sql_dependency type, list *ignore_ids);
     256             : 
     257             : /* variable management */
     258             : extern int init_global_variables(mvc *sql);
     259             : extern sql_var *find_global_var(mvc *sql, sql_schema *s, const char *name);
     260             : extern sql_var *push_global_var(mvc *sql, const char *sname, const char *name, sql_subtype *type);
     261             : extern int remove_global_var(mvc *sql, sql_schema *s, const char *name);
     262             : 
     263             : extern sql_var* frame_push_var(mvc *sql, const char *name, sql_subtype *type);
     264             : extern sql_local_table* frame_push_table(mvc *sql, sql_table *t);
     265             : extern sql_rel_view* stack_push_rel_view(mvc *sql, const char *name, sql_rel *var);
     266             : extern sql_window_definition* frame_push_window_def(mvc *sql, const char *name, dlist *wdef);
     267             : extern dlist* frame_get_window_def(mvc *sql, const char *name, int *pos);
     268             : extern sql_groupby_expression* frame_push_groupby_expression(mvc *sql, symbol *def, sql_exp *exp);
     269             : extern sql_exp* frame_get_groupby_expression(mvc *sql, symbol *def);
     270             : extern void stack_update_rel_view(mvc *sql, const char *name, sql_rel *view);
     271             : 
     272             : extern bool frame_check_var_visited(mvc *sql, int i);
     273             : extern void frame_set_var_visited(mvc *sql, int i);
     274             : extern void frame_clear_visited_flag(mvc *sql);
     275             : 
     276             : extern sql_frame *stack_push_frame(mvc *sql, const char *name);
     277             : extern void stack_pop_frame(mvc *sql);
     278             : extern void clear_frame(mvc *sql, sql_frame *frame);
     279             : extern void stack_pop_until(mvc *sql, int frame);
     280             : 
     281             : /* find variable in the stack */
     282             : extern sql_var *stack_find_var_frame(mvc *sql, const char *name, int *level);
     283             : extern sql_var *stack_find_var_at_level(mvc *sql, const char *name, int level);
     284             : extern sql_table *stack_find_table(mvc *sql, const char *name);
     285             : extern sql_table *frame_find_table(mvc *sq, const char *name);
     286             : extern sql_rel *stack_find_rel_view(mvc *sql, const char *name);
     287             : extern int stack_find_rel_view_projection_columns(mvc *sql, const char *name, sql_rel **res);
     288             : 
     289             : /* find variable in the current frame */
     290             : extern int frame_find_var(mvc *sql, const char *name);
     291             : extern sql_rel *frame_find_rel_view(mvc *sql, const char *name);
     292             : 
     293             : extern int stack_has_frame(mvc *sql, const char *name);
     294             : extern int stack_nr_of_declared_tables(mvc *sql);
     295             : 
     296             : extern atom *sqlvar_set(sql_var *var, ValRecord *v);
     297             : extern str sqlvar_get_string(sql_var *var);
     298             : extern str sqlvar_set_string(sql_var *var, const char *v);
     299             : #ifdef HAVE_HGE
     300             : extern hge val_get_number(const ValRecord *val);
     301             : extern void sqlvar_set_number(sql_var *var, hge v);
     302             : #else
     303             : extern lng val_get_number(const ValRecord *val);
     304             : extern void sqlvar_set_number(sql_var *var, lng v);
     305             : #endif
     306             : 
     307             : #define get_string_global_var(m, val) (sqlvar_get_string(find_global_var(m, mvc_bind_schema(m, "sys"), val)))
     308             : 
     309             : extern int mvc_copy_column(mvc *m, sql_table *t, sql_column *c, sql_column **cres);
     310             : extern int mvc_copy_key(mvc *m, sql_table *t, sql_key *k, sql_key **kres);
     311             : extern int mvc_copy_idx(mvc *m, sql_table *t, sql_idx *i, sql_idx **ires);
     312             : extern int mvc_copy_trigger(mvc *m, sql_table *t, sql_trigger *tr, sql_trigger **tres);
     313             : 
     314             : extern sql_rel *sql_processrelation(mvc *sql, sql_rel *rel, int profile, int instantiate, int value_based_opt, int storage_based_opt);
     315             : 
     316             : extern void *sql_error(mvc *sql, int error_code, _In_z_ _Printf_format_string_ char *format, ...)
     317             :         __attribute__((__format__(__printf__, 3, 4)));
     318             : 
     319             : extern int symbol_cmp(mvc* sql, symbol *s1, symbol *s2);
     320             : 
     321   560167624 : static inline int mvc_highwater(mvc *sql)
     322             : {
     323             : #if defined(__GNUC__) || defined(__clang__)
     324   560167624 :         uintptr_t c = (uintptr_t) __builtin_frame_address(0);
     325             : #else
     326             :         int l = 0;
     327             :         uintptr_t c = (uintptr_t) (&l);
     328             : #endif
     329   560183071 :         int rc = 0;
     330             : 
     331   560183071 :         size_t diff = c < sql->sp ? sql->sp - c : c - sql->sp;
     332   560183071 :         if (diff > THREAD_STACK_SIZE - 280 * 1024)
     333         506 :                 rc = 1;
     334   560183018 :         return rc;
     335             : }
     336             : 
     337             : #endif /*_SQL_MVC_H*/

Generated by: LCOV version 1.14