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

Generated by: LCOV version 1.14