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-25 20:03:45 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             :         sql_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             :         struct qc *qc;
     140             :         int clientid;           /* id of the owner */
     141             : 
     142             :         /* session variables */
     143             :         sqlid user_id;
     144             :         sqlid role_id;
     145             :         int timezone;           /* milliseconds west of UTC */
     146             :         int reply_size;         /* reply size */
     147             :         int debug;
     148             :         int sql_optimizer; /* SQL optimizer mask */
     149             :         sql_optimizer_run *runs; /* Information about SQL optimizer runs */
     150             : 
     151             :         char emode;             /* execution mode */
     152             :         char emod;              /* execution modifier */
     153             : 
     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             :         list *cascade_action;  /* protection against recursive cascade actions */
     163             :         list *schema_path; /* schema search path for object lookup */
     164             :         uintptr_t sp;
     165             : } mvc;
     166             : 
     167             : /* should return structure */
     168             : extern sql_store mvc_init(int debug, store_type store, int ro, int su, const char *initpasswd);
     169             : extern void mvc_exit(sql_store store);
     170             : 
     171             : extern void mvc_logmanager(sql_store store);
     172             : 
     173             : extern mvc *mvc_create(sql_store *store, sql_allocator *pa, int clientid, int debug, bstream *rs, stream *ws);
     174             : extern void mvc_destroy(mvc *c);
     175             : 
     176             : extern int mvc_status(mvc *c);
     177             : extern int mvc_error_retry(mvc *c); // error code on errors else 0, errors AMBIGUOUS and GROUPBY will also output 0
     178             : extern int mvc_type(mvc *c);
     179             : extern int mvc_debug_on(mvc *m, int flag);
     180             : extern void mvc_cancel_session(mvc *m);
     181             : 
     182             : /* since Savepoints and transactions are related the
     183             :  * commit function includes the savepoint creation.
     184             :  * Rollbacks can be either full or until a given savepoint.
     185             :  * The special mvc_release can be used to release savepoints.
     186             :  */
     187             : #define has_snapshots(tr) ((tr) && (tr)->parent && (tr)->parent->parent)
     188             : 
     189             : extern int mvc_trans(mvc *c);
     190             : sql_export str mvc_commit(mvc *c, int chain, const char *name, bool enabling_auto_commit);
     191             : sql_export str mvc_rollback(mvc *c, int chain, const char *name, bool disabling_auto_commit);
     192             : extern str mvc_release(mvc *c, const char *name);
     193             : 
     194             : extern sql_type *mvc_bind_type(mvc *sql, const char *name);
     195             : extern sql_type *schema_bind_type(mvc *sql, sql_schema * s, const char *name);
     196             : 
     197             : sql_export sql_schema *mvc_bind_schema(mvc *c, const char *sname);
     198             : sql_export sql_table *mvc_bind_table(mvc *c, sql_schema *s, const char *tname);
     199             : extern sql_column *mvc_bind_column(mvc *c, sql_table *t, const char *cname);
     200             : extern sql_column *mvc_first_column(mvc *c, sql_table *t);
     201             : extern sql_idx *mvc_bind_idx(mvc *c, sql_schema *s, const char *iname);
     202             : extern sql_key *mvc_bind_key(mvc *c, sql_schema *s, const char *kname);
     203             : extern sql_key *mvc_bind_ukey(sql_table *t, list *cols);
     204             : extern sql_trigger *mvc_bind_trigger(mvc *c, sql_schema *s, const char *tname);
     205             : 
     206             : extern int mvc_create_type(mvc *sql, sql_schema *s, const char *sqlname, unsigned int digits, unsigned int scale, int radix, const char *impl);
     207             : extern int mvc_drop_type(mvc *sql, sql_schema *s, sql_type *t, int drop_action);
     208             : 
     209             : 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,
     210             :                                                    const char *mod, const char *impl, const char *query, bit varres, bit vararg, bit system, bit side_effect);
     211             : extern int mvc_drop_func(mvc *c, sql_schema *s, sql_func * func, int drop_action);
     212             : extern int mvc_drop_all_func(mvc *c, sql_schema *s, list *list_func, int drop_action);
     213             : 
     214             : extern int mvc_drop_schema(mvc *c, sql_schema *s, int drop_action);
     215             : extern int mvc_create_schema(mvc *m, const char *name, sqlid auth_id, sqlid owner);
     216             : extern BUN mvc_clear_table(mvc *m, sql_table *t);
     217             : extern str mvc_drop_table(mvc *c, sql_schema *s, sql_table * t, int drop_action);
     218             : 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);
     219             : extern int mvc_create_view(sql_table **t, mvc *m, sql_schema *s, const char *name, int persistence, const char *sql, bit system);
     220             : extern int mvc_create_remote(sql_table **t, mvc *m, sql_schema *s, const char *name, int persistence, const char *loc);
     221             : 
     222             : extern int mvc_drop_column(mvc *c, sql_table *t, sql_column *col, int drop_action);
     223             : sql_export int mvc_create_column(sql_column **col, mvc *m, sql_table *t, const char *name, sql_subtype *tpe);
     224             : extern int mvc_create_column_(sql_column **col, mvc *m, sql_table *t, const char *name, const char *type, unsigned int digits);
     225             : extern int mvc_null(mvc *c, sql_column *col, int flag);
     226             : extern int mvc_default(mvc *c, sql_column *col, char *val);
     227             : extern int mvc_drop_default(mvc *c, sql_column *col);
     228             : extern int mvc_storage(mvc *c, sql_column *col, char *storage);
     229             : extern int mvc_access(mvc *m, sql_table *t, sht access);
     230             : extern int mvc_is_sorted(mvc *c, sql_column *col);
     231             : extern int mvc_is_unique(mvc *m, sql_column *col);
     232             : extern int mvc_is_duplicate_eliminated(mvc *c, sql_column *col);
     233             : extern int mvc_col_stats(mvc *m, sql_column *col, bool *nonil, bool *unique, double *unique_est, ValPtr min, ValPtr max);
     234             : 
     235             : extern int mvc_create_ukey(sql_key **kres, mvc *m, sql_table *t, const char *name, key_type kt);
     236             : 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);
     237             : extern int mvc_create_kc(mvc *m, sql_key *k, sql_column *c);
     238             : extern int mvc_create_fkc(mvc *m, sql_fkey *fk, sql_column *c);
     239             : extern int mvc_create_key_done(mvc *m, sql_key *k);
     240             : 
     241             : extern int mvc_drop_key(mvc *c, sql_schema *s, sql_key *key, int drop_action);
     242             : 
     243             : extern int mvc_create_idx(sql_idx **i, mvc *m, sql_table *t, const char *name, idx_type it);
     244             : extern int mvc_create_ic(mvc *m, sql_idx * i, sql_column *c);
     245             : extern int mvc_create_idx_done(mvc *m, sql_idx *i);
     246             : extern int mvc_drop_idx(mvc *c, sql_schema *s, sql_idx * i);
     247             : 
     248             : 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);
     249             : extern int mvc_drop_trigger(mvc *m, sql_schema *s, sql_trigger * tri);
     250             : 
     251             : /*dependency control*/
     252             : extern int mvc_create_dependency(mvc *m, sql_base *b, sqlid depend_id, sql_dependency depend_type);
     253             : extern int mvc_create_dependencies(mvc *m, list *blist, sqlid depend_id, sql_dependency dep_type);
     254             : extern int mvc_check_dependency(mvc *m, sqlid id, sql_dependency type, list *ignore_ids);
     255             : 
     256             : /* variable management */
     257             : extern int init_global_variables(mvc *sql);
     258             : extern sql_var *find_global_var(mvc *sql, sql_schema *s, const char *name);
     259             : extern sql_var *push_global_var(mvc *sql, const char *sname, const char *name, sql_subtype *type);
     260             : extern int remove_global_var(mvc *sql, sql_schema *s, const char *name);
     261             : 
     262             : extern sql_var* frame_push_var(mvc *sql, const char *name, sql_subtype *type);
     263             : extern sql_local_table* frame_push_table(mvc *sql, sql_table *t);
     264             : extern sql_rel_view* stack_push_rel_view(mvc *sql, const char *name, sql_rel *var);
     265             : extern sql_window_definition* frame_push_window_def(mvc *sql, const char *name, dlist *wdef);
     266             : extern dlist* frame_get_window_def(mvc *sql, const char *name, int *pos);
     267             : extern sql_groupby_expression* frame_push_groupby_expression(mvc *sql, symbol *def, sql_exp *exp);
     268             : extern sql_exp* frame_get_groupby_expression(mvc *sql, symbol *def);
     269             : extern void stack_update_rel_view(mvc *sql, const char *name, sql_rel *view);
     270             : 
     271             : extern bool frame_check_var_visited(mvc *sql, int i);
     272             : extern void frame_set_var_visited(mvc *sql, int i);
     273             : extern void frame_clear_visited_flag(mvc *sql);
     274             : 
     275             : extern sql_frame *stack_push_frame(mvc *sql, const char *name);
     276             : extern void stack_pop_frame(mvc *sql);
     277             : extern void clear_frame(mvc *sql, sql_frame *frame);
     278             : extern void stack_pop_until(mvc *sql, int frame);
     279             : 
     280             : /* find variable in the stack */
     281             : extern sql_var *stack_find_var_frame(mvc *sql, const char *name, int *level);
     282             : extern sql_var *stack_find_var_at_level(mvc *sql, const char *name, int level);
     283             : extern sql_table *stack_find_table(mvc *sql, const char *name);
     284             : extern sql_table *frame_find_table(mvc *sq, const char *name);
     285             : extern sql_rel *stack_find_rel_view(mvc *sql, const char *name);
     286             : extern int stack_find_rel_view_projection_columns(mvc *sql, const char *name, sql_rel **res);
     287             : 
     288             : /* find variable in the current frame */
     289             : extern int frame_find_var(mvc *sql, const char *name);
     290             : extern sql_rel *frame_find_rel_view(mvc *sql, const char *name);
     291             : 
     292             : extern int stack_has_frame(mvc *sql, const char *name);
     293             : extern int stack_nr_of_declared_tables(mvc *sql);
     294             : 
     295             : extern atom *sqlvar_set(sql_var *var, ValRecord *v);
     296             : extern str sqlvar_get_string(sql_var *var);
     297             : extern str sqlvar_set_string(sql_var *var, const char *v);
     298             : #ifdef HAVE_HGE
     299             : extern hge val_get_number(ValRecord *val);
     300             : extern void sqlvar_set_number(sql_var *var, hge v);
     301             : #else
     302             : extern lng val_get_number(ValRecord *val);
     303             : extern void sqlvar_set_number(sql_var *var, lng v);
     304             : #endif
     305             : 
     306             : #define get_string_global_var(m, val) (sqlvar_get_string(find_global_var(m, mvc_bind_schema(m, "sys"), val)))
     307             : 
     308             : extern int mvc_copy_column(mvc *m, sql_table *t, sql_column *c, sql_column **cres);
     309             : extern int mvc_copy_key(mvc *m, sql_table *t, sql_key *k, sql_key **kres);
     310             : extern int mvc_copy_idx(mvc *m, sql_table *t, sql_idx *i, sql_idx **ires);
     311             : extern int mvc_copy_trigger(mvc *m, sql_table *t, sql_trigger *tr, sql_trigger **tres);
     312             : 
     313             : extern sql_rel *sql_processrelation(mvc *sql, sql_rel *rel, int profile, int instantiate, int value_based_opt, int storage_based_opt);
     314             : 
     315             : extern void *sql_error(mvc *sql, int error_code, _In_z_ _Printf_format_string_ char *format, ...)
     316             :         __attribute__((__format__(__printf__, 3, 4)));
     317             : 
     318             : extern int symbol_cmp(mvc* sql, symbol *s1, symbol *s2);
     319             : 
     320   625545973 : static inline int mvc_highwater(mvc *sql)
     321             : {
     322             : #if defined(__GNUC__) || defined(__clang__)
     323   625545973 :         uintptr_t c = (uintptr_t) __builtin_frame_address(0);
     324             : #else
     325             :         int l = 0;
     326             :         uintptr_t c = (uintptr_t) (&l);
     327             : #endif
     328   625546025 :         int rc = 0;
     329             : 
     330   625546025 :         size_t diff = c < sql->sp ? sql->sp - c : c - sql->sp;
     331   625546025 :         if (diff > THREAD_STACK_SIZE - 280 * 1024)
     332           8 :                 rc = 1;
     333   625546035 :         return rc;
     334             : }
     335             : 
     336             : #endif /*_SQL_MVC_H*/

Generated by: LCOV version 1.14