LCOV - code coverage report
Current view: top level - sql/server - rel_schema.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1718 2167 79.3 %
Date: 2024-10-04 20:04:04 Functions: 61 61 100.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             : #include "monetdb_config.h"
      14             : #include "rel_trans.h"
      15             : #include "rel_rel.h"
      16             : #include "rel_basetable.h"
      17             : #include "rel_select.h"
      18             : #include "rel_updates.h"
      19             : #include "rel_exp.h"
      20             : #include "rel_schema.h"
      21             : #include "rel_remote.h"
      22             : #include "rel_psm.h"
      23             : #include "rel_dump.h"
      24             : #include "rel_propagate.h"
      25             : #include "rel_unnest.h"
      26             : #include "sql_parser.h"
      27             : #include "sql_privileges.h"
      28             : #include "sql_partition.h"
      29             : 
      30             : #include "mal_authorize.h"
      31             : #include "mal_exception.h"
      32             : 
      33             : sql_rel *
      34       33899 : rel_table(mvc *sql, int cat_type, const char *sname, sql_table *t, int nr)
      35             : {
      36       33899 :         sql_rel *rel = rel_create(sql->sa);
      37       33899 :         list *exps = new_exp_list(sql->sa);
      38       33899 :         if (!rel || !exps)
      39             :                 return NULL;
      40             : 
      41       33899 :         append(exps, exp_atom_int(sql->sa, nr));
      42       33899 :         append(exps, exp_atom_str(sql->sa, sname, sql_bind_localtype("str") ));
      43       33899 :         append(exps, exp_atom_str(sql->sa, t->base.name, sql_bind_localtype("str") ));
      44       33899 :         append(exps, exp_atom_ptr(sql->sa, t));
      45       33899 :         rel->l = rel_basetable(sql, t, t->base.name);
      46       33899 :         rel->r = NULL;
      47       33899 :         rel->op = op_ddl;
      48       33899 :         rel->flag = cat_type;
      49       33899 :         rel->exps = exps;
      50       33899 :         rel->card = CARD_MULTI;
      51       33899 :         rel->nrcols = 0;
      52       33899 :         return rel;
      53             : }
      54             : 
      55             : static sql_rel *
      56         101 : rel_create_remote(mvc *sql, int cat_type, const char *sname, sql_table *t, int pw_encrypted, const char *username, const char *passwd)
      57             : {
      58         101 :         sql_rel *rel = rel_create(sql->sa);
      59         101 :         list *exps = new_exp_list(sql->sa);
      60         101 :         if (!rel || !exps)
      61             :                 return NULL;
      62             : 
      63         101 :         append(exps, exp_atom_int(sql->sa, pw_encrypted));
      64         101 :         append(exps, exp_atom_str(sql->sa, sname, sql_bind_localtype("str") ));
      65         101 :         append(exps, exp_atom_str(sql->sa, t->base.name, sql_bind_localtype("str") ));
      66         101 :         append(exps, exp_atom_ptr(sql->sa, t));
      67         101 :         append(exps, exp_atom_str(sql->sa, username, sql_bind_localtype("str") ));
      68         101 :         append(exps, exp_atom_str(sql->sa, passwd, sql_bind_localtype("str") ));
      69         101 :         rel->l = rel_basetable(sql, t, t->base.name);
      70         101 :         rel->r = NULL;
      71         101 :         rel->op = op_ddl;
      72         101 :         rel->flag = cat_type;
      73         101 :         rel->exps = exps;
      74         101 :         rel->card = CARD_MULTI;
      75         101 :         rel->nrcols = 0;
      76         101 :         return rel;
      77             : }
      78             : 
      79             : static sql_rel *
      80       22358 : rel_create_view_ddl(mvc *sql, int cat_type, const char *sname, sql_table *t, int nr, int replace)
      81             : {
      82       22358 :         sql_rel *rel = rel_table(sql, cat_type, sname, t, nr);
      83       22358 :         if (!rel)
      84             :                 return NULL;
      85       22358 :         append(rel->exps, exp_atom_int(sql->sa, replace));
      86       22358 :         return rel;
      87             : }
      88             : 
      89             : static sql_rel *
      90        2802 : rel_alter_table(allocator *sa, int cattype, char *sname, char *tname, char *sname2, char *tname2, int action)
      91             : {
      92        2802 :         sql_rel *rel = rel_create(sa);
      93        2802 :         list *exps = new_exp_list(sa);
      94        2802 :         if (!rel || !exps)
      95             :                 return NULL;
      96             : 
      97        2802 :         append(exps, exp_atom_clob(sa, sname));
      98        2802 :         append(exps, exp_atom_clob(sa, tname));
      99        2802 :         assert((sname2 && tname2) || (!sname2 && !tname2));
     100        2802 :         if (sname2) {
     101         492 :                 append(exps, exp_atom_clob(sa, sname2));
     102         492 :                 append(exps, exp_atom_clob(sa, tname2));
     103             :         }
     104        2802 :         append(exps, exp_atom_int(sa, action));
     105        2802 :         rel->l = NULL;
     106        2802 :         rel->r = NULL;
     107        2802 :         rel->op = op_ddl;
     108        2802 :         rel->flag = cattype;
     109        2802 :         rel->exps = exps;
     110        2802 :         rel->card = CARD_MULTI;
     111        2802 :         rel->nrcols = 0;
     112        2802 :         return rel;
     113             : }
     114             : 
     115             : static sql_rel *
     116       12155 : view_rename_columns(mvc *sql, const char *name, sql_rel *sq, dlist *column_spec)
     117             : {
     118       12155 :         dnode *n = column_spec->h;
     119       12155 :         node *m = sq->exps->h, *p = m;
     120             : 
     121       12155 :         assert(is_project(sq->op));
     122       53926 :         for (; n && m; n = n->next, p = m, m = m->next) {
     123       41771 :                 char *cname = n->data.sval;
     124       41771 :                 sql_exp *e = m->data;
     125       41771 :                 sql_exp *n = e;
     126             : 
     127       41771 :                 exp_setname(sql, n, name, cname);
     128       41771 :                 set_basecol(n);
     129             :         }
     130             :         /* skip any intern columns */
     131       12155 :         for (; m; m = m->next) {
     132           0 :                 sql_exp *e = m->data;
     133           0 :                 if (!is_intern(e))
     134             :                         break;
     135             :         }
     136       12155 :         if (p)
     137       12155 :                 p->next = 0;
     138       12155 :         if (n || m)
     139           0 :                 return sql_error(sql, 02, SQLSTATE(M0M03) "Column lists do not match");
     140       12155 :         list_hash_clear(sq->exps);
     141       12155 :         set_processed(sq);
     142       12155 :         return sq;
     143             : }
     144             : 
     145             : static int
     146       22468 : as_subquery(mvc *sql, sql_table *t, table_types tt, sql_rel *sq, dlist *column_spec, const char *msg)
     147             : {
     148       22468 :         sql_rel *r = sq;
     149             : 
     150       22468 :         if (!r)
     151             :                 return 0;
     152       22468 :         if (column_spec) {
     153         689 :                 dnode *n = column_spec->h;
     154         689 :                 node *m = r->exps->h;
     155             : 
     156        3391 :                 for (; n && m; n = n->next, m = m->next) {
     157        2702 :                         char *cname = n->data.sval;
     158        2702 :                         sql_exp *e = m->data;
     159        2702 :                         sql_subtype *tp = exp_subtype(e);
     160        2702 :                         sql_column *col = NULL;
     161             : 
     162        2702 :                         if (tt != tt_view && cname && cname[0] == '%') {
     163           0 :                                 sql_error(sql, 01, SQLSTATE(42000) "%s: generated labels not allowed in column names, use an alias instead", msg);
     164           0 :                                 return -1;
     165        2702 :                         } else if (!tp) {
     166           0 :                                 sql_error(sql, 01, SQLSTATE(42000) "%s: columns must have a type defined", msg);
     167           0 :                                 return -1;
     168        2702 :                         } else if (tp->type->eclass == EC_ANY) {
     169           0 :                                 sql_error(sql, 01, SQLSTATE(42000) "%s: any type (plain null value) not allowed as a column type, use an explicit cast", msg);
     170           0 :                                 return -1;
     171        2702 :                         } else if (mvc_bind_column(sql, t, cname)) {
     172           0 :                                 sql_error(sql, 01, SQLSTATE(42S21) "%s: duplicate column name %s", msg, cname);
     173           0 :                                 return -1;
     174             :                         }
     175        2702 :                         switch (mvc_create_column(&col, sql, t, cname, tp)) {
     176           0 :                                 case -1:
     177           0 :                                         sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     178           0 :                                         return -1;
     179           0 :                                 case -2:
     180             :                                 case -3:
     181           0 :                                         sql_error(sql, 01, SQLSTATE(42000) "%s: transaction conflict detected", msg);
     182           0 :                                         return -1;
     183             :                                 default:
     184        2702 :                                         break;
     185             :                         }
     186             :                 }
     187         689 :                 if (n || m) {
     188           0 :                         sql_error(sql, 01, SQLSTATE(21S02) "%s: number of columns does not match", msg);
     189           0 :                         return -1;
     190             :                 }
     191             :         } else {
     192      222402 :                 for (node *m = r->exps->h; m; m = m->next) {
     193      200627 :                         sql_exp *e = m->data;
     194      200627 :                         const char *cname = exp_name(e);
     195      200627 :                         sql_subtype *tp = exp_subtype(e);
     196      200627 :                         sql_column *col = NULL;
     197             : 
     198      200627 :                         if (!cname)
     199           4 :                                 cname = "v";
     200      200627 :                         if (tt != tt_view && cname[0] == '%') {
     201           1 :                                 sql_error(sql, 01, SQLSTATE(42000) "%s: generated labels not allowed in column names, use an alias instead", msg);
     202           4 :                                 return -1;
     203      200626 :                         } else if (!tp) {
     204           0 :                                 sql_error(sql, 01, SQLSTATE(42000) "%s: columns must have a type defined", msg);
     205           0 :                                 return -1;
     206      200626 :                         } else if (tp->type->eclass == EC_ANY) {
     207           1 :                                 sql_error(sql, 01, SQLSTATE(42000) "%s: any type (plain null value) not allowed as a column type, use an explicit cast", msg);
     208           1 :                                 return -1;
     209      200625 :                         } else if (mvc_bind_column(sql, t, cname)) {
     210           2 :                                 sql_error(sql, 01, SQLSTATE(42S21) "%s: duplicate column name %s", msg, cname);
     211           2 :                                 return -1;
     212             :                         }
     213      200623 :                         switch (mvc_create_column(&col, sql, t, cname, tp)) {
     214           0 :                                 case -1:
     215           0 :                                         sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     216           0 :                                         return -1;
     217           0 :                                 case -2:
     218             :                                 case -3:
     219           0 :                                         sql_error(sql, 01, SQLSTATE(42000) "%s: transaction conflict detected", msg);
     220           0 :                                         return -1;
     221             :                                 default:
     222      200623 :                                         break;
     223             :                         }
     224             :                 }
     225             :         }
     226             :         return 0;
     227             : }
     228             : 
     229             : sql_table *
     230         105 : mvc_create_table_as_subquery(mvc *sql, sql_rel *sq, sql_schema *s, const char *tname, dlist *column_spec, int temp, int commit_action, const char *action)
     231             : {
     232         105 :         sql_table *t = NULL;
     233         105 :         table_types tt =(temp == SQL_REMOTE)?tt_remote:
     234             :                 (temp == SQL_MERGE_TABLE)?tt_merge_table:
     235             :                 (temp == SQL_REPLICA_TABLE)?tt_replica_table:
     236             :                 (temp == SQL_UNLOGGED_TABLE)?tt_unlogged_table:tt_table;
     237             : 
     238         105 :         switch (mvc_create_table(&t, sql, s, tname, tt, 0, SQL_DECLARED_TABLE, commit_action, -1, 0)) {
     239           0 :                 case -1:
     240           0 :                         return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     241           0 :                 case -2:
     242             :                 case -3:
     243           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: transaction conflict detected", action);
     244           0 :                 case -4:
     245           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: the partition's expression is too long", action);
     246             :                 case -5:
     247             :                         return NULL;
     248             :                 default:
     249         105 :                         break;
     250             :         }
     251         105 :         if (as_subquery(sql, t, tt, sq, column_spec, action) != 0)
     252             :                 return NULL;
     253         102 :         return t;
     254             : }
     255             : 
     256             : sql_table *
     257           4 : mvc_create_remote_as_subquery(mvc *sql, sql_rel *sq, sql_schema *s, const char *tname, dlist *column_spec, const char *loc, const char *action)
     258             : {
     259           4 :         sql_table *t = NULL;
     260           4 :         switch(mvc_create_remote(&t, sql, s, tname, SQL_DECLARED_TABLE, loc)) {
     261           0 :                 case -1:
     262           0 :                         return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     263           0 :                 case -2:
     264             :                 case -3:
     265           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: transaction conflict detected", action);
     266           0 :                 case -4:
     267           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: the partition's expression is too long", action);
     268             :                 case -5:
     269             :                         return NULL;
     270             :                 default:
     271           4 :                         break;
     272             :         }
     273           4 :         if (as_subquery(sql, t, tt_remote, sq, column_spec, action) != 0)
     274             :                 return NULL;
     275           4 :         return t;
     276             : }
     277             : 
     278             : static char *
     279         894 : table_constraint_name(mvc *sql, symbol *s, sql_schema *ss, sql_table *t)
     280             : {
     281             :         /* create a descriptive name like table_col_pkey */
     282         894 :         char *suffix;           /* stores the type of this constraint */
     283         894 :         dnode *nms = NULL;
     284         894 :         char *buf;
     285         894 :         size_t buflen, len, slen;
     286             : 
     287         894 :         switch (s->token) {
     288          50 :                 case SQL_UNIQUE:
     289          50 :                         suffix = "_unique";
     290          50 :                         nms = s->data.lval->h;    /* list of columns */
     291         890 :                         break;
     292         463 :                 case SQL_PRIMARY_KEY:
     293         463 :                         suffix = "_pkey";
     294         463 :                         nms = s->data.lval->h;    /* list of columns */
     295         463 :                         break;
     296         377 :                 case SQL_FOREIGN_KEY:
     297         377 :                         suffix = "_fkey";
     298         377 :                         nms = s->data.lval->h->next->data.lval->h;       /* list of columns */
     299         377 :                         break;
     300             :                 case SQL_CHECK:
     301           3 :                         suffix = "_check";
     302           3 :                         char name[512], name2[512], *nme, *nme2;
     303           3 :                         bool found;
     304           3 :                         do {
     305           3 :                                 nme = number2name(name, sizeof(name), ++sql->label);
     306           3 :                                 buflen = snprintf(name2, sizeof(name2), "%s_%s%s", t->base.name, nme, suffix);
     307           3 :                                 nme2 = name2;
     308           3 :                                 found = ol_find_name(t->keys, nme2) || mvc_bind_key(sql, ss, nme2);
     309           3 :                         } while (found);
     310           3 :                         buf = SA_NEW_ARRAY(sql->ta, char, buflen);
     311           3 :                         strcpy(buf, nme2);
     312           3 :                         return buf;
     313           1 :                 default:
     314           1 :                         suffix = "_?";
     315           1 :                         nms = NULL;
     316             :         }
     317             : 
     318             :         /* copy table name */
     319         891 :         len = strlen(t->base.name);
     320         891 :         buflen = BUFSIZ;
     321         891 :         slen = strlen(suffix);
     322         891 :         while (len + slen >= buflen)
     323           0 :                 buflen += BUFSIZ;
     324         891 :         buf = SA_NEW_ARRAY(sql->ta, char, buflen);
     325         891 :         strcpy(buf, t->base.name);
     326             : 
     327             :         /* add column name(s) */
     328        2154 :         for (; nms; nms = nms->next) {
     329        1263 :                 slen = strlen(nms->data.sval);
     330        1263 :                 while (len + slen + 1 >= buflen) {
     331           0 :                         size_t nbuflen = buflen + BUFSIZ;
     332           0 :                         char *nbuf = SA_RENEW_ARRAY(sql->ta, char, buf, nbuflen, buflen);
     333             :                         buf = nbuf;
     334             :                         buflen = nbuflen;
     335             :                 }
     336        1263 :                 snprintf(buf + len, buflen - len, "_%s", nms->data.sval);
     337        1263 :                 len += slen + 1;
     338             :         }
     339             : 
     340             :         /* add suffix */
     341         891 :         slen = strlen(suffix);
     342         891 :         while (len + slen >= buflen) {
     343           0 :                 size_t nbuflen = buflen + BUFSIZ;
     344           0 :                 char *nbuf = SA_RENEW_ARRAY(sql->ta, char, buf, nbuflen, buflen);
     345             :                 buf = nbuf;
     346             :                 buflen = nbuflen;
     347             :         }
     348         891 :         snprintf(buf + len, buflen - len, "%s", suffix);
     349         891 :         return buf;
     350             : }
     351             : 
     352             : static char *
     353       21723 : column_constraint_name(mvc *sql, symbol *s, sql_column *sc, sql_table *t)
     354             : {
     355             :         /* create a descriptive name like table_col_pkey */
     356       21723 :         char *suffix /* stores the type of this constraint */, *buf;
     357       21723 :         size_t buflen;
     358             : 
     359       21723 :         switch (s->token) {
     360             :                 case SQL_UNIQUE:
     361             :                         suffix = "unique";
     362             :                         break;
     363           4 :                 case SQL_UNIQUE_NULLS_NOT_DISTINCT:
     364           4 :                         suffix = "nndunique";
     365           4 :                         break;
     366        2858 :                 case SQL_PRIMARY_KEY:
     367        2858 :                         suffix = "pkey";
     368        2858 :                         break;
     369          57 :                 case SQL_FOREIGN_KEY:
     370          57 :                         suffix = "fkey";
     371          57 :                         break;
     372          17 :                 case SQL_CHECK:
     373          17 :                         suffix = "check";
     374          17 :                         break;
     375       17053 :                 default:
     376       17053 :                         suffix = "?";
     377             :         }
     378             : 
     379       21723 :         buflen = strlen(t->base.name) + strlen(sc->base.name) + strlen(suffix) + 3;
     380       21723 :         buf = SA_NEW_ARRAY(sql->ta, char, buflen);
     381       21723 :         snprintf(buf, buflen, "%s_%s_%s", t->base.name, sc->base.name, suffix);
     382       21723 :         return buf;
     383             : }
     384             : 
     385             : #define COL_NULL        0
     386             : #define COL_DEFAULT 1
     387             : 
     388             : static bool
     389         971 : foreign_key_check_types(sql_subtype *lt, sql_subtype *rt)
     390             : {
     391         971 :         if (lt->type->eclass == EC_EXTERNAL && rt->type->eclass == EC_EXTERNAL)
     392           1 :                 return lt->type->localtype == rt->type->localtype;
     393        1931 :         return lt->type->eclass == rt->type->eclass || (EC_VARCHAR(lt->type->eclass) && EC_VARCHAR(rt->type->eclass));
     394             : }
     395             : 
     396             : static key_type
     397        5978 : token2key_type(int token)
     398             : {
     399        5978 :                 switch (token) {
     400             :                 case SQL_UNIQUE:                                        return ukey;
     401           7 :                 case SQL_UNIQUE_NULLS_NOT_DISTINCT:     return unndkey;
     402        3885 :                 case SQL_PRIMARY_KEY:                           return pkey;
     403          38 :                 case SQL_CHECK:                                         return ckey;
     404             :                 }
     405           0 :                 assert(0);
     406             :                 return -1;
     407             : }
     408             : 
     409             : static sql_rel*
     410          38 : create_check_plan(sql_query *query, symbol *s, sql_table *t)
     411             : {
     412          38 :         mvc *sql = query->sql;
     413          38 :         exp_kind ek = {type_value, card_value, FALSE};
     414          38 :         sql_rel *rel = rel_basetable(sql, t, t->base.name);
     415          38 :         sql_exp *e = rel_logical_value_exp(query, &rel, s->data.lval->h->data.sym, sql_sel | sql_no_subquery, ek);
     416             : 
     417          38 :         if (!e || !rel || !is_basetable(rel->op))
     418             :                 return NULL;
     419          32 :         e->comment = sa_strdup(sql->sa, s->data.lval->h->next->data.sval);
     420          32 :         rel->exps = rel_base_projection(sql, rel, 0);
     421          32 :         list *pexps = sa_list(sql->sa);
     422          32 :         pexps = append(pexps, e);
     423          32 :         rel = rel_project(sql->sa, rel, pexps);
     424          32 :         return rel;
     425             : }
     426             : 
     427             : static int
     428       21728 : column_constraint_type(sql_query *query, const char *name, symbol *s, sql_schema *ss, sql_table *t, sql_column *cs, bool isDeclared, int *used)
     429             : {
     430       21728 :         mvc *sql = query->sql;
     431       21728 :         int res = SQL_ERR;
     432             : 
     433       21728 :         if (isDeclared && (s->token != SQL_NULL && s->token != SQL_NOT_NULL)) {
     434           1 :                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT: constraints on declared tables are not supported");
     435           1 :                 return res;
     436             :         }
     437       21727 :         switch (s->token) {
     438        4616 :         case SQL_UNIQUE:
     439             :         case SQL_UNIQUE_NULLS_NOT_DISTINCT:
     440             :         case SQL_PRIMARY_KEY:
     441             :         case SQL_CHECK: {
     442        4616 :                 key_type kt = token2key_type(s->token);
     443        4616 :                 sql_key *k;
     444        4616 :                 const char *ns = name;
     445             : 
     446        4616 :                 if (kt == pkey && t->pkey) {
     447           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT PRIMARY KEY: a table can have only one PRIMARY KEY");
     448           3 :                         return res;
     449             :                 }
     450        4616 :                 if (!ns || !*ns) { /* add this to be safe */
     451           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name name cannot be empty", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
     452           0 :                         return res;
     453             :                 }
     454        4616 :                 while (isdigit((unsigned char) *ns))
     455           0 :                         ns++;
     456        4616 :                 if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
     457           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name cannot contain just digit characters (0 through 9)", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
     458           0 :                         return res;
     459             :                 }
     460        4616 :                 if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
     461           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key %s already exists", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE", name);
     462           0 :                         return res;
     463             :                 }
     464        4616 :                 if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
     465           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: an index named '%s' already exists, and it would conflict with the key", kt == pkey ? "PRIMARY KEY" : "UNIQUE", name);
     466           0 :                         return res;
     467             :                 }
     468        4616 :                 char* check = NULL;
     469        4616 :                 sql_rel* check_rel = NULL;
     470        4616 :                 if (kt == ckey) {
     471          17 :                         if ((check_rel = create_check_plan(query, s, t)) == NULL) {
     472             :                                 return -3;
     473             :                         }
     474          14 :                         check = exp2str(sql, check_rel->exps->h->data);
     475             :                 }
     476        4613 :                 switch (mvc_create_ukey(&k, sql, t, name, kt, check)) {
     477           0 :                         case -1:
     478           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     479           0 :                                 return res;
     480           0 :                         case -2:
     481             :                         case -3:
     482           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
     483           0 :                                 return res;
     484             :                         default:
     485        4613 :                                 break;
     486             :                 }
     487        4613 :                 if (check) {
     488          14 :                         sql_rel* btrel = check_rel->l;
     489          14 :                         node* n = NULL;
     490          28 :                         for (n = btrel->exps->h; n; n = n->next) {
     491          14 :                                 sql_exp* e = n->data;
     492          14 :                                 const char *nm = e->alias.name;
     493          14 :                                 sql_column *c = mvc_bind_column(sql, t, nm);
     494          14 :                                 if (!c) {
     495           0 :                                         (void) sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CONSTRAINT CHECK: no such column '%s' for table '%s'",
     496             :                                                         nm, t->base.name);
     497           0 :                                         return SQL_ERR;
     498             :                                 }
     499          14 :                                 switch (mvc_create_kc(sql, k, c)) {
     500           0 :                                         case -1:
     501           0 :                                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     502           0 :                                                 return SQL_ERR;
     503           0 :                                         case -2:
     504             :                                         case -3:
     505           0 :                                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT CHECK: transaction conflict detected");
     506           0 :                                                 return SQL_ERR;
     507             :                                         default:
     508          14 :                                                 break;
     509             :                                 }
     510             :                         }
     511             :                 }
     512             :                 else
     513        4599 :                 switch (mvc_create_kc(sql, k, cs)) {
     514           0 :                         case -1:
     515           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     516           0 :                                 return res;
     517           0 :                         case -2:
     518             :                         case -3:
     519           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
     520           0 :                                 return res;
     521             :                         default:
     522             :                                 break;
     523             :                 }
     524        4613 :                 switch (mvc_create_key_done(sql, k)) {
     525           0 :                         case -1:
     526           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     527           0 :                                 return res;
     528           0 :                         case -2:
     529             :                         case -3:
     530           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
     531           0 :                                 return res;
     532             :                         default:
     533        4613 :                                 break;
     534             :                 }
     535        4613 :                 res = SQL_OK;
     536        4613 :         }       break;
     537          58 :         case SQL_FOREIGN_KEY: {
     538          58 :                 dnode *n = s->data.lval->h;
     539          58 :                 char *rsname = qname_schema(n->data.lval);
     540          58 :                 char *rtname = qname_schema_object(n->data.lval);
     541          58 :                 int ref_actions = n->next->next->next->data.i_val;
     542          58 :                 sql_table *rt;
     543          58 :                 sql_fkey *fk;
     544          58 :                 list *cols;
     545          58 :                 sql_key *rk = NULL;
     546          58 :                 sql_kc *kc;
     547          58 :                 const char *ns = name;
     548             : 
     549          58 :                 assert(n->next->next->next->type == type_int);
     550          58 :                 rt = find_table_or_view_on_scope(sql, ss, rsname, rtname, "CONSTRAINT FOREIGN KEY", false);
     551             :                 /* self referenced table */
     552          58 :                 if (!rt && t->s == ss && strcmp(t->base.name, rtname) == 0) {
     553           1 :                         sql->errstr[0] = '\0'; /* reset table not found error */
     554           1 :                         sql->session->status = 0;
     555           1 :                         rt = t;
     556             :                 }
     557           1 :                 if (!rt)
     558           1 :                         return SQL_ERR;
     559          58 :                 if (!rt->s) { /* disable foreign key on declared table */
     560           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key with declared tables");
     561           0 :                         return res;
     562             :                 }
     563          58 :                 if (isTempSchema(t->s) != isTempSchema(rt->s)) { /* disable foreign key between temp and non temp */
     564           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between temporary and non temporary tables");
     565           0 :                         return res;
     566             :                 }
     567          58 :                 if (isUnloggedTable(t) != isUnloggedTable(rt)) { /* disable foreign key between logged and unlogged */
     568           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between logged and unlogged tables");
     569           0 :                         return res;
     570             :                 }
     571          58 :                 if (!ns || !*ns) { /* add this to be safe */
     572           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name name cannot be empty");
     573           0 :                         return res;
     574             :                 }
     575          58 :                 while (isdigit((unsigned char) *ns))
     576           0 :                         ns++;
     577          58 :                 if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
     578           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name cannot contain just digit characters (0 through 9)");
     579           0 :                         return res;
     580             :                 }
     581          58 :                 if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
     582           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key '%s' already exists", name);
     583           0 :                         return res;
     584             :                 }
     585          58 :                 if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
     586           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: an index named '%s' already exists, and it would conflict with the key", name);
     587           0 :                         return res;
     588             :                 }
     589             : 
     590             :                 /* find unique referenced key */
     591          58 :                 if (n->next->data.lval) {
     592          53 :                         char *rcname = n->next->data.lval->h->data.sval;
     593             : 
     594          53 :                         cols = list_append(sa_list(sql->sa), rcname);
     595          53 :                         rk = mvc_bind_ukey(rt, cols);
     596           5 :                 } else if (rt->pkey) {
     597             :                         /* no columns specified use rt.pkey */
     598           5 :                         rk = &rt->pkey->k;
     599             :                 }
     600          58 :                 if (!rk) {
     601           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: could not find referenced PRIMARY KEY in table %s.%s", rsname, rtname);
     602           0 :                         return res;
     603             :                 }
     604          58 :                 if (list_length(rk->columns) != 1) {
     605           1 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: not all columns are handled");
     606           1 :                         return res;
     607             :                 }
     608          57 :                 kc = rk->columns->h->data;
     609          57 :                 if (!foreign_key_check_types(&cs->type, &kc->c->type)) {
     610           0 :                         str tp1 = sql_subtype_string(sql->ta, &cs->type), tp2 = sql_subtype_string(sql->ta, &kc->c->type);
     611           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: the type of the FOREIGN KEY column '%s' %s is not compatible with the referenced %s KEY column type %s",
     612           0 :                                                          cs->base.name, tp1, rk->type == pkey ? "PRIMARY" : "UNIQUE", tp2);
     613           0 :                         return res;
     614             :                 }
     615          57 :                 switch (mvc_create_fkey(&fk, sql, t, name, fkey, rk, ref_actions & 255, (ref_actions>>8) & 255)) {
     616           0 :                         case -1:
     617           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     618           0 :                                 return res;
     619           0 :                         case -2:
     620             :                         case -3:
     621           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
     622           0 :                                 return res;
     623             :                         default:
     624          57 :                                 break;
     625             :                 }
     626          57 :                 switch (mvc_create_fkc(sql, fk, cs)) {
     627           0 :                         case -1:
     628           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     629           0 :                                 return res;
     630           0 :                         case -2:
     631             :                         case -3:
     632           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
     633           0 :                                 return res;
     634             :                         default:
     635          57 :                                 break;
     636             :                 }
     637          57 :                 switch (mvc_create_key_done(sql, (sql_key*)fk)) {
     638           0 :                         case -1:
     639           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     640           0 :                                 return res;
     641           0 :                         case -2:
     642             :                         case -3:
     643           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
     644           0 :                                 return res;
     645             :                         default:
     646          57 :                                 break;
     647             :                 }
     648          57 :                 res = SQL_OK;
     649          57 :         }       break;
     650       17053 :         case SQL_NOT_NULL:
     651             :         case SQL_NULL: {
     652       17053 :                 int null = (s->token != SQL_NOT_NULL);
     653             : 
     654       17053 :                 if (((*used)&(1<<COL_NULL))) {
     655           1 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "NULL constraint for a column may be specified at most once");
     656           1 :                         return SQL_ERR;
     657             :                 }
     658       17052 :                 *used |= (1<<COL_NULL);
     659             : 
     660       17052 :                 switch (mvc_null(sql, cs, null)) {
     661           0 :                         case -1:
     662           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     663           0 :                                 return SQL_ERR;
     664           0 :                         case -2:
     665             :                         case -3:
     666           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "NULL CONSTRAINT: transaction conflict detected");
     667           0 :                                 return SQL_ERR;
     668             :                         default:
     669             :                                 break;
     670             :                 }
     671             :                 res = SQL_OK;
     672             :         }       break;
     673             :         default:{
     674             :                 res = SQL_ERR;
     675             :         }
     676             :         }
     677        4670 :         if (res == SQL_ERR) {
     678           0 :                 (void) sql_error(sql, 02, SQLSTATE(M0M03) "Unknown constraint (%p)->token = %s", s, token2string(s->token));
     679             :         }
     680             :         return res;
     681             : }
     682             : 
     683             : static int
     684       53103 : column_options(sql_query *query, dlist *opt_list, sql_schema *ss, sql_table *t, sql_column *cs, bool isDeclared)
     685             : {
     686       53103 :         mvc *sql = query->sql;
     687       53103 :         int res = SQL_OK, used = 0;
     688       53103 :         assert(cs);
     689             : 
     690       53103 :         if (opt_list) {
     691       41170 :                 for (dnode *n = opt_list->h; n && res == SQL_OK; n = n->next) {
     692       23123 :                         symbol *s = n->data.sym;
     693             : 
     694       23123 :                         switch (s->token) {
     695       21728 :                                 case SQL_CONSTRAINT: {
     696       21728 :                                         dlist *l = s->data.lval;
     697       21728 :                                         char *opt_name = l->h->data.sval, *default_name = NULL;
     698       21728 :                                         symbol *sym = l->h->next->data.sym;
     699             : 
     700       21728 :                                         if (!opt_name && !(default_name = column_constraint_name(sql, sym, cs, t)))
     701             :                                                 return SQL_ERR;
     702             : 
     703       21728 :                                         res = column_constraint_type(query, opt_name ? opt_name : default_name, sym, ss, t, cs, isDeclared, &used);
     704       21728 :                                 }       break;
     705        1395 :                                 case SQL_DEFAULT: {
     706        1395 :                                         symbol *sym = s->data.sym;
     707        1395 :                                         char *err = NULL, *r;
     708             : 
     709        1395 :                                         if ((used&(1<<COL_DEFAULT))) {
     710           1 :                                                 (void) sql_error(sql, 02, SQLSTATE(42000) "A default value for a column may be specified at most once");
     711           4 :                                                 return SQL_ERR;
     712             :                                         }
     713        1394 :                                         used |= (1<<COL_DEFAULT);
     714             : 
     715        1394 :                                         if (sym->token == SQL_COLUMN || sym->token == SQL_IDENT || sym->token == SQL_NEXT) {
     716         265 :                                                 exp_kind ek = {type_value, card_value, FALSE};
     717         265 :                                                 sql_exp *e = rel_logical_value_exp(query, NULL, sym, sql_sel, ek);
     718             : 
     719         265 :                                                 if (!e)
     720           2 :                                                         return SQL_ERR;
     721         263 :                                                 if (e && is_atom(e->type)) {
     722           0 :                                                         atom *a = exp_value(sql, e);
     723             : 
     724           0 :                                                         if (a && atom_null(a)) {
     725           0 :                                                                 switch (mvc_default(sql, cs, NULL)) {
     726           0 :                                                                         case -1:
     727           0 :                                                                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     728           0 :                                                                                 return SQL_ERR;
     729           0 :                                                                         case -2:
     730             :                                                                         case -3:
     731           0 :                                                                                 (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
     732           0 :                                                                                 return SQL_ERR;
     733             :                                                                         default:
     734           0 :                                                                                 break;
     735             :                                                                 }
     736           0 :                                                                 break;
     737             :                                                         }
     738             :                                                 }
     739             :                                                 /* reset error */
     740         263 :                                                 sql->session->status = 0;
     741         263 :                                                 sql->errstr[0] = '\0';
     742             :                                         }
     743        1392 :                                         r = symbol2string(sql, s->data.sym, 0, &err);
     744        1392 :                                         if (!r) {
     745           1 :                                                 (void) sql_error(sql, 02, SQLSTATE(42000) "Incorrect default value '%s'", err?err:"");
     746           1 :                                                 return SQL_ERR;
     747             :                                         } else {
     748        1391 :                                                 switch (mvc_default(sql, cs, r)) {
     749           0 :                                                         case -1:
     750           0 :                                                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     751           0 :                                                                 return SQL_ERR;
     752           0 :                                                         case -2:
     753             :                                                         case -3:
     754           0 :                                                                 (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
     755           0 :                                                                 return SQL_ERR;
     756             :                                                         default:
     757        1391 :                                                                 break;
     758             :                                                 }
     759             :                                         }
     760        1391 :                                 }       break;
     761           0 :                                 case SQL_NOT_NULL:
     762             :                                 case SQL_NULL: {
     763           0 :                                         int null = (s->token != SQL_NOT_NULL);
     764             : 
     765           0 :                                         if ((used&(1<<COL_NULL))) {
     766           0 :                                                 (void) sql_error(sql, 02, SQLSTATE(42000) "NULL constraint for a column may be specified at most once");
     767           0 :                                                 return SQL_ERR;
     768             :                                         }
     769           0 :                                         used |= (1<<COL_NULL);
     770             : 
     771           0 :                                         switch (mvc_null(sql, cs, null)) {
     772           0 :                                                 case -1:
     773           0 :                                                         (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     774           0 :                                                         return SQL_ERR;
     775           0 :                                                 case -2:
     776             :                                                 case -3:
     777           0 :                                                         (void) sql_error(sql, 02, SQLSTATE(42000) "NULL CONSTRAINT: transaction conflict detected");
     778           0 :                                                         return SQL_ERR;
     779             :                                                 default:
     780             :                                                         break;
     781             :                                         }
     782             :                                 }       break;
     783           0 :                                 default: {
     784           0 :                                         (void) sql_error(sql, 02, SQLSTATE(M0M03) "Unknown column option (%p)->token = %s", s, token2string(s->token));
     785           0 :                                         return SQL_ERR;
     786             :                                 }
     787             :                         }
     788             :                 }
     789             :         }
     790             :         return res;
     791             : }
     792             : 
     793             : static int
     794         897 : table_foreign_key(mvc *sql, const char *name, symbol *s, sql_schema *ss, sql_table *t)
     795             : {
     796         897 :         dnode *n = s->data.lval->h;
     797         897 :         char *rsname = qname_schema(n->data.lval);
     798         897 :         char *rtname = qname_schema_object(n->data.lval);
     799         897 :         const char *ns = name;
     800         897 :         sql_table *ft = NULL;
     801             : 
     802         897 :         ft = find_table_or_view_on_scope(sql, ss, rsname, rtname, "CONSTRAINT FOREIGN KEY", false);
     803             :         /* self referenced table */
     804         897 :         if (!ft && t->s == ss && strcmp(t->base.name, rtname) == 0) {
     805          10 :                 sql->errstr[0] = '\0'; /* reset table not found error */
     806          10 :                 sql->session->status = 0;
     807          10 :                 ft = t;
     808             :         }
     809          10 :         if (!ft) {
     810             :                 return SQL_ERR;
     811             :         } else {
     812         897 :                 sql_key *rk = NULL;
     813         897 :                 sql_fkey *fk;
     814         897 :                 dnode *nms = n->next->data.lval->h;
     815         897 :                 node *fnms;
     816         897 :                 int ref_actions = n->next->next->next->next->data.i_val;
     817             : 
     818         897 :                 assert(n->next->next->next->next->type == type_int);
     819         897 :                 if (!ft->s) { /* disable foreign key on declared table */
     820           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key with declared tables");
     821          26 :                         return SQL_ERR;
     822             :                 }
     823         897 :                 if (isTempSchema(t->s) != isTempSchema(ft->s)) { /* disable foreign key between temp and non temp */
     824           2 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between temporary and non temporary tables");
     825           2 :                         return SQL_ERR;
     826             :                 }
     827         895 :                 if (isTempTable(ft) && !isGlobal(ft) && ft != t) { /* disable foreign key on local temporary table */
     828           1 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key with local temporary tables");
     829           1 :                         return SQL_ERR;
     830             :                 }
     831         894 :                 if (isUnloggedTable(t) != isUnloggedTable(ft)) { /* disable foreign key between logged and unlogged */
     832           2 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between logged and unlogged tables");
     833           2 :                         return SQL_ERR;
     834             :                 }
     835         892 :                 if (!ns || !*ns) { /* add this to be safe */
     836           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name name cannot be empty");
     837           0 :                         return SQL_ERR;
     838             :                 }
     839         892 :                 while (isdigit((unsigned char) *ns))
     840           0 :                         ns++;
     841         892 :                 if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
     842           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name cannot contain just digit characters (0 through 9)");
     843           0 :                         return SQL_ERR;
     844             :                 }
     845         892 :                 if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
     846           2 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key '%s' already exists", name);
     847           2 :                         return SQL_ERR;
     848             :                 }
     849         890 :                 if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
     850           1 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: an index named '%s' already exists, and it would conflict with the key", name);
     851           1 :                         return SQL_ERR;
     852             :                 }
     853         889 :                 if (n->next->next->data.lval) {        /* find unique referenced key */
     854         852 :                         dnode *rnms = n->next->next->data.lval->h;
     855         852 :                         list *cols = sa_list(sql->sa);
     856             : 
     857        2587 :                         for (; rnms; rnms = rnms->next)
     858         883 :                                 list_append(cols, rnms->data.sval);
     859             : 
     860             :                         /* find key in ft->keys */
     861         852 :                         rk = mvc_bind_ukey(ft, cols);
     862          37 :                 } else if (ft->pkey) {
     863             :                         /* no columns specified use ft.pkey */
     864          37 :                         rk = &ft->pkey->k;
     865             :                 }
     866         889 :                 if (!rk) {
     867           5 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: could not find referenced PRIMARY KEY in table '%s'", ft->base.name);
     868           5 :                         return SQL_ERR;
     869             :                 }
     870         884 :                 switch (mvc_create_fkey(&fk, sql, t, name, fkey, rk, ref_actions & 255, (ref_actions>>8) & 255)) {
     871           0 :                         case -1:
     872           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     873           0 :                                 return SQL_ERR;
     874           0 :                         case -2:
     875             :                         case -3:
     876           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
     877           0 :                                 return SQL_ERR;
     878             :                         default:
     879         884 :                                 break;
     880             :                 }
     881             : 
     882        1789 :                 for (fnms = rk->columns->h; nms && fnms; nms = nms->next, fnms = fnms->next) {
     883         917 :                         char *nm = nms->data.sval;
     884         917 :                         sql_column *cs = mvc_bind_column(sql, t, nm);
     885         917 :                         sql_kc *kc = fnms->data;
     886             : 
     887         917 :                         if (!cs) {
     888           3 :                                 (void) sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CONSTRAINT FOREIGN KEY: no such column '%s' in table '%s'", nm, t->base.name);
     889           3 :                                 return SQL_ERR;
     890             :                         }
     891         914 :                         if (!foreign_key_check_types(&cs->type, &kc->c->type)) {
     892           9 :                                 str tp1 = sql_subtype_string(sql->ta, &cs->type), tp2 = sql_subtype_string(sql->ta, &kc->c->type);
     893          18 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: the type of the FOREIGN KEY column '%s' %s is not compatible with the referenced %s KEY column type %s",
     894           9 :                                                                  cs->base.name, tp1, rk->type == pkey ? "PRIMARY" : "UNIQUE", tp2);
     895           9 :                                 return SQL_ERR;
     896             :                         }
     897         905 :                         switch (mvc_create_fkc(sql, fk, cs)) {
     898           0 :                                 case -1:
     899           0 :                                         (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     900           0 :                                         return SQL_ERR;
     901           0 :                                 case -2:
     902             :                                 case -3:
     903           0 :                                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
     904           0 :                                         return SQL_ERR;
     905             :                                 default:
     906         905 :                                         break;
     907             :                         }
     908             :                 }
     909         872 :                 if (nms || fnms) {
     910           1 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: not all columns are handled");
     911           1 :                         return SQL_ERR;
     912             :                 }
     913         871 :                 switch (mvc_create_key_done(sql, (sql_key*)fk)) {
     914           0 :                         case -1:
     915           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     916           0 :                                 return SQL_ERR;
     917           0 :                         case -2:
     918             :                         case -3:
     919           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
     920           0 :                                 return SQL_ERR;
     921             :                         default:
     922         871 :                                 break;
     923             :                 }
     924             :         }
     925         871 :         return SQL_OK;
     926             : }
     927             : 
     928             : static int
     929        2259 : table_constraint_type(sql_query *query, const char *name, symbol *s, sql_schema *ss, sql_table *t)
     930             : {
     931        2259 :         mvc *sql = query->sql;
     932        2259 :         int res = SQL_OK;
     933             : 
     934        2259 :         switch (s->token) {
     935        1362 :         case SQL_UNIQUE:
     936             :         case SQL_UNIQUE_NULLS_NOT_DISTINCT:
     937             :         case SQL_PRIMARY_KEY:
     938             :         case SQL_CHECK: {
     939        1362 :                 key_type kt = token2key_type(s->token);
     940        1362 :                 dnode *nms = s->data.lval->h;
     941        1362 :                 sql_key *k;
     942        1362 :                 const char *ns = name;
     943             : 
     944        1362 :                 if (kt == pkey && t->pkey) {
     945           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT PRIMARY KEY: a table can have only one PRIMARY KEY");
     946          12 :                         return SQL_ERR;
     947             :                 }
     948        1362 :                 if (!ns || !*ns) { /* add this to be safe */
     949           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name name cannot be empty", kt == pkey ? "PRIMARY KEY" : "UNIQUE");
     950           0 :                         return SQL_ERR;
     951             :                 }
     952        1362 :                 while (isdigit((unsigned char) *ns))
     953           0 :                         ns++;
     954        1362 :                 if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
     955           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name cannot contain just digit characters (0 through 9)", kt == pkey ? "PRIMARY KEY" : "UNIQUE");
     956           0 :                         return SQL_ERR;
     957             :                 }
     958        1362 :                 if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
     959           3 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key '%s' already exists", kt == pkey ? "PRIMARY KEY" : "UNIQUE", name);
     960           3 :                         return SQL_ERR;
     961             :                 }
     962        1359 :                 if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
     963           0 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: an index named '%s' already exists, and it would conflict with the key", kt == pkey ? "PRIMARY KEY" : "UNIQUE", name);
     964           0 :                         return SQL_ERR;
     965             :                 }
     966        1359 :                 char* check = NULL;
     967        1359 :                 sql_rel* check_rel = NULL;
     968             : 
     969        1359 :                 if (kt == ckey) {
     970          21 :                         if ((check_rel = create_check_plan(query, s, t)) == NULL) {
     971             :                                 return -3;
     972             :                         }
     973          18 :                         check = exp2str(sql, check_rel->exps->h->data);
     974             :                 }
     975             : 
     976        1356 :                 switch (mvc_create_ukey(&k, sql, t, name, kt, check)) {
     977           0 :                         case -1:
     978           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     979           0 :                                 return SQL_ERR;
     980           0 :                         case -2:
     981             :                         case -3:
     982           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
     983           0 :                                 return SQL_ERR;
     984             :                         default:
     985        1356 :                                 break;
     986             :                 }
     987        1356 :                 node* n = NULL;
     988        1356 :                 if (check) {
     989          18 :                         sql_rel* btrel = check_rel->l;
     990          18 :                         n = btrel->exps->h;
     991             :                 }
     992        3310 :                 while (true) {
     993        3310 :                         const char *nm;
     994        3310 :                         if (check) {
     995          40 :                                 if (!n)
     996             :                                         break;
     997          22 :                                 sql_exp* e = n->data;
     998          22 :                                 nm = e->alias.name;
     999          22 :                                 n = n->next;
    1000             :                         } else {
    1001        3270 :                                 if (!nms)
    1002             :                                         break;
    1003        1938 :                                 nm = nms->data.sval;
    1004        1938 :                                 nms = nms->next;
    1005             :                         }
    1006        1960 :                         sql_column *c = mvc_bind_column(sql, t, nm);
    1007             : 
    1008        1960 :                         if (!c) {
    1009           6 :                                 (void) sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CONSTRAINT %s: no such column '%s' for table '%s'",
    1010             :                                                 kt == pkey ? "PRIMARY KEY" : "UNIQUE",
    1011             :                                                 nm, t->base.name);
    1012           6 :                                 return SQL_ERR;
    1013             :                         }
    1014        1954 :                         switch (mvc_create_kc(sql, k, c)) {
    1015           0 :                                 case -1:
    1016           0 :                                         (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1017           0 :                                         return SQL_ERR;
    1018           0 :                                 case -2:
    1019             :                                 case -3:
    1020           0 :                                         (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
    1021           0 :                                         return SQL_ERR;
    1022             :                                 default:
    1023             :                                         break;
    1024             :                         }
    1025             :                 }
    1026        1350 :                 switch (mvc_create_key_done(sql, k)) {
    1027           0 :                         case -1:
    1028           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1029           0 :                                 return SQL_ERR;
    1030           0 :                         case -2:
    1031             :                         case -3:
    1032           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
    1033           0 :                                 return SQL_ERR;
    1034             :                         default:
    1035        1350 :                                 break;
    1036             :                 }
    1037        1350 :         }       break;
    1038         897 :         case SQL_FOREIGN_KEY:
    1039         897 :                 res = table_foreign_key(sql, name, s, ss, t);
    1040         897 :                 break;
    1041             :         default:
    1042             :                 res = SQL_ERR;
    1043             :         }
    1044        2247 :         if (res != SQL_OK) {
    1045          26 :                 (void) sql_error(sql, 02, SQLSTATE(M0M03) "Table constraint type: wrong token (%p) = %s", s, token2string(s->token));
    1046          26 :                 return SQL_ERR;
    1047             :         }
    1048             :         return res;
    1049             : }
    1050             : 
    1051             : static int
    1052        2259 : table_constraint(sql_query *query, symbol *s, sql_schema *ss, sql_table *t)
    1053             : {
    1054        2259 :         mvc *sql = query->sql;
    1055        2259 :         int res = SQL_OK;
    1056             : 
    1057        2259 :         if (s->token == SQL_CONSTRAINT) {
    1058        2259 :                 dlist *l = s->data.lval;
    1059        2259 :                 char *opt_name = l->h->data.sval;
    1060        2259 :                 symbol *sym = l->h->next->data.sym;
    1061             : 
    1062        2259 :                 if (!opt_name)
    1063         894 :                         opt_name = table_constraint_name(sql, sym, ss, t);
    1064             :                 else if (s->token)
    1065             :                 if (opt_name == NULL)
    1066             :                         return SQL_ERR;
    1067        2259 :                 res = table_constraint_type(query, opt_name, sym, ss, t);
    1068             :         }
    1069             : 
    1070        2259 :         if (res != SQL_OK) {
    1071          38 :                 (void) sql_error(sql, 02, SQLSTATE(M0M03) "Table constraint: wrong token (%p) = %s", s, token2string(s->token));
    1072          38 :                 return SQL_ERR;
    1073             :         }
    1074             :         return res;
    1075             : }
    1076             : 
    1077             : static int
    1078       53105 : create_column(sql_query *query, symbol *s, sql_schema *ss, sql_table *t, int alter, bool isDeclared)
    1079             : {
    1080       53105 :         mvc *sql = query->sql;
    1081       53105 :         dlist *l = s->data.lval;
    1082       53105 :         char *cname = l->h->data.sval;
    1083       53105 :         sql_subtype *ctype = &l->h->next->data.typeval;
    1084       53105 :         dlist *opt_list = NULL;
    1085       53105 :         int res = SQL_OK;
    1086             : 
    1087       53105 :         (void) ss;
    1088       53105 :         if (alter && !(isTable(t) || ((isMergeTable(t) || isReplicaTable(t)) && list_length(t->members)==0))) {
    1089           0 :                 (void) sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot add column to %s '%s'%s",
    1090           0 :                                   TABLE_TYPE_DESCRIPTION(t->type, t->properties),
    1091           0 :                                   t->base.name, ((isMergeTable(t) || isReplicaTable(t)) && list_length(t->members)) ? " while it has partitions" : "");
    1092           0 :                 return SQL_ERR;
    1093             :         }
    1094       53105 :         if (l->h->next->next)
    1095       53105 :                 opt_list = l->h->next->next->data.lval;
    1096             : 
    1097       53105 :         if (cname && ctype) {
    1098       53105 :                 sql_column *cs = NULL;
    1099             : 
    1100       53105 :                 if (!isView(t) && cname && cname[0] == '%') {
    1101           0 :                         (void) sql_error(sql, 01, SQLSTATE(42000) "%s TABLE: generated labels not allowed in column names, use an alias instead", (alter)?"ALTER":"CREATE");
    1102           9 :                         return SQL_ERR;
    1103       53105 :                 } else if (ctype->type->eclass == EC_ANY) {
    1104           0 :                         (void) sql_error(sql, 01, SQLSTATE(42000) "%s TABLE: any type (plain null value) not allowed as a column type, use an explicit cast", (alter)?"ALTER":"CREATE");
    1105           0 :                         return SQL_ERR;
    1106       53105 :                 } else if ((cs = find_sql_column(t, cname))) {
    1107           2 :                         (void) sql_error(sql, 02, SQLSTATE(42S21) "%s TABLE: a column named '%s' already exists", (alter)?"ALTER":"CREATE", cname);
    1108           2 :                         return SQL_ERR;
    1109             :                 }
    1110       53103 :                 switch (mvc_create_column(&cs, sql, t, cname, ctype)) {
    1111           0 :                         case -1:
    1112           0 :                                 (void) sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1113           0 :                                 return SQL_ERR;
    1114           0 :                         case -2:
    1115             :                         case -3:
    1116           0 :                                 (void) sql_error(sql, 01, SQLSTATE(42000) "%s TABLE: transaction conflict detected", (alter)?"ALTER":"CREATE");
    1117           0 :                                 return SQL_ERR;
    1118             :                         default:
    1119       53103 :                                 break;
    1120             :                 }
    1121       53103 :                 if (column_options(query, opt_list, ss, t, cs, isDeclared) == SQL_ERR)
    1122             :                         return SQL_ERR;
    1123             :         }
    1124             :         return res;
    1125             : }
    1126             : 
    1127             : static int
    1128       55762 : table_element(sql_query *query, symbol *s, sql_schema *ss, sql_table *t, int alter, bool isDeclared, const char *action)
    1129             : {
    1130       55762 :         mvc *sql = query->sql;
    1131       55762 :         int res = SQL_OK;
    1132             : 
    1133       55762 :         if (alter &&
    1134        1260 :                 (isView(t) ||
    1135        1252 :                 ((isMergeTable(t) || isReplicaTable(t)) && (s->token != SQL_TABLE && s->token != SQL_DROP_TABLE && list_length(t->members))) ||
    1136        2494 :                 (isTable(t) && (s->token == SQL_TABLE || s->token == SQL_DROP_TABLE)) ||
    1137        1247 :                 (partition_find_part(sql->session->tr, t, NULL) &&
    1138           5 :                          (s->token == SQL_DROP_COLUMN || s->token == SQL_COLUMN || s->token == SQL_CONSTRAINT ||
    1139             :                           s->token == SQL_DEFAULT || s->token == SQL_DROP_DEFAULT || s->token == SQL_NOT_NULL || s->token == SQL_NULL || s->token == SQL_DROP_CONSTRAINT)))){
    1140          18 :                 const char *msg = "";
    1141             : 
    1142          18 :                 switch (s->token) {
    1143             :                 case SQL_TABLE:
    1144             :                         msg = "add table to";
    1145             :                         break;
    1146           3 :                 case SQL_COLUMN:
    1147           3 :                         msg = "add column to";
    1148           3 :                         break;
    1149           4 :                 case SQL_CONSTRAINT:
    1150           4 :                         msg = "add constraint to";
    1151           4 :                         break;
    1152           4 :                 case SQL_COLUMN_OPTIONS:
    1153             :                 case SQL_DEFAULT:
    1154             :                 case SQL_NOT_NULL:
    1155             :                 case SQL_NULL:
    1156           4 :                         msg = "set column options for";
    1157           4 :                         break;
    1158           0 :                 case SQL_STORAGE:
    1159           0 :                         msg = "set column storage for";
    1160           0 :                         break;
    1161           0 :                 case SQL_DROP_DEFAULT:
    1162           0 :                         msg = "drop default column option from";
    1163           0 :                         break;
    1164           0 :                 case SQL_DROP_TABLE:
    1165           0 :                         msg = "drop table from";
    1166           0 :                         break;
    1167           5 :                 case SQL_DROP_COLUMN:
    1168           5 :                         msg = "drop column from";
    1169           5 :                         break;
    1170           2 :                 case SQL_DROP_CONSTRAINT:
    1171           2 :                         msg = "drop constraint from";
    1172           2 :                         break;
    1173           0 :                 default:
    1174           0 :                         sql_error(sql, 02, SQLSTATE(M0M03) "%s: Unknown table element (%p)->token = %s\n", action, s, token2string(s->token));
    1175           0 :                         return SQL_ERR;
    1176             :                 }
    1177          18 :                 sql_error(sql, 02, SQLSTATE(42000) "%s: cannot %s %s '%s'%s\n",
    1178             :                                 action,
    1179             :                                 msg,
    1180          18 :                                 partition_find_part(sql->session->tr, t, NULL)?"a PARTITION of a MERGE or REPLICA TABLE":
    1181          13 :                                 TABLE_TYPE_DESCRIPTION(t->type, t->properties),
    1182          18 :                                 t->base.name, ((isMergeTable(t) || isReplicaTable(t)) && list_length(t->members)) ? " while it has partitions" : "");
    1183          18 :                 return SQL_ERR;
    1184             :         }
    1185             : 
    1186       55744 :         switch (s->token) {
    1187       53105 :         case SQL_COLUMN:
    1188       53105 :                 res = create_column(query, s, ss, t, alter, isDeclared);
    1189       53105 :                 break;
    1190        2259 :         case SQL_CONSTRAINT:
    1191        2259 :                 res = table_constraint(query, s, ss, t);
    1192        2259 :                 break;
    1193           0 :         case SQL_COLUMN_OPTIONS:
    1194             :         {
    1195           0 :                 dnode *n = s->data.lval->h;
    1196           0 :                 char *cname = n->data.sval;
    1197           0 :                 sql_column *c = mvc_bind_column(sql, t, cname);
    1198           0 :                 dlist *olist = n->next->data.lval;
    1199             : 
    1200           0 :                 if (!c) {
    1201           0 :                         sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
    1202           0 :                         return SQL_ERR;
    1203             :                 } else {
    1204           0 :                         return column_options(query, olist, ss, t, c, isDeclared);
    1205             :                 }
    1206          22 :         }       break;
    1207          22 :         case SQL_DEFAULT:
    1208             :         {
    1209          22 :                 char *r, *err = NULL;
    1210          22 :                 dlist *l = s->data.lval;
    1211          22 :                 char *cname = l->h->data.sval;
    1212          22 :                 symbol *sym = l->h->next->data.sym;
    1213          22 :                 sql_column *c = mvc_bind_column(sql, t, cname);
    1214             : 
    1215          22 :                 if (!c) {
    1216           3 :                         sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
    1217           5 :                         return SQL_ERR;
    1218             :                 }
    1219          19 :                 if (sym->token == SQL_COLUMN || sym->token == SQL_IDENT || sym->token == SQL_NEXT) {
    1220           7 :                                 exp_kind ek = {type_value, card_value, FALSE};
    1221           7 :                                 sql_exp *e = rel_logical_value_exp(query, NULL, sym, sql_sel, ek);
    1222             : 
    1223           7 :                                 if (!e)
    1224           1 :                                         return SQL_ERR;
    1225           6 :                                 if (e && is_atom(e->type)) {
    1226           0 :                                         atom *a = exp_value(sql, e);
    1227             : 
    1228           0 :                                         if (a && atom_null(a)) {
    1229           0 :                                                 switch (mvc_default(sql, c, NULL)) {
    1230           0 :                                                 case -1:
    1231           0 :                                                         (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1232           0 :                                                         return SQL_ERR;
    1233           0 :                                                 case -2:
    1234             :                                                 case -3:
    1235           0 :                                                         (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
    1236           0 :                                                         return SQL_ERR;
    1237             :                                                 default:
    1238           0 :                                                         break;
    1239             :                                         }
    1240           0 :                                         break;
    1241             :                                 }
    1242             :                         }
    1243             :                         /* reset error */
    1244           6 :                         sql->session->status = 0;
    1245           6 :                         sql->errstr[0] = '\0';
    1246             :                 }
    1247          18 :                 r = symbol2string(sql, sym, 0, &err);
    1248          18 :                 if (!r) {
    1249           1 :                         (void) sql_error(sql, 02, SQLSTATE(42000) "%s: incorrect default value '%s'\n", action, err?err:"");
    1250           1 :                         return SQL_ERR;
    1251             :                 }
    1252          17 :                 switch (mvc_default(sql, c, r)) {
    1253           0 :                         case -1:
    1254           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1255           0 :                                 return SQL_ERR;
    1256           0 :                         case -2:
    1257             :                         case -3:
    1258           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
    1259           0 :                                 return SQL_ERR;
    1260             :                         default:
    1261          17 :                                 break;
    1262             :                 }
    1263             :         }
    1264          17 :         break;
    1265           0 :         case SQL_STORAGE:
    1266             :         {
    1267           0 :                 dlist *l = s->data.lval;
    1268           0 :                 char *cname = l->h->data.sval;
    1269           0 :                 char *storage_type = l->h->next->data.sval;
    1270           0 :                 sql_column *c = mvc_bind_column(sql, t, cname);
    1271             : 
    1272           0 :                 if (!c) {
    1273           0 :                         sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
    1274           0 :                         return SQL_ERR;
    1275             :                 }
    1276           0 :                 switch (mvc_storage(sql, c, storage_type)) {
    1277           0 :                         case -1:
    1278           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1279           0 :                                 return SQL_ERR;
    1280           0 :                         case -2:
    1281             :                         case -3:
    1282           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "STORAGE: transaction conflict detected");
    1283           0 :                                 return SQL_ERR;
    1284             :                         default:
    1285             :                                 break;
    1286             :                 }
    1287             :         }
    1288             :         break;
    1289          45 :         case SQL_NOT_NULL:
    1290             :         case SQL_NULL:
    1291             :         {
    1292          45 :                 dnode *n = s->data.lval->h;
    1293          45 :                 char *cname = n->data.sval;
    1294          45 :                 sql_column *c = mvc_bind_column(sql, t, cname);
    1295          45 :                 int null = (s->token != SQL_NOT_NULL);
    1296             : 
    1297          45 :                 if (!c) {
    1298           4 :                         sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
    1299           4 :                         return SQL_ERR;
    1300             :                 }
    1301          41 :                 switch (mvc_null(sql, c, null)) {
    1302           0 :                         case -1:
    1303           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1304           0 :                                 return SQL_ERR;
    1305           0 :                         case -2:
    1306             :                         case -3:
    1307           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "NULL CONSTRAINT: transaction conflict detected");
    1308           0 :                                 return SQL_ERR;
    1309             :                         default:
    1310             :                                 break;
    1311             :                 }
    1312             :         }       break;
    1313           8 :         case SQL_DROP_DEFAULT:
    1314             :         {
    1315           8 :                 char *cname = s->data.sval;
    1316           8 :                 sql_column *c = mvc_bind_column(sql, t, cname);
    1317           8 :                 if (!c) {
    1318           2 :                         sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
    1319           2 :                         return SQL_ERR;
    1320             :                 }
    1321           6 :                 switch (mvc_drop_default(sql, c)) {
    1322           0 :                         case -1:
    1323           0 :                                 (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1324           0 :                                 return SQL_ERR;
    1325           0 :                         case -2:
    1326             :                         case -3:
    1327           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
    1328           0 :                                 return SQL_ERR;
    1329             :                         default:
    1330             :                                 break;
    1331             :                 }
    1332             :         }       break;
    1333          69 :         case SQL_LIKE:
    1334             :         {
    1335          69 :                 char *sname = qname_schema(s->data.lval);
    1336          69 :                 char *name = qname_schema_object(s->data.lval);
    1337          69 :                 sql_table *ot = NULL;
    1338             : 
    1339          69 :                 if (!(ot = find_table_or_view_on_scope(sql, ss, sname, name, action, false)))
    1340             :                         return SQL_ERR;
    1341         143 :                 for (node *n = ol_first_node(ot->columns); n; n = n->next) {
    1342          76 :                         sql_column *oc = n->data, *nc = NULL;
    1343             : 
    1344          76 :                         if (!isView(t) && oc->base.name && oc->base.name[0] == '%') {
    1345           0 :                                 sql_error(sql, 02, SQLSTATE(42000) "%s: generated labels not allowed in column names, use an alias instead", action);
    1346           2 :                                 return SQL_ERR;
    1347          76 :                         } else if (mvc_bind_column(sql, t, oc->base.name)) {
    1348           2 :                                 sql_error(sql, 02, SQLSTATE(42S21) "%s: a column named '%s' already exists\n", action, oc->base.name);
    1349           2 :                                 return SQL_ERR;
    1350             :                         }
    1351          74 :                         assert(oc->type.type->eclass != EC_ANY);
    1352          74 :                         switch (mvc_create_column(&nc, sql, t, oc->base.name, &oc->type)) {
    1353           0 :                                 case -1:
    1354           0 :                                         sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1355           0 :                                         return SQL_ERR;
    1356           0 :                                 case -2:
    1357             :                                 case -3:
    1358           0 :                                         sql_error(sql, 01, SQLSTATE(42000) "%s: transaction conflict detected", action);
    1359           0 :                                         return SQL_ERR;
    1360             :                                 default:
    1361          74 :                                         break;
    1362             :                         }
    1363             :                 }
    1364             :         }       break;
    1365          86 :         case SQL_DROP_COLUMN:
    1366             :         {
    1367          86 :                 dlist *l = s->data.lval;
    1368          86 :                 char *cname = l->h->data.sval;
    1369          86 :                 int drop_action = l->h->next->data.i_val;
    1370          86 :                 sql_column *col = mvc_bind_column(sql, t, cname);
    1371             : 
    1372          86 :                 assert(l->h->next->type == type_int);
    1373          86 :                 if (col == NULL) {
    1374           9 :                         sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
    1375           9 :                         return SQL_ERR;
    1376             :                 }
    1377          77 :                 if (ol_length(t->columns) <= 1) {
    1378           2 :                         sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': table needs at least one column\n", action, cname);
    1379           2 :                         return SQL_ERR;
    1380             :                 }
    1381          75 :                 if (t->system) {
    1382           0 :                         sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': table is a system table\n", action, cname);
    1383           0 :                         return SQL_ERR;
    1384             :                 }
    1385          75 :                 if (isView(t)) {
    1386           0 :                         sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': '%s' is a view\n", action, cname, t->base.name);
    1387           0 :                         return SQL_ERR;
    1388             :                 }
    1389          75 :                 if (!drop_action && mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL)) {
    1390           3 :                         sql_error(sql, 02, SQLSTATE(2BM37) "%s: cannot drop column '%s': there are database objects which depend on it\n", action, cname);
    1391           3 :                         return SQL_ERR;
    1392             :                 }
    1393          64 :                 if (!drop_action  && t->keys) {
    1394          64 :                         node *n, *m;
    1395             : 
    1396          64 :                         for (n = ol_first_node(t->keys); n; n = n->next) {
    1397           0 :                                 sql_key *k = n->data;
    1398           0 :                                 for (m = k->columns->h; m; m = m->next) {
    1399           0 :                                         sql_kc *kc = m->data;
    1400           0 :                                         if (strcmp(kc->c->base.name, cname) == 0) {
    1401           0 :                                                 sql_error(sql, 02, SQLSTATE(2BM37) "%s: cannot drop column '%s': there are constraints which depend on it\n", action, cname);
    1402           0 :                                                 return SQL_ERR;
    1403             :                                         }
    1404             :                                 }
    1405             :                         }
    1406             :                 }
    1407          72 :                 if (isPartitionedByColumnTable(t) && t->part.pcol->base.id == col->base.id) {
    1408           2 :                         sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': is the partitioned column on the table '%s'\n", action, cname, t->base.name);
    1409           2 :                         return SQL_ERR;
    1410             :                 }
    1411          70 :                 if (isPartitionedByExpressionTable(t)) {
    1412           8 :                         for (node *n = t->part.pexp->cols->h; n; n = n->next) {
    1413           6 :                                 int next = *(int*) n->data;
    1414           6 :                                 if (next == col->colnr) {
    1415           2 :                                         sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': the expression used in '%s' depends on it\n", action, cname, t->base.name);
    1416           2 :                                         return SQL_ERR;
    1417             :                                 }
    1418             :                         }
    1419             :                 }
    1420          68 :                 switch (mvc_drop_column(sql, t, col, drop_action)) {
    1421           0 :                         case -1:
    1422           0 :                                 sql_error(sql, 02, SQLSTATE(42000) "%s: %s\n", action, MAL_MALLOC_FAIL);
    1423           0 :                                 return SQL_ERR;
    1424           0 :                         case -2:
    1425             :                         case -3:
    1426           0 :                                 sql_error(sql, 02, SQLSTATE(42000) "%s: transaction conflict detected\n", action);
    1427           0 :                                 return SQL_ERR;
    1428             :                         default:
    1429             :                                 break;
    1430             :                 }
    1431             :         }       break;
    1432             :         case SQL_DROP_CONSTRAINT:
    1433             :                 res = SQL_OK;
    1434             :                 break;
    1435             :         default:
    1436             :                 res = SQL_ERR;
    1437             :         }
    1438       55448 :         if (res == SQL_ERR) {
    1439          47 :                 sql_error(sql, 02, SQLSTATE(M0M03) "%s: Unknown table element (%p)->token = %s\n", action, s, token2string(s->token));
    1440          47 :                 return SQL_ERR;
    1441             :         }
    1442             :         return res;
    1443             : }
    1444             : 
    1445             : static int
    1446       10112 : create_partition_definition(mvc *sql, sql_table *t, symbol *partition_def)
    1447             : {
    1448       10112 :         char *err = NULL;
    1449             : 
    1450       10112 :         if (partition_def) {
    1451         107 :                 dlist *list = partition_def->data.lval;
    1452         107 :                 symbol *type = list->h->next->data.sym;
    1453         107 :                 dlist *list2 = type->data.lval;
    1454         107 :                 if (isPartitionedByColumnTable(t)) {
    1455          84 :                         str colname = list2->h->data.sval;
    1456          84 :                         node *n;
    1457          84 :                         sql_class sql_ec;
    1458         100 :                         for (n = ol_first_node(t->columns); n ; n = n->next) {
    1459          98 :                                 sql_column *col = n->data;
    1460          98 :                                 if (!strcmp(col->base.name, colname)) {
    1461          82 :                                         t->part.pcol = col;
    1462          82 :                                         break;
    1463             :                                 }
    1464             :                         }
    1465          84 :                         if (!t->part.pcol) {
    1466           2 :                                 sql_error(sql, 02, SQLSTATE(42000) "CREATE MERGE TABLE: the partition column '%s' is not part of the table", colname);
    1467           2 :                                 return SQL_ERR;
    1468             :                         }
    1469          82 :                         sql_ec = t->part.pcol->type.type->eclass;
    1470          82 :                         if (!(sql_ec == EC_BIT || EC_VARCHAR(sql_ec) || EC_TEMP(sql_ec) || sql_ec == EC_POS || sql_ec == EC_NUM ||
    1471           2 :                                  EC_INTERVAL(sql_ec)|| sql_ec == EC_DEC || sql_ec == EC_BLOB)) {
    1472           2 :                                 err = sql_subtype_string(sql->ta, &(t->part.pcol->type));
    1473           2 :                                 sql_error(sql, 02, SQLSTATE(42000) "CREATE MERGE TABLE: column type %s not yet supported for the partition column", err);
    1474           2 :                                 return SQL_ERR;
    1475             :                         }
    1476          23 :                 } else if (isPartitionedByExpressionTable(t)) {
    1477          23 :                         char *query = symbol2string(sql, list2->h->data.sym, 1, &err);
    1478          23 :                         if (!query) {
    1479           1 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "CREATE MERGE TABLE: error compiling expression '%s'", err?err:"");
    1480           1 :                                 return SQL_ERR;
    1481             :                         }
    1482          22 :                         t->part.pexp = SA_ZNEW(sql->sa, sql_expression);
    1483          22 :                         t->part.pexp->exp = query;
    1484          22 :                         t->part.pexp->type = *sql_bind_localtype("void");
    1485             :                 }
    1486             :         }
    1487             :         return SQL_OK;
    1488             : }
    1489             : 
    1490             : sql_rel *
    1491       10272 : rel_create_table(sql_query *query, int temp, const char *sname, const char *name, bool global, symbol *table_elements_or_subquery,
    1492             :                                  int commit_action, const char *loc, const char *username, const char *password, bool pw_encrypted,
    1493             :                                  symbol* partition_def, int if_not_exists)
    1494             : {
    1495       10272 :         mvc *sql = query->sql;
    1496       10272 :         int tt = (temp == SQL_REMOTE)?tt_remote:
    1497             :                  (temp == SQL_MERGE_TABLE)?tt_merge_table:
    1498             :                  (temp == SQL_REPLICA_TABLE)?tt_replica_table:
    1499             :                  (temp == SQL_UNLOGGED_TABLE)?tt_unlogged_table:tt_table;
    1500       10272 :         bit properties = partition_def ? (bit) partition_def->data.lval->h->next->next->data.i_val : 0;
    1501       10272 :         sql_table *t = NULL;
    1502       10272 :         const char *action = (temp == SQL_DECLARED_TABLE)?"DECLARE":"CREATE";
    1503       10272 :         sql_schema *s = cur_schema(sql);
    1504             : 
    1505       10272 :         if (sname && !(s = mvc_bind_schema(sql, sname)))
    1506           1 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "%s TABLE: no such schema '%s'", action, sname);
    1507             : 
    1508       10271 :         if ((temp != SQL_PERSIST && tt == tt_table && commit_action == CA_COMMIT) || temp == SQL_DECLARE)
    1509         195 :                 commit_action = CA_DELETE;
    1510             : 
    1511       10271 :         if (temp == SQL_LOCAL_TEMP || temp == SQL_GLOBAL_TEMP) {
    1512         112 :                 if (sname && strcmp(sname, "tmp") != 0)
    1513           7 :                         return sql_error(sql, 02, SQLSTATE(3F000) "%s TABLE: %s temporary tables should be stored in the 'tmp' schema",
    1514             :                                                          action, (temp == SQL_LOCAL_TEMP) ? "local" : "global");
    1515         107 :                 s = tmp_schema(sql);
    1516             :         }
    1517             : 
    1518       10266 :         if (global && mvc_bind_table(sql, s, name)) {
    1519           8 :                 if (if_not_exists)
    1520           2 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    1521           6 :                 return sql_error(sql, 02, SQLSTATE(42S01) "%s TABLE: name '%s' already in use", action, name);
    1522         121 :         } else if (!global && frame_find_table(sql, name)) {
    1523           1 :                 assert(temp == SQL_DECLARED_TABLE);
    1524           1 :                 if (if_not_exists)
    1525           0 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    1526           1 :                 return sql_error(sql, 02, SQLSTATE(42S01) "%s TABLE: name '%s' already declared", action, name);
    1527       10257 :         } else if (temp != SQL_DECLARED_TABLE && (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && temp == SQL_LOCAL_TEMP))){
    1528           4 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE TABLE: insufficient privileges for user '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
    1529       10253 :         } else if ((temp == SQL_PERSIST || temp == SQL_MERGE_TABLE || temp == SQL_REMOTE || temp == SQL_REPLICA_TABLE || temp == SQL_UNLOGGED_TABLE) && isTempSchema(s)) {
    1530           7 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: cannot create persistent table '%s' in the schema '%s'", TABLE_TYPE_DESCRIPTION(tt, properties), name, s->base.name);
    1531       10247 :         } else if (table_elements_or_subquery->token == SQL_CREATE_TABLE) {
    1532             :                 /* table element list */
    1533       10132 :                 dnode *n;
    1534       10132 :                 dlist *columns = table_elements_or_subquery->data.lval;
    1535       10132 :                 int res = LOG_OK;
    1536             : 
    1537       10132 :                 if (tt == tt_remote) {
    1538          97 :                         if (!mapiuri_valid(loc))
    1539           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: incorrect uri '%s' for remote table '%s'", action, loc, name);
    1540          97 :                         res = mvc_create_remote(&t, sql, s, name, SQL_DECLARED_TABLE, loc);
    1541             :                 } else {
    1542       10035 :                         res = mvc_create_table(&t, sql, s, name, tt, 0, SQL_DECLARED_TABLE, commit_action, -1, properties);
    1543             :                 }
    1544       10132 :                 switch (res) {
    1545           0 :                         case -1:
    1546           0 :                                 return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1547           0 :                         case -2:
    1548             :                         case -3:
    1549           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: transaction conflict detected", action);
    1550           0 :                         case -4:
    1551           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: the partition's expression is too long", action);
    1552             :                         case -5:
    1553             :                                 return NULL;
    1554             :                         default:
    1555       10132 :                                 break;
    1556             :                 }
    1557             : 
    1558       64614 :                 for (n = columns->h; n; n = n->next) {
    1559       54502 :                         symbol *sym = n->data.sym;
    1560      108781 :                         int res = table_element(query, sym, s, t, 0, (temp == SQL_DECLARED_TABLE), (temp == SQL_DECLARED_TABLE)?"DECLARE TABLE":"CREATE TABLE");
    1561             : 
    1562       54502 :                         if (res == SQL_ERR)
    1563             :                                 return NULL;
    1564             :                 }
    1565             : 
    1566       10112 :                 if (create_partition_definition(sql, t, partition_def) != SQL_OK)
    1567             :                         return NULL;
    1568             : 
    1569       10107 :                 temp = (tt == tt_table)?temp:SQL_PERSIST;
    1570             : 
    1571         395 :                 if (tt == tt_remote)
    1572          97 :                         return rel_create_remote(sql, ddl_create_table, s->base.name, t, pw_encrypted, username, password);
    1573       10010 :                 return rel_table(sql, ddl_create_table, s->base.name, t, temp);
    1574             :         } else { /* [col name list] as subquery with or without data */
    1575         115 :                 sql_rel *sq = NULL, *res = NULL;
    1576         115 :                 dlist *as_sq = table_elements_or_subquery->data.lval;
    1577         115 :                 dlist *column_spec = as_sq->h->data.lval;
    1578         115 :                 symbol *subquery = as_sq->h->next->data.sym;
    1579         115 :                 int with_data = as_sq->h->next->next->data.i_val;
    1580             : 
    1581         115 :                 assert(as_sq->h->next->next->type == type_int);
    1582         115 :                 sq = rel_selects(query, subquery);
    1583         115 :                 if (!sq)
    1584             :                         return NULL;
    1585         112 :                 if (!is_project(sq->op)) /* make sure sq is a projection */
    1586           6 :                         sq = rel_project(sql->sa, sq, rel_projections(sql, sq, NULL, 1, 1));
    1587             : 
    1588         112 :                 if ((tt != tt_table && tt != tt_unlogged_table) && with_data)
    1589           3 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: cannot create %s 'with data'", action,
    1590           3 :                                                          TABLE_TYPE_DESCRIPTION(tt, properties));
    1591             : 
    1592             :                 /* create table */
    1593           6 :                 if (tt == tt_remote) {
    1594           4 :                         if (!mapiuri_valid(loc))
    1595           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: incorrect uri '%s' for remote table '%s'", action, loc, name);
    1596           8 :                         if ((t = mvc_create_remote_as_subquery(sql, sq, s, name, column_spec, loc, (temp == SQL_DECLARED_TABLE)?"DECLARE TABLE":"CREATE TABLE")) == NULL) {
    1597           0 :                                 rel_destroy(sq);
    1598           0 :                                 return NULL;
    1599             :                         }
    1600             :                 } else {
    1601         207 :                         if ((t = mvc_create_table_as_subquery(sql, sq, s, name, column_spec, temp, commit_action, (temp == SQL_DECLARED_TABLE)?"DECLARE TABLE":"CREATE TABLE")) == NULL) {
    1602           3 :                                 rel_destroy(sq);
    1603           3 :                                 return NULL;
    1604             :                         }
    1605             :                 }
    1606             : 
    1607             :                 /* insert query result into this table */
    1608         106 :                 if (tt == tt_remote) {
    1609           4 :                         res = rel_create_remote(sql, ddl_create_table, s->base.name, t, pw_encrypted, username, password);
    1610             :                         /* we cannot insert in remote so just remove the subquery */
    1611           4 :                         rel_destroy(sq);
    1612             :                 } else {
    1613         105 :                         res = rel_table(sql, ddl_create_table, s->base.name, t, (tt == tt_table)?temp:SQL_PERSIST);
    1614         102 :                         if (with_data) {
    1615          95 :                                 res = rel_insert(query->sql, res, sq);
    1616             :                         } else {
    1617           7 :                                 rel_destroy(sq);
    1618             :                         }
    1619             :                 }
    1620         106 :                 return res;
    1621             :         }
    1622             :         /*return NULL;*/ /* never reached as all branches of the above if () end with return ... */
    1623             : }
    1624             : 
    1625             : static sql_rel *
    1626       79024 : rel_create_view(sql_query *query, int temp, dlist *qname, dlist *column_spec, symbol *ast, int check, int persistent, int replace)
    1627             : {
    1628       79024 :         mvc *sql = query->sql;
    1629       79024 :         const char *name = qname_schema_object(qname);
    1630       79024 :         const char *sname = qname_schema(qname);
    1631       79024 :         sql_schema *s = cur_schema(sql);
    1632       79024 :         sql_table *t = NULL;
    1633       79024 :         int instantiate = (sql->emode == m_instantiate || !persistent);
    1634       79024 :         int deps = (sql->emode == m_deps);
    1635       79024 :         int create = (!instantiate && !deps);
    1636       79024 :         sqlid pfoundid = 0, foundid = 0;
    1637       79024 :         const char *base = replace ? "CREATE OR REPLACE VIEW" : "CREATE VIEW";
    1638       79024 :         const char *action = (temp == SQL_DECLARED_TABLE)?"DECLARE":"CREATE";
    1639             : 
    1640       79024 :         (void) check;           /* Stefan: unused!? */
    1641             : 
    1642       79024 :         if (temp == SQL_GLOBAL_TEMP)
    1643           0 :                 temp = SQL_PERSIST; /* just normal view */
    1644             : 
    1645       79024 :         if (temp == SQL_LOCAL_TEMP || temp == SQL_GLOBAL_TEMP) {
    1646           0 :                 if (sname && strcmp(sname, "tmp") != 0)
    1647           0 :                         return sql_error(sql, 02, SQLSTATE(3F000) "%s VIEW: %s temporary views should be stored in the 'tmp' schema",
    1648             :                                                          action, (temp == SQL_LOCAL_TEMP) ? "local" : "global");
    1649           0 :                 s = tmp_schema(sql);
    1650       79024 :         } else if (sname && !(s = mvc_bind_schema(sql, sname)))
    1651           0 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "%s: no such schema '%s'", base, sname);
    1652       79024 :         if (create && (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && persistent == SQL_LOCAL_TEMP)))
    1653           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "%s: access denied for %s to schema '%s'", base, get_string_global_var(sql, "current_user"), s->base.name);
    1654       22373 :         if (create && (t = mvc_bind_table(sql, s, name))) {
    1655          21 :                 if (!replace)
    1656           1 :                         return sql_error(sql, 02, SQLSTATE(42S01) "%s: name '%s' already in use", base, name);
    1657          20 :                 if (!isView(t))
    1658           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: '%s' is not a view", base, name);
    1659          20 :                 if (t->system)
    1660           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: cannot replace system view '%s'", base, name);
    1661          20 :                 foundid = t->base.id; /* when recreating a view, the view itself can't be found */
    1662             :         }
    1663             : 
    1664       79023 :         if (ast) {
    1665       79023 :                 sql_rel *sq = NULL;
    1666       79023 :                 char *q = QUERY(sql->scanner);
    1667       79023 :                 symbol *sym = ast;
    1668             : 
    1669       79023 :                 if (sym->token == SQL_WITH)
    1670        5663 :                         sym = sym->data.lval->h->next->data.sym;
    1671       79023 :                 if (sym->token == SQL_SELECT) {
    1672       76158 :                         SelectNode *sn = (SelectNode *) sym;
    1673             : 
    1674       76158 :                         if (sn->limit || sn->sample)
    1675           4 :                                 return sql_error(sql, 01, SQLSTATE(42000) "%s: %s not supported", base, sn->limit ? "LIMIT" : "SAMPLE");
    1676             :                 }
    1677             : 
    1678       79020 :                 pfoundid = sql->objid;
    1679       79020 :                 sql->objid = foundid; /* when recreating a view, the view itself can't be found */
    1680       79020 :                 sq = schema_selects(query, s, ast);
    1681       79020 :                 sql->objid = pfoundid;
    1682       79020 :                 if (!sq)
    1683             :                         return NULL;
    1684       79005 :                 if (!is_project(sq->op)) /* make sure sq is a projection */
    1685           0 :                         sq = rel_project(sql->sa, sq, rel_projections(sql, sq, NULL, 1, 1));
    1686             : 
    1687       79005 :                 if (!create) {
    1688       56646 :                         if (column_spec) {
    1689       13819 :                                 dnode *n = column_spec->h;
    1690       13819 :                                 node *m = sq->exps->h;
    1691             : 
    1692       63706 :                                 for (; n && m; n = n->next, m = m->next)
    1693             :                                         ;
    1694       13819 :                                 if (n || m) {
    1695           2 :                                         sql_error(sql, 01, SQLSTATE(21S02) "WITH CLAUSE: number of columns does not match");
    1696           2 :                                         rel_destroy(sq);
    1697           2 :                                         return NULL;
    1698             :                                 }
    1699             :                         }
    1700             :                 }
    1701             : 
    1702       56644 :                 if (create) {
    1703       22359 :                         q = query_cleaned(sql->ta, q);
    1704       22359 :                         switch (mvc_create_view(&t, sql, s, name, SQL_DECLARED_TABLE, q, 0)) {
    1705           0 :                                 case -1:
    1706           0 :                                         return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1707           0 :                                 case -2:
    1708             :                                 case -3:
    1709           0 :                                         return sql_error(sql, 02, SQLSTATE(42000) "%s: transaction conflict detected", base);
    1710             :                                 default:
    1711       22359 :                                         break;
    1712             :                         }
    1713       22359 :                         if (as_subquery(sql, t, tt_view, sq, column_spec, base) != 0) {
    1714           1 :                                 rel_destroy(sq);
    1715           1 :                                 return NULL;
    1716             :                         }
    1717       22358 :                         return rel_create_view_ddl(sql, ddl_create_view, s->base.name, t, temp, replace);
    1718             :                 }
    1719       56644 :                 if (!persistent && column_spec)
    1720       12155 :                         sq = view_rename_columns(sql, name, sq, column_spec);
    1721       56644 :                 if (sq && is_simple_project(sq->op) && sq->l && sq->exps && sq->card == CARD_AGGR) {
    1722        4043 :                         exps_setcard(sq->exps, CARD_MULTI);
    1723        4043 :                         sq->card = CARD_MULTI;
    1724             :                 }
    1725       56644 :                 return sq;
    1726             :         }
    1727             :         return NULL;
    1728             : }
    1729             : 
    1730             : static sql_rel *
    1731         321 : rel_schema2(allocator *sa, int cat_type, char *sname, char *auth, int nr)
    1732             : {
    1733         321 :         sql_rel *rel = rel_create(sa);
    1734         321 :         list *exps = new_exp_list(sa);
    1735         321 :         if (!rel || !exps)
    1736             :                 return NULL;
    1737             : 
    1738         321 :         append(exps, exp_atom_clob(sa, sname));
    1739         321 :         append(exps, exp_atom_clob(sa, auth));
    1740         321 :         append(exps, exp_atom_int(sa, nr));
    1741         321 :         rel->l = NULL;
    1742         321 :         rel->r = NULL;
    1743         321 :         rel->op = op_ddl;
    1744         321 :         rel->flag = cat_type;
    1745         321 :         rel->exps = exps;
    1746         321 :         rel->card = 0;
    1747         321 :         rel->nrcols = 0;
    1748         321 :         return rel;
    1749             : }
    1750             : 
    1751             : static sql_rel *
    1752         906 : rel_schema3(allocator *sa, int cat_type, char *sname, char *tname, char *name)
    1753             : {
    1754         906 :         sql_rel *rel = rel_create(sa);
    1755         906 :         list *exps = new_exp_list(sa);
    1756         906 :         if (!rel || !exps)
    1757             :                 return NULL;
    1758             : 
    1759         906 :         append(exps, exp_atom_clob(sa, sname));
    1760         906 :         append(exps, exp_atom_clob(sa, tname));
    1761         906 :         append(exps, exp_atom_clob(sa, name));
    1762         906 :         rel->l = NULL;
    1763         906 :         rel->r = NULL;
    1764         906 :         rel->op = op_ddl;
    1765         906 :         rel->flag = cat_type;
    1766         906 :         rel->exps = exps;
    1767         906 :         rel->card = 0;
    1768         906 :         rel->nrcols = 0;
    1769         906 :         return rel;
    1770             : }
    1771             : 
    1772             : static sql_rel *
    1773           5 : rel_drop_type(mvc *sql, dlist *qname, int drop_action)
    1774             : {
    1775           5 :         char *name = qname_schema_object(qname);
    1776           5 :         char *sname = qname_schema(qname);
    1777           5 :         sql_type *t = NULL;
    1778             : 
    1779           5 :         if (!(t = find_type_on_scope(sql, sname, name, "DROP TYPE")))
    1780             :                 return NULL;
    1781           4 :         if (!mvc_schema_privs(sql, t->s))
    1782           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "DROP TYPE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
    1783           4 :         return rel_schema2(sql->sa, ddl_drop_type, t->s->base.name, name, drop_action);
    1784             : }
    1785             : 
    1786             : static sql_rel *
    1787         906 : rel_create_type(mvc *sql, dlist *qname, char *impl)
    1788             : {
    1789         906 :         char *name = qname_schema_object(qname);
    1790         906 :         char *sname = qname_schema(qname);
    1791         906 :         sql_schema *s = cur_schema(sql);
    1792             : 
    1793         906 :         if (sname && !(s = mvc_bind_schema(sql, sname)))
    1794           0 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "CREATE TYPE: no such schema '%s'", sname);
    1795         906 :         if (schema_bind_type(sql, s, name) != NULL)
    1796           0 :                 return sql_error(sql, 02, SQLSTATE(42S01) "CREATE TYPE: name '%s' already in use", name);
    1797         906 :         if (!mvc_schema_privs(sql, s))
    1798           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE TYPE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
    1799         906 :         return rel_schema3(sql->sa, ddl_create_type, s->base.name, name, impl);
    1800             : }
    1801             : 
    1802             : static char *
    1803        1276 : dlist_get_schema_name(dlist *name_auth)
    1804             : {
    1805        1276 :         assert(name_auth && name_auth->h);
    1806        1276 :         return name_auth->h->data.sval;
    1807             : }
    1808             : 
    1809             : static char *
    1810        1085 : schema_auth(dlist *name_auth)
    1811             : {
    1812        1085 :         assert(name_auth && name_auth->h && dlist_length(name_auth) == 2);
    1813        1085 :         return name_auth->h->next->data.sval;
    1814             : }
    1815             : 
    1816             : static sql_rel *
    1817        4336 : rel_drop(allocator *sa, int cat_type, char *sname, char *first_val, char *second_val, int nr, int exists_check)
    1818             : {
    1819        4336 :         sql_rel *rel = rel_create(sa);
    1820        4336 :         list *exps = new_exp_list(sa);
    1821             : 
    1822        4336 :         append(exps, exp_atom_int(sa, nr));
    1823        4336 :         append(exps, exp_atom_clob(sa, sname));
    1824        4336 :         if (first_val)
    1825        4145 :                 append(exps, exp_atom_clob(sa, first_val));
    1826        4336 :         if (second_val)
    1827         150 :                 append(exps, exp_atom_clob(sa, second_val));
    1828        4336 :         append(exps, exp_atom_int(sa, exists_check));
    1829        4336 :         rel->l = NULL;
    1830        4336 :         rel->r = NULL;
    1831        4336 :         rel->op = op_ddl;
    1832        4336 :         rel->flag = cat_type;
    1833        4336 :         rel->exps = exps;
    1834        4336 :         rel->card = 0;
    1835        4336 :         rel->nrcols = 0;
    1836        4336 :         return rel;
    1837             : }
    1838             : 
    1839             : static sql_rel *
    1840        1082 : rel_create_schema_dll(allocator *sa, char *sname, char *auth, int nr)
    1841             : {
    1842        1082 :         sql_rel *rel = rel_create(sa);
    1843        1082 :         list *exps = new_exp_list(sa);
    1844        1082 :         if (!rel || !exps)
    1845             :                 return NULL;
    1846             : 
    1847        1082 :         append(exps, exp_atom_int(sa, nr));
    1848        1082 :         append(exps, exp_atom_clob(sa, sname));
    1849        1082 :         if (auth)
    1850          38 :                 append(exps, exp_atom_clob(sa, auth));
    1851        1082 :         rel->l = NULL;
    1852        1082 :         rel->r = NULL;
    1853        1082 :         rel->op = op_ddl;
    1854        1082 :         rel->flag = ddl_create_schema;
    1855        1082 :         rel->exps = exps;
    1856        1082 :         rel->card = 0;
    1857        1082 :         rel->nrcols = 0;
    1858        1082 :         return rel;
    1859             : }
    1860             : 
    1861             : static sql_rel *
    1862        1085 : rel_create_schema(sql_query *query, dlist *auth_name, dlist *schema_elements, int if_not_exists)
    1863             : {
    1864        1085 :         mvc *sql = query->sql;
    1865        1085 :         char *name = dlist_get_schema_name(auth_name);
    1866        1085 :         char *auth = schema_auth(auth_name);
    1867        1085 :         sqlid auth_id = sql->role_id;
    1868             : 
    1869        1085 :         if (auth && (auth_id = sql_find_auth(sql, auth)) < 0)
    1870           0 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(28000) "CREATE SCHEMA: no such authorization '%s'", auth);
    1871        1085 :         if (sql->user_id != USER_MONETDB && sql->role_id != ROLE_SYSADMIN)
    1872           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE SCHEMA: insufficient privileges for user '%s'", get_string_global_var(sql, "current_user"));
    1873        1084 :         if (!name)
    1874           0 :                 name = auth;
    1875           0 :         assert(name);
    1876        1084 :         if (mvc_bind_schema(sql, name)) {
    1877           2 :                 if (!if_not_exists)
    1878           1 :                         return sql_error(sql, 02, SQLSTATE(3F000) "CREATE SCHEMA: name '%s' already in use", name);
    1879           1 :                 return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    1880             :         } else {
    1881        1082 :                 sql_schema *os = cur_schema(sql);
    1882        1082 :                 dnode *n = schema_elements->h;
    1883        1082 :                 sql_schema *ss = SA_ZNEW(sql->sa, sql_schema);
    1884        1082 :                 sql_rel *ret = rel_create_schema_dll(sql->sa, name, auth, 0);
    1885             : 
    1886        1082 :                 ss->base.name = name;
    1887        1082 :                 ss->auth_id = auth_id;
    1888        1082 :                 ss->owner = sql->user_id;
    1889             : 
    1890        1082 :                 sql->session->schema = ss;
    1891        1084 :                 while (n) {
    1892           2 :                         sql_rel *res = rel_semantic(query, n->data.sym);
    1893           2 :                         if (!res) {
    1894           0 :                                 rel_destroy(ret);
    1895           0 :                                 sql->session->schema = os;
    1896           0 :                                 return NULL;
    1897             :                         }
    1898           2 :                         ret = rel_list(sql->sa, ret, res);
    1899           2 :                         n = n->next;
    1900             :                 }
    1901        1082 :                 sql->session->schema = os;
    1902        1082 :                 return ret;
    1903             :         }
    1904             : }
    1905             : 
    1906             : static sql_rel *
    1907        3775 : sql_drop_table(sql_query *query, dlist *qname, int nr, int if_exists)
    1908             : {
    1909        3775 :         mvc *sql = query->sql;
    1910        3775 :         char *sname = qname_schema(qname);
    1911        3775 :         char *tname = qname_schema_object(qname);
    1912        3775 :         sql_table *t = NULL;
    1913             : 
    1914        3775 :         if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "DROP TABLE", false))) {
    1915          94 :                 if (if_exists) {
    1916          48 :                         sql->errstr[0] = '\0'; /* reset table not found error */
    1917          48 :                         sql->session->status = 0;
    1918          48 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    1919             :                 }
    1920             :                 return NULL;
    1921             :         }
    1922        3681 :         if (isDeclaredTable(t))
    1923           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "DROP TABLE: cannot drop a declared table");
    1924             : 
    1925        3681 :         return rel_drop(sql->sa, ddl_drop_table, t->s->base.name, tname, NULL, nr, if_exists);
    1926             : }
    1927             : 
    1928             : static sql_rel *
    1929         335 : sql_drop_view(sql_query *query, dlist *qname, int nr, int if_exists)
    1930             : {
    1931         335 :         mvc *sql = query->sql;
    1932         335 :         char *sname = qname_schema(qname);
    1933         335 :         char *tname = qname_schema_object(qname);
    1934         335 :         sql_table *t = NULL;
    1935             : 
    1936         335 :         if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "DROP VIEW", true))) {
    1937          20 :                 if (if_exists) {
    1938           1 :                         sql->errstr[0] = '\0'; /* reset table not found error */
    1939           1 :                         sql->session->status = 0;
    1940           1 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    1941             :                 }
    1942             :                 return NULL;
    1943             :         }
    1944         315 :         if (!isView(t))
    1945           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "DROP VIEW: unable to drop view '%s': is a table", tname);
    1946             : 
    1947         314 :         return rel_drop(sql->sa, ddl_drop_view, t->s->base.name, tname, NULL, nr, if_exists);
    1948             : }
    1949             : 
    1950             : static sql_rel *
    1951        4383 : sql_alter_table(sql_query *query, dlist *dl, dlist *qname, symbol *te, int if_exists)
    1952             : {
    1953        4383 :         mvc *sql = query->sql;
    1954        4383 :         char *sname = qname_schema(qname);
    1955        4383 :         char *tname = qname_schema_object(qname);
    1956        4383 :         sql_table *t = NULL, *nt = NULL;
    1957        4383 :         sql_rel *res = NULL, *r;
    1958        4383 :         sql_exp **updates, *e;
    1959             : 
    1960        4383 :         if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "ALTER TABLE", false))) {
    1961          15 :                 if (if_exists) {
    1962           1 :                         sql->errstr[0] = '\0'; /* reset table not found error */
    1963           1 :                         sql->session->status = 0;
    1964           1 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    1965             :                 }
    1966             :                 return NULL;
    1967             :         }
    1968        4368 :         if (isDeclaredTable(t))
    1969           0 :                 return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: can't alter declared table '%s'", tname);
    1970        4368 :         if (isTempSchema(t->s))
    1971           3 :                 return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: can't alter temporary table '%s'", tname);
    1972        4365 :         if (!mvc_schema_privs(sql, t->s))
    1973           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
    1974             : 
    1975        4365 :         assert(te);
    1976        4365 :         if (t->persistence != SQL_DECLARED_TABLE)
    1977        4365 :                 sname = t->s->base.name;
    1978             : 
    1979        4365 :         if ((te->token == SQL_TABLE || te->token == SQL_DROP_TABLE)) {
    1980         795 :                 dlist *nqname = te->data.lval->h->data.lval;
    1981         795 :                 sql_table *pt = NULL;
    1982         795 :                 char *nsname = qname_schema(nqname);
    1983         795 :                 char *ntname = qname_schema_object(nqname);
    1984             : 
    1985         795 :                 if (!(pt = find_table_or_view_on_scope(sql, t->s, nsname, ntname, "ALTER TABLE", false)))
    1986             :                         return NULL;
    1987         785 :                 if (isView(pt))
    1988           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a view into a %s",
    1989           1 :                                                         TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    1990         784 :                 if (isDeclaredTable(pt))
    1991           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a declared table into a %s",
    1992           0 :                                                         TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    1993         784 :                 if (isTempSchema(pt->s))
    1994           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a temporary table into a %s",
    1995           2 :                                                         TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    1996         782 :                 if (isReplicaTable(t) && isMergeTable(pt))
    1997           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a %s table into a %s",
    1998           0 :                                                         TABLE_TYPE_DESCRIPTION(pt->type, pt->properties), TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    1999         782 :                 nsname = pt->s->base.name;
    2000         782 :                 if (strcmp(sname, nsname) != 0)
    2001           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: all children tables of '%s.%s' must be part of schema '%s'",
    2002             :                                                 sname, tname, sname);
    2003             : 
    2004         781 :                 if (te->token == SQL_TABLE) {
    2005         601 :                         symbol *extra = dl->h->next->next->next->data.sym;
    2006             : 
    2007         601 :                         if (!extra) {
    2008         314 :                                 if (isRangePartitionTable(t)) {
    2009           1 :                                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: a range partition is required while adding under a %s",
    2010           1 :                                                                 TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    2011         313 :                                 } else if (isListPartitionTable(t)) {
    2012           1 :                                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: a value partition is required while adding under a %s",
    2013           1 :                                                                 TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    2014             :                                 }
    2015         312 :                                 return rel_alter_table(sql->sa, ddl_alter_table_add_table, sname, tname, nsname, ntname, 0);
    2016             :                         }
    2017         287 :                         if ((isMergeTable(pt) || isReplicaTable(pt)) && list_length(pt->members)==0)
    2018           2 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s '%s'.'%s' should have at least one table associated",
    2019           2 :                                                                 TABLE_TYPE_DESCRIPTION(pt->type, pt->properties), pt->s->base.name, pt->base.name);
    2020             : 
    2021         285 :                         if (extra->token == SQL_MERGE_PARTITION) { /* partition to hold null values only */
    2022          17 :                                 dlist* ll = extra->data.lval;
    2023          17 :                                 int update = ll->h->next->next->next->data.i_val;
    2024             : 
    2025          17 :                                 if (isRangePartitionTable(t)) {
    2026          14 :                                         return rel_alter_table_add_partition_range(query, t, pt, sname, tname, nsname, ntname, NULL, NULL, true, update);
    2027           3 :                                 } else if (isListPartitionTable(t)) {
    2028           3 :                                         return rel_alter_table_add_partition_list(query, t, pt, sname, tname, nsname, ntname, NULL, true, update);
    2029             :                                 } else {
    2030           0 :                                         return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot add a partition into a %s",
    2031           0 :                                                                 TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    2032             :                                 }
    2033         268 :                         } else if (extra->token == SQL_PARTITION_RANGE) {
    2034         208 :                                 dlist* ll = extra->data.lval;
    2035         208 :                                 symbol* min = ll->h->data.sym, *max = ll->h->next->data.sym;
    2036         208 :                                 int nills = ll->h->next->next->data.i_val, update = ll->h->next->next->next->data.i_val;
    2037             : 
    2038         208 :                                 if (!isRangePartitionTable(t)) {
    2039           2 :                                         return sql_error(sql, 02,SQLSTATE(42000) "ALTER TABLE: cannot add a range partition into a %s",
    2040           1 :                                                                 TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    2041             :                                 }
    2042             : 
    2043         207 :                                 assert(nills == 0 || nills == 1);
    2044         207 :                                 return rel_alter_table_add_partition_range(query, t, pt, sname, tname, nsname, ntname, min, max, (bit) nills, update);
    2045          60 :                         } else if (extra->token == SQL_PARTITION_LIST) {
    2046          60 :                                 dlist* ll = extra->data.lval, *values = ll->h->data.lval;
    2047          60 :                                 int nills = ll->h->next->data.i_val, update = ll->h->next->next->data.i_val;
    2048             : 
    2049          60 :                                 if (!isListPartitionTable(t)) {
    2050           2 :                                         return sql_error(sql, 02,SQLSTATE(42000) "ALTER TABLE: cannot add a value partition into a %s",
    2051           1 :                                                                 TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    2052             :                                 }
    2053             : 
    2054          59 :                                 assert(nills == 0 || nills == 1);
    2055          59 :                                 return rel_alter_table_add_partition_list(query, t, pt, sname, tname, nsname, ntname, values, (bit) nills, update);
    2056             :                         }
    2057           0 :                         assert(0);
    2058             :                 } else {
    2059         180 :                         int drop_action = te->data.lval->h->next->data.i_val;
    2060             : 
    2061         180 :                         return rel_alter_table(sql->sa, ddl_alter_table_del_table, sname, tname, nsname, ntname, drop_action);
    2062             :                 }
    2063             :         }
    2064             : 
    2065             :         /* read only or read write */
    2066        3570 :         if (te->token == SQL_ALTER_TABLE) {
    2067        2310 :                 int state = te->data.i_val;
    2068             : 
    2069        2310 :                 if (state == tr_readonly) {
    2070             :                         state = TABLE_READONLY;
    2071             :                 } else if (state == tr_append) {
    2072             :                         state = TABLE_APPENDONLY;
    2073             :                 } else {
    2074           0 :                         assert(state == tr_writable);
    2075             :                         state = TABLE_WRITABLE;
    2076             :                 }
    2077        2310 :                 return rel_alter_table(sql->sa, ddl_alter_table_set_access, sname, tname, NULL, NULL, state);
    2078             :         }
    2079             : 
    2080        1260 :         nt = dup_sql_table(sql->sa, t);
    2081        1260 :         if (!nt || (table_element(query, te, t->s, nt, 1, t->persistence == SQL_DECLARED_TABLE, "ALTER TABLE") == SQL_ERR))
    2082          76 :                 return NULL;
    2083             : 
    2084        1184 :         if (te->token == SQL_DROP_CONSTRAINT) {
    2085         150 :                 dlist *l = te->data.lval;
    2086         150 :                 char *kname = l->h->data.sval;
    2087         150 :                 int drop_action = l->h->next->data.i_val;
    2088             : 
    2089         150 :                 return rel_drop(sql->sa, ddl_drop_constraint, sname, tname, kname, drop_action, 0);
    2090             :         }
    2091             : 
    2092        1034 :         res = rel_table(sql, ddl_alter_table, sname, nt, 0);
    2093        1034 :         sql_rel *bt = rel_ddl_basetable_get(res);
    2094             : 
    2095        1034 :         if (!isTable(nt))
    2096             :                 return res;
    2097             : 
    2098             :         /* New columns need update with default values. Add one more element for new column */
    2099        1009 :         updates = SA_ZNEW_ARRAY(sql->sa, sql_exp*, (ol_length(nt->columns) + 1));
    2100        1009 :         rel_base_use_tid(sql, bt);
    2101             : 
    2102        1009 :         e = basetable_get_tid_or_add_it(sql, bt);
    2103        1009 :         e = exp_ref(sql, e);
    2104             : 
    2105             :         /*
    2106             :         e = exp_column(sql->sa, nt->base.name, TID, sql_bind_localtype("oid"), CARD_MULTI, 0, 1, 1);
    2107             :         e->alias.label = rel_base_nid(bt, NULL);
    2108             :         */
    2109        1009 :         r = rel_project(sql->sa, res, append(new_exp_list(sql->sa),e));
    2110             : 
    2111        1009 :         list *cols = new_exp_list(sql->sa);
    2112        1009 :         sql_exp *ne;
    2113       14571 :         for (node *n = ol_first_node(nt->columns); n; n = n->next) {
    2114       13562 :                 sql_column *c = n->data;
    2115             : 
    2116       13562 :                 rel_base_use(sql, bt, c->colnr);
    2117             :                 /* handle new columns */
    2118       13562 :                 if (!c->base.new || c->base.deleted)
    2119       13457 :                         continue;
    2120         105 :                 if (c->def) {
    2121           5 :                         e = rel_parse_val(sql, nt->s, c->def, &c->type, sql->emode, NULL);
    2122             :                 } else {
    2123         100 :                         e = exp_atom(sql->sa, atom_general(sql->sa, &c->type, NULL, 0));
    2124             :                 }
    2125         105 :                 if (!e || (e = exp_check_type(sql, &c->type, r, e, type_equal)) == NULL) {
    2126           0 :                         rel_destroy(r);
    2127           0 :                         return NULL;
    2128             :                 }
    2129         105 :                 list_append(cols, ne=exp_column(sql->sa, nt->base.name, c->base.name, &c->type, CARD_MULTI, 0, 0, 0));
    2130         105 :                 ne->alias.label = rel_base_nid(bt, c);
    2131         105 :                 ne->nid = ne->alias.label;
    2132             : 
    2133         105 :                 assert(!updates[c->colnr]);
    2134         105 :                 exp_setname(sql, e, c->t->base.name, c->base.name);
    2135         105 :                 updates[c->colnr] = e;
    2136             :         }
    2137        1009 :         res = rel_update(sql, res, r, updates, list_length(cols)?cols:NULL);
    2138        1009 :         return res;
    2139             : }
    2140             : 
    2141             : static sql_rel *
    2142          50 : rel_role(allocator *sa, char *grantee, char *auth, int grantor, int admin, int type)
    2143             : {
    2144          50 :         sql_rel *rel = rel_create(sa);
    2145          50 :         list *exps = new_exp_list(sa);
    2146          50 :         if (!rel || !exps)
    2147             :                 return NULL;
    2148             : 
    2149          50 :         assert(type == ddl_grant_roles || type == ddl_revoke_roles);
    2150          50 :         append(exps, exp_atom_clob(sa, grantee));
    2151          50 :         append(exps, exp_atom_clob(sa, auth));
    2152          50 :         append(exps, exp_atom_int(sa, grantor));
    2153          50 :         append(exps, exp_atom_int(sa, admin));
    2154          50 :         rel->l = NULL;
    2155          50 :         rel->r = NULL;
    2156          50 :         rel->op = op_ddl;
    2157          50 :         rel->flag = type;
    2158          50 :         rel->exps = exps;
    2159          50 :         rel->card = 0;
    2160          50 :         rel->nrcols = 0;
    2161          50 :         return rel;
    2162             : }
    2163             : 
    2164             : static sql_rel *
    2165          50 : rel_grant_or_revoke_roles(mvc *sql, dlist *roles, dlist *grantees, int grant, int grantor, ddl_statement action)
    2166             : {
    2167          50 :         sql_rel *res = NULL;
    2168             :         /* grant/revoke roles to the grantees */
    2169             : 
    2170         100 :         for (dnode *r = roles->h; r; r = r->next) {
    2171          50 :                 char *role = r->data.sval;
    2172             : 
    2173         100 :                 for (dnode *g = grantees->h; g; g = g->next) {
    2174          50 :                         char *grantee = g->data.sval;
    2175             : 
    2176          50 :                         if ((res = rel_list(sql->sa, res, rel_role(sql->sa, grantee, role, grantor, grant, action))) == NULL) {
    2177           0 :                                 rel_destroy(res);
    2178           0 :                                 return NULL;
    2179             :                         }
    2180             :                 }
    2181             :         }
    2182             :         return res;
    2183             : }
    2184             : 
    2185             : static sql_rel *
    2186       18470 : rel_priv(allocator *sa, char *sname, char *name, char *grantee, int privs, char *cname, int grant, int grantor, int type)
    2187             : {
    2188       18470 :         sql_rel *rel = rel_create(sa);
    2189       18470 :         list *exps = new_exp_list(sa);
    2190       18470 :         if (!rel || !exps)
    2191             :                 return NULL;
    2192             : 
    2193       18470 :         assert(type == ddl_grant || type == ddl_revoke);
    2194       18470 :         append(exps, exp_atom_clob(sa, sname));
    2195       18470 :         append(exps, exp_atom_clob(sa, name));
    2196       18470 :         append(exps, exp_atom_clob(sa, grantee));
    2197       18470 :         append(exps, exp_atom_int(sa, privs));
    2198       18470 :         append(exps, cname?(void*)exp_atom_clob(sa, cname):(void*)cname);
    2199       18470 :         append(exps, exp_atom_int(sa, grant));
    2200       18470 :         append(exps, exp_atom_int(sa, grantor));
    2201       18470 :         rel->l = NULL;
    2202       18470 :         rel->r = NULL;
    2203       18470 :         rel->op = op_ddl;
    2204       18470 :         rel->flag = type;
    2205       18470 :         rel->exps = exps;
    2206       18470 :         rel->card = 0;
    2207       18470 :         rel->nrcols = 0;
    2208       18470 :         return rel;
    2209             : }
    2210             : 
    2211             : static sql_rel *
    2212       99260 : rel_func_priv(allocator *sa, char *sname, int func, char *grantee, int privs, int grant, int grantor, int type)
    2213             : {
    2214       99260 :         sql_rel *rel = rel_create(sa);
    2215       99260 :         list *exps = new_exp_list(sa);
    2216       99260 :         if (!rel || !exps)
    2217             :                 return NULL;
    2218             : 
    2219       99260 :         assert(type == ddl_grant_func || type == ddl_revoke_func);
    2220       99260 :         append(exps, exp_atom_clob(sa, sname));
    2221       99260 :         append(exps, exp_atom_int(sa, func));
    2222       99260 :         append(exps, exp_atom_clob(sa, grantee));
    2223       99260 :         append(exps, exp_atom_int(sa, privs));
    2224       99260 :         append(exps, exp_atom_int(sa, grant));
    2225       99260 :         append(exps, exp_atom_int(sa, grantor));
    2226       99260 :         rel->l = NULL;
    2227       99260 :         rel->r = NULL;
    2228       99260 :         rel->op = op_ddl;
    2229       99260 :         rel->flag = type;
    2230       99260 :         rel->exps = exps;
    2231       99260 :         rel->card = 0;
    2232       99260 :         rel->nrcols = 0;
    2233       99260 :         return rel;
    2234             : }
    2235             : 
    2236             : static sql_rel *
    2237          12 : rel_grant_or_revoke_global(mvc *sql, dlist *privs, dlist *grantees, int grant, int grantor, ddl_statement action)
    2238             : {
    2239          12 :         sql_rel *res = NULL;
    2240          12 :         char *sname = cur_schema(sql)->base.name;
    2241             : 
    2242          12 :         if (!privs)
    2243             :                 return NULL;
    2244          24 :         for (dnode *gn = grantees->h; gn; gn = gn->next) {
    2245          12 :                 char *grantee = gn->data.sval;
    2246             : 
    2247          12 :                 if (!grantee)
    2248           0 :                         grantee = "public";
    2249             : 
    2250          24 :                 for (dnode *opn = privs->h; opn; opn = opn->next) {
    2251          12 :                         int priv = opn->data.i_val;
    2252             : 
    2253          12 :                         if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, sname, NULL, grantee, priv, NULL, grant, grantor, action))) == NULL) {
    2254           0 :                                 rel_destroy(res);
    2255           0 :                                 return NULL;
    2256             :                         }
    2257             :                 }
    2258             :         }
    2259             :         return res;
    2260             : }
    2261             : 
    2262             : static sql_rel *
    2263       18420 : rel_grant_or_revoke_table(mvc *sql, dlist *privs, dlist *qname, dlist *grantees, int grant, int grantor, ddl_statement action, const char *err)
    2264             : {
    2265       18420 :         sql_rel *res = NULL;
    2266       18420 :         int all = PRIV_SELECT | PRIV_UPDATE | PRIV_INSERT | PRIV_DELETE | PRIV_TRUNCATE;
    2267       18420 :         char *sname = qname_schema(qname);
    2268       18420 :         char *tname = qname_schema_object(qname);
    2269       18420 :         sql_table *t = NULL;
    2270             : 
    2271       18420 :         if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, err, false)))
    2272             :                 return NULL;
    2273       18419 :         if (isDeclaredTable(t))
    2274           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "Cannot %s on a declared table", err);
    2275       36838 :         for (dnode *gn = grantees->h; gn; gn = gn->next) {
    2276       18419 :                 char *grantee = gn->data.sval;
    2277             : 
    2278       18419 :                 if (!grantee)
    2279       18304 :                         grantee = "public";
    2280             : 
    2281       18419 :                 if (!privs) {
    2282           7 :                         if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, t->s->base.name, tname, grantee, all, NULL, grant, grantor, action))) == NULL) {
    2283           0 :                                 rel_destroy(res);
    2284           0 :                                 return NULL;
    2285             :                         }
    2286           7 :                         continue;
    2287             :                 }
    2288       36850 :                 for (dnode *opn = privs->h; opn; opn = opn->next) {
    2289       18438 :                         symbol *op = opn->data.sym;
    2290       18438 :                         int priv = PRIV_SELECT;
    2291             : 
    2292       18438 :                         switch (op->token) {
    2293             :                         case SQL_SELECT:
    2294             :                                 priv = PRIV_SELECT;
    2295             :                                 break;
    2296          28 :                         case SQL_UPDATE:
    2297          28 :                                 priv = PRIV_UPDATE;
    2298          28 :                                 break;
    2299          28 :                         case SQL_INSERT:
    2300          28 :                                 priv = PRIV_INSERT;
    2301          28 :                                 break;
    2302          21 :                         case SQL_DELETE:
    2303          21 :                                 priv = PRIV_DELETE;
    2304          21 :                                 break;
    2305           1 :                         case SQL_TRUNCATE:
    2306           1 :                                 priv = PRIV_TRUNCATE;
    2307           1 :                                 break;
    2308           0 :                         case SQL_EXECUTE:
    2309             :                         default:
    2310           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Cannot %s EXECUTE on table name %s", err, tname);
    2311             :                         }
    2312             : 
    2313       18438 :                         if ((op->token == SQL_SELECT || op->token == SQL_UPDATE) && op->data.lval) {
    2314         103 :                                 for (dnode *cn = op->data.lval->h; cn; cn = cn->next) {
    2315          58 :                                         char *cname = cn->data.sval;
    2316          58 :                                         if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, t->s->base.name, tname, grantee, priv, cname, grant, grantor, action))) == NULL) {
    2317           0 :                                                 rel_destroy(res);
    2318           0 :                                                 return NULL;
    2319             :                                         }
    2320             :                                 }
    2321       18393 :                         } else if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, t->s->base.name, tname, grantee, priv, NULL, grant, grantor, action))) == NULL) {
    2322           0 :                                 rel_destroy(res);
    2323           0 :                                 return NULL;
    2324             :                         }
    2325             :                 }
    2326             :         }
    2327             :         return res;
    2328             : }
    2329             : 
    2330             : static sql_rel *
    2331       99261 : rel_grant_or_revoke_func(mvc *sql, dlist *privs, dlist *qname, dlist *typelist, sql_ftype type, dlist *grantees, int grant, int grantor, ddl_statement action, const char *err)
    2332             : {
    2333       99261 :         sql_rel *res = NULL;
    2334       99261 :         char *sname = qname_schema(qname);
    2335       99261 :         char *fname = qname_schema_object(qname);
    2336       99261 :         sql_func *func = resolve_func(sql, sname, fname, typelist, type, err, 0);
    2337             : 
    2338       99261 :         if (!func)
    2339             :                 return NULL;
    2340       99260 :         if (!func->s)
    2341           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "Cannot %s EXECUTE on system function '%s'", err, fname);
    2342      198520 :         for (dnode *gn = grantees->h; gn; gn = gn->next) {
    2343       99260 :                 char *grantee = gn->data.sval;
    2344             : 
    2345       99260 :                 if (!grantee)
    2346       98785 :                         grantee = "public";
    2347             : 
    2348       99260 :                 if (!privs) {
    2349           4 :                         if ((res = rel_list(sql->sa, res, rel_func_priv(sql->sa, func->s->base.name, func->base.id, grantee, PRIV_EXECUTE, grant, grantor, action))) == NULL) {
    2350           0 :                                 rel_destroy(res);
    2351           0 :                                 return NULL;
    2352             :                         }
    2353           4 :                         continue;
    2354             :                 }
    2355      198512 :                 for (dnode *opn = privs->h; opn; opn = opn->next) {
    2356       99256 :                         symbol *op = opn->data.sym;
    2357             : 
    2358       99256 :                         if (op->token != SQL_EXECUTE)
    2359           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Can only %s 'EXECUTE' on function '%s'", err, fname);
    2360       99256 :                         if ((res = rel_list(sql->sa, res, rel_func_priv(sql->sa, func->s->base.name, func->base.id, grantee, PRIV_EXECUTE, grant, grantor, action))) == NULL) {
    2361           0 :                                 rel_destroy(res);
    2362           0 :                                 return NULL;
    2363             :                         }
    2364             :                 }
    2365             :         }
    2366             :         return res;
    2367             : }
    2368             : 
    2369             : static sql_rel *
    2370      117693 : rel_grant_or_revoke_privs(mvc *sql, dlist *privs, dlist *grantees, int grant, int grantor, ddl_statement action)
    2371             : {
    2372      117693 :         dlist *obj_privs = privs->h->data.lval;
    2373      117693 :         symbol *obj = privs->h->next->data.sym;
    2374      117693 :         tokens token = obj->token;
    2375      117693 :         const char *err = (action == ddl_grant) ? "GRANT" : "REVOKE";
    2376             : 
    2377      117693 :         switch (token) {
    2378          12 :         case SQL_GRANT:
    2379          12 :                 return rel_grant_or_revoke_global(sql, obj_privs, grantees, grant, grantor, action);
    2380       18420 :         case SQL_TABLE:
    2381             :         case SQL_NAME:
    2382       18420 :                 return rel_grant_or_revoke_table(sql, obj_privs, obj->data.lval, grantees, grant, grantor, action, err);
    2383       99261 :         case SQL_FUNC: {
    2384       99261 :                 dlist *r = obj->data.lval;
    2385       99261 :                 dlist *qname = r->h->data.lval;
    2386       99261 :                 dlist *typelist = r->h->next->data.lval;
    2387       99261 :                 sql_ftype type = (sql_ftype) r->h->next->next->data.i_val;
    2388             : 
    2389       99262 :                 return rel_grant_or_revoke_func(sql, obj_privs, qname, typelist, type, grantees, grant, grantor, (action == ddl_grant) ? ddl_grant_func : ddl_revoke_func, err);
    2390             :         }
    2391           0 :         default:
    2392           0 :                 return sql_error(sql, 02, SQLSTATE(M0M03) "%s: unknown token %d", err, (int) token);
    2393             :         }
    2394             : }
    2395             : 
    2396             : /* iname, itype, sname.tname (col1 .. coln) */
    2397             : static sql_rel *
    2398         343 : rel_create_index(mvc *sql, char *iname, idx_type itype, dlist *qname, dlist *column_list)
    2399             : {
    2400         343 :         sql_table *t = NULL, *nt;
    2401         343 :         sql_rel *r, *res;
    2402         343 :         sql_exp **updates, *e;
    2403         343 :         sql_idx *i;
    2404         343 :         dnode *n;
    2405         343 :         char *sname = qname_schema(qname), *tname = qname_schema_object(qname), *s = iname;
    2406             : 
    2407         343 :         if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "CREATE INDEX", false)))
    2408             :                 return NULL;
    2409         342 :         if (isDeclaredTable(t))
    2410           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: cannot create index on a declared table");
    2411         342 :         if (!mvc_schema_privs(sql, t->s))
    2412           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
    2413         342 :         if (!s || !*s) /* add this to be safe */
    2414           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: index name cannot be empty");
    2415         342 :         while (isdigit((unsigned char) *s))
    2416           0 :                 s++;
    2417         342 :         if (!*s) /* if an index name just contains digit characters, it can be mistaken with a label */
    2418           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: index name cannot contain just digit characters (0 through 9)");
    2419         342 :         if ((i = mvc_bind_idx(sql, t->s, iname)))
    2420           3 :                 return sql_error(sql, 02, SQLSTATE(42S11) "CREATE INDEX: name '%s' already in use", iname);
    2421         339 :         if (ol_find_name(t->keys, iname) || mvc_bind_key(sql, t->s, iname))
    2422           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: a key named '%s' already exists, and it would conflict with the index", iname);
    2423         339 :         if (!isTable(t))
    2424           2 :                 return sql_error(sql, 02, SQLSTATE(42S02) "CREATE INDEX: cannot create index on %s '%s'", TABLE_TYPE_DESCRIPTION(t->type, t->properties), tname);
    2425         337 :         nt = dup_sql_table(sql->sa, t);
    2426             : 
    2427         337 :         if (t->persistence != SQL_DECLARED_TABLE)
    2428         337 :                 sname = t->s->base.name;
    2429             : 
    2430             :         /* add index here */
    2431         337 :         switch (mvc_create_idx(&i, sql, nt, iname, itype)) {
    2432           0 :                 case -1:
    2433           0 :                         return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2434           0 :                 case -2:
    2435             :                 case -3:
    2436           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: transaction conflict detected");
    2437             :                 default:
    2438         337 :                         break;
    2439             :         }
    2440         768 :         for (n = column_list->h; n; n = n->next) {
    2441         433 :                 sql_column *c = mvc_bind_column(sql, nt, n->data.sval);
    2442             : 
    2443         433 :                 if (!c)
    2444           2 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CREATE INDEX: no such column '%s'", n->data.sval);
    2445         431 :                 switch (mvc_create_ic(sql, i, c)) {
    2446           0 :                         case -1:
    2447           0 :                                 return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2448           0 :                         case -2:
    2449             :                         case -3:
    2450           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: transaction conflict detected");
    2451             :                         default:
    2452         431 :                                 break;
    2453             :                 }
    2454             :         }
    2455         335 :         mvc_create_idx_done(sql, i);
    2456             : 
    2457             :         /* new columns need update with default values */
    2458         335 :         updates = SA_ZNEW_ARRAY(sql->sa, sql_exp*, ol_length(nt->columns));
    2459             : 
    2460         335 :         res = rel_table(sql, ddl_alter_table, sname, nt, 0);
    2461         335 :         e = exp_column(sql->sa, nt->base.name, TID, sql_bind_localtype("oid"), CARD_MULTI, 0, 1, 1);
    2462         335 :         sql_rel *bt = rel_ddl_basetable_get(res);
    2463         335 :         e->alias.label = rel_base_nid(bt, NULL);
    2464         335 :         e->nid = e->alias.label;
    2465         335 :         r = rel_project(sql->sa, res, append(new_exp_list(sql->sa),e));
    2466         335 :         res = rel_update(sql, res, r, updates, NULL);
    2467         335 :         return res;
    2468             : }
    2469             : 
    2470             : static sql_rel *
    2471         358 : rel_create_user(allocator *sa, char *user, char *passwd, int enc, char *fullname, char *schema, char *schema_path, lng max_memory, int max_workers, char *optimizer, char *default_role)
    2472             : {
    2473         358 :         sql_rel *rel = rel_create(sa);
    2474         358 :         list *exps = new_exp_list(sa);
    2475         358 :         if (!rel || !exps)
    2476             :                 return NULL;
    2477             : 
    2478         358 :         append(exps, exp_atom_clob(sa, user));
    2479         358 :         append(exps, exp_atom_clob(sa, passwd));
    2480         358 :         append(exps, exp_atom_int(sa, enc));
    2481         358 :         append(exps, exp_atom_clob(sa, schema));
    2482         358 :         append(exps, exp_atom_clob(sa, schema_path));
    2483         358 :         append(exps, exp_atom_clob(sa, fullname));
    2484         358 :         append(exps, exp_atom_lng(sa, max_memory >= 0 ? max_memory : 0));
    2485         358 :         append(exps, exp_atom_int(sa, max_workers >= 0 ? max_workers: 0));
    2486         358 :         append(exps, exp_atom_clob(sa, optimizer));
    2487         358 :         append(exps, exp_atom_clob(sa, default_role));
    2488         358 :         rel->l = NULL;
    2489         358 :         rel->r = NULL;
    2490         358 :         rel->op = op_ddl;
    2491         358 :         rel->flag = ddl_create_user;
    2492         358 :         rel->exps = exps;
    2493         358 :         rel->card = 0;
    2494         358 :         rel->nrcols = 0;
    2495         358 :         return rel;
    2496             : }
    2497             : 
    2498             : static sql_rel *
    2499          83 : rel_alter_user(allocator *sa, char *user, char *passwd, int enc, char *schema, char *schema_path, char *oldpasswd, char *role, lng max_memory, int max_workers)
    2500             : {
    2501          83 :         sql_rel *rel = rel_create(sa);
    2502          83 :         list *exps = new_exp_list(sa);
    2503          83 :         if (!rel || !exps)
    2504             :                 return NULL;
    2505             : 
    2506          83 :         append(exps, exp_atom_clob(sa, user));
    2507          83 :         append(exps, exp_atom_clob(sa, passwd));
    2508          83 :         append(exps, exp_atom_int(sa, enc));
    2509          83 :         append(exps, exp_atom_clob(sa, schema));
    2510          83 :         append(exps, exp_atom_clob(sa, schema_path));
    2511          83 :         append(exps, exp_atom_clob(sa, oldpasswd));
    2512          83 :         append(exps, exp_atom_clob(sa, role));
    2513          83 :         append(exps, exp_atom_lng(sa, max_memory));
    2514          83 :         append(exps, exp_atom_int(sa, max_workers));
    2515             : 
    2516          83 :         rel->l = NULL;
    2517          83 :         rel->r = NULL;
    2518          83 :         rel->op = op_ddl;
    2519          83 :         rel->flag = ddl_alter_user;
    2520          83 :         rel->exps = exps;
    2521          83 :         rel->card = 0;
    2522          83 :         rel->nrcols = 0;
    2523          83 :         return rel;
    2524             : }
    2525             : 
    2526             : static sqlid
    2527         245 : rel_find_designated_schema(mvc *sql, symbol *sym, sql_schema **schema_out) {
    2528         245 :         char *sname;
    2529         245 :         sql_schema *s;
    2530             : 
    2531         245 :         assert(sym->type == type_string);
    2532         245 :         sname = sym->data.sval;
    2533         245 :         if (!(s = mvc_bind_schema(sql, sname))) {
    2534           0 :                 sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "COMMENT ON: no such schema: '%s'", sname);
    2535           0 :                 return 0;
    2536             :         }
    2537             : 
    2538         245 :         *schema_out = s;
    2539         245 :         return s->base.id;
    2540             : }
    2541             : 
    2542             : static sqlid
    2543          29 : rel_find_designated_table(mvc *sql, symbol *sym, sql_schema **schema_out) {
    2544          29 :         dlist *qname;
    2545          29 :         char *sname, *tname;
    2546          29 :         sql_table *t;
    2547          29 :         int want_table = sym->token == SQL_TABLE;
    2548             : 
    2549          29 :         assert(sym->type == type_list);
    2550          29 :         qname = sym->data.lval;
    2551          29 :         sname = qname_schema(qname);
    2552          29 :         tname = qname_schema_object(qname);
    2553          29 :         t = find_table_or_view_on_scope(sql, NULL, sname, tname, "COMMENT ON", !want_table);
    2554          29 :         if (t && isDeclaredTable(t)) {
    2555           0 :                 sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON declared table not allowed");
    2556           0 :                 return 0;
    2557             :         }
    2558          29 :         if (t && t->s && isTempSchema(t->s)) {
    2559           0 :                 sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
    2560           0 :                 return 0;
    2561             :         }
    2562          51 :         if (t && !want_table == !isKindOfTable(t)) {    /* comparing booleans can be tricky */
    2563          29 :                 *schema_out = t->s;
    2564          29 :                 return t->base.id;
    2565             :         }
    2566             : 
    2567           0 :         sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "COMMENT ON: no such %s: %s%s%s'%s'",
    2568             :                           want_table ? "table" : "view", sname ? "'":"", sname ? sname : "", sname ? "'.":"", tname);
    2569           0 :         return 0;
    2570             : }
    2571             : 
    2572             : static sqlid
    2573          24 : rel_find_designated_column(mvc *sql, symbol *sym, sql_schema **schema_out) {
    2574          24 :         char *sname, *tname, *cname;
    2575          24 :         dlist *colname;
    2576          24 :         sql_table *t;
    2577          24 :         sql_column *c;
    2578             : 
    2579          24 :         assert(sym->type == type_list);
    2580          24 :         colname = sym->data.lval;
    2581          24 :         assert(colname->cnt == 2 || colname->cnt == 3);
    2582          24 :         assert(colname->h->type == type_string);
    2583          24 :         assert(colname->h->next->type == type_string);
    2584          24 :         if (colname->cnt == 2) {
    2585          10 :                 sname = NULL;
    2586          10 :                 tname = colname->h->data.sval;
    2587          10 :                 cname = colname->h->next->data.sval;
    2588             :         } else {
    2589             :                 // cnt == 3
    2590          14 :                 sname = colname->h->data.sval;
    2591          14 :                 tname = colname->h->next->data.sval;
    2592          14 :                 assert(colname->h->next->next->type == type_string);
    2593          14 :                 cname = colname->h->next->next->data.sval;
    2594             :         }
    2595          24 :         if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "COMMENT ON", false)))
    2596             :                 return 0;
    2597          24 :         if (t && isDeclaredTable(t)) {
    2598           0 :                 sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON declared table not allowed");
    2599           0 :                 return 0;
    2600             :         }
    2601          24 :         if (t && t->s && isTempSchema(t->s)) {
    2602           0 :                 sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
    2603           0 :                 return 0;
    2604             :         }
    2605          24 :         if (!(c = mvc_bind_column(sql, t, cname))) {
    2606           0 :                 sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S12) "COMMENT ON: no such column: %s%s%s'%s'.'%s'",
    2607             :                                   sname ? "'":"", sname ? sname : "", sname ? "'.":"", tname, cname);
    2608           0 :                 return 0;
    2609             :         }
    2610          24 :         *schema_out = t->s;
    2611          24 :         return c->base.id;
    2612             : }
    2613             : 
    2614             : static sqlid
    2615          21 : rel_find_designated_index(mvc *sql, symbol *sym, sql_schema **schema_out) {
    2616          21 :         dlist *qname;
    2617          21 :         char *iname, *sname;
    2618          21 :         sql_idx *idx;
    2619             : 
    2620          21 :         assert(sym->type == type_list);
    2621          21 :         qname = sym->data.lval;
    2622          21 :         sname = qname_schema(qname);
    2623          21 :         iname = qname_schema_object(qname);
    2624          21 :         if (!(idx = find_idx_on_scope(sql, sname, iname, "COMMENT ON")))
    2625             :                 return 0;
    2626          21 :         if (idx && idx->t->s && isTempSchema(idx->t->s)) {
    2627           0 :                 sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
    2628           0 :                 return 0;
    2629             :         }
    2630             :         if (idx) {
    2631          21 :                 *schema_out = idx->t->s;
    2632          21 :                 return idx->base.id;
    2633             :         }
    2634             : 
    2635             :         return 0;
    2636             : }
    2637             : 
    2638             : static sqlid
    2639          12 : rel_find_designated_sequence(mvc *sql, symbol *sym, sql_schema **schema_out) {
    2640          12 :         (void)sql;
    2641          12 :         (void)sym;
    2642          12 :         dlist *qname;
    2643          12 :         char *seqname, *sname;
    2644          12 :         sql_sequence *seq;
    2645             : 
    2646          12 :         assert(sym->type == type_list);
    2647          12 :         qname = sym->data.lval;
    2648          12 :         sname = qname_schema(qname);
    2649          12 :         seqname = qname_schema_object(qname);
    2650             : 
    2651          12 :         seq = find_sequence_on_scope(sql, sname, seqname, "COMMENT ON");
    2652          12 :         if (seq && seq->s && isTempSchema(seq->s)) {
    2653           0 :                 sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
    2654           0 :                 return 0;
    2655             :         }
    2656             :         if (seq) {
    2657          12 :                 *schema_out = seq->s;
    2658          12 :                 return seq->base.id;
    2659             :         }
    2660             : 
    2661             :         return 0;
    2662             : }
    2663             : 
    2664             : static sqlid
    2665          24 : rel_find_designated_routine(mvc *sql, symbol *sym, sql_schema **schema_out) {
    2666          24 :         (void)sql;
    2667          24 :         (void)sym;
    2668          24 :         dlist *designator;
    2669          24 :         dlist *qname;
    2670          24 :         dlist *typelist;
    2671          24 :         sql_ftype func_type;
    2672          24 :         char *fname, *sname;
    2673          24 :         sql_func *func;
    2674             : 
    2675          24 :         assert(sym->type == type_list);
    2676          24 :         designator = sym->data.lval;
    2677          24 :         assert(designator->cnt == 3);
    2678          24 :         qname = designator->h->data.lval;
    2679          24 :         sname = qname_schema(qname);
    2680          24 :         typelist = designator->h->next->data.lval;
    2681          24 :         func_type = (sql_ftype) designator->h->next->next->data.i_val;
    2682             : 
    2683          24 :         fname = qname_schema_object(qname);
    2684          24 :         func = resolve_func(sql, sname, fname, typelist, func_type, "COMMENT", 0);
    2685          24 :         if (func && func->s && isTempSchema(func->s)) {
    2686           0 :                 sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
    2687           0 :                 return 0;
    2688             :         }
    2689             :         if (func) {
    2690          24 :                 *schema_out = func->s ? func->s : mvc_bind_schema(sql, "sys");
    2691          24 :                 return func->base.id;
    2692             :         }
    2693             : 
    2694             :         return 0;
    2695             : }
    2696             : 
    2697             : static sqlid
    2698         355 : rel_find_designated_object(mvc *sql, symbol *sym, sql_schema **schema_out)
    2699             : {
    2700         355 :         sql_schema *dummy = NULL;
    2701             : 
    2702         355 :         if (schema_out == NULL)
    2703           0 :                 schema_out = &dummy;
    2704         355 :         switch (sym->token) {
    2705         245 :         case SQL_SCHEMA:
    2706         245 :                 return rel_find_designated_schema(sql, sym, schema_out);
    2707          22 :         case SQL_TABLE:
    2708          22 :                 return rel_find_designated_table(sql, sym, schema_out);
    2709           7 :         case SQL_VIEW:
    2710           7 :                 return rel_find_designated_table(sql, sym, schema_out);
    2711          24 :         case SQL_COLUMN:
    2712          24 :                 return rel_find_designated_column(sql, sym, schema_out);
    2713          21 :         case SQL_INDEX:
    2714          21 :                 return rel_find_designated_index(sql, sym, schema_out);
    2715          12 :         case SQL_SEQUENCE:
    2716          12 :                 return rel_find_designated_sequence(sql, sym, schema_out);
    2717          24 :         case SQL_ROUTINE:
    2718          24 :                 return rel_find_designated_routine(sql, sym, schema_out);
    2719           0 :         default:
    2720           0 :                 sql_error(sql, 2, SQLSTATE(42000) "COMMENT ON %s is not supported", token2string(sym->token));
    2721           0 :                 return 0;
    2722             :         }
    2723             : }
    2724             : 
    2725             : static sql_rel *
    2726         354 : rel_comment_on(allocator *sa, sqlid obj_id, const char *remark)
    2727             : {
    2728         354 :         sql_rel *rel = rel_create(sa);
    2729         354 :         list *exps = new_exp_list(sa);
    2730             : 
    2731         354 :         if (rel == NULL || exps == NULL)
    2732             :                 return NULL;
    2733             : 
    2734         354 :         append(exps, exp_atom_int(sa, obj_id));
    2735         354 :         append(exps, exp_atom_clob(sa, remark));
    2736         354 :         rel->l = NULL;
    2737         354 :         rel->r = NULL;
    2738         354 :         rel->op = op_ddl;
    2739         354 :         rel->flag = ddl_comment_on;
    2740         354 :         rel->exps = exps;
    2741         354 :         rel->card = 0;
    2742         354 :         rel->nrcols = 0;
    2743         354 :         return rel;
    2744             : }
    2745             : 
    2746             : static char *
    2747       10151 : credentials_username(dlist *credentials)
    2748             : {
    2749       10151 :         if (credentials == NULL) {
    2750             :                 return NULL;
    2751             :         }
    2752         104 :         assert(credentials->h);
    2753             : 
    2754         104 :         if (credentials->h->data.sval != NULL) {
    2755             :                 return credentials->h->data.sval;
    2756             :         }
    2757             : 
    2758             :         // No username specified.
    2759             :         return NULL;
    2760             : }
    2761             : 
    2762             : static char *
    2763       10151 : credentials_password(dlist *credentials)
    2764             : {
    2765       10151 :         if (credentials == NULL) {
    2766             :                 return NULL;
    2767             :         }
    2768         104 :         assert(credentials->h);
    2769             : 
    2770         104 :         char *password = credentials->h->next->next->data.sval;
    2771             : 
    2772         104 :         return password;
    2773             : }
    2774             : 
    2775             : static sql_rel *
    2776          11 : rel_rename_schema(mvc *sql, char *old_name, char *new_name, int if_exists)
    2777             : {
    2778          11 :         sql_schema *s;
    2779          11 :         sql_rel *rel;
    2780          11 :         list *exps;
    2781             : 
    2782          11 :         assert(old_name && new_name);
    2783          11 :         if (!(s = mvc_bind_schema(sql, old_name))) {
    2784           1 :                 if (if_exists)
    2785           1 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    2786           0 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000)
    2787             :                                                  "ALTER SCHEMA: no such schema '%s'", old_name);
    2788             :         }
    2789             : 
    2790          10 :         if (!mvc_schema_privs(sql, s))
    2791           0 :                 return sql_error(sql, 02, SQLSTATE(3F000)
    2792             :                                                  "ALTER SCHEMA: access denied for %s to schema '%s'",
    2793             :                                                  get_string_global_var(sql, "current_user"), old_name);
    2794             : 
    2795          10 :         if (s->system)
    2796           1 :                 return sql_error(sql, 02, SQLSTATE(3F000)
    2797             :                                                  "ALTER SCHEMA: cannot rename a system schema");
    2798             : 
    2799           9 :         if (strNil(new_name) || *new_name == '\0')
    2800           0 :                 return sql_error(sql, 02, SQLSTATE(3F000)
    2801             :                                                  "ALTER SCHEMA: invalid new schema name");
    2802             : 
    2803           9 :         if (mvc_bind_schema(sql, new_name))
    2804           0 :                 return sql_error(sql, 02, SQLSTATE(3F000)
    2805             :                                                  "ALTER SCHEMA: there is a schema named '%s' in the database", new_name);
    2806             : 
    2807           9 :         if (mvc_check_dependency(sql, s->base.id, SCHEMA_DEPENDENCY, NULL) != NO_DEPENDENCY) {
    2808           1 :                 return sql_error(sql, 02,
    2809             :                                                  SQLSTATE(2BM37) "ALTER SCHEMA: unable to"
    2810             :                                                  " rename schema '%s', there are database objects"
    2811             :                                                  " which depend on it", old_name);
    2812             :         }
    2813             : 
    2814           8 :         rel = rel_create(sql->sa);
    2815           8 :         exps = new_exp_list(sql->sa);
    2816           8 :         append(exps, exp_atom_clob(sql->sa, old_name));
    2817           8 :         append(exps, exp_atom_clob(sql->sa, new_name));
    2818           8 :         rel->op = op_ddl;
    2819           8 :         rel->flag = ddl_rename_schema;
    2820           8 :         rel->exps = exps;
    2821             : 
    2822           8 :         return rel;
    2823             : }
    2824             : 
    2825             : static sql_rel *
    2826          23 : rel_rename_table(mvc *sql, char *schema_name, char *old_name, char *new_name, int if_exists)
    2827             : {
    2828          23 :         sql_table *t = NULL;
    2829          23 :         sql_rel *rel;
    2830          23 :         list *exps;
    2831             : 
    2832          23 :         assert(old_name && new_name);
    2833             : 
    2834          23 :         if (!(t = find_table_or_view_on_scope(sql, NULL, schema_name, old_name, "ALTER TABLE", false))) {
    2835           1 :                 if (if_exists) {
    2836           1 :                         sql->errstr[0] = '\0'; /* reset table not found error */
    2837           1 :                         sql->session->status = 0;
    2838           1 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    2839             :                 }
    2840             :                 return NULL;
    2841             :         }
    2842          22 :         if (isDeclaredTable(t))
    2843           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a declared table");
    2844          22 :         if (!mvc_schema_privs(sql, t->s))
    2845           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
    2846          22 :         if (t->system)
    2847           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a system table");
    2848          22 :         if (isView(t))
    2849           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a view");
    2850          21 :         if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL))
    2851           2 :                 return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: unable to rename table '%s' (there are database objects which depend on it)", old_name);
    2852          19 :         if (strNil(new_name) || *new_name == '\0')
    2853           0 :                 return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: invalid new table name");
    2854          19 :         if (mvc_bind_table(sql, t->s, new_name))
    2855           0 :                 return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: there is a table named '%s' in schema '%s'", new_name, t->s->base.name);
    2856             : 
    2857          19 :         rel = rel_create(sql->sa);
    2858          19 :         exps = new_exp_list(sql->sa);
    2859          19 :         append(exps, exp_atom_clob(sql->sa, t->s->base.name));
    2860          19 :         append(exps, exp_atom_clob(sql->sa, t->s->base.name));
    2861          19 :         append(exps, exp_atom_clob(sql->sa, old_name));
    2862          19 :         append(exps, exp_atom_clob(sql->sa, new_name));
    2863          19 :         rel->op = op_ddl;
    2864          19 :         rel->flag = ddl_rename_table;
    2865          19 :         rel->exps = exps;
    2866          19 :         return rel;
    2867             : }
    2868             : 
    2869             : static sql_rel *
    2870          20 : rel_rename_column(mvc *sql, char *schema_name, char *table_name, char *old_name, char *new_name, int if_exists)
    2871             : {
    2872          20 :         sql_table *t = NULL;
    2873          20 :         sql_column *col;
    2874          20 :         sql_rel *rel;
    2875          20 :         list *exps;
    2876             : 
    2877          20 :         assert(table_name && old_name && new_name);
    2878             : 
    2879          20 :         if (!(t = find_table_or_view_on_scope(sql, NULL, schema_name, table_name, "ALTER TABLE", false))) {
    2880           2 :                 if (if_exists) {
    2881           1 :                         sql->errstr[0] = '\0'; /* reset table not found error */
    2882           1 :                         sql->session->status = 0;
    2883           1 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    2884             :                 }
    2885             :                 return NULL;
    2886             :         }
    2887          18 :         if (isDeclaredTable(t))
    2888           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a declared table");
    2889          18 :         if (!mvc_schema_privs(sql, t->s))
    2890           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
    2891          18 :         if (t->system)
    2892           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a system table");
    2893          18 :         if (isView(t))
    2894           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, table_name);
    2895          18 :         if (!(col = mvc_bind_column(sql, t, old_name)))
    2896           5 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, table_name);
    2897          13 :         if (mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL))
    2898           0 :                 return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: cannot rename column '%s' (there are database objects which depend on it)", old_name);
    2899          13 :         if (strNil(new_name) || *new_name == '\0')
    2900           0 :                 return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: invalid new column name");
    2901          13 :         if (mvc_bind_column(sql, t, new_name))
    2902           0 :                 return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, table_name);
    2903             : 
    2904          13 :         rel = rel_create(sql->sa);
    2905          13 :         exps = new_exp_list(sql->sa);
    2906          13 :         append(exps, exp_atom_clob(sql->sa, t->s->base.name));
    2907          13 :         append(exps, exp_atom_clob(sql->sa, table_name));
    2908          13 :         append(exps, exp_atom_clob(sql->sa, old_name));
    2909          13 :         append(exps, exp_atom_clob(sql->sa, new_name));
    2910          13 :         rel->op = op_ddl;
    2911          13 :         rel->flag = ddl_rename_column;
    2912          13 :         rel->exps = exps;
    2913          13 :         return rel;
    2914             : }
    2915             : 
    2916             : static sql_rel *
    2917          29 : rel_set_table_schema(sql_query *query, char *old_schema, char *tname, char *new_schema, int if_exists)
    2918             : {
    2919          29 :         mvc *sql = query->sql;
    2920          29 :         sql_schema *ns = NULL;
    2921          29 :         sql_table *ot = NULL;
    2922          29 :         sql_rel *rel;
    2923          29 :         list *exps;
    2924             : 
    2925          29 :         assert(tname && new_schema);
    2926             : 
    2927          29 :         if (!(ot = find_table_or_view_on_scope(sql, NULL, old_schema, tname, "ALTER TABLE", false))) {
    2928           0 :                 if (if_exists) {
    2929           0 :                         sql->errstr[0] = '\0'; /* reset table not found error */
    2930           0 :                         sql->session->status = 0;
    2931           0 :                         return rel_psm_block(sql->sa, new_exp_list(sql->sa));
    2932             :                 }
    2933             :                 return NULL;
    2934             :         }
    2935          29 :         if (isDeclaredTable(ot))
    2936           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a declared table");
    2937          29 :         if (!mvc_schema_privs(sql, ot->s))
    2938           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), ot->s->base.name);
    2939          29 :         if (ot->system)
    2940           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot set schema of a system table");
    2941          29 :         if (isTempSchema(ot->s))
    2942           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: not possible to change a temporary table schema");
    2943          29 :         if (isView(ot))
    2944           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a view");
    2945          29 :         if (mvc_check_dependency(sql, ot->base.id, TABLE_DEPENDENCY, NULL) || list_length(ot->members) || ol_length(ot->triggers))
    2946           3 :                 return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: unable to set schema of table '%s' (there are database objects which depend on it)", tname);
    2947          26 :         if (!(ns = mvc_bind_schema(sql, new_schema)))
    2948           0 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", new_schema);
    2949          26 :         if (!mvc_schema_privs(sql, ns))
    2950           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for '%s' to schema '%s'", get_string_global_var(sql, "current_user"), new_schema);
    2951          26 :         if (isTempSchema(ns))
    2952           0 :                 return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: not possible to change table's schema to temporary");
    2953          26 :         if (mvc_bind_table(sql, ns, tname))
    2954           0 :                 return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: table '%s' on schema '%s' already exists", tname, new_schema);
    2955             : 
    2956          26 :         rel = rel_create(sql->sa);
    2957          26 :         exps = new_exp_list(sql->sa);
    2958          26 :         append(exps, exp_atom_clob(sql->sa, ot->s->base.name));
    2959          26 :         append(exps, exp_atom_clob(sql->sa, new_schema));
    2960          26 :         append(exps, exp_atom_clob(sql->sa, tname));
    2961          26 :         append(exps, exp_atom_clob(sql->sa, tname));
    2962          26 :         rel->op = op_ddl;
    2963          26 :         rel->flag = ddl_rename_table;
    2964          26 :         rel->exps = exps;
    2965          26 :         return rel;
    2966             : }
    2967             : 
    2968             : sql_rel *
    2969      219177 : rel_schemas(sql_query *query, symbol *s)
    2970             : {
    2971      219177 :         mvc *sql = query->sql;
    2972      219177 :         sql_rel *ret = NULL;
    2973             : 
    2974      219177 :         if (s->token != SQL_CREATE_TABLE && s->token != SQL_CREATE_VIEW && store_readonly(sql->session->tr->store))
    2975           2 :                 return sql_error(sql, 06, SQLSTATE(25006) "Schema statements cannot be executed on a readonly database.");
    2976             : 
    2977      219175 :         switch (s->token) {
    2978        1085 :         case SQL_CREATE_SCHEMA:
    2979             :         {
    2980        1085 :                 dlist *l = s->data.lval;
    2981             : 
    2982        1085 :                 ret = rel_create_schema(query, l->h->data.lval,
    2983        1085 :                                 l->h->next->next->next->data.lval,
    2984        1085 :                                 l->h->next->next->next->next->data.i_val); /* if not exists */
    2985        1085 :         }       break;
    2986         191 :         case SQL_DROP_SCHEMA:
    2987             :         {
    2988         191 :                 dlist *l = s->data.lval;
    2989         191 :                 dlist *auth_name = l->h->data.lval;
    2990             : 
    2991         191 :                 assert(l->h->next->type == type_int);
    2992         382 :                 ret = rel_drop(sql->sa, ddl_drop_schema,
    2993             :                            dlist_get_schema_name(auth_name),
    2994             :                            NULL,
    2995             :                            NULL,
    2996             :                            l->h->next->data.i_val,     /* drop_action */
    2997         191 :                            l->h->next->next->data.i_val); /* if exists */
    2998         191 :         }       break;
    2999           1 :         case SQL_DECLARE_TABLE:
    3000           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "Tables cannot be declared on the global scope");
    3001       10151 :         case SQL_CREATE_TABLE:
    3002             :         {
    3003       10151 :                 dlist *l = s->data.lval;
    3004       10151 :                 dlist *qname = l->h->next->data.lval;
    3005       10151 :                 char *sname = qname_schema(qname);
    3006       10151 :                 char *name = qname_schema_object(qname);
    3007       10151 :                 int temp = l->h->data.i_val;
    3008       10151 :                 dlist *credentials = l->h->next->next->next->next->next->data.lval;
    3009       10151 :                 char *username = credentials_username(credentials);
    3010       10151 :                 char *password = credentials_password(credentials);
    3011       10151 :                 bool pw_encrypted = credentials == NULL || credentials->h->next->data.i_val == SQL_PW_ENCRYPTED;
    3012       10151 :                 if (username == NULL) {
    3013             :                         // No username specified, get the current username
    3014       10135 :                         username = get_string_global_var(sql, "current_user");
    3015             :                 }
    3016             : 
    3017       10151 :                 assert(l->h->type == type_int);
    3018       10151 :                 assert(l->h->next->next->next->type == type_int);
    3019       10151 :                 ret = rel_create_table(query, temp, sname, name, true,
    3020       10151 :                                        l->h->next->next->data.sym,                   /* elements or subquery */
    3021             :                                        l->h->next->next->next->data.i_val,           /* commit action */
    3022       10151 :                                        l->h->next->next->next->next->data.sval,      /* location */
    3023             :                                        username, password, pw_encrypted,
    3024       10151 :                                        l->h->next->next->next->next->next->next->next->data.sym,
    3025       10151 :                                        l->h->next->next->next->next->next->next->data.i_val); /* if not exists */
    3026       10151 :         }       break;
    3027       79024 :         case SQL_CREATE_VIEW:
    3028             :         {
    3029       79024 :                 dlist *l = s->data.lval;
    3030             : 
    3031       79024 :                 assert(l->h->type == type_int);
    3032       79024 :                 assert(l->h->next->next->next->next->type == type_int);
    3033       79024 :                 assert(l->h->next->next->next->next->next->type == type_int);
    3034       79024 :                 ret = rel_create_view(query, l->h->data.i_val,
    3035       79024 :                                                           l->h->next->data.lval,
    3036       79024 :                                                           l->h->next->next->data.lval,
    3037       79024 :                                                           l->h->next->next->next->data.sym,
    3038             :                                                           l->h->next->next->next->next->data.i_val,
    3039             :                                                           l->h->next->next->next->next->next->data.i_val,
    3040       79024 :                                                           l->h->next->next->next->next->next->next->data.i_val); /* or replace */
    3041       79024 :         }       break;
    3042        3775 :         case SQL_DROP_TABLE:
    3043             :         {
    3044        3775 :                 dlist *l = s->data.lval;
    3045             : 
    3046        3775 :                 assert(l->h->next->type == type_int);
    3047        3775 :                 ret = sql_drop_table(query, l->h->data.lval,
    3048             :                                                          l->h->next->data.i_val,
    3049        3775 :                                                          l->h->next->next->data.i_val); /* if exists */
    3050        3775 :         }       break;
    3051         335 :         case SQL_DROP_VIEW:
    3052             :         {
    3053         335 :                 dlist *l = s->data.lval;
    3054             : 
    3055         335 :                 assert(l->h->next->type == type_int);
    3056         335 :                 ret = sql_drop_view(query, l->h->data.lval,
    3057             :                                                         l->h->next->data.i_val,
    3058         335 :                                                         l->h->next->next->data.i_val); /* if exists */
    3059         335 :         }       break;
    3060        4383 :         case SQL_ALTER_TABLE:
    3061             :         {
    3062        4383 :                 dlist *l = s->data.lval;
    3063             : 
    3064        4383 :                 ret = sql_alter_table(query, l,
    3065        4383 :                         l->h->data.lval,      /* table name */
    3066        4383 :                         l->h->next->data.sym, /* table element */
    3067        4383 :                         l->h->next->next->data.i_val); /* if exists */
    3068        4383 :         }       break;
    3069          40 :         case SQL_GRANT_ROLES:
    3070             :         {
    3071          40 :                 dlist *l = s->data.lval;
    3072             : 
    3073          40 :                 assert(l->h->next->next->type == type_int);
    3074          40 :                 assert(l->h->next->next->next->type == type_int);
    3075          40 :                 ret = rel_grant_or_revoke_roles(sql, l->h->data.lval,     /* authids */
    3076          40 :                                   l->h->next->data.lval,       /* grantees */
    3077             :                                   l->h->next->next->data.i_val,     /* admin? */
    3078          40 :                                   l->h->next->next->next->data.i_val == cur_user ? sql->user_id : sql->role_id, ddl_grant_roles);
    3079             :                 /* grantor ? */
    3080          40 :         }       break;
    3081          10 :         case SQL_REVOKE_ROLES:
    3082             :         {
    3083          10 :                 dlist *l = s->data.lval;
    3084             : 
    3085          10 :                 assert(l->h->next->next->type == type_int);
    3086          10 :                 assert(l->h->next->next->next->type == type_int);
    3087          10 :                 ret = rel_grant_or_revoke_roles(sql, l->h->data.lval,     /* authids */
    3088          10 :                                   l->h->next->data.lval,       /* grantees */
    3089             :                                   l->h->next->next->data.i_val,     /* admin? */
    3090          10 :                                   l->h->next->next->next->data.i_val  == cur_user? sql->user_id : sql->role_id, ddl_revoke_roles);
    3091             :                 /* grantor ? */
    3092          10 :         }       break;
    3093      117677 :         case SQL_GRANT:
    3094             :         {
    3095      117677 :                 dlist *l = s->data.lval;
    3096             : 
    3097      117677 :                 assert(l->h->next->next->type == type_int);
    3098      117677 :                 assert(l->h->next->next->next->type == type_int);
    3099      117677 :                 ret = rel_grant_or_revoke_privs(sql, l->h->data.lval,     /* privileges */
    3100      117677 :                                   l->h->next->data.lval,       /* grantees */
    3101             :                                   l->h->next->next->data.i_val,     /* grant ? */
    3102      117677 :                                   l->h->next->next->next->data.i_val  == cur_user? sql->user_id : sql->role_id, ddl_grant);
    3103             :                 /* grantor ? */
    3104      117677 :         }       break;
    3105          16 :         case SQL_REVOKE:
    3106             :         {
    3107          16 :                 dlist *l = s->data.lval;
    3108             : 
    3109          16 :                 assert(l->h->next->next->type == type_int);
    3110          16 :                 assert(l->h->next->next->next->type == type_int);
    3111          16 :                 ret = rel_grant_or_revoke_privs(sql, l->h->data.lval,     /* privileges */
    3112          16 :                                    l->h->next->data.lval,      /* grantees */
    3113             :                                    l->h->next->next->data.i_val,    /* grant ? */
    3114          16 :                                    l->h->next->next->next->data.i_val  == cur_user? sql->user_id : sql->role_id, ddl_revoke);
    3115             :                 /* grantor ? */
    3116          16 :         }       break;
    3117          26 :         case SQL_CREATE_ROLE:
    3118             :         {
    3119          26 :                 dlist *l = s->data.lval;
    3120          26 :                 char *rname = l->h->data.sval;
    3121          26 :                 ret = rel_schema2(sql->sa, ddl_create_role, rname, NULL,
    3122          26 :                                  l->h->next->data.i_val  == cur_user? sql->user_id : sql->role_id);
    3123          26 :         }       break;
    3124          19 :         case SQL_DROP_ROLE:
    3125             :         {
    3126          19 :                 char *rname = s->data.sval;
    3127          19 :                 ret = rel_schema2(sql->sa, ddl_drop_role, rname, NULL, 0);
    3128          19 :         }       break;
    3129         343 :         case SQL_CREATE_INDEX: {
    3130         343 :                 dlist *l = s->data.lval;
    3131             : 
    3132         343 :                 assert(l->h->next->type == type_int);
    3133         343 :                 ret = rel_create_index(sql, l->h->data.sval, (idx_type) l->h->next->data.i_val, l->h->next->next->data.lval, l->h->next->next->next->data.lval);
    3134         343 :         }       break;
    3135         197 :         case SQL_DROP_INDEX: {
    3136         197 :                 dlist *l = s->data.lval;
    3137         197 :                 char *sname = qname_schema(l);
    3138         197 :                 char *iname = qname_schema_object(l);
    3139         197 :                 sql_idx *idx = NULL;
    3140             : 
    3141         197 :                 if (!(idx = find_idx_on_scope(sql, sname, iname, "DROP INDEX")))
    3142             :                         return NULL;
    3143         160 :                 ret = rel_schema2(sql->sa, ddl_drop_index, idx->t->s->base.name, iname, 0);
    3144         160 :         }       break;
    3145         358 :         case SQL_CREATE_USER: {
    3146         358 :                 dlist *l = s->data.lval;
    3147         358 :                 dlist *schema_details = l->h->next->next->next->data.lval;
    3148             : 
    3149         358 :                 ret = rel_create_user(sql->sa, l->h->data.sval,        /* user name */
    3150             :                                   l->h->next->data.sval,       /* password */
    3151         358 :                                   l->h->next->next->next->next->data.i_val == SQL_PW_ENCRYPTED, /* encrypted */
    3152             :                                   l->h->next->next->data.sval,      /* fullname */
    3153             :                                   schema_details->h->data.sval,   /* schema ident*/
    3154         358 :                                   schema_details->h->next->data.sval,  /* schema path */
    3155             :                                   l->h->next->next->next->next->next->data.l_val,  /* max memory */
    3156             :                                   l->h->next->next->next->next->next->next->data.i_val, /* max workers */
    3157             :                                   l->h->next->next->next->next->next->next->next->data.sval, /* optimizer */
    3158         358 :                                   l->h->next->next->next->next->next->next->next->next->data.sval); /* default role */
    3159         358 :         }       break;
    3160         107 :         case SQL_DROP_USER:
    3161         107 :                 ret = rel_schema2(sql->sa, ddl_drop_user, s->data.sval, NULL, 0);
    3162         107 :                 break;
    3163          83 :         case SQL_ALTER_USER: {
    3164          83 :                 dlist *l = s->data.lval;
    3165          83 :                 dnode *a = l->h->next->data.lval->h;
    3166             : 
    3167          83 :                 ret = rel_alter_user(sql->sa, l->h->data.sval, /* user */
    3168             :                              a->data.sval,   /* passwd */
    3169          83 :                              a->next->next->next->data.i_val == SQL_PW_ENCRYPTED, /* encrypted */
    3170             :                              a->next->data.sval,  /* schema */
    3171             :                                  a->next->next->data.sval, /* schema path */
    3172          83 :                              a->next->next->next->next->data.sval, /* old passwd */
    3173             :                              l->h->next->next->data.sval, /* default role */
    3174             :                              l->h->next->next->next->data.l_val, /* max_memory */
    3175          83 :                              l->h->next->next->next->next->data.i_val /* max_workers */
    3176             :                     );
    3177          83 :         }       break;
    3178           5 :         case SQL_RENAME_USER: {
    3179           5 :                 dlist *l = s->data.lval;
    3180             : 
    3181           5 :                 ret = rel_schema2(sql->sa, ddl_rename_user, l->h->data.sval, l->h->next->data.sval, 0);
    3182           5 :         }       break;
    3183          11 :         case SQL_RENAME_SCHEMA: {
    3184          11 :                 dlist *l = s->data.lval;
    3185          11 :                 ret = rel_rename_schema(sql, l->h->data.sval, l->h->next->data.sval, l->h->next->next->data.i_val);
    3186          11 :         }       break;
    3187          23 :         case SQL_RENAME_TABLE: {
    3188          23 :                 dlist *l = s->data.lval;
    3189          23 :                 char *sname = qname_schema(l->h->data.lval);
    3190          23 :                 char *tname = qname_schema_object(l->h->data.lval);
    3191          23 :                 ret = rel_rename_table(sql, sname, tname, l->h->next->data.sval, l->h->next->next->data.i_val);
    3192          23 :         }       break;
    3193          20 :         case SQL_RENAME_COLUMN: {
    3194          20 :                 dlist *l = s->data.lval;
    3195          20 :                 char *sname = qname_schema(l->h->data.lval);
    3196          20 :                 char *tname = qname_schema_object(l->h->data.lval);
    3197          20 :                 ret = rel_rename_column(sql, sname, tname, l->h->next->data.sval, l->h->next->next->data.sval, l->h->next->next->next->data.i_val);
    3198          20 :         }       break;
    3199          29 :         case SQL_SET_TABLE_SCHEMA: {
    3200          29 :                 dlist *l = s->data.lval;
    3201          29 :                 char *sname = qname_schema(l->h->data.lval);
    3202          29 :                 char *tname = qname_schema_object(l->h->data.lval);
    3203          29 :                 ret = rel_set_table_schema(query, sname, tname, l->h->next->data.sval, l->h->next->next->data.i_val);
    3204          29 :         }       break;
    3205         906 :         case SQL_CREATE_TYPE: {
    3206         906 :                 dlist *l = s->data.lval;
    3207             : 
    3208         906 :                 ret = rel_create_type(sql, l->h->data.lval, l->h->next->data.sval);
    3209         906 :         }       break;
    3210           5 :         case SQL_DROP_TYPE: {
    3211           5 :                 dlist *l = s->data.lval;
    3212           5 :                 ret = rel_drop_type(sql, l->h->data.lval, l->h->next->data.i_val);
    3213           5 :         }       break;
    3214         355 :         case SQL_COMMENT:
    3215             :         {
    3216         355 :                 dlist *l = s->data.lval;
    3217         355 :                 symbol *catalog_object = l->h->data.sym;
    3218         355 :                 char *remark;
    3219         355 :                 sql_schema *s;
    3220         355 :                 sqlid id;
    3221             : 
    3222         355 :                 assert(l->cnt == 2);
    3223         355 :                 remark = l->h->next->data.sval;
    3224             : 
    3225         355 :                 id = rel_find_designated_object(sql, catalog_object, &s);
    3226         355 :                 if (!id) {
    3227             :                         /* rel_find_designated_object has already set the error message so we don't have to */
    3228             :                         return NULL;
    3229             :                 }
    3230             : 
    3231             :                 // Check authorization
    3232         355 :                 if (!mvc_schema_privs(sql, s)) {
    3233           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON: insufficient privileges for user '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
    3234             :                 }
    3235             : 
    3236         354 :                 return rel_comment_on(sql->sa, id, remark);
    3237             :         }
    3238           0 :         default:
    3239           0 :                 return sql_error(sql, 01, SQLSTATE(M0M03) "Schema statement unknown symbol(%p)->token = %s", s, token2string(s->token));
    3240             :         }
    3241             : 
    3242      218782 :         sql->type = Q_SCHEMA;
    3243      218782 :         return ret;
    3244             : }

Generated by: LCOV version 1.14