LCOV - code coverage report
Current view: top level - sql/include - sql_catalog.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 22 86.4 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.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             : #ifndef SQL_CATALOG_H
      10             : #define SQL_CATALOG_H
      11             : 
      12             : #include "sql_mem.h"
      13             : #include "sql_list.h"
      14             : #include "sql_hash.h"
      15             : #include "mapi_querytype.h"
      16             : #include "stream.h"
      17             : #include "matomic.h"
      18             : 
      19             : #define tr_none         0
      20             : #define tr_readonly     1
      21             : #define tr_writable     2
      22             : #define tr_snapshot 4
      23             : #define tr_serializable 8
      24             : #define tr_append       16
      25             : 
      26             : #define ACT_NO_ACTION 0
      27             : #define ACT_CASCADE 1
      28             : #define ACT_RESTRICT 2
      29             : #define ACT_SET_NULL 3
      30             : #define ACT_SET_DEFAULT 4
      31             : 
      32             : #define DROP_RESTRICT 0
      33             : #define DROP_CASCADE 1
      34             : #define DROP_CASCADE_START 2
      35             : 
      36             : #define PRIV_SELECT 1
      37             : #define PRIV_UPDATE 2
      38             : #define PRIV_INSERT 4
      39             : #define PRIV_DELETE 8
      40             : #define PRIV_EXECUTE 16
      41             : #define PRIV_GRANT 32
      42             : #define PRIV_TRUNCATE 64
      43             : /* global privs */
      44             : #define PRIV_COPYFROMFILE 1
      45             : #define PRIV_COPYINTOFILE 2
      46             : 
      47             : typedef enum sql_dependency {
      48             :         SCHEMA_DEPENDENCY = 1,
      49             :         TABLE_DEPENDENCY = 2,
      50             :         COLUMN_DEPENDENCY = 3,
      51             :         KEY_DEPENDENCY = 4,
      52             :         VIEW_DEPENDENCY = 5,
      53             :         USER_DEPENDENCY = 6,
      54             :         FUNC_DEPENDENCY = 7,
      55             :         TRIGGER_DEPENDENCY = 8,
      56             :         OWNER_DEPENDENCY = 9,
      57             :         INDEX_DEPENDENCY = 10,
      58             :         FKEY_DEPENDENCY = 11,
      59             :         SEQ_DEPENDENCY = 12,
      60             :         PROC_DEPENDENCY = 13,
      61             :         BEDROPPED_DEPENDENCY = 14, /*The object must be dropped when the dependent object is dropped independently of the DROP type.*/
      62             :         TYPE_DEPENDENCY = 15
      63             : } sql_dependency;
      64             : 
      65             : #define NO_DEPENDENCY 0
      66             : #define HAS_DEPENDENCY 1
      67             : #define CICLE_DEPENDENCY 2
      68             : #define DEPENDENCY_CHECK_ERROR 3
      69             : #define DEPENDENCY_CHECK_OK 0
      70             : 
      71             : #define ROLE_PUBLIC   1
      72             : #define ROLE_SYSADMIN 2
      73             : #define USER_MONETDB  3
      74             : 
      75             : #define ISO_READ_UNCOMMITED 1
      76             : #define ISO_READ_COMMITED   2
      77             : #define ISO_READ_REPEAT     3
      78             : #define ISO_SERIALIZABLE    4
      79             : 
      80             : #define SCALE_NONE      0
      81             : #define SCALE_FIX       1       /* many numerical functions require equal
      82             :                            scales/precision for all their inputs */
      83             : #define SCALE_NOFIX     2
      84             : #define SCALE_MUL       3       /* multiplication gives the sum of scales */
      85             : #define SCALE_DIV       4       /* div on the other hand reduces the scales */
      86             : #define DIGITS_ADD      5       /* some types grow under functions (concat) */
      87             : #define INOUT           6       /* output type equals input type */
      88             : #define SCALE_EQ        7       /* user defined functions need equal scales */
      89             : 
      90             : #define RDONLY 0
      91             : #define RD_INS 1
      92             : #define RD_UPD_ID 2
      93             : #define RD_UPD_VAL 3
      94             : #define QUICK  4
      95             : 
      96             : /* the following list of macros are used by rel_rankop function */
      97             : #define UNBOUNDED_PRECEDING_BOUND 0
      98             : #define UNBOUNDED_FOLLOWING_BOUND 1
      99             : #define CURRENT_ROW_BOUND         2
     100             : 
     101             : #define FRAME_ROWS  0           /* number of rows (preceding/following) */
     102             : #define FRAME_RANGE 1           /* logical range (based on the ordering column).
     103             :                                    Example:
     104             :                                    RANGE BETWEEN INTERVAL '1' MONTH PRECEDING
     105             :                                              AND INTERVAL '1' MONTH FOLLOWING */
     106             : #define FRAME_GROUPS 2
     107             : #define FRAME_UNBOUNDED_TILL_CURRENT_ROW 3
     108             : #define FRAME_CURRENT_ROW_TILL_UNBOUNDED 4
     109             : #define FRAME_ALL 5
     110             : #define FRAME_CURRENT_ROW 6
     111             : 
     112             : /* the following list of macros are used by SQLwindow_bound function */
     113             : #define BOUND_FIRST_HALF_PRECEDING  0
     114             : #define BOUND_FIRST_HALF_FOLLOWING  1
     115             : #define BOUND_SECOND_HALF_PRECEDING 2
     116             : #define BOUND_SECOND_HALF_FOLLOWING 3
     117             : #define CURRENT_ROW_PRECEDING       4
     118             : #define CURRENT_ROW_FOLLOWING       5
     119             : 
     120             : #define EXCLUDE_NONE 0          /* nothing excluded (also the default) */
     121             : #define EXCLUDE_CURRENT_ROW 1   /* exclude the current row */
     122             : #define EXCLUDE_GROUP 2         /* exclude group */
     123             : #define EXCLUDE_TIES 3          /* exclude group but not the current row */
     124             : 
     125             : /* The following macros are used in properties field of sql_table */
     126             : #define PARTITION_RANGE       1
     127             : #define PARTITION_LIST        2
     128             : #define PARTITION_COLUMN      4
     129             : #define PARTITION_EXPRESSION  8
     130             : 
     131             : #define STORAGE_MAX_VALUE_LENGTH 2048
     132             : 
     133             : #define cur_user 1
     134             : #define cur_role 2
     135             : 
     136             : #define sql_max(i1,i2) ((i1)<(i2))?(i2):(i1)
     137             : 
     138             : #define dt_schema       "%dt%"
     139             : #define isDeclaredSchema(s)     (strcmp(s->base.name, dt_schema) == 0)
     140             : 
     141             : extern const char *TID;
     142             : 
     143             : typedef enum temp_t {
     144             :         SQL_PERSIST = 0,
     145             :         SQL_LOCAL_TEMP = 1,
     146             :         SQL_GLOBAL_TEMP = 2,
     147             :         SQL_DECLARED_TABLE = 3, /* variable inside a stored procedure */
     148             :         SQL_MERGE_TABLE = 4,
     149             :         /* SQL_STREAM = 5, stream tables are not used anymore */
     150             :         SQL_REMOTE = 6,
     151             :         SQL_REPLICA_TABLE = 7
     152             : } temp_t;
     153             : 
     154             : typedef enum comp_type {
     155             :         cmp_gt = 0,
     156             :         cmp_gte = 1,
     157             :         cmp_lte = 2,
     158             :         cmp_lt = 3,
     159             :         cmp_equal = 4,
     160             :         cmp_notequal = 5,
     161             : 
     162             :         cmp_filter = 6,
     163             :         cmp_or = 7,
     164             :         cmp_in = 8,                     /* in value list */
     165             :         cmp_notin = 9,                  /* not in value list */
     166             : 
     167             :         mark_in = 10,                   /* mark joins */
     168             :         mark_notin = 11,
     169             : 
     170             :         /* The followin cmp_* are only used within stmt (not sql_exp) */
     171             :         cmp_all = 12,                   /* special case for crossproducts */
     172             :         cmp_project = 13,               /* special case for projection joins */
     173             :         cmp_joined = 14,                /* special case already joined */
     174             :         cmp_left_project = 15   /* last step of outer join */
     175             : } comp_type;
     176             : 
     177             : #define is_theta_exp(e) ((e) == cmp_gt || (e) == cmp_gte || (e) == cmp_lte ||\
     178             :                                                  (e) == cmp_lt || (e) == cmp_equal || (e) == cmp_notequal)
     179             : 
     180             : #define is_complex_exp(et) ((et) == cmp_or || (et) == cmp_in || (et) == cmp_notin || (et) == cmp_filter)
     181             : 
     182             : #define is_equality_or_inequality_exp(et) ((et) == cmp_equal || (et) == cmp_notequal || (et) == cmp_in || \
     183             :                                                                                    (et) == cmp_notin || (et) == mark_in || (et) == mark_notin)
     184             : 
     185             : typedef enum commit_action_t {
     186             :         CA_COMMIT,      /* commit rows, only for persistent tables */
     187             :         CA_DELETE,      /* delete rows */
     188             :         CA_PRESERVE,    /* preserve rows */
     189             :         CA_DROP         /* drop table */
     190             : } ca_t;
     191             : 
     192             : typedef int sqlid;
     193             : typedef void *sql_store;
     194             : 
     195             : typedef struct sql_base {
     196             :         unsigned int
     197             :                 new:1,
     198             :                 deleted:1,
     199             :                 refcnt:30;
     200             :         sqlid id;
     201             :         char *name;
     202             : } sql_base;
     203             : 
     204             : #define isNew(x)          ((x)->base.new)
     205             : 
     206             : extern void base_init(sql_allocator *sa, sql_base * b, sqlid id, bool isnew, const char *name);
     207             : 
     208             : typedef struct changeset {
     209             :         sql_allocator *sa;
     210             :         fdestroy destroy;
     211             :         struct list *set;
     212             :         struct list *dset;
     213             :         node *nelm;
     214             : } changeset;
     215             : 
     216             : typedef struct objlist {
     217             :         list *l;
     218             :         sql_hash *h;
     219             :         sql_store store;
     220             : } objlist;
     221             : 
     222             : struct sql_trans;
     223             : struct sql_change;
     224             : struct objectset;
     225             : struct versionhead;
     226             : struct os_iter {
     227             :         struct objectset *os;
     228             :         struct sql_trans *tr;
     229             :         struct versionhead *n;
     230             :         struct sql_hash_e *e;
     231             :         const char *name;
     232             : };
     233             : 
     234             : /* transaction changes */
     235             : typedef int (*tc_valid_fptr) (struct sql_trans *tr, struct sql_change *c/*, ulng commit_ts, ulng oldest*/);
     236             : typedef int (*tc_log_fptr) (struct sql_trans *tr, struct sql_change *c);                                                                /* write changes to the log */
     237             : typedef int (*tc_commit_fptr) (struct sql_trans *tr, struct sql_change *c, ulng commit_ts, ulng oldest);/* commit/rollback changes */
     238             : typedef int (*tc_cleanup_fptr) (sql_store store, struct sql_change *c, ulng oldest);    /* garbage collection, ie cleanup structures when possible */
     239             : typedef void (*destroy_fptr)(sql_store store, sql_base *b);
     240             : typedef int (*validate_fptr)(struct sql_trans *tr, sql_base *b, int delete);
     241             : 
     242             : extern struct objectset *os_new(sql_allocator *sa, destroy_fptr destroy, bool temporary, bool unique, bool concurrent, sql_store store);
     243             : extern struct objectset *os_dup(struct objectset *os);
     244             : extern void os_destroy(struct objectset *os, sql_store store);
     245             : extern int /*ok, error (name existed) and conflict (added before) */ os_add(struct objectset *os, struct sql_trans *tr, const char *name, sql_base *b);
     246             : extern int os_del(struct objectset *os, struct sql_trans *tr, const char *name, sql_base *b);
     247             : extern int os_size(struct objectset *os, struct sql_trans *tr);
     248             : extern int os_empty(struct objectset *os, struct sql_trans *tr);
     249             : extern int os_remove(struct objectset *os, struct sql_trans *tr, const char *name);
     250             : extern sql_base *os_find_name(struct objectset *os, struct sql_trans *tr, const char *name);
     251             : extern sql_base *os_find_id(struct objectset *os, struct sql_trans *tr, sqlid id);
     252             : /* iterating (for example for location functinos) */
     253             : extern void os_iterator(struct os_iter *oi, struct objectset *os, struct sql_trans *tr, const char *name /*optional*/);
     254             : extern sql_base *oi_next(struct os_iter *oi);
     255             : extern bool os_obj_intransaction(struct objectset *os, struct sql_trans *tr, sql_base *b);
     256             : extern bool os_has_changes(struct objectset *os, struct sql_trans *tr);
     257             : 
     258             : extern objlist *ol_new(sql_allocator *sa, destroy_fptr destroy, sql_store store);
     259             : extern void ol_destroy(objlist *ol, sql_store store);
     260             : extern int ol_add(objlist *ol, sql_base *data);
     261             : extern void ol_del(objlist *ol, sql_store store, node *data);
     262             : extern node *ol_find_name(objlist *ol, const char *name);
     263             : extern node *ol_find_id(objlist *ol, sqlid id);
     264             : extern node *ol_rehash(objlist *ol, const char *oldname, node *n);
     265             : #define ol_length(ol) (list_length(ol->l))
     266             : #define ol_first_node(ol) (ol->l->h)
     267             : #define ol_last_node(ol) (ol->l->t)
     268             : #define ol_fetch(ol,nr) (list_fetch(ol->l, nr))
     269             : 
     270             : extern void cs_new(changeset * cs, sql_allocator *sa, fdestroy destroy);
     271             : extern void cs_destroy(changeset * cs, void *data);
     272             : extern void cs_add(changeset * cs, void *elm, bool isnew);
     273             : extern void cs_del(changeset * cs, void *gdata, node *elm, bool isnew);
     274             : extern int cs_size(changeset * cs);
     275             : extern node *cs_find_id(changeset * cs, sqlid id);
     276             : 
     277             : typedef void *backend_code;
     278             : typedef size_t backend_stack;
     279             : 
     280             : typedef struct sql_schema {
     281             :         sql_base base;
     282             :         sqlid auth_id;
     283             :         sqlid owner;
     284             :         bit system;             /* system or user schema */
     285             :         // TODO? int type;      /* persistent, session local, transaction local */
     286             : 
     287             :         struct objectset *tables;
     288             :         struct objectset *types;
     289             :         struct objectset *funcs;
     290             :         struct objectset *seqs;
     291             :         struct objectset *keys;         /* Names for keys, idxs, and triggers and parts are */
     292             :         struct objectset *idxs;         /* global, but these objects are only */
     293             :         struct objectset *triggers;     /* useful within a table */
     294             :         struct objectset *parts;
     295             : 
     296             :         char *internal;         /* optional internal module name */
     297             :         sql_store store;
     298             : } sql_schema;
     299             : 
     300             : typedef struct sql_catalog {
     301             :         struct objectset *schemas;
     302             :         struct objectset *objects;
     303             : } sql_catalog;
     304             : 
     305             : typedef struct sql_trans {
     306             :         char *name;
     307             : 
     308             :         ulng ts;                        /* transaction start timestamp */
     309             :         ulng tid;                       /* transaction id */
     310             : 
     311             :         sql_store store;        /* keep link into the global store */
     312             :         MT_Lock lock;           /* lock protecting concurrent writes to the changes list */
     313             :         list *changes;          /* list of changes */
     314             : 
     315             :         list *dropped;          /* protection against recursive cascade action*/
     316             :         list *predicates;       /* list of read predicates logged during update transactions */
     317             :         list *dependencies;     /* list of dependencies created (list of sqlids from the objects) */
     318             :         list *depchanges;       /* list of dependencies changed (it would be tested for conflicts at the end of the transaction) */
     319             : 
     320             :         int logchanges;         /* count number of changes to be applied to the wal */
     321             :         int active;                     /* is active transaction */
     322             :         int status;                     /* status of the last query */
     323             : 
     324             :         sql_catalog *cat;
     325             :         sql_schema *tmp;        /* each session has its own tmp schema */
     326             :         changeset localtmps;
     327             :         sql_allocator *sa;      /* transaction allocator */
     328             : 
     329             :         struct sql_trans *parent;       /* multilevel transaction support */
     330             : } sql_trans;
     331             : 
     332             : typedef enum sql_class {
     333             :         EC_ANY,
     334             :         EC_TABLE,
     335             :         EC_BIT,
     336             :         EC_CHAR,
     337             :         EC_STRING,
     338             :         EC_BLOB,
     339             :         EC_POS,
     340             :         EC_NUM,
     341             :         EC_MONTH,
     342             :         EC_SEC,
     343             :         EC_DEC,
     344             :         EC_FLT,
     345             :         EC_TIME,
     346             :         EC_TIME_TZ,
     347             :         EC_DATE,
     348             :         EC_TIMESTAMP,
     349             :         EC_TIMESTAMP_TZ,
     350             :         EC_GEOM,
     351             :         EC_EXTERNAL,
     352             :         EC_MAX /* evaluated to the max value, should be always kept at the bottom */
     353             : } sql_class;
     354             : 
     355             : #define has_tz(e)                       (EC_TEMP_TZ(e))
     356             : #define type_has_tz(t)          has_tz((t)->type->eclass)
     357             : #define EC_VARCHAR(e)           ((e)==EC_CHAR||(e)==EC_STRING)
     358             : #define EC_INTERVAL(e)          ((e)==EC_MONTH||(e)==EC_SEC)
     359             : #define EC_NUMBER(e)            ((e)==EC_POS||(e)==EC_NUM||EC_INTERVAL(e)||(e)==EC_DEC||(e)==EC_FLT)
     360             : #define EC_EXACTNUM(e)          ((e)==EC_NUM||(e)==EC_DEC)
     361             : #define EC_APPNUM(e)            ((e)==EC_FLT)
     362             : #define EC_COMPUTE(e)           ((e)==EC_NUM||(e)==EC_FLT)
     363             : #define EC_BOOLEAN(e)           ((e)==EC_BIT||(e)==EC_NUM||(e)==EC_FLT)
     364             : #define EC_TEMP_TZ(e)           ((e)==EC_TIME_TZ||(e)==EC_TIMESTAMP_TZ)
     365             : #define EC_TEMP(e)                      ((e)==EC_TIME||(e)==EC_DATE||(e)==EC_TIMESTAMP||EC_TEMP_TZ(e))
     366             : #define EC_TEMP_FRAC(e)         ((e)==EC_TIME||(e)==EC_TIMESTAMP||EC_TEMP_TZ(e))
     367             : #define EC_TEMP_NOFRAC(e)       ((e)==EC_TIME||(e)==EC_TIMESTAMP)
     368             : #define EC_SCALE(e)                     ((e)==EC_DEC||EC_TEMP_FRAC(e)||(e)==EC_SEC)
     369             : #define EC_BACKEND_FIXED(e)     (EC_NUMBER(e)||(e)==EC_BIT||EC_TEMP(e))
     370             : 
     371             : typedef struct sql_type {
     372             :         sql_base base;
     373             : 
     374             :         char *impl; /* backend correspondent type */
     375             :         unsigned int digits;
     376             :         unsigned int scale;     /* indicates how scale is used in functions */
     377             :         int localtype;          /* localtype, need for coersions */
     378             :         unsigned char radix;
     379             :         unsigned int bits;
     380             :         sql_class eclass;       /* types are grouped into equivalence classes */
     381             :         sql_schema *s;
     382             : } sql_type;
     383             : 
     384             : typedef struct sql_alias {
     385             :         char *name;
     386             :         char *alias;
     387             : } sql_alias;
     388             : 
     389             : #define ARG_IN 1
     390             : #define ARG_OUT 0
     391             : 
     392             : typedef struct sql_subtype {
     393             :         sql_type *type;
     394             :         unsigned int digits;
     395             :         unsigned int scale;
     396             : } sql_subtype;
     397             : 
     398             : /* sql_func need type transform rules types are equal if underlying
     399             :  * types are equal + scale is equal if types do not mach we try type
     400             :  * conversions which means for simple 1 arg functions
     401             :  */
     402             : 
     403             : typedef struct sql_arg {
     404             :         char *name;
     405             :         bte inout;
     406             :         sql_subtype type;
     407             : } sql_arg;
     408             : 
     409             : typedef enum sql_ftype {
     410             :         F_FUNC = 1,
     411             :         F_PROC = 2,
     412             :         F_AGGR = 3,
     413             :         F_FILT = 4,
     414             :         F_UNION = 5,
     415             :         F_ANALYTIC = 6,
     416             :         F_LOADER = 7
     417             : } sql_ftype;
     418             : 
     419             : #define IS_FUNC(f)     ((f)->type == F_FUNC)
     420             : #define IS_PROC(f)     ((f)->type == F_PROC)
     421             : #define IS_AGGR(f)     ((f)->type == F_AGGR)
     422             : #define IS_FILT(f)     ((f)->type == F_FILT)
     423             : #define IS_UNION(f)    ((f)->type == F_UNION)
     424             : #define IS_ANALYTIC(f) ((f)->type == F_ANALYTIC)
     425             : #define IS_LOADER(f)   ((f)->type == F_LOADER)
     426             : 
     427             : #define FUNC_TYPE_STR(type, F, fn) \
     428             :         switch (type) { \
     429             :                 case F_FUNC: \
     430             :                         F = "FUNCTION"; \
     431             :                         fn = "function"; \
     432             :                         break; \
     433             :                 case F_PROC: \
     434             :                         F = "PROCEDURE"; \
     435             :                         fn = "procedure"; \
     436             :                         break; \
     437             :                 case F_AGGR: \
     438             :                         F = "AGGREGATE"; \
     439             :                         fn = "aggregate"; \
     440             :                         break; \
     441             :                 case F_FILT: \
     442             :                         F = "FILTER FUNCTION"; \
     443             :                         fn = "filter function"; \
     444             :                         break; \
     445             :                 case F_UNION: \
     446             :                         F = "UNION FUNCTION"; \
     447             :                         fn = "table returning function"; \
     448             :                         break; \
     449             :                 case F_ANALYTIC: \
     450             :                         F = "WINDOW FUNCTION"; \
     451             :                         fn = "window function"; \
     452             :                         break; \
     453             :                 case F_LOADER: \
     454             :                         F = "LOADER FUNCTION"; \
     455             :                         fn = "loader function"; \
     456             :                         break; \
     457             :                 default: \
     458             :                         assert(0); \
     459             :         }
     460             : 
     461             : typedef enum sql_flang {
     462             :         FUNC_LANG_INT = 0, /* internal */
     463             :         FUNC_LANG_MAL = 1, /* create sql external mod.func */
     464             :         FUNC_LANG_SQL = 2, /* create ... sql function/procedure */
     465             :         FUNC_LANG_R = 3,   /* create .. language R */
     466             :         FUNC_LANG_C = 4,   /* create .. language C */
     467             :         FUNC_LANG_J = 5,   /* create .. language JAVASCRIPT (not implemented) */
     468             :         /* this should probably be done in a better way */
     469             :         FUNC_LANG_PY = 6,       /* create .. language PYTHON */
     470             :         FUNC_LANG_MAP_PY = 7,   /* create .. language PYTHON_MAP */
     471             :         /* values 8 and 9 were for Python 2 */
     472             :         FUNC_LANG_PY3 = 10,     /* create .. language PYTHON3 */
     473             :         FUNC_LANG_MAP_PY3 = 11, /* create .. language PYTHON3_MAP */
     474             :         FUNC_LANG_CPP = 12      /* create .. language CPP */
     475             : } sql_flang;
     476             : 
     477             : #define LANG_EXT(l)  ((l)>FUNC_LANG_SQL)
     478             : #define UDF_LANG(l)  ((l)>=FUNC_LANG_SQL)
     479             : 
     480             : typedef struct sql_func {
     481             :         sql_base base;
     482             : 
     483             :         char *imp;
     484             :         char *mod;
     485             :         sql_ftype type;
     486             :         list *ops;      /* param list */
     487             :         list *res;      /* list of results */
     488             :         int sql;        /* 0 native implementation
     489             :                            1 sql
     490             :                            2 sql instantiated proc
     491             :                         */
     492             :         sql_flang lang;
     493             :         char *query;    /* sql code */
     494             :         bit semantics; /*When set to true, function incorporates some kind of null semantics.*/
     495             :         bit side_effect;
     496             :         bit varres;     /* variable output result */
     497             :         bit vararg;     /* variable input arguments */
     498             :         bit system;     /* system function */
     499             :         int fix_scale;
     500             :                         /*
     501             :                            SCALE_NOFIX/SCALE_NONE => nothing
     502             :                            SCALE_FIX => input scale fixing,
     503             :                            SCALE_ADD => leave inputs as is and do add scales
     504             :                            example numerical multiplication
     505             :                            SCALE_SUB => first input scale, fix with second scale
     506             :                            result scale is equal to first input
     507             :                            example numerical division
     508             :                            DIGITS_ADD => result digits, sum of args
     509             :                            example string concat
     510             :                         */
     511             :         sql_schema *s;
     512             :         sql_allocator *sa;
     513             : } sql_func;
     514             : 
     515             : typedef struct sql_subfunc {
     516             :         sql_func *func;
     517             :         list *res;
     518             :         list *coltypes; /* we need this for copy into from loader */
     519             :         list *colnames; /* we need this for copy into from loader */
     520             :         char *sname, *tname; /* we need this for create table from loader */
     521             : } sql_subfunc;
     522             : 
     523             : typedef enum key_type {
     524             :         pkey,
     525             :         ukey,
     526             :         fkey
     527             : } key_type;
     528             : 
     529             : typedef struct sql_kc {
     530             :         struct sql_column *c;
     531             :         int trunc;              /* 0 not truncated, >0 colum is truncated */
     532             : } sql_kc;
     533             : 
     534             : typedef enum idx_type {
     535             :         hash_idx,
     536             :         join_idx,
     537             :         oph_idx,                /* order preserving hash */
     538             :         no_idx,                 /* no idx, ie no storage */
     539             :         imprints_idx,
     540             :         ordered_idx,
     541             :         new_idx_types
     542             : } idx_type;
     543             : 
     544             : #define hash_index(t)           ((t) == hash_idx || (t) == oph_idx)
     545             : #define idx_has_column(t)       (hash_index(t) || (t) == join_idx)
     546             : #define oid_index(t)            ((t) == join_idx)
     547             : #define non_updatable_index(t) ((t) == ordered_idx || (t) == no_idx || !idx_has_column(t))
     548             : 
     549             : typedef struct sql_idx {
     550             :         sql_base base;
     551             :         idx_type type;          /* unique */
     552             :         struct list *columns;   /* list of sql_kc */
     553             :         struct sql_table *t;
     554             :         struct sql_key *key;    /* key */
     555             :         ATOMIC_PTR_TYPE data;
     556             : } sql_idx;
     557             : 
     558             : /* fkey consists of two of these */
     559             : typedef struct sql_key {        /* pkey, ukey, fkey */
     560             :         sql_base base;
     561             :         key_type type;          /* pkey, ukey, fkey */
     562             :         sql_idx *idx;           /* idx to accelerate key check */
     563             : 
     564             :         struct list *columns;   /* list of sql_kc */
     565             :         struct sql_table *t;
     566             :         int drop_action;        /* only needed for alter drop key */
     567             : } sql_key;
     568             : 
     569             : typedef struct sql_ukey {       /* pkey, ukey */
     570             :         sql_key k;
     571             :         //list *keys;
     572             : } sql_ukey;
     573             : 
     574             : typedef struct sql_fkey {       /* fkey */
     575             :         sql_key k;
     576             :         /* no action, restrict (default), cascade, set null, set default */
     577             :         int on_delete;
     578             :         int on_update;
     579             :         sqlid rkey;
     580             : } sql_fkey;
     581             : 
     582             : typedef struct sql_trigger {
     583             :         sql_base base;
     584             :         sht time;               /* before or after */
     585             :         sht orientation;        /* row or statement */
     586             :         sht event;              /* insert, delete, update, truncate */
     587             :         /* int action_order;     TODO, order within the set of triggers */
     588             :         struct list *columns;   /* update trigger on list of (sql_kc) columns */
     589             : 
     590             :         struct sql_table *t;
     591             :         char *old_name;         /* name referencing the old values */
     592             :         char *new_name;         /* name referencing the new values */
     593             : 
     594             :         char *condition;        /* when search condition, ie query */
     595             :         char *statement;        /* action, ie list of sql statements */
     596             : } sql_trigger;
     597             : 
     598             : typedef struct sql_sequence {
     599             :         sql_base base;
     600             :         lng start;
     601             :         lng minvalue;
     602             :         lng maxvalue;
     603             :         lng increment;
     604             :         lng cacheinc;
     605             :         bit cycle;
     606             :         bit bedropped;          /*Drop the SEQUENCE if you are dropping the column, e.g., SERIAL COLUMN".*/
     607             :         sql_schema *s;
     608             : } sql_sequence;
     609             : 
     610             : typedef struct sql_column {
     611             :         sql_base base;
     612             :         sql_subtype type;
     613             :         int colnr;
     614             :         bit null;
     615             :         char *def;
     616             :         char unique;            /* 0 NOT UNIQUE, 1 SUB_UNIQUE, 2 UNIQUE */
     617             :         int drop_action;        /* only used for alter statements */
     618             :         char *storage_type;
     619             :         int sorted;             /* for DECLARED (dupped tables) we keep order info */
     620             :         size_t dcount;
     621             :         char *min;
     622             :         char *max;
     623             : 
     624             :         struct sql_table *t;
     625             :         ATOMIC_PTR_TYPE data;
     626             : } sql_column;
     627             : 
     628             : typedef enum table_types {
     629             :         tt_table = 0,           /* table */
     630             :         tt_view = 1,            /* view */
     631             :         tt_merge_table = 3,     /* multiple tables form one table */
     632             :         /* tt_stream = 4, stream tables are not used anymore */
     633             :         tt_remote = 5,          /* stored on a remote server */
     634             :         tt_replica_table = 6    /* multiple replica of the same table */
     635             : } table_types;
     636             : 
     637             : #define TABLE_TYPE_DESCRIPTION(tt, properties)                                                                      \
     638             : ((tt) == tt_table)?"TABLE":((tt) == tt_view)?"VIEW":((tt) == tt_merge_table && !(properties))?"MERGE TABLE":                \
     639             : ((tt) == tt_remote)?"REMOTE TABLE":                                                  \
     640             : ((tt) == tt_merge_table && ((properties) & PARTITION_LIST) == PARTITION_LIST)?"LIST PARTITION TABLE":                   \
     641             : ((tt) == tt_merge_table && ((properties) & PARTITION_RANGE) == PARTITION_RANGE)?"RANGE PARTITION TABLE":"REPLICA TABLE"
     642             : 
     643             : #define isTable(x)                        ((x)->type==tt_table)
     644             : #define isView(x)                         ((x)->type==tt_view)
     645             : #define isNonPartitionedTable(x)          ((x)->type==tt_merge_table && !(x)->properties)
     646             : #define isRangePartitionTable(x)          ((x)->type==tt_merge_table && ((x)->properties & PARTITION_RANGE) == PARTITION_RANGE)
     647             : #define isListPartitionTable(x)           ((x)->type==tt_merge_table && ((x)->properties & PARTITION_LIST) == PARTITION_LIST)
     648             : #define isPartitionedByColumnTable(x)     ((x)->type==tt_merge_table && ((x)->properties & PARTITION_COLUMN) == PARTITION_COLUMN)
     649             : #define isPartitionedByExpressionTable(x) ((x)->type==tt_merge_table && ((x)->properties & PARTITION_EXPRESSION) == PARTITION_EXPRESSION)
     650             : #define isMergeTable(x)                   ((x)->type==tt_merge_table)
     651             : #define isRemote(x)                       ((x)->type==tt_remote)
     652             : #define isReplicaTable(x)                 ((x)->type==tt_replica_table)
     653             : #define isKindOfTable(x)                  (isTable(x) || isMergeTable(x) || isRemote(x) || isReplicaTable(x))
     654             : 
     655             : #define TABLE_WRITABLE  0
     656             : #define TABLE_READONLY  1
     657             : #define TABLE_APPENDONLY        2
     658             : 
     659             : typedef struct sql_part_value {
     660             :         ptr value;
     661             :         size_t length;
     662             : } sql_part_value;
     663             : 
     664             : typedef struct sql_part {
     665             :         sql_base base;
     666             :         struct sql_table *t;    /* the merge table */
     667             :         sqlid member;                   /* the member of the merge table */
     668             :         bit with_nills;                 /* 0 no nills, 1 holds nills, NULL holds all values -> range FROM MINVALUE TO MAXVALUE WITH NULL */
     669             :         union {
     670             :                 list *values;       /* partition by values/list */
     671             :                 struct sql_range {  /* partition by range */
     672             :                         ptr minvalue;
     673             :                         ptr maxvalue;
     674             :                         size_t minlength;
     675             :                         size_t maxlength;
     676             :                 } range;
     677             :         } part;
     678             : } sql_part;
     679             : 
     680             : typedef struct sql_expression {
     681             :         sql_subtype type; /* the returning sql_subtype of the expression */
     682             :         char *exp;        /* the expression itself */
     683             :         list *cols;       /* list of colnr of the columns of the table used in the expression */
     684             : } sql_expression;
     685             : 
     686             : typedef struct sql_table {
     687             :         sql_base base;
     688             :         sht type;               /* table, view, etc */
     689             :         sht access;             /* writable, readonly, appendonly */
     690             :         bit system;             /* system or user table */
     691             :         bit bootstrap;          /* system table created during bootstrap */
     692             :         bte properties;         /* used for merge_tables */
     693             :         temp_t persistence;     /* persistent, global or local temporary */
     694             :         ca_t commit_action;     /* on commit action */
     695             :         char *query;            /* views may require some query */
     696             :         int  sz;
     697             : 
     698             :         sql_ukey *pkey;
     699             :         objlist *columns;
     700             :         objlist *idxs;
     701             :         objlist *keys;
     702             :         objlist *triggers;
     703             :         list *members;          /* member tables of merge/replica tables */
     704             :         int drop_action;        /* only needed for alter drop table */
     705             : 
     706             :         ATOMIC_PTR_TYPE data;
     707             :         struct sql_schema *s;
     708             : 
     709             :         union {
     710             :                 struct sql_column *pcol; /* If it is partitioned on a column */
     711             :                 struct sql_expression *pexp; /* If it is partitioned by an expression */
     712             :         } part;
     713             : } sql_table;
     714             : 
     715             : typedef struct res_col {
     716             :         char *tn;
     717             :         char *name;
     718             :         sql_subtype type;
     719             :         bat b;
     720             :         char mtype;
     721             :         bool cached;
     722             :         ptr *p;
     723             : } res_col;
     724             : 
     725             : typedef struct res_table {
     726             :         int id;
     727             :         oid query_id;
     728             :         mapi_query_t query_type;
     729             :         int nr_cols;
     730             :         BUN nr_rows;
     731             :         BUN cur_row;
     732             :         int cur_col;
     733             :         const char *tsep;
     734             :         const char *rsep;
     735             :         const char *ssep;
     736             :         const char *ns;
     737             :         res_col *cols;
     738             :         bat order;
     739             :         struct res_table *next;
     740             : } res_table;
     741             : 
     742             : typedef struct sql_session {
     743             :         sql_allocator *sa;
     744             :         sql_trans *tr;          /* active transaction */
     745             : 
     746             :         char *schema_name; /* transaction's schema name */
     747             :         sql_schema *schema;
     748             : 
     749             :         char ac_on_commit;      /* if 1, auto_commit should be enabled on
     750             :                                    commit, rollback, etc. */
     751             :         char auto_commit;
     752             :         int level;              /* TRANSACTION isolation level */
     753             :         int status;             /* status, ok/error */
     754             :         backend_stack stk;
     755             : } sql_session;
     756             : 
     757             : #define sql_base_loop(l, n) for (n=l->h; n; n=n->next)
     758             : 
     759             : extern int base_key(sql_base *b);
     760             : extern node *list_find_name(list *l, const char *name);
     761             : extern node *list_find_id(list *l, sqlid id);
     762             : extern node *list_find_base_id(list *l, sqlid id);
     763             : 
     764             : extern sql_key *find_sql_key(sql_table *t, const char *kname);
     765             : extern sql_key *sql_trans_find_key(sql_trans *tr, sqlid id);
     766             : 
     767             : extern sql_idx *find_sql_idx(sql_table *t, const char *kname);
     768             : extern sql_idx *sql_trans_find_idx(sql_trans *tr, sqlid id);
     769             : 
     770             : extern sql_column *find_sql_column(sql_table *t, const char *cname);
     771             : 
     772             : extern sql_table *find_sql_table(sql_trans *tr, sql_schema *s, const char *tname);
     773             : extern sql_table *find_sql_table_id(sql_trans *tr, sql_schema *s, sqlid id);
     774             : extern sql_table *sql_trans_find_table(sql_trans *tr, sqlid id);
     775             : 
     776             : extern sql_sequence *find_sql_sequence(sql_trans *tr, sql_schema *s, const char *sname);
     777             : 
     778             : extern sql_schema *find_sql_schema(sql_trans *t, const char *sname);
     779             : extern sql_schema *find_sql_schema_id(sql_trans *t, sqlid id);
     780             : 
     781             : extern sql_type *find_sql_type(sql_trans *tr, sql_schema * s, const char *tname);
     782             : extern sql_type *sql_trans_bind_type(sql_trans *tr, sql_schema *s, const char *name);
     783             : extern sql_type *sql_trans_find_type(sql_trans *tr, sql_schema *s /*optional */, sqlid id);
     784             : extern sql_func *sql_trans_find_func(sql_trans *tr, sqlid id);
     785             : extern sql_trigger *sql_trans_find_trigger(sql_trans *tr, sqlid id);
     786             : 
     787             : extern void find_partition_type(sql_subtype *tpe, sql_table *mt);
     788             : extern void *sql_values_list_element_validate_and_insert(void *v1, void *v2, void *tpe, int* res);
     789             : extern void *sql_range_part_validate_and_insert(void *v1, void *v2, void *tpe);
     790             : extern void *sql_values_part_validate_and_insert(void *v1, void *v2, void *tpe);
     791             : 
     792             : typedef struct {
     793             :         BAT *b;
     794             :         char* name;
     795             :         void* def;
     796             : } sql_emit_col;
     797             : 
     798             : extern int nested_mergetable(sql_trans *tr, sql_table *t, const char *sname, const char *tname);
     799             : extern bool is_column_unique(sql_column *c);
     800             : sql_export sql_part *partition_find_part(sql_trans *tr, sql_table *pt, sql_part *pp);
     801             : extern node *members_find_child_id(list *l, sqlid id);
     802             : 
     803             : #define outside_str 1
     804             : #define inside_str 2
     805             : 
     806             : #define extracting_schema 1
     807             : #define extracting_sequence 2
     808             : 
     809             : static inline void
     810         157 : extract_schema_and_sequence_name(sql_allocator *sa, char *default_value, char **schema, char **sequence)
     811             : {
     812             :         int status = outside_str, identifier = extracting_schema;
     813             :         char next_identifier[1024]; /* needs one extra character for null terminator */
     814             :         size_t bp = 0;
     815             : 
     816        2509 :         for (size_t i = 0; default_value[i]; i++) {
     817             :                 char next = default_value[i];
     818             : 
     819        2509 :                 if (next == '"') {
     820         472 :                         if (status == inside_str && default_value[i + 1] == '"') {
     821           1 :                                 next_identifier[bp++] = '"';
     822           1 :                                 i++; /* has to advance two positions */
     823         471 :                         } else if (status == inside_str) {
     824         314 :                                 next_identifier[bp++] = '\0';
     825         314 :                                 if (identifier == extracting_schema) {
     826         157 :                                         *schema = SA_STRDUP(sa, next_identifier);
     827             :                                         identifier = extracting_sequence;
     828         157 :                                 } else if (identifier == extracting_sequence) {
     829         157 :                                         *sequence = SA_STRDUP(sa, next_identifier);
     830         157 :                                         break; /* done extracting */
     831             :                                 }
     832             :                                 bp = 0;
     833             :                                 status = outside_str;
     834             :                         } else {
     835         157 :                                 assert(status == outside_str);
     836             :                                 status = inside_str;
     837             :                         }
     838        2037 :                 } else if (next == '.') {
     839         157 :                         if (status == outside_str && default_value[i + 1] == '"') {
     840             :                                 status = inside_str;
     841             :                                 i++; /* has to advance two positions */
     842             :                         } else {
     843           0 :                                 assert(status == inside_str);
     844           0 :                                 next_identifier[bp++] = '.'; /* used inside an identifier name */
     845             :                         }
     846        1880 :                 } else if (status == inside_str) {
     847        1880 :                         next_identifier[bp++] = next;
     848             :                 } else {
     849           0 :                         assert(status == outside_str);
     850             :                 }
     851             :         }
     852         157 : }
     853             : 
     854             : extern void arg_destroy(sql_store store, sql_arg *a);
     855             : extern void part_value_destroy(sql_store store, sql_part_value *pv);
     856             : 
     857             : typedef struct atom {
     858             :         int isnull;
     859             :         sql_subtype tpe;
     860             :         ValRecord data;
     861             : } atom;
     862             : 
     863             : /* duplicate atom */
     864             : extern ValPtr SA_VALcopy(sql_allocator *sa, ValPtr d, const ValRecord *s);
     865             : extern atom *atom_dup(sql_allocator *sa, atom *a);
     866             : 
     867             : typedef struct pl {
     868             :         sql_column *c;
     869             :         unsigned int cmp;
     870             :         atom *r; /* if r is NULL then a full match is required */
     871             :         atom *f; /* make it match range expressions */
     872             :         uint8_t
     873             :          anti:1,
     874             :          semantics:1;
     875             : } pl;
     876             : 
     877             : #endif /* SQL_CATALOG_H */

Generated by: LCOV version 1.14