LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql_upgrades.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 530 1459 36.3 %
Date: 2024-04-26 00:35:57 Functions: 21 29 72.4 %

          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             : /*
      14             :  * SQL upgrade code
      15             :  * N. Nes, M.L. Kersten, S. Mullender
      16             :  */
      17             : #include "monetdb_config.h"
      18             : #include "mal_backend.h"
      19             : #include "sql_execute.h"
      20             : #include "sql_mvc.h"
      21             : #include "gdk_time.h"
      22             : #include <unistd.h>
      23             : #include "sql_upgrades.h"
      24             : #include "rel_rel.h"
      25             : #include "rel_semantic.h"
      26             : 
      27             : #include "mal_authorize.h"
      28             : 
      29             : /* this function can be used to recreate the system tables (types,
      30             :  * functions, args) when internal types and/or functions have changed
      31             :  * (i.e. the ones in sql_types.c) */
      32             : static str
      33          16 : sql_fix_system_tables(Client c, mvc *sql)
      34             : {
      35          16 :         size_t bufsize = 1000000, pos = 0;
      36          16 :         char *buf = GDKmalloc(bufsize), *err = NULL;
      37          16 :         node *n;
      38          16 :         sql_schema *s;
      39          16 :         static const char *boolnames[2] = {"false", "true"};
      40             : 
      41          16 :         if (buf == NULL)
      42           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
      43          16 :         s = mvc_bind_schema(sql, "sys");
      44             : 
      45          16 :         pos += snprintf(buf + pos, bufsize - pos,
      46             :                         "delete from sys.dependencies where id < 2000;\n");
      47             : 
      48             :         /* recreate internal types */
      49          16 :         pos += snprintf(buf + pos, bufsize - pos,
      50             :                         "delete from sys.types where id < 2000;\n");
      51         512 :         for (n = types->h; n; n = n->next) {
      52         496 :                 sql_type *t = n->data;
      53             : 
      54         496 :                 if (t->base.id >= FUNC_OIDS)
      55           0 :                         continue;
      56             : 
      57         496 :                 pos += snprintf(buf + pos, bufsize - pos,
      58             :                                 "insert into sys.types values"
      59             :                                 " (%d, '%s', '%s', %u, %u, %d, %d, %d);\n",
      60             :                                 t->base.id, t->impl, t->base.name, t->digits,
      61         496 :                                 t->scale, t->radix, (int) t->eclass,
      62         496 :                                 t->s ? t->s->base.id : s->base.id);
      63             :         }
      64             : 
      65             :         /* recreate internal functions */
      66          16 :         pos += snprintf(buf + pos, bufsize - pos,
      67             :                         "delete from sys.functions where id < 2000;\n"
      68             :                         "delete from sys.args where func_id not in"
      69             :                         " (select id from sys.functions);\n");
      70          16 :         sqlstore *store = sql->session->tr->store;
      71       15088 :         for (n = funcs->h; n; n = n->next) {
      72       15072 :                 sql_func *func = n->data;
      73       15072 :                 int number = 0;
      74       15072 :                 sql_arg *arg;
      75       15072 :                 node *m;
      76             : 
      77       15072 :                 if (func->private || func->base.id >= FUNC_OIDS)
      78        3664 :                         continue;
      79             : 
      80       22816 :                 pos += snprintf(buf + pos, bufsize - pos,
      81             :                                 "insert into sys.functions values"
      82             :                                 " (%d, '%s', '%s', '%s',"
      83             :                                 " %d, %d, %s, %s, %s, %d, %s, %s);\n",
      84             :                                 func->base.id, func->base.name,
      85             :                                 sql_func_imp(func), sql_func_mod(func), (int) FUNC_LANG_INT,
      86       11408 :                                 (int) func->type,
      87       11408 :                                 boolnames[func->side_effect],
      88       11408 :                                 boolnames[func->varres],
      89       11408 :                                 boolnames[func->vararg],
      90       11408 :                                 func->s ? func->s->base.id : s->base.id,
      91       11408 :                                 boolnames[func->system],
      92       11408 :                                 boolnames[func->semantics]);
      93       11408 :                 if (func->res) {
      94       22752 :                         for (m = func->res->h; m; m = m->next, number++) {
      95       11376 :                                 arg = m->data;
      96       11376 :                                 pos += snprintf(buf + pos, bufsize - pos,
      97             :                                                 "insert into sys.args"
      98             :                                                 " values"
      99             :                                                 " (%d, %d, 'res_%d',"
     100             :                                                 " '%s', %u, %u, %d,"
     101             :                                                 " %d);\n",
     102             :                                                 store_next_oid(store),
     103             :                                                 func->base.id,
     104             :                                                 number,
     105       11376 :                                                 arg->type.type->base.name,
     106             :                                                 arg->type.digits,
     107             :                                                 arg->type.scale,
     108       11376 :                                                 arg->inout, number);
     109             :                         }
     110             :                 }
     111       30400 :                 for (m = func->ops->h; m; m = m->next, number++) {
     112       18992 :                         arg = m->data;
     113       18992 :                         if (arg->name)
     114           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
     115             :                                                 "insert into sys.args"
     116             :                                                 " values"
     117             :                                                 " (%d, %d, '%s', '%s',"
     118             :                                                 " %u, %u, %d, %d);\n",
     119             :                                                 store_next_oid(store),
     120             :                                                 func->base.id,
     121             :                                                 arg->name,
     122           0 :                                                 arg->type.type->base.name,
     123             :                                                 arg->type.digits,
     124             :                                                 arg->type.scale,
     125           0 :                                                 arg->inout, number);
     126             :                         else
     127       18992 :                                 pos += snprintf(buf + pos, bufsize - pos,
     128             :                                                 "insert into sys.args"
     129             :                                                 " values"
     130             :                                                 " (%d, %d, 'arg_%d',"
     131             :                                                 " '%s', %u, %u, %d,"
     132             :                                                 " %d);\n",
     133             :                                                 store_next_oid(store),
     134             :                                                 func->base.id,
     135             :                                                 number,
     136       18992 :                                                 arg->type.type->base.name,
     137             :                                                 arg->type.digits,
     138             :                                                 arg->type.scale,
     139       18992 :                                                 arg->inout, number);
     140             :                 }
     141             :         }
     142             : 
     143          16 :         assert(pos < bufsize);
     144          16 :         printf("Running database upgrade commands to update system tables.\n\n");
     145          16 :         fflush(stdout);
     146          16 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     147          16 :         GDKfree(buf);
     148          16 :         return err;             /* usually MAL_SUCCEED */
     149             : }
     150             : 
     151             : static str
     152         109 : check_sys_tables(Client c, mvc *m, sql_schema *s)
     153             : {
     154         109 :         struct {
     155             :                 const char *name;
     156             :                 const char *func;
     157             :                 const char *type;
     158             :                 sql_ftype ftype;
     159         109 :         } tests[] = {
     160             :                 /* tests a few internal functions: the last one created, the
     161             :                  * first one created, and one of the first ones created after
     162             :                  * the geom module */
     163             :                 { "quarter",           "quarter",       "date", F_FUNC, },
     164             :                 { "sys_update_tables", "update_tables", NULL,   F_PROC, },
     165             :                 { "length",            "nitems",        "blob", F_FUNC, },
     166             :                 { "isnull",            "isnil",         "void", F_FUNC, },
     167             :                 {0},
     168             :         };
     169         109 :         char *err;
     170             : 
     171             :         /* if any of the tested function's internal ID does not match the ID
     172             :          * in the sys.functions table, we recreate the internal part of the
     173             :          * system tables */
     174         481 :         for (int i = 0; tests[i].name; i++) {
     175         388 :                 bool needsystabfix = true;
     176         388 :                 sql_subtype tp, *tpp;
     177         388 :                 if (tests[i].type) {
     178         295 :                         sql_find_subtype(&tp, tests[i].type, 0, 0);
     179         295 :                         tpp = &tp;
     180             :                 } else {
     181             :                         tpp = NULL;
     182             :                 }
     183         388 :                 sql_subfunc *f = sql_bind_func(m, s->base.name, tests[i].name, tpp, NULL, tests[i].ftype, true, true);
     184         388 :                 if (f == NULL)
     185          16 :                         throw(SQL, __func__, "cannot find procedure sys.%s(%s)", tests[i].name, tests[i].type ? tests[i].type : "");
     186         388 :                 sqlid id = f->func->base.id;
     187         388 :                 char buf[256];
     188         388 :                 snprintf(buf, sizeof(buf),
     189             :                                  "select id from sys.functions where name = '%s' and func = '%s' and schema_id = 2000;\n",
     190             :                                  tests[i].name, tests[i].func);
     191         388 :                 res_table *output = NULL;
     192         388 :                 err = SQLstatementIntern(c, buf, "update", true, false, &output);
     193         388 :                 if (err)
     194           0 :                         return err;
     195         388 :                 BAT *b;
     196         388 :                 b = BATdescriptor(output->cols[0].b);
     197         388 :                 res_table_destroy(output);
     198         388 :                 if (b == NULL)
     199           0 :                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     200         388 :                 if (BATcount(b) > 0) {
     201         388 :                         BATiter bi = bat_iterator(b);
     202         388 :                         needsystabfix = * (int *) BUNtloc(bi, 0) != id;
     203         388 :                         bat_iterator_end(&bi);
     204             :                 }
     205         388 :                 BBPunfix(b->batCacheid);
     206         388 :                 if (i == 0 && !needsystabfix) {
     207          93 :                         snprintf(buf, sizeof(buf),
     208             :                                          "select a.type from sys.functions f join sys.args a on f.id = a.func_id where f.name = 'quarter' and f.schema_id = 2000 and a.inout = 0 and a.type = 'int';\n");
     209          93 :                         err = SQLstatementIntern(c, buf, "update", true, false, &output);
     210          93 :                         if (err)
     211           0 :                                 return err;
     212          93 :                         b = BATdescriptor(output->cols[0].b);
     213          93 :                         res_table_destroy(output);
     214          93 :                         if (b == NULL)
     215           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     216          93 :                         needsystabfix = BATcount(b) > 0;
     217          93 :                         BBPunfix(b->batCacheid);
     218             :                 }
     219         388 :                 if (needsystabfix)
     220          16 :                         return sql_fix_system_tables(c, m);
     221             :         }
     222             :         return NULL;
     223             : }
     224             : 
     225             : #ifdef HAVE_HGE
     226             : static str
     227           8 : sql_update_hugeint(Client c, mvc *sql)
     228             : {
     229           8 :         size_t bufsize = 8192, pos = 0;
     230           8 :         char *buf, *err;
     231             : 
     232           8 :         (void) sql;
     233           8 :         if ((buf = GDKmalloc(bufsize)) == NULL)
     234           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     235             : 
     236             :         /* 90_generator_hge.sql */
     237           8 :         pos += snprintf(buf + pos, bufsize - pos,
     238             :                         "create function sys.generate_series(first hugeint, \"limit\" hugeint)\n"
     239             :                         "returns table (value hugeint)\n"
     240             :                         "external name generator.series;\n"
     241             :                         "create function sys.generate_series(first hugeint, \"limit\" hugeint, stepsize hugeint)\n"
     242             :                         "returns table (value hugeint)\n"
     243             :                         "external name generator.series;\n");
     244             : 
     245             :         /* 39_analytics_hge.sql */
     246           8 :         pos += snprintf(buf + pos, bufsize - pos,
     247             :                         "create aggregate stddev_samp(val HUGEINT) returns DOUBLE\n"
     248             :                         " external name \"aggr\".\"stdev\";\n"
     249             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(HUGEINT) TO PUBLIC;\n"
     250             :                         "create window stddev_samp(val HUGEINT) returns DOUBLE\n"
     251             :                         " external name \"sql\".\"stdev\";\n"
     252             :                         "GRANT EXECUTE ON WINDOW stddev_samp(HUGEINT) TO PUBLIC;\n"
     253             :                         "create aggregate stddev_pop(val HUGEINT) returns DOUBLE\n"
     254             :                         " external name \"aggr\".\"stdevp\";\n"
     255             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(HUGEINT) TO PUBLIC;\n"
     256             :                         "create window stddev_pop(val HUGEINT) returns DOUBLE\n"
     257             :                         " external name \"sql\".\"stdevp\";\n"
     258             :                         "GRANT EXECUTE ON WINDOW stddev_pop(HUGEINT) TO PUBLIC;\n"
     259             :                         "create aggregate var_samp(val HUGEINT) returns DOUBLE\n"
     260             :                         " external name \"aggr\".\"variance\";\n"
     261             :                         "GRANT EXECUTE ON AGGREGATE var_samp(HUGEINT) TO PUBLIC;\n"
     262             :                         "create window var_samp(val HUGEINT) returns DOUBLE\n"
     263             :                         " external name \"sql\".\"variance\";\n"
     264             :                         "GRANT EXECUTE ON WINDOW var_samp(HUGEINT) TO PUBLIC;\n"
     265             :                         "create aggregate covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     266             :                         " external name \"aggr\".\"covariance\";\n"
     267             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
     268             :                         "create window covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     269             :                         " external name \"sql\".\"covariance\";\n"
     270             :                         "GRANT EXECUTE ON WINDOW covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
     271             :                         "create aggregate var_pop(val HUGEINT) returns DOUBLE\n"
     272             :                         " external name \"aggr\".\"variancep\";\n"
     273             :                         "GRANT EXECUTE ON AGGREGATE var_pop(HUGEINT) TO PUBLIC;\n"
     274             :                         "create window var_pop(val HUGEINT) returns DOUBLE\n"
     275             :                         " external name \"sql\".\"variancep\";\n"
     276             :                         "GRANT EXECUTE ON WINDOW var_pop(HUGEINT) TO PUBLIC;\n"
     277             :                         "create aggregate covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     278             :                         " external name \"aggr\".\"covariancep\";\n"
     279             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
     280             :                         "create window covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     281             :                         " external name \"sql\".\"covariancep\";\n"
     282             :                         "GRANT EXECUTE ON WINDOW covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
     283             :                         "create aggregate median(val HUGEINT) returns HUGEINT\n"
     284             :                         " external name \"aggr\".\"median\";\n"
     285             :                         "GRANT EXECUTE ON AGGREGATE median(HUGEINT) TO PUBLIC;\n"
     286             :                         "create aggregate quantile(val HUGEINT, q DOUBLE) returns HUGEINT\n"
     287             :                         " external name \"aggr\".\"quantile\";\n"
     288             :                         "GRANT EXECUTE ON AGGREGATE quantile(HUGEINT, DOUBLE) TO PUBLIC;\n"
     289             :                         "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
     290             :                         " external name \"aggr\".\"median_avg\";\n"
     291             :                         "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
     292             :                         "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
     293             :                         " external name \"aggr\".\"quantile_avg\";\n"
     294             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n"
     295             :                         "create aggregate corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     296             :                         " external name \"aggr\".\"corr\";\n"
     297             :                         "GRANT EXECUTE ON AGGREGATE corr(HUGEINT, HUGEINT) TO PUBLIC;\n"
     298             :                         "create window corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     299             :                         " external name \"sql\".\"corr\";\n"
     300             :                         "GRANT EXECUTE ON WINDOW corr(HUGEINT, HUGEINT) TO PUBLIC;\n");
     301             : 
     302             :         /* 40_json_hge.sql */
     303           8 :         pos += snprintf(buf + pos, bufsize - pos,
     304             :                         "create function json.filter(js json, name hugeint)\n"
     305             :                         "returns json external name json.filter;\n"
     306             :                         "GRANT EXECUTE ON FUNCTION json.filter(json, hugeint) TO PUBLIC;\n");
     307             : 
     308           8 :         pos += snprintf(buf + pos, bufsize - pos,
     309             :                         "update sys.functions set system = true where system <> true and name in ('generate_series') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n"
     310             :                         "update sys.functions set system = true where system <> true and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'covar_samp', 'var_pop', 'covar_pop', 'median', 'median_avg', 'quantile', 'quantile_avg', 'corr') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n"
     311             :                         "update sys.functions set system = true where system <> true and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'covar_samp', 'var_pop', 'covar_pop', 'corr') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n"
     312             :                         "update sys.functions set system = true where system <> true and name = 'filter' and schema_id = (select id from sys.schemas where name = 'json') and type = %d;\n",
     313             :                         (int) F_UNION, (int) F_AGGR, (int) F_ANALYTIC, (int) F_FUNC);
     314             : 
     315           8 :         assert(pos < bufsize);
     316             : 
     317           8 :         printf("Running database upgrade commands:\n%s\n", buf);
     318           8 :         fflush(stdout);
     319           8 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     320           8 :         GDKfree(buf);
     321           8 :         return err;             /* usually MAL_SUCCEED */
     322             : }
     323             : #endif
     324             : 
     325             : #ifdef HAVE_SHP
     326             : static str
     327           8 : sql_create_shp(Client c)
     328             : {
     329             :         //Create the new SHPload procedures
     330           8 :         const char *query = "create procedure SHPLoad(fname string, schemaname string, tablename string) external name shp.load;\n"
     331             :                 "create procedure SHPLoad(fname string, tablename string) external name shp.load;\n"
     332             :                 "update sys.functions set system = true where schema_id = 2000 and name in ('shpload');";
     333           8 :         printf("Running database upgrade commands:\n%s\n", query);
     334           8 :         fflush(stdout);
     335           8 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
     336             : }
     337             : #endif
     338             : 
     339             : static str
     340           8 : sql_drop_shp(Client c)
     341             : {
     342             :         //Drop the old SHP procedures (upgrade from version before shpload upgrade)
     343           8 :         const char *query = "drop procedure if exists SHPattach(string) cascade;\n"
     344             :                 "drop procedure if exists SHPload(integer) cascade;\n"
     345             :                 "drop procedure if exists SHPload(integer, geometry) cascade;\n";
     346           8 :         printf("Running database upgrade commands:\n%s\n", query);
     347           8 :         fflush(stdout);
     348           8 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
     349             : }
     350             : 
     351             : static str
     352         109 : sql_update_generator(Client c)
     353             : {
     354         109 :         const char *query = "update sys.args set name = 'limit' where name = 'last' and func_id in (select id from sys.functions where schema_id = 2000 and name = 'generate_series' and func like '% last %');\n"
     355             :                 "update sys.functions set func = replace(func, ' last ', ' \"limit\" ') where schema_id = 2000 and name = 'generate_series' and func like '% last %';\n";
     356         109 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
     357             : }
     358             : 
     359             : static str
     360           0 : sql_drop_functions_dependencies_Xs_on_Ys(Client c)
     361             : {
     362           0 :         size_t bufsize = 1600, pos = 0;
     363           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     364             : 
     365           0 :         if (buf == NULL)
     366           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     367             : 
     368             :         /* remove functions which were created in sql/scripts/21_dependency_functions.sql */
     369           0 :         pos += snprintf(buf + pos, bufsize - pos,
     370             :                         "DROP FUNCTION dependencies_schemas_on_users() CASCADE;\n"
     371             :                         "DROP FUNCTION dependencies_owners_on_schemas() CASCADE;\n"
     372             :                         "DROP FUNCTION dependencies_tables_on_views() CASCADE;\n"
     373             :                         "DROP FUNCTION dependencies_tables_on_indexes() CASCADE;\n"
     374             :                         "DROP FUNCTION dependencies_tables_on_triggers() CASCADE;\n"
     375             :                         "DROP FUNCTION dependencies_tables_on_foreignKeys() CASCADE;\n"
     376             :                         "DROP FUNCTION dependencies_tables_on_functions() CASCADE;\n"
     377             :                         "DROP FUNCTION dependencies_columns_on_views() CASCADE;\n"
     378             :                         "DROP FUNCTION dependencies_columns_on_keys() CASCADE;\n"
     379             :                         "DROP FUNCTION dependencies_columns_on_indexes() CASCADE;\n"
     380             :                         "DROP FUNCTION dependencies_columns_on_functions() CASCADE;\n"
     381             :                         "DROP FUNCTION dependencies_columns_on_triggers() CASCADE;\n"
     382             :                         "DROP FUNCTION dependencies_views_on_functions() CASCADE;\n"
     383             :                         "DROP FUNCTION dependencies_views_on_triggers() CASCADE;\n"
     384             :                         "DROP FUNCTION dependencies_functions_on_functions() CASCADE;\n"
     385             :                         "DROP FUNCTION dependencies_functions_on_triggers() CASCADE;\n"
     386             :                         "DROP FUNCTION dependencies_keys_on_foreignKeys() CASCADE;\n");
     387             : 
     388           0 :         assert(pos < bufsize);
     389             : 
     390           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     391           0 :         fflush(stdout);
     392           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     393           0 :         GDKfree(buf);
     394           0 :         return err;             /* usually MAL_SUCCEED */
     395             : }
     396             : 
     397             : static str
     398           0 : sql_update_storagemodel(Client c, mvc *sql, bool oct2020_upgrade)
     399             : {
     400           0 :         size_t bufsize = 20000, pos = 0;
     401           0 :         char *buf, *err;
     402           0 :         sql_schema *s = mvc_bind_schema(sql, "sys");
     403           0 :         sql_table *t;
     404           0 :         char *day_interval_str = oct2020_upgrade ? " 'day_interval'," : "";
     405             : 
     406           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
     407           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     408             : 
     409             :         /* set views and tables internally to non-system to allow drop commands to succeed without error */
     410           0 :         if ((t = mvc_bind_table(sql, s, "storage")) != NULL)
     411           0 :                 t->system = 0;
     412           0 :         if ((t = mvc_bind_table(sql, s, "storagemodel")) != NULL)
     413           0 :                 t->system = 0;
     414           0 :         if ((t = mvc_bind_table(sql, s, "storagemodelinput")) != NULL)
     415           0 :                 t->system = 0;
     416           0 :         if ((t = mvc_bind_table(sql, s, "tablestoragemodel")) != NULL)
     417           0 :                 t->system = 0;
     418             : 
     419             :         /* new 75_storagemodel.sql */
     420           0 :         pos += snprintf(buf + pos, bufsize - pos,
     421             :                 /* drop objects in reverse order of original creation of old 75_storagemodel.sql */
     422             :                 "drop view if exists sys.tablestoragemodel cascade;\n"
     423             :                 "drop view if exists sys.storagemodel cascade;\n"
     424             :                 "drop function if exists sys.storagemodel() cascade;\n");
     425             : 
     426           0 :         if (oct2020_upgrade) {
     427           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     428             :                         "drop function if exists sys.imprintsize(varchar(1024), bigint) cascade;\n");
     429             :         } else {
     430           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     431             :                         "drop function if exists sys.imprintsize(bigint, clob) cascade;\n");
     432             :         }
     433             : 
     434           0 :         pos += snprintf(buf + pos, bufsize - pos,
     435             :                 "drop function if exists sys.hashsize(boolean, bigint) cascade;\n");
     436             : 
     437           0 :         if (oct2020_upgrade) {
     438           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     439             :                         "drop function if exists sys.columnsize(varchar(1024), bigint) cascade;\n"
     440             :                         "drop function if exists sys.heapsize(varchar(1024), bigint, bigint, int) cascade;\n");
     441             :         } else {
     442           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     443             :                         "drop function if exists sys.columnsize(clob, bigint, bigint) cascade;\n"
     444             :                         "drop function if exists sys.heapsize(clob, bigint, int) cascade;\n");
     445             :         }
     446             : 
     447           0 :         pos += snprintf(buf + pos, bufsize - pos,
     448             :                 "drop procedure if exists sys.storagemodelinit() cascade;\n"
     449             :                 "drop table if exists sys.storagemodelinput cascade;\n"
     450             :                 "drop view if exists sys.\"storage\" cascade;\n");
     451             : 
     452           0 :         if (oct2020_upgrade) {
     453           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     454             :                         "drop function if exists sys.\"storage\"(varchar(1024), varchar(1024), varchar(1024)) cascade;\n"
     455             :                         "drop function if exists sys.\"storage\"(varchar(1024), varchar(1024)) cascade;\n"
     456             :                         "drop function if exists sys.\"storage\"(varchar(1024)) cascade;\n");
     457             :         } else {
     458           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     459             :                         "drop function if exists sys.\"storage\"(clob, clob, clob) cascade;\n"
     460             :                         "drop function if exists sys.\"storage\"(clob, clob) cascade;\n"
     461             :                         "drop function if exists sys.\"storage\"(clob) cascade;\n");
     462             :         }
     463             : 
     464             :         /* new 75_storagemodel.sql */
     465           0 :         pos += snprintf(buf + pos, bufsize - pos,
     466             :                 "drop function if exists sys.\"storage\"() cascade;\n"
     467             :                 "create function sys.\"storage\"()\n"
     468             :                 "returns table (\n"
     469             :                 "  \"schema\" varchar(1024),\n"
     470             :                 "  \"table\" varchar(1024),\n"
     471             :                 "  \"column\" varchar(1024),\n"
     472             :                 "  \"type\" varchar(1024),\n"
     473             :                 "  \"mode\" varchar(15),\n"
     474             :                 "  location varchar(1024),\n"
     475             :                 "  \"count\" bigint,\n"
     476             :                 "  typewidth int,\n"
     477             :                 "  columnsize bigint,\n"
     478             :                 "  heapsize bigint,\n"
     479             :                 "  hashes bigint,\n"
     480             :                 "  phash boolean,\n"
     481             :                 "  \"imprints\" bigint,\n"
     482             :                 "  sorted boolean,\n"
     483             :                 "  revsorted boolean,\n"
     484             :                 "  \"unique\" boolean,\n"
     485             :                 "  orderidx bigint\n"
     486             :                 ")\n"
     487             :                 "external name sql.\"storage\";\n"
     488             :                 "create view sys.\"storage\" as\n"
     489             :                 "select * from sys.\"storage\"()\n"
     490             :                 " where (\"schema\", \"table\") in (\n"
     491             :                 "  SELECT sch.\"name\", tbl.\"name\"\n"
     492             :                 "    FROM sys.\"tables\" AS tbl JOIN sys.\"schemas\" AS sch ON tbl.schema_id = sch.id\n"
     493             :                 "   WHERE tbl.\"system\" = FALSE)\n"
     494             :                 "order by \"schema\", \"table\", \"column\";\n"
     495             :                 "create view sys.\"tablestorage\" as\n"
     496             :                 "select \"schema\", \"table\",\n"
     497             :                 "  max(\"count\") as \"rowcount\",\n"
     498             :                 "  count(*) as \"storages\",\n"
     499             :                 "  sum(columnsize) as columnsize,\n"
     500             :                 "  sum(heapsize) as heapsize,\n"
     501             :                 "  sum(hashes) as hashsize,\n"
     502             :                 "  sum(\"imprints\") as imprintsize,\n"
     503             :                 "  sum(orderidx) as orderidxsize\n"
     504             :                 " from sys.\"storage\"\n"
     505             :                 "group by \"schema\", \"table\"\n"
     506             :                 "order by \"schema\", \"table\";\n"
     507             :                 "create view sys.\"schemastorage\" as\n"
     508             :                 "select \"schema\",\n"
     509             :                 "  count(*) as \"storages\",\n"
     510             :                 "  sum(columnsize) as columnsize,\n"
     511             :                 "  sum(heapsize) as heapsize,\n"
     512             :                 "  sum(hashes) as hashsize,\n"
     513             :                 "  sum(\"imprints\") as imprintsize,\n"
     514             :                 "  sum(orderidx) as orderidxsize\n"
     515             :                 " from sys.\"storage\"\n"
     516             :                 "group by \"schema\"\n"
     517             :                 "order by \"schema\";\n"
     518             :                 "create function sys.\"storage\"(sname varchar(1024))\n"
     519             :                 "returns table (\n"
     520             :                 "  \"schema\" varchar(1024),\n"
     521             :                 "  \"table\" varchar(1024),\n"
     522             :                 "  \"column\" varchar(1024),\n"
     523             :                 "  \"type\" varchar(1024),\n"
     524             :                 "  \"mode\" varchar(15),\n"
     525             :                 "  location varchar(1024),\n"
     526             :                 "  \"count\" bigint,\n"
     527             :                 "  typewidth int,\n"
     528             :                 "  columnsize bigint,\n"
     529             :                 "  heapsize bigint,\n"
     530             :                 "  hashes bigint,\n"
     531             :                 "  phash boolean,\n"
     532             :                 "  \"imprints\" bigint,\n"
     533             :                 "  sorted boolean,\n"
     534             :                 "  revsorted boolean,\n"
     535             :                 "  \"unique\" boolean,\n"
     536             :                 "  orderidx bigint\n"
     537             :                 ")\n"
     538             :                 "external name sql.\"storage\";\n"
     539             :                 "create function sys.\"storage\"(sname varchar(1024), tname varchar(1024))\n"
     540             :                 "returns table (\n"
     541             :                 "  \"schema\" varchar(1024),\n"
     542             :                 "  \"table\" varchar(1024),\n"
     543             :                 "  \"column\" varchar(1024),\n"
     544             :                 "  \"type\" varchar(1024),\n"
     545             :                 "  \"mode\" varchar(15),\n"
     546             :                 "  location varchar(1024),\n"
     547             :                 "  \"count\" bigint,\n"
     548             :                 "  typewidth int,\n"
     549             :                 "  columnsize bigint,\n"
     550             :                 "  heapsize bigint,\n"
     551             :                 "  hashes bigint,\n"
     552             :                 "  phash boolean,\n"
     553             :                 "  \"imprints\" bigint,\n"
     554             :                 "  sorted boolean,\n"
     555             :                 "  revsorted boolean,\n"
     556             :                 "  \"unique\" boolean,\n"
     557             :                 "  orderidx bigint\n"
     558             :                 ")\n"
     559             :                 "external name sql.\"storage\";\n"
     560             :                 "create function sys.\"storage\"(sname varchar(1024), tname varchar(1024), cname varchar(1024))\n"
     561             :                 "returns table (\n"
     562             :                 "  \"schema\" varchar(1024),\n"
     563             :                 "  \"table\" varchar(1024),\n"
     564             :                 "  \"column\" varchar(1024),\n"
     565             :                 "  \"type\" varchar(1024),\n"
     566             :                 "  \"mode\" varchar(15),\n"
     567             :                 "  location varchar(1024),\n"
     568             :                 "  \"count\" bigint,\n"
     569             :                 "  typewidth int,\n"
     570             :                 "  columnsize bigint,\n"
     571             :                 "  heapsize bigint,\n"
     572             :                 "  hashes bigint,\n"
     573             :                 "  phash boolean,\n"
     574             :                 "  \"imprints\" bigint,\n"
     575             :                 "  sorted boolean,\n"
     576             :                 "  revsorted boolean,\n"
     577             :                 "  \"unique\" boolean,\n"
     578             :                 "  orderidx bigint\n"
     579             :                 ")\n"
     580             :                 "external name sql.\"storage\";\n"
     581             :                 "create table sys.storagemodelinput(\n"
     582             :                 "  \"schema\" varchar(1024) NOT NULL,\n"
     583             :                 "  \"table\" varchar(1024) NOT NULL,\n"
     584             :                 "  \"column\" varchar(1024) NOT NULL,\n"
     585             :                 "  \"type\" varchar(1024) NOT NULL,\n"
     586             :                 "  typewidth int NOT NULL,\n"
     587             :                 "  \"count\" bigint NOT NULL,\n"
     588             :                 "  \"distinct\" bigint NOT NULL,\n"
     589             :                 "  atomwidth int NOT NULL,\n"
     590             :                 "  reference boolean NOT NULL DEFAULT FALSE,\n"
     591             :                 "  sorted boolean,\n"
     592             :                 "  \"unique\" boolean,\n"
     593             :                 "  isacolumn boolean NOT NULL DEFAULT TRUE\n"
     594             :                 ");\n"
     595             :                 "create procedure sys.storagemodelinit()\n"
     596             :                 "begin\n"
     597             :                 "  delete from sys.storagemodelinput;\n"
     598             :                 "  insert into sys.storagemodelinput\n"
     599             :                 "  select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
     600             :                 "          case when (\"unique\" or \"type\" IN ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
     601             :                 "                  then \"count\" else 0 end,\n"
     602             :                 "          case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
     603             :                 "                  then cast((heapsize - 8192) / \"count\" as bigint)\n"
     604             :                 "          when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
     605             :                 "                  then cast((heapsize - 32) / \"count\" as bigint)\n"
     606             :                 "          else typewidth end,\n"
     607             :                 "          FALSE, case sorted when true then true else false end, \"unique\", TRUE\n"
     608             :                 "    from sys.\"storage\";\n"
     609             :                 "  update sys.storagemodelinput\n"
     610             :                 "     set reference = TRUE\n"
     611             :                 "   where (\"schema\", \"table\", \"column\") in (\n"
     612             :                 "          SELECT fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
     613             :                 "            FROM  sys.\"keys\" AS fkkey,\n"
     614             :                 "                  sys.\"objects\" AS fkkeycol,\n"
     615             :                 "                  sys.\"tables\" AS fktable,\n"
     616             :                 "                  sys.\"schemas\" AS fkschema\n"
     617             :                 "          WHERE fktable.\"id\" = fkkey.\"table_id\"\n"
     618             :                 "            AND fkkey.\"id\" = fkkeycol.\"id\"\n"
     619             :                 "            AND fkschema.\"id\" = fktable.\"schema_id\"\n"
     620             :                 "            AND fkkey.\"rkey\" > -1 );\n"
     621             :                 "  update sys.storagemodelinput\n"
     622             :                 "     set isacolumn = FALSE\n"
     623             :                 "   where (\"schema\", \"table\", \"column\") NOT in (\n"
     624             :                 "          SELECT sch.\"name\", tbl.\"name\", col.\"name\"\n"
     625             :                 "            FROM sys.\"schemas\" AS sch,\n"
     626             :                 "                  sys.\"tables\" AS tbl,\n"
     627             :                 "                  sys.\"columns\" AS col\n"
     628             :                 "          WHERE sch.\"id\" = tbl.\"schema_id\"\n"
     629             :                 "            AND tbl.\"id\" = col.\"table_id\");\n"
     630             :                 "end;\n"
     631             :                 "create function sys.columnsize(tpe varchar(1024), count bigint)\n"
     632             :                 "returns bigint\n"
     633             :                 "begin\n"
     634             :                 "  if tpe in ('tinyint', 'boolean')\n"
     635             :                 "          then return count;\n"
     636             :                 "  end if;\n"
     637             :                 "  if tpe = 'smallint'\n"
     638             :                 "          then return 2 * count;\n"
     639             :                 "  end if;\n"
     640             :                 "  if tpe in ('int', 'real', 'date', 'time', 'timetz', 'sec_interval',%s 'month_interval')\n"
     641             :                 "          then return 4 * count;\n"
     642             :                 "  end if;\n"
     643             :                 "  if tpe in ('bigint', 'double', 'timestamp', 'timestamptz', 'inet', 'oid')\n"
     644             :                 "          then return 8 * count;\n"
     645             :                 "  end if;\n"
     646             :                 "  if tpe in ('hugeint', 'decimal', 'uuid', 'mbr')\n"
     647             :                 "          then return 16 * count;\n"
     648             :                 "  end if;\n"
     649             :                 "  if tpe in ('varchar', 'char', 'clob', 'json', 'url')\n"
     650             :                 "          then return 4 * count;\n"
     651             :                 "  end if;\n"
     652             :                 "  if tpe in ('blob', 'geometry', 'geometrya')\n"
     653             :                 "          then return 8 * count;\n"
     654             :                 "  end if;\n"
     655             :                 "  return 8 * count;\n"
     656             :                 "end;\n"
     657             :                 "create function sys.heapsize(tpe varchar(1024), count bigint, distincts bigint, avgwidth int)\n"
     658             :                 "returns bigint\n"
     659             :                 "begin\n"
     660             :                 "  if tpe in ('varchar', 'char', 'clob', 'json', 'url')\n"
     661             :                 "          then return 8192 + ((avgwidth + 8) * distincts);\n"
     662             :                 "  end if;\n"
     663             :                 "  if tpe in ('blob', 'geometry', 'geometrya')\n"
     664             :                 "          then return 32 + (avgwidth * count);\n"
     665             :                 "  end if;\n"
     666             :                 "  return 0;\n"
     667             :                 "end;\n"
     668             :                 "create function sys.hashsize(b boolean, count bigint)\n"
     669             :                 "returns bigint\n"
     670             :                 "begin\n"
     671             :                 "  if b = true\n"
     672             :                 "          then return 8 * count;\n"
     673             :                 "  end if;\n"
     674             :                 "  return 0;\n"
     675             :                 "end;\n"
     676             :                 "create function sys.imprintsize(tpe varchar(1024), count bigint)\n"
     677             :                 "returns bigint\n"
     678             :                 "begin\n"
     679             :                 "  if tpe in ('tinyint', 'boolean')\n"
     680             :                 "          then return cast(0.2 * count as bigint);\n"
     681             :                 "  end if;\n"
     682             :                 "  if tpe = 'smallint'\n"
     683             :                 "          then return cast(0.4 * count as bigint);\n"
     684             :                 "  end if;\n"
     685             :                 "  if tpe in ('int', 'real', 'date', 'time', 'timetz', 'sec_interval',%s 'month_interval')\n"
     686             :                 "          then return cast(0.8 * count as bigint);\n"
     687             :                 "  end if;\n"
     688             :                 "  if tpe in ('bigint', 'double', 'timestamp', 'timestamptz', 'inet', 'oid')\n"
     689             :                 "          then return cast(1.6 * count as bigint);\n"
     690             :                 "  end if;\n"
     691             :                 "  if tpe in ('hugeint', 'decimal', 'uuid', 'mbr')\n"
     692             :                 "          then return cast(3.2 * count as bigint);\n"
     693             :                 "  end if;\n"
     694             :                 "  return 0;\n"
     695             :                 "end;\n"
     696             :                 "create view sys.storagemodel as\n"
     697             :                 "select \"schema\", \"table\", \"column\", \"type\", \"count\",\n"
     698             :                 "  sys.columnsize(\"type\", \"count\") as columnsize,\n"
     699             :                 "  sys.heapsize(\"type\", \"count\", \"distinct\", \"atomwidth\") as heapsize,\n"
     700             :                 "  sys.hashsize(\"reference\", \"count\") as hashsize,\n"
     701             :                 "  case when isacolumn then sys.imprintsize(\"type\", \"count\") else 0 end as imprintsize,\n"
     702             :                 "  case when (isacolumn and not sorted) then cast(8 * \"count\" as bigint) else 0 end as orderidxsize,\n"
     703             :                 "  sorted, \"unique\", isacolumn\n"
     704             :                 " from sys.storagemodelinput\n"
     705             :                 "order by \"schema\", \"table\", \"column\";\n"
     706             :                 "create view sys.tablestoragemodel as\n"
     707             :                 "select \"schema\", \"table\",\n"
     708             :                 "  max(\"count\") as \"rowcount\",\n"
     709             :                 "  count(*) as \"storages\",\n"
     710             :                 "  sum(sys.columnsize(\"type\", \"count\")) as columnsize,\n"
     711             :                 "  sum(sys.heapsize(\"type\", \"count\", \"distinct\", \"atomwidth\")) as heapsize,\n"
     712             :                 "  sum(sys.hashsize(\"reference\", \"count\")) as hashsize,\n"
     713             :                 "  sum(case when isacolumn then sys.imprintsize(\"type\", \"count\") else 0 end) as imprintsize,\n"
     714             :                 "  sum(case when (isacolumn and not sorted) then cast(8 * \"count\" as bigint) else 0 end) as orderidxsize\n"
     715             :                 " from sys.storagemodelinput\n"
     716             :                 "group by \"schema\", \"table\"\n"
     717             :                 "order by \"schema\", \"table\";\n", day_interval_str, day_interval_str);
     718           0 :         assert(pos < bufsize);
     719             : 
     720           0 :         pos += snprintf(buf + pos, bufsize - pos,
     721             :                 "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
     722             :                 " and name in ('storage', 'tablestorage', 'schemastorage', 'storagemodelinput', 'storagemodel', 'tablestoragemodel');\n");
     723           0 :         pos += snprintf(buf + pos, bufsize - pos,
     724             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     725             :                 " and name in ('storage') and type = %d;\n", (int) F_UNION);
     726           0 :         pos += snprintf(buf + pos, bufsize - pos,
     727             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     728             :                 " and name in ('storagemodelinit') and type = %d;\n", (int) F_PROC);
     729           0 :         pos += snprintf(buf + pos, bufsize - pos,
     730             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     731             :                 " and name in ('columnsize', 'heapsize', 'hashsize', 'imprintsize') and type = %d;\n", (int) F_FUNC);
     732             : 
     733           0 :         assert(pos < bufsize);
     734             : 
     735           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     736           0 :         fflush(stdout);
     737           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     738           0 :         GDKfree(buf);
     739           0 :         return err;             /* usually MAL_SUCCEED */
     740             : }
     741             : 
     742             : #define FLUSH_INSERTS_IF_BUFFERFILLED                                                                   \
     743             :         do {                                                                                                                            \
     744             :                 /* Each new value should add about 20 bytes to the buffer, */   \
     745             :                 /* "flush" when is 200 bytes from being full */                                       \
     746             :                 if (pos > 7900) {                                                                                            \
     747             :                         pos += snprintf(buf + pos, bufsize - pos,                                       \
     748             :                                                         ") as t1(c1,c2,c3) where t1.c1 not in (select \"id\" from sys.dependencies where depend_id = t1.c2);\n"); \
     749             :                         assert(pos < bufsize);                                                                               \
     750             :                         printf("Running database upgrade commands:\n%s\n", buf);      \
     751             :                         fflush(stdout);                                                                                         \
     752             :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL); \
     753             :                         if (err)                                                                                                        \
     754             :                                 goto bailout;                                                                                   \
     755             :                         pos = 0;                                                                                                        \
     756             :                         pos += snprintf(buf + pos, bufsize - pos, "insert into sys.dependencies select c1, c2, c3 from (values"); \
     757             :                         ppos = pos;                                                                                                     \
     758             :                         first = true;                                                                                           \
     759             :                 }                                                                                                                               \
     760             :         } while (0)
     761             : 
     762             : static str
     763           0 : sql_update_nov2019_missing_dependencies(Client c, mvc *sql)
     764             : {
     765           0 :         size_t bufsize = 8192, pos = 0, ppos;
     766           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     767           0 :         allocator *old_sa = sql->sa;
     768           0 :         bool first = true;
     769           0 :         sql_trans *tr = sql->session->tr;
     770           0 :         struct os_iter si;
     771             : 
     772           0 :         if (buf == NULL)
     773           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     774             : 
     775           0 :         if (!(sql->sa = sa_create(sql->pa))) {
     776           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     777           0 :                 goto bailout;
     778             :         }
     779             : 
     780           0 :         pos += snprintf(buf + pos, bufsize - pos, "insert into sys.dependencies select c1, c2, c3 from (values");
     781           0 :         ppos = pos; /* later check if found updatable database objects */
     782             : 
     783           0 :         os_iterator(&si, sql->session->tr->cat->schemas, sql->session->tr, NULL);
     784           0 :         for (sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
     785           0 :                 sql_schema *s = (sql_schema*)b;
     786             : 
     787           0 :                 struct os_iter oi;
     788           0 :                 os_iterator(&oi, s->funcs, sql->session->tr, NULL);
     789           0 :                 for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
     790           0 :                         sql_func *f = (sql_func*)b;
     791             : 
     792           0 :                         if (f->query && f->lang == FUNC_LANG_SQL) {
     793           0 :                                 char *relt;
     794           0 :                                 sql_rel *r = NULL;
     795             : 
     796           0 :                                 if (!(relt = sa_strdup(sql->sa, f->query))) {
     797           0 :                                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     798           0 :                                         goto bailout;
     799             :                                 }
     800             : 
     801           0 :                                 r = rel_parse(sql, s, relt, m_deps);
     802           0 :                                 if (r)
     803           0 :                                         r = sql_processrelation(sql, r, 0, 0, 0, 0);
     804           0 :                                 if (r) {
     805           0 :                                         list *id_l = rel_dependencies(sql, r);
     806             : 
     807           0 :                                         for (node *o = id_l->h ; o ; o = o->next) {
     808           0 :                                                 sqlid next = ((sql_base*) o->data)->id;
     809           0 :                                                 if (next != f->base.id) {
     810           0 :                                                         pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",", next,
     811           0 :                                                                                         f->base.id, (int)(!IS_PROC(f) ? FUNC_DEPENDENCY : PROC_DEPENDENCY));
     812           0 :                                                         first = false;
     813           0 :                                                         FLUSH_INSERTS_IF_BUFFERFILLED;
     814             :                                                 }
     815             :                                         }
     816           0 :                                 } else if (sql->session->status == -1) {
     817           0 :                                         sql->session->status = 0;
     818           0 :                                         sql->errstr[0] = 0;
     819             :                                 }
     820             :                         }
     821             :                 }
     822           0 :                 if (s->tables) {
     823           0 :                         struct os_iter oi;
     824           0 :                         os_iterator(&oi, s->tables, tr, NULL);
     825           0 :                         for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
     826           0 :                                 sql_table *t = (sql_table*) b;
     827             : 
     828           0 :                                 if (t->query && isView(t)) {
     829           0 :                                         char *relt;
     830           0 :                                         sql_rel *r = NULL;
     831             : 
     832           0 :                                         if (!(relt = sa_strdup(sql->sa, t->query))) {
     833           0 :                                                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     834           0 :                                                 goto bailout;
     835             :                                         }
     836             : 
     837           0 :                                         r = rel_parse(sql, s, relt, m_deps);
     838           0 :                                         if (r)
     839           0 :                                                 r = sql_processrelation(sql, r, 0, 0, 0, 0);
     840           0 :                                         if (r) {
     841           0 :                                                 list *id_l = rel_dependencies(sql, r);
     842             : 
     843           0 :                                                 for (node *o = id_l->h ; o ; o = o->next) {
     844           0 :                                                         sqlid next = ((sql_base*) o->data)->id;
     845           0 :                                                         if (next != t->base.id) {
     846           0 :                                                                 pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",",
     847             :                                                                                                 next, t->base.id, (int) VIEW_DEPENDENCY);
     848           0 :                                                                 first = false;
     849           0 :                                                                 FLUSH_INSERTS_IF_BUFFERFILLED;
     850             :                                                         }
     851             :                                                 }
     852             :                                         }
     853             :                                 }
     854           0 :                                 if (t->triggers)
     855           0 :                                         for (node *mm = ol_first_node(t->triggers); mm; mm = mm->next) {
     856           0 :                                                 sql_trigger *tr = (sql_trigger*) mm->data;
     857           0 :                                                 char *relt;
     858           0 :                                                 sql_rel *r = NULL;
     859             : 
     860           0 :                                                 if (!(relt = sa_strdup(sql->sa, tr->statement))) {
     861           0 :                                                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     862           0 :                                                         goto bailout;
     863             :                                                 }
     864             : 
     865           0 :                                                 r = rel_parse(sql, s, relt, m_deps);
     866           0 :                                                 if (r)
     867           0 :                                                         r = sql_processrelation(sql, r, 0, 0, 0, 0);
     868           0 :                                                 if (r) {
     869           0 :                                                         list *id_l = rel_dependencies(sql, r);
     870             : 
     871           0 :                                                         for (node *o = id_l->h ; o ; o = o->next) {
     872           0 :                                                                 sqlid next = ((sql_base*) o->data)->id;
     873           0 :                                                                 if (next != tr->base.id) {
     874           0 :                                                                         pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",",
     875             :                                                                                                         next, tr->base.id, (int) TRIGGER_DEPENDENCY);
     876           0 :                                                                         first = false;
     877           0 :                                                                         FLUSH_INSERTS_IF_BUFFERFILLED;
     878             :                                                                 }
     879             :                                                         }
     880             :                                                 }
     881             :                                         }
     882             :                         }
     883             :                 }
     884             :         }
     885             : 
     886           0 :         if (ppos != pos) { /* found updatable functions */
     887           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     888             :                                                 ") as t1(c1,c2,c3) where t1.c1 not in (select \"id\" from sys.dependencies where depend_id = t1.c2);\n");
     889             : 
     890           0 :                 assert(pos < bufsize);
     891           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
     892           0 :                 fflush(stdout);
     893           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     894             :         }
     895             : 
     896           0 : bailout:
     897           0 :         if (sql->sa)
     898           0 :                 sa_destroy(sql->sa);
     899           0 :         sql->sa = old_sa;
     900           0 :         GDKfree(buf);
     901           0 :         return err;
     902             : }
     903             : 
     904             : static str
     905           0 : sql_update_nov2019(Client c, mvc *sql)
     906             : {
     907           0 :         size_t bufsize = 16384, pos = 0;
     908           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     909             : 
     910           0 :         (void) sql;
     911           0 :         if (buf == NULL)
     912           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     913             : 
     914           0 :         pos += snprintf(buf + pos, bufsize - pos,
     915             :                         "create function sys.deltas (\"schema\" string)"
     916             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     917             :                         " external name \"sql\".\"deltas\";\n"
     918             :                         "create function sys.deltas (\"schema\" string, \"table\" string)"
     919             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     920             :                         " external name \"sql\".\"deltas\";\n"
     921             :                         "create function sys.deltas (\"schema\" string, \"table\" string, \"column\" string)"
     922             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     923             :                         " external name \"sql\".\"deltas\";\n"
     924             :                         "create aggregate median_avg(val TINYINT) returns DOUBLE\n"
     925             :                         " external name \"aggr\".\"median_avg\";\n"
     926             :                         "GRANT EXECUTE ON AGGREGATE median_avg(TINYINT) TO PUBLIC;\n"
     927             :                         "create aggregate median_avg(val SMALLINT) returns DOUBLE\n"
     928             :                         " external name \"aggr\".\"median_avg\";\n"
     929             :                         "GRANT EXECUTE ON AGGREGATE median_avg(SMALLINT) TO PUBLIC;\n"
     930             :                         "create aggregate median_avg(val INTEGER) returns DOUBLE\n"
     931             :                         " external name \"aggr\".\"median_avg\";\n"
     932             :                         "GRANT EXECUTE ON AGGREGATE median_avg(INTEGER) TO PUBLIC;\n"
     933             :                         "create aggregate median_avg(val BIGINT) returns DOUBLE\n"
     934             :                         " external name \"aggr\".\"median_avg\";\n"
     935             :                         "GRANT EXECUTE ON AGGREGATE median_avg(BIGINT) TO PUBLIC;\n"
     936             :                         "create aggregate median_avg(val DECIMAL) returns DOUBLE\n"
     937             :                         " external name \"aggr\".\"median_avg\";\n"
     938             :                         "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL) TO PUBLIC;\n"
     939             :                         "create aggregate median_avg(val REAL) returns DOUBLE\n"
     940             :                         " external name \"aggr\".\"median_avg\";\n"
     941             :                         "GRANT EXECUTE ON AGGREGATE median_avg(REAL) TO PUBLIC;\n"
     942             :                         "create aggregate median_avg(val DOUBLE) returns DOUBLE\n"
     943             :                         " external name \"aggr\".\"median_avg\";\n"
     944             :                         "GRANT EXECUTE ON AGGREGATE median_avg(DOUBLE) TO PUBLIC;\n"
     945             :                         "create aggregate quantile_avg(val TINYINT, q DOUBLE) returns DOUBLE\n"
     946             :                         " external name \"aggr\".\"quantile_avg\";\n"
     947             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(TINYINT, DOUBLE) TO PUBLIC;\n"
     948             :                         "create aggregate quantile_avg(val SMALLINT, q DOUBLE) returns DOUBLE\n"
     949             :                         " external name \"aggr\".\"quantile_avg\";\n"
     950             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(SMALLINT, DOUBLE) TO PUBLIC;\n"
     951             :                         "create aggregate quantile_avg(val INTEGER, q DOUBLE) returns DOUBLE\n"
     952             :                         " external name \"aggr\".\"quantile_avg\";\n"
     953             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(INTEGER, DOUBLE) TO PUBLIC;\n"
     954             :                         "create aggregate quantile_avg(val BIGINT, q DOUBLE) returns DOUBLE\n"
     955             :                         " external name \"aggr\".\"quantile_avg\";\n"
     956             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(BIGINT, DOUBLE) TO PUBLIC;\n"
     957             :                         "create aggregate quantile_avg(val DECIMAL, q DOUBLE) returns DOUBLE\n"
     958             :                         " external name \"aggr\".\"quantile_avg\";\n"
     959             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL, DOUBLE) TO PUBLIC;\n"
     960             :                         "create aggregate quantile_avg(val REAL, q DOUBLE) returns DOUBLE\n"
     961             :                         " external name \"aggr\".\"quantile_avg\";\n"
     962             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(REAL, DOUBLE) TO PUBLIC;\n"
     963             :                         "create aggregate quantile_avg(val DOUBLE, q DOUBLE) returns DOUBLE\n"
     964             :                         " external name \"aggr\".\"quantile_avg\";\n"
     965             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(DOUBLE, DOUBLE) TO PUBLIC;\n");
     966             : #ifdef HAVE_HGE
     967           0 :         pos += snprintf(buf + pos, bufsize - pos,
     968             :                                 "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
     969             :                                 " external name \"aggr\".\"median_avg\";\n"
     970             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
     971             :                                 "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
     972             :                                 " external name \"aggr\".\"quantile_avg\";\n"
     973             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n");
     974             : #endif
     975             :         /* 60/61_wlcr signatures migrations */
     976           0 :         pos += snprintf(buf + pos, bufsize - pos,
     977             :                         "drop procedure master() cascade;\n"
     978             :                         "drop procedure master(string) cascade;\n"
     979             :                         "drop procedure stopmaster() cascade;\n"
     980             :                         "drop procedure masterbeat(int) cascade;\n"
     981             :                         "drop function masterClock() cascade;\n"
     982             :                         "drop function masterTick() cascade;\n"
     983             :                         "drop procedure replicate() cascade;\n"
     984             :                         "drop procedure replicate(timestamp) cascade;\n"
     985             :                         "drop procedure replicate(string) cascade;\n"
     986             :                         "drop procedure replicate(string, timestamp) cascade;\n"
     987             :                         "drop procedure replicate(string, tinyint) cascade;\n"
     988             :                         "drop procedure replicate(string, smallint) cascade;\n"
     989             :                         "drop procedure replicate(string, integer) cascade;\n"
     990             :                         "drop procedure replicate(string, bigint) cascade;\n"
     991             :                         "drop procedure replicabeat(integer) cascade;\n"
     992             :                         "drop function replicaClock() cascade;\n"
     993             :                         "drop function replicaTick() cascade;\n"
     994             :                 );
     995             : 
     996           0 :         pos += snprintf(buf + pos, bufsize - pos,
     997             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     998             :                         " and name in ('deltas') and type = %d;\n", (int) F_UNION);
     999           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1000             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1001             :                         " and name in ('median_avg', 'quantile_avg') and type = %d;\n", (int) F_AGGR);
    1002             : 
    1003             :         /* 39_analytics.sql */
    1004           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1005             :                         "create aggregate stddev_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1006             :                         "external name \"aggr\".\"stdev\";\n"
    1007             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1008             :                         "create aggregate stddev_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1009             :                         "external name \"aggr\".\"stdev\";\n"
    1010             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1011             : 
    1012             :                         "create aggregate stddev_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1013             :                         "external name \"aggr\".\"stdevp\";\n"
    1014             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1015             :                         "create aggregate stddev_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1016             :                         "external name \"aggr\".\"stdevp\";\n"
    1017             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1018             : 
    1019             :                         "create aggregate var_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1020             :                         "external name \"aggr\".\"variance\";\n"
    1021             :                         "GRANT EXECUTE ON AGGREGATE var_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1022             :                         "create aggregate var_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1023             :                         "external name \"aggr\".\"variance\";\n"
    1024             :                         "GRANT EXECUTE ON AGGREGATE var_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1025             : 
    1026             :                         "create aggregate var_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1027             :                         "external name \"aggr\".\"variancep\";\n"
    1028             :                         "GRANT EXECUTE ON AGGREGATE var_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1029             :                         "create aggregate var_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1030             :                         "external name \"aggr\".\"variancep\";\n"
    1031             :                         "GRANT EXECUTE ON AGGREGATE var_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1032             : 
    1033             :                         "create aggregate median(val INTERVAL SECOND) returns INTERVAL SECOND\n"
    1034             :                         "external name \"aggr\".\"median\";\n"
    1035             :                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL SECOND) TO PUBLIC;\n"
    1036             :                         "create aggregate median(val INTERVAL MONTH) returns INTERVAL MONTH\n"
    1037             :                         "external name \"aggr\".\"median\";\n"
    1038             :                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL MONTH) TO PUBLIC;\n"
    1039             : 
    1040             :                         "create aggregate quantile(val INTERVAL SECOND, q DOUBLE) returns INTERVAL SECOND\n"
    1041             :                         "external name \"aggr\".\"quantile\";\n"
    1042             :                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL SECOND, DOUBLE) TO PUBLIC;\n"
    1043             :                         "create aggregate quantile(val INTERVAL MONTH, q DOUBLE) returns INTERVAL MONTH\n"
    1044             :                         "external name \"aggr\".\"quantile\";\n"
    1045             :                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL MONTH, DOUBLE) TO PUBLIC;\n"
    1046             :                 );
    1047             : 
    1048           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1049             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1050             :                         " and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'var_pop', 'median', 'quantile') and type = %d;\n", (int) F_AGGR);
    1051             : 
    1052             :         /* The MAL implementation of functions json.text(string) and json.text(int) do not exist */
    1053           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1054             :                         "drop function json.text(string) cascade;\n"
    1055             :                         "drop function json.text(int) cascade;\n");
    1056             : 
    1057             :         /* The first argument to copyfrom is a PTR type */
    1058           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1059             :                         "update sys.args set type = 'ptr' where"
    1060             :                         " func_id = (select id from sys.functions where name = 'copyfrom' and func = 'copy_from' and mod = 'sql' and type = %d) and name = 'arg_1';\n", (int) F_UNION);
    1061             : 
    1062           0 :         assert(pos < bufsize);
    1063             : 
    1064           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1065           0 :         fflush(stdout);
    1066           0 :         err = SQLstatementIntern(c, buf, "update", 1, 0, NULL);
    1067           0 :         GDKfree(buf);
    1068           0 :         return err;             /* usually MAL_SUCCEED */
    1069             : }
    1070             : 
    1071             : #ifdef HAVE_HGE
    1072             : static str
    1073           0 : sql_update_nov2019_sp1_hugeint(Client c, mvc *sql)
    1074             : {
    1075           0 :         size_t bufsize = 1024, pos = 0;
    1076           0 :         char *buf, *err;
    1077             : 
    1078           0 :         (void) sql;
    1079           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1080           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1081             : 
    1082             :         /* 39_analytics_hge.sql */
    1083           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1084             :                         "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
    1085             :                         " external name \"aggr\".\"median_avg\";\n"
    1086             :                         "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
    1087             :                         "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
    1088             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1089             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n");
    1090             : 
    1091           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1092             :                         "update sys.functions set system = true where system <> true and name in ('median_avg', 'quantile_avg') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_AGGR);
    1093             : 
    1094           0 :         assert(pos < bufsize);
    1095             : 
    1096           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1097           0 :         fflush(stdout);
    1098           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1099           0 :         GDKfree(buf);
    1100           0 :         return err;             /* usually MAL_SUCCEED */
    1101             : }
    1102             : #endif
    1103             : 
    1104             : static str
    1105           0 : sql_update_jun2020(Client c, mvc *sql)
    1106             : {
    1107           0 :         sql_table *t;
    1108           0 :         size_t bufsize = 32768, pos = 0;
    1109           0 :         char *err = NULL, *buf = NULL;
    1110           0 :         sql_schema *sys = mvc_bind_schema(sql, "sys");
    1111             : 
    1112           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1113           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1114             : 
    1115             :         /* convert old PYTHON2 and PYTHON2_MAP to PYTHON and PYTHON_MAP
    1116             :          * see also function load_func() in store.c */
    1117           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1118             :                         "update sys.functions set language = language - 2 where language in (8, 9);\n");
    1119             : 
    1120           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1121             :                         "update sys.args set name = name || '_' || cast(number as string) where name in ('arg', 'res') and func_id in (select id from sys.functions f where f.system);\n");
    1122           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1123             :                         "insert into sys.dependencies values ((select id from sys.functions where name = 'ms_trunc' and schema_id = (select id from sys.schemas where name = 'sys')), (select id from sys.functions where name = 'ms_round' and schema_id = (select id from sys.schemas where name = 'sys')), (select dependency_type_id from sys.dependency_types where dependency_type_name = 'FUNCTION'));\n");
    1124             : 
    1125             :         /* 12_url */
    1126           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1127             :                         "drop function isaURL(url) cascade;\n"
    1128             :                         "CREATE function isaURL(theUrl string) RETURNS BOOL\n"
    1129             :                         " EXTERNAL NAME url.\"isaURL\";\n"
    1130             :                         "GRANT EXECUTE ON FUNCTION isaURL(string) TO PUBLIC;\n"
    1131             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1132             :                         " and name = 'isaurl' and type = %d;\n", (int) F_FUNC);
    1133             : 
    1134             :         /* 13_date.sql */
    1135           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1136             :                         "drop function str_to_time(string, string) cascade;\n"
    1137             :                         "drop function time_to_str(time, string) cascade;\n"
    1138             :                         "drop function str_to_timestamp(string, string) cascade;\n"
    1139             :                         "drop function timestamp_to_str(timestamp, string) cascade;\n"
    1140             :                         "create function str_to_time(s string, format string) returns time with time zone\n"
    1141             :                         " external name mtime.\"str_to_time\";\n"
    1142             :                         "create function time_to_str(d time with time zone, format string) returns string\n"
    1143             :                         " external name mtime.\"time_to_str\";\n"
    1144             :                         "create function str_to_timestamp(s string, format string) returns timestamp with time zone\n"
    1145             :                         " external name mtime.\"str_to_timestamp\";\n"
    1146             :                         "create function timestamp_to_str(d timestamp with time zone, format string) returns string\n"
    1147             :                         " external name mtime.\"timestamp_to_str\";\n"
    1148             :                         "grant execute on function str_to_time to public;\n"
    1149             :                         "grant execute on function time_to_str to public;\n"
    1150             :                         "grant execute on function str_to_timestamp to public;\n"
    1151             :                         "grant execute on function timestamp_to_str to public;\n"
    1152             :                         "update sys.functions set system = true where system <> true and name in"
    1153             :                         " ('str_to_time', 'str_to_timestamp', 'time_to_str', 'timestamp_to_str')"
    1154             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_FUNC);
    1155             : 
    1156             :         /* 16_tracelog */
    1157           0 :         t = mvc_bind_table(sql, sys, "tracelog");
    1158           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1159           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1160             :                         "drop view sys.tracelog cascade;\n"
    1161             :                         "drop function sys.tracelog() cascade;\n"
    1162             :                         "create function sys.tracelog()\n"
    1163             :                         " returns table (\n"
    1164             :                         "  ticks bigint, -- time in microseconds\n"
    1165             :                         "  stmt string  -- actual statement executed\n"
    1166             :                         " )\n"
    1167             :                         " external name sql.dump_trace;\n"
    1168             :                         "create view sys.tracelog as select * from sys.tracelog();\n"
    1169             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1170             :                         " and name = 'tracelog' and type = %d;\n", (int) F_UNION);
    1171           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1172             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1173             :                         " and name = 'tracelog';\n");
    1174             : 
    1175             :         /* 17_temporal.sql */
    1176           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1177             :                         "drop function sys.epoch(bigint) cascade;\n"
    1178             :                         "drop function sys.epoch(int) cascade;\n"
    1179             :                         "drop function sys.epoch(timestamp) cascade;\n"
    1180             :                         "drop function sys.epoch(timestamp with time zone) cascade;\n"
    1181             :                         "create function sys.epoch(sec BIGINT) returns TIMESTAMP WITH TIME ZONE\n"
    1182             :                         " external name mtime.epoch;\n"
    1183             :                         "create function sys.epoch(sec INT) returns TIMESTAMP WITH TIME ZONE\n"
    1184             :                         " external name mtime.epoch;\n"
    1185             :                         "create function sys.epoch(ts TIMESTAMP WITH TIME ZONE) returns INT\n"
    1186             :                         " external name mtime.epoch;\n"
    1187             :                         "create function sys.date_trunc(txt string, t timestamp with time zone)\n"
    1188             :                         "returns timestamp with time zone\n"
    1189             :                         "external name sql.date_trunc;\n"
    1190             :                         "grant execute on function sys.date_trunc(string, timestamp with time zone) to public;\n"
    1191             :                         "grant execute on function sys.epoch (BIGINT) to public;\n"
    1192             :                         "grant execute on function sys.epoch (INT) to public;\n"
    1193             :                         "grant execute on function sys.epoch (TIMESTAMP WITH TIME ZONE) to public;\n"
    1194             :                         "update sys.functions set system = true where system <> true and name in"
    1195             :                         " ('epoch', 'date_trunc')"
    1196             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_FUNC);
    1197             : 
    1198             :         /* 22_clients */
    1199           0 :         t = mvc_bind_table(sql, sys, "sessions");
    1200           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1201             : 
    1202           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1203             :                         "drop view sys.sessions cascade;\n"
    1204             :                         "drop function sys.sessions cascade;\n"
    1205             :                         "create function sys.sessions()\n"
    1206             :                         "returns table(\n"
    1207             :                                 "\"sessionid\" int,\n"
    1208             :                                 "\"username\" string,\n"
    1209             :                                 "\"login\" timestamp,\n"
    1210             :                                 "\"idle\" timestamp,\n"
    1211             :                                 "\"optimizer\" string,\n"
    1212             :                                 "\"sessiontimeout\" int,\n"
    1213             :                                 "\"querytimeout\" int,\n"
    1214             :                                 "\"workerlimit\" int,\n"
    1215             :                                 "\"memorylimit\" int)\n"
    1216             :                         " external name sql.sessions;\n"
    1217             :                         "create view sys.sessions as select * from sys.sessions();\n");
    1218             : 
    1219           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1220             :                         "grant execute on procedure sys.settimeout(bigint) to public;\n"
    1221             :                         "grant execute on procedure sys.settimeout(bigint,bigint) to public;\n"
    1222             :                         "grant execute on procedure sys.setsession(bigint) to public;\n");
    1223             : 
    1224           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1225             :                         "create procedure sys.setoptimizer(\"optimizer\" string)\n"
    1226             :                         " external name clients.setoptimizer;\n"
    1227             :                         "grant execute on procedure sys.setoptimizer(string) to public;\n"
    1228             :                         "create procedure sys.setquerytimeout(\"query\" int)\n"
    1229             :                         " external name clients.setquerytimeout;\n"
    1230             :                         "grant execute on procedure sys.setquerytimeout(int) to public;\n"
    1231             :                         "create procedure sys.setsessiontimeout(\"timeout\" int)\n"
    1232             :                         " external name clients.setsessiontimeout;\n"
    1233             :                         "grant execute on procedure sys.setsessiontimeout(int) to public;\n"
    1234             :                         "create procedure sys.setworkerlimit(\"limit\" int)\n"
    1235             :                         " external name clients.setworkerlimit;\n"
    1236             :                         "grant execute on procedure sys.setworkerlimit(int) to public;\n"
    1237             :                         "create procedure sys.setmemorylimit(\"limit\" int)\n"
    1238             :                         " external name clients.setmemorylimit;\n"
    1239             :                         "grant execute on procedure sys.setmemorylimit(int) to public;\n"
    1240             :                         "create procedure sys.setoptimizer(\"sessionid\" int, \"optimizer\" string)\n"
    1241             :                         " external name clients.setoptimizer;\n"
    1242             :                         "create procedure sys.setquerytimeout(\"sessionid\" int, \"query\" int)\n"
    1243             :                         " external name clients.setquerytimeout;\n"
    1244             :                         "create procedure sys.setsessiontimeout(\"sessionid\" int, \"query\" int)\n"
    1245             :                         " external name clients.setsessiontimeout;\n"
    1246             :                         "create procedure sys.setworkerlimit(\"sessionid\" int, \"limit\" int)\n"
    1247             :                         " external name clients.setworkerlimit;\n"
    1248             :                         "create procedure sys.setmemorylimit(\"sessionid\" int, \"limit\" int)\n"
    1249             :                         " external name clients.setmemorylimit;\n"
    1250             :                         "create procedure sys.stopsession(\"sessionid\" int)\n"
    1251             :                         " external name clients.stopsession;\n");
    1252             : 
    1253           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1254             :                         "create function sys.prepared_statements()\n"
    1255             :                         "returns table(\n"
    1256             :                         "\"sessionid\" int,\n"
    1257             :                         "\"username\" string,\n"
    1258             :                         "\"statementid\" int,\n"
    1259             :                         "\"statement\" string,\n"
    1260             :                         "\"created\" timestamp)\n"
    1261             :                         " external name sql.prepared_statements;\n"
    1262             :                         "grant execute on function sys.prepared_statements to public;\n"
    1263             :                         "create view sys.prepared_statements as select * from sys.prepared_statements();\n"
    1264             :                         "grant select on sys.prepared_statements to public;\n"
    1265             :                         "create function sys.prepared_statements_args()\n"
    1266             :                         "returns table(\n"
    1267             :                         "\"statementid\" int,\n"
    1268             :                         "\"type\" string,\n"
    1269             :                         "\"type_digits\" int,\n"
    1270             :                         "\"type_scale\" int,\n"
    1271             :                         "\"inout\" tinyint,\n"
    1272             :                         "\"number\" int,\n"
    1273             :                         "\"schema\" string,\n"
    1274             :                         "\"table\" string,\n"
    1275             :                         "\"column\" string)\n"
    1276             :                         " external name sql.prepared_statements_args;\n"
    1277             :                         "grant execute on function sys.prepared_statements_args to public;\n"
    1278             :                         "create view sys.prepared_statements_args as select * from sys.prepared_statements_args();\n"
    1279             :                         "grant select on sys.prepared_statements_args to public;\n");
    1280             : 
    1281           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1282             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1283             :                         " and name in ('sessions', 'prepared_statements', 'prepared_statements_args') and type = %d;\n", (int) F_UNION);
    1284           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1285             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1286             :                         " and name in ('sessions', 'prepared_statements', 'prepared_statements_args');\n");
    1287           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1288             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1289             :                         " and name in ('setoptimizer', 'setquerytimeout', 'setsessiontimeout', 'setworkerlimit', 'setmemorylimit', 'setoptimizer', 'stopsession') and type = %d;\n", (int) F_PROC);
    1290             : 
    1291             :         /* 25_debug */
    1292           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1293             :                         "create procedure sys.suspend_log_flushing()\n"
    1294             :                         " external name sql.suspend_log_flushing;\n"
    1295             :                         "create procedure sys.resume_log_flushing()\n"
    1296             :                         " external name sql.resume_log_flushing;\n"
    1297             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1298             :                         " and name in ('suspend_log_flushing', 'resume_log_flushing') and type = %d;\n", (int) F_PROC);
    1299             : 
    1300           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1301             :                         "create function sys.debug(flag string) returns integer\n"
    1302             :                         " external name mdb.\"setDebug\";\n"
    1303             :                         "create function sys.debugflags()\n"
    1304             :                         " returns table(flag string, val bool)\n"
    1305             :                         " external name mdb.\"getDebugFlags\";\n");
    1306           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1307             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1308             :                         " and name in ('debug') and type = %d;\n"
    1309             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1310             :                         " and name in ('debugflags') and type = %d;\n",
    1311             :                         (int) F_FUNC, (int) F_UNION);
    1312             : 
    1313             :         /* 26_sysmon */
    1314           0 :         t = mvc_bind_table(sql, sys, "queue");
    1315           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1316             : 
    1317           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1318             :                         "drop view sys.queue cascade;\n"
    1319             :                         "drop function sys.queue cascade;\n"
    1320             :                         "create function sys.queue()\n"
    1321             :                         "returns table(\n"
    1322             :                         "\"tag\" bigint,\n"
    1323             :                         "\"sessionid\" int,\n"
    1324             :                         "\"username\" string,\n"
    1325             :                         "\"started\" timestamp,\n"
    1326             :                         "\"status\" string,\n"
    1327             :                         "\"query\" string,\n"
    1328             :                         "\"progress\" int,\n"
    1329             :                         "\"maxworkers\" int,\n"
    1330             :                         "\"footprint\" int)\n"
    1331             :                         " external name sysmon.queue;\n"
    1332             :                         "grant execute on function sys.queue to public;\n"
    1333             :                         "create view sys.queue as select * from sys.queue();\n"
    1334             :                         "grant select on sys.queue to public;\n"
    1335             : 
    1336             :                         "drop procedure sys.pause(int) cascade;\n"
    1337             :                         "drop procedure sys.resume(int) cascade;\n"
    1338             :                         "drop procedure sys.stop(int) cascade;\n"
    1339             : 
    1340             :                         "grant execute on procedure sys.pause(bigint) to public;\n"
    1341             :                         "grant execute on procedure sys.resume(bigint) to public;\n"
    1342             :                         "grant execute on procedure sys.stop(bigint) to public;\n");
    1343             : 
    1344           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1345             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1346             :                         " and name = 'queue' and type = %d;\n", (int) F_UNION);
    1347           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1348             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1349             :                         " and name = 'queue';\n");
    1350             : 
    1351             :         /* 39_analytics.sql */
    1352           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1353             :                         "create window stddev_samp(val TINYINT) returns DOUBLE\n"
    1354             :                         " external name \"sql\".\"stdev\";\n"
    1355             :                         "GRANT EXECUTE ON WINDOW stddev_samp(TINYINT) TO PUBLIC;\n"
    1356             :                         "create window stddev_samp(val SMALLINT) returns DOUBLE"
    1357             :                         " external name \"sql\".\"stdev\";\n"
    1358             :                         "GRANT EXECUTE ON WINDOW stddev_samp(SMALLINT) TO PUBLIC;\n"
    1359             :                         "create window stddev_samp(val INTEGER) returns DOUBLE\n"
    1360             :                         " external name \"sql\".\"stdev\";\n"
    1361             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTEGER) TO PUBLIC;\n"
    1362             :                         "create window stddev_samp(val BIGINT) returns DOUBLE\n"
    1363             :                         " external name \"sql\".\"stdev\";\n"
    1364             :                         "GRANT EXECUTE ON WINDOW stddev_samp(BIGINT) TO PUBLIC;\n"
    1365             :                         "create window stddev_samp(val REAL) returns DOUBLE\n"
    1366             :                         " external name \"sql\".\"stdev\";\n"
    1367             :                         "GRANT EXECUTE ON WINDOW stddev_samp(REAL) TO PUBLIC;\n"
    1368             :                         "create window stddev_samp(val DOUBLE) returns DOUBLE\n"
    1369             :                         " external name \"sql\".\"stdev\";\n"
    1370             :                         "GRANT EXECUTE ON WINDOW stddev_samp(DOUBLE) TO PUBLIC;\n"
    1371             :                         "create window stddev_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1372             :                         " external name \"sql\".\"stdev\";\n"
    1373             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1374             :                         "create window stddev_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1375             :                         " external name \"sql\".\"stdev\";\n"
    1376             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1377             :                         "create window stddev_pop(val TINYINT) returns DOUBLE\n"
    1378             :                         " external name \"sql\".\"stdevp\";\n"
    1379             :                         "GRANT EXECUTE ON WINDOW stddev_pop(TINYINT) TO PUBLIC;\n"
    1380             :                         "create window stddev_pop(val SMALLINT) returns DOUBLE\n"
    1381             :                         " external name \"sql\".\"stdevp\";\n"
    1382             :                         "GRANT EXECUTE ON WINDOW stddev_pop(SMALLINT) TO PUBLIC;\n"
    1383             :                         "create window stddev_pop(val INTEGER) returns DOUBLE\n"
    1384             :                         " external name \"sql\".\"stdevp\";\n"
    1385             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTEGER) TO PUBLIC;\n"
    1386             :                         "create window stddev_pop(val BIGINT) returns DOUBLE\n"
    1387             :                         " external name \"sql\".\"stdevp\";\n"
    1388             :                         "GRANT EXECUTE ON WINDOW stddev_pop(BIGINT) TO PUBLIC;\n"
    1389             :                         "create window stddev_pop(val REAL) returns DOUBLE\n"
    1390             :                         " external name \"sql\".\"stdevp\";\n"
    1391             :                         "GRANT EXECUTE ON WINDOW stddev_pop(REAL) TO PUBLIC;\n"
    1392             :                         "create window stddev_pop(val DOUBLE) returns DOUBLE\n"
    1393             :                         " external name \"sql\".\"stdevp\";\n"
    1394             :                         "GRANT EXECUTE ON WINDOW stddev_pop(DOUBLE) TO PUBLIC;\n"
    1395             :                         "create window stddev_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1396             :                         " external name \"sql\".\"stdevp\";\n"
    1397             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1398             :                         "create window stddev_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1399             :                         " external name \"sql\".\"stdevp\";\n"
    1400             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1401             :                         "create window var_samp(val TINYINT) returns DOUBLE\n"
    1402             :                         " external name \"sql\".\"variance\";\n"
    1403             :                         "GRANT EXECUTE ON WINDOW var_samp(TINYINT) TO PUBLIC;\n"
    1404             :                         "create window var_samp(val SMALLINT) returns DOUBLE\n"
    1405             :                         " external name \"sql\".\"variance\";\n"
    1406             :                         "GRANT EXECUTE ON WINDOW var_samp(SMALLINT) TO PUBLIC;\n"
    1407             :                         "create window var_samp(val INTEGER) returns DOUBLE\n"
    1408             :                         " external name \"sql\".\"variance\";\n"
    1409             :                         "GRANT EXECUTE ON WINDOW var_samp(INTEGER) TO PUBLIC;\n"
    1410             :                         "create window var_samp(val BIGINT) returns DOUBLE\n"
    1411             :                         " external name \"sql\".\"variance\";\n"
    1412             :                         "GRANT EXECUTE ON WINDOW var_samp(BIGINT) TO PUBLIC;\n"
    1413             :                         "create window var_samp(val REAL) returns DOUBLE\n"
    1414             :                         " external name \"sql\".\"variance\";\n"
    1415             :                         "GRANT EXECUTE ON WINDOW var_samp(REAL) TO PUBLIC;\n"
    1416             :                         "create window var_samp(val DOUBLE) returns DOUBLE\n"
    1417             :                         " external name \"sql\".\"variance\";\n"
    1418             :                         "GRANT EXECUTE ON WINDOW var_samp(DOUBLE) TO PUBLIC;\n"
    1419             :                         "create window var_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1420             :                         " external name \"sql\".\"variance\";\n"
    1421             :                         "GRANT EXECUTE ON WINDOW var_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1422             :                         "create window var_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1423             :                         " external name \"sql\".\"variance\";\n"
    1424             :                         "GRANT EXECUTE ON WINDOW var_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1425             :                         "create window var_pop(val TINYINT) returns DOUBLE\n"
    1426             :                         " external name \"sql\".\"variancep\";\n"
    1427             :                         "GRANT EXECUTE ON WINDOW var_pop(TINYINT) TO PUBLIC;\n"
    1428             :                         "create window var_pop(val SMALLINT) returns DOUBLE\n"
    1429             :                         " external name \"sql\".\"variancep\";\n"
    1430             :                         "GRANT EXECUTE ON WINDOW var_pop(SMALLINT) TO PUBLIC;\n"
    1431             :                         "create window var_pop(val INTEGER) returns DOUBLE\n"
    1432             :                         " external name \"sql\".\"variancep\";\n"
    1433             :                         "GRANT EXECUTE ON WINDOW var_pop(INTEGER) TO PUBLIC;\n"
    1434             :                         "create window var_pop(val BIGINT) returns DOUBLE\n"
    1435             :                         " external name \"sql\".\"variancep\";\n"
    1436             :                         "GRANT EXECUTE ON WINDOW var_pop(BIGINT) TO PUBLIC;\n"
    1437             :                         "create window var_pop(val REAL) returns DOUBLE\n"
    1438             :                         " external name \"sql\".\"variancep\";\n"
    1439             :                         "GRANT EXECUTE ON WINDOW var_pop(REAL) TO PUBLIC;\n"
    1440             :                         "create window var_pop(val DOUBLE) returns DOUBLE\n"
    1441             :                         " external name \"sql\".\"variancep\";\n"
    1442             :                         "GRANT EXECUTE ON WINDOW var_pop(DOUBLE) TO PUBLIC;\n"
    1443             :                         "create window var_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1444             :                         " external name \"sql\".\"variancep\";\n"
    1445             :                         "GRANT EXECUTE ON WINDOW var_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1446             :                         "create window var_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1447             :                         " external name \"sql\".\"variancep\";\n"
    1448             :                         "GRANT EXECUTE ON WINDOW var_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1449             :                         "create aggregate covar_samp(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1450             :                         " external name \"aggr\".\"covariance\";\n"
    1451             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(TINYINT, TINYINT) TO PUBLIC;\n"
    1452             :                         "create aggregate covar_samp(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1453             :                         " external name \"aggr\".\"covariance\";\n"
    1454             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1455             :                         "create aggregate covar_samp(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1456             :                         " external name \"aggr\".\"covariance\";\n"
    1457             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTEGER, INTEGER) TO PUBLIC;\n"
    1458             :                         "create aggregate covar_samp(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1459             :                         " external name \"aggr\".\"covariance\";\n"
    1460             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(BIGINT, BIGINT) TO PUBLIC;\n"
    1461             :                         "create aggregate covar_samp(e1 REAL, e2 REAL) returns DOUBLE\n"
    1462             :                         " external name \"aggr\".\"covariance\";\n"
    1463             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(REAL, REAL) TO PUBLIC;\n"
    1464             :                         "create aggregate covar_samp(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1465             :                         " external name \"aggr\".\"covariance\";\n"
    1466             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1467             :                         "create aggregate covar_samp(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1468             :                         " external name \"aggr\".\"covariance\";\n"
    1469             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1470             :                         "create aggregate covar_samp(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1471             :                         " external name \"aggr\".\"covariance\";\n"
    1472             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1473             :                         "create window covar_samp(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1474             :                         " external name \"sql\".\"covariance\";\n"
    1475             :                         "GRANT EXECUTE ON WINDOW covar_samp(TINYINT, TINYINT) TO PUBLIC;\n"
    1476             :                         "create window covar_samp(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1477             :                         " external name \"sql\".\"covariance\";\n"
    1478             :                         "GRANT EXECUTE ON WINDOW covar_samp(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1479             :                         "create window covar_samp(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1480             :                         " external name \"sql\".\"covariance\";\n"
    1481             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTEGER, INTEGER) TO PUBLIC;\n"
    1482             :                         "create window covar_samp(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1483             :                         " external name \"sql\".\"covariance\";\n"
    1484             :                         "GRANT EXECUTE ON WINDOW covar_samp(BIGINT, BIGINT) TO PUBLIC;\n"
    1485             :                         "create window covar_samp(e1 REAL, e2 REAL) returns DOUBLE\n"
    1486             :                         " external name \"sql\".\"covariance\";\n"
    1487             :                         "GRANT EXECUTE ON WINDOW covar_samp(REAL, REAL) TO PUBLIC;\n"
    1488             :                         "create window covar_samp(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1489             :                         " external name \"sql\".\"covariance\";\n"
    1490             :                         "GRANT EXECUTE ON WINDOW covar_samp(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1491             :                         "create window covar_samp(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1492             :                         " external name \"sql\".\"covariance\";\n"
    1493             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1494             :                         "create window covar_samp(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1495             :                         " external name \"sql\".\"covariance\";\n"
    1496             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1497             :                         "create aggregate covar_pop(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1498             :                         " external name \"aggr\".\"covariancep\";\n"
    1499             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(TINYINT, TINYINT) TO PUBLIC;\n"
    1500             :                         "create aggregate covar_pop(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1501             :                         " external name \"aggr\".\"covariancep\";\n"
    1502             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1503             :                         "create aggregate covar_pop(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1504             :                         " external name \"aggr\".\"covariancep\";\n"
    1505             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTEGER, INTEGER) TO PUBLIC;\n"
    1506             :                         "create aggregate covar_pop(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1507             :                         " external name \"aggr\".\"covariancep\";\n"
    1508             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(BIGINT, BIGINT) TO PUBLIC;\n"
    1509             :                         "create aggregate covar_pop(e1 REAL, e2 REAL) returns DOUBLE\n"
    1510             :                         " external name \"aggr\".\"covariancep\";\n"
    1511             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(REAL, REAL) TO PUBLIC;\n"
    1512             :                         "create aggregate covar_pop(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1513             :                         " external name \"aggr\".\"covariancep\";\n"
    1514             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1515             :                         "create aggregate covar_pop(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1516             :                         " external name \"aggr\".\"covariancep\";\n"
    1517             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1518             :                         "create aggregate covar_pop(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1519             :                         " external name \"aggr\".\"covariancep\";\n"
    1520             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1521             :                         "create window covar_pop(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1522             :                         " external name \"sql\".\"covariancep\";\n"
    1523             :                         "GRANT EXECUTE ON WINDOW covar_pop(TINYINT, TINYINT) TO PUBLIC;\n"
    1524             :                         "create window covar_pop(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1525             :                         " external name \"sql\".\"covariancep\";\n"
    1526             :                         "GRANT EXECUTE ON WINDOW covar_pop(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1527             :                         "create window covar_pop(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1528             :                         " external name \"sql\".\"covariancep\";\n"
    1529             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTEGER, INTEGER) TO PUBLIC;\n"
    1530             :                         "create window covar_pop(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1531             :                         " external name \"sql\".\"covariancep\";\n"
    1532             :                         "GRANT EXECUTE ON WINDOW covar_pop(BIGINT, BIGINT) TO PUBLIC;\n"
    1533             :                         "create window covar_pop(e1 REAL, e2 REAL) returns DOUBLE\n"
    1534             :                         " external name \"sql\".\"covariancep\";\n"
    1535             :                         "GRANT EXECUTE ON WINDOW covar_pop(REAL, REAL) TO PUBLIC;\n"
    1536             :                         "create window covar_pop(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1537             :                         " external name \"sql\".\"covariancep\";\n"
    1538             :                         "GRANT EXECUTE ON WINDOW covar_pop(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1539             :                         "create window covar_pop(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1540             :                         " external name \"sql\".\"covariancep\";\n"
    1541             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1542             :                         "create window covar_pop(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1543             :                         " external name \"sql\".\"covariancep\";\n"
    1544             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1545             :                         "create aggregate corr(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1546             :                         " external name \"aggr\".\"corr\";\n"
    1547             :                         "GRANT EXECUTE ON AGGREGATE corr(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1548             :                         "create aggregate corr(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1549             :                         " external name \"aggr\".\"corr\";\n"
    1550             :                         "GRANT EXECUTE ON AGGREGATE corr(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1551             :                         "create window corr(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1552             :                         " external name \"sql\".\"corr\";\n"
    1553             :                         "GRANT EXECUTE ON WINDOW corr(TINYINT, TINYINT) TO PUBLIC;\n"
    1554             :                         "create window corr(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1555             :                         " external name \"sql\".\"corr\";\n"
    1556             :                         "GRANT EXECUTE ON WINDOW corr(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1557             :                         "create window corr(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1558             :                         " external name \"sql\".\"corr\";\n"
    1559             :                         "GRANT EXECUTE ON WINDOW corr(INTEGER, INTEGER) TO PUBLIC;\n"
    1560             :                         "create window corr(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1561             :                         " external name \"sql\".\"corr\";\n"
    1562             :                         "GRANT EXECUTE ON WINDOW corr(BIGINT, BIGINT) TO PUBLIC;\n"
    1563             :                         "create window corr(e1 REAL, e2 REAL) returns DOUBLE\n"
    1564             :                         " external name \"sql\".\"corr\";\n"
    1565             :                         "GRANT EXECUTE ON WINDOW corr(REAL, REAL) TO PUBLIC;\n"
    1566             :                         "create window corr(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1567             :                         " external name \"sql\".\"corr\";\n"
    1568             :                         "GRANT EXECUTE ON WINDOW corr(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1569             :                         "create window corr(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1570             :                         " external name \"sql\".\"corr\";\n"
    1571             :                         "GRANT EXECUTE ON WINDOW corr(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1572             :                         "create window corr(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1573             :                         " external name \"sql\".\"corr\";\n"
    1574             :                         "GRANT EXECUTE ON WINDOW corr(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n");
    1575             : 
    1576           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1577             :                 "create window sys.group_concat(str STRING) returns STRING\n"
    1578             :                 " external name \"sql\".\"str_group_concat\";\n"
    1579             :                 "GRANT EXECUTE ON WINDOW sys.group_concat(STRING) TO PUBLIC;\n"
    1580             :                 "create window sys.group_concat(str STRING, sep STRING) returns STRING\n"
    1581             :                 " external name \"sql\".\"str_group_concat\";\n"
    1582             :                 "GRANT EXECUTE ON WINDOW sys.group_concat(STRING, STRING) TO PUBLIC;\n");
    1583             : 
    1584           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1585             :                         "update sys.functions set system = true where system <> true and name in"
    1586             :                         " ('stddev_samp', 'stddev_pop', 'var_samp', 'var_pop', 'covar_samp', 'covar_pop', 'corr', 'group_concat')"
    1587             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type in (%d, %d);\n", (int) F_ANALYTIC, (int) F_AGGR);
    1588             : 
    1589           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1590             :                         "DROP AGGREGATE stddev_samp(date) CASCADE;\n"
    1591             :                         "DROP AGGREGATE stddev_samp(time) CASCADE;\n"
    1592             :                         "DROP AGGREGATE stddev_samp(timestamp) CASCADE;\n"
    1593             :                         "DROP AGGREGATE stddev_pop(date) CASCADE;\n"
    1594             :                         "DROP AGGREGATE stddev_pop(time) CASCADE;\n"
    1595             :                         "DROP AGGREGATE stddev_pop(timestamp) CASCADE;\n"
    1596             :                         "DROP AGGREGATE var_samp(date) CASCADE;\n"
    1597             :                         "DROP AGGREGATE var_samp(time) CASCADE;\n"
    1598             :                         "DROP AGGREGATE var_samp(timestamp) CASCADE;\n"
    1599             :                         "DROP AGGREGATE var_pop(date) CASCADE;\n"
    1600             :                         "DROP AGGREGATE var_pop(time) CASCADE;\n"
    1601             :                         "DROP AGGREGATE var_pop(timestamp) CASCADE;\n");
    1602             : 
    1603             :         /* 51_sys_schema_extensions */
    1604           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1605             :                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    1606             :                         "DELETE FROM sys.keywords where keyword IN ('NOCYCLE','NOMAXVALUE','NOMINVALUE');\n"
    1607             :                         "insert into sys.keywords values ('ANALYZE'),('AT'),('AUTHORIZATION'),('CACHE'),('CENTURY'),('COLUMN'),('CLIENT'),"
    1608             :                         "('CUBE'),('CYCLE'),('DATA'),('DATE'),('DEBUG'),('DECADE'),('DEALLOCATE'),('DIAGNOSTICS'),('DISTINCT'),"
    1609             :                         "('DOW'),('DOY'),('EXEC'),('EXECUTE'),('EXPLAIN'),('FIRST'),('FWF'),('GROUPING'),('GROUPS'),('INCREMENT'),"
    1610             :                         "('INTERVAL'),('KEY'),('LANGUAGE'),('LARGE'),('LAST'),('LATERAL'),('LEVEL'),('LOADER'),('MATCH'),('MATCHED'),('MAXVALUE'),"
    1611             :                         "('MINVALUE'),('NAME'),('NO'),('NULLS'),('OBJECT'),('OPTIONS'),('PASSWORD'),('PLAN'),('PRECISION'),('PREP'),('PREPARE'),"
    1612             :                         "('QUARTER'),('RELEASE'),('REPLACE'),('ROLLUP'),('SCHEMA'),('SEED'),('SERVER'),('SESSION'),('SETS'),('SIZE'),"
    1613             :                         "('STATEMENT'),('TABLE'),('TEMP'),('TEMPORARY'),('TEXT'),('TIME'),('TIMESTAMP'),('TRACE'),('TYPE'),"
    1614             :                         "('WEEK'),('YEAR'),('ZONE');\n");
    1615           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1616             :                         "ALTER TABLE sys.function_languages SET READ WRITE;\n"
    1617             :                         "DELETE FROM sys.function_languages where language_keyword IN ('PYTHON2','PYTHON2_MAP');\n");
    1618             : 
    1619             :         /* 58_hot_snapshot */
    1620           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1621             :                         "create procedure sys.hot_snapshot(tarfile string)\n"
    1622             :                         " external name sql.hot_snapshot;\n"
    1623             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1624             :                         " and name in ('hot_snapshot') and type = %d;\n", (int) F_PROC);
    1625             : 
    1626             :         /* 81_tracer.sql */
    1627           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1628             :                         "CREATE SCHEMA logging;\n"
    1629             :                         "CREATE PROCEDURE logging.flush()\n"
    1630             :                         " EXTERNAL NAME logging.flush;\n"
    1631             :                         "CREATE PROCEDURE logging.setcomplevel(comp_id STRING, lvl_id STRING)\n"
    1632             :                         " EXTERNAL NAME logging.setcomplevel;\n"
    1633             :                         "CREATE PROCEDURE logging.resetcomplevel(comp_id STRING)\n"
    1634             :                         " EXTERNAL NAME logging.resetcomplevel;\n"
    1635             :                         "CREATE PROCEDURE logging.setlayerlevel(layer_id STRING, lvl_id STRING)\n"
    1636             :                         " EXTERNAL NAME logging.setlayerlevel;\n"
    1637             :                         "CREATE PROCEDURE logging.resetlayerlevel(layer_id STRING)\n"
    1638             :                         " EXTERNAL NAME logging.resetlayerlevel;\n"
    1639             :                         "CREATE PROCEDURE logging.setflushlevel(lvl_id STRING)\n"
    1640             :                         " EXTERNAL NAME logging.setflushlevel;\n"
    1641             :                         "CREATE PROCEDURE logging.resetflushlevel()\n"
    1642             :                         " EXTERNAL NAME logging.resetflushlevel;\n"
    1643             :                         "CREATE PROCEDURE logging.setadapter(adapter_id STRING)\n"
    1644             :                         " EXTERNAL NAME logging.setadapter;\n"
    1645             :                         "CREATE PROCEDURE logging.resetadapter()\n"
    1646             :                         " EXTERNAL NAME logging.resetadapter;\n"
    1647             :                         "CREATE FUNCTION logging.compinfo()\n"
    1648             :                         "RETURNS TABLE(\n"
    1649             :                         " \"id\" int,\n"
    1650             :                         " \"component\" string,\n"
    1651             :                         " \"log_level\" string\n"
    1652             :                         ")\n"
    1653             :                         "EXTERNAL NAME logging.compinfo;\n"
    1654             :                         "GRANT EXECUTE ON FUNCTION logging.compinfo TO public;\n"
    1655             :                         "CREATE view logging.compinfo AS SELECT * FROM logging.compinfo();\n"
    1656             :                         "GRANT SELECT ON logging.compinfo TO public;\n");
    1657           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1658             :                         "update sys.schemas set system = true where name = 'logging';\n"
    1659             :                         "update sys.functions set system = true where system <> true and name in"
    1660             :                         " ('flush', 'setcomplevel', 'resetcomplevel', 'setlayerlevel', 'resetlayerlevel', 'setflushlevel', 'resetflushlevel', 'setadapter', 'resetadapter')"
    1661             :                         " and schema_id = (select id from sys.schemas where name = 'logging') and type = %d;\n"
    1662             :                         "update sys.functions set system = true where system <> true and name in"
    1663             :                         " ('compinfo')"
    1664             :                         " and schema_id = (select id from sys.schemas where name = 'logging') and type = %d;\n"
    1665             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'logging')"
    1666             :                         " and name = 'compinfo';\n",
    1667             :                         (int) F_PROC, (int) F_UNION);
    1668             : 
    1669           0 :         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    1670           0 :         assert(pos < bufsize);
    1671             : 
    1672           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1673           0 :         fflush(stdout);
    1674           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1675           0 :         if (err == MAL_SUCCEED) {
    1676           0 :                 pos = snprintf(buf, bufsize,
    1677             :                                "ALTER TABLE sys.keywords SET READ ONLY;\n"
    1678             :                                "ALTER TABLE sys.function_languages SET READ ONLY;\n");
    1679           0 :                 assert(pos < bufsize);
    1680           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    1681           0 :                 fflush(stdout);
    1682           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1683             :         }
    1684           0 :         GDKfree(buf);
    1685           0 :         return err;             /* usually MAL_SUCCEED */
    1686             : }
    1687             : 
    1688             : static str
    1689         106 : sql_update_jun2020_bam(Client c, mvc *m)
    1690             : {
    1691         106 :         size_t bufsize = 10240, pos = 0;
    1692         106 :         char *err = NULL, *buf;
    1693         106 :         res_table *output;
    1694         106 :         BAT *b;
    1695         106 :         sql_schema *s = mvc_bind_schema(m, "bam");
    1696         106 :         sql_table *t;
    1697             : 
    1698         106 :         if (s == NULL || !s->system)
    1699             :                 return NULL;    /* no system schema "bam": nothing to do */
    1700             : 
    1701           0 :         buf = GDKmalloc(bufsize);
    1702           0 :         if (buf == NULL)
    1703           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1704             : 
    1705           0 :         s->system = 0;
    1706           0 :         if ((t = mvc_bind_table(m, s, "files")) != NULL)
    1707           0 :                 t->system = 0;
    1708           0 :         if ((t = mvc_bind_table(m, s, "sq")) != NULL)
    1709           0 :                 t->system = 0;
    1710           0 :         if ((t = mvc_bind_table(m, s, "rg")) != NULL)
    1711           0 :                 t->system = 0;
    1712           0 :         if ((t = mvc_bind_table(m, s, "pg")) != NULL)
    1713           0 :                 t->system = 0;
    1714           0 :         if ((t = mvc_bind_table(m, s, "export")) != NULL)
    1715           0 :                 t->system = 0;
    1716             : 
    1717             :         /* check if any of the tables in the bam schema have any content */
    1718           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1719             :                         "select sum(count) from sys.storage('bam');\n");
    1720           0 :         err = SQLstatementIntern(c, buf, "update", 1, 0, &output);
    1721           0 :         if (err) {
    1722           0 :                 GDKfree(buf);
    1723           0 :                 return err;
    1724             :         }
    1725           0 :         b = BATdescriptor(output->cols[0].b);
    1726           0 :         res_table_destroy(output);
    1727           0 :         if (b == NULL) {
    1728           0 :                 GDKfree(buf);
    1729           0 :                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1730             :         }
    1731           0 :         pos = 0;
    1732           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1733             :                         "update sys.schemas set system = false where name = 'bam';\n"
    1734             :                         "update sys._tables set system = false where schema_id in (select id from sys.schemas where name = 'bam');\n"
    1735             :                         "drop procedure bam.bam_loader_repos cascade;\n"
    1736             :                         "drop procedure bam.bam_loader_files cascade;\n"
    1737             :                         "drop procedure bam.bam_loader_file cascade;\n"
    1738             :                         "drop procedure bam.bam_drop_file cascade;\n"
    1739             :                         "drop function bam.bam_flag cascade;\n"
    1740             :                         "drop function bam.reverse_seq cascade;\n"
    1741             :                         "drop function bam.reverse_qual cascade;\n"
    1742             :                         "drop function bam.seq_length cascade;\n"
    1743             :                         "drop function bam.seq_char cascade;\n"
    1744             :                         "drop procedure bam.sam_export cascade;\n"
    1745             :                         "drop procedure bam.bam_export cascade;\n");
    1746           0 :         if (BATcount(b) > 0 && *(lng *) Tloc(b, 0) == 0) {
    1747             :                 /* tables in bam schema are empty: drop them */
    1748           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    1749             :                                                 "drop table bam.sq cascade;\n"
    1750             :                                                 "drop table bam.rg cascade;\n"
    1751             :                                                 "drop table bam.pg cascade;\n"
    1752             :                                                 "drop table bam.export cascade;\n"
    1753             :                                                 "drop table bam.files cascade;\n"
    1754             :                                                 "drop schema bam cascade;\n");
    1755             :         }
    1756           0 :         BBPunfix(b->batCacheid);
    1757             : 
    1758           0 :         assert(pos < bufsize);
    1759             : 
    1760           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1761           0 :         fflush(stdout);
    1762           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1763             : 
    1764           0 :         GDKfree(buf);
    1765           0 :         return err;
    1766             : }
    1767             : 
    1768             : #ifdef HAVE_HGE
    1769             : static str
    1770           0 : sql_update_jun2020_sp1_hugeint(Client c)
    1771             : {
    1772           0 :         size_t bufsize = 8192, pos = 0;
    1773           0 :         char *buf, *err;
    1774             : 
    1775           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1776           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1777             : 
    1778             :         /* 39_analytics_hge.sql */
    1779           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1780             :                         "create window stddev_samp(val HUGEINT) returns DOUBLE\n"
    1781             :                         " external name \"sql\".\"stdev\";\n"
    1782             :                         "GRANT EXECUTE ON WINDOW stddev_samp(HUGEINT) TO PUBLIC;\n"
    1783             :                         "create window stddev_pop(val HUGEINT) returns DOUBLE\n"
    1784             :                         " external name \"sql\".\"stdevp\";\n"
    1785             :                         "GRANT EXECUTE ON WINDOW stddev_pop(HUGEINT) TO PUBLIC;\n"
    1786             :                         "create window var_samp(val HUGEINT) returns DOUBLE\n"
    1787             :                         " external name \"sql\".\"variance\";\n"
    1788             :                         "GRANT EXECUTE ON WINDOW var_samp(HUGEINT) TO PUBLIC;\n"
    1789             :                         "create aggregate covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1790             :                         " external name \"aggr\".\"covariance\";\n"
    1791             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1792             :                         "create window covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1793             :                         " external name \"sql\".\"covariance\";\n"
    1794             :                         "GRANT EXECUTE ON WINDOW covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1795             :                         "create window var_pop(val HUGEINT) returns DOUBLE\n"
    1796             :                         " external name \"sql\".\"variancep\";\n"
    1797             :                         "GRANT EXECUTE ON WINDOW var_pop(HUGEINT) TO PUBLIC;\n"
    1798             :                         "create aggregate covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1799             :                         " external name \"aggr\".\"covariancep\";\n"
    1800             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1801             :                         "create window covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1802             :                         " external name \"sql\".\"covariancep\";\n"
    1803             :                         "GRANT EXECUTE ON WINDOW covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1804             :                         "create window corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1805             :                         " external name \"sql\".\"corr\";\n"
    1806             :                         "GRANT EXECUTE ON WINDOW corr(HUGEINT, HUGEINT) TO PUBLIC;\n");
    1807             : 
    1808           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1809             :                         "update sys.functions set system = true where system <> true and name in ('covar_samp', 'covar_pop') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n"
    1810             :                         "update sys.functions set system = true where system <> true and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'covar_samp', 'var_pop', 'covar_pop', 'corr') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n",
    1811             :                         (int) F_AGGR, (int) F_ANALYTIC);
    1812             : 
    1813           0 :         assert(pos < bufsize);
    1814             : 
    1815           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1816           0 :         fflush(stdout);
    1817           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1818           0 :         GDKfree(buf);
    1819           0 :         return err;             /* usually MAL_SUCCEED */
    1820             : }
    1821             : #endif
    1822             : 
    1823             : static str
    1824           0 : sql_update_oscar_lidar(Client c)
    1825             : {
    1826           0 :         char *query =
    1827             :                 "drop procedure sys.lidarattach(string) cascade;\n"
    1828             :                 "drop procedure sys.lidarload(string) cascade;\n"
    1829             :                 "drop procedure sys.lidarexport(string, string, string) cascade;\n";
    1830           0 :         printf("Running database upgrade commands:\n%s\n", query);
    1831           0 :         fflush(stdout);
    1832           0 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
    1833             : }
    1834             : 
    1835             : static str
    1836         106 : sql_update_oscar(Client c, mvc *sql)
    1837             : {
    1838         106 :         size_t bufsize = 8192, pos = 0;
    1839         106 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    1840         106 :         sql_schema *sys = mvc_bind_schema(sql, "sys");
    1841         106 :         res_table *output;
    1842         106 :         BAT *b;
    1843             : 
    1844         106 :         if (buf == NULL)
    1845           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1846             : 
    1847             :         /* if column 6 of sys.queue is named "progress" we need to update */
    1848         106 :         pos += snprintf(buf + pos, bufsize - pos,
    1849             :                         "select name from sys._columns where table_id = (select id from sys._tables where name = 'queue' and schema_id = (select id from sys.schemas where name = 'sys')) and number = 6;\n");
    1850         106 :         err = SQLstatementIntern(c, buf, "update", true, false, &output);
    1851         106 :         if (err) {
    1852           0 :                 GDKfree(buf);
    1853           0 :                 return err;
    1854             :         }
    1855         106 :         b = BATdescriptor(output->cols[0].b);
    1856         106 :         if (b) {
    1857         106 :                 BATiter bi = bat_iterator_nolock(b);
    1858         106 :                 if (BATcount(b) > 0 && strcmp(BUNtail(bi, 0), "progress") == 0) {
    1859           0 :                         pos = 0;
    1860             : 
    1861             :                         /* the real update of sys.env() has happened
    1862             :                          * in load_func, here we merely update the
    1863             :                          * sys.functions table */
    1864           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1865             :                                         "update sys.functions set"
    1866             :                                         " mod = 'inspect',"
    1867             :                                         " func = 'CREATE FUNCTION env() RETURNS TABLE( name varchar(1024), value varchar(2048)) EXTERNAL NAME inspect.\"getEnvironment\";'"
    1868             :                                         " where schema_id = (select id from sys.schemas where name = 'sys')"
    1869             :                                         " and name = 'env' and type = %d;\n",
    1870             :                                         (int) F_UNION);
    1871             : 
    1872             :                         /* 26_sysmon */
    1873           0 :                         sql_table *t;
    1874           0 :                         t = mvc_bind_table(sql, sys, "queue");
    1875           0 :                         t->system = 0; /* make it non-system else the drop view will fail */
    1876             : 
    1877           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1878             :                                         "drop view sys.queue cascade;\n"
    1879             :                                         "drop function sys.queue cascade;\n"
    1880             :                                         "create function sys.queue()\n"
    1881             :                                         "returns table(\n"
    1882             :                                         "\"tag\" bigint,\n"
    1883             :                                         "\"sessionid\" int,\n"
    1884             :                                         "\"username\" string,\n"
    1885             :                                         "\"started\" timestamp,\n"
    1886             :                                         "\"status\" string,\n"
    1887             :                                         "\"query\" string,\n"
    1888             :                                         "\"finished\" timestamp,\n"
    1889             :                                         "\"workers\" int,\n"
    1890             :                                         "\"memory\" int)\n"
    1891             :                                         " external name sysmon.queue;\n"
    1892             :                                         "grant execute on function sys.queue to public;\n"
    1893             :                                         "create view sys.queue as select * from sys.queue();\n"
    1894             :                                         "grant select on sys.queue to public;\n"
    1895             :                                         "drop procedure sys.pause(bigint) cascade;\n"
    1896             :                                         "drop procedure sys.resume(bigint) cascade;\n"
    1897             :                                         "drop procedure sys.stop(bigint) cascade;\n"
    1898             :                                         "create procedure sys.pause(tag bigint)\n"
    1899             :                                         "external name sysmon.pause;\n"
    1900             :                                         "grant execute on procedure sys.pause(bigint) to public;\n"
    1901             :                                         "create procedure sys.resume(tag bigint)\n"
    1902             :                                         "external name sysmon.resume;\n"
    1903             :                                         "grant execute on procedure sys.resume(bigint) to public;\n"
    1904             :                                         "create procedure sys.stop(tag bigint)\n"
    1905             :                                         "external name sysmon.stop;\n"
    1906             :                                         "grant execute on procedure sys.stop(bigint) to public;\n");
    1907             : 
    1908           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1909             :                                         "update sys.functions set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1910             :                                         " and name = 'queue' and type = %d;\n"
    1911             :                                         "update sys.functions set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1912             :                                         " and name in ('pause', 'resume', 'stop') and type = %d;\n",
    1913             :                                         (int) F_UNION, (int) F_PROC);
    1914           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1915             :                                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1916             :                                         " and name = 'queue';\n");
    1917             : 
    1918             :                         /* scoping branch changes */
    1919           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1920             :                                         "drop function \"sys\".\"var\"() cascade;\n"
    1921             :                                         "create function \"sys\".\"var\"() "
    1922             :                                         "returns table("
    1923             :                                         "\"schema\" string, "
    1924             :                                         "\"name\" string, "
    1925             :                                         "\"type\" string, "
    1926             :                                         "\"value\" string) "
    1927             :                                         "external name \"sql\".\"sql_variables\";\n"
    1928             :                                         "grant execute on function \"sys\".\"var\" to public;\n");
    1929             : 
    1930           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1931             :                                         "update sys.functions set system = true"
    1932             :                                         " where name = 'var' and schema_id = (select id from sys.schemas where name = 'sys');\n"
    1933             :                                         "update sys.privileges set grantor = 0 where obj_id = (select id from sys.functions where name = 'var' and schema_id = (select id from sys.schemas where name = 'sys') and type = %d);\n",
    1934             :                                         (int) F_UNION);
    1935             : 
    1936           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1937             :                                         "create procedure sys.hot_snapshot(tarfile string, onserver bool)\n"
    1938             :                                         "external name sql.hot_snapshot;\n"
    1939             :                                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1940             :                                         " and name in ('hot_snapshot') and type = %d;\n",
    1941             :                                         (int) F_PROC);
    1942             :                         /* .snapshot user */
    1943           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1944             :                                 "create user \".snapshot\"\n"
    1945             :                                 " with encrypted password '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'\n"
    1946             :                                 " name 'Snapshot User'\n"
    1947             :                                 " schema sys;\n"
    1948             :                                 "grant execute on procedure sys.hot_snapshot(string) to \".snapshot\";\n"
    1949             :                                 "grant execute on procedure sys.hot_snapshot(string, bool) to \".snapshot\";\n"
    1950             :                         );
    1951             : 
    1952             :                         /* SQL functions without backend implementations */
    1953           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1954             :                                         "DROP FUNCTION \"sys\".\"getcontent\"(url) CASCADE;\n"
    1955             :                                         "DROP AGGREGATE \"json\".\"output\"(json) CASCADE;\n");
    1956             : 
    1957             :                         /* Move sys.degrees,sys.radians,sys.like and sys.ilike to sql_types.c definitions (I did this at the bat_logger) Remove the obsolete entries at privileges table */
    1958           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1959             :                                         "delete from privileges where obj_id in (select obj_id from privileges left join functions on privileges.obj_id = functions.id where functions.id is null and privileges.obj_id not in ((SELECT tables.id from tables), 0));\n");
    1960             : 
    1961           0 :                         assert(pos < bufsize);
    1962             : 
    1963           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    1964           0 :                         fflush(stdout);
    1965           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1966             :                 }
    1967         106 :                 BBPunfix(b->batCacheid);
    1968             :         } else {
    1969           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1970             :         }
    1971         106 :         res_table_destroy(output);
    1972         106 :         GDKfree(buf);
    1973         106 :         return err;             /* usually MAL_SUCCEED */
    1974             : }
    1975             : 
    1976             : static str
    1977         106 : sql_update_oct2020(Client c, mvc *sql)
    1978             : {
    1979         106 :         size_t bufsize = 4096, pos = 0;
    1980         106 :         char *buf = NULL, *err = NULL;
    1981         106 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    1982         106 :         sql_table *t;
    1983         106 :         res_table *output = NULL;
    1984         106 :         BAT *b = NULL;
    1985             : 
    1986         106 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1987           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1988             : 
    1989             :         /* if view sys.var_values mentions the query cache ('cache') we need
    1990             :            to update */
    1991         106 :         pos += snprintf(buf + pos, bufsize - pos,
    1992             :                                         "select id from sys._tables where name = 'var_values' and query like '%%''cache''%%' and schema_id = (select id from sys.schemas where name = 'sys');\n");
    1993         106 :         err = SQLstatementIntern(c, buf, "update", true, false, &output);
    1994         106 :         if (err) {
    1995           0 :                 GDKfree(buf);
    1996           0 :                 return err;
    1997             :         }
    1998         106 :         b = BATdescriptor(output->cols[0].b);
    1999         106 :         if (b) {
    2000         106 :                 if (BATcount(b) > 0) {
    2001           0 :                         pos = 0;
    2002             : 
    2003             :                         /* 51_sys_schema_extensions.sql */
    2004           0 :                         t = mvc_bind_table(sql, s, "var_values");
    2005           0 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    2006           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2007             :                                                         "DROP VIEW sys.var_values CASCADE;\n"
    2008             :                                                         "CREATE VIEW sys.var_values (var_name, value) AS\n"
    2009             :                                                         "SELECT 'current_role', current_role UNION ALL\n"
    2010             :                                                         "SELECT 'current_schema', current_schema UNION ALL\n"
    2011             :                                                         "SELECT 'current_timezone', current_timezone UNION ALL\n"
    2012             :                                                         "SELECT 'current_user', current_user UNION ALL\n"
    2013             :                                                         "SELECT 'debug', debug UNION ALL\n"
    2014             :                                                         "SELECT 'last_id', last_id UNION ALL\n"
    2015             :                                                         "SELECT 'optimizer', optimizer UNION ALL\n"
    2016             :                                                         "SELECT 'pi', pi() UNION ALL\n"
    2017             :                                                         "SELECT 'rowcnt', rowcnt;\n"
    2018             :                                                         "UPDATE sys._tables SET system = true WHERE name = 'var_values' AND schema_id = (SELECT id FROM sys.schemas WHERE name = 'sys');\n"
    2019             :                                                         "GRANT SELECT ON sys.var_values TO PUBLIC;\n");
    2020             :                         /* 26_sysmon.sql */
    2021           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2022             :                                         "create function sys.user_statistics()\n"
    2023             :                                         "returns table(\n"
    2024             :                                                 " username string,\n"
    2025             :                                                 " querycount bigint,\n"
    2026             :                                                 " totalticks bigint,\n"
    2027             :                                                 " started timestamp,\n"
    2028             :                                                 " finished timestamp,\n"
    2029             :                                                 " maxticks bigint,\n"
    2030             :                                                 " maxquery string\n"
    2031             :                                         ")\n"
    2032             :                                         "external name sysmon.user_statistics;\n"
    2033             :                                         "update sys.functions set system = true where system <> true and name = 'user_statistics' and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_UNION);
    2034             : 
    2035             :                         /* Remove entries on sys.args table without correspondents on sys.functions table */
    2036           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2037             :                                         "delete from sys.args where id in (select args.id from sys.args left join sys.functions on args.func_id = functions.id where functions.id is null);\n");
    2038             : 
    2039             :                         /* 39_analytics.sql */
    2040           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2041             :                                         "DROP AGGREGATE stddev_samp(INTERVAL SECOND) CASCADE;\n"
    2042             :                                         "DROP AGGREGATE stddev_samp(INTERVAL MONTH) CASCADE;\n"
    2043             :                                         "DROP WINDOW stddev_samp(INTERVAL SECOND) CASCADE;\n"
    2044             :                                         "DROP WINDOW stddev_samp(INTERVAL MONTH) CASCADE;\n"
    2045             :                                         "DROP AGGREGATE stddev_pop(INTERVAL SECOND) CASCADE;\n"
    2046             :                                         "DROP AGGREGATE stddev_pop(INTERVAL MONTH) CASCADE;\n"
    2047             :                                         "DROP WINDOW stddev_pop(INTERVAL SECOND) CASCADE;\n"
    2048             :                                         "DROP WINDOW stddev_pop(INTERVAL MONTH) CASCADE;\n"
    2049             :                                         "DROP AGGREGATE var_samp(INTERVAL SECOND) CASCADE;\n"
    2050             :                                         "DROP AGGREGATE var_samp(INTERVAL MONTH) CASCADE;\n"
    2051             :                                         "DROP WINDOW var_samp(INTERVAL SECOND) CASCADE;\n"
    2052             :                                         "DROP WINDOW var_samp(INTERVAL MONTH) CASCADE;\n"
    2053             :                                         "DROP AGGREGATE var_pop(INTERVAL SECOND) CASCADE;\n"
    2054             :                                         "DROP AGGREGATE var_pop(INTERVAL MONTH) CASCADE;\n"
    2055             :                                         "DROP WINDOW var_pop(INTERVAL SECOND) CASCADE;\n"
    2056             :                                         "DROP WINDOW var_pop(INTERVAL MONTH) CASCADE;\n"
    2057             :                                         "DROP AGGREGATE covar_samp(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2058             :                                         "DROP AGGREGATE covar_samp(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2059             :                                         "DROP WINDOW covar_samp(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2060             :                                         "DROP WINDOW covar_samp(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2061             :                                         "DROP AGGREGATE covar_pop(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2062             :                                         "DROP AGGREGATE covar_pop(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2063             :                                         "DROP WINDOW covar_pop(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2064             :                                         "DROP WINDOW covar_pop(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2065             :                                         "DROP AGGREGATE corr(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2066             :                                         "DROP AGGREGATE corr(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2067             :                                         "DROP WINDOW corr(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2068             :                                         "DROP WINDOW corr(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2069             :                                         "create aggregate median(val INTERVAL DAY) returns INTERVAL DAY\n"
    2070             :                                         " external name \"aggr\".\"median\";\n"
    2071             :                                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL DAY) TO PUBLIC;\n"
    2072             :                                         "create aggregate quantile(val INTERVAL DAY, q DOUBLE) returns INTERVAL DAY\n"
    2073             :                                         " external name \"aggr\".\"quantile\";\n"
    2074             :                                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL DAY, DOUBLE) TO PUBLIC;\n"
    2075             :                                         "update sys.functions set system = true where system <> true and name in ('median', 'quantile') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_AGGR);
    2076             : 
    2077             :                         /* 90_generator.sql */
    2078           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2079             :                                         "create function sys.generate_series(first timestamp, \"limit\" timestamp, stepsize interval day) returns table (value timestamp)\n"
    2080             :                                         " external name generator.series;\n"
    2081             :                                         "update sys.functions set system = true where system <> true and name in ('generate_series') and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_UNION);
    2082             : 
    2083             :                         /* 51_sys_schema_extensions.sql */
    2084           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2085             :                                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    2086             :                                         "insert into sys.keywords values ('EPOCH');\n");
    2087             : 
    2088           0 :                         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    2089             : 
    2090           0 :                         assert(pos < bufsize);
    2091           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    2092           0 :                         fflush(stdout);
    2093           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2094           0 :                         if (err != MAL_SUCCEED)
    2095           0 :                                 goto bailout;
    2096             : 
    2097           0 :                         pos = snprintf(buf, bufsize,
    2098             :                                         "ALTER TABLE sys.keywords SET READ ONLY;\n");
    2099           0 :                         assert(pos < bufsize);
    2100           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    2101           0 :                         fflush(stdout);
    2102           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2103           0 :                         if (err != MAL_SUCCEED)
    2104           0 :                                 goto bailout;
    2105           0 :                         err = sql_update_storagemodel(c, sql, true); /* because of day interval addition, we have to recreate the storagmodel views */
    2106             :                 }
    2107             :         } else {
    2108           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2109             :         }
    2110             : 
    2111           0 : bailout:
    2112         106 :         BBPreclaim(b);
    2113         106 :         if (output)
    2114         106 :                 res_table_destroy(output);
    2115         106 :         GDKfree(buf);
    2116         106 :         return err;             /* usually MAL_SUCCEED */
    2117             : }
    2118             : 
    2119             : static str
    2120         106 : sql_update_oct2020_sp1(Client c, mvc *sql)
    2121             : {
    2122         106 :         size_t bufsize = 1024, pos = 0;
    2123         106 :         char *buf = NULL, *err = NULL;
    2124             : 
    2125         106 :         if (!sql_bind_func(sql, "sys", "uuid", sql_bind_localtype("int"), NULL, F_FUNC, true, true)) {
    2126           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    2127           0 :                 sql->errstr[0] = '\0';
    2128             : 
    2129           0 :                 if ((buf = GDKmalloc(bufsize)) == NULL)
    2130           0 :                         throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2131             : 
    2132             :                 /* 45_uuid.sql */
    2133           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    2134             :                         "create function sys.uuid(d int) returns uuid\n"
    2135             :                         " external name uuid.\"new\";\n"
    2136             :                         "GRANT EXECUTE ON FUNCTION sys.uuid(int) TO PUBLIC;\n"
    2137             :                         "update sys.functions set system = true where system <> true and name = 'uuid' and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_FUNC);
    2138             : 
    2139           0 :                 assert(pos < bufsize);
    2140           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    2141           0 :                 fflush(stdout);
    2142           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2143             :         }
    2144           0 :         GDKfree(buf);
    2145         106 :         return err;             /* usually MAL_SUCCEED */
    2146             : }
    2147             : 
    2148             : static str
    2149         106 : sql_update_jul2021(Client c, mvc *sql)
    2150             : {
    2151         106 :         size_t bufsize = 65536, pos = 0;
    2152         106 :         char *buf = NULL, *err = NULL;
    2153         106 :         res_table *output = NULL;
    2154         106 :         BAT *b = NULL;
    2155         106 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    2156         106 :         sql_table *t;
    2157             : 
    2158         106 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    2159           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2160             : 
    2161             :         /* if the keyword STREAM is in the list of keywords, upgrade */
    2162         106 :         pos += snprintf(buf + pos, bufsize - pos,
    2163             :                                         "select keyword from sys.keywords where keyword = 'STREAM';\n");
    2164         106 :         assert(pos < bufsize);
    2165         106 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    2166           0 :                 goto bailout;
    2167         106 :         if ((b = BATdescriptor(output->cols[0].b))) {
    2168         106 :                 if (BATcount(b) == 1) {
    2169             :                         /* 20_vacuum.sql */
    2170           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2171             :                                                         "drop procedure sys.shrink(string, string) cascade;\n"
    2172             :                                                         "drop procedure sys.reuse(string, string) cascade;\n"
    2173             :                                                         "drop procedure sys.vacuum(string, string) cascade;\n");
    2174             : 
    2175             :                         /* 22_clients.sql */
    2176           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2177             :                                                         "create function sys.current_sessionid() returns int\n"
    2178             :                                                         "external name clients.current_sessionid;\n"
    2179             :                                                         "grant execute on function sys.current_sessionid to public;\n"
    2180             :                                                         "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'current_sessionid' and type = %d;\n", (int) F_FUNC);
    2181             : 
    2182             :                         /* 25_debug.sql */
    2183           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2184             :                                                         "drop procedure sys.flush_log() cascade;\n");
    2185             : 
    2186           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2187             :                                                         "drop function sys.deltas(string) cascade;\n"
    2188             :                                                         "drop function sys.deltas(string, string) cascade;\n"
    2189             :                                                         "drop function sys.deltas(string, string, string) cascade;\n");
    2190           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2191             :                                                         "create function sys.deltas (\"schema\" string)\n"
    2192             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2193             :                                                         "external name \"sql\".\"deltas\";\n"
    2194             :                                                         "create function sys.deltas (\"schema\" string, \"table\" string)\n"
    2195             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2196             :                                                         "external name \"sql\".\"deltas\";\n"
    2197             :                                                         "create function sys.deltas (\"schema\" string, \"table\" string, \"column\" string)\n"
    2198             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2199             :                                                         "external name \"sql\".\"deltas\";\n"
    2200             :                                                         "update sys.functions set system = true"
    2201             :                                                         " where schema_id = 2000 and name = 'deltas';\n");
    2202             : 
    2203             :                         /* 26_sysmon */
    2204           0 :                         t = mvc_bind_table(sql, s, "queue");
    2205           0 :                         t->system = 0; /* make it non-system else the drop view will fail */
    2206             : 
    2207           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2208             :                                                         "drop view sys.queue cascade;\n"
    2209             :                                                         "drop function sys.queue cascade;\n"
    2210             :                                                         "create function sys.queue()\n"
    2211             :                                                         "returns table(\n"
    2212             :                                                         "\"tag\" bigint,\n"
    2213             :                                                         "\"sessionid\" int,\n"
    2214             :                                                         "\"username\" string,\n"
    2215             :                                                         "\"started\" timestamp,\n"
    2216             :                                                         "\"status\" string,\n"
    2217             :                                                         "\"query\" string,\n"
    2218             :                                                         "\"finished\" timestamp,\n"
    2219             :                                                         "\"maxworkers\" int,\n"
    2220             :                                                         "\"footprint\" int\n"
    2221             :                                                         ")\n"
    2222             :                                                         "external name sysmon.queue;\n"
    2223             :                                                         "grant execute on function sys.queue to public;\n"
    2224             :                                                         "create view sys.queue as select * from sys.queue();\n"
    2225             :                                                         "grant select on sys.queue to public;\n");
    2226           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2227             :                                                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    2228             :                                                         " and name = 'queue' and type = %d;\n", (int) F_UNION);
    2229           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2230             :                                                         "update sys._tables set system = true where schema_id = 2000"
    2231             :                                                         " and name = 'queue';\n");
    2232             : 
    2233             :                         /* fix up dependencies for function getproj4 (if it exists) */
    2234           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2235             :                                                         "delete from sys.dependencies d where d.depend_id = (select id from sys.functions where name = 'getproj4' and schema_id = 2000) and id in (select id from sys._columns where name not in ('proj4text', 'srid'));\n");
    2236             : 
    2237             :                         /* 41_json.sql */
    2238           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2239             :                                                         "drop function json.isobject(string) cascade;\n"
    2240             :                                                         "drop function json.isarray(string) cascade;\n"
    2241             :                                                         "drop function json.isvalid(json) cascade;\n"
    2242             :                                                         "create function json.isvalid(js json)\n"
    2243             :                                                         "returns bool begin return true; end;\n"
    2244             :                                                         "grant execute on function json.isvalid(json) to public;\n"
    2245             :                                                         "update sys.functions set system = true"
    2246             :                                                         " where schema_id = (select id from sys.schemas where name = 'json')"
    2247             :                                                         " and name = 'isvalid';\n");
    2248             : 
    2249             :                         /* 51_sys_schema_extensions, remove stream table entries and update window function description */
    2250           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2251             :                                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    2252             :                                         "DELETE FROM sys.keywords where keyword = 'STREAM';\n"
    2253             :                                         "INSERT INTO sys.keywords VALUES ('BIG'), ('LITTLE'), ('NATIVE'), ('ENDIAN'), ('CURRENT_SCHEMA'), ('CURRENT_TIMEZONE'), ('IMPRINTS'), ('ORDERED'), ('PATH'), ('ROLE'), ('ROW'), ('VALUE');\n"
    2254             :                                         "ALTER TABLE sys.table_types SET READ WRITE;\n"
    2255             :                                         "DELETE FROM sys.table_types where table_type_id = 4;\n"
    2256             :                                         "ALTER TABLE sys.function_types SET READ WRITE;\n"
    2257             :                                         "UPDATE sys.function_types SET function_type_keyword = 'WINDOW' WHERE function_type_id = 6;\n");
    2258             : 
    2259             :                         /* 52_describe.sql */
    2260           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2261             :                                         "CREATE FUNCTION sys.describe_type(ctype string, digits integer, tscale integer)\n"
    2262             :                                         "  RETURNS string\n"
    2263             :                                         "BEGIN\n"
    2264             :                                         "  RETURN\n"
    2265             :                                         "    CASE ctype\n"
    2266             :                                         "      WHEN 'bigint' THEN 'BIGINT'\n"
    2267             :                                         "      WHEN 'blob' THEN\n"
    2268             :                                         "        CASE digits\n"
    2269             :                                         "          WHEN 0 THEN 'BINARY LARGE OBJECT'\n"
    2270             :                                         "          ELSE 'BINARY LARGE OBJECT(' || digits || ')'\n"
    2271             :                                         "        END\n"
    2272             :                                         "      WHEN 'boolean' THEN 'BOOLEAN'\n"
    2273             :                                         "      WHEN 'char' THEN\n"
    2274             :                                         "        CASE digits\n"
    2275             :                                         "          WHEN 1 THEN 'CHARACTER'\n"
    2276             :                                         "          ELSE 'CHARACTER(' || digits || ')'\n"
    2277             :                                         "        END\n"
    2278             :                                         "      WHEN 'clob' THEN\n"
    2279             :                                         "    CASE digits\n"
    2280             :                                         "      WHEN 0 THEN 'CHARACTER LARGE OBJECT'\n"
    2281             :                                         "      ELSE 'CHARACTER LARGE OBJECT(' || digits || ')'\n"
    2282             :                                         "    END\n"
    2283             :                                         "      WHEN 'date' THEN 'DATE'\n"
    2284             :                                         "      WHEN 'day_interval' THEN 'INTERVAL DAY'\n"
    2285             :                                         "      WHEN ctype = 'decimal' THEN\n"
    2286             :                                         "      CASE\n"
    2287             :                                         "      WHEN (digits = 1 AND tscale = 0) OR digits = 0 THEN 'DECIMAL'\n"
    2288             :                                         "      WHEN tscale = 0 THEN 'DECIMAL(' || digits || ')'\n"
    2289             :                                         "      WHEN digits = 39 THEN 'DECIMAL(' || 38 || ',' || tscale || ')'\n"
    2290             :                                         "      WHEN digits = 19 AND (SELECT COUNT(*) = 0 FROM sys.types WHERE sqlname = 'hugeint' ) THEN 'DECIMAL(' || 18 || ',' || tscale || ')'\n"
    2291             :                                         "      ELSE 'DECIMAL(' || digits || ',' || tscale || ')'\n"
    2292             :                                         "    END\n"
    2293             :                                         "      WHEN 'double' THEN\n"
    2294             :                                         "    CASE\n"
    2295             :                                         "      WHEN digits = 53 and tscale = 0 THEN 'DOUBLE'\n"
    2296             :                                         "      WHEN tscale = 0 THEN 'FLOAT(' || digits || ')'\n"
    2297             :                                         "      ELSE 'FLOAT(' || digits || ',' || tscale || ')'\n"
    2298             :                                         "    END\n"
    2299             :                                         "      WHEN 'geometry' THEN\n"
    2300             :                                         "    CASE digits\n"
    2301             :                                         "      WHEN 4 THEN 'GEOMETRY(POINT' ||\n"
    2302             :                                         "            CASE tscale\n"
    2303             :                                         "              WHEN 0 THEN ''\n"
    2304             :                                         "              ELSE ',' || tscale\n"
    2305             :                                         "            END || ')'\n"
    2306             :                                         "      WHEN 8 THEN 'GEOMETRY(LINESTRING' ||\n"
    2307             :                                         "            CASE tscale\n"
    2308             :                                         "              WHEN 0 THEN ''\n"
    2309             :                                         "              ELSE ',' || tscale\n"
    2310             :                                         "            END || ')'\n"
    2311             :                                         "      WHEN 16 THEN 'GEOMETRY(POLYGON' ||\n"
    2312             :                                         "            CASE tscale\n"
    2313             :                                         "              WHEN 0 THEN ''\n"
    2314             :                                         "              ELSE ',' || tscale\n"
    2315             :                                         "            END || ')'\n"
    2316             :                                         "      WHEN 20 THEN 'GEOMETRY(MULTIPOINT' ||\n"
    2317             :                                         "            CASE tscale\n"
    2318             :                                         "              WHEN 0 THEN ''\n"
    2319             :                                         "              ELSE ',' || tscale\n"
    2320             :                                         "            END || ')'\n"
    2321             :                                         "      WHEN 24 THEN 'GEOMETRY(MULTILINESTRING' ||\n"
    2322             :                                         "            CASE tscale\n"
    2323             :                                         "              WHEN 0 THEN ''\n"
    2324             :                                         "              ELSE ',' || tscale\n"
    2325             :                                         "            END || ')'\n"
    2326             :                                         "      WHEN 28 THEN 'GEOMETRY(MULTIPOLYGON' ||\n"
    2327             :                                         "            CASE tscale\n"
    2328             :                                         "              WHEN 0 THEN ''\n"
    2329             :                                         "              ELSE ',' || tscale\n"
    2330             :                                         "            END || ')'\n"
    2331             :                                         "      WHEN 32 THEN 'GEOMETRY(GEOMETRYCOLLECTION' ||\n"
    2332             :                                         "            CASE tscale\n"
    2333             :                                         "              WHEN 0 THEN ''\n"
    2334             :                                         "              ELSE ',' || tscale\n"
    2335             :                                         "            END || ')'\n"
    2336             :                                         "      ELSE 'GEOMETRY'\n"
    2337             :                                         "        END\n"
    2338             :                                         "      WHEN 'hugeint' THEN 'HUGEINT'\n"
    2339             :                                         "      WHEN 'int' THEN 'INTEGER'\n"
    2340             :                                         "      WHEN 'month_interval' THEN\n"
    2341             :                                         "    CASE digits\n"
    2342             :                                         "      WHEN 1 THEN 'INTERVAL YEAR'\n"
    2343             :                                         "      WHEN 2 THEN 'INTERVAL YEAR TO MONTH'\n"
    2344             :                                         "      WHEN 3 THEN 'INTERVAL MONTH'\n"
    2345             :                                         "    END\n"
    2346             :                                         "      WHEN 'real' THEN\n"
    2347             :                                         "    CASE\n"
    2348             :                                         "      WHEN digits = 24 and tscale = 0 THEN 'REAL'\n"
    2349             :                                         "      WHEN tscale = 0 THEN 'FLOAT(' || digits || ')'\n"
    2350             :                                         "      ELSE 'FLOAT(' || digits || ',' || tscale || ')'\n"
    2351             :                                         "    END\n"
    2352             :                                         "      WHEN 'sec_interval' THEN\n"
    2353             :                                         "    CASE digits\n"
    2354             :                                         "      WHEN 4 THEN 'INTERVAL DAY'\n"
    2355             :                                         "      WHEN 5 THEN 'INTERVAL DAY TO HOUR'\n"
    2356             :                                         "      WHEN 6 THEN 'INTERVAL DAY TO MINUTE'\n"
    2357             :                                         "      WHEN 7 THEN 'INTERVAL DAY TO SECOND'\n"
    2358             :                                         "      WHEN 8 THEN 'INTERVAL HOUR'\n"
    2359             :                                         "      WHEN 9 THEN 'INTERVAL HOUR TO MINUTE'\n"
    2360             :                                         "      WHEN 10 THEN 'INTERVAL HOUR TO SECOND'\n"
    2361             :                                         "      WHEN 11 THEN 'INTERVAL MINUTE'\n"
    2362             :                                         "      WHEN 12 THEN 'INTERVAL MINUTE TO SECOND'\n"
    2363             :                                         "      WHEN 13 THEN 'INTERVAL SECOND'\n"
    2364             :                                         "    END\n"
    2365             :                                         "      WHEN 'smallint' THEN 'SMALLINT'\n"
    2366             :                                         "      WHEN 'time' THEN\n"
    2367             :                                         "    CASE digits\n"
    2368             :                                         "      WHEN 1 THEN 'TIME'\n"
    2369             :                                         "      ELSE 'TIME(' || (digits - 1) || ')'\n"
    2370             :                                         "    END\n"
    2371             :                                         "      WHEN 'timestamp' THEN\n"
    2372             :                                         "    CASE digits\n"
    2373             :                                         "      WHEN 7 THEN 'TIMESTAMP'\n"
    2374             :                                         "      ELSE 'TIMESTAMP(' || (digits - 1) || ')'\n"
    2375             :                                         "    END\n"
    2376             :                                         "      WHEN 'timestamptz' THEN\n"
    2377             :                                         "    CASE digits\n"
    2378             :                                         "      WHEN 7 THEN 'TIMESTAMP'\n"
    2379             :                                         "      ELSE 'TIMESTAMP(' || (digits - 1) || ')'\n"
    2380             :                                         "    END || ' WITH TIME ZONE'\n"
    2381             :                                         "      WHEN 'timetz' THEN\n"
    2382             :                                         "    CASE digits\n"
    2383             :                                         "      WHEN 1 THEN 'TIME'\n"
    2384             :                                         "      ELSE 'TIME(' || (digits - 1) || ')'\n"
    2385             :                                         "    END || ' WITH TIME ZONE'\n"
    2386             :                                         "      WHEN 'tinyint' THEN 'TINYINT'\n"
    2387             :                                         "      WHEN 'varchar' THEN 'CHARACTER VARYING(' || digits || ')'\n"
    2388             :                                         "      ELSE\n"
    2389             :                                         "        CASE\n"
    2390             :                                         "          WHEN lower(ctype) = ctype THEN upper(ctype)\n"
    2391             :                                         "          ELSE '\"' || ctype || '\"'\n"
    2392             :                                         "        END || CASE digits\n"
    2393             :                                         "      WHEN 0 THEN ''\n"
    2394             :                                         "          ELSE '(' || digits || CASE tscale\n"
    2395             :                                         "        WHEN 0 THEN ''\n"
    2396             :                                         "            ELSE ',' || tscale\n"
    2397             :                                         "          END || ')'\n"
    2398             :                                         "    END\n"
    2399             :                                         "    END;\n"
    2400             :                                         "END;\n"
    2401             :                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN ' ''' || sys.replace(s,'''','''''') || ''' '; END;\n"
    2402             :                                         "CREATE FUNCTION sys.DQ (s STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(s,'\"','\"\"') || '\"'; END; --TODO: Figure out why this breaks with the space\n"
    2403             :                                         "CREATE FUNCTION sys.FQN(s STRING, t STRING) RETURNS STRING BEGIN RETURN sys.DQ(s) || '.' || sys.DQ(t); END;\n"
    2404             :                                         "CREATE FUNCTION sys.ALTER_TABLE(s STRING, t STRING) RETURNS STRING BEGIN RETURN 'ALTER TABLE ' || sys.FQN(s, t) || ' '; END;\n"
    2405             :                                         "--We need pcre to implement a header guard which means adding the schema of an object explicitely to its identifier.\n"
    2406             :                                         "CREATE FUNCTION sys.replace_first(ori STRING, pat STRING, rep STRING, flg STRING) RETURNS STRING EXTERNAL NAME \"pcre\".\"replace_first\";\n"
    2407             :                                         "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
    2408             :                                         "RETURN\n"
    2409             :                                         "    SELECT sys.replace_first(stmt, '(\\\\s*\"?' || sch ||  '\"?\\\\s*\\\\.|)\\\\s*\"?' || nme || '\"?\\\\s*', ' ' || sys.FQN(sch, nme) || ' ', 'imsx');\n"
    2410             :                                         "END;\n"
    2411             :                                         "CREATE VIEW sys.describe_constraints AS\n"
    2412             :                                         "    SELECT\n"
    2413             :                                         "        s.name sch,\n"
    2414             :                                         "        t.name tbl,\n"
    2415             :                                         "        kc.name col,\n"
    2416             :                                         "        k.name con,\n"
    2417             :                                         "        CASE WHEN k.type = 0 THEN 'PRIMARY KEY' WHEN k.type = 1 THEN 'UNIQUE' END tpe\n"
    2418             :                                         "    FROM sys.schemas s, sys._tables t, sys.objects kc, sys.keys k\n"
    2419             :                                         "    WHERE kc.id = k.id\n"
    2420             :                                         "        AND k.table_id = t.id\n"
    2421             :                                         "        AND s.id = t.schema_id\n"
    2422             :                                         "        AND t.system = FALSE\n"
    2423             :                                         "        AND k.type in (0, 1)\n"
    2424             :                                         "        AND t.type IN (0, 6);\n"
    2425             :                                         "CREATE VIEW sys.describe_indices AS\n"
    2426             :                                         "    WITH it (id, idx) AS (VALUES (0, 'INDEX'), (4, 'IMPRINTS INDEX'), (5, 'ORDERED INDEX')) --UNIQUE INDEX wraps to INDEX.\n"
    2427             :                                         "    SELECT\n"
    2428             :                                         "        i.name ind,\n"
    2429             :                                         "        s.name sch,\n"
    2430             :                                         "        t.name tbl,\n"
    2431             :                                         "        c.name col,\n"
    2432             :                                         "        it.idx tpe\n"
    2433             :                                         "    FROM\n"
    2434             :                                         "        sys.idxs AS i LEFT JOIN sys.keys AS k ON i.name = k.name,\n"
    2435             :                                         "        sys.objects AS kc,\n"
    2436             :                                         "        sys._columns AS c,\n"
    2437             :                                         "        sys.schemas s,\n"
    2438             :                                         "        sys._tables AS t,\n"
    2439             :                                         "        it\n"
    2440             :                                         "    WHERE\n"
    2441             :                                         "        i.table_id = t.id\n"
    2442             :                                         "        AND i.id = kc.id\n"
    2443             :                                         "        AND kc.name = c.name\n"
    2444             :                                         "        AND t.id = c.table_id\n"
    2445             :                                         "        AND t.schema_id = s.id\n"
    2446             :                                         "        AND k.type IS NULL\n"
    2447             :                                         "        AND i.type = it.id\n"
    2448             :                                         "    ORDER BY i.name, kc.nr;\n"
    2449             :                                         "CREATE VIEW sys.describe_column_defaults AS\n"
    2450             :                                         "    SELECT\n"
    2451             :                                         "        s.name sch,\n"
    2452             :                                         "        t.name tbl,\n"
    2453             :                                         "        c.name col,\n"
    2454             :                                         "        c.\"default\" def\n"
    2455             :                                         "    FROM sys.schemas s, sys.tables t, sys.columns c\n"
    2456             :                                         "    WHERE\n"
    2457             :                                         "        s.id = t.schema_id AND\n"
    2458             :                                         "        t.id = c.table_id AND\n"
    2459             :                                         "        s.name <> 'tmp' AND\n"
    2460             :                                         "        NOT t.system AND\n"
    2461             :                                         "        c.\"default\" IS NOT NULL;\n"
    2462             :                                         "CREATE VIEW sys.describe_foreign_keys AS\n"
    2463             :                                         "        WITH action_type (id, act) AS (VALUES\n"
    2464             :                                         "            (0, 'NO ACTION'),\n"
    2465             :                                         "            (1, 'CASCADE'),\n"
    2466             :                                         "            (2, 'RESTRICT'),\n"
    2467             :                                         "            (3, 'SET NULL'),\n"
    2468             :                                         "            (4, 'SET DEFAULT'))\n"
    2469             :                                         "        SELECT\n"
    2470             :                                         "            fs.name fk_s,\n"
    2471             :                                         "            fkt.name fk_t,\n"
    2472             :                                         "            fkkc.name fk_c,\n"
    2473             :                                         "            fkkc.nr o,\n"
    2474             :                                         "            fkk.name fk,\n"
    2475             :                                         "            ps.name pk_s,\n"
    2476             :                                         "            pkt.name pk_t,\n"
    2477             :                                         "            pkkc.name pk_c,\n"
    2478             :                                         "            ou.act on_update,\n"
    2479             :                                         "            od.act on_delete\n"
    2480             :                                         "        FROM sys._tables fkt,\n"
    2481             :                                         "            sys.objects fkkc,\n"
    2482             :                                         "            sys.keys fkk,\n"
    2483             :                                         "            sys._tables pkt,\n"
    2484             :                                         "            sys.objects pkkc,\n"
    2485             :                                         "            sys.keys pkk,\n"
    2486             :                                         "            sys.schemas ps,\n"
    2487             :                                         "            sys.schemas fs,\n"
    2488             :                                         "            action_type ou,\n"
    2489             :                                         "            action_type od\n"
    2490             :                                         "        WHERE fkt.id = fkk.table_id\n"
    2491             :                                         "        AND pkt.id = pkk.table_id\n"
    2492             :                                         "        AND fkk.id = fkkc.id\n"
    2493             :                                         "        AND pkk.id = pkkc.id\n"
    2494             :                                         "        AND fkk.rkey = pkk.id\n"
    2495             :                                         "        AND fkkc.nr = pkkc.nr\n"
    2496             :                                         "        AND pkt.schema_id = ps.id\n"
    2497             :                                         "        AND fkt.schema_id = fs.id\n"
    2498             :                                         "        AND (fkk.\"action\" & 255)         = od.id\n"
    2499             :                                         "        AND ((fkk.\"action\" >> 8) & 255)  = ou.id\n"
    2500             :                                         "        ORDER BY fkk.name, fkkc.nr;\n"
    2501             :                                         "--TODO: CRASHES when this function gets inlined into describe_tables\n"
    2502             :                                         "CREATE FUNCTION sys.get_merge_table_partition_expressions(tid INT) RETURNS STRING\n"
    2503             :                                         "BEGIN\n"
    2504             :                                         "    RETURN\n"
    2505             :                                         "        SELECT\n"
    2506             :                                         "            CASE WHEN tp.table_id IS NOT NULL THEN    --updatable merge table\n"
    2507             :                                         "                ' PARTITION BY ' ||\n"
    2508             :                                         "                CASE\n"
    2509             :                                         "                    WHEN bit_and(tp.type, 2) = 2\n"
    2510             :                                         "                    THEN 'VALUES '\n"
    2511             :                                         "                    ELSE 'RANGE '\n"
    2512             :                                         "                END ||\n"
    2513             :                                         "                CASE\n"
    2514             :                                         "                    WHEN bit_and(tp.type, 4) = 4 --column expression\n"
    2515             :                                         "                    THEN 'ON ' || '(' || (SELECT sys.DQ(c.name) || ')' FROM sys.columns c WHERE c.id = tp.column_id)\n"
    2516             :                                         "                    ELSE 'USING ' || '(' || tp.expression || ')' --generic expression\n"
    2517             :                                         "                END\n"
    2518             :                                         "            ELSE                                    --read only partition merge table.\n"
    2519             :                                         "                ''\n"
    2520             :                                         "            END\n"
    2521             :                                         "        FROM (VALUES (tid)) t(id) LEFT JOIN sys.table_partitions tp ON t.id = tp.table_id;\n"
    2522             :                                         "END;\n"
    2523             :                                         "--TODO: gives mergejoin errors when inlined\n"
    2524             :                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    2525             :                                         "    RETURN SELECT ' ON ' || sys.SQ(uri) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(\"hash\") FROM sys.remote_table_credentials(s ||'.' || t);\n"
    2526             :                                         "END;\n"
    2527             :                                         "CREATE VIEW sys.describe_tables AS\n"
    2528             :                                         "    SELECT\n"
    2529             :                                         "        t.id o,\n"
    2530             :                                         "        s.name sch,\n"
    2531             :                                         "        t.name tab,\n"
    2532             :                                         "        ts.table_type_name typ,\n"
    2533             :                                         "        (SELECT\n"
    2534             :                                         "            ' (' ||\n"
    2535             :                                         "            GROUP_CONCAT(\n"
    2536             :                                         "                sys.DQ(c.name) || ' ' ||\n"
    2537             :                                         "                sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    2538             :                                         "                ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    2539             :                                         "            , ', ') || ')'\n"
    2540             :                                         "        FROM sys._columns c\n"
    2541             :                                         "        WHERE c.table_id = t.id) col,\n"
    2542             :                                         "        CASE\n"
    2543             :                                         "            WHEN ts.table_type_name = 'REMOTE TABLE' THEN\n"
    2544             :                                         "                sys.get_remote_table_expressions(s.name, t.name)\n"
    2545             :                                         "            WHEN ts.table_type_name = 'MERGE TABLE' THEN\n"
    2546             :                                         "                sys.get_merge_table_partition_expressions(t.id)\n"
    2547             :                                         "            WHEN ts.table_type_name = 'VIEW' THEN\n"
    2548             :                                         "                sys.schema_guard(s.name, t.name, t.query)\n"
    2549             :                                         "            ELSE\n"
    2550             :                                         "                ''\n"
    2551             :                                         "        END opt\n"
    2552             :                                         "    FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    2553             :                                         "    WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE')\n"
    2554             :                                         "        AND t.system = FALSE\n"
    2555             :                                         "        AND s.id = t.schema_id\n"
    2556             :                                         "        AND ts.table_type_id = t.type\n"
    2557             :                                         "        AND s.name <> 'tmp';\n"
    2558             :                                         "CREATE VIEW sys.describe_triggers AS\n"
    2559             :                                         "        SELECT\n"
    2560             :                                         "            s.name sch,\n"
    2561             :                                         "            t.name tab,\n"
    2562             :                                         "            tr.name tri,\n"
    2563             :                                         "            tr.statement def\n"
    2564             :                                         "        FROM sys.schemas s, sys.tables t, sys.triggers tr\n"
    2565             :                                         "        WHERE s.id = t.schema_id AND t.id = tr.table_id AND NOT t.system;\n"
    2566             :                                         "CREATE VIEW sys.describe_comments AS\n"
    2567             :                                         "        SELECT\n"
    2568             :                                         "            o.id id,\n"
    2569             :                                         "            o.tpe tpe,\n"
    2570             :                                         "            o.nme fqn,\n"
    2571             :                                         "            c.remark rem\n"
    2572             :                                         "        FROM (\n"
    2573             :                                         "            SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas\n"
    2574             :                                         "            UNION ALL\n"
    2575             :                                         "            SELECT t.id, CASE WHEN ts.table_type_name = 'VIEW' THEN 'VIEW' ELSE 'TABLE' END, sys.FQN(s.name, t.name)\n"
    2576             :                                         "            FROM sys.schemas s JOIN sys.tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
    2577             :                                         "            WHERE NOT s.name <> 'tmp'\n"
    2578             :                                         "            UNION ALL\n"
    2579             :                                         "            SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys.tables t, sys.schemas s WHERE c.table_id = t.id AND t.schema_id = s.id\n"
    2580             :                                         "            UNION ALL\n"
    2581             :                                         "            SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE idx.table_id = t.id AND t.schema_id = s.id\n"
    2582             :                                         "            UNION ALL\n"
    2583             :                                         "            SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    2584             :                                         "            UNION ALL\n"
    2585             :                                         "            SELECT f.id, ft.function_type_keyword, sys.FQN(s.name, f.name) FROM sys.functions f, sys.function_types ft, sys.schemas s WHERE f.type = ft.function_type_id AND f.schema_id = s.id\n"
    2586             :                                         "            ) AS o(id, tpe, nme)\n"
    2587             :                                         "            JOIN sys.comments c ON c.id = o.id;\n"
    2588             :                                         "CREATE VIEW sys.fully_qualified_functions AS\n"
    2589             :                                         "    WITH fqn(id, tpe, sig, num) AS\n"
    2590             :                                         "    (\n"
    2591             :                                         "        SELECT\n"
    2592             :                                         "            f.id,\n"
    2593             :                                         "            ft.function_type_keyword,\n"
    2594             :                                         "            CASE WHEN a.type IS NULL THEN\n"
    2595             :                                         "                s.name || '.' || f.name || '()'\n"
    2596             :                                         "            ELSE\n"
    2597             :                                         "                s.name || '.' || f.name || '(' || group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ',') OVER (PARTITION BY f.id ORDER BY a.number)  || ')'\n"
    2598             :                                         "            END,\n"
    2599             :                                         "            a.number\n"
    2600             :                                         "        FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
    2601             :                                         "        WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
    2602             :                                         "    )\n"
    2603             :                                         "    SELECT\n"
    2604             :                                         "        fqn1.id id,\n"
    2605             :                                         "        fqn1.tpe tpe,\n"
    2606             :                                         "        fqn1.sig nme\n"
    2607             :                                         "    FROM\n"
    2608             :                                         "        fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id)  fqn2(id, num)\n"
    2609             :                                         "        ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
    2610             :                                         "CREATE VIEW sys.describe_privileges AS\n"
    2611             :                                         "    SELECT\n"
    2612             :                                         "        CASE\n"
    2613             :                                         "            WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
    2614             :                                         "                'COPY FROM'\n"
    2615             :                                         "            WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
    2616             :                                         "                'COPY INTO'\n"
    2617             :                                         "            ELSE\n"
    2618             :                                         "                o.nme\n"
    2619             :                                         "        END o_nme,\n"
    2620             :                                         "        CASE\n"
    2621             :                                         "            WHEN o.tpe IS NOT NULL THEN\n"
    2622             :                                         "                o.tpe\n"
    2623             :                                         "            ELSE\n"
    2624             :                                         "                'GLOBAL'\n"
    2625             :                                         "        END o_tpe,\n"
    2626             :                                         "        pc.privilege_code_name p_nme,\n"
    2627             :                                         "        a.name a_nme,\n"
    2628             :                                         "        g.name g_nme,\n"
    2629             :                                         "        p.grantable grantable\n"
    2630             :                                         "    FROM\n"
    2631             :                                         "        sys.privileges p LEFT JOIN\n"
    2632             :                                         "        (\n"
    2633             :                                         "        SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
    2634             :                                         "            from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
    2635             :                                         "        UNION ALL\n"
    2636             :                                         "            SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
    2637             :                                         "            FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
    2638             :                                         "        UNION ALL\n"
    2639             :                                         "            SELECT f.id, f.nme, f.tpe\n"
    2640             :                                         "            FROM sys.fully_qualified_functions f\n"
    2641             :                                         "        ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
    2642             :                                         "        sys.privilege_codes pc,\n"
    2643             :                                         "        auths a, auths g\n"
    2644             :                                         "    WHERE\n"
    2645             :                                         "        p.privileges = pc.privilege_code_id AND\n"
    2646             :                                         "        p.auth_id = a.id AND\n"
    2647             :                                         "        p.grantor = g.id;\n"
    2648             :                                         "CREATE FUNCTION sys.describe_table(schemaName string, tableName string)\n"
    2649             :                                         "  RETURNS TABLE(name string, query string, type string, id integer, remark string)\n"
    2650             :                                         "BEGIN\n"
    2651             :                                         "    RETURN SELECT t.name, t.query, tt.table_type_name, t.id, c.remark\n"
    2652             :                                         "        FROM sys.schemas s, sys.table_types tt, sys._tables t\n"
    2653             :                                         "        LEFT OUTER JOIN sys.comments c ON t.id = c.id\n"
    2654             :                                         "            WHERE s.name = schemaName\n"
    2655             :                                         "            AND t.schema_id = s.id\n"
    2656             :                                         "            AND t.name = tableName\n"
    2657             :                                         "            AND t.type = tt.table_type_id;\n"
    2658             :                                         "END;\n"
    2659             :                                         "CREATE VIEW sys.describe_user_defined_types AS\n"
    2660             :                                         "    SELECT\n"
    2661             :                                         "        s.name sch,\n"
    2662             :                                         "        t.sqlname sql_tpe,\n"
    2663             :                                         "        t.systemname ext_tpe\n"
    2664             :                                         "    FROM sys.types t JOIN sys.schemas s ON t.schema_id = s.id\n"
    2665             :                                         "    WHERE\n"
    2666             :                                         "        t.eclass = 18 AND\n"
    2667             :                                         "        (\n"
    2668             :                                         "            (s.name = 'sys' AND t.sqlname not in ('geometrya', 'mbr', 'url', 'inet', 'json', 'uuid', 'xml')) OR\n"
    2669             :                                         "            (s.name <> 'sys')\n"
    2670             :                                         "        );\n"
    2671             :                                         "CREATE VIEW sys.describe_partition_tables AS\n"
    2672             :                                         "    SELECT \n"
    2673             :                                         "        m_sch,\n"
    2674             :                                         "        m_tbl,\n"
    2675             :                                         "        p_sch,\n"
    2676             :                                         "        p_tbl,\n"
    2677             :                                         "        CASE\n"
    2678             :                                         "            WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    2679             :                                         "            WHEN (p_raw_type = 'VALUES' AND pvalues IS NULL) OR (p_raw_type = 'RANGE' AND minimum IS NULL AND maximum IS NULL AND with_nulls) THEN 'FOR NULLS'\n"
    2680             :                                         "            ELSE p_raw_type\n"
    2681             :                                         "        END AS tpe,\n"
    2682             :                                         "        pvalues,\n"
    2683             :                                         "        minimum,\n"
    2684             :                                         "        maximum,\n"
    2685             :                                         "        with_nulls\n"
    2686             :                                         "    FROM \n"
    2687             :                                         "    (WITH\n"
    2688             :                                         "        tp(\"type\", table_id) AS\n"
    2689             :                                         "        (SELECT CASE WHEN (table_partitions.\"type\" & 2) = 2 THEN 'VALUES' ELSE 'RANGE' END, table_partitions.table_id FROM sys.table_partitions),\n"
    2690             :                                         "        subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    2691             :                                         "        (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    2692             :                                         "        FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    2693             :                                         "        WHERE m_t.\"type\" IN (3, 6)\n"
    2694             :                                         "            AND m_t.schema_id = m_s.id\n"
    2695             :                                         "            AND m_s.name <> 'tmp'\n"
    2696             :                                         "            AND m_t.system = FALSE\n"
    2697             :                                         "            AND m_t.id = d.depend_id\n"
    2698             :                                         "            AND d.id = p_m.id\n"
    2699             :                                         "            AND p_m.schema_id = p_s.id\n"
    2700             :                                         "        ORDER BY m_t.id, p_m.id)\n"
    2701             :                                         "    SELECT\n"
    2702             :                                         "        subq.m_sch,\n"
    2703             :                                         "        subq.m_tbl,\n"
    2704             :                                         "        subq.p_sch,\n"
    2705             :                                         "        subq.p_tbl,\n"
    2706             :                                         "        tp.\"type\" AS p_raw_type,\n"
    2707             :                                         "        CASE WHEN tp.\"type\" = 'VALUES'\n"
    2708             :                                         "            THEN (SELECT GROUP_CONCAT(vp.value, ',')FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid)\n"
    2709             :                                         "            ELSE NULL\n"
    2710             :                                         "        END AS pvalues,\n"
    2711             :                                         "        CASE WHEN tp.\"type\" = 'RANGE'\n"
    2712             :                                         "            THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2713             :                                         "            ELSE NULL\n"
    2714             :                                         "        END AS minimum,\n"
    2715             :                                         "        CASE WHEN tp.\"type\" = 'RANGE'\n"
    2716             :                                         "            THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2717             :                                         "            ELSE NULL\n"
    2718             :                                         "        END AS maximum,\n"
    2719             :                                         "        CASE WHEN tp.\"type\" = 'VALUES'\n"
    2720             :                                         "            THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    2721             :                                         "            ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2722             :                                         "        END AS with_nulls\n"
    2723             :                                         "    FROM \n"
    2724             :                                         "        subq LEFT OUTER JOIN tp\n"
    2725             :                                         "        ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    2726             :                                         "CREATE VIEW sys.describe_sequences AS\n"
    2727             :                                         "    SELECT\n"
    2728             :                                         "        s.name as sch,\n"
    2729             :                                         "        seq.name as seq,\n"
    2730             :                                         "        seq.\"start\" s,\n"
    2731             :                                         "        get_value_for(s.name, seq.name) AS rs,\n"
    2732             :                                         "        seq.\"minvalue\" mi,\n"
    2733             :                                         "        seq.\"maxvalue\" ma,\n"
    2734             :                                         "        seq.\"increment\" inc,\n"
    2735             :                                         "        seq.\"cacheinc\" cache,\n"
    2736             :                                         "        seq.\"cycle\" cycle\n"
    2737             :                                         "    FROM sys.sequences seq, sys.schemas s\n"
    2738             :                                         "    WHERE s.id = seq.schema_id\n"
    2739             :                                         "    AND s.name <> 'tmp'\n"
    2740             :                                         "    ORDER BY s.name, seq.name;\n"
    2741             :                                         "CREATE VIEW sys.describe_functions AS\n"
    2742             :                                         "    SELECT\n"
    2743             :                                         "        f.id o,\n"
    2744             :                                         "        s.name sch,\n"
    2745             :                                         "        f.name fun,\n"
    2746             :                                         "        f.func def\n"
    2747             :                                         "    FROM sys.functions f JOIN sys.schemas s ON f.schema_id = s.id WHERE s.name <> 'tmp' AND NOT f.system;\n"
    2748             :                                         "CREATE FUNCTION sys.describe_columns(schemaName string, tableName string)\n"
    2749             :                                         "    RETURNS TABLE(name string, type string, digits integer, scale integer, Nulls boolean, cDefault string, number integer, sqltype string, remark string)\n"
    2750             :                                         "BEGIN\n"
    2751             :                                         "    RETURN SELECT c.name, c.\"type\", c.type_digits, c.type_scale, c.\"null\", c.\"default\", c.number, sys.describe_type(c.\"type\", c.type_digits, c.type_scale), com.remark\n"
    2752             :                                         "        FROM sys._tables t, sys.schemas s, sys._columns c\n"
    2753             :                                         "        LEFT OUTER JOIN sys.comments com ON c.id = com.id\n"
    2754             :                                         "            WHERE c.table_id = t.id\n"
    2755             :                                         "            AND t.name = tableName\n"
    2756             :                                         "            AND t.schema_id = s.id\n"
    2757             :                                         "            AND s.name = schemaName\n"
    2758             :                                         "        ORDER BY c.number;\n"
    2759             :                                         "END;\n"
    2760             :                                         "CREATE FUNCTION sys.describe_function(schemaName string, functionName string)\n"
    2761             :                                         "    RETURNS TABLE(id integer, name string, type string, language string, remark string)\n"
    2762             :                                         "BEGIN\n"
    2763             :                                         "    RETURN SELECT f.id, f.name, ft.function_type_keyword, fl.language_keyword, c.remark\n"
    2764             :                                         "        FROM sys.functions f\n"
    2765             :                                         "        JOIN sys.schemas s ON f.schema_id = s.id\n"
    2766             :                                         "        JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
    2767             :                                         "        LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
    2768             :                                         "        LEFT OUTER JOIN sys.comments c ON f.id = c.id\n"
    2769             :                                         "        WHERE f.name=functionName AND s.name = schemaName;\n"
    2770             :                                         "END;\n");
    2771             : 
    2772             :                         /* 75_storagemodel.sql not changed but dependencies changed
    2773             :                          * since sys.objects has a new column */
    2774           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2775             :                                         "drop procedure sys.storagemodelinit() cascade;\n"
    2776             :                                         "create procedure sys.storagemodelinit()\n"
    2777             :                                         "begin\n"
    2778             :                                         "    delete from sys.storagemodelinput;\n"
    2779             :                                         "    insert into sys.storagemodelinput\n"
    2780             :                                         "    select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
    2781             :                                         "        -- assume all variable size types contain distinct values\n"
    2782             :                                         "        case when (\"unique\" or \"type\" IN ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
    2783             :                                         "            then \"count\" else 0 end,\n"
    2784             :                                         "        case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
    2785             :                                         "            -- string heaps have a header of 8192\n"
    2786             :                                         "            then cast((heapsize - 8192) / \"count\" as bigint)\n"
    2787             :                                         "        when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
    2788             :                                         "            -- binary data heaps have a header of 32\n"
    2789             :                                         "            then cast((heapsize - 32) / \"count\" as bigint)\n"
    2790             :                                         "        else typewidth end,\n"
    2791             :                                         "        FALSE, case sorted when true then true else false end, \"unique\", TRUE\n"
    2792             :                                         "      from sys.\"storage\";  -- view sys.\"storage\" excludes system tables (as those are not useful to be modeled for storagesize by application users)\n"
    2793             :                                         "    update sys.storagemodelinput\n"
    2794             :                                         "       set reference = TRUE\n"
    2795             :                                         "     where (\"schema\", \"table\", \"column\") in (\n"
    2796             :                                         "        SELECT fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
    2797             :                                         "          FROM    sys.\"keys\" AS fkkey,\n"
    2798             :                                         "            sys.\"objects\" AS fkkeycol,\n"
    2799             :                                         "            sys.\"tables\" AS fktable,\n"
    2800             :                                         "            sys.\"schemas\" AS fkschema\n"
    2801             :                                         "        WHERE fktable.\"id\" = fkkey.\"table_id\"\n"
    2802             :                                         "          AND fkkey.\"id\" = fkkeycol.\"id\"\n"
    2803             :                                         "          AND fkschema.\"id\" = fktable.\"schema_id\"\n"
    2804             :                                         "          AND fkkey.\"rkey\" > -1 );\n"
    2805             :                                         "    update sys.storagemodelinput\n"
    2806             :                                         "       set isacolumn = FALSE\n"
    2807             :                                         "     where (\"schema\", \"table\", \"column\") NOT in (\n"
    2808             :                                         "        SELECT sch.\"name\", tbl.\"name\", col.\"name\"\n"
    2809             :                                         "          FROM sys.\"schemas\" AS sch,\n"
    2810             :                                         "            sys.\"tables\" AS tbl,\n"
    2811             :                                         "            sys.\"columns\" AS col\n"
    2812             :                                         "        WHERE sch.\"id\" = tbl.\"schema_id\"\n"
    2813             :                                         "          AND tbl.\"id\" = col.\"table_id\");\n"
    2814             :                                         "end;\n"
    2815             :                                         "update sys.functions set system = true where name = 'storagemodelinit' and schema_id = 2000;\n");
    2816             : 
    2817             :                         /* 76_dump.sql */
    2818           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2819             :                                         "CREATE VIEW sys.dump_create_roles AS\n"
    2820             :                                         "    SELECT\n"
    2821             :                                         "        'CREATE ROLE ' || sys.dq(name) || ';' stmt FROM sys.auths\n"
    2822             :                                         "    WHERE name NOT IN (SELECT name FROM sys.db_user_info)\n"
    2823             :                                         "    AND grantor <> 0;\n"
    2824             :                                         "CREATE VIEW sys.dump_create_users AS\n"
    2825             :                                         "    SELECT\n"
    2826             :                                         "        'CREATE USER ' ||  sys.dq(ui.name) ||  ' WITH ENCRYPTED PASSWORD ' ||\n"
    2827             :                                         "        sys.sq(sys.password_hash(ui.name)) ||\n"
    2828             :                                         "    ' NAME ' || sys.sq(ui.fullname) ||  ' SCHEMA sys;' stmt\n"
    2829             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    2830             :                                         "    WHERE ui.default_schema = s.id\n"
    2831             :                                         "        AND ui.name <> 'monetdb'\n"
    2832             :                                         "        AND ui.name <> '.snapshot';\n"
    2833             :                                         "CREATE VIEW sys.dump_create_schemas AS\n"
    2834             :                                         "    SELECT\n"
    2835             :                                         "        'CREATE SCHEMA ' ||  sys.dq(s.name) || ifthenelse(a.name <> 'sysadmin', ' AUTHORIZATION ' || a.name, ' ') || ';' stmt\n"
    2836             :                                         "    FROM sys.schemas s, sys.auths a\n"
    2837             :                                         "    WHERE s.authorization = a.id AND s.system = FALSE;\n"
    2838             :                                         "CREATE VIEW sys.dump_add_schemas_to_users AS\n"
    2839             :                                         "    SELECT\n"
    2840             :                                         "        'ALTER USER ' || sys.dq(ui.name) || ' SET SCHEMA ' || sys.dq(s.name) || ';' stmt\n"
    2841             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    2842             :                                         "    WHERE ui.default_schema = s.id\n"
    2843             :                                         "        AND ui.name <> 'monetdb'\n"
    2844             :                                         "        AND ui.name <> '.snapshot'\n"
    2845             :                                         "        AND s.name <> 'sys';\n"
    2846             :                                         "CREATE VIEW sys.dump_grant_user_privileges AS\n"
    2847             :                                         "    SELECT\n"
    2848             :                                         "        'GRANT ' || sys.dq(a2.name) || ' ' || ifthenelse(a1.name = 'public', 'PUBLIC', sys.dq(a1.name)) || ';' stmt\n"
    2849             :                                         "    FROM sys.auths a1, sys.auths a2, sys.user_role ur\n"
    2850             :                                         "    WHERE a1.id = ur.login_id AND a2.id = ur.role_id;\n"
    2851             :                                         "CREATE VIEW sys.dump_table_constraint_type AS\n"
    2852             :                                         "    SELECT\n"
    2853             :                                         "        'ALTER TABLE ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    2854             :                                         "        ' ADD CONSTRAINT ' || sys.DQ(con) || ' '||\n"
    2855             :                                         "        tpe || ' (' || GROUP_CONCAT(sys.DQ(col), ', ') || ');' stmt\n"
    2856             :                                         "    FROM sys.describe_constraints GROUP BY sch, tbl, con, tpe;\n"
    2857             :                                         "CREATE VIEW sys.dump_indices AS\n"
    2858             :                                         "    SELECT\n"
    2859             :                                         "        'CREATE ' || tpe || ' ' ||\n"
    2860             :                                         "        sys.DQ(ind) || ' ON ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    2861             :                                         "        '(' || GROUP_CONCAT(col) || ');' stmt\n"
    2862             :                                         "    FROM sys.describe_indices GROUP BY ind, tpe, sch, tbl;\n"
    2863             :                                         "CREATE VIEW sys.dump_column_defaults AS\n"
    2864             :                                         "    SELECT 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ALTER COLUMN ' || sys.DQ(col) || ' SET DEFAULT ' || def || ';' stmt\n"
    2865             :                                         "    FROM sys.describe_column_defaults;\n"
    2866             :                                         "CREATE VIEW sys.dump_foreign_keys AS\n"
    2867             :                                         "    SELECT\n"
    2868             :                                         "        'ALTER TABLE ' || sys.DQ(fk_s) || '.'|| sys.DQ(fk_t) || ' ADD CONSTRAINT ' || sys.DQ(fk) || ' ' ||\n"
    2869             :                                         "        'FOREIGN KEY(' || GROUP_CONCAT(sys.DQ(fk_c), ',') ||') ' ||\n"
    2870             :                                         "        'REFERENCES ' || sys.DQ(pk_s) || '.' || sys.DQ(pk_t) || '(' || GROUP_CONCAT(sys.DQ(pk_c), ',') || ') ' ||\n"
    2871             :                                         "        'ON DELETE ' || on_delete || ' ON UPDATE ' || on_update ||\n"
    2872             :                                         "        ';' stmt\n"
    2873             :                                         "    FROM sys.describe_foreign_keys GROUP BY fk_s, fk_t, pk_s, pk_t, fk, on_delete, on_update;\n"
    2874             :                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    2875             :                                         "    SELECT\n"
    2876             :                                         "        sys.ALTER_TABLE(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    2877             :                                         "        CASE \n"
    2878             :                                         "            WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    2879             :                                         "            WHEN tpe = 'RANGE' THEN ' AS PARTITION FROM ' || ifthenelse(minimum IS NOT NULL, sys.SQ(minimum), 'RANGE MINVALUE') || ' TO ' || ifthenelse(maximum IS NOT NULL, sys.SQ(maximum), 'RANGE MAXVALUE')\n"
    2880             :                                         "            WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    2881             :                                         "            ELSE '' --'READ ONLY'\n"
    2882             :                                         "        END ||\n"
    2883             :                                         "        CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    2884             :                                         "        ';' stmt\n"
    2885             :                                         "    FROM sys.describe_partition_tables;\n"
    2886             :                                         "CREATE VIEW sys.dump_sequences AS\n"
    2887             :                                         "    SELECT\n"
    2888             :                                         "        'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT ' ||\n"
    2889             :                                         "        CASE WHEN \"s\" <> 0 THEN 'START WITH ' || \"rs\" ELSE '' END ||\n"
    2890             :                                         "        CASE WHEN \"inc\" <> 1 THEN ' INCREMENT BY ' || \"inc\" ELSE '' END ||\n"
    2891             :                                         "        CASE WHEN \"mi\" <> 0 THEN ' MINVALUE ' || \"mi\" ELSE '' END ||\n"
    2892             :                                         "        CASE WHEN \"ma\" <> 0 THEN ' MAXVALUE ' || \"ma\" ELSE '' END ||\n"
    2893             :                                         "        CASE WHEN \"cache\" <> 1 THEN ' CACHE ' || \"cache\" ELSE '' END ||\n"
    2894             :                                         "        CASE WHEN \"cycle\" THEN ' CYCLE' ELSE '' END || ';' stmt\n"
    2895             :                                         "    FROM sys.describe_sequences;\n"
    2896             :                                         "CREATE VIEW sys.dump_start_sequences AS\n"
    2897             :                                         "    SELECT\n"
    2898             :                                         "        'UPDATE sys.sequences seq SET start = ' || s  ||\n"
    2899             :                                         "        ' WHERE name = ' || sys.SQ(seq) ||\n"
    2900             :                                         "        ' AND schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = ' || sys.SQ(sch) || ');' stmt\n"
    2901             :                                         "    FROM sys.describe_sequences;\n"
    2902             :                                         "CREATE VIEW sys.dump_functions AS\n"
    2903             :                                         "    SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt FROM sys.describe_functions f;\n"
    2904             :                                         "CREATE VIEW sys.dump_tables AS\n"
    2905             :                                         "    SELECT\n"
    2906             :                                         "        t.o o,\n"
    2907             :                                         "        CASE\n"
    2908             :                                         "            WHEN t.typ <> 'VIEW' THEN\n"
    2909             :                                         "                'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    2910             :                                         "            ELSE\n"
    2911             :                                         "                t.opt\n"
    2912             :                                         "        END stmt\n"
    2913             :                                         "    FROM sys.describe_tables t;\n"
    2914             :                                         "CREATE VIEW sys.dump_triggers AS\n"
    2915             :                                         "    SELECT sys.schema_guard(sch, tab, def) stmt FROM sys.describe_triggers;\n"
    2916             :                                         "CREATE VIEW sys.dump_comments AS\n"
    2917             :                                         "    SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    2918             :                                         "CREATE VIEW sys.dump_user_defined_types AS\n"
    2919             :                                         "        SELECT 'CREATE TYPE ' || sys.FQN(sch, sql_tpe) || ' EXTERNAL NAME ' || sys.DQ(ext_tpe) || ';' stmt FROM sys.describe_user_defined_types;\n"
    2920             :                                         "CREATE VIEW sys.dump_privileges AS\n"
    2921             :                                         "    SELECT\n"
    2922             :                                         "        'INSERT INTO sys.privileges VALUES (' ||\n"
    2923             :                                         "            CASE\n"
    2924             :                                         "                WHEN dp.o_tpe = 'GLOBAL' THEN\n"
    2925             :                                         "                    '0,'\n"
    2926             :                                         "                WHEN dp.o_tpe = 'TABLE' THEN\n"
    2927             :                                         "                    '(SELECT t.id FROM sys.schemas s, sys.tables t WHERE s.id = t.schema_id' ||\n"
    2928             :                                         "                        ' AND s.name || ''.'' || t.name =' || sys.SQ(dp.o_nme) || '),'\n"
    2929             :                                         "                WHEN dp.o_tpe = 'COLUMN' THEN\n"
    2930             :                                         "                    '(SELECT c.id FROM sys.schemas s, sys.tables t, sys.columns c WHERE s.id = t.schema_id AND t.id = c.table_id' ||\n"
    2931             :                                         "                        ' AND s.name || ''.'' || t.name || ''.'' || c.name =' || sys.SQ(dp.o_nme) || '),'\n"
    2932             :                                         "                ELSE -- FUNCTION-LIKE\n"
    2933             :                                         "                    '(SELECT fqn.id FROM sys.fully_qualified_functions fqn WHERE' ||\n"
    2934             :                                         "                        ' fqn.nme = ' || sys.SQ(dp.o_nme) || ' AND fqn.tpe = ' || sys.SQ(dp.o_tpe) || '),'\n"
    2935             :                                         "            END ||\n"
    2936             :                                         "            '(SELECT id FROM sys.auths a WHERE a.name = ' || sys.SQ(dp.a_nme) || '),' ||\n"
    2937             :                                         "            '(SELECT pc.privilege_code_id FROM sys.privilege_codes pc WHERE pc.privilege_code_name = ' || sys.SQ(p_nme) || '),'\n"
    2938             :                                         "            '(SELECT id FROM sys.auths g WHERE g.name = ' || sys.SQ(dp.g_nme) || '),' ||\n"
    2939             :                                         "            dp.grantable ||\n"
    2940             :                                         "        ');' stmt\n"
    2941             :                                         "    FROM sys.describe_privileges dp;\n"
    2942             :                                         "CREATE PROCEDURE sys.EVAL(stmt STRING) EXTERNAL NAME sql.eval;\n"
    2943             :                                         "CREATE FUNCTION sys.esc(s STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(sys.replace(sys.replace(s,E'\\\\', E'\\\\\\\\'), E'\\n', E'\\\\n'), '\"', E'\\\\\"') || '\"'; END;\n"
    2944             :                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    2945             :                                         "BEGIN\n"
    2946             :                                         "    RETURN\n"
    2947             :                                         "        CASE\n"
    2948             :                                         "            WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    2949             :                                         "                'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    2950             :                                         "            ELSE\n"
    2951             :                                         "                'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    2952             :                                         "        END;\n"
    2953             :                                         "END;\n"
    2954             :                                         "CREATE TABLE sys.dump_statements(o INT, s STRING);\n"
    2955             :                                         "CREATE PROCEDURE sys._dump_table_data(sch STRING, tbl STRING) BEGIN\n"
    2956             :                                         "    DECLARE k INT;\n"
    2957             :                                         "    SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    2958             :                                         "    IF k IS NOT NULL THEN\n"
    2959             :                                         "        DECLARE cname STRING;\n"
    2960             :                                         "        DECLARE ctype STRING;\n"
    2961             :                                         "        SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    2962             :                                         "        SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    2963             :                                         "        DECLARE COPY_INTO_STMT STRING;\n"
    2964             :                                         "        DECLARE _cnt INT;\n"
    2965             :                                         "        SET _cnt = (SELECT MIN(s.count) FROM sys.storage() s WHERE s.schema = sch AND s.table = tbl);\n"
    2966             :                                         "        IF _cnt > 0 THEN\n"
    2967             :                                         "            SET COPY_INTO_STMT = 'COPY ' || _cnt ||  ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    2968             :                                         "            DECLARE SELECT_DATA_STMT STRING;\n"
    2969             :                                         "            SET SELECT_DATA_STMT = 'SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    2970             :                                         "            DECLARE M INT;\n"
    2971             :                                         "            SET M = (SELECT MAX(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    2972             :                                         "            WHILE (k < M) DO\n"
    2973             :                                         "                SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl AND c.id > k);\n"
    2974             :                                         "                SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    2975             :                                         "                SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    2976             :                                         "                SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    2977             :                                         "                SET SELECT_DATA_STMT = SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype);\n"
    2978             :                                         "            END WHILE;\n"
    2979             :                                         "            SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    2980             :                                         "            SET SELECT_DATA_STMT =  SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl);\n"
    2981             :                                         "            insert into sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    2982             :                                         "            CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    2983             :                                         "        END IF;\n"
    2984             :                                         "    END IF;\n"
    2985             :                                         "END;\n"
    2986             :                                         "CREATE PROCEDURE sys.dump_table_data() BEGIN\n"
    2987             :                                         "    DECLARE i INT;\n"
    2988             :                                         "    SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    2989             :                                         "    IF i IS NOT NULL THEN\n"
    2990             :                                         "        DECLARE M INT;\n"
    2991             :                                         "        SET M = (SELECT MAX(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    2992             :                                         "        DECLARE sch STRING;\n"
    2993             :                                         "        DECLARE tbl STRING;\n"
    2994             :                                         "        WHILE i < M DO\n"
    2995             :                                         "            set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    2996             :                                         "            set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    2997             :                                         "            CALL sys._dump_table_data(sch, tbl);\n"
    2998             :                                         "            SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system AND t.id > i);\n"
    2999             :                                         "        END WHILE;\n"
    3000             :                                         "        set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3001             :                                         "        set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3002             :                                         "        CALL sys._dump_table_data(sch, tbl);\n"
    3003             :                                         "    END IF;\n"
    3004             :                                         "END;\n"
    3005             :                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    3006             :                                         "BEGIN\n"
    3007             :                                         "    SET SCHEMA sys;\n"
    3008             :                                         "    TRUNCATE sys.dump_statements;\n"
    3009             :                                         "    INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    3010             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'SET SCHEMA \"sys\";');\n"
    3011             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    3012             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    3013             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    3014             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    3015             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    3016             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    3017             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    3018             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    3019             :                                         "    --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    3020             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    3021             :                                         "    FROM (\n"
    3022             :                                         "            SELECT * FROM sys.dump_functions f\n"
    3023             :                                         "            UNION\n"
    3024             :                                         "            SELECT * FROM sys.dump_tables t\n"
    3025             :                                         "        ) AS stmts(o, s);\n"
    3026             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    3027             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    3028             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    3029             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    3030             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    3031             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    3032             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    3033             :                                         "    --We are dumping ALL privileges so we need to erase existing privileges on the receiving side;\n"
    3034             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'TRUNCATE sys.privileges;');\n"
    3035             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_privileges;\n"
    3036             :                                         "    IF NOT DESCRIBE THEN\n"
    3037             :                                         "        CALL sys.dump_table_data();\n"
    3038             :                                         "    END IF;\n"
    3039             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    3040             :                                         "    RETURN sys.dump_statements;\n"
    3041             :                                         "END;\n");
    3042             : 
    3043             :                         // Set the system flag for the new dump and describe SQL objects.
    3044           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3045             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3046             :                                         "    system <> true AND\n"
    3047             :                                         "    schema_id = 2000 AND\n"
    3048             :                                         "    type = %d AND\n"
    3049             :                                         "    name in (\n"
    3050             :                                         "        'describe_columns',\n"
    3051             :                                         "        'describe_function',\n"
    3052             :                                         "        'describe_table',\n"
    3053             :                                         "        'dump_database'\n"
    3054             :                                         "    );\n",
    3055             :                                         F_UNION);
    3056           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3057             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3058             :                                         "    system <> true AND\n"
    3059             :                                         "    schema_id = 2000 AND\n"
    3060             :                                         "    type = %d AND\n"
    3061             :                                         "    name in (\n"
    3062             :                                         "        'alter_table',\n"
    3063             :                                         "        'describe_type',\n"
    3064             :                                         "        'dq',\n"
    3065             :                                         "        'esc',\n"
    3066             :                                         "        'fqn',\n"
    3067             :                                         "        'get_merge_table_partition_expressions',\n"
    3068             :                                         "        'get_remote_table_expressions',\n"
    3069             :                                         "        'prepare_esc',\n"
    3070             :                                         "        'replace_first',\n"
    3071             :                                         "        'schema_guard',\n"
    3072             :                                         "        'sq'\n"
    3073             :                                         "    );\n",
    3074             :                                         F_FUNC);
    3075           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3076             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3077             :                                         "    system <> true AND\n"
    3078             :                                         "    schema_id = 2000 AND\n"
    3079             :                                         "    type = %d AND\n"
    3080             :                                         "    name in (\n"
    3081             :                                         "        '_dump_table_data',\n"
    3082             :                                         "        'dump_table_data',\n"
    3083             :                                         "        'eval'\n"
    3084             :                                         "    );\n",
    3085             :                                         F_PROC);
    3086           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3087             :                                         "UPDATE sys._tables SET system = true WHERE\n"
    3088             :                                         "    system <> true AND\n"
    3089             :                                         "    schema_id = 2000 AND\n"
    3090             :                                         "    type = %d AND\n"
    3091             :                                         "    name = 'dump_statements';\n",
    3092             :                                         (int) tt_table);
    3093           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3094             :                                         "UPDATE sys._tables SET system = true WHERE\n"
    3095             :                                         "    system <> true AND\n"
    3096             :                                         "    schema_id = 2000 AND\n"
    3097             :                                         "    type = %d AND\n"
    3098             :                                         "    name in (\n"
    3099             :                                         "        'describe_column_defaults',\n"
    3100             :                                         "        'describe_comments',\n"
    3101             :                                         "        'describe_constraints',\n"
    3102             :                                         "        'describe_foreign_keys',\n"
    3103             :                                         "        'describe_functions',\n"
    3104             :                                         "        'describe_indices',\n"
    3105             :                                         "        'describe_partition_tables',\n"
    3106             :                                         "        'describe_privileges',\n"
    3107             :                                         "        'describe_sequences',\n"
    3108             :                                         "        'describe_tables',\n"
    3109             :                                         "        'describe_triggers',\n"
    3110             :                                         "        'describe_user_defined_types',\n"
    3111             :                                         "        'dump_add_schemas_to_users',\n"
    3112             :                                         "        'dump_column_defaults',\n"
    3113             :                                         "        'dump_comments',\n"
    3114             :                                         "        'dump_create_roles',\n"
    3115             :                                         "        'dump_create_schemas',\n"
    3116             :                                         "        'dump_create_users',\n"
    3117             :                                         "        'dump_foreign_keys',\n"
    3118             :                                         "        'dump_functions',\n"
    3119             :                                         "        'dump_grant_user_privileges',\n"
    3120             :                                         "        'dump_indices',\n"
    3121             :                                         "        'dump_partition_tables',\n"
    3122             :                                         "        'dump_privileges',\n"
    3123             :                                         "        'dump_sequences',\n"
    3124             :                                         "        'dump_start_sequences',\n"
    3125             :                                         "        'dump_statements',\n"
    3126             :                                         "        'dump_table_constraint_type',\n"
    3127             :                                         "        'dump_tables',\n"
    3128             :                                         "        'dump_triggers',\n"
    3129             :                                         "        'dump_user_defined_types',\n"
    3130             :                                         "        'fully_qualified_functions'\n"
    3131             :                                         "    );\n",
    3132             :                                         (int) tt_view);
    3133             : 
    3134             :                         /* scoping2 branch changes, the 'users' view has to be re-created because of the 'schema_path' addition on 'db_user_info' table
    3135             :                            However 'dependency_schemas_on_users' has a dependency on 'users', so it has to be re-created as well */
    3136           0 :                         t = mvc_bind_table(sql, s, "users");
    3137           0 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    3138           0 :                         t = mvc_bind_table(sql, s, "dependency_schemas_on_users");
    3139           0 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    3140           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3141             :                                         "DROP VIEW sys.dependency_schemas_on_users CASCADE;\n"
    3142             :                                         "DROP VIEW sys.users CASCADE;\n"
    3143             : 
    3144             :                                         "ALTER TABLE sys.db_user_info ADD COLUMN schema_path CLOB;\n"
    3145             :                                         "UPDATE sys.db_user_info SET schema_path = '\"sys\"';\n"
    3146             : 
    3147             :                                         "CREATE VIEW sys.users AS\n"
    3148             :                                         "SELECT u.\"name\" AS \"name\", ui.\"fullname\", ui.\"default_schema\", ui.\"schema_path\"\n"
    3149             :                                         " FROM sys.db_users() AS u\n"
    3150             :                                         " LEFT JOIN \"sys\".\"db_user_info\" AS ui ON u.\"name\" = ui.\"name\";\n"
    3151             :                                         "CREATE VIEW sys.dependency_schemas_on_users AS\n"
    3152             :                                         " SELECT s.id AS schema_id, s.name AS schema_name, u.name AS user_name, CAST(6 AS smallint) AS depend_type\n"
    3153             :                                         " FROM sys.users AS u, sys.schemas AS s\n"
    3154             :                                         " WHERE u.default_schema = s.id\n"
    3155             :                                         " ORDER BY s.name, u.name;\n"
    3156             :                                         "GRANT SELECT ON sys.dependency_schemas_on_users TO PUBLIC;\n"
    3157             :                                         "update sys._tables set system = true where system <> true and name in ('users','dependency_schemas_on_users')"
    3158             :                                         " and schema_id = 2000 and type = %d;\n", (int) tt_view);
    3159             : 
    3160           0 :                         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    3161             : 
    3162           0 :                         assert(pos < bufsize);
    3163           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    3164           0 :                         fflush(stdout);
    3165           0 :                         if ((err = SQLstatementIntern(c, buf, "update", true, false, NULL)) != MAL_SUCCEED)
    3166           0 :                                 goto bailout;
    3167             : 
    3168           0 :                         pos = snprintf(buf, bufsize,
    3169             :                                         "ALTER TABLE sys.keywords SET READ ONLY;\n"
    3170             :                                         "ALTER TABLE sys.table_types SET READ ONLY;\n"
    3171             :                                         "ALTER TABLE sys.function_types SET READ ONLY;\n");
    3172           0 :                         assert(pos < bufsize);
    3173           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    3174           0 :                         fflush(stdout);
    3175           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    3176             :                 }
    3177             :         } else {
    3178           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3179             :         }
    3180             : 
    3181           0 : bailout:
    3182         106 :         BBPreclaim(b);
    3183         106 :         if (output)
    3184         106 :                 res_table_destroy(output);
    3185         106 :         GDKfree(buf);
    3186         106 :         return err;             /* usually MAL_SUCCEED */
    3187             : }
    3188             : 
    3189             : /* upgrades after Jul2021_3 build */
    3190             : static str
    3191         106 : sql_update_jul2021_5(Client c, mvc *sql)
    3192             : {
    3193         106 :         size_t bufsize = 65536, pos = 0;
    3194         106 :         char *buf = NULL, *err = NULL;
    3195         106 :         res_table *output = NULL;
    3196         106 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    3197         106 :         sql_table *t;
    3198             : 
    3199         106 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    3200           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3201             : 
    3202             :         /* if the string 'partition of merge table' is not in the sys.ids
    3203             :          * query, upgrade */
    3204         106 :         pos += snprintf(buf + pos, bufsize - pos,
    3205             :                                         "select query from sys._tables where name = 'ids' and schema_id = 2000 and query like '%%partition of merge table%%';\n");
    3206         106 :         assert(pos < bufsize);
    3207         106 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)) == NULL) {
    3208         106 :                 BAT *b;
    3209         106 :                 if ((b = BATdescriptor(output->cols[0].b))) {
    3210         106 :                         if (BATcount(b) == 0) {
    3211             :                                 /* 21_dependency_views.sql */
    3212           0 :                                 t = mvc_bind_table(sql, s, "ids");
    3213           0 :                                 t->system = 0;       /* make it non-system else the drop view will fail */
    3214           0 :                                 t = mvc_bind_table(sql, s, "dependencies_vw");
    3215           0 :                                 t->system = 0;       /* make it non-system else the drop view will fail */
    3216           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3217             :                                                                 "drop view sys.dependencies_vw cascade;\n"
    3218             :                                                                 "drop view sys.ids cascade;\n");
    3219           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3220             :                                                                 "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table) AS\n"
    3221             :                                                                 "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'author' AS obj_type, 'sys.auths' AS sys_table FROM sys.auths UNION ALL\n"
    3222             :                                                                 "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'schema', 'sys.schemas' FROM sys.schemas UNION ALL\n"
    3223             :                                                                 "SELECT id, name, schema_id, id as table_id, name as table_name, case when type = 1 then 'view' else 'table' end, 'sys._tables' FROM sys._tables UNION ALL\n"
    3224             :                                                                 "SELECT id, name, schema_id, id as table_id, name as table_name, case when type = 1 then 'view' else 'table' end, 'tmp._tables' FROM tmp._tables UNION ALL\n"
    3225             :                                                                 "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, 'column', 'sys._columns' FROM sys._columns c JOIN sys._tables t ON c.table_id = t.id UNION ALL\n"
    3226             :                                                                 "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, 'column', 'tmp._columns' FROM tmp._columns c JOIN tmp._tables t ON c.table_id = t.id UNION ALL\n"
    3227             :                                                                 "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, 'key', 'sys.keys' FROM sys.keys k JOIN sys._tables t ON k.table_id = t.id UNION ALL\n"
    3228             :                                                                 "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, 'key', 'tmp.keys' FROM tmp.keys k JOIN tmp._tables t ON k.table_id = t.id UNION ALL\n"
    3229             :                                                                 "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, 'index', 'sys.idxs' FROM sys.idxs i JOIN sys._tables t ON i.table_id = t.id UNION ALL\n"
    3230             :                                                                 "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, 'index', 'tmp.idxs' FROM tmp.idxs i JOIN tmp._tables t ON i.table_id = t.id UNION ALL\n"
    3231             :                                                                 "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, 'trigger', 'sys.triggers' FROM sys.triggers g JOIN sys._tables t ON g.table_id = t.id UNION ALL\n"
    3232             :                                                                 "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, 'trigger', 'tmp.triggers' FROM tmp.triggers g JOIN tmp._tables t ON g.table_id = t.id UNION ALL\n"
    3233             :                                                                 "SELECT id, name, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, case when type = 2 then 'procedure' else 'function' end, 'sys.functions' FROM sys.functions UNION ALL\n"
    3234             :                                                                 "SELECT a.id, a.name, f.schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, case when f.type = 2 then 'procedure arg' else 'function arg' end, 'sys.args' FROM sys.args a JOIN sys.functions f ON a.func_id = f.id UNION ALL\n"
    3235             :                                                                 "SELECT id, name, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'sequence', 'sys.sequences' FROM sys.sequences UNION ALL\n"
    3236             :                                                                 "SELECT o.id, o.name, pt.schema_id, pt.id, pt.name, 'partition of merge table', 'sys.objects' FROM sys.objects o JOIN sys._tables pt ON o.sub = pt.id JOIN sys._tables mt ON o.nr = mt.id WHERE mt.type = 3 UNION ALL\n"
    3237             :                                                                 "SELECT id, sqlname, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'type', 'sys.types' FROM sys.types WHERE id > 2000\n"
    3238             :                                                                 " ORDER BY id;\n"
    3239             :                                                                 "GRANT SELECT ON sys.ids TO PUBLIC;\n");
    3240           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3241             :                                                                 "CREATE VIEW sys.dependencies_vw AS\n"
    3242             :                                                                 "SELECT d.id, i1.obj_type, i1.name,\n"
    3243             :                                                                 "       d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
    3244             :                                                                 "       d.depend_type, dt.dependency_type_name\n"
    3245             :                                                                 "  FROM sys.dependencies d\n"
    3246             :                                                                 "  JOIN sys.ids i1 ON d.id = i1.id\n"
    3247             :                                                                 "  JOIN sys.ids i2 ON d.depend_id = i2.id\n"
    3248             :                                                                 "  JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
    3249             :                                                                 " ORDER BY id, depend_id;\n"
    3250             :                                                                 "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n");
    3251           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3252             :                                                                 "UPDATE sys._tables SET system = true WHERE name in ('ids', 'dependencies_vw') AND schema_id = 2000;\n");
    3253             : 
    3254           0 :                                 assert(pos < bufsize);
    3255           0 :                                 printf("Running database upgrade commands:\n%s\n", buf);
    3256           0 :                                 fflush(stdout);
    3257           0 :                                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    3258             :                         }
    3259         106 :                         BBPunfix(b->batCacheid);
    3260             :                 } else {
    3261           0 :                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3262             :                 }
    3263         106 :                 res_table_destroy(output);
    3264             :         }
    3265             : 
    3266         106 :         GDKfree(buf);
    3267         106 :         return err;             /* usually MAL_SUCCEED */
    3268             : }
    3269             : 
    3270             : static str
    3271         106 : sql_update_jan2022(Client c, mvc *sql)
    3272             : {
    3273         106 :         sql_subtype tp;
    3274         106 :         size_t bufsize = 65536, pos = 0;
    3275         106 :         char *buf = NULL, *err = NULL;
    3276         106 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    3277         106 :         sql_table *t;
    3278             : 
    3279             :         /* this bit of code is to upgrade from a Jan2022 RC to the Jan2022 release */
    3280         106 :         allocator *old_sa = sql->sa;
    3281         106 :         if ((sql->sa = sa_create(sql->pa)) != NULL) {
    3282         106 :                 list *l;
    3283         106 :                 if ((l = sa_list(sql->sa)) != NULL) {
    3284         106 :                         sql_find_subtype(&tp, "varchar", 0, 0);
    3285         106 :                         list_append(l, &tp);
    3286         106 :                         list_append(l, &tp);
    3287         106 :                         list_append(l, &tp);
    3288         106 :                         if (sql_bind_func_(sql, s->base.name, "strimp_create", l, F_PROC, true, true)) {
    3289             :                                 /* do the upgrade by removing the two functions */
    3290           0 :                                 const char *query =
    3291             :                                         "drop filter function sys.strimp_filter(string, string) cascade;\n"
    3292             :                                         "drop procedure sys.strimp_create(string, string, string) cascade;\n";
    3293           0 :                                 printf("Running database upgrade commands:\n%s\n", query);
    3294           0 :                                 fflush(stdout);
    3295           0 :                                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    3296             :                         }
    3297         106 :                         sql->session->status = 0; /* if the function was not found clean the error */
    3298         106 :                         sql->errstr[0] = '\0';
    3299             :                 }
    3300         106 :                 sa_destroy(sql->sa);
    3301         106 :                 if (err)
    3302             :                         return err;
    3303             :         }
    3304         106 :         sql->sa = old_sa;
    3305             : 
    3306         106 :         sql_find_subtype(&tp, "bigint", 0, 0);
    3307         106 :         if (!sql_bind_func(sql, s->base.name, "epoch", &tp, NULL, F_FUNC, true, true)) {
    3308         106 :                 sql->session->status = 0; /* if the function was not found clean the error */
    3309         106 :                 sql->errstr[0] = '\0';
    3310             :                 /* nothing to do */
    3311         106 :                 return NULL;
    3312             :         }
    3313             : 
    3314           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    3315           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3316             : 
    3317             :         /* sys.epoch_ms now returns a decimal(18,3) */
    3318           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3319             :                                         "update sys.args set type = 'decimal', type_digits = 18, type_scale = 3 where func_id in (select id from sys.functions where name = 'epoch_ms' and schema_id = 2000) and number = 0 and type = 'bigint';\n");
    3320             : 
    3321             :         /* 16_tracelog */
    3322           0 :         t = mvc_bind_table(sql, s, "tracelog");
    3323           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    3324           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3325             :                         "drop view sys.tracelog cascade;\n"
    3326             :                         "drop function sys.tracelog() cascade;\n"
    3327             :                         "create function sys.tracelog()\n"
    3328             :                         " returns table (\n"
    3329             :                         "  ticks bigint, -- time in microseconds\n"
    3330             :                         "  stmt string,  -- actual statement executed\n"
    3331             :                         "  event string  -- profiler event executed\n"
    3332             :                         " )\n"
    3333             :                         " external name sql.dump_trace;\n"
    3334             :                         "create view sys.tracelog as select * from sys.tracelog();\n"
    3335             :                         "update sys._tables set system = true where system <> true and schema_id = 2000"
    3336             :                         " and name = 'tracelog';\n"
    3337             :                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    3338             :                         " and name = 'tracelog' and type = %d;\n", (int) F_UNION);
    3339             : 
    3340             :         /* 17_temporal.sql */
    3341           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3342             :                                         "drop function sys.epoch(bigint) cascade;\n");
    3343           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3344             :                                         "create function sys.epoch(sec DECIMAL(18,3)) "
    3345             :                                         "returns TIMESTAMP WITH TIME ZONE\n"
    3346             :                                         "external name mtime.epoch;\n"
    3347             :                                         "grant execute on function sys.epoch (DECIMAL(18,3)) to public;\n"
    3348             :                                         "update sys.functions set system = true where system <> true and name in ('epoch') and schema_id = 2000 and type = %d;\n", F_FUNC);
    3349             : 
    3350             :         /* 25_debug.sql */
    3351           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3352             :                                         "drop function sys.malfunctions() cascade;\n"
    3353             :                                         "create function sys.malfunctions()\n"
    3354             :                                         " returns table(\"module\" string, \"function\" string, \"signature\" string, \"address\" string, \"comment\" string)\n"
    3355             :                                         " external name \"manual\".\"functions\";\n"
    3356             :                                         "create view sys.malfunctions as select * from sys.malfunctions();\n"
    3357             :                                         "update sys._tables set system = true where system <> true and schema_id = 2000"
    3358             :                                         " and name = 'malfunctions';\n"
    3359             :                                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    3360             :                                         " and name = 'malfunctions';\n");
    3361             : 
    3362             :         /* 21_dependency_views.sql */
    3363           0 :         t = mvc_bind_table(sql, s, "ids");
    3364           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    3365           0 :         t = mvc_bind_table(sql, s, "dependencies_vw");
    3366           0 :         t->system = 0;       /* make it non-system else the drop view will fail */
    3367           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3368             :                                         "drop view sys.dependencies_vw cascade;\n" /* depends on sys.ids */
    3369             :                                         "drop view sys.ids cascade;\n"
    3370             :                                         "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table, system) AS\n"
    3371             :                                         "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'author' AS obj_type, 'sys.auths' AS sys_table, (name in ('public','sysadmin','monetdb','.snapshot')) AS system FROM sys.auths UNION ALL\n"
    3372             :                                         "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, ifthenelse(system, 'system schema', 'schema'), 'sys.schemas', system FROM sys.schemas UNION ALL\n"
    3373             :                                         "SELECT t.id, name, t.schema_id, t.id as table_id, t.name as table_name, cast(lower(tt.table_type_name) as varchar(40)), 'sys.tables', t.system FROM sys.tables t left outer join sys.table_types tt on t.type = tt.table_type_id UNION ALL\n"
    3374             :                                         "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, ifthenelse(t.system, 'system column', 'column'), 'sys._columns', t.system FROM sys._columns c JOIN sys._tables t ON c.table_id = t.id UNION ALL\n"
    3375             :                                         "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, 'column', 'tmp._columns', t.system FROM tmp._columns c JOIN tmp._tables t ON c.table_id = t.id UNION ALL\n"
    3376             :                                         "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, ifthenelse(t.system, 'system key', 'key'), 'sys.keys', t.system FROM sys.keys k JOIN sys._tables t ON k.table_id = t.id UNION ALL\n"
    3377             :                                         "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, 'key', 'tmp.keys', t.system FROM tmp.keys k JOIN tmp._tables t ON k.table_id = t.id UNION ALL\n"
    3378             :                                         "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, ifthenelse(t.system, 'system index', 'index'), 'sys.idxs', t.system FROM sys.idxs i JOIN sys._tables t ON i.table_id = t.id UNION ALL\n"
    3379             :                                         "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, 'index' , 'tmp.idxs', t.system FROM tmp.idxs i JOIN tmp._tables t ON i.table_id = t.id UNION ALL\n"
    3380             :                                         "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, ifthenelse(t.system, 'system trigger', 'trigger'), 'sys.triggers', t.system FROM sys.triggers g JOIN sys._tables t ON g.table_id = t.id UNION ALL\n"
    3381             :                                         "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, 'trigger', 'tmp.triggers', t.system FROM tmp.triggers g JOIN tmp._tables t ON g.table_id = t.id UNION ALL\n"
    3382             :                                         "SELECT f.id, f.name, f.schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, cast(ifthenelse(f.system, 'system ', '') || lower(ft.function_type_keyword) as varchar(40)), 'sys.functions', f.system FROM sys.functions f left outer join sys.function_types ft on f.type = ft.function_type_id UNION ALL\n"
    3383             :                                         "SELECT a.id, a.name, f.schema_id, a.func_id as table_id, f.name as table_name, cast(ifthenelse(f.system, 'system ', '') || lower(ft.function_type_keyword) || ' arg' as varchar(44)), 'sys.args', f.system FROM sys.args a JOIN sys.functions f ON a.func_id = f.id left outer join sys.function_types ft on f.type = ft.function_type_id UNION ALL\n"
    3384             :                                         "SELECT id, name, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'sequence', 'sys.sequences', false FROM sys.sequences UNION ALL\n"
    3385             :                                         "SELECT o.id, o.name, pt.schema_id, pt.id, pt.name, 'partition of merge table', 'sys.objects', false FROM sys.objects o JOIN sys._tables pt ON o.sub = pt.id JOIN sys._tables mt ON o.nr = mt.id WHERE mt.type = 3 UNION ALL\n"
    3386             :                                         "SELECT id, sqlname, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'type', 'sys.types', (sqlname in ('inet','json','url','uuid')) FROM sys.types WHERE id > 2000\n"
    3387             :                                         " ORDER BY id;\n"
    3388             :                                         "GRANT SELECT ON sys.ids TO PUBLIC;\n");
    3389           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3390             :                                         "CREATE VIEW sys.dependencies_vw AS\n"
    3391             :                                         "SELECT d.id, i1.obj_type, i1.name,\n"
    3392             :                                         "       d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
    3393             :                                         "       d.depend_type, dt.dependency_type_name\n"
    3394             :                                         "  FROM sys.dependencies d\n"
    3395             :                                         "  JOIN sys.ids i1 ON d.id = i1.id\n"
    3396             :                                         "  JOIN sys.ids i2 ON d.depend_id = i2.id\n"
    3397             :                                         "  JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
    3398             :                                         " ORDER BY id, depend_id;\n"
    3399             :                                         "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n");
    3400           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3401             :                                         "UPDATE sys._tables SET system = true WHERE name in ('ids', 'dependencies_vw') AND schema_id = 2000;\n");
    3402             : 
    3403             :         /* 52_describe.sql; but we need to drop most everything from
    3404             :          * 76_dump.sql first */
    3405           0 :         t = mvc_bind_table(sql, s, "describe_comments");
    3406           0 :         t->system = 0;
    3407           0 :         t = mvc_bind_table(sql, s, "describe_constraints");
    3408           0 :         t->system = 0;
    3409           0 :         t = mvc_bind_table(sql, s, "describe_functions");
    3410           0 :         t->system = 0;
    3411           0 :         t = mvc_bind_table(sql, s, "describe_partition_tables");
    3412           0 :         t->system = 0;
    3413           0 :         t = mvc_bind_table(sql, s, "describe_privileges");
    3414           0 :         t->system = 0;
    3415           0 :         t = mvc_bind_table(sql, s, "describe_sequences");
    3416           0 :         t->system = 0;
    3417           0 :         t = mvc_bind_table(sql, s, "describe_tables");
    3418           0 :         t->system = 0;
    3419           0 :         t = mvc_bind_table(sql, s, "dump_add_schemas_to_users");
    3420           0 :         t->system = 0;
    3421           0 :         t = mvc_bind_table(sql, s, "dump_column_defaults");
    3422           0 :         t->system = 0;
    3423           0 :         t = mvc_bind_table(sql, s, "dump_comments");
    3424           0 :         t->system = 0;
    3425           0 :         t = mvc_bind_table(sql, s, "dump_create_roles");
    3426           0 :         t->system = 0;
    3427           0 :         t = mvc_bind_table(sql, s, "dump_create_schemas");
    3428           0 :         t->system = 0;
    3429           0 :         t = mvc_bind_table(sql, s, "dump_create_users");
    3430           0 :         t->system = 0;
    3431           0 :         t = mvc_bind_table(sql, s, "dump_foreign_keys");
    3432           0 :         t->system = 0;
    3433           0 :         t = mvc_bind_table(sql, s, "dump_functions");
    3434           0 :         t->system = 0;
    3435           0 :         t = mvc_bind_table(sql, s, "dump_grant_user_privileges");
    3436           0 :         t->system = 0;
    3437           0 :         t = mvc_bind_table(sql, s, "dump_indices");
    3438           0 :         t->system = 0;
    3439           0 :         t = mvc_bind_table(sql, s, "dump_partition_tables");
    3440           0 :         t->system = 0;
    3441           0 :         t = mvc_bind_table(sql, s, "dump_privileges");
    3442           0 :         t->system = 0;
    3443           0 :         t = mvc_bind_table(sql, s, "dump_sequences");
    3444           0 :         t->system = 0;
    3445           0 :         t = mvc_bind_table(sql, s, "dump_start_sequences");
    3446           0 :         t->system = 0;
    3447           0 :         t = mvc_bind_table(sql, s, "dump_table_constraint_type");
    3448           0 :         t->system = 0;
    3449           0 :         t = mvc_bind_table(sql, s, "dump_tables");
    3450           0 :         t->system = 0;
    3451           0 :         t = mvc_bind_table(sql, s, "dump_triggers");
    3452           0 :         t->system = 0;
    3453           0 :         t = mvc_bind_table(sql, s, "dump_user_defined_types");
    3454           0 :         t->system = 0;
    3455           0 :         t = mvc_bind_table(sql, s, "fully_qualified_functions");
    3456           0 :         t->system = 0;
    3457           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3458             :                                         /* drop dependant stuff from 76_dump.sql */
    3459             :                                         "drop function sys.dump_database(boolean) cascade;\n"
    3460             :                                         "drop procedure sys.dump_table_data() cascade;\n"
    3461             :                                         "drop procedure sys._dump_table_data(string, string) cascade;\n"
    3462             :                                         "drop function sys.prepare_esc(string, string) cascade;\n"
    3463             :                                         "drop function sys.esc(string) cascade;\n"
    3464             :                                         "drop view sys.dump_privileges cascade;\n"
    3465             :                                         "drop view sys.dump_user_defined_types cascade;\n"
    3466             :                                         "drop view sys.dump_comments cascade;\n"
    3467             :                                         "drop view sys.dump_triggers cascade;\n"
    3468             :                                         "drop view sys.dump_tables cascade;\n"
    3469             :                                         "drop view sys.dump_functions cascade;\n"
    3470             :                                         "drop view sys.dump_start_sequences cascade;\n"
    3471             :                                         "drop view sys.dump_sequences cascade;\n"
    3472             :                                         "drop view sys.dump_partition_tables cascade;\n"
    3473             :                                         "drop view sys.dump_foreign_keys cascade;\n"
    3474             :                                         "drop view sys.dump_column_defaults cascade;\n"
    3475             :                                         "drop view sys.dump_indices cascade;\n"
    3476             :                                         "drop view sys.dump_table_constraint_type cascade;\n"
    3477             :                                         "drop view sys.dump_grant_user_privileges cascade;\n"
    3478             :                                         "drop view sys.dump_add_schemas_to_users cascade;\n"
    3479             :                                         "drop view sys.dump_create_schemas cascade;\n"
    3480             :                                         "drop view sys.dump_create_users cascade;\n"
    3481             :                                         "drop view sys.dump_create_roles cascade;\n"
    3482             : 
    3483             :                                         "drop view sys.describe_functions cascade;\n"
    3484             :                                         "drop view sys.describe_partition_tables cascade;\n"
    3485             :                                         "drop view sys.describe_privileges cascade;\n"
    3486             :                                         "drop view sys.fully_qualified_functions cascade;\n"
    3487             :                                         "drop view sys.describe_comments cascade;\n"
    3488             :                                         "drop view sys.describe_tables cascade;\n"
    3489             :                                         "drop view sys.describe_sequences cascade;\n"
    3490             :                                         "drop function sys.schema_guard(string, string, string) cascade;\n"
    3491             :                                         "drop function sys.get_remote_table_expressions(string, string) cascade;\n"
    3492             :                                         "drop function sys.get_merge_table_partition_expressions(int) cascade;\n"
    3493             :                                         "drop view sys.describe_constraints cascade;\n"
    3494             :                                         "drop function sys.alter_table(string, string) cascade;\n"
    3495             :                                         "drop function sys.FQN(string, string) cascade;\n"
    3496             :                                         "drop function sys.sq(string) cascade;\n");
    3497           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3498             :                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN '''' || sys.replace(s,'''','''''') || ''''; END;\n"
    3499             :                                         "CREATE FUNCTION sys.FQN(s STRING, t STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(s,'\"','\"\"') || '\".\"' || sys.replace(t,'\"','\"\"') || '\"'; END;\n"
    3500             :                                         "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
    3501             :                                         "RETURN\n"
    3502             :                                         "    SELECT sys.replace_first(stmt, '(\\\\s*\"?' || sch ||  '\"?\\\\s*\\\\.|)\\\\s*\"?' || nme || '\"?\\\\s*', ' ' || sys.FQN(sch, nme) || ' ', 'imsx');\n"
    3503             :                                         "END;\n"
    3504             :                                         "CREATE VIEW sys.describe_constraints AS\n"
    3505             :                                         "  SELECT\n"
    3506             :                                         "          s.name sch,\n"
    3507             :                                         "          t.name tbl,\n"
    3508             :                                         "          kc.name col,\n"
    3509             :                                         "          k.name con,\n"
    3510             :                                         "          CASE k.type WHEN 0 THEN 'PRIMARY KEY' WHEN 1 THEN 'UNIQUE' END tpe\n"
    3511             :                                         "  FROM sys.schemas s, sys._tables t, sys.objects kc, sys.keys k\n"
    3512             :                                         "  WHERE kc.id = k.id\n"
    3513             :                                         "          AND k.table_id = t.id\n"
    3514             :                                         "          AND s.id = t.schema_id\n"
    3515             :                                         "          AND t.system = FALSE\n"
    3516             :                                         "          AND k.type in (0, 1);\n"
    3517             :                                         "CREATE FUNCTION sys.get_merge_table_partition_expressions(tid INT) RETURNS STRING\n"
    3518             :                                         "BEGIN\n"
    3519             :                                         "  RETURN\n"
    3520             :                                         "          SELECT\n"
    3521             :                                         "                  CASE WHEN tp.table_id IS NOT NULL THEN\n"
    3522             :                                         "                          ' PARTITION BY ' ||\n"
    3523             :                                         "                          ifthenelse(bit_and(tp.type, 2) = 2, 'VALUES ', 'RANGE ') ||\n"
    3524             :                                         "                          CASE\n"
    3525             :                                         "                                  WHEN bit_and(tp.type, 4) = 4\n"
    3526             :                                         "                                  THEN 'ON ' || '(' || (SELECT sys.DQ(c.name) || ')' FROM sys.columns c WHERE c.id = tp.column_id)\n"
    3527             :                                         "                                  ELSE 'USING ' || '(' || tp.expression || ')'\n"
    3528             :                                         "                          END\n"
    3529             :                                         "                  ELSE\n"
    3530             :                                         "                          ''\n"
    3531             :                                         "                  END\n"
    3532             :                                         "          FROM (VALUES (tid)) t(id) LEFT JOIN sys.table_partitions tp ON t.id = tp.table_id;\n"
    3533             :                                         "END;\n"
    3534             :                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    3535             :                                         "  RETURN SELECT ' ON ' || sys.SQ(uri) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(\"hash\") FROM sys.remote_table_credentials(s ||'.' || t);\n"
    3536             :                                         "END;\n"
    3537             :                                         "CREATE VIEW sys.describe_tables AS\n"
    3538             :                                         "  SELECT\n"
    3539             :                                         "          t.id o,\n"
    3540             :                                         "          s.name sch,\n"
    3541             :                                         "          t.name tab,\n"
    3542             :                                         "          ts.table_type_name typ,\n"
    3543             :                                         "          (SELECT\n"
    3544             :                                         "                  ' (' ||\n"
    3545             :                                         "                  GROUP_CONCAT(\n"
    3546             :                                         "                          sys.DQ(c.name) || ' ' ||\n"
    3547             :                                         "                          sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    3548             :                                         "                          ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    3549             :                                         "                  , ', ') || ')'\n"
    3550             :                                         "          FROM sys._columns c\n"
    3551             :                                         "          WHERE c.table_id = t.id) col,\n"
    3552             :                                         "          CASE ts.table_type_name\n"
    3553             :                                         "                  WHEN 'REMOTE TABLE' THEN\n"
    3554             :                                         "                          sys.get_remote_table_expressions(s.name, t.name)\n"
    3555             :                                         "                  WHEN 'MERGE TABLE' THEN\n"
    3556             :                                         "                          sys.get_merge_table_partition_expressions(t.id)\n"
    3557             :                                         "                  WHEN 'VIEW' THEN\n"
    3558             :                                         "                          sys.schema_guard(s.name, t.name, t.query)\n"
    3559             :                                         "                  ELSE\n"
    3560             :                                         "                          ''\n"
    3561             :                                         "          END opt\n"
    3562             :                                         "  FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    3563             :                                         "  WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE')\n"
    3564             :                                         "          AND t.system = FALSE\n"
    3565             :                                         "          AND s.id = t.schema_id\n"
    3566             :                                         "          AND ts.table_type_id = t.type\n"
    3567             :                                         "          AND s.name <> 'tmp';\n"
    3568             :                                         "CREATE VIEW sys.fully_qualified_functions AS\n"
    3569             :                                         "  WITH fqn(id, tpe, sig, num) AS\n"
    3570             :                                         "  (\n"
    3571             :                                         "          SELECT\n"
    3572             :                                         "                  f.id,\n"
    3573             :                                         "                  ft.function_type_keyword,\n"
    3574             :                                         "                  CASE WHEN a.type IS NULL THEN\n"
    3575             :                                         "                          sys.fqn(s.name, f.name) || '()'\n"
    3576             :                                         "                  ELSE\n"
    3577             :                                         "                          sys.fqn(s.name, f.name) || '(' || group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ',') OVER (PARTITION BY f.id ORDER BY a.number)  || ')'\n"
    3578             :                                         "                  END,\n"
    3579             :                                         "                  a.number\n"
    3580             :                                         "          FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
    3581             :                                         "          WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
    3582             :                                         "  )\n"
    3583             :                                         "  SELECT\n"
    3584             :                                         "          fqn1.id id,\n"
    3585             :                                         "          fqn1.tpe tpe,\n"
    3586             :                                         "          fqn1.sig nme\n"
    3587             :                                         "  FROM\n"
    3588             :                                         "          fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id)  fqn2(id, num)\n"
    3589             :                                         "          ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
    3590             :                                         "CREATE VIEW sys.describe_comments AS\n"
    3591             :                                         "          SELECT\n"
    3592             :                                         "                  o.id id,\n"
    3593             :                                         "                  o.tpe tpe,\n"
    3594             :                                         "                  o.nme fqn,\n"
    3595             :                                         "                  c.remark rem\n"
    3596             :                                         "          FROM (\n"
    3597             :                                         "                  SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas\n"
    3598             :                                         "                  UNION ALL\n"
    3599             :                                         "                  SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
    3600             :                                         "                  FROM sys.schemas s JOIN sys.tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
    3601             :                                         "                  WHERE s.name <> 'tmp'\n"
    3602             :                                         "                  UNION ALL\n"
    3603             :                                         "                  SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys.tables t, sys.schemas s WHERE c.table_id = t.id AND t.schema_id = s.id\n"
    3604             :                                         "                  UNION ALL\n"
    3605             :                                         "                  SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE idx.table_id = t.id AND t.schema_id = s.id\n"
    3606             :                                         "                  UNION ALL\n"
    3607             :                                         "                  SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    3608             :                                         "                  UNION ALL\n"
    3609             :                                         "                  SELECT f.id, ft.function_type_keyword, qf.nme FROM sys.functions f, sys.function_types ft, sys.schemas s, sys.fully_qualified_functions qf WHERE f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
    3610             :                                         "                  ) AS o(id, tpe, nme)\n"
    3611             :                                         "                  JOIN sys.comments c ON c.id = o.id;\n"
    3612             :                                         "CREATE VIEW sys.describe_privileges AS\n"
    3613             :                                         "  SELECT\n"
    3614             :                                         "          CASE\n"
    3615             :                                         "                  WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
    3616             :                                         "                          'COPY FROM'\n"
    3617             :                                         "                  WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
    3618             :                                         "                          'COPY INTO'\n"
    3619             :                                         "                  ELSE\n"
    3620             :                                         "                          o.nme\n"
    3621             :                                         "          END o_nme,\n"
    3622             :                                         "          coalesce(o.tpe, 'GLOBAL') o_tpe,\n"
    3623             :                                         "          pc.privilege_code_name p_nme,\n"
    3624             :                                         "          a.name a_nme,\n"
    3625             :                                         "          g.name g_nme,\n"
    3626             :                                         "          p.grantable grantable\n"
    3627             :                                         "  FROM\n"
    3628             :                                         "          sys.privileges p LEFT JOIN\n"
    3629             :                                         "          (\n"
    3630             :                                         "          SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
    3631             :                                         "                  from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
    3632             :                                         "          UNION ALL\n"
    3633             :                                         "                  SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
    3634             :                                         "                  FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
    3635             :                                         "          UNION ALL\n"
    3636             :                                         "                  SELECT f.id, f.nme, f.tpe\n"
    3637             :                                         "                  FROM sys.fully_qualified_functions f\n"
    3638             :                                         "          ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
    3639             :                                         "          sys.privilege_codes pc,\n"
    3640             :                                         "          auths a, auths g\n"
    3641             :                                         "  WHERE\n"
    3642             :                                         "          p.privileges = pc.privilege_code_id AND\n"
    3643             :                                         "          p.auth_id = a.id AND\n"
    3644             :                                         "          p.grantor = g.id;\n"
    3645             :                                         "CREATE VIEW sys.describe_partition_tables AS\n"
    3646             :                                         "  SELECT \n"
    3647             :                                         "          m_sch,\n"
    3648             :                                         "          m_tbl,\n"
    3649             :                                         "          p_sch,\n"
    3650             :                                         "          p_tbl,\n"
    3651             :                                         "          CASE\n"
    3652             :                                         "                  WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    3653             :                                         "                  WHEN (p_raw_type = 'VALUES' AND pvalues IS NULL) OR (p_raw_type = 'RANGE' AND minimum IS NULL AND maximum IS NULL AND with_nulls) THEN 'FOR NULLS'\n"
    3654             :                                         "                  ELSE p_raw_type\n"
    3655             :                                         "          END AS tpe,\n"
    3656             :                                         "          pvalues,\n"
    3657             :                                         "          minimum,\n"
    3658             :                                         "          maximum,\n"
    3659             :                                         "          with_nulls\n"
    3660             :                                         "  FROM \n"
    3661             :                                         "    (WITH\n"
    3662             :                                         "          tp(\"type\", table_id) AS\n"
    3663             :                                         "          (SELECT ifthenelse((table_partitions.\"type\" & 2) = 2, 'VALUES', 'RANGE'), table_partitions.table_id FROM sys.table_partitions),\n"
    3664             :                                         "          subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    3665             :                                         "          (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    3666             :                                         "          FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    3667             :                                         "          WHERE m_t.\"type\" IN (3, 6)\n"
    3668             :                                         "                  AND m_t.schema_id = m_s.id\n"
    3669             :                                         "                  AND m_s.name <> 'tmp'\n"
    3670             :                                         "                  AND m_t.system = FALSE\n"
    3671             :                                         "                  AND m_t.id = d.depend_id\n"
    3672             :                                         "                  AND d.id = p_m.id\n"
    3673             :                                         "                  AND p_m.schema_id = p_s.id\n"
    3674             :                                         "          ORDER BY m_t.id, p_m.id)\n"
    3675             :                                         "  SELECT\n"
    3676             :                                         "          subq.m_sch,\n"
    3677             :                                         "          subq.m_tbl,\n"
    3678             :                                         "          subq.p_sch,\n"
    3679             :                                         "          subq.p_tbl,\n"
    3680             :                                         "          tp.\"type\" AS p_raw_type,\n"
    3681             :                                         "          CASE WHEN tp.\"type\" = 'VALUES'\n"
    3682             :                                         "                  THEN (SELECT GROUP_CONCAT(vp.value, ',') FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid)\n"
    3683             :                                         "                  ELSE NULL\n"
    3684             :                                         "          END AS pvalues,\n"
    3685             :                                         "          CASE WHEN tp.\"type\" = 'RANGE'\n"
    3686             :                                         "                  THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3687             :                                         "                  ELSE NULL\n"
    3688             :                                         "          END AS minimum,\n"
    3689             :                                         "          CASE WHEN tp.\"type\" = 'RANGE'\n"
    3690             :                                         "                  THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3691             :                                         "                  ELSE NULL\n"
    3692             :                                         "          END AS maximum,\n"
    3693             :                                         "          CASE WHEN tp.\"type\" = 'VALUES'\n"
    3694             :                                         "                  THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    3695             :                                         "                  ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3696             :                                         "          END AS with_nulls\n"
    3697             :                                         "  FROM \n"
    3698             :                                         "          subq LEFT OUTER JOIN tp\n"
    3699             :                                         "          ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    3700             :                                         "CREATE VIEW sys.describe_functions AS\n"
    3701             :                                         "  WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    3702             :                                         "  (\n"
    3703             :                                         "          SELECT\n"
    3704             :                                         "                  func_id,\n"
    3705             :                                         "                  number,\n"
    3706             :                                         "                  max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    3707             :                                         "                  group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    3708             :                                         "          FROM sys.args\n"
    3709             :                                         "          WHERE inout = 1\n"
    3710             :                                         "  ),\n"
    3711             :                                         "  func_args(func_id, func_arg) AS\n"
    3712             :                                         "  (\n"
    3713             :                                         "          SELECT func_id, func_arg\n"
    3714             :                                         "          FROM func_args_all\n"
    3715             :                                         "          WHERE number = max_number\n"
    3716             :                                         "  ),\n"
    3717             :                                         "  func_rets_all(func_id, number, max_number, func_ret, func_ret_type) AS\n"
    3718             :                                         "  (\n"
    3719             :                                         "          SELECT\n"
    3720             :                                         "                  func_id,\n"
    3721             :                                         "                  number,\n"
    3722             :                                         "                  max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    3723             :                                         "                  group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number),\n"
    3724             :                                         "                  group_concat(sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    3725             :                                         "          FROM sys.args\n"
    3726             :                                         "          WHERE inout = 0\n"
    3727             :                                         "  ),\n"
    3728             :                                         "  func_rets(func_id, func_ret, func_ret_type) AS\n"
    3729             :                                         "  (\n"
    3730             :                                         "          SELECT\n"
    3731             :                                         "                  func_id,\n"
    3732             :                                         "                  func_ret,\n"
    3733             :                                         "                  func_ret_type\n"
    3734             :                                         "          FROM func_rets_all\n"
    3735             :                                         "          WHERE number = max_number\n"
    3736             :                                         "  )\n"
    3737             :                                         "  SELECT\n"
    3738             :                                         "          f.id o,\n"
    3739             :                                         "          s.name sch,\n"
    3740             :                                         "          f.name fun,\n"
    3741             :                                         "          CASE WHEN f.language IN (1, 2) THEN f.func ELSE 'CREATE ' || ft.function_type_keyword || ' ' || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ')' || CASE WHEN f.type = 5 THEN ' RETURNS TABLE (' || coalesce(fr.func_ret, '') || ')' WHEN f.type IN (1,3) THEN ' RETURNS ' || fr.func_ret_type ELSE '' END || CASE WHEN fl.language_keyword IS NULL THEN '' ELSE ' LANGUAGE ' || fl.language_keyword END || ' ' || f.func END def\n"
    3742             :                                         "  FROM sys.functions f\n"
    3743             :                                         "          LEFT OUTER JOIN func_args fa ON fa.func_id = f.id\n"
    3744             :                                         "          LEFT OUTER JOIN func_rets fr ON fr.func_id = f.id\n"
    3745             :                                         "          JOIN sys.schemas s ON f.schema_id = s.id\n"
    3746             :                                         "          JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
    3747             :                                         "          LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
    3748             :                                         "  WHERE s.name <> 'tmp' AND NOT f.system;\n"
    3749             :                                         "CREATE VIEW sys.describe_sequences AS\n"
    3750             :                                         "  SELECT\n"
    3751             :                                         "          s.name sch,\n"
    3752             :                                         "          seq.name seq,\n"
    3753             :                                         "          seq.\"start\" s,\n"
    3754             :                                         "          get_value_for(s.name, seq.name) rs,\n"
    3755             :                                         "          seq.\"minvalue\" mi,\n"
    3756             :                                         "          seq.\"maxvalue\" ma,\n"
    3757             :                                         "          seq.\"increment\" inc,\n"
    3758             :                                         "          seq.\"cacheinc\" cache,\n"
    3759             :                                         "          seq.\"cycle\" cycle,\n"
    3760             :                                         "          CASE WHEN seq.\"minvalue\" = -9223372036854775807 AND seq.\"increment\" > 0 AND seq.\"start\" =  1 THEN TRUE ELSE FALSE END nomin,\n"
    3761             :                                         "          CASE WHEN seq.\"maxvalue\" =  9223372036854775807 AND seq.\"increment\" < 0 AND seq.\"start\" = -1 THEN TRUE ELSE FALSE END nomax,\n"
    3762             :                                         "          CASE\n"
    3763             :                                         "                  WHEN seq.\"minvalue\" = 0 AND seq.\"increment\" > 0 THEN NULL\n"
    3764             :                                         "                  WHEN seq.\"minvalue\" <> -9223372036854775807 THEN seq.\"minvalue\"\n"
    3765             :                                         "                  ELSE\n"
    3766             :                                         "                          CASE\n"
    3767             :                                         "                                  WHEN seq.\"increment\" < 0  THEN NULL\n"
    3768             :                                         "                                  ELSE CASE WHEN seq.\"start\" = 1 THEN NULL ELSE seq.\"maxvalue\" END\n"
    3769             :                                         "                          END\n"
    3770             :                                         "          END rmi,\n"
    3771             :                                         "          CASE\n"
    3772             :                                         "                  WHEN seq.\"maxvalue\" = 0 AND seq.\"increment\" < 0 THEN NULL\n"
    3773             :                                         "                  WHEN seq.\"maxvalue\" <> 9223372036854775807 THEN seq.\"maxvalue\"\n"
    3774             :                                         "                  ELSE\n"
    3775             :                                         "                          CASE\n"
    3776             :                                         "                                  WHEN seq.\"increment\" > 0  THEN NULL\n"
    3777             :                                         "                                  ELSE CASE WHEN seq.\"start\" = -1 THEN NULL ELSE seq.\"maxvalue\" END\n"
    3778             :                                         "                          END\n"
    3779             :                                         "          END rma\n"
    3780             :                                         "  FROM sys.sequences seq, sys.schemas s\n"
    3781             :                                         "  WHERE s.id = seq.schema_id\n"
    3782             :                                         "  AND s.name <> 'tmp'\n"
    3783             :                                         "  ORDER BY s.name, seq.name;\n"
    3784             :                                         "GRANT SELECT ON sys.describe_constraints TO PUBLIC;\n"
    3785             :                                         "GRANT SELECT ON sys.describe_indices TO PUBLIC;\n"
    3786             :                                         "GRANT SELECT ON sys.describe_column_defaults TO PUBLIC;\n"
    3787             :                                         "GRANT SELECT ON sys.describe_foreign_keys TO PUBLIC;\n"
    3788             :                                         "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
    3789             :                                         "GRANT SELECT ON sys.describe_triggers TO PUBLIC;\n"
    3790             :                                         "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
    3791             :                                         "GRANT SELECT ON sys.fully_qualified_functions TO PUBLIC;\n"
    3792             :                                         "GRANT SELECT ON sys.describe_privileges TO PUBLIC;\n"
    3793             :                                         "GRANT SELECT ON sys.describe_user_defined_types TO PUBLIC;\n"
    3794             :                                         "GRANT SELECT ON sys.describe_partition_tables TO PUBLIC;\n"
    3795             :                                         "GRANT SELECT ON sys.describe_sequences TO PUBLIC;\n"
    3796             :                                         "GRANT SELECT ON sys.describe_functions TO PUBLIC;\n");
    3797           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3798             :                                         "update sys.functions set system = true where system <> true and name in ('sq', 'fqn', 'get_merge_table_partition_expressions', 'get_remote_table_expressions', 'schema_guard') and schema_id = 2000 and type = %d;\n", F_FUNC);
    3799           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3800             :                                         "update sys._tables set system = true where name in ('describe_constraints', 'describe_tables', 'fully_qualified_functions', 'describe_comments', 'describe_privileges', 'describe_partition_tables', 'describe_sequences', 'describe_functions') AND schema_id = 2000;\n");
    3801             : 
    3802             :         /* 76_dump.sql (most everything already dropped) */
    3803           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3804             :                                         "CREATE VIEW sys.dump_create_roles AS\n"
    3805             :                                         "  SELECT\n"
    3806             :                                         "    'CREATE ROLE ' || sys.dq(name) || ';' stmt,\n"
    3807             :                                         "    name user_name\n"
    3808             :                                         "    FROM sys.auths\n"
    3809             :                                         "   WHERE name NOT IN (SELECT name FROM sys.db_user_info)\n"
    3810             :                                         "     AND grantor <> 0;\n"
    3811             :                                         "CREATE VIEW sys.dump_create_users AS\n"
    3812             :                                         "  SELECT\n"
    3813             :                                         "    'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
    3814             :                                         "      sys.sq(sys.password_hash(ui.name)) ||\n"
    3815             :                                         "      ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
    3816             :                                         "    ui.name user_name\n"
    3817             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3818             :                                         "   WHERE ui.default_schema = s.id\n"
    3819             :                                         "     AND ui.name <> 'monetdb'\n"
    3820             :                                         "     AND ui.name <> '.snapshot';\n"
    3821             :                                         "CREATE VIEW sys.dump_create_schemas AS\n"
    3822             :                                         "  SELECT\n"
    3823             :                                         "    'CREATE SCHEMA ' || sys.dq(s.name) || ifthenelse(a.name <> 'sysadmin', ' AUTHORIZATION ' || sys.dq(a.name), ' ') || ';' stmt,\n"
    3824             :                                         "    s.name schema_name\n"
    3825             :                                         "    FROM sys.schemas s, sys.auths a\n"
    3826             :                                         "   WHERE s.authorization = a.id AND s.system = FALSE;\n"
    3827             :                                         "CREATE VIEW sys.dump_add_schemas_to_users AS\n"
    3828             :                                         "  SELECT\n"
    3829             :                                         "    'ALTER USER ' || sys.dq(ui.name) || ' SET SCHEMA ' || sys.dq(s.name) || ';' stmt,\n"
    3830             :                                         "    s.name schema_name,\n"
    3831             :                                         "    ui.name user_name\n"
    3832             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3833             :                                         "   WHERE ui.default_schema = s.id\n"
    3834             :                                         "     AND ui.name <> 'monetdb'\n"
    3835             :                                         "     AND ui.name <> '.snapshot'\n"
    3836             :                                         "     AND s.name <> 'sys';\n"
    3837             :                                         "CREATE VIEW sys.dump_grant_user_privileges AS\n"
    3838             :                                         "  SELECT\n"
    3839             :                                         "    'GRANT ' || sys.dq(a2.name) || ' ' || ifthenelse(a1.name = 'public', 'PUBLIC', sys.dq(a1.name)) || ';' stmt,\n"
    3840             :                                         "    a2.name grantee,\n"
    3841             :                                         "    a1.name grantor\n"
    3842             :                                         "    FROM sys.auths a1, sys.auths a2, sys.user_role ur\n"
    3843             :                                         "   WHERE a1.id = ur.login_id AND a2.id = ur.role_id;\n"
    3844             :                                         "CREATE VIEW sys.dump_table_constraint_type AS\n"
    3845             :                                         "  SELECT\n"
    3846             :                                         "    'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ADD CONSTRAINT ' || sys.DQ(con) || ' '||\n"
    3847             :                                         "      tpe || ' (' || GROUP_CONCAT(sys.DQ(col), ', ') || ');' stmt,\n"
    3848             :                                         "    sch schema_name,\n"
    3849             :                                         "    tbl table_name,\n"
    3850             :                                         "    con constraint_name\n"
    3851             :                                         "    FROM sys.describe_constraints GROUP BY sch, tbl, con, tpe;\n"
    3852             :                                         "CREATE VIEW sys.dump_table_grants AS\n"
    3853             :                                         "  WITH table_grants (sname, tname, grantee, grants, grantor, grantable)\n"
    3854             :                                         "  AS (SELECT s.name, t.name, a.name, sum(p.privileges), g.name, p.grantable\n"
    3855             :                                         "  FROM sys.schemas s, sys.tables t, sys.auths a, sys.privileges p, sys.auths g\n"
    3856             :                                         "       WHERE p.obj_id = t.id AND p.auth_id = a.id AND t.schema_id = s.id AND t.system = FALSE AND p.grantor = g.id\n"
    3857             :                                         "       GROUP BY s.name, t.name, a.name, g.name, p.grantable\n"
    3858             :                                         "       ORDER BY s.name, t.name, a.name, g.name, p.grantable)\n"
    3859             :                                         "  SELECT\n"
    3860             :                                         "    'GRANT ' || pc.privilege_code_name || ' ON TABLE ' || sys.FQN(sname, tname)\n"
    3861             :                                         "      || ' TO ' || ifthenelse(grantee = 'public', 'PUBLIC', sys.dq(grantee))\n"
    3862             :                                         "      || CASE WHEN grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3863             :                                         "    sname schema_name,\n"
    3864             :                                         "    tname table_name,\n"
    3865             :                                         "    grantee\n"
    3866             :                                         "    FROM table_grants LEFT OUTER JOIN sys.privilege_codes pc ON grants = pc.privilege_code_id;\n"
    3867             :                                         "CREATE VIEW sys.dump_column_grants AS\n"
    3868             :                                         "  SELECT\n"
    3869             :                                         "    'GRANT ' || pc.privilege_code_name || '(' || sys.dq(c.name) || ') ON ' || sys.FQN(s.name, t.name)\n"
    3870             :                                         "      || ' TO ' || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
    3871             :                                         "      || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3872             :                                         "    s.name schema_name,\n"
    3873             :                                         "    t.name table_name,\n"
    3874             :                                         "    c.name column_name,\n"
    3875             :                                         "    a.name grantee\n"
    3876             :                                         "    FROM sys.schemas s,\n"
    3877             :                                         "   sys.tables t,\n"
    3878             :                                         "   sys.columns c,\n"
    3879             :                                         "   sys.auths a,\n"
    3880             :                                         "   sys.privileges p,\n"
    3881             :                                         "   sys.auths g,\n"
    3882             :                                         "   sys.privilege_codes pc\n"
    3883             :                                         "   WHERE p.obj_id = c.id\n"
    3884             :                                         "     AND c.table_id = t.id\n"
    3885             :                                         "     AND p.auth_id = a.id\n"
    3886             :                                         "     AND t.schema_id = s.id\n"
    3887             :                                         "     AND NOT t.system\n"
    3888             :                                         "     AND p.grantor = g.id\n"
    3889             :                                         "     AND p.privileges = pc.privilege_code_id\n"
    3890             :                                         "   ORDER BY s.name, t.name, c.name, a.name, g.name, p.grantable;\n"
    3891             :                                         "CREATE VIEW sys.dump_function_grants AS\n"
    3892             :                                         "  WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    3893             :                                         "  (SELECT a.func_id,\n"
    3894             :                                         "    a.number,\n"
    3895             :                                         "    max(a.number) OVER (PARTITION BY a.func_id ORDER BY a.number DESC),\n"
    3896             :                                         "    group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ', ') OVER (PARTITION BY a.func_id ORDER BY a.number)\n"
    3897             :                                         "     FROM sys.args a\n"
    3898             :                                         "    WHERE a.inout = 1),\n"
    3899             :                                         "  func_args(func_id, func_arg) AS\n"
    3900             :                                         "  (SELECT func_id, func_arg FROM func_args_all WHERE number = max_number)\n"
    3901             :                                         "  SELECT\n"
    3902             :                                         "    'GRANT ' || pc.privilege_code_name || ' ON ' || ft.function_type_keyword || ' '\n"
    3903             :                                         "      || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ') TO '\n"
    3904             :                                         "      || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
    3905             :                                         "      || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3906             :                                         "    s.name schema_name,\n"
    3907             :                                         "    f.name function_name,\n"
    3908             :                                         "    a.name grantee\n"
    3909             :                                         "    FROM sys.schemas s,\n"
    3910             :                                         "   sys.functions f LEFT OUTER JOIN func_args fa ON f.id = fa.func_id,\n"
    3911             :                                         "   sys.auths a,\n"
    3912             :                                         "   sys.privileges p,\n"
    3913             :                                         "   sys.auths g,\n"
    3914             :                                         "   sys.function_types ft,\n"
    3915             :                                         "   sys.privilege_codes pc\n"
    3916             :                                         "   WHERE s.id = f.schema_id\n"
    3917             :                                         "     AND f.id = p.obj_id\n"
    3918             :                                         "     AND p.auth_id = a.id\n"
    3919             :                                         "     AND p.grantor = g.id\n"
    3920             :                                         "     AND p.privileges = pc.privilege_code_id\n"
    3921             :                                         "     AND f.type = ft.function_type_id\n"
    3922             :                                         "     AND NOT f.system\n"
    3923             :                                         "   ORDER BY s.name, f.name, a.name, g.name, p.grantable;\n"
    3924             :                                         "CREATE VIEW sys.dump_indices AS\n"
    3925             :                                         "  SELECT\n"
    3926             :                                         "    'CREATE ' || tpe || ' ' || sys.DQ(ind) || ' ON ' || sys.FQN(sch, tbl) || '(' || GROUP_CONCAT(col) || ');' stmt,\n"
    3927             :                                         "    sch schema_name,\n"
    3928             :                                         "    tbl table_name,\n"
    3929             :                                         "    ind index_name\n"
    3930             :                                         "    FROM sys.describe_indices GROUP BY ind, tpe, sch, tbl;\n"
    3931             :                                         "CREATE VIEW sys.dump_column_defaults AS\n"
    3932             :                                         "  SELECT 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ALTER COLUMN ' || sys.DQ(col) || ' SET DEFAULT ' || def || ';' stmt,\n"
    3933             :                                         "   sch schema_name,\n"
    3934             :                                         "   tbl table_name,\n"
    3935             :                                         "   col column_name\n"
    3936             :                                         "    FROM sys.describe_column_defaults;\n"
    3937             :                                         "CREATE VIEW sys.dump_foreign_keys AS\n"
    3938             :                                         "  SELECT\n"
    3939             :                                         "    'ALTER TABLE ' || sys.FQN(fk_s, fk_t) || ' ADD CONSTRAINT ' || sys.DQ(fk) || ' ' ||\n"
    3940             :                                         "      'FOREIGN KEY(' || GROUP_CONCAT(sys.DQ(fk_c), ',') ||') ' ||\n"
    3941             :                                         "      'REFERENCES ' || sys.FQN(pk_s, pk_t) || '(' || GROUP_CONCAT(sys.DQ(pk_c), ',') || ') ' ||\n"
    3942             :                                         "      'ON DELETE ' || on_delete || ' ON UPDATE ' || on_update ||\n"
    3943             :                                         "      ';' stmt,\n"
    3944             :                                         "    fk_s foreign_schema_name,\n"
    3945             :                                         "    fk_t foreign_table_name,\n"
    3946             :                                         "    pk_s primary_schema_name,\n"
    3947             :                                         "    pk_t primary_table_name,\n"
    3948             :                                         "    fk key_name\n"
    3949             :                                         "    FROM sys.describe_foreign_keys GROUP BY fk_s, fk_t, pk_s, pk_t, fk, on_delete, on_update;\n"
    3950             :                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    3951             :                                         "  SELECT\n"
    3952             :                                         "    'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    3953             :                                         "      CASE \n"
    3954             :                                         "      WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    3955             :                                         "      WHEN tpe = 'RANGE' THEN ' AS PARTITION FROM ' || ifthenelse(minimum IS NOT NULL, sys.SQ(minimum), 'RANGE MINVALUE') || ' TO ' || ifthenelse(maximum IS NOT NULL, sys.SQ(maximum), 'RANGE MAXVALUE')\n"
    3956             :                                         "      WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    3957             :                                         "      ELSE ''\n"
    3958             :                                         "      END ||\n"
    3959             :                                         "      CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    3960             :                                         "      ';' stmt,\n"
    3961             :                                         "    m_sch merge_schema_name,\n"
    3962             :                                         "    m_tbl merge_table_name,\n"
    3963             :                                         "    p_sch partition_schema_name,\n"
    3964             :                                         "    p_tbl partition_table_name\n"
    3965             :                                         "    FROM sys.describe_partition_tables;\n"
    3966             :                                         "CREATE VIEW sys.dump_sequences AS\n"
    3967             :                                         "  SELECT\n"
    3968             :                                         "    'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT ' ||\n"
    3969             :                                         "    CASE WHEN \"s\" <> 0 THEN 'START WITH ' ||  \"rs\" ELSE '' END ||\n"
    3970             :                                         "    CASE WHEN \"inc\" <> 1 THEN ' INCREMENT BY ' ||  \"inc\" ELSE '' END ||\n"
    3971             :                                         "    CASE\n"
    3972             :                                         "      WHEN nomin THEN ' NO MINVALUE'\n"
    3973             :                                         "      WHEN rmi IS NOT NULL THEN ' MINVALUE ' || rmi\n"
    3974             :                                         "      ELSE ''\n"
    3975             :                                         "    END ||\n"
    3976             :                                         "    CASE\n"
    3977             :                                         "      WHEN nomax THEN ' NO MAXVALUE'\n"
    3978             :                                         "      WHEN rma IS NOT NULL THEN ' MAXVALUE ' || rma\n"
    3979             :                                         "      ELSE ''\n"
    3980             :                                         "    END ||\n"
    3981             :                                         "    CASE WHEN \"cache\" <> 1 THEN ' CACHE ' ||  \"cache\" ELSE '' END ||\n"
    3982             :                                         "    CASE WHEN \"cycle\" THEN ' CYCLE' ELSE '' END ||\n"
    3983             :                                         "    ';' stmt,\n"
    3984             :                                         "    sch schema_name,\n"
    3985             :                                         "    seq seqname\n"
    3986             :                                         "    FROM sys.describe_sequences;\n"
    3987             :                                         "CREATE VIEW sys.dump_start_sequences AS\n"
    3988             :                                         "  SELECT\n"
    3989             :                                         "    'UPDATE sys.sequences seq SET start = ' || s ||\n"
    3990             :                                         "      ' WHERE name = ' || sys.SQ(seq) ||\n"
    3991             :                                         "      ' AND schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = ' || sys.SQ(sch) || ');' stmt,\n"
    3992             :                                         "    sch schema_name,\n"
    3993             :                                         "    seq sequence_name\n"
    3994             :                                         "    FROM sys.describe_sequences;\n"
    3995             :                                         "CREATE VIEW sys.dump_functions AS\n"
    3996             :                                         "  SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
    3997             :                                         "   f.sch schema_name,\n"
    3998             :                                         "   f.fun function_name\n"
    3999             :                                         "    FROM sys.describe_functions f;\n"
    4000             :                                         "CREATE VIEW sys.dump_tables AS\n"
    4001             :                                         "  SELECT\n"
    4002             :                                         "    t.o o,\n"
    4003             :                                         "    CASE\n"
    4004             :                                         "      WHEN t.typ <> 'VIEW' THEN\n"
    4005             :                                         "      'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    4006             :                                         "      ELSE\n"
    4007             :                                         "      t.opt\n"
    4008             :                                         "      END stmt,\n"
    4009             :                                         "    t.sch schema_name,\n"
    4010             :                                         "    t.tab table_name\n"
    4011             :                                         "    FROM sys.describe_tables t;\n"
    4012             :                                         "CREATE VIEW sys.dump_triggers AS\n"
    4013             :                                         "  SELECT sys.schema_guard(sch, tab, def) stmt,\n"
    4014             :                                         "   sch schema_name,\n"
    4015             :                                         "   tab table_name,\n"
    4016             :                                         "   tri trigger_name\n"
    4017             :                                         "    FROM sys.describe_triggers;\n"
    4018             :                                         "CREATE VIEW sys.dump_comments AS\n"
    4019             :                                         "  SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    4020             :                                         "CREATE VIEW sys.dump_user_defined_types AS\n"
    4021             :                                         "  SELECT 'CREATE TYPE ' || sys.FQN(sch, sql_tpe) || ' EXTERNAL NAME ' || sys.DQ(ext_tpe) || ';' stmt,\n"
    4022             :                                         "   sch schema_name,\n"
    4023             :                                         "   sql_tpe type_name\n"
    4024             :                                         "    FROM sys.describe_user_defined_types;\n"
    4025             :                                         "CREATE FUNCTION sys.esc(s STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(sys.replace(sys.replace(s,E'\\\\', E'\\\\\\\\'), E'\\n', E'\\\\n'), '\"', E'\\\\\"') || '\"'; END;\n"
    4026             :                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    4027             :                                         "BEGIN\n"
    4028             :                                         "  RETURN\n"
    4029             :                                         "    CASE\n"
    4030             :                                         "    WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    4031             :                                         "    'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    4032             :                                         "    ELSE\n"
    4033             :                                         "    'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    4034             :                                         "    END;\n"
    4035             :                                         "END;\n"
    4036             :                                         "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
    4037             :                                         "BEGIN\n"
    4038             :                                         "  DECLARE k INT;\n"
    4039             :                                         "  SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t, sys.schemas s WHERE c.table_id = t.id AND t.name = tbl AND t.schema_id = s.id AND s.name = sch);\n"
    4040             :                                         "  IF k IS NOT NULL THEN\n"
    4041             :                                         "    DECLARE cname STRING;\n"
    4042             :                                         "    DECLARE ctype STRING;\n"
    4043             :                                         "    SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4044             :                                         "    SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4045             :                                         "    DECLARE COPY_INTO_STMT STRING;\n"
    4046             :                                         "    DECLARE _cnt INT;\n"
    4047             :                                         "    SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
    4048             :                                         "    IF _cnt > 0 THEN\n"
    4049             :                                         "      SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    4050             :                                         "      DECLARE SELECT_DATA_STMT STRING;\n"
    4051             :                                         "      SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    4052             :                                         "      DECLARE M INT;\n"
    4053             :                                         "      SET M = (SELECT MAX(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    4054             :                                         "      WHILE (k < M) DO\n"
    4055             :                                         "  SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl AND c.id > k);\n"
    4056             :                                         "        SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4057             :                                         "  SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4058             :                                         "  SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    4059             :                                         "  SET SELECT_DATA_STMT = SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype);\n"
    4060             :                                         "      END WHILE;\n"
    4061             :                                         "      SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    4062             :                                         "      SET SELECT_DATA_STMT = SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl);\n"
    4063             :                                         "      insert into sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    4064             :                                         "      CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    4065             :                                         "    END IF;\n"
    4066             :                                         "  END IF;\n"
    4067             :                                         "END;\n"
    4068             :                                         "CREATE PROCEDURE sys.dump_table_data()\n"
    4069             :                                         "BEGIN\n"
    4070             :                                         "  DECLARE i INT;\n"
    4071             :                                         "  SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    4072             :                                         "  IF i IS NOT NULL THEN\n"
    4073             :                                         "    DECLARE M INT;\n"
    4074             :                                         "    SET M = (SELECT MAX(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    4075             :                                         "    DECLARE sch STRING;\n"
    4076             :                                         "    DECLARE tbl STRING;\n"
    4077             :                                         "    WHILE i < M DO\n"
    4078             :                                         "      set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4079             :                                         "      set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4080             :                                         "      CALL sys.dump_table_data(sch, tbl);\n"
    4081             :                                         "      SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system AND t.id > i);\n"
    4082             :                                         "    END WHILE;\n"
    4083             :                                         "    set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4084             :                                         "    set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4085             :                                         "    CALL sys.dump_table_data(sch, tbl);\n"
    4086             :                                         "  END IF;\n"
    4087             :                                         "END;\n"
    4088             :                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    4089             :                                         "BEGIN\n"
    4090             :                                         "  SET SCHEMA sys;\n"
    4091             :                                         "  TRUNCATE sys.dump_statements;\n"
    4092             :                                         "  INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    4093             :                                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'SET SCHEMA \"sys\";');\n"
    4094             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    4095             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    4096             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    4097             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    4098             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    4099             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    4100             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    4101             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    4102             :                                         "                              FROM (\n"
    4103             :                                         "                                SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    4104             :                                         "                                 UNION\n"
    4105             :                                         "                                SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    4106             :                                         "                              ) AS stmts(o, s);\n"
    4107             :                                         "  IF NOT DESCRIBE THEN\n"
    4108             :                                         "    CALL sys.dump_table_data();\n"
    4109             :                                         "  END IF;\n"
    4110             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    4111             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    4112             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    4113             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    4114             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    4115             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    4116             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    4117             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    4118             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    4119             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    4120             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    4121             :                                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    4122             :                                         "  RETURN sys.dump_statements;\n"
    4123             :                                         "END;\n");
    4124           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4125             :                                         "update sys.functions set system = true where system <> true and name in ('esc', 'prepare_esc') and schema_id = 2000 and type = %d;\n", F_FUNC);
    4126           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4127             :                                         "update sys.functions set system = true where system <> true and name in ('dump_database') and schema_id = 2000 and type = %d;\n", F_UNION);
    4128           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4129             :                                         "update sys.functions set system = true where system <> true and name in ('dump_table_data') and schema_id = 2000 and type = %d;\n", F_PROC);
    4130           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4131             :                                         "update sys._tables set system = true where name in ('dump_create_roles', 'dump_create_users', 'dump_create_schemas', 'dump_add_schemas_to_users', 'dump_grant_user_privileges', 'dump_table_constraint_type', 'dump_table_grants', 'dump_column_grants', 'dump_function_grants', 'dump_indices', 'dump_column_defaults', 'dump_foreign_keys', 'dump_partition_tables', 'dump_sequences', 'dump_start_sequences', 'dump_functions', 'dump_tables', 'dump_triggers', 'dump_comments', 'dump_user_defined_types') AND schema_id = 2000;\n");
    4132             : 
    4133             :         /* 80_udf.sql (removed) */
    4134           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4135             :                                         "drop function sys.reverse(string) cascade;\n"
    4136             :                                         "drop all function sys.fuse cascade;\n");
    4137             : 
    4138             :         /* 26_sysmon.sql */
    4139           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4140             :                                         "create procedure sys.vacuum(sname string, tname string, cname string)\n"
    4141             :                                         "  external name sql.vacuum;\n"
    4142             :                                         "create procedure sys.vacuum(sname string, tname string, cname string, interval int)\n"
    4143             :                                         "  external name sql.vacuum;\n"
    4144             :                                         "create procedure sys.stop_vacuum(sname string, tname string, cname string)\n"
    4145             :                                         "  external name sql.stop_vacuum;\n");
    4146           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4147             :                                         "update sys.functions set system = true where system <> true and name in ('vacuum', 'stop_vacuum') and schema_id = 2000 and type = %d;\n", F_PROC);
    4148             : 
    4149             :         /* 10_sys_schema_extension.sql */
    4150           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4151             :                                         "CREATE TABLE sys.fkey_actions (\n"
    4152             :                                         "    action_id   SMALLINT NOT NULL PRIMARY KEY,\n"
    4153             :                                         "    action_name VARCHAR(15) NOT NULL);\n"
    4154             :                                         "INSERT INTO sys.fkey_actions (action_id, action_name) VALUES\n"
    4155             :                                         "  (0, 'NO ACTION'),\n"
    4156             :                                         "  (1, 'CASCADE'),\n"
    4157             :                                         "  (2, 'RESTRICT'),\n"
    4158             :                                         "  (3, 'SET NULL'),\n"
    4159             :                                         "  (4, 'SET DEFAULT');\n"
    4160             :                                         "ALTER TABLE sys.fkey_actions SET READ ONLY;\n"
    4161             :                                         "GRANT SELECT ON sys.fkey_actions TO PUBLIC;\n"
    4162             :                                         "CREATE VIEW sys.fkeys AS\n"
    4163             :                                         "SELECT id, table_id, type, name, rkey, update_action_id, upd.action_name as update_action, delete_action_id, del.action_name as delete_action FROM (\n"
    4164             :                                         " SELECT id, table_id, type, name, rkey, cast(((\"action\" >> 8) & 255) as smallint) as update_action_id, cast((\"action\" & 255) as smallint) AS delete_action_id FROM sys.keys WHERE type = 2\n"
    4165             :                                         " UNION ALL\n"
    4166             :                                         " SELECT id, table_id, type, name, rkey, cast(((\"action\" >> 8) & 255) as smallint) as update_action_id, cast((\"action\" & 255) as smallint) AS delete_action_id FROM tmp.keys WHERE type = 2\n"
    4167             :                                         ") AS fks\n"
    4168             :                                         "JOIN sys.fkey_actions upd ON fks.update_action_id = upd.action_id\n"
    4169             :                                         "JOIN sys.fkey_actions del ON fks.delete_action_id = del.action_id;\n"
    4170             :                                         "GRANT SELECT ON sys.fkeys TO PUBLIC;\n"
    4171             :                                         );
    4172           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4173             :                                         "update sys._tables set system = true where name in ('fkey_actions', 'fkeys') AND schema_id = 2000;\n");
    4174             : 
    4175             :         /* recreate SQL functions that just need to be recompiled since the
    4176             :          * MAL functions's "unsafe" property was changed */
    4177           0 :         sql_schema *lg = mvc_bind_schema(sql, "logging");
    4178           0 :         t = mvc_bind_table(sql, lg, "compinfo");
    4179           0 :         t->system = 0;
    4180           0 :         t = mvc_bind_table(sql, s, "schemastorage");
    4181           0 :         t->system = 0;
    4182           0 :         t = mvc_bind_table(sql, s, "tablestorage");
    4183           0 :         t->system = 0;
    4184           0 :         t = mvc_bind_table(sql, s, "storage");
    4185           0 :         t->system = 0;
    4186           0 :         t = mvc_bind_table(sql, s, "rejects");
    4187           0 :         t->system = 0;
    4188           0 :         t = mvc_bind_table(sql, s, "queue");
    4189           0 :         t->system = 0;
    4190           0 :         t = mvc_bind_table(sql, s, "optimizers");
    4191           0 :         t->system = 0;
    4192           0 :         t = mvc_bind_table(sql, s, "prepared_statements_args");
    4193           0 :         t->system = 0;
    4194           0 :         t = mvc_bind_table(sql, s, "prepared_statements");
    4195           0 :         t->system = 0;
    4196           0 :         t = mvc_bind_table(sql, s, "sessions");
    4197           0 :         t->system = 0;
    4198           0 :         t = mvc_bind_table(sql, s, "querylog_calls");
    4199           0 :         t->system = 0;
    4200           0 :         t = mvc_bind_table(sql, s, "querylog_history");
    4201           0 :         t->system = 0;
    4202           0 :         t = mvc_bind_table(sql, s, "querylog_catalog");
    4203           0 :         t->system = 0;
    4204           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4205             :                                         "drop view logging.compinfo cascade;\n"
    4206             :                                         "drop function logging.compinfo cascade;\n"
    4207             :                                         "drop procedure sys.storagemodelinit() cascade;\n"
    4208             :                                         "drop view sys.schemastorage cascade;\n"
    4209             :                                         "drop view sys.tablestorage cascade;\n"
    4210             :                                         "drop view sys.storage cascade;\n"
    4211             :                                         "drop function sys.storage() cascade;\n"
    4212             :                                         "drop function if exists wlr.tick cascade;\n"
    4213             :                                         "drop function if exists wlr.clock cascade;\n"
    4214             :                                         "drop function if exists wlc.tick cascade;\n"
    4215             :                                         "drop function if exists wlc.clock cascade;\n"
    4216             :                                         "drop function profiler.getlimit cascade;\n"
    4217             :                                         "drop view sys.rejects cascade;\n"
    4218             :                                         "drop function sys.rejects cascade;\n"
    4219             :                                         "drop function sys.user_statistics cascade;\n"
    4220             :                                         "drop view sys.queue cascade;\n"
    4221             :                                         "drop function sys.queue cascade;\n"
    4222             :                                         "drop function sys.debugflags cascade;\n"
    4223             :                                         "drop function sys.bbp cascade;\n"
    4224             :                                         "drop view sys.optimizers cascade;\n"
    4225             :                                         "drop function sys.optimizers cascade;\n"
    4226             :                                         "drop function sys.querycache cascade;\n"
    4227             :                                         "drop function sys.optimizer_stats cascade;\n"
    4228             :                                         "drop function sys.current_sessionid cascade;\n"
    4229             :                                         "drop view sys.prepared_statements_args cascade;\n"
    4230             :                                         "drop function sys.prepared_statements_args cascade;\n"
    4231             :                                         "drop view sys.prepared_statements cascade;\n"
    4232             :                                         "drop function sys.prepared_statements cascade;\n"
    4233             :                                         "drop view sys.sessions cascade;\n"
    4234             :                                         "drop function sys.sessions cascade;\n"
    4235             :                                         "drop view sys.querylog_history cascade;\n"
    4236             :                                         "drop view sys.querylog_calls cascade;\n"
    4237             :                                         "drop function sys.querylog_calls cascade;\n"
    4238             :                                         "drop view sys.querylog_catalog cascade;\n"
    4239             :                                         "drop function sys.querylog_catalog cascade;\n"
    4240             :                                         "create function sys.querylog_catalog()\n"
    4241             :                                         "returns table(\n"
    4242             :                                         " id oid,\n"
    4243             :                                         " owner string,\n"
    4244             :                                         " defined timestamp,\n"
    4245             :                                         " query string,\n"
    4246             :                                         " pipe string,\n"
    4247             :                                         " \"plan\" string,\n"
    4248             :                                         " mal int,\n"
    4249             :                                         " optimize bigint\n"
    4250             :                                         ")\n"
    4251             :                                         "external name sql.querylog_catalog;\n"
    4252             :                                         "create view sys.querylog_catalog as select * from sys.querylog_catalog();\n"
    4253             :                                         "create function sys.querylog_calls()\n"
    4254             :                                         "returns table(\n"
    4255             :                                         " id oid,\n"
    4256             :                                         " \"start\" timestamp,\n"
    4257             :                                         " \"stop\" timestamp,\n"
    4258             :                                         " arguments string,\n"
    4259             :                                         " tuples bigint,\n"
    4260             :                                         " run bigint,\n"
    4261             :                                         " ship bigint,\n"
    4262             :                                         " cpu int,\n"
    4263             :                                         " io int\n"
    4264             :                                         ")\n"
    4265             :                                         "external name sql.querylog_calls;\n"
    4266             :                                         "create view sys.querylog_calls as select * from sys.querylog_calls();\n"
    4267             :                                         "create view sys.querylog_history as\n"
    4268             :                                         "select qd.*, ql.\"start\",ql.\"stop\", ql.arguments, ql.tuples, ql.run, ql.ship, ql.cpu, ql.io\n"
    4269             :                                         "from sys.querylog_catalog() qd, sys.querylog_calls() ql\n"
    4270             :                                         "where qd.id = ql.id and qd.owner = user;\n"
    4271             :                                         "create function sys.sessions()\n"
    4272             :                                         "returns table(\n"
    4273             :                                         " \"sessionid\" int,\n"
    4274             :                                         " \"username\" string,\n"
    4275             :                                         " \"login\" timestamp,\n"
    4276             :                                         " \"idle\" timestamp,\n"
    4277             :                                         " \"optimizer\" string,\n"
    4278             :                                         " \"sessiontimeout\" int,\n"
    4279             :                                         " \"querytimeout\" int,\n"
    4280             :                                         " \"workerlimit\" int,\n"
    4281             :                                         " \"memorylimit\" int\n"
    4282             :                                         ")\n"
    4283             :                                         "external name sql.sessions;\n"
    4284             :                                         "create view sys.sessions as select * from sys.sessions();\n"
    4285             :                                         "create function sys.prepared_statements()\n"
    4286             :                                         "returns table(\n"
    4287             :                                         " \"sessionid\" int,\n"
    4288             :                                         " \"username\" string,\n"
    4289             :                                         " \"statementid\" int,\n"
    4290             :                                         " \"statement\" string,\n"
    4291             :                                         " \"created\" timestamp\n"
    4292             :                                         ")\n"
    4293             :                                         "external name sql.prepared_statements;\n"
    4294             :                                         "grant execute on function sys.prepared_statements to public;\n"
    4295             :                                         "create view sys.prepared_statements as select * from sys.prepared_statements();\n"
    4296             :                                         "grant select on sys.prepared_statements to public;\n"
    4297             :                                         "create function sys.prepared_statements_args()\n"
    4298             :                                         "returns table(\n"
    4299             :                                         " \"statementid\" int,\n"
    4300             :                                         " \"type\" string,\n"
    4301             :                                         " \"type_digits\" int,\n"
    4302             :                                         " \"type_scale\" int,\n"
    4303             :                                         " \"inout\" tinyint,\n"
    4304             :                                         " \"number\" int,\n"
    4305             :                                         " \"schema\" string,\n"
    4306             :                                         " \"table\" string,\n"
    4307             :                                         " \"column\" string\n"
    4308             :                                         ")\n"
    4309             :                                         "external name sql.prepared_statements_args;\n"
    4310             :                                         "grant execute on function sys.prepared_statements_args to public;\n"
    4311             :                                         "create view sys.prepared_statements_args as select * from sys.prepared_statements_args();\n"
    4312             :                                         "grant select on sys.prepared_statements_args to public;\n"
    4313             :                                         "create function sys.current_sessionid() returns int\n"
    4314             :                                         "external name clients.current_sessionid;\n"
    4315             :                                         "grant execute on function sys.current_sessionid to public;\n"
    4316             :                                         "create function sys.optimizer_stats()\n"
    4317             :                                         " returns table (optname string, count int, timing bigint)\n"
    4318             :                                         " external name inspect.optimizer_stats;\n"
    4319             :                                         "create function sys.querycache()\n"
    4320             :                                         " returns table (query string, count int)\n"
    4321             :                                         " external name sql.dump_cache;\n"
    4322             :                                         "create function sys.optimizers ()\n"
    4323             :                                         " returns table (name string, def string, status string)\n"
    4324             :                                         " external name sql.optimizers;\n"
    4325             :                                         "create view sys.optimizers as select * from sys.optimizers();\n"
    4326             :                                         "create function sys.bbp ()\n"
    4327             :                                         " returns table (id int, name string,\n"
    4328             :                                         " ttype string, count bigint, refcnt int, lrefcnt int,\n"
    4329             :                                         " location string, heat int, dirty string,\n"
    4330             :                                         " status string, kind string)\n"
    4331             :                                         " external name bbp.get;\n"
    4332             :                                         "create function sys.debugflags()\n"
    4333             :                                         " returns table(flag string, val bool)\n"
    4334             :                                         " external name mdb.\"getDebugFlags\";\n"
    4335             :                                         "create function sys.queue()\n"
    4336             :                                         "returns table(\n"
    4337             :                                         " \"tag\" bigint,\n"
    4338             :                                         " \"sessionid\" int,\n"
    4339             :                                         " \"username\" string,\n"
    4340             :                                         " \"started\" timestamp,\n"
    4341             :                                         " \"status\" string,\n"
    4342             :                                         " \"query\" string,\n"
    4343             :                                         " \"finished\" timestamp,\n"
    4344             :                                         " \"maxworkers\" int,\n"
    4345             :                                         " \"footprint\" int\n"
    4346             :                                         ")\n"
    4347             :                                         "external name sysmon.queue;\n"
    4348             :                                         "grant execute on function sys.queue to public;\n"
    4349             :                                         "create view sys.queue as select * from sys.queue();\n"
    4350             :                                         "grant select on sys.queue to public;\n"
    4351             :                                         "create function sys.user_statistics()\n"
    4352             :                                         "returns table(\n"
    4353             :                                         " username string,\n"
    4354             :                                         " querycount bigint,\n"
    4355             :                                         " totalticks bigint,\n"
    4356             :                                         " started timestamp,\n"
    4357             :                                         " finished timestamp,\n"
    4358             :                                         " maxticks bigint,\n"
    4359             :                                         " maxquery string\n"
    4360             :                                         ")\n"
    4361             :                                         "external name sysmon.user_statistics;\n"
    4362             :                                         "create function sys.rejects()\n"
    4363             :                                         "returns table(\n"
    4364             :                                         " rowid bigint,\n"
    4365             :                                         " fldid int,\n"
    4366             :                                         " \"message\" string,\n"
    4367             :                                         " \"input\" string\n"
    4368             :                                         ")\n"
    4369             :                                         "external name sql.copy_rejects;\n"
    4370             :                                         "grant execute on function rejects to public;\n"
    4371             :                                         "create view sys.rejects as select * from sys.rejects();\n"
    4372             :                                         "create function profiler.getlimit() returns integer external name profiler.getlimit;\n"
    4373             :                                         "create function sys.\"storage\"()\n"
    4374             :                                         "returns table (\n"
    4375             :                                         " \"schema\" varchar(1024),\n"
    4376             :                                         " \"table\" varchar(1024),\n"
    4377             :                                         " \"column\" varchar(1024),\n"
    4378             :                                         " \"type\" varchar(1024),\n"
    4379             :                                         " \"mode\" varchar(15),\n"
    4380             :                                         " location varchar(1024),\n"
    4381             :                                         " \"count\" bigint,\n"
    4382             :                                         " typewidth int,\n"
    4383             :                                         " columnsize bigint,\n"
    4384             :                                         " heapsize bigint,\n"
    4385             :                                         " hashes bigint,\n"
    4386             :                                         " phash boolean,\n"
    4387             :                                         " \"imprints\" bigint,\n"
    4388             :                                         " sorted boolean,\n"
    4389             :                                         " revsorted boolean,\n"
    4390             :                                         " \"unique\" boolean,\n"
    4391             :                                         " orderidx bigint\n"
    4392             :                                         ")\n"
    4393             :                                         "external name sql.\"storage\";\n"
    4394             :                                         "create view sys.\"storage\" as\n"
    4395             :                                         "select * from sys.\"storage\"()\n"
    4396             :                                         " where (\"schema\", \"table\") in (\n"
    4397             :                                         " select sch.\"name\", tbl.\"name\"\n"
    4398             :                                         " from sys.\"tables\" as tbl join sys.\"schemas\" as sch on tbl.schema_id = sch.id\n"
    4399             :                                         " where tbl.\"system\" = false)\n"
    4400             :                                         "order by \"schema\", \"table\", \"column\";\n"
    4401             :                                         "create view sys.\"tablestorage\" as\n"
    4402             :                                         "select \"schema\", \"table\",\n"
    4403             :                                         " max(\"count\") as \"rowcount\",\n"
    4404             :                                         " count(*) as \"storages\",\n"
    4405             :                                         " sum(columnsize) as columnsize,\n"
    4406             :                                         " sum(heapsize) as heapsize,\n"
    4407             :                                         " sum(hashes) as hashsize,\n"
    4408             :                                         " sum(\"imprints\") as imprintsize,\n"
    4409             :                                         " sum(orderidx) as orderidxsize\n"
    4410             :                                         " from sys.\"storage\"\n"
    4411             :                                         "group by \"schema\", \"table\"\n"
    4412             :                                         "order by \"schema\", \"table\";\n"
    4413             :                                         "create view sys.\"schemastorage\" as\n"
    4414             :                                         "select \"schema\",\n"
    4415             :                                         " count(*) as \"storages\",\n"
    4416             :                                         " sum(columnsize) as columnsize,\n"
    4417             :                                         " sum(heapsize) as heapsize,\n"
    4418             :                                         " sum(hashes) as hashsize,\n"
    4419             :                                         " sum(\"imprints\") as imprintsize,\n"
    4420             :                                         " sum(orderidx) as orderidxsize\n"
    4421             :                                         " from sys.\"storage\"\n"
    4422             :                                         "group by \"schema\"\n"
    4423             :                                         "order by \"schema\";\n"
    4424             :                                         "create procedure sys.storagemodelinit()\n"
    4425             :                                         "begin\n"
    4426             :                                         " delete from sys.storagemodelinput;\n"
    4427             :                                         " insert into sys.storagemodelinput\n"
    4428             :                                         " select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
    4429             :                                         " case when (\"unique\" or \"type\" in ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
    4430             :                                         " then \"count\" else 0 end,\n"
    4431             :                                         " case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
    4432             :                                         " then cast((heapsize - 8192) / \"count\" as bigint)\n"
    4433             :                                         " when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
    4434             :                                         " then cast((heapsize - 32) / \"count\" as bigint)\n"
    4435             :                                         " else typewidth end,\n"
    4436             :                                         " false, case sorted when true then true else false end, \"unique\", true\n"
    4437             :                                         " from sys.\"storage\";\n"
    4438             :                                         " update sys.storagemodelinput\n"
    4439             :                                         " set reference = true\n"
    4440             :                                         " where (\"schema\", \"table\", \"column\") in (\n"
    4441             :                                         " select fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
    4442             :                                         " from sys.\"keys\" as fkkey,\n"
    4443             :                                         " sys.\"objects\" as fkkeycol,\n"
    4444             :                                         " sys.\"tables\" as fktable,\n"
    4445             :                                         " sys.\"schemas\" as fkschema\n"
    4446             :                                         " where fktable.\"id\" = fkkey.\"table_id\"\n"
    4447             :                                         " and fkkey.\"id\" = fkkeycol.\"id\"\n"
    4448             :                                         " and fkschema.\"id\" = fktable.\"schema_id\"\n"
    4449             :                                         " and fkkey.\"rkey\" > -1 );\n"
    4450             :                                         " update sys.storagemodelinput\n"
    4451             :                                         " set isacolumn = false\n"
    4452             :                                         " where (\"schema\", \"table\", \"column\") not in (\n"
    4453             :                                         " select sch.\"name\", tbl.\"name\", col.\"name\"\n"
    4454             :                                         " from sys.\"schemas\" as sch,\n"
    4455             :                                         " sys.\"tables\" as tbl,\n"
    4456             :                                         " sys.\"columns\" as col\n"
    4457             :                                         " where sch.\"id\" = tbl.\"schema_id\"\n"
    4458             :                                         " and tbl.\"id\" = col.\"table_id\");\n"
    4459             :                                         "end;\n"
    4460             :                                         "create function logging.compinfo()\n"
    4461             :                                         "returns table(\n"
    4462             :                                         " \"id\" int,\n"
    4463             :                                         " \"component\" string,\n"
    4464             :                                         " \"log_level\" string\n"
    4465             :                                         ")\n"
    4466             :                                         "external name logging.compinfo;\n"
    4467             :                                         "grant execute on function logging.compinfo to public;\n"
    4468             :                                         "create view logging.compinfo as select * from logging.compinfo();\n"
    4469             :                                         "grant select on logging.compinfo to public;\n"
    4470             :                                         "update sys._tables set system = true where system <> true and schema_id = 2000 and name in ('schemastorage', 'tablestorage', 'storage', 'rejects', 'queue', 'optimizers', 'prepared_statements_args', 'prepared_statements', 'sessions', 'querylog_history', 'querylog_calls', 'querylog_catalog');\n"
    4471             :                                         "update sys._tables set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'logging') and name = 'compinfo';\n"
    4472             :                                         "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('storagemodelinit', 'storage', 'rejects', 'user_statistics', 'queue', 'debugflags', 'bbp', 'optimizers', 'querycache', 'optimizer_stats', 'current_sessionid', 'prepared_statements_args', 'prepared_statements', 'sessions', 'querylog_calls', 'querylog_catalog');\n"
    4473             :                                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'logging') and name = 'compinfo';\n"
    4474             :                                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'profiler') and name = 'getlimit';\n"
    4475             :                 );
    4476             : 
    4477             :         /* 99_system.sql */
    4478           0 :         t = mvc_bind_table(sql, s, "systemfunctions");
    4479           0 :         t->system = 0;
    4480           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4481             :                         "drop view sys.systemfunctions cascade;\n");
    4482             : 
    4483             :         /* 80_statistics.sql */
    4484           0 :         t = mvc_bind_table(sql, s, "statistics");
    4485           0 :         t->system = 0;
    4486           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4487             :                                 "drop table sys.statistics cascade;\n"
    4488             :                                 "drop procedure sys.analyze(int,bigint) cascade;\n"
    4489             :                                 "drop procedure sys.analyze(int,bigint,string) cascade;\n"
    4490             :                                 "drop procedure sys.analyze(int,bigint,string,string) cascade;\n"
    4491             :                                 "drop procedure sys.analyze(int,bigint,string,string,string) cascade;\n"
    4492             :                                 "create procedure sys.\"analyze\"()\n"
    4493             :                                 "external name sql.\"analyze\";\n"
    4494             :                                 "grant execute on procedure sys.\"analyze\"() to public;\n"
    4495             :                                 "create procedure sys.\"analyze\"(\"sname\" varchar(1024))\n"
    4496             :                                 "external name sql.\"analyze\";\n"
    4497             :                                 "grant execute on procedure sys.\"analyze\"(varchar(1024)) to public;\n"
    4498             :                                 "create procedure sys.\"analyze\"(\"sname\" varchar(1024), \"tname\" varchar(1024))\n"
    4499             :                                 "external name sql.\"analyze\";\n"
    4500             :                                 "grant execute on procedure sys.\"analyze\"(varchar(1024),varchar(1024)) to public;\n"
    4501             :                                 "create procedure sys.\"analyze\"(\"sname\" varchar(1024), \"tname\" varchar(1024), \"cname\" varchar(1024))\n"
    4502             :                                 "external name sql.\"analyze\";\n"
    4503             :                                 "grant execute on procedure sys.\"analyze\"(varchar(1024),varchar(1024),varchar(1024)) to public;\n"
    4504             :                                 "create function sys.\"statistics\"()\n"
    4505             :                                 "returns table (\n"
    4506             :                                 "  \"column_id\" integer,\n"
    4507             :                                 "  \"schema\" varchar(1024),\n"
    4508             :                                 "  \"table\" varchar(1024),\n"
    4509             :                                 "  \"column\" varchar(1024),\n"
    4510             :                                 "  \"type\" varchar(1024),\n"
    4511             :                                 "  \"width\" integer,\n"
    4512             :                                 "  \"count\" bigint,\n"
    4513             :                                 "  \"unique\" boolean,\n"
    4514             :                                 "  \"nils\" boolean,\n"
    4515             :                                 "  \"minval\" string,\n"
    4516             :                                 "  \"maxval\" string,\n"
    4517             :                                 "  \"sorted\" boolean,\n"
    4518             :                                 "  \"revsorted\" boolean\n"
    4519             :                                 ")\n"
    4520             :                                 "external name sql.\"statistics\";\n"
    4521             :                                 "grant execute on function sys.\"statistics\"() to public;\n"
    4522             :                                 "create view sys.\"statistics\" as\n"
    4523             :                                 "select * from sys.\"statistics\"()\n"
    4524             :                                 "-- exclude system tables\n"
    4525             :                                 "where (\"schema\", \"table\") in (\n"
    4526             :                                 "  SELECT sch.\"name\", tbl.\"name\"\n"
    4527             :                                 "  FROM sys.\"tables\" AS tbl JOIN sys.\"schemas\" AS sch ON tbl.schema_id = sch.id\n"
    4528             :                                 "  WHERE tbl.\"system\" = FALSE)\n"
    4529             :                                 "order by \"schema\", \"table\", \"column\";\n"
    4530             :                                 "grant select on sys.\"statistics\" to public;\n"
    4531             :                                 "create function sys.\"statistics\"(\"sname\" varchar(1024))\n"
    4532             :                                 "returns table (\n"
    4533             :                                 "  \"column_id\" integer,\n"
    4534             :                                 "  \"schema\" varchar(1024),\n"
    4535             :                                 "  \"table\" varchar(1024),\n"
    4536             :                                 "  \"column\" varchar(1024),\n"
    4537             :                                 "  \"type\" varchar(1024),\n"
    4538             :                                 "  \"width\" integer,\n"
    4539             :                                 "  \"count\" bigint,\n"
    4540             :                                 "  \"unique\" boolean,\n"
    4541             :                                 "  \"nils\" boolean,\n"
    4542             :                                 "  \"minval\" string,\n"
    4543             :                                 "  \"maxval\" string,\n"
    4544             :                                 "  \"sorted\" boolean,\n"
    4545             :                                 "  \"revsorted\" boolean\n"
    4546             :                                 ")\n"
    4547             :                                 "external name sql.\"statistics\";\n"
    4548             :                                 "grant execute on function sys.\"statistics\"(varchar(1024)) to public;\n"
    4549             :                                 "create function sys.\"statistics\"(\"sname\" varchar(1024), \"tname\" varchar(1024))\n"
    4550             :                                 "returns table (\n"
    4551             :                                 "  \"column_id\" integer,\n"
    4552             :                                 "  \"schema\" varchar(1024),\n"
    4553             :                                 "  \"table\" varchar(1024),\n"
    4554             :                                 "  \"column\" varchar(1024),\n"
    4555             :                                 "  \"type\" varchar(1024),\n"
    4556             :                                 "  \"width\" integer,\n"
    4557             :                                 "  \"count\" bigint,\n"
    4558             :                                 "  \"unique\" boolean,\n"
    4559             :                                 "  \"nils\" boolean,\n"
    4560             :                                 "  \"minval\" string,\n"
    4561             :                                 "  \"maxval\" string,\n"
    4562             :                                 "  \"sorted\" boolean,\n"
    4563             :                                 "  \"revsorted\" boolean\n"
    4564             :                                 ")\n"
    4565             :                                 "external name sql.\"statistics\";\n"
    4566             :                                 "grant execute on function sys.\"statistics\"(varchar(1024),varchar(1024)) to public;\n"
    4567             :                                 "create function sys.\"statistics\"(\"sname\" varchar(1024), \"tname\" varchar(1024), \"cname\" varchar(1024))\n"
    4568             :                                 "returns table (\n"
    4569             :                                 "  \"column_id\" integer,\n"
    4570             :                                 "  \"schema\" varchar(1024),\n"
    4571             :                                 "  \"table\" varchar(1024),\n"
    4572             :                                 "  \"column\" varchar(1024),\n"
    4573             :                                 "  \"type\" varchar(1024),\n"
    4574             :                                 "  \"width\" integer,\n"
    4575             :                                 "  \"count\" bigint,\n"
    4576             :                                 "  \"unique\" boolean,\n"
    4577             :                                 "  \"nils\" boolean,\n"
    4578             :                                 "  \"minval\" string,\n"
    4579             :                                 "  \"maxval\" string,\n"
    4580             :                                 "  \"sorted\" boolean,\n"
    4581             :                                 "  \"revsorted\" boolean\n"
    4582             :                                 ")\n"
    4583             :                                 "external name sql.\"statistics\";\n"
    4584             :                                 "grant execute on function sys.\"statistics\"(varchar(1024),varchar(1024),varchar(1024)) to public;\n"
    4585             :                                 "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'statistics';\n"
    4586             :                                 "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('analyze','statistics');\n");
    4587             : 
    4588           0 :         assert(pos < bufsize);
    4589           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    4590           0 :         fflush(stdout);
    4591           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    4592             : 
    4593           0 :         GDKfree(buf);
    4594           0 :         return err;             /* usually MAL_SUCCEED */
    4595             : }
    4596             : 
    4597             : static str
    4598         106 : sql_update_sep2022(Client c, mvc *sql, sql_schema *s)
    4599             : {
    4600         106 :         size_t bufsize = 65536, pos = 0;
    4601         106 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    4602         106 :         res_table *output;
    4603         106 :         BAT *b;
    4604             : 
    4605         106 :         if (buf == NULL)
    4606           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4607             : 
    4608             :         /* if sys.db_user_info does not have a column password, we need to
    4609             :          * add a bunch of columns */
    4610         106 :         sql_table *db_user_info = find_sql_table(sql->session->tr, s, "db_user_info");
    4611         106 :         if (find_sql_column(db_user_info, "password") == NULL) {
    4612           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4613             :                                                 "alter table sys.db_user_info add column max_memory bigint;\n"
    4614             :                                                 "alter table sys.db_user_info add column max_workers int;\n"
    4615             :                                                 "alter table sys.db_user_info add column optimizer varchar(1024);\n"
    4616             :                                                 "alter table sys.db_user_info add column default_role int;\n"
    4617             :                                                 "alter table sys.db_user_info add column password varchar(256);\n");
    4618           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4619             :                                                 "update sys.db_user_info u set max_memory = 0, max_workers = 0, optimizer = 'default_pipe', default_role = (select id from sys.auths a where a.name = u.name);\n");
    4620           0 :                 int endprint = (int) pos;
    4621           0 :                 bat bid;
    4622           0 :                 BAT *u = NULL, *p = NULL, *d = NULL;
    4623           0 :                 if ((bid = BBPindex("M5system_auth_user")) == 0 ||
    4624           0 :                         (u = BATdescriptor(bid)) == NULL ||
    4625           0 :                         (bid = BBPindex("M5system_auth_passwd_v2")) == 0 ||
    4626           0 :                         (p = BATdescriptor(bid)) == NULL ||
    4627           0 :                         (bid = BBPindex("M5system_auth_deleted")) == 0 ||
    4628           0 :                         (d = BATdescriptor(bid)) == NULL) {
    4629           0 :                         BBPreclaim(u);
    4630           0 :                         BBPreclaim(p);
    4631           0 :                         BBPreclaim(d);
    4632           0 :                         throw(SQL, __func__, INTERNAL_BAT_ACCESS);
    4633             :                 }
    4634           0 :                 BATiter ui = bat_iterator(u);
    4635           0 :                 BATiter pi = bat_iterator(p);
    4636           0 :                 for (oid i = 0; i < ui.count; i++) {
    4637           0 :                         if (BUNfnd(d, &i) == BUN_NONE) {
    4638           0 :                                 const char *user = BUNtvar(ui, i);
    4639           0 :                                 const char *pass = BUNtvar(pi, i);
    4640           0 :                                 if (pos + 4 * (strlen(user) + strlen(pass)) + 64 >= bufsize) {
    4641           0 :                                         char *nbuf = GDKrealloc(buf, bufsize + 65536);
    4642           0 :                                         if (nbuf == NULL) {
    4643           0 :                                                 err = createException(SQL, __func__, MAL_MALLOC_FAIL);
    4644           0 :                                                 break;
    4645             :                                         }
    4646             :                                         buf = nbuf;
    4647             :                                         bufsize += 65536;
    4648             :                                 }
    4649           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    4650             :                                                                 "update sys.db_user_info set password = e'");
    4651           0 :                                 for (const char *p = pass; *p; p++) {
    4652           0 :                                         if (*p < '\040' || *p >= '\177') {
    4653             :                                                 /* control character or high bit set */
    4654           0 :                                                 pos += snprintf(buf + pos, bufsize - pos,
    4655             :                                                                                 "\\%03o", (unsigned char) *p);
    4656             :                                         } else {
    4657           0 :                                                 if (*p == '\\' || *p == '\'')
    4658           0 :                                                         buf[pos++] = *p;
    4659           0 :                                                 buf[pos++] = *p;
    4660             :                                         }
    4661             :                                 }
    4662           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    4663             :                                                                 "' where name = e'");
    4664           0 :                                 for (const char *p = user; *p; p++) {
    4665           0 :                                         if (*p < '\040' || *p >= '\177') {
    4666             :                                                 /* control character or high bit set */
    4667           0 :                                                 pos += snprintf(buf + pos, bufsize - pos,
    4668             :                                                                                 "\\%03o", (unsigned char) *p);
    4669             :                                         } else {
    4670           0 :                                                 if (*p == '\\' || *p == '\'')
    4671           0 :                                                         buf[pos++] = *p;
    4672           0 :                                                 buf[pos++] = *p;
    4673             :                                         }
    4674             :                                 }
    4675           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    4676             :                                                                 "';\n");
    4677             :                         }
    4678             :                 }
    4679           0 :                 if (err == MAL_SUCCEED) {
    4680           0 :                         assert(pos < bufsize);
    4681           0 :                         printf("Running database upgrade commands:\n%.*s-- and copying passwords\n\n", endprint, buf);
    4682           0 :                         fflush(stdout);
    4683           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    4684             :                 }
    4685           0 :                 bat_iterator_end(&ui);
    4686           0 :                 bat_iterator_end(&pi);
    4687           0 :                 bat authbats[4];
    4688           0 :                 authbats[0] = 0;
    4689           0 :                 authbats[1] = u->batCacheid;
    4690           0 :                 authbats[2] = p->batCacheid;
    4691           0 :                 authbats[3] = d->batCacheid;
    4692           0 :                 if (err == MAL_SUCCEED &&
    4693           0 :                         (BATmode(u, true) != GDK_SUCCEED ||
    4694           0 :                          BATmode(p, true) != GDK_SUCCEED ||
    4695           0 :                          BATmode(d, true) != GDK_SUCCEED ||
    4696           0 :                          BBPrename(u, NULL) != 0 ||
    4697           0 :                          BBPrename(p, NULL) != 0 ||
    4698           0 :                          BBPrename(d, NULL) != 0 ||
    4699           0 :                          TMsubcommit_list(authbats, NULL, 4, -1) != GDK_SUCCEED)) {
    4700           0 :                                 fprintf(stderr, "Committing removal of old user/password BATs failed\n");
    4701             :                 }
    4702           0 :                 BBPunfix(u->batCacheid);
    4703           0 :                 BBPunfix(p->batCacheid);
    4704           0 :                 BBPunfix(d->batCacheid);
    4705             : 
    4706           0 :                 if (err == MAL_SUCCEED) {
    4707           0 :                         sql_schema *s = mvc_bind_schema(sql, "sys");
    4708           0 :                         sql_table *t = mvc_bind_table(sql, s, "roles");
    4709           0 :                         t->system = 0;
    4710           0 :                         t = mvc_bind_table(sql, s, "users");
    4711           0 :                         t->system = 0;
    4712           0 :                         t = mvc_bind_table(sql, s, "dependency_schemas_on_users");
    4713           0 :                         t->system = 0;
    4714           0 :                         pos = 0;
    4715           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    4716             :                                                         "drop view sys.dependency_schemas_on_users cascade;\n"
    4717             :                                                         "drop view sys.roles cascade;\n"
    4718             :                                                         "drop view sys.users cascade;\n"
    4719             :                                                         "drop function sys.db_users() cascade;\n"
    4720             :                                                         "CREATE VIEW sys.roles AS SELECT id, name, grantor FROM sys.auths a WHERE a.name NOT IN (SELECT u.name FROM sys.db_user_info u);\n"
    4721             :                                                         "GRANT SELECT ON sys.roles TO PUBLIC;\n"
    4722             :                                                         "CREATE VIEW sys.users AS SELECT name, fullname, default_schema, schema_path, max_memory, max_workers, optimizer, default_role FROM sys.db_user_info;\n"
    4723             :                                                         "GRANT SELECT ON sys.users TO PUBLIC;\n"
    4724             :                                                         "CREATE FUNCTION sys.db_users() RETURNS TABLE(name varchar(2048)) RETURN SELECT name FROM sys.db_user_info;\n"
    4725             :                                                         "CREATE VIEW sys.dependency_schemas_on_users AS\n"
    4726             :                                                         "SELECT s.id AS schema_id, s.name AS schema_name, u.name AS user_name, CAST(6 AS smallint) AS depend_type\n"
    4727             :                                                         " FROM sys.db_user_info AS u, sys.schemas AS s\n"
    4728             :                                                         " WHERE u.default_schema = s.id\n"
    4729             :                                                         " ORDER BY s.name, u.name;\n"
    4730             :                                                         "GRANT SELECT ON sys.dependency_schemas_on_users TO PUBLIC;\n"
    4731             :                                                         "update sys._tables set system = true where name in ('users', 'roles', 'dependency_schemas_on_users') AND schema_id = 2000;\n"
    4732             :                                                         "update sys.functions set system = true where system <> true and name in ('db_users') and schema_id = 2000 and type = %d;\n", F_UNION);
    4733           0 :                         assert(pos < bufsize);
    4734           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    4735           0 :                         fflush(stdout);
    4736           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    4737             :                 }
    4738             :         }
    4739         106 :         if (err != MAL_SUCCEED) {
    4740           0 :                 GDKfree(buf);
    4741           0 :                 return err;
    4742             :         }
    4743             : 
    4744             :         /* if 'describe_partition_tables' system view doesn't use 'vals'
    4745             :          * CTE, re-create it; while we're at it, also update the sequence
    4746             :          * dumping code */
    4747         106 :         pos = snprintf(buf, bufsize,
    4748             :                         "select 1 from sys.tables where schema_id = (select \"id\" from sys.schemas where \"name\" = 'sys') and \"name\" = 'describe_partition_tables' and \"query\" not like '%%vals%%';\n");
    4749         106 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output))) {
    4750           0 :                 GDKfree(buf);
    4751           0 :                 return err;
    4752             :         }
    4753             : 
    4754         106 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) > 0) {
    4755             :                 /* 52_describe.sql; but we need to drop dependencies from 76_dump.sql first */
    4756           0 :                 sql_schema *s = mvc_bind_schema(sql, "sys");
    4757           0 :                 sql_table *t = mvc_bind_table(sql, s, "describe_partition_tables");
    4758           0 :                 t->system = 0;
    4759           0 :                 t = mvc_bind_table(sql, s, "dump_partition_tables");
    4760           0 :                 t->system = 0;
    4761           0 :                 t = mvc_bind_table(sql, s, "dump_sequences");
    4762           0 :                 t->system = 0;
    4763           0 :                 t = mvc_bind_table(sql, s, "dump_start_sequences");
    4764           0 :                 t->system = 0;
    4765           0 :                 t = mvc_bind_table(sql, s, "describe_tables");
    4766           0 :                 t->system = 0;
    4767           0 :                 t = mvc_bind_table(sql, s, "dump_tables");
    4768           0 :                 t->system = 0;
    4769           0 :                 t = mvc_bind_table(sql, s, "dump_create_users");
    4770           0 :                 t->system = 0;
    4771           0 :                 t = mvc_bind_table(sql, s, "dump_functions");
    4772           0 :                 t->system = 0;
    4773           0 :                 t = mvc_bind_table(sql, s, "dump_triggers");
    4774           0 :                 t->system = 0;
    4775             : 
    4776           0 :                 pos = 0;
    4777           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4778             :                         /* drop dependent stuff from 76_dump.sql */
    4779             :                         "drop function sys.dump_database(boolean) cascade;\n"
    4780             :                         "drop procedure sys.dump_table_data() cascade;\n"
    4781             :                         "drop procedure sys.dump_table_data(string, string) cascade;\n"
    4782             :                         "drop view sys.dump_partition_tables cascade;\n"
    4783             :                         "drop view sys.describe_partition_tables cascade;\n"
    4784             :                         "drop view sys.dump_sequences cascade;\n"
    4785             :                         "drop view sys.dump_start_sequences cascade;\n"
    4786             :                         "drop view sys.dump_tables cascade;\n"
    4787             :                         "drop view sys.describe_tables cascade;\n"
    4788             :                         "drop view sys.dump_create_users cascade;\n"
    4789             :                         "drop view sys.dump_functions cascade;\n"
    4790             :                         "drop view sys.dump_triggers cascade;\n"
    4791             :                         "drop function sys.schema_guard cascade;\n"
    4792             :                         "drop function sys.replace_first(string, string, string, string) cascade;\n");
    4793             : 
    4794           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4795             :                         "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
    4796             :                         "RETURN\n"
    4797             :                         " SELECT 'SET SCHEMA ' || sys.dq(sch) || '; ' || stmt;\n"
    4798             :                         "END;\n"
    4799             :                         "CREATE VIEW sys.dump_functions AS\n"
    4800             :                         " SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
    4801             :                         " f.sch schema_name,\n"
    4802             :                         " f.fun function_name\n"
    4803             :                         " FROM sys.describe_functions f;\n"
    4804             :                         "CREATE VIEW sys.dump_triggers AS\n"
    4805             :                         " SELECT sys.schema_guard(sch, tab, def) stmt,\n"
    4806             :                         " sch schema_name,\n"
    4807             :                         " tab table_name,\n"
    4808             :                         " tri trigger_name\n"
    4809             :                         " FROM sys.describe_triggers;\n"
    4810             :                         "CREATE VIEW sys.describe_partition_tables AS\n"
    4811             :                         " SELECT\n"
    4812             :                         " m_sch,\n"
    4813             :                         " m_tbl,\n"
    4814             :                         " p_sch,\n"
    4815             :                         " p_tbl,\n"
    4816             :                         " CASE\n"
    4817             :                         " WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    4818             :                         " WHEN (p_raw_type = 'VALUES' AND pvalues IS NULL) OR (p_raw_type = 'RANGE' AND minimum IS NULL AND maximum IS NULL AND with_nulls) THEN 'FOR NULLS'\n"
    4819             :                         " ELSE p_raw_type\n"
    4820             :                         " END AS tpe,\n"
    4821             :                         " pvalues,\n"
    4822             :                         " minimum,\n"
    4823             :                         " maximum,\n"
    4824             :                         " with_nulls\n"
    4825             :                         " FROM\n"
    4826             :                         " (WITH\n"
    4827             :                         " tp(\"type\", table_id) AS\n"
    4828             :                         " (SELECT ifthenelse((table_partitions.\"type\" & 2) = 2, 'VALUES', 'RANGE'), table_partitions.table_id FROM sys.table_partitions),\n"
    4829             :                         " subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    4830             :                         " (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    4831             :                         " FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    4832             :                         " WHERE m_t.\"type\" IN (3, 6)\n"
    4833             :                         " AND m_t.schema_id = m_s.id\n"
    4834             :                         " AND m_s.name <> 'tmp'\n"
    4835             :                         " AND m_t.system = FALSE\n"
    4836             :                         " AND m_t.id = d.depend_id\n"
    4837             :                         " AND d.id = p_m.id\n"
    4838             :                         " AND p_m.schema_id = p_s.id\n"
    4839             :                         " ORDER BY m_t.id, p_m.id),\n"
    4840             :                         " vals(id,vals) as\n"
    4841             :                         " (SELECT vp.table_id, GROUP_CONCAT(vp.value, ',') FROM sys.value_partitions vp GROUP BY vp.table_id)\n"
    4842             :                         " SELECT\n"
    4843             :                         " subq.m_sch,\n"
    4844             :                         " subq.m_tbl,\n"
    4845             :                         " subq.p_sch,\n"
    4846             :                         " subq.p_tbl,\n"
    4847             :                         " tp.\"type\" AS p_raw_type,\n"
    4848             :                         " CASE WHEN tp.\"type\" = 'VALUES'\n"
    4849             :                         " THEN (SELECT vals.vals FROM vals WHERE vals.id = subq.p_mid)\n"
    4850             :                         " ELSE NULL\n"
    4851             :                         " END AS pvalues,\n"
    4852             :                         " CASE WHEN tp.\"type\" = 'RANGE'\n"
    4853             :                         " THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    4854             :                         " ELSE NULL\n"
    4855             :                         " END AS minimum,\n"
    4856             :                         " CASE WHEN tp.\"type\" = 'RANGE'\n"
    4857             :                         " THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    4858             :                         " ELSE NULL\n"
    4859             :                         " END AS maximum,\n"
    4860             :                         " CASE WHEN tp.\"type\" = 'VALUES'\n"
    4861             :                         " THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    4862             :                         " ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    4863             :                         " END AS with_nulls\n"
    4864             :                         " FROM\n"
    4865             :                         " subq LEFT OUTER JOIN tp\n"
    4866             :                         " ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    4867             :                         "GRANT SELECT ON sys.describe_partition_tables TO PUBLIC;\n"
    4868             :                         "CREATE VIEW sys.dump_partition_tables AS\n"
    4869             :                         "SELECT\n"
    4870             :                         " 'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    4871             :                         " CASE\n"
    4872             :                         " WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    4873             :                         " WHEN tpe = 'RANGE' THEN ' AS PARTITION FROM ' || ifthenelse(minimum IS NOT NULL, sys.SQ(minimum), 'RANGE MINVALUE') || ' TO ' || ifthenelse(maximum IS NOT NULL, sys.SQ(maximum), 'RANGE MAXVALUE')\n"
    4874             :                         " WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    4875             :                         " ELSE '' --'READ ONLY'\n"
    4876             :                         " END ||\n"
    4877             :                         " CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    4878             :                         " ';' stmt,\n"
    4879             :                         " m_sch merge_schema_name,\n"
    4880             :                         " m_tbl merge_table_name,\n"
    4881             :                         " p_sch partition_schema_name,\n"
    4882             :                         " p_tbl partition_table_name\n"
    4883             :                         " FROM sys.describe_partition_tables;\n"
    4884             :                         "CREATE VIEW sys.dump_sequences AS\n"
    4885             :                         " SELECT\n"
    4886             :                         " 'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT;' stmt,\n"
    4887             :                         " sch schema_name,\n"
    4888             :                         " seq seqname\n"
    4889             :                         " FROM sys.describe_sequences;\n"
    4890             :                         "CREATE VIEW sys.dump_start_sequences AS\n"
    4891             :                         " SELECT 'ALTER SEQUENCE ' || sys.FQN(sch, seq) ||\n"
    4892             :                         " CASE WHEN s = 0 THEN '' ELSE ' RESTART WITH ' || rs END ||\n"
    4893             :                         " CASE WHEN inc = 1 THEN '' ELSE ' INCREMENT BY ' || inc END ||\n"
    4894             :                         " CASE WHEN nomin THEN ' NO MINVALUE' WHEN rmi IS NULL THEN '' ELSE ' MINVALUE ' || rmi END ||\n"
    4895             :                         " CASE WHEN nomax THEN ' NO MAXVALUE' WHEN rma IS NULL THEN '' ELSE ' MAXVALUE ' || rma END ||\n"
    4896             :                         " CASE WHEN \"cache\" = 1 THEN '' ELSE ' CACHE ' || \"cache\" END ||\n"
    4897             :                         " CASE WHEN \"cycle\" THEN '' ELSE ' NO' END || ' CYCLE;' stmt,\n"
    4898             :                         " sch schema_name,\n"
    4899             :                         " seq sequence_name\n"
    4900             :                         " FROM sys.describe_sequences;\n"
    4901             :                         "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
    4902             :                         "BEGIN\n"
    4903             :                         " DECLARE tid INT;\n"
    4904             :                         " SET tid = (SELECT MIN(t.id) FROM sys.tables t, sys.schemas s WHERE t.name = tbl AND t.schema_id = s.id AND s.name = sch);\n"
    4905             :                         " IF tid IS NOT NULL THEN\n"
    4906             :                         " DECLARE k INT;\n"
    4907             :                         " DECLARE m INT;\n"
    4908             :                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    4909             :                         " SET m = (SELECT MAX(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    4910             :                         " IF k IS NOT NULL AND m IS NOT NULL THEN\n"
    4911             :                         " DECLARE cname STRING;\n"
    4912             :                         " DECLARE ctype STRING;\n"
    4913             :                         " DECLARE _cnt INT;\n"
    4914             :                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4915             :                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4916             :                         " SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
    4917             :                         " IF _cnt > 0 THEN\n"
    4918             :                         " DECLARE COPY_INTO_STMT STRING;\n"
    4919             :                         " DECLARE SELECT_DATA_STMT STRING;\n"
    4920             :                         " SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    4921             :                         " SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    4922             :                         " WHILE (k < m) DO\n"
    4923             :                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid AND c.id > k);\n"
    4924             :                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4925             :                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4926             :                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    4927             :                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype));\n"
    4928             :                         " END WHILE;\n"
    4929             :                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    4930             :                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl));\n"
    4931             :                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    4932             :                         " CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    4933             :                         " END IF;\n"
    4934             :                         " END IF;\n"
    4935             :                         " END IF;\n"
    4936             :                         "END;\n"
    4937             :                         "CREATE PROCEDURE sys.dump_table_data()\n"
    4938             :                         "BEGIN\n"
    4939             :                         " DECLARE i INT;\n"
    4940             :                         " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    4941             :                         " IF i IS NOT NULL THEN\n"
    4942             :                         " DECLARE M INT;\n"
    4943             :                         " SET M = (SELECT MAX(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    4944             :                         " DECLARE sch STRING;\n"
    4945             :                         " DECLARE tbl STRING;\n"
    4946             :                         " WHILE i IS NOT NULL AND i <= M DO\n"
    4947             :                         " SET sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4948             :                         " SET tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4949             :                         " CALL sys.dump_table_data(sch, tbl);\n"
    4950             :                         " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system AND t.id > i);\n"
    4951             :                         " END WHILE;\n"
    4952             :                         " END IF;\n"
    4953             :                         "END;\n"
    4954             :                         "CREATE VIEW sys.dump_create_users AS\n"
    4955             :                         " SELECT\n"
    4956             :                         " 'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
    4957             :                         " sys.sq(sys.password_hash(ui.name)) ||\n"
    4958             :                         " ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
    4959             :                         " ui.name user_name\n"
    4960             :                         " FROM sys.db_user_info ui, sys.schemas s\n"
    4961             :                         " WHERE ui.default_schema = s.id\n"
    4962             :                         " AND ui.name <> 'monetdb'\n"
    4963             :                         " AND ui.name <> '.snapshot';\n");
    4964             : 
    4965           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4966             :                         "CREATE VIEW sys.describe_tables AS\n"
    4967             :                         " SELECT\n"
    4968             :                         " t.id o,\n"
    4969             :                         " s.name sch,\n"
    4970             :                         " t.name tab,\n"
    4971             :                         " ts.table_type_name typ,\n"
    4972             :                         " (SELECT\n"
    4973             :                         " ' (' ||\n"
    4974             :                         " GROUP_CONCAT(\n"
    4975             :                         " sys.DQ(c.name) || ' ' ||\n"
    4976             :                         " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    4977             :                         " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    4978             :                         " , ', ') || ')'\n"
    4979             :                         " FROM sys._columns c\n"
    4980             :                         " WHERE c.table_id = t.id) col,\n"
    4981             :                         " CASE ts.table_type_name\n"
    4982             :                         " WHEN 'REMOTE TABLE' THEN\n"
    4983             :                         " sys.get_remote_table_expressions(s.name, t.name)\n"
    4984             :                         " WHEN 'MERGE TABLE' THEN\n"
    4985             :                         " sys.get_merge_table_partition_expressions(t.id)\n"
    4986             :                         " WHEN 'VIEW' THEN\n"
    4987             :                         " sys.schema_guard(s.name, t.name, t.query)\n"
    4988             :                         " ELSE\n"
    4989             :                         " ''\n"
    4990             :                         " END opt\n"
    4991             :                         " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    4992             :                         " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
    4993             :                         " AND t.system = FALSE\n"
    4994             :                         " AND s.id = t.schema_id\n"
    4995             :                         " AND ts.table_type_id = t.type\n"
    4996             :                         " AND s.name <> 'tmp';\n"
    4997             :                         "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
    4998             :                         "CREATE VIEW sys.dump_tables AS\n"
    4999             :                         " SELECT\n"
    5000             :                         " t.o o,\n"
    5001             :                         " CASE\n"
    5002             :                         " WHEN t.typ <> 'VIEW' THEN\n"
    5003             :                         " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    5004             :                         " ELSE\n"
    5005             :                         " t.opt\n"
    5006             :                         " END stmt,\n"
    5007             :                         " t.sch schema_name,\n"
    5008             :                         " t.tab table_name\n"
    5009             :                         " FROM sys.describe_tables t;\n"
    5010             :                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    5011             :                         "BEGIN\n"
    5012             :                         " SET SCHEMA sys;\n"
    5013             :                         " TRUNCATE sys.dump_statements;\n"
    5014             :                         " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    5015             :                         " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
    5016             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    5017             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    5018             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    5019             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    5020             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    5021             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    5022             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    5023             :                         " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    5024             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    5025             :                         " FROM (\n"
    5026             :                         " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    5027             :                         " UNION ALL\n"
    5028             :                         " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    5029             :                         " ) AS stmts(o, s);\n"
    5030             :                         " -- dump table data before adding constraints and fixing sequences\n"
    5031             :                         " IF NOT DESCRIBE THEN\n"
    5032             :                         " CALL sys.dump_table_data();\n"
    5033             :                         " END IF;\n"
    5034             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    5035             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    5036             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    5037             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    5038             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    5039             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    5040             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    5041             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    5042             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    5043             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    5044             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    5045             :                         " --TODO Improve performance of dump_table_data.\n"
    5046             :                         " --TODO loaders ,procedures, window and filter sys.functions.\n"
    5047             :                         " --TODO look into order dependent group_concat\n"
    5048             :                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    5049             :                         " RETURN sys.dump_statements;\n"
    5050             :                         "END;\n");
    5051           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5052             :                         "update sys._tables set system = true where name in ('describe_partition_tables', 'dump_partition_tables', 'dump_sequences', 'dump_start_sequences', 'describe_tables', 'dump_tables', 'dump_create_users', 'dump_functions', 'dump_triggers') AND schema_id = 2000;\n");
    5053           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5054             :                         "update sys.functions set system = true where system <> true and name in ('dump_table_data') and schema_id = 2000 and type = %d;\n", F_PROC);
    5055           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5056             :                         "update sys.functions set system = true where system <> true and name in ('dump_database') and schema_id = 2000 and type = %d;\n", F_UNION);
    5057           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5058             :                         "update sys.functions set system = true where system <> true and name in ('schema_guard') and schema_id = 2000 and type = %d;\n", F_FUNC);
    5059             : 
    5060             :                 /* 12_url.sql */
    5061           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5062             :                                                 "CREATE function sys.url_extract_host(url string, no_www bool) RETURNS STRING\n"
    5063             :                                                 "EXTERNAL NAME url.\"extractURLHost\";\n"
    5064             :                                                 "GRANT EXECUTE ON FUNCTION url_extract_host(string, bool) TO PUBLIC;\n"
    5065             :                                                 "update sys.functions set system = true where system <> true and name = 'url_extract_host' and schema_id = 2000 and type = %d;\n", F_FUNC);
    5066             : 
    5067           0 :                 assert(pos < bufsize);
    5068           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5069           0 :                 fflush(stdout);
    5070           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5071             :         }
    5072         106 :         res_table_destroy(output);
    5073         106 :         output = NULL;
    5074         106 :         if (err != MAL_SUCCEED) {
    5075           0 :                 GDKfree(buf);
    5076           0 :                 return err;
    5077             :         }
    5078             : 
    5079             :         /* 10_sys_schema_extensions */
    5080             :         /* if the keyword LOCKED is in the list of keywords, upgrade */
    5081         106 :         pos = snprintf(buf, bufsize, "select keyword from sys.keywords where keyword = 'LOCKED';\n");
    5082         106 :         assert(pos < bufsize);
    5083         106 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    5084           0 :                 goto bailout;
    5085         106 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) > 0) {
    5086           0 :                 pos = snprintf(buf, bufsize,
    5087             :                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    5088             :                         "DELETE FROM sys.keywords WHERE keyword IN ('LOCKED');\n");
    5089           0 :                 assert(pos < bufsize);
    5090           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5091           0 :                 fflush(stdout);
    5092           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5093           0 :                 if (err == MAL_SUCCEED) {
    5094           0 :                         pos = snprintf(buf, bufsize, "ALTER TABLE sys.keywords SET READ ONLY;\n");
    5095           0 :                         assert(pos < bufsize);
    5096           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5097           0 :                         fflush(stdout);
    5098           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5099             :                 }
    5100             :         }
    5101         106 :         res_table_destroy(output);
    5102         106 :         output = NULL;
    5103         106 :         if (err != MAL_SUCCEED) {
    5104           0 :                 GDKfree(buf);
    5105           0 :                 return err;
    5106             :         }
    5107             : 
    5108             :         /* if the table type UNLOGGED TABLE is not in the list of table
    5109             :          * types, upgrade */
    5110         106 :         pos = snprintf(buf, bufsize, "select table_type_name from sys.table_types where table_type_name = 'UNLOGGED TABLE';\n");
    5111         106 :         assert(pos < bufsize);
    5112         106 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    5113           0 :                 goto bailout;
    5114         106 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
    5115           0 :                 pos = snprintf(buf, bufsize,
    5116             :                                 "ALTER TABLE sys.table_types SET READ WRITE;\n"
    5117             :                                 "INSERT INTO sys.table_types VALUES (7, 'UNLOGGED TABLE');\n");
    5118           0 :                 assert(pos < bufsize);
    5119           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5120           0 :                 fflush(stdout);
    5121           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5122           0 :                 if (err == MAL_SUCCEED) {
    5123           0 :                         pos = snprintf(buf, bufsize, "ALTER TABLE sys.table_types SET READ ONLY;\n");
    5124           0 :                         assert(pos < bufsize);
    5125           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5126           0 :                         fflush(stdout);
    5127           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5128             :                 }
    5129             :         }
    5130         106 :         res_table_destroy(output);
    5131         106 :         output = NULL;
    5132             : 
    5133             :         /* 16_tracelog */
    5134         106 :         pos = snprintf(buf, bufsize,
    5135             :                                    "select f.id "
    5136             :                                    "from sys.schemas s, "
    5137             :                                                 "sys.functions f, "
    5138             :                                                 "sys.auths a, "
    5139             :                                                 "sys.privileges p, "
    5140             :                                                 "sys.auths g, "
    5141             :                                                 "sys.function_types ft, "
    5142             :                                                 "sys.privilege_codes pc "
    5143             :                                    "where s.id = f.schema_id "
    5144             :                                          "and f.id = p.obj_id "
    5145             :                                          "and p.auth_id = a.id "
    5146             :                                          "and p.grantor = g.id "
    5147             :                                          "and p.privileges = pc.privilege_code_id "
    5148             :                                          "and f.type = ft.function_type_id "
    5149             :                                          "and s.name = 'sys' "
    5150             :                                          "and f.name = 'tracelog' "
    5151             :                                          "and ft.function_type_keyword = 'FUNCTION';\n");
    5152         106 :         assert(pos < bufsize);
    5153         106 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    5154           0 :                 goto bailout;
    5155         106 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
    5156           0 :                 pos = snprintf(buf, bufsize,
    5157             :                                            "grant execute on function sys.tracelog to public;\n"
    5158             :                                            "grant select on sys.tracelog to public;\n");
    5159           0 :                 assert(pos < bufsize);
    5160           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5161           0 :                 fflush(stdout);
    5162           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5163             :         }
    5164         106 :         res_table_destroy(output);
    5165         106 :         output = NULL;
    5166         106 :         if (err != MAL_SUCCEED) {
    5167           0 :                 GDKfree(buf);
    5168           0 :                 return err;
    5169             :         }
    5170             : 
    5171         106 : bailout:
    5172         106 :         if (output)
    5173           0 :                 res_table_destroy(output);
    5174         106 :         GDKfree(buf);
    5175         106 :         return err;             /* usually MAL_SUCCEED */
    5176             : }
    5177             : 
    5178             : static str
    5179         106 : sql_update_jun2023(Client c, mvc *sql, sql_schema *s)
    5180             : {
    5181         106 :         size_t bufsize = 65536, pos = 0;
    5182         106 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    5183         106 :         res_table *output;
    5184         106 :         BAT *b;
    5185         106 :         sql_subtype t1, t2;
    5186             : 
    5187         106 :         (void) sql;
    5188         106 :         if (buf == NULL)
    5189           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5190             : 
    5191             :         /* wlc/wlr support was removed */
    5192             :         {
    5193         106 :                 sql_schema *wl = mvc_bind_schema(sql, "wlc");
    5194         106 :                 sql_schema *wr = mvc_bind_schema(sql, "wlr");
    5195         106 :                 if (wl != NULL || wr != NULL) {
    5196           0 :                         if (wl)
    5197           0 :                                 wl->system = 0;
    5198           0 :                         if (wr)
    5199           0 :                                 wr->system = 0;
    5200             : 
    5201           0 :                         const char *query =
    5202             :                                 "drop procedure if exists wlc.master() cascade;\n"
    5203             :                                 "drop procedure if exists wlc.master(string) cascade;\n"
    5204             :                                 "drop procedure if exists wlc.stop() cascade;\n"
    5205             :                                 "drop procedure if exists wlc.flush() cascade;\n"
    5206             :                                 "drop procedure if exists wlc.beat(int) cascade;\n"
    5207             :                                 "drop function if exists wlc.clock() cascade;\n"
    5208             :                                 "drop function if exists wlc.tick() cascade;\n"
    5209             :                                 "drop procedure if exists wlr.master(string) cascade;\n"
    5210             :                                 "drop procedure if exists wlr.stop() cascade;\n"
    5211             :                                 "drop procedure if exists wlr.accept() cascade;\n"
    5212             :                                 "drop procedure if exists wlr.replicate() cascade;\n"
    5213             :                                 "drop procedure if exists wlr.replicate(timestamp) cascade;\n"
    5214             :                                 "drop procedure if exists wlr.replicate(tinyint) cascade;\n"
    5215             :                                 "drop procedure if exists wlr.replicate(smallint) cascade;\n"
    5216             :                                 "drop procedure if exists wlr.replicate(integer) cascade;\n"
    5217             :                                 "drop procedure if exists wlr.replicate(bigint) cascade;\n"
    5218             :                                 "drop procedure if exists wlr.beat(integer) cascade;\n"
    5219             :                                 "drop function if exists wlr.clock() cascade;\n"
    5220             :                                 "drop function if exists wlr.tick() cascade;\n"
    5221             :                                 "drop schema if exists wlc cascade;\n"
    5222             :                                 "drop schema if exists wlr cascade;\n";
    5223           0 :                         printf("Running database upgrade commands:\n%s\n", query);
    5224           0 :                         fflush(stdout);
    5225           0 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5226             :                 }
    5227             :         }
    5228             : 
    5229             :         /* new function sys.regexp_replace */
    5230         106 :         allocator *old_sa = sql->sa;
    5231         106 :         if ((sql->sa = sa_create(sql->pa)) != NULL) {
    5232         106 :                 list *l;
    5233         106 :                 if ((l = sa_list(sql->sa)) != NULL) {
    5234         106 :                         sql_subtype tp;
    5235         106 :                         sql_find_subtype(&tp, "varchar", 0, 0);
    5236         106 :                         list_append(l, &tp);
    5237         106 :                         list_append(l, &tp);
    5238         106 :                         list_append(l, &tp);
    5239         106 :                         list_append(l, &tp);
    5240         106 :                         if (!sql_bind_func_(sql, s->base.name, "regexp_replace", l, F_FUNC, true, true)) {
    5241           0 :                                 pos = snprintf(buf, bufsize,
    5242             :                                                            "create function sys.regexp_replace(ori string, pat string, rep string, flg string)\n"
    5243             :                                                            "returns string external name pcre.replace;\n"
    5244             :                                                            "grant execute on function regexp_replace(string, string, string, string) to public;\n"
    5245             :                                                            "create function sys.regexp_replace(ori string, pat string, rep string)\n"
    5246             :                                                            "returns string\n"
    5247             :                                                            "begin\n"
    5248             :                                                            " return sys.regexp_replace(ori, pat, rep, '');\n"
    5249             :                                                            "end;\n"
    5250             :                                                            "grant execute on function regexp_replace(string, string, string) to public;\n"
    5251             :                                                            "update sys.functions set system = true where system <> true and name = 'regexp_replace' and schema_id = 2000 and type = %d;\n",
    5252             :                                                            F_FUNC);
    5253           0 :                                 assert(pos < bufsize);
    5254           0 :                                 sql->session->status = 0;
    5255           0 :                                 sql->errstr[0] = '\0';
    5256           0 :                                 printf("Running database upgrade commands:\n%s\n", buf);
    5257           0 :                                 fflush(stdout);
    5258           0 :                                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5259             :                         }
    5260         106 :                         sa_destroy(sql->sa);
    5261             :                 }
    5262             :         }
    5263         106 :         sql->sa = old_sa;
    5264             : 
    5265             :         /* fixes for handling single quotes in strings so that we can run
    5266             :          * with raw_strings after having created a database without (and
    5267             :          * v.v.) */
    5268         106 :         if ((err = SQLstatementIntern(c, "select id from sys.functions where name = 'dump_table_data' and schema_id = 2000 and func like '% R'')%';\n", "update", true, false, &output)) == NULL) {
    5269         106 :                 if (((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) || find_sql_table(sql->session->tr, s, "remote_user_info") == NULL) {
    5270           0 :                         sql_table *t;
    5271           0 :                         if ((t = mvc_bind_table(sql, s, "describe_tables")) != NULL)
    5272           0 :                                 t->system = 0;
    5273           0 :                         if ((t = mvc_bind_table(sql, s, "dump_create_users")) != NULL)
    5274           0 :                                 t->system = 0;
    5275           0 :                         if ((t = mvc_bind_table(sql, s, "dump_partition_tables")) != NULL)
    5276           0 :                                 t->system = 0;
    5277           0 :                         if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
    5278           0 :                                 t->system = 0;
    5279           0 :                         if ((t = mvc_bind_table(sql, s, "dump_tables")) != NULL)
    5280           0 :                                 t->system = 0;
    5281           0 :                         pos = 0;
    5282           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    5283             :                                                         "drop function if exists sys.dump_database(boolean) cascade;\n"
    5284             :                                                         "drop procedure if exists sys.dump_table_data() cascade;\n"
    5285             :                                                         "drop procedure if exists sys.dump_table_data(string, string) cascade;\n"
    5286             :                                                         "drop view if exists sys.dump_tables cascade;\n"
    5287             :                                                         "drop view if exists sys.dump_comments cascade;\n"
    5288             :                                                         "drop function if exists sys.prepare_esc(string, string) cascade;\n"
    5289             :                                                         "drop view if exists sys.dump_partition_tables cascade;\n"
    5290             :                                                         "drop view if exists sys.dump_create_users cascade;\n"
    5291             :                                                         "drop view if exists sys.describe_tables cascade;\n"
    5292             :                                                         "drop function if exists sys.get_remote_table_expressions(string, string) cascade;\n"
    5293             :                                                         "drop function if exists sys.remote_table_credentials(string) cascade;\n"
    5294             :                                                         "drop function if exists sys.sq(string) cascade;\n");
    5295           0 :                         if (find_sql_table(sql->session->tr, s, "remote_user_info") == NULL) {
    5296           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    5297             :                                                                 "create table sys.remote_user_info (table_id int, username varchar(1024), password varchar(256));\n"
    5298             :                                                                 "create function sys.decypher (cypher string) returns string external name sql.decypher;\n"
    5299             :                                                                 "update sys.functions set system = true where system <> true and name = 'decypher' and schema_id = 2000 and type = %d;\n"
    5300             :                                                                 "update sys._tables set system = true where system <> true and name = 'remote_user_info' and schema_id = 2000;\n",
    5301             :                                                                 F_FUNC);
    5302             :                         }
    5303           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    5304             :                                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN '''' || sys.replace(s,'''','''''') || ''''; END;\n"
    5305             :                                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    5306             :                                                         " RETURN SELECT ' ON ' || sys.SQ(tt.query) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(sys.decypher(\"password\")) FROM sys.remote_user_info r, sys._tables tt, sys.schemas ss where tt.name = t and ss.name = s and tt.schema_id = ss.id and r.table_id = tt.id;\n"
    5307             :                                                         "END;\n"
    5308             :                                                         "CREATE VIEW sys.describe_tables AS\n"
    5309             :                                                         " SELECT\n"
    5310             :                                                         " t.id o,\n"
    5311             :                                                         " s.name sch,\n"
    5312             :                                                         " t.name tab,\n"
    5313             :                                                         " ts.table_type_name typ,\n"
    5314             :                                                         " (SELECT\n"
    5315             :                                                         " ' (' ||\n"
    5316             :                                                         " GROUP_CONCAT(\n"
    5317             :                                                         " sys.DQ(c.name) || ' ' ||\n"
    5318             :                                                         " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    5319             :                                                         " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    5320             :                                                         " , ', ') || ')'\n"
    5321             :                                                         " FROM sys._columns c\n"
    5322             :                                                         " WHERE c.table_id = t.id) col,\n"
    5323             :                                                         " CASE ts.table_type_name\n"
    5324             :                                                         " WHEN 'REMOTE TABLE' THEN\n"
    5325             :                                                         " sys.get_remote_table_expressions(s.name, t.name)\n"
    5326             :                                                         " WHEN 'MERGE TABLE' THEN\n"
    5327             :                                                         " sys.get_merge_table_partition_expressions(t.id)\n"
    5328             :                                                         " WHEN 'VIEW' THEN\n"
    5329             :                                                         " sys.schema_guard(s.name, t.name, t.query)\n"
    5330             :                                                         " ELSE\n"
    5331             :                                                         " ''\n"
    5332             :                                                         " END opt\n"
    5333             :                                                         " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    5334             :                                                         " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
    5335             :                                                         " AND t.system = FALSE\n"
    5336             :                                                         " AND s.id = t.schema_id\n"
    5337             :                                                         " AND ts.table_type_id = t.type\n"
    5338             :                                                         " AND s.name <> 'tmp';\n"
    5339             :                                                         "CREATE VIEW sys.dump_create_users AS\n"
    5340             :                                                         " SELECT\n"
    5341             :                                                         " 'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
    5342             :                                                         " sys.sq(sys.password_hash(ui.name)) ||\n"
    5343             :                                                         " ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
    5344             :                                                         " ui.name user_name\n"
    5345             :                                                         " FROM sys.db_user_info ui, sys.schemas s\n"
    5346             :                                                         " WHERE ui.default_schema = s.id\n"
    5347             :                                                         " AND ui.name <> 'monetdb'\n"
    5348             :                                                         " AND ui.name <> '.snapshot';\n"
    5349             :                                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    5350             :                                                         " SELECT\n"
    5351             :                                                         " 'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    5352             :                                                         " CASE\n"
    5353             :                                                         " WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    5354             :                                                         " WHEN tpe = 'RANGE' THEN ' AS PARTITION FROM ' || ifthenelse(minimum IS NOT NULL, sys.SQ(minimum), 'RANGE MINVALUE') || ' TO ' || ifthenelse(maximum IS NOT NULL, sys.SQ(maximum), 'RANGE MAXVALUE')\n"
    5355             :                                                         " WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    5356             :                                                         " ELSE '' --'READ ONLY'\n"
    5357             :                                                         " END ||\n"
    5358             :                                                         " CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    5359             :                                                         " ';' stmt,\n"
    5360             :                                                         " m_sch merge_schema_name,\n"
    5361             :                                                         " m_tbl merge_table_name,\n"
    5362             :                                                         " p_sch partition_schema_name,\n"
    5363             :                                                         " p_tbl partition_table_name\n"
    5364             :                                                         " FROM sys.describe_partition_tables;\n"
    5365             :                                                         "CREATE VIEW sys.dump_tables AS\n"
    5366             :                                                         " SELECT\n"
    5367             :                                                         " t.o o,\n"
    5368             :                                                         " CASE\n"
    5369             :                                                         " WHEN t.typ <> 'VIEW' THEN\n"
    5370             :                                                         " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    5371             :                                                         " ELSE\n"
    5372             :                                                         " t.opt\n"
    5373             :                                                         " END stmt,\n"
    5374             :                                                         " t.sch schema_name,\n"
    5375             :                                                         " t.tab table_name\n"
    5376             :                                                         " FROM sys.describe_tables t;\n"
    5377             :                                                         "CREATE VIEW sys.dump_comments AS\n"
    5378             :                                                         " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    5379             :                                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    5380             :                                                         "BEGIN\n"
    5381             :                                                         " RETURN\n"
    5382             :                                                         " CASE\n"
    5383             :                                                         " WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    5384             :                                                         " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    5385             :                                                         " ELSE\n"
    5386             :                                                         " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    5387             :                                                         " END;\n"
    5388             :                                                         "END;\n"
    5389             :                                                         "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
    5390             :                                                         "BEGIN\n"
    5391             :                                                         " DECLARE tid INT;\n"
    5392             :                                                         " SET tid = (SELECT MIN(t.id) FROM sys.tables t, sys.schemas s WHERE t.name = tbl AND t.schema_id = s.id AND s.name = sch);\n"
    5393             :                                                         " IF tid IS NOT NULL THEN\n"
    5394             :                                                         " DECLARE k INT;\n"
    5395             :                                                         " DECLARE m INT;\n"
    5396             :                                                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    5397             :                                                         " SET m = (SELECT MAX(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    5398             :                                                         " IF k IS NOT NULL AND m IS NOT NULL THEN\n"
    5399             :                                                         " DECLARE cname STRING;\n"
    5400             :                                                         " DECLARE ctype STRING;\n"
    5401             :                                                         " DECLARE _cnt INT;\n"
    5402             :                                                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    5403             :                                                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    5404             :                                                         " SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
    5405             :                                                         " IF _cnt > 0 THEN\n"
    5406             :                                                         " DECLARE COPY_INTO_STMT STRING;\n"
    5407             :                                                         " DECLARE SELECT_DATA_STMT STRING;\n"
    5408             :                                                         " SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    5409             :                                                         " SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    5410             :                                                         " WHILE (k < m) DO\n"
    5411             :                                                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid AND c.id > k);\n"
    5412             :                                                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    5413             :                                                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    5414             :                                                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    5415             :                                                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype));\n"
    5416             :                                                         " END WHILE;\n"
    5417             :                                                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || R') FROM STDIN USING DELIMITERS ''|'',E''\\n'',''\"'';');\n"
    5418             :                                                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl));\n"
    5419             :                                                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    5420             :                                                         " CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    5421             :                                                         " END IF;\n"
    5422             :                                                         " END IF;\n"
    5423             :                                                         " END IF;\n"
    5424             :                                                         " END;\n"
    5425             :                                                         "CREATE PROCEDURE sys.dump_table_data()\n"
    5426             :                                                         "BEGIN\n"
    5427             :                                                         " DECLARE i INT;\n"
    5428             :                                                         " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    5429             :                                                         " IF i IS NOT NULL THEN\n"
    5430             :                                                         " DECLARE M INT;\n"
    5431             :                                                         " SET M = (SELECT MAX(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
    5432             :                                                         " DECLARE sch STRING;\n"
    5433             :                                                         " DECLARE tbl STRING;\n"
    5434             :                                                         " WHILE i IS NOT NULL AND i <= M DO\n"
    5435             :                                                         " SET sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    5436             :                                                         " SET tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    5437             :                                                         " CALL sys.dump_table_data(sch, tbl);\n"
    5438             :                                                         " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system AND t.id > i);\n"
    5439             :                                                         " END WHILE;\n"
    5440             :                                                         " END IF;\n"
    5441             :                                                         "END;\n"
    5442             :                                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    5443             :                                                         "BEGIN\n"
    5444             :                                                         " SET SCHEMA sys;\n"
    5445             :                                                         " TRUNCATE sys.dump_statements;\n"
    5446             :                                                         " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    5447             :                                                         " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
    5448             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    5449             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    5450             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    5451             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    5452             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    5453             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    5454             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    5455             :                                                         " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    5456             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    5457             :                                                         " FROM (\n"
    5458             :                                                         " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    5459             :                                                         " UNION ALL\n"
    5460             :                                                         " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    5461             :                                                         " ) AS stmts(o, s);\n"
    5462             :                                                         " -- dump table data before adding constraints and fixing sequences\n"
    5463             :                                                         " IF NOT DESCRIBE THEN\n"
    5464             :                                                         " CALL sys.dump_table_data();\n"
    5465             :                                                         " END IF;\n"
    5466             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    5467             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    5468             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    5469             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    5470             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    5471             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    5472             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    5473             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    5474             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    5475             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    5476             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    5477             :                                                         " --TODO Improve performance of dump_table_data.\n"
    5478             :                                                         " --TODO loaders ,procedures, window and filter sys.functions.\n"
    5479             :                                                         " --TODO look into order dependent group_concat\n"
    5480             :                                                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    5481             :                                                         " RETURN sys.dump_statements;\n"
    5482             :                                                         "END;\n"
    5483             :                                                         "update sys.functions set system = true where system <> true and name in ('sq', 'get_remote_table_expressions', 'prepare_esc') and schema_id = 2000 and type = %d;\n"
    5484             :                                                         "update sys._tables set system = true where system <> true and name in ('describe_tables', 'dump_create_users', 'dump_partition_tables', 'dump_comments', 'dump_tables') and schema_id = 2000;\n"
    5485             :                                                         "update sys.functions set system = true where system <> true and name = 'dump_table_data' and schema_id = 2000 and type = %d;\n"
    5486             :                                                         "update sys.functions set system = true where system <> true and name = 'dump_database' and schema_id = 2000 and type = %d;\n"
    5487             :                                                         "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n",
    5488             :                                                         F_FUNC, F_PROC, F_UNION);
    5489           0 :                         assert(pos < bufsize);
    5490           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5491           0 :                         fflush(stdout);
    5492           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5493             :                 }
    5494         106 :                 res_table_destroy(output);
    5495         106 :                 output = NULL;
    5496             :         }
    5497             : 
    5498             :         /* Add new column 'function_id' to views
    5499             :          * sys.dependency_tables_on_functions and dependency_views_on_functions */
    5500             :         {
    5501         106 :                 sql_table *t = find_sql_table(sql->session->tr, s, "dependency_tables_on_functions");
    5502         106 :                 if (t != NULL && find_sql_column(t, "function_id") == NULL) {
    5503           0 :                         t->system = 0;               /* sys.dependency_tables_on_functions */
    5504           0 :                         if ((t = mvc_bind_table(sql, s, "dependency_views_on_functions")) != NULL)
    5505           0 :                                 t->system = 0;
    5506           0 :                         pos = 0;
    5507           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    5508             :                                                         "drop view if exists sys.dependency_tables_on_functions cascade;\n"
    5509             :                                                         "drop view if exists sys.dependency_views_on_functions cascade;\n"
    5510             :                                                         "CREATE VIEW sys.dependency_tables_on_functions AS\n"
    5511             :                                                         "SELECT t.schema_id AS table_schema_id, t.id AS table_id, t.name AS table_name,"
    5512             :                                                         " f.id AS function_id, f.name AS function_name, f.type AS function_type, dep.depend_type AS depend_type\n"
    5513             :                                                         "  FROM sys.functions AS f, sys.tables AS t, sys.dependencies AS dep\n"
    5514             :                                                         " WHERE t.id = dep.id AND f.id = dep.depend_id\n"
    5515             :                                                         "   AND dep.depend_type = 7 AND f.type <> 2 AND t.type NOT IN (1, 11)\n"
    5516             :                                                         " ORDER BY t.name, t.schema_id, f.name, f.id;\n"
    5517             :                                                         "GRANT SELECT ON sys.dependency_tables_on_functions TO PUBLIC;\n"
    5518             :                                                         "CREATE VIEW sys.dependency_views_on_functions AS\n"
    5519             :                                                         "SELECT v.schema_id AS view_schema_id, v.id AS view_id, v.name AS view_name,"
    5520             :                                                         " f.id AS function_id, f.name AS function_name, f.type AS function_type, dep.depend_type AS depend_type\n"
    5521             :                                                         "  FROM sys.functions AS f, sys.tables AS v, sys.dependencies AS dep\n"
    5522             :                                                         " WHERE v.id = dep.id AND f.id = dep.depend_id\n"
    5523             :                                                         "   AND dep.depend_type = 7 AND f.type <> 2 AND v.type IN (1, 11)\n"
    5524             :                                                         " ORDER BY v.name, v.schema_id, f.name, f.id;\n"
    5525             :                                                         "GRANT SELECT ON sys.dependency_views_on_functions TO PUBLIC;\n"
    5526             :                                                         "update sys._tables set system = true where system <> true and name in "
    5527             :                                                         "('dependency_tables_on_functions','dependency_views_on_functions') and schema_id = 2000;\n");
    5528           0 :                         assert(pos < bufsize);
    5529           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5530           0 :                         fflush(stdout);
    5531           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5532             :                 }
    5533             :         }
    5534             : 
    5535         106 :         if (!sql_bind_func(sql, "sys", "database", NULL, NULL, F_FUNC, true, true)) {
    5536           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5537           0 :                 sql->errstr[0] = '\0';
    5538           0 :                 pos = snprintf(buf, bufsize,
    5539             :                                            "create function sys.database ()\n"
    5540             :                                            "returns string\n"
    5541             :                                            "external name inspect.\"getDatabaseName\";\n"
    5542             :                                            "grant execute on function sys.database() to public;\n"
    5543             :                                            "update sys.functions set system = true where system <> true and name = 'database' and schema_id = 2000 and type = %d;\n",
    5544             :                                            (int) F_FUNC);
    5545           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5546           0 :                 fflush(stdout);
    5547           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5548             :         }
    5549             : 
    5550             :         /* Add new sysadmin procedure calls: stop, pause and resume with two
    5551             :            arguments, first arg is query OID and second the user username that
    5552             :            the query in bound to. */
    5553         106 :         sql_find_subtype(&t1, "bigint", 64, 0);
    5554         106 :         sql_find_subtype(&t2, "varchar", 0, 0);
    5555         106 :         if (!sql_bind_func(sql, "sys", "pause", &t1, &t2, F_PROC, true, true)) {
    5556           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5557           0 :                 sql->errstr[0] = '\0';
    5558           0 :                 const char *query =
    5559             :                         "create function sys.queue(username string) returns table(\"tag\" bigint, \"sessionid\" int, \"username\" string, \"started\" timestamp, \"status\" string, \"query\" string, \"finished\" timestamp, \"maxworkers\" int, \"footprint\" int) external name sysmon.queue;\n"
    5560             :                         "create procedure sys.pause(tag bigint, username string) external name sysmon.pause;\n"
    5561             :                         "create procedure sys.resume(tag bigint, username string) external name sysmon.resume;\n"
    5562             :                         "create procedure sys.stop(tag bigint, username string) external name sysmon.stop;\n"
    5563             :                         "update sys.functions set system = true where system <> true and mod = 'sysmon' and name in ('stop', 'pause', 'resume', 'queue');\n";
    5564           0 :                 printf("Running database upgrade commands:\n%s\n", query);
    5565           0 :                 fflush(stdout);
    5566           0 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5567             :         }
    5568             : 
    5569             :         /* sys.settimeout and sys.setsession where removed */
    5570         106 :         if (sql_bind_func(sql, "sys", "settimeout", &t1, NULL, F_PROC, true, true)) {
    5571           0 :                 const char *query =
    5572             :                         "drop procedure sys.settimeout(bigint) cascade;\n"
    5573             :                         "drop procedure sys.settimeout(bigint, bigint) cascade;\n"
    5574             :                         "drop procedure sys.setsession(bigint) cascade;\n";
    5575           0 :                 printf("Running database upgrade commands:\n%s\n", query);
    5576           0 :                 fflush(stdout);
    5577           0 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5578             :         }
    5579         106 :         sql->session->status = 0; /* if the function was not found clean the error */
    5580         106 :         sql->errstr[0] = '\0';
    5581             : 
    5582         106 :         if (!sql_bind_func(sql, "sys", "jarowinkler", &t2, &t2, F_FUNC, true, true)) {
    5583           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5584           0 :                 sql->errstr[0] = '\0';
    5585           0 :                 pos = snprintf(buf, bufsize,
    5586             :                                            "create function sys.levenshtein(x string, y string)\n"
    5587             :                                            "returns int external name txtsim.levenshtein;\n"
    5588             :                                            "grant execute on function levenshtein(string, string) to public;\n"
    5589             :                                            "create function sys.levenshtein(x string, y string, insdel int, rep int)\n"
    5590             :                                            "returns int external name txtsim.levenshtein;\n"
    5591             :                                            "grant execute on function levenshtein(string, string, int, int) to public;\n"
    5592             :                                            "create function sys.levenshtein(x string, y string, insdel int, rep int, trans int)\n"
    5593             :                                            "returns int external name txtsim.levenshtein;\n"
    5594             :                                            "grant execute on function levenshtein(string, string, int, int, int) to public;\n"
    5595             :                                            "create filter function sys.maxlevenshtein(x string, y string, k int)\n"
    5596             :                                            "external name txtsim.maxlevenshtein;\n"
    5597             :                                            "grant execute on filter function maxlevenshtein(string, string, int) to public;\n"
    5598             :                                            "create filter function sys.maxlevenshtein(x string, y string, k int, insdel int, rep int)\n"
    5599             :                                            "external name txtsim.maxlevenshtein;\n"
    5600             :                                            "grant execute on filter function maxlevenshtein(string, string, int, int, int) to public;\n"
    5601             :                                            "create function sys.jarowinkler(x string, y string)\n"
    5602             :                                            "returns double external name txtsim.jarowinkler;\n"
    5603             :                                            "grant execute on function jarowinkler(string, string) to public;\n"
    5604             :                                            "create filter function minjarowinkler(x string, y string, threshold double)\n"
    5605             :                                            "external name txtsim.minjarowinkler;\n"
    5606             :                                            "grant execute on filter function minjarowinkler(string, string, double) to public;\n"
    5607             :                                            "create function sys.dameraulevenshtein(x string, y string)\n"
    5608             :                                            "returns int external name txtsim.dameraulevenshtein;\n"
    5609             :                                            "grant execute on function dameraulevenshtein(string, string) to public;\n"
    5610             :                                            "create function sys.dameraulevenshtein(x string, y string, insdel int, rep int, trans int)\n"
    5611             :                                            "returns int external name txtsim.dameraulevenshtein;\n"
    5612             :                                            "grant execute on function dameraulevenshtein(string, string, int, int, int) to public;\n"
    5613             : 
    5614             :                                            "create function sys.editdistance(x string, y string)\n"
    5615             :                                            "returns int external name txtsim.editdistance;\n"
    5616             :                                            "grant execute on function editdistance(string, string) to public;\n"
    5617             :                                            "create function sys.editdistance2(x string, y string)\n"
    5618             :                                            "returns int external name txtsim.editdistance2;\n"
    5619             :                                            "grant execute on function editdistance2(string, string) to public;\n"
    5620             :                                            "create function sys.soundex(x string)\n"
    5621             :                                            "returns string external name txtsim.soundex;\n"
    5622             :                                            "grant execute on function soundex(string) to public;\n"
    5623             :                                            "create function sys.difference(x string, y string)\n"
    5624             :                                            "returns int external name txtsim.stringdiff;\n"
    5625             :                                            "grant execute on function difference(string, string) to public;\n"
    5626             :                                            "create function sys.qgramnormalize(x string)\n"
    5627             :                                            "returns string external name txtsim.qgramnormalize;\n"
    5628             :                                            "grant execute on function qgramnormalize(string) to public;\n"
    5629             : 
    5630             :                                            "create function asciify(x string)\n"
    5631             :                                            "returns string external name str.asciify;\n"
    5632             :                                            "grant execute on function asciify(string) to public;\n"
    5633             :                                            "create function sys.startswith(x string, y string)\n"
    5634             :                                            "returns boolean external name str.startswith;\n"
    5635             :                                            "grant execute on function startswith(string, string) to public;\n"
    5636             :                                            "create function sys.startswith(x string, y string, icase boolean)\n"
    5637             :                                            "returns boolean external name str.startswith;\n"
    5638             :                                            "grant execute on function startswith(string, string, boolean) to public;\n"
    5639             :                                            "create filter function sys.startswith(x string, y string)\n"
    5640             :                                            "external name str.startswith;\n"
    5641             :                                            "grant execute on filter function startswith(string, string) to public;\n"
    5642             :                                            "create filter function sys.startswith(x string, y string, icase boolean)\n"
    5643             :                                            "external name str.startswith;\n"
    5644             :                                            "grant execute on filter function startswith(string, string, boolean) to public;\n"
    5645             :                                            "create function sys.endswith(x string, y string)\n"
    5646             :                                            "returns boolean external name str.endswith;\n"
    5647             :                                            "grant execute on function endswith(string, string) to public;\n"
    5648             :                                            "create function sys.endswith(x string, y string, icase boolean)\n"
    5649             :                                            "returns boolean external name str.endswith;\n"
    5650             :                                            "grant execute on function endswith(string, string, boolean) to public;\n"
    5651             :                                            "create filter function sys.endswith(x string, y string)\n"
    5652             :                                            "external name str.endswith;\n"
    5653             :                                            "grant execute on filter function endswith(string, string) to public;\n"
    5654             :                                            "create filter function sys.endswith(x string, y string, icase boolean)\n"
    5655             :                                            "external name str.endswith;\n"
    5656             :                                            "grant execute on filter function endswith(string, string, boolean) to public;\n"
    5657             :                                            "create function sys.contains(x string, y string)\n"
    5658             :                                            "returns boolean external name str.contains;\n"
    5659             :                                            "grant execute on function contains(string, string) to public;\n"
    5660             :                                            "create function sys.contains(x string, y string, icase boolean)\n"
    5661             :                                            "returns boolean external name str.contains;\n"
    5662             :                                            "grant execute on function contains(string, string, boolean) to public;\n"
    5663             :                                            "create filter function sys.contains(x string, y string)\n"
    5664             :                                            "external name str.contains;\n"
    5665             :                                            "grant execute on filter function contains(string, string) to public;\n"
    5666             :                                            "create filter function sys.contains(x string, y string, icase boolean)\n"
    5667             :                                            "external name str.contains;\n"
    5668             :                                            "grant execute on filter function contains(string, string, boolean) to public;\n"
    5669             : 
    5670             :                                            "update sys.functions set system = true where system <> true and name in ('levenshtein', 'dameraulevenshtein', 'jarowinkler', 'editdistance', 'editdistance2', 'soundex', 'difference', 'qgramnormalize') and schema_id = 2000 and type = %d;\n"
    5671             :                                            "update sys.functions set system = true where system <> true and name in ('maxlevenshtein', 'minjarowinkler') and schema_id = 2000 and type = %d;\n"
    5672             :                                            "update sys.functions set system = true where system <> true and name in ('asciify', 'startswith', 'endswith', 'contains') and schema_id = 2000 and type = %d;\n"
    5673             :                                            "update sys.functions set system = true where system <> true and name in ('startswith', 'endswith', 'contains') and schema_id = 2000 and type = %d;\n"
    5674             : 
    5675             :                                            "delete from sys.triggers where name = 'system_update_tables' and table_id = 2067;\n",
    5676             :                                            F_FUNC, F_FILT, F_FUNC, F_FILT);
    5677           0 :                 assert(pos < bufsize);
    5678           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5679           0 :                 fflush(stdout);
    5680           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5681             :         }
    5682             : 
    5683             :         /* remote credentials where moved */
    5684         106 :         sql_trans *tr = sql->session->tr;
    5685         106 :         sqlstore *store = tr->store;
    5686         106 :         sql_table *remote_user_info = find_sql_table(tr, s, "remote_user_info");
    5687         106 :         sql_column *remote_user_info_id = find_sql_column(remote_user_info, "table_id");
    5688         106 :         BAT *rt_key = NULL, *rt_username = NULL, *rt_pwhash = NULL, *rt_uri = NULL, *rt_deleted = NULL;
    5689         106 :         if (!err && store->storage_api.count_col(tr, remote_user_info_id, 0) == 0 && BBPindex("M5system_auth_rt_key")) {
    5690             : 
    5691           0 :                 rt_key = BATdescriptor(BBPindex("M5system_auth_rt_key"));
    5692           0 :                 rt_uri = BATdescriptor(BBPindex("M5system_auth_rt_uri"));
    5693           0 :                 rt_username = BATdescriptor(BBPindex("M5system_auth_rt_remoteuser"));
    5694           0 :                 rt_pwhash = BATdescriptor(BBPindex("M5system_auth_rt_hashedpwd"));
    5695           0 :                 rt_deleted = BATdescriptor(BBPindex("M5system_auth_rt_deleted"));
    5696           0 :                 if (rt_key == NULL || rt_username == NULL || rt_pwhash == NULL || rt_uri == NULL || rt_deleted == NULL) {
    5697             :                         /* cleanup remainders and continue or full stop ? */
    5698           0 :                         BBPreclaim(rt_key);
    5699           0 :                         BBPreclaim(rt_uri);
    5700           0 :                         BBPreclaim(rt_username);
    5701           0 :                         BBPreclaim(rt_pwhash);
    5702           0 :                         BBPreclaim(rt_deleted);
    5703           0 :                         throw(SQL, __func__, "cannot find M5system_auth bats");
    5704             :                 }
    5705             : 
    5706           0 :                 BATiter ik = bat_iterator(rt_key);
    5707           0 :                 BATiter iu = bat_iterator(rt_username);
    5708           0 :                 BATiter ip = bat_iterator(rt_pwhash);
    5709           0 :                 for (oid p = 0; p < ik.count; p++) {
    5710           0 :                         if (BUNfnd(rt_deleted, &p) == BUN_NONE) {
    5711           0 :                                 char *key = GDKstrdup(BUNtvar(ik, p));
    5712           0 :                                 char *username = BUNtvar(iu, p);
    5713           0 :                                 char *pwhash = BUNtvar(ip, p);
    5714             : 
    5715           0 :                                 if (!key) {
    5716           0 :                                         bat_iterator_end(&ik);
    5717           0 :                                         bat_iterator_end(&iu);
    5718           0 :                                         bat_iterator_end(&ip);
    5719           0 :                                         BBPunfix(rt_key->batCacheid);
    5720           0 :                                         BBPunfix(rt_username->batCacheid);
    5721           0 :                                         BBPunfix(rt_pwhash->batCacheid);
    5722           0 :                                         BBPunfix(rt_deleted->batCacheid);
    5723           0 :                                         throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5724             :                                 }
    5725           0 :                                 char *d = strchr(key, '.');
    5726             :                                 /* . not found simply skip */
    5727           0 :                                 if (d) {
    5728           0 :                                         *d++ = '\0';
    5729           0 :                                         sql_schema *s = find_sql_schema(tr, key);
    5730           0 :                                         if (s) {
    5731           0 :                                                 sql_table *t = find_sql_table(tr, s, d);
    5732           0 :                                                 if (t && store->table_api.table_insert(tr, remote_user_info, &t->base.id, &username, &pwhash) != LOG_OK) {
    5733           0 :                                                         bat_iterator_end(&ik);
    5734           0 :                                                         bat_iterator_end(&iu);
    5735           0 :                                                         bat_iterator_end(&ip);
    5736           0 :                                                         BBPunfix(rt_key->batCacheid);
    5737           0 :                                                         BBPunfix(rt_username->batCacheid);
    5738           0 :                                                         BBPunfix(rt_pwhash->batCacheid);
    5739           0 :                                                         BBPunfix(rt_deleted->batCacheid);
    5740           0 :                                                         GDKfree(key);
    5741           0 :                                                         throw(SQL, __func__, "Failed to insert remote credentials during upgrade");
    5742             :                                                 }
    5743             :                                         }
    5744             :                                 }
    5745           0 :                                 GDKfree(key);
    5746             :                         }
    5747             :                 }
    5748           0 :                 bat_iterator_end(&ik);
    5749           0 :                 bat_iterator_end(&iu);
    5750           0 :                 bat_iterator_end(&ip);
    5751             :         }
    5752         106 :         if (!err && rt_key) {
    5753           0 :                 bat rtauthbats[6];
    5754             : 
    5755           0 :                 rtauthbats[0] = 0;
    5756           0 :                 rtauthbats[1] = rt_key->batCacheid;
    5757           0 :                 rtauthbats[2] = rt_uri->batCacheid;
    5758           0 :                 rtauthbats[3] = rt_username->batCacheid;
    5759           0 :                 rtauthbats[4] = rt_pwhash->batCacheid;
    5760           0 :                 rtauthbats[5] = rt_deleted->batCacheid;
    5761             : 
    5762           0 :                 if (BATmode(rt_key, true) != GDK_SUCCEED ||
    5763           0 :                         BBPrename(rt_key, NULL) != 0 ||
    5764           0 :                         BATmode(rt_username, true) != GDK_SUCCEED ||
    5765           0 :                         BBPrename(rt_username, NULL) != 0 ||
    5766           0 :                         BATmode(rt_pwhash, true) != GDK_SUCCEED ||
    5767           0 :                         BBPrename(rt_pwhash, NULL) != 0 ||
    5768           0 :                         BATmode(rt_uri, true) != GDK_SUCCEED ||
    5769           0 :                         BBPrename(rt_uri, NULL) != 0 ||
    5770           0 :                         BATmode(rt_deleted, true) != GDK_SUCCEED ||
    5771           0 :                         BBPrename(rt_deleted, NULL) != 0 ||
    5772           0 :                         TMsubcommit_list(rtauthbats, NULL, 6, -1) != GDK_SUCCEED) {
    5773           0 :                         fprintf(stderr, "Committing removal of old remote user/password BATs failed\n");
    5774             :                 }
    5775           0 :                 BBPunfix(rt_key->batCacheid);
    5776           0 :                 BBPunfix(rt_username->batCacheid);
    5777           0 :                 BBPunfix(rt_pwhash->batCacheid);
    5778           0 :                 BBPunfix(rt_uri->batCacheid);
    5779           0 :                 BBPunfix(rt_deleted->batCacheid);
    5780             :         }
    5781             : 
    5782         106 :         GDKfree(buf);
    5783         106 :         return err;             /* usually MAL_SUCCEED */
    5784             : }
    5785             : 
    5786             : static str
    5787         106 : sql_update_jun2023_sp3(Client c, mvc *sql, sql_schema *s)
    5788             : {
    5789         106 :         (void)s;
    5790         106 :         char *err = NULL;
    5791         106 :         sql_subtype t1, t2;
    5792             : 
    5793         106 :         sql_find_subtype(&t1, "timestamp", 0, 0);
    5794         106 :         sql_find_subtype(&t2, "varchar", 0, 0);
    5795             : 
    5796         106 :         if (!sql_bind_func(sql, "sys", "timestamp_to_str", &t1, &t2, F_FUNC, true, true)) {
    5797           0 :                 sql->session->status = 0;
    5798           0 :                 sql->errstr[0] = '\0';
    5799             : 
    5800           0 :                 char *query = GDKmalloc(512);
    5801           0 :                 if (query == NULL)
    5802           0 :                         throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5803             : 
    5804           0 :                 snprintf(query, 512, "CREATE FUNCTION timestamp_to_str(d TIMESTAMP, format STRING) RETURNS STRING "
    5805             :                                  "EXTERNAL NAME mtime.\"timestamp_to_str\";\n"
    5806             :                                  "GRANT EXECUTE ON FUNCTION timestamp_to_str(TIMESTAMP, STRING) TO PUBLIC;\n"
    5807             :                                  "UPDATE sys.functions SET system = true WHERE system <> true AND name = 'timestamp_to_str' "
    5808             :                                  "AND schema_id = 2000 and type = %d;\n", F_FUNC);
    5809             : 
    5810           0 :                 printf("Running database upgrade commands:\n%s\n", query);
    5811           0 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5812           0 :                 GDKfree(query);
    5813             :         }
    5814             : 
    5815             :         return err;             /* usually MAL_SUCCEED */
    5816             : }
    5817             : 
    5818             : static str
    5819         106 : sql_update_dec2023_geom(Client c, mvc *sql, sql_schema *s)
    5820             : {
    5821         106 :         sql_subtype tp;
    5822         106 :         char *err = NULL;
    5823             : 
    5824             :         /* the shp module was changed: drop the old stuff if it exists, only
    5825             :          * add the new stuff if the appropriate module is available */
    5826         106 :         sql_find_subtype(&tp, "varchar", 0, 0);
    5827             :         /* Drop old SHP procedures */
    5828         106 :         if (sql_bind_func(sql, s->base.name, "shpattach", &tp, NULL, F_PROC, true, true)) {
    5829           8 :                 if ((err = sql_drop_shp(c)) != NULL)
    5830             :                         return err;
    5831             :         }
    5832         106 :         sql->session->status = 0; /* if the shpattach function was not found clean the error */
    5833         106 :         sql->errstr[0] = '\0';
    5834             : #ifdef HAVE_GEOM
    5835         106 :         if (backend_has_module(&(int){0}, "geom")) {
    5836             : #ifdef HAVE_SHP
    5837         106 :                 if (backend_has_module(&(int){0}, "shp")) {
    5838             :                         /* if shpload with two varchar args does not exist, add the
    5839             :                          * procedures */
    5840         106 :                         if (!sql_bind_func(sql, s->base.name, "shpload", &tp, &tp, F_PROC, true, true)) {
    5841           8 :                                 sql->session->status = 0;
    5842           8 :                                 sql->errstr[0] = '\0';
    5843           8 :                                 if ((err = sql_create_shp(c)) != NULL)
    5844           0 :                                         return err;
    5845             :                         }
    5846             :                 }
    5847             : #endif
    5848         106 :                 sql_find_subtype(&tp, "geometry", 0, 0);
    5849         106 :                 if (!sql_bind_func(sql, s->base.name, "st_intersects_noindex", &tp, &tp, F_FILT, true, true)) {
    5850           8 :                         sql->session->status = 0;
    5851           8 :                         sql->errstr[0] = '\0';
    5852           8 :                         sql_table *t;
    5853           8 :                         if ((t = mvc_bind_table(sql, s, "geometry_columns")) != NULL)
    5854           8 :                                 t->system = 0;
    5855           8 :                         const char *query =
    5856             :                                 "drop function if exists sys.st_intersects(geometry, geometry) cascade;\n"
    5857             :                                 "drop function if exists sys.st_dwithin(geometry, geometry, double) cascade;\n"
    5858             :                                 "drop view if exists sys.geometry_columns cascade;\n"
    5859             :                                 "drop function if exists sys.st_collect(geometry, geometry) cascade;\n"
    5860             :                                 "drop aggregate if exists sys.st_collect(geometry) cascade;\n"
    5861             :                                 "drop aggregate if exists sys.st_makeline(geometry) cascade;\n"
    5862             :                                 "create view sys.geometry_columns as\n"
    5863             :                                 " select cast(null as varchar(1)) as f_table_catalog,\n"
    5864             :                                 "  s.name as f_table_schema,\n"
    5865             :                                 "  t.name as f_table_name,\n"
    5866             :                                 "  c.name as f_geometry_column,\n"
    5867             :                                 "  cast(has_z(c.type_digits) + has_m(c.type_digits) +2 as integer) as coord_dimension,\n"
    5868             :                                 "  c.type_scale as srid,\n"
    5869             :                                 "  get_type(c.type_digits, 0) as geometry_type\n"
    5870             :                                 " from sys.columns c, sys.tables t, sys.schemas s\n"
    5871             :                                 " where c.table_id = t.id and t.schema_id = s.id\n"
    5872             :                                 "  and c.type in (select sqlname from sys.types where systemname in ('wkb', 'wkba'));\n"
    5873             :                                 "GRANT SELECT ON sys.geometry_columns TO PUBLIC;\n"
    5874             :                                 "CREATE FUNCTION ST_Collect(geom1 Geometry, geom2 Geometry) RETURNS Geometry EXTERNAL NAME geom.\"Collect\";\n"
    5875             :                                 "GRANT EXECUTE ON FUNCTION ST_Collect(Geometry, Geometry) TO PUBLIC;\n"
    5876             :                                 "CREATE AGGREGATE ST_Collect(geom Geometry) RETURNS Geometry external name aggr.\"Collect\";\n"
    5877             :                                 "GRANT EXECUTE ON AGGREGATE ST_Collect(Geometry) TO PUBLIC;\n"
    5878             :                                 "CREATE FUNCTION ST_DistanceGeographic(geom1 Geometry, geom2 Geometry) RETURNS double EXTERNAL NAME geom.\"DistanceGeographic\";\n"
    5879             :                                 "GRANT EXECUTE ON FUNCTION ST_DistanceGeographic(Geometry, Geometry) TO PUBLIC;\n"
    5880             :                                 "CREATE FILTER FUNCTION ST_DWithinGeographic(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME geom.\"DWithinGeographic\";\n"
    5881             :                                 "GRANT EXECUTE ON FILTER ST_DWithinGeographic(Geometry, Geometry, double) TO PUBLIC;\n"
    5882             :                                 "CREATE FILTER FUNCTION ST_DWithin(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME rtree.\"DWithin\";\n"
    5883             :                                 "GRANT EXECUTE ON FILTER ST_DWithin(Geometry, Geometry, double) TO PUBLIC;\n"
    5884             :                                 "CREATE FILTER FUNCTION ST_DWithin_NoIndex(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME geom.\"DWithin_noindex\";\n"
    5885             :                                 "GRANT EXECUTE ON FILTER ST_DWithin_NoIndex(Geometry, Geometry, double) TO PUBLIC;\n"
    5886             :                                 "CREATE FUNCTION ST_DWithin2(geom1 Geometry, geom2 Geometry, bbox1 mbr, bbox2 mbr, dst double) RETURNS boolean EXTERNAL NAME geom.\"DWithin2\";\n"
    5887             :                                 "GRANT EXECUTE ON FUNCTION ST_DWithin2(Geometry, Geometry, mbr, mbr, double) TO PUBLIC;\n"
    5888             :                                 "CREATE FILTER FUNCTION ST_IntersectsGeographic(geom1 Geometry, geom2 Geometry) EXTERNAL NAME geom.\"IntersectsGeographic\";\n"
    5889             :                                 "GRANT EXECUTE ON FILTER ST_IntersectsGeographic(Geometry, Geometry) TO PUBLIC;\n"
    5890             :                                 "CREATE FILTER FUNCTION ST_Intersects(geom1 Geometry, geom2 Geometry) EXTERNAL NAME rtree.\"Intersects\";\n"
    5891             :                                 "GRANT EXECUTE ON FILTER ST_Intersects(Geometry, Geometry) TO PUBLIC;\n"
    5892             :                                 "CREATE FILTER FUNCTION ST_Intersects_NoIndex(geom1 Geometry, geom2 Geometry) EXTERNAL NAME geom.\"Intersects_noindex\";\n"
    5893             :                                 "GRANT EXECUTE ON FILTER ST_Intersects_NoIndex(Geometry, Geometry) TO PUBLIC;\n"
    5894             :                                 "CREATE AGGREGATE ST_MakeLine(geom Geometry) RETURNS Geometry external name aggr.\"MakeLine\";\n"
    5895             :                                 "GRANT EXECUTE ON AGGREGATE ST_MakeLine(Geometry) TO PUBLIC;\n"
    5896             :                                 "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('st_collect', 'st_distancegeographic', 'st_dwithingeographic', 'st_dwithin', 'st_dwithin_noindex', 'st_dwithin2', 'st_intersectsgeographic', 'st_intersects', 'st_intersects_noindex', 'st_makeline');\n"
    5897             :                                 "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'geometry_columns';\n";
    5898           8 :                         printf("Running database upgrade commands:\n%s\n", query);
    5899           8 :                         fflush(stdout);
    5900           8 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5901             :                 }
    5902             :         }
    5903             : #endif
    5904         106 :         return err;
    5905             : }
    5906             : 
    5907             : static str
    5908         106 : sql_update_dec2023(Client c, mvc *sql, sql_schema *s)
    5909             : {
    5910         106 :         sql_subtype tp;
    5911         106 :         sql_schema *info;
    5912         106 :         char *err = NULL;
    5913         106 :         res_table *output = NULL;
    5914             : 
    5915         106 :         sql_find_subtype(&tp, "varchar", 0, 0);
    5916         106 :         if (sql_bind_func(sql, s->base.name, "similarity", &tp, &tp, F_FUNC, true, true)) {
    5917           8 :                 const char *query = "drop function sys.similarity(string, string) cascade;\n";
    5918           8 :                 printf("Running database upgrade commands:\n%s\n", query);
    5919           8 :                 fflush(stdout);
    5920           8 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5921             :         } else {
    5922          98 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5923          98 :                 sql->errstr[0] = '\0';
    5924             :         }
    5925             : 
    5926         106 :         if (mvc_bind_table(sql, s, "describe_accessible_tables") == NULL) {
    5927           8 :                 sql->session->status = 0; /* if the view was not found clean the error */
    5928           8 :                 sql->errstr[0] = '\0';
    5929           8 :                 const char *query =
    5930             :                 "CREATE VIEW sys.describe_accessible_tables AS\n"
    5931             :                 " SELECT\n"
    5932             :                 " schemas.name AS schema,\n"
    5933             :                 " tables.name  AS table,\n"
    5934             :                 " tt.table_type_name AS table_type,\n"
    5935             :                 " pc.privilege_code_name AS privs,\n"
    5936             :                 " p.privileges AS privs_code\n"
    5937             :                 " FROM privileges p\n"
    5938             :                 " JOIN sys.roles ON p.auth_id = roles.id\n"
    5939             :                 " JOIN sys.tables ON p.obj_id = tables.id\n"
    5940             :                 " JOIN sys.table_types tt ON tables.type = tt.table_type_id\n"
    5941             :                 " JOIN sys.schemas ON tables.schema_id = schemas.id\n"
    5942             :                 " JOIN sys.privilege_codes pc ON p.privileges = pc.privilege_code_id\n"
    5943             :                 " WHERE roles.name = current_role;\n"
    5944             :                 "GRANT SELECT ON sys.describe_accessible_tables TO PUBLIC;\n"
    5945             :                 "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'describe_accessible_tables';\n"
    5946             : 
    5947             :                         /* PYTHON_MAP and PYTHON3_MAP have been removed */
    5948             :                         "alter table sys.function_languages set read write;\n"
    5949             :                         "delete from sys.function_languages where language_keyword like 'PYTHON%_MAP';\n"
    5950             :                         /* for these two, also see load_func() */
    5951             :                         "update sys.functions set language = language - 1 where language in (7, 11);\n"
    5952             :                         "update sys.functions set mod = 'pyapi3' where mod in ('pyapi', 'pyapi3map');\n"
    5953             :                         "commit;\n";
    5954           8 :                 printf("Running database upgrade commands:\n%s\n", query);
    5955           8 :                 fflush(stdout);
    5956           8 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5957           8 :                 if (err == MAL_SUCCEED) {
    5958           8 :                         query = "alter table sys.function_languages set read only;\n";
    5959           8 :                         printf("Running database upgrade commands:\n%s\n", query);
    5960           8 :                         fflush(stdout);
    5961           8 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5962             :                 }
    5963             :         }
    5964             : 
    5965             :         /* 52_describe.sql changes to update sys.describe_comments view */
    5966         106 :         if ((err = SQLstatementIntern(c, "select id from sys.tables where name = 'describe_comments' and schema_id = 2000 and query like '% not t.system%';", "update", true, false, &output)) == NULL) {
    5967         106 :                 BAT *b;
    5968         106 :                 if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
    5969           8 :                         sql_table *t;
    5970             :                         /* set views internally to non-system to allow drop commands to succeed without error */
    5971           8 :                         if ((t = mvc_bind_table(sql, s, "describe_comments")) != NULL)
    5972           8 :                                 t->system = 0;
    5973           8 :                         if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
    5974           8 :                                 t->system = 0;
    5975             : 
    5976           8 :                         const char *cmds =
    5977             :                         "DROP FUNCTION IF EXISTS sys.dump_database(BOOLEAN) CASCADE;\n"
    5978             :                         "DROP VIEW IF EXISTS sys.dump_comments CASCADE;\n"
    5979             :                         "DROP VIEW IF EXISTS sys.describe_comments CASCADE;\n"
    5980             :                         "CREATE VIEW sys.describe_comments AS\n"
    5981             :                         "  SELECT o.id AS id, o.tpe AS tpe, o.nme AS fqn, cm.remark AS rem\n"
    5982             :                         "  FROM (\n"
    5983             :                         "          SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas WHERE NOT system\n"
    5984             :                         "          UNION ALL\n"
    5985             :                         "          SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
    5986             :                         "            FROM sys.schemas s JOIN sys._tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
    5987             :                         "           WHERE NOT t.system\n"
    5988             :                         "          UNION ALL\n"
    5989             :                         "          SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys._tables t, sys.schemas s WHERE NOT t.system AND c.table_id = t.id AND t.schema_id = s.id\n"
    5990             :                         "          UNION ALL\n"
    5991             :                         "          SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE NOT t.system AND idx.table_id = t.id AND t.schema_id = s.id\n"
    5992             :                         "          UNION ALL\n"
    5993             :                         "          SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    5994             :                         "          UNION ALL\n"
    5995             :                         "          SELECT f.id, ft.function_type_keyword, qf.nme FROM sys.functions f, sys.function_types ft, sys.schemas s, sys.fully_qualified_functions qf\n"
    5996             :                         "           WHERE NOT f.system AND f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
    5997             :                         "          ) AS o(id, tpe, nme)\n"
    5998             :                         "  JOIN sys.comments cm ON cm.id = o.id;\n"
    5999             :                         "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
    6000             :                         "CREATE VIEW sys.dump_comments AS\n"
    6001             :                         "  SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    6002             :                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    6003             :                         "BEGIN\n"
    6004             :                         "  SET SCHEMA sys;\n"
    6005             :                         "  TRUNCATE sys.dump_statements;\n"
    6006             :                         "  INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    6007             :                         "  INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
    6008             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    6009             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    6010             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    6011             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    6012             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    6013             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    6014             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    6015             :                         "  --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    6016             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    6017             :                         "                              FROM (\n"
    6018             :                         "                                SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    6019             :                         "                                 UNION ALL\n"
    6020             :                         "                                SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    6021             :                         "                              ) AS stmts(o, s);\n"
    6022             :                         "  IF NOT DESCRIBE THEN\n"
    6023             :                         "    CALL sys.dump_table_data();\n"
    6024             :                         "  END IF;\n"
    6025             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    6026             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    6027             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    6028             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    6029             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    6030             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    6031             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    6032             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    6033             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    6034             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    6035             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    6036             :                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    6037             :                         "  RETURN sys.dump_statements;\n"
    6038             :                         "END;\n"
    6039             :                         "update sys._tables set system = true where schema_id = 2000 and name in ('describe_comments','dump_comments');\n"
    6040             :                         "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'dump_database' and type = 5;\n";
    6041             : 
    6042           8 :                         printf("Running database upgrade commands:\n%s\n", cmds);
    6043           8 :                         fflush(stdout);
    6044           8 :                         err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
    6045             :                 }
    6046         106 :                 res_table_destroy(output);
    6047         106 :                 output = NULL;
    6048             :         }
    6049             : 
    6050             :         /* 52_describe.sql New function sys.sql_datatype(mtype varchar(999), digits integer, tscale integer, nameonly boolean, shortname boolean) */
    6051         106 :         allocator *old_sa = sql->sa;
    6052         106 :         if ((sql->sa = sa_create(sql->pa)) != NULL) {
    6053         106 :                 list *l;
    6054         106 :                 if ((l = sa_list(sql->sa)) != NULL) {
    6055         106 :                         sql_subtype t1, t2;
    6056         106 :                         sql_find_subtype(&t1, "int", 0, 0);
    6057         106 :                         sql_find_subtype(&t2, "boolean", 0, 0);
    6058         106 :                         list_append(l, &tp);
    6059         106 :                         list_append(l, &t1);
    6060         106 :                         list_append(l, &t1);
    6061         106 :                         list_append(l, &t2);
    6062         106 :                         list_append(l, &t2);
    6063         106 :                         if (!sql_bind_func_(sql, s->base.name, "sql_datatype", l, F_FUNC, true, true)) {
    6064           8 :                                 const char *cmds =
    6065             :                                 "CREATE FUNCTION sys.sql_datatype(mtype varchar(999), digits integer, tscale integer, nameonly boolean, shortname boolean)\n"
    6066             :                                 "  RETURNS varchar(1024)\n"
    6067             :                                 "BEGIN\n"
    6068             :                                 "  RETURN\n"
    6069             :                                 "    CASE mtype\n"
    6070             :                                 "    WHEN 'char' THEN sys.ifthenelse(nameonly OR digits <= 1, sys.ifthenelse(shortname, 'CHAR', 'CHARACTER'), sys.ifthenelse(shortname, 'CHAR(', 'CHARACTER(') || digits || ')')\n"
    6071             :                                 "    WHEN 'varchar' THEN sys.ifthenelse(nameonly OR digits = 0, sys.ifthenelse(shortname, 'VARCHAR', 'CHARACTER VARYING'), sys.ifthenelse(shortname, 'VARCHAR(', 'CHARACTER VARYING(') || digits || ')')\n"
    6072             :                                 "    WHEN 'clob' THEN sys.ifthenelse(nameonly OR digits = 0, sys.ifthenelse(shortname, 'CLOB', 'CHARACTER LARGE OBJECT'), sys.ifthenelse(shortname, 'CLOB(', 'CHARACTER LARGE OBJECT(') || digits || ')')\n"
    6073             :                                 "    WHEN 'blob' THEN sys.ifthenelse(nameonly OR digits = 0, sys.ifthenelse(shortname, 'BLOB', 'BINARY LARGE OBJECT'), sys.ifthenelse(shortname, 'BLOB(', 'BINARY LARGE OBJECT(') || digits || ')')\n"
    6074             :                                 "    WHEN 'int' THEN 'INTEGER'\n"
    6075             :                                 "    WHEN 'bigint' THEN 'BIGINT'\n"
    6076             :                                 "    WHEN 'smallint' THEN 'SMALLINT'\n"
    6077             :                                 "    WHEN 'tinyint' THEN 'TINYINT'\n"
    6078             :                                 "    WHEN 'hugeint' THEN 'HUGEINT'\n"
    6079             :                                 "    WHEN 'boolean' THEN 'BOOLEAN'\n"
    6080             :                                 "    WHEN 'date' THEN 'DATE'\n"
    6081             :                                 "    WHEN 'time' THEN sys.ifthenelse(nameonly OR digits = 1, 'TIME', 'TIME(' || (digits -1) || ')')\n"
    6082             :                                 "    WHEN 'timestamp' THEN sys.ifthenelse(nameonly OR digits = 7, 'TIMESTAMP', 'TIMESTAMP(' || (digits -1) || ')')\n"
    6083             :                                 "    WHEN 'timestamptz' THEN sys.ifthenelse(nameonly OR digits = 7, 'TIMESTAMP WITH TIME ZONE', 'TIMESTAMP(' || (digits -1) || ') WITH TIME ZONE')\n"
    6084             :                                 "    WHEN 'timetz' THEN sys.ifthenelse(nameonly OR digits = 1, 'TIME WITH TIME ZONE', 'TIME(' || (digits -1) || ') WITH TIME ZONE')\n"
    6085             :                                 "    WHEN 'decimal' THEN sys.ifthenelse(nameonly OR digits = 0, 'DECIMAL', 'DECIMAL(' || digits || sys.ifthenelse(tscale = 0, '', ',' || tscale) || ')')\n"
    6086             :                                 "    WHEN 'double' THEN sys.ifthenelse(nameonly OR (digits = 53 AND tscale = 0), sys.ifthenelse(shortname, 'DOUBLE', 'DOUBLE PRECISION'), 'FLOAT(' || digits || ')')\n"
    6087             :                                 "    WHEN 'real' THEN sys.ifthenelse(nameonly OR (digits = 24 AND tscale = 0), 'REAL', 'FLOAT(' || digits || ')')\n"
    6088             :                                 "    WHEN 'day_interval' THEN 'INTERVAL DAY'\n"
    6089             :                                 "    WHEN 'month_interval' THEN CASE digits WHEN 1 THEN 'INTERVAL YEAR' WHEN 2 THEN 'INTERVAL YEAR TO MONTH' WHEN 3 THEN 'INTERVAL MONTH' END\n"
    6090             :                                 "    WHEN 'sec_interval' THEN\n"
    6091             :                                 "  CASE digits\n"
    6092             :                                 "  WHEN 4 THEN 'INTERVAL DAY'\n"
    6093             :                                 "  WHEN 5 THEN 'INTERVAL DAY TO HOUR'\n"
    6094             :                                 "  WHEN 6 THEN 'INTERVAL DAY TO MINUTE'\n"
    6095             :                                 "  WHEN 7 THEN 'INTERVAL DAY TO SECOND'\n"
    6096             :                                 "  WHEN 8 THEN 'INTERVAL HOUR'\n"
    6097             :                                 "  WHEN 9 THEN 'INTERVAL HOUR TO MINUTE'\n"
    6098             :                                 "  WHEN 10 THEN 'INTERVAL HOUR TO SECOND'\n"
    6099             :                                 "  WHEN 11 THEN 'INTERVAL MINUTE'\n"
    6100             :                                 "  WHEN 12 THEN 'INTERVAL MINUTE TO SECOND'\n"
    6101             :                                 "  WHEN 13 THEN 'INTERVAL SECOND'\n"
    6102             :                                 "  END\n"
    6103             :                                 "    WHEN 'oid' THEN 'OID'\n"
    6104             :                                 "    WHEN 'json' THEN sys.ifthenelse(nameonly OR digits = 0, 'JSON', 'JSON(' || digits || ')')\n"
    6105             :                                 "    WHEN 'url' THEN sys.ifthenelse(nameonly OR digits = 0, 'URL', 'URL(' || digits || ')')\n"
    6106             :                                 "    WHEN 'xml' THEN sys.ifthenelse(nameonly OR digits = 0, 'XML', 'XML(' || digits || ')')\n"
    6107             :                                 "    WHEN 'geometry' THEN\n"
    6108             :                                 "  sys.ifthenelse(nameonly, 'GEOMETRY',\n"
    6109             :                                 "  CASE digits\n"
    6110             :                                 "  WHEN 4 THEN 'GEOMETRY(POINT' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6111             :                                 "  WHEN 8 THEN 'GEOMETRY(LINESTRING' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6112             :                                 "  WHEN 16 THEN 'GEOMETRY(POLYGON' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6113             :                                 "  WHEN 20 THEN 'GEOMETRY(MULTIPOINT' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6114             :                                 "  WHEN 24 THEN 'GEOMETRY(MULTILINESTRING' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6115             :                                 "  WHEN 28 THEN 'GEOMETRY(MULTIPOLYGON' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6116             :                                 "  WHEN 32 THEN 'GEOMETRY(GEOMETRYCOLLECTION' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6117             :                                 "  ELSE 'GEOMETRY'\n"
    6118             :                                 "        END)\n"
    6119             :                                 "    ELSE sys.ifthenelse(mtype = lower(mtype), upper(mtype), '\"' || mtype || '\"') || sys.ifthenelse(nameonly OR digits = 0, '', '(' || digits || sys.ifthenelse(tscale = 0, '', ',' || tscale) || ')')\n"
    6120             :                                 "    END;\n"
    6121             :                                 "END;\n"
    6122             :                                 "GRANT EXECUTE ON FUNCTION sys.sql_datatype(varchar(999), integer, integer, boolean, boolean) TO PUBLIC;\n"
    6123             :                                 "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'sql_datatype' and type = 1 and language = 2;\n";
    6124             : 
    6125           8 :                                 sql->session->status = 0;
    6126           8 :                                 sql->errstr[0] = '\0';
    6127           8 :                                 printf("Running database upgrade commands:\n%s\n", cmds);
    6128           8 :                                 fflush(stdout);
    6129           8 :                                 err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
    6130             :                         }
    6131             :                 }
    6132         106 :                 sa_destroy(sql->sa);
    6133             :         }
    6134         106 :         sql->sa = old_sa;
    6135             : 
    6136             : 
    6137             :         /* 91_information_schema.sql */
    6138         106 :         info = mvc_bind_schema(sql, "information_schema");
    6139         106 :         if (info == NULL) {
    6140           8 :                 sql->session->status = 0; /* if the schema was not found clean the error */
    6141           8 :                 sql->errstr[0] = '\0';
    6142           8 :                 const char *cmds =
    6143             :                 "CREATE SCHEMA INFORMATION_SCHEMA;\n"
    6144             :                 "COMMENT ON SCHEMA INFORMATION_SCHEMA IS 'ISO/IEC 9075-11 SQL/Schemata';\n"
    6145             :                 "update sys.schemas set system = true where name = 'information_schema';\n"
    6146             : 
    6147             :                 "CREATE VIEW INFORMATION_SCHEMA.CHARACTER_SETS AS SELECT\n"
    6148             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6149             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6150             :                 "  cast('UTF-8' AS varchar(16)) AS CHARACTER_SET_NAME,\n"
    6151             :                 "  cast('ISO/IEC 10646:2021' AS varchar(20)) AS CHARACTER_REPERTOIRE,\n"
    6152             :                 "  cast('UTF-8' AS varchar(16)) AS FORM_OF_USE,\n"
    6153             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_CATALOG,\n"
    6154             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_SCHEMA,\n"
    6155             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_NAME;\n"
    6156             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.CHARACTER_SETS TO PUBLIC WITH GRANT OPTION;\n"
    6157             : 
    6158             :                 "CREATE VIEW INFORMATION_SCHEMA.SCHEMATA AS SELECT\n"
    6159             :                 "  cast(NULL AS varchar(1)) AS CATALOG_NAME,\n"
    6160             :                 "  s.\"name\" AS SCHEMA_NAME,\n"
    6161             :                 "  a.\"name\" AS SCHEMA_OWNER,\n"
    6162             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_CHARACTER_SET_CATALOG,\n"
    6163             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_CHARACTER_SET_SCHEMA,\n"
    6164             :                 "  cast('UTF-8' AS varchar(16)) AS DEFAULT_CHARACTER_SET_NAME,\n"
    6165             :                 "  cast(NULL AS varchar(1)) AS SQL_PATH,\n"
    6166             :                 "  s.\"id\" AS schema_id,\n"
    6167             :                 "  s.\"system\" AS is_system,\n"
    6168             :                 "  cm.\"remark\" AS comments\n"
    6169             :                 " FROM sys.\"schemas\" s\n"
    6170             :                 " INNER JOIN sys.\"auths\" a ON s.\"owner\" = a.\"id\"\n"
    6171             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON s.\"id\" = cm.\"id\"\n"
    6172             :                 " ORDER BY s.\"name\";\n"
    6173             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.SCHEMATA TO PUBLIC WITH GRANT OPTION;\n"
    6174             : 
    6175             :                 "CREATE VIEW INFORMATION_SCHEMA.TABLES AS SELECT\n"
    6176             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6177             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6178             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6179             :                 "  tt.\"table_type_name\" AS TABLE_TYPE,\n"
    6180             :                 "  cast(NULL AS varchar(1)) AS SELF_REFERENCING_COLUMN_NAME,\n"
    6181             :                 "  cast(NULL AS varchar(1)) AS REFERENCE_GENERATION,\n"
    6182             :                 "  cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_CATALOG,\n"
    6183             :                 "  cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_SCHEMA,\n"
    6184             :                 "  cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_NAME,\n"
    6185             :                 "  cast(sys.ifthenelse((t.\"type\" IN (0, 3, 7, 20, 30) AND t.\"access\" IN (0, 2)), 'YES', 'NO') AS varchar(3)) AS IS_INSERTABLE_INTO,\n"
    6186             :                 "  cast('NO' AS varchar(3)) AS IS_TYPED,\n"
    6187             :                 "  cast((CASE t.\"commit_action\" WHEN 1 THEN 'DELETE' WHEN 2 THEN 'PRESERVE' WHEN 3 THEN 'DROP' ELSE NULL END) AS varchar(10)) AS COMMIT_ACTION,\n"
    6188             :                 "  t.\"schema_id\" AS schema_id,\n"
    6189             :                 "  t.\"id\" AS table_id,\n"
    6190             :                 "  t.\"type\" AS table_type_id,\n"
    6191             :                 "  st.\"count\" AS row_count,\n"
    6192             :                 "  t.\"system\" AS is_system,\n"
    6193             :                 "  sys.ifthenelse(t.\"type\" IN (1, 11), TRUE, FALSE) AS is_view,\n"
    6194             :                 "  t.\"query\" AS query_def,\n"
    6195             :                 "  cm.\"remark\" AS comments\n"
    6196             :                 " FROM sys.\"tables\" t\n"
    6197             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6198             :                 " INNER JOIN sys.\"table_types\" tt ON t.\"type\" = tt.\"table_type_id\"\n"
    6199             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON t.\"id\" = cm.\"id\"\n"
    6200             :                 " LEFT OUTER JOIN (SELECT DISTINCT \"schema\", \"table\", \"count\" FROM sys.\"statistics\"()) st ON (s.\"name\" = st.\"schema\" AND t.\"name\" = st.\"table\")\n"
    6201             :                 " ORDER BY s.\"name\", t.\"name\";\n"
    6202             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.TABLES TO PUBLIC WITH GRANT OPTION;\n"
    6203             : 
    6204             :                 "CREATE VIEW INFORMATION_SCHEMA.VIEWS AS SELECT\n"
    6205             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6206             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6207             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6208             :                 "  t.\"query\" AS VIEW_DEFINITION,\n"
    6209             :                 "  cast('NONE' AS varchar(10)) AS CHECK_OPTION,\n"
    6210             :                 "  cast('NO' AS varchar(3)) AS IS_UPDATABLE,\n"
    6211             :                 "  cast('NO' AS varchar(3)) AS INSERTABLE_INTO,\n"
    6212             :                 "  cast('NO' AS varchar(3)) AS IS_TRIGGER_UPDATABLE,\n"
    6213             :                 "  cast('NO' AS varchar(3)) AS IS_TRIGGER_DELETABLE,\n"
    6214             :                 "  cast('NO' AS varchar(3)) AS IS_TRIGGER_INSERTABLE_INTO,\n"
    6215             :                 "  t.\"schema_id\" AS schema_id,\n"
    6216             :                 "  t.\"id\" AS table_id,\n"
    6217             :                 "  cast(sys.ifthenelse(t.\"system\", t.\"type\" + 10 , t.\"type\") AS smallint) AS table_type_id,\n"
    6218             :                 "  t.\"system\" AS is_system,\n"
    6219             :                 "  cm.\"remark\" AS comments\n"
    6220             :                 " FROM sys.\"_tables\" t\n"
    6221             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6222             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON t.\"id\" = cm.\"id\"\n"
    6223             :                 " WHERE t.\"type\" = 1\n"
    6224             :                 " ORDER BY s.\"name\", t.\"name\";\n"
    6225             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.VIEWS TO PUBLIC WITH GRANT OPTION;\n"
    6226             : 
    6227             :                 "CREATE VIEW INFORMATION_SCHEMA.COLUMNS AS SELECT\n"
    6228             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6229             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6230             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6231             :                 "  c.\"name\" AS COLUMN_NAME,\n"
    6232             :                 "  cast(1 + c.\"number\" AS int) AS ORDINAL_POSITION,\n"
    6233             :                 "  c.\"default\" AS COLUMN_DEFAULT,\n"
    6234             :                 "  cast(sys.ifthenelse(c.\"null\", 'YES', 'NO') AS varchar(3)) AS IS_NULLABLE,\n"
    6235             :                 "  cast(sys.\"sql_datatype\"(c.\"type\", c.\"type_digits\", c.\"type_scale\", true, true) AS varchar(1024)) AS DATA_TYPE,\n"
    6236             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('varchar','clob','char','json','url','xml') AND c.\"type_digits\" > 0, c.\"type_digits\", NULL) AS int) AS CHARACTER_MAXIMUM_LENGTH,\n"
    6237             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('varchar','clob','char','json','url','xml') AND c.\"type_digits\" > 0, 4 * cast(c.\"type_digits\" as bigint), NULL) AS bigint) AS CHARACTER_OCTET_LENGTH,\n"
    6238             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), c.\"type_digits\", NULL) AS int) AS NUMERIC_PRECISION,\n"
    6239             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','oid'), 2, sys.ifthenelse(c.\"type\" IN ('decimal','numeric'), 10, NULL)) AS int) AS NUMERIC_PRECISION_RADIX,\n"
    6240             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), c.\"type_scale\", NULL) AS int) AS NUMERIC_SCALE,\n"
    6241             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), sys.ifthenelse(c.\"type_scale\" > 0, c.\"type_scale\" -1, 0), NULL) AS int) AS DATETIME_PRECISION,\n"
    6242             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('day_interval','month_interval','sec_interval'), sys.\"sql_datatype\"(c.\"type\", c.\"type_digits\", c.\"type_scale\", true, true), NULL) AS varchar(40)) AS INTERVAL_TYPE,\n"
    6243             :                 "  cast(CASE c.\"type\" WHEN 'day_interval' THEN 0 WHEN 'month_interval' THEN 0 WHEN 'sec_interval' THEN (sys.ifthenelse(c.\"type_digits\" IN (7, 10, 12, 13), sys.ifthenelse(c.\"type_scale\" > 0, c.\"type_scale\", 3), 0)) ELSE NULL END AS int) AS INTERVAL_PRECISION,\n"
    6244             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6245             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6246             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('varchar','clob','char','json','url','xml'), 'UTF-8', NULL) AS varchar(16)) AS CHARACTER_SET_NAME,\n"
    6247             :                 "  cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
    6248             :                 "  cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
    6249             :                 "  cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
    6250             :                 "  cast(NULL AS varchar(1)) AS DOMAIN_CATALOG,\n"
    6251             :                 "  cast(NULL AS varchar(1)) AS DOMAIN_SCHEMA,\n"
    6252             :                 "  cast(NULL AS varchar(1)) AS DOMAIN_NAME,\n"
    6253             :                 "  cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
    6254             :                 "  cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
    6255             :                 "  cast(NULL AS varchar(1)) AS UDT_NAME,\n"
    6256             :                 "  cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
    6257             :                 "  cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
    6258             :                 "  cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
    6259             :                 "  cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
    6260             :                 "  cast(NULL AS varchar(1)) AS DTD_IDENTIFIER,\n"
    6261             :                 "  cast('NO' AS varchar(3)) AS IS_SELF_REFERENCING,\n"
    6262             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL OR c.\"null\", 'NO', 'YES') AS varchar(3)) AS IS_IDENTITY,\n"
    6263             :                 "  seq.\"name\" AS IDENTITY_GENERATION,\n"
    6264             :                 "  seq.\"start\" AS IDENTITY_START,\n"
    6265             :                 "  seq.\"increment\" AS IDENTITY_INCREMENT,\n"
    6266             :                 "  seq.\"maxvalue\" AS IDENTITY_MAXIMUM,\n"
    6267             :                 "  seq.\"minvalue\" AS IDENTITY_MINIMUM,\n"
    6268             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL, NULL, sys.ifthenelse(seq.\"cycle\", 'YES', 'NO')) AS varchar(3)) AS IDENTITY_CYCLE,\n"
    6269             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL, 'NO', 'YES') AS varchar(3)) AS IS_GENERATED,\n"
    6270             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL, NULL, c.\"default\") AS varchar(1024)) AS GENERATION_EXPRESSION,\n"
    6271             :                 "  cast('NO' AS varchar(3)) AS IS_SYSTEM_TIME_PERIOD_START,\n"
    6272             :                 "  cast('NO' AS varchar(3)) AS IS_SYSTEM_TIME_PERIOD_END,\n"
    6273             :                 "  cast('NO' AS varchar(3)) AS SYSTEM_TIME_PERIOD_TIMESTAMP_GENERATION,\n"
    6274             :                 "  cast(sys.ifthenelse(t.\"type\" IN (0,3,7,20,30), 'YES', 'NO') AS varchar(3)) AS IS_UPDATABLE,\n"
    6275             :                 "  cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
    6276             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
    6277             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
    6278             :                 "  t.\"schema_id\" AS schema_id,\n"
    6279             :                 "  c.\"table_id\" AS table_id,\n"
    6280             :                 "  c.\"id\" AS column_id,\n"
    6281             :                 "  seq.\"id\" AS sequence_id,\n"
    6282             :                 "  t.\"system\" AS is_system,\n"
    6283             :                 "  cm.\"remark\" AS comments\n"
    6284             :                 " FROM sys.\"columns\" c\n"
    6285             :                 " INNER JOIN sys.\"tables\" t ON c.\"table_id\" = t.\"id\"\n"
    6286             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6287             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON c.\"id\" = cm.\"id\"\n"
    6288             :                 " LEFT OUTER JOIN sys.\"sequences\" seq ON ((seq.\"name\"||'\"') = substring(c.\"default\", 3 + sys.\"locate\"('\".\"seq_',c.\"default\",14)))\n"
    6289             :                 " ORDER BY s.\"name\", t.\"name\", c.\"number\";\n"
    6290             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.COLUMNS TO PUBLIC WITH GRANT OPTION;\n"
    6291             : 
    6292             :                 "CREATE VIEW INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS SELECT\n"
    6293             :                 "  cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
    6294             :                 "  cast(NULL AS varchar(1024)) AS CONSTRAINT_SCHEMA,\n"
    6295             :                 "  cast(NULL AS varchar(1024)) AS CONSTRAINT_NAME,\n"
    6296             :                 "  cast(NULL AS varchar(1024)) AS CHECK_CLAUSE\n"
    6297             :                 " WHERE 1=0;\n"
    6298             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.CHECK_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
    6299             : 
    6300             :                 "CREATE VIEW INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS SELECT\n"
    6301             :                 "  cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
    6302             :                 "  s.\"name\" AS CONSTRAINT_SCHEMA,\n"
    6303             :                 "  k.\"name\" AS CONSTRAINT_NAME,\n"
    6304             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6305             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6306             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6307             :                 "  cast(CASE k.\"type\" WHEN 0 THEN 'PRIMARY KEY' WHEN 1 THEN 'UNIQUE' WHEN 2 THEN 'FOREIGN KEY' ELSE NULL END AS varchar(16)) AS CONSTRAINT_TYPE,\n"
    6308             :                 "  cast('NO' AS varchar(3)) AS IS_DEFERRABLE,\n"
    6309             :                 "  cast('NO' AS varchar(3)) AS INITIALLY_DEFERRED,\n"
    6310             :                 "  cast('YES' AS varchar(3)) AS ENFORCED,\n"
    6311             :                 "  t.\"schema_id\" AS schema_id,\n"
    6312             :                 "  t.\"id\" AS table_id,\n"
    6313             :                 "  k.\"id\" AS key_id,\n"
    6314             :                 "  k.\"type\" AS key_type,\n"
    6315             :                 "  t.\"system\" AS is_system\n"
    6316             :                 " FROM (SELECT sk.\"id\", sk.\"table_id\", sk.\"name\", sk.\"type\" FROM sys.\"keys\" sk UNION ALL SELECT tk.\"id\", tk.\"table_id\", tk.\"name\", tk.\"type\" FROM tmp.\"keys\" tk) k\n"
    6317             :                 " INNER JOIN (SELECT st.\"id\", st.\"schema_id\", st.\"name\", st.\"system\" FROM sys.\"_tables\" st UNION ALL"
    6318             :                         " SELECT tt.\"id\", tt.\"schema_id\", tt.\"name\", tt.\"system\" FROM tmp.\"_tables\" tt) t ON k.\"table_id\" = t.\"id\"\n"
    6319             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6320             :                 " ORDER BY s.\"name\", t.\"name\", k.\"name\";\n"
    6321             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.TABLE_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
    6322             : 
    6323             :                 "CREATE VIEW INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS SELECT\n"
    6324             :                 "  cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
    6325             :                 "  s.\"name\" AS CONSTRAINT_SCHEMA,\n"
    6326             :                 "  fk.\"name\" AS CONSTRAINT_NAME,\n"
    6327             :                 "  cast(NULL AS varchar(1)) AS UNIQUE_CONSTRAINT_CATALOG,\n"
    6328             :                 "  uks.\"name\" AS UNIQUE_CONSTRAINT_SCHEMA,\n"
    6329             :                 "  uk.\"name\" AS UNIQUE_CONSTRAINT_NAME,\n"
    6330             :                 "  cast('FULL' AS varchar(7)) AS MATCH_OPTION,\n"
    6331             :                 "  fk.\"update_action\" AS UPDATE_RULE,\n"
    6332             :                 "  fk.\"delete_action\" AS DELETE_RULE,\n"
    6333             :                 "  t.\"schema_id\" AS fk_schema_id,\n"
    6334             :                 "  t.\"id\" AS fk_table_id,\n"
    6335             :                 "  t.\"name\" AS fk_table_name,\n"
    6336             :                 "  fk.\"id\" AS fk_key_id,\n"
    6337             :                 "  ukt.\"schema_id\" AS uc_schema_id,\n"
    6338             :                 "  uk.\"table_id\" AS uc_table_id,\n"
    6339             :                 "  ukt.\"name\" AS uc_table_name,\n"
    6340             :                 "  uk.\"id\" AS uc_key_id\n"
    6341             :                 " FROM sys.\"fkeys\" fk\n"
    6342             :                 " INNER JOIN sys.\"tables\" t ON t.\"id\" = fk.\"table_id\"\n"
    6343             :                 " INNER JOIN sys.\"schemas\" s ON s.\"id\" = t.\"schema_id\"\n"
    6344             :                 " LEFT OUTER JOIN sys.\"keys\" uk ON uk.\"id\" = fk.\"rkey\"\n"
    6345             :                 " LEFT OUTER JOIN sys.\"tables\" ukt ON ukt.\"id\" = uk.\"table_id\"\n"
    6346             :                 " LEFT OUTER JOIN sys.\"schemas\" uks ON uks.\"id\" = ukt.\"schema_id\"\n"
    6347             :                 " ORDER BY s.\"name\", t.\"name\", fk.\"name\";\n"
    6348             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
    6349             : 
    6350             :                 "CREATE VIEW INFORMATION_SCHEMA.ROUTINES AS SELECT\n"
    6351             :                 "  cast(NULL AS varchar(1)) AS SPECIFIC_CATALOG,\n"
    6352             :                 "  s.\"name\" AS SPECIFIC_SCHEMA,\n"
    6353             :                 "  cast(f.\"name\"||'('||f.\"id\"||')' AS varchar(270)) AS SPECIFIC_NAME,\n"
    6354             :                 "  cast(NULL AS varchar(1)) AS ROUTINE_CATALOG,\n"
    6355             :                 "  s.\"name\" AS ROUTINE_SCHEMA,\n"
    6356             :                 "  f.\"name\" AS ROUTINE_NAME,\n"
    6357             :                 "  ft.\"function_type_keyword\" AS ROUTINE_TYPE,\n"
    6358             :                 "  cast(NULL AS varchar(1)) AS MODULE_CATALOG,\n"
    6359             :                 "  cast(NULL AS varchar(1)) AS MODULE_SCHEMA,\n"
    6360             :                 "  cast(f.\"mod\" AS varchar(128)) AS MODULE_NAME,\n"
    6361             :                 "  cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
    6362             :                 "  cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
    6363             :                 "  cast(NULL AS varchar(1)) AS UDT_NAME,\n"
    6364             :                 "  cast(CASE f.\"type\" WHEN 1 THEN sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true) WHEN 2 THEN NULL WHEN 5 THEN 'TABLE' WHEN 7 THEN 'TABLE' ELSE NULL END AS varchar(1024)) AS DATA_TYPE,\n"
    6365             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, a.\"type_digits\", NULL) AS int) AS CHARACTER_MAXIMUM_LENGTH,\n"
    6366             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, 4 * cast(a.\"type_digits\" as bigint), NULL) AS bigint) AS CHARACTER_OCTET_LENGTH,\n"
    6367             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6368             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6369             :                 "  'UTF-8' AS CHARACTER_SET_NAME,\n"
    6370             :                 "  cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
    6371             :                 "  cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
    6372             :                 "  cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
    6373             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_digits\", NULL) AS int) AS NUMERIC_PRECISION,\n"
    6374             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','oid'), 2, sys.ifthenelse(a.\"type\" IN ('decimal','numeric'), 10, NULL)) AS int) AS NUMERIC_PRECISION_RADIX,\n"
    6375             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_scale\", NULL) AS int) AS NUMERIC_SCALE,\n"
    6376             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), a.\"type_scale\" -1, NULL) AS int) AS DATETIME_PRECISION,\n"
    6377             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('day_interval','month_interval','sec_interval'), sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true), NULL) AS varchar(40)) AS INTERVAL_TYPE,\n"
    6378             :                 "  cast(CASE a.\"type\" WHEN 'day_interval' THEN 0 WHEN 'month_interval' THEN 0 WHEN 'sec_interval' THEN (sys.ifthenelse(a.\"type_digits\" IN (7, 10, 12, 13), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\", 3), 0)) ELSE NULL END AS int) AS INTERVAL_PRECISION,\n"
    6379             :                 "  cast(NULL AS varchar(1)) AS TYPE_UDT_CATALOG,\n"
    6380             :                 "  cast(NULL AS varchar(1)) AS TYPE_UDT_SCHEMA,\n"
    6381             :                 "  cast(NULL AS varchar(1)) AS TYPE_UDT_NAME,\n"
    6382             :                 "  cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
    6383             :                 "  cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
    6384             :                 "  cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
    6385             :                 "  cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
    6386             :                 "  cast(NULL AS int) AS DTD_IDENTIFIER,\n"
    6387             :                 "  cast(sys.\"ifthenelse\"(sys.\"locate\"('begin',f.\"func\") > 0, sys.\"ifthenelse\"(sys.\"endswith\"(f.\"func\",';'), sys.\"substring\"(f.\"func\", sys.\"locate\"('begin',f.\"func\"), sys.\"length\"(sys.\"substring\"(f.\"func\", sys.\"locate\"('begin',f.\"func\")))-1), sys.\"substring\"(f.\"func\", sys.\"locate\"('begin',f.\"func\"))), NULL) AS varchar(8196)) AS ROUTINE_BODY,\n"
    6388             :                 "  f.\"func\" AS ROUTINE_DEFINITION,\n"
    6389             :                 "  cast(sys.\"ifthenelse\"(sys.\"locate\"('external name',f.\"func\") > 0, sys.\"ifthenelse\"(sys.\"endswith\"(f.\"func\",';'), sys.\"substring\"(f.\"func\", 14 + sys.\"locate\"('external name',f.\"func\"), sys.\"length\"(sys.\"substring\"(f.\"func\", 14 + sys.\"locate\"('external name',f.\"func\")))-1), sys.\"substring\"(f.\"func\", 14 + sys.\"locate\"('external name',f.\"func\"))), NULL) AS varchar(1024)) AS EXTERNAL_NAME,\n"
    6390             :                 "  fl.\"language_keyword\" AS EXTERNAL_LANGUAGE,\n"
    6391             :                 "  'GENERAL' AS PARAMETER_STYLE,\n"
    6392             :                 "  'YES' AS IS_DETERMINISTIC,\n"
    6393             :                 "  cast(sys.ifthenelse(f.\"side_effect\", 'MODIFIES', 'READ') AS varchar(10)) AS SQL_DATA_ACCESS,\n"
    6394             :                 "  cast(CASE f.\"type\" WHEN 2 THEN NULL ELSE 'NO' END AS varchar(3)) AS IS_NULL_CALL,\n"
    6395             :                 "  cast(NULL AS varchar(1)) AS SQL_PATH,\n"
    6396             :                 "  cast(NULL AS varchar(1)) AS SCHEMA_LEVEL_ROUTINE,\n"
    6397             :                 "  cast(NULL AS int) AS MAX_DYNAMIC_RESULT_SETS,\n"
    6398             :                 "  cast(NULL AS varchar(1)) AS IS_USER_DEFINED_CAST,\n"
    6399             :                 "  cast(NULL AS varchar(1)) AS IS_IMPLICITLY_INVOCABLE,\n"
    6400             :                 "  cast(NULL AS varchar(1)) AS SECURITY_TYPE,\n"
    6401             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_CATALOG,\n"
    6402             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_SCHEMA,\n"
    6403             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_NAME,\n"
    6404             :                 "  cast(NULL AS varchar(1)) AS AS_LOCATOR,\n"
    6405             :                 "  cast(NULL AS timestamp) AS CREATED,\n"
    6406             :                 "  cast(NULL AS timestamp) AS LAST_ALTERED,\n"
    6407             :                 "  cast(NULL AS varchar(1)) AS NEW_SAVEPOINT_LEVEL,\n"
    6408             :                 "  cast(NULL AS varchar(1)) AS IS_UDT_DEPENDENT,\n"
    6409             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_FROM_DATA_TYPE,\n"
    6410             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_AS_LOCATOR,\n"
    6411             :                 "  cast(NULL AS int) AS RESULT_CAST_CHAR_MAX_LENGTH,\n"
    6412             :                 "  cast(NULL AS int) AS RESULT_CAST_CHAR_OCTET_LENGTH,\n"
    6413             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_CHAR_SET_CATALOG,\n"
    6414             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_CHAR_SET_SCHEMA,\n"
    6415             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_CHARACTER_SET_NAME,\n"
    6416             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_CATALOG,\n"
    6417             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_SCHEMA,\n"
    6418             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_NAME,\n"
    6419             :                 "  cast(NULL AS int) AS RESULT_CAST_NUMERIC_PRECISION,\n"
    6420             :                 "  cast(NULL AS int) AS RESULT_CAST_NUMERIC_RADIX,\n"
    6421             :                 "  cast(NULL AS int) AS RESULT_CAST_NUMERIC_SCALE,\n"
    6422             :                 "  cast(NULL AS int) AS RESULT_CAST_DATETIME_PRECISION,\n"
    6423             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_INTERVAL_TYPE,\n"
    6424             :                 "  cast(NULL AS int) AS RESULT_CAST_INTERVAL_PRECISION,\n"
    6425             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_CATALOG,\n"
    6426             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_SCHEMA,\n"
    6427             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_NAME,\n"
    6428             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_CATALOG,\n"
    6429             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_SCHEMA,\n"
    6430             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_NAME,\n"
    6431             :                 "  cast(NULL AS int) AS RESULT_CAST_MAX_CARDINALITY,\n"
    6432             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_DTD_IDENTIFIER,\n"
    6433             :                 "  cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
    6434             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
    6435             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
    6436             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_FROM_DECLARED_DATA_TYPE,\n"
    6437             :                 "  cast(NULL AS int) AS RESULT_CAST_DECLARED_NUMERIC_PRECISION,\n"
    6438             :                 "  cast(NULL AS int) AS RESULT_CAST_DECLARED_NUMERIC_SCALE,\n"
    6439             :                 "  f.\"schema_id\" AS schema_id,\n"
    6440             :                 "  f.\"id\" AS function_id,\n"
    6441             :                 "  f.\"type\" AS function_type,\n"
    6442             :                 "  f.\"language\" AS function_language,\n"
    6443             :                 "  f.\"system\" AS is_system,\n"
    6444             :                 "  cm.\"remark\" AS comments\n"
    6445             :                 " FROM sys.\"functions\" f\n"
    6446             :                 " INNER JOIN sys.\"schemas\" s ON s.\"id\" = f.\"schema_id\"\n"
    6447             :                 " INNER JOIN sys.\"function_types\" ft ON ft.\"function_type_id\" = f.\"type\"\n"
    6448             :                 " INNER JOIN sys.\"function_languages\" fl ON fl.\"language_id\" = f.\"language\"\n"
    6449             :                 " LEFT OUTER JOIN sys.\"args\" a ON a.\"func_id\" = f.\"id\" and a.\"inout\" = 0 and a.\"number\" = 0\n"
    6450             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON cm.\"id\" = f.\"id\"\n"
    6451             :                 " WHERE f.\"type\" in (1, 2, 5, 7)\n"
    6452             :                 " ORDER BY s.\"name\", f.\"name\";\n"
    6453             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.ROUTINES TO PUBLIC WITH GRANT OPTION;\n"
    6454             : 
    6455             :                 "CREATE VIEW INFORMATION_SCHEMA.PARAMETERS AS SELECT\n"
    6456             :                 "  cast(NULL AS varchar(1)) AS SPECIFIC_CATALOG,\n"
    6457             :                 "  s.\"name\" AS SPECIFIC_SCHEMA,\n"
    6458             :                 "  cast(f.\"name\"||'('||f.\"id\"||')' AS varchar(270)) AS SPECIFIC_NAME,\n"
    6459             :                 "  cast(sys.ifthenelse((a.\"inout\" = 0 OR f.\"type\" = 2), 1 + a.\"number\", sys.ifthenelse(f.\"type\" = 1, a.\"number\", (1 + a.\"number\" - f.count_out_cols))) AS int) AS ORDINAL_POSITION,\n"
    6460             :                 "  cast(sys.ifthenelse(a.\"inout\" = 0, 'OUT', sys.ifthenelse(a.\"inout\" = 1, 'IN', 'INOUT')) as varchar(5)) AS PARAMETER_MODE,\n"
    6461             :                 "  cast(sys.ifthenelse(a.\"inout\" = 0, 'YES', 'NO') as varchar(3)) AS IS_RESULT,\n"
    6462             :                 "  cast(NULL AS varchar(1)) AS AS_LOCATOR,\n"
    6463             :                 "  a.\"name\" AS PARAMETER_NAME,\n"
    6464             :                 "  cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_CATALOG,\n"
    6465             :                 "  cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_SCHEMA,\n"
    6466             :                 "  cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_NAME,\n"
    6467             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_CATALOG,\n"
    6468             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_SCHEMA,\n"
    6469             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_NAME,\n"
    6470             :                 "  cast(sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true) AS varchar(1024)) AS DATA_TYPE,\n"
    6471             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, a.\"type_digits\", NULL) AS int) AS CHARACTER_MAXIMUM_LENGTH,\n"
    6472             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, 4 * cast(a.\"type_digits\" as bigint), NULL) AS bigint) AS CHARACTER_OCTET_LENGTH,\n"
    6473             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6474             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6475             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml'), 'UTF-8', NULL) AS varchar(16)) AS CHARACTER_SET_NAME,\n"
    6476             :                 "  cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
    6477             :                 "  cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
    6478             :                 "  cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
    6479             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_digits\", NULL) AS int) AS NUMERIC_PRECISION,\n"
    6480             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','oid'), 2, sys.ifthenelse(a.\"type\" IN ('decimal','numeric'), 10, NULL)) AS int) AS NUMERIC_PRECISION_RADIX,\n"
    6481             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_scale\", NULL) AS int) AS NUMERIC_SCALE,\n"
    6482             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\" -1, 0), NULL) AS int) AS DATETIME_PRECISION,\n"
    6483             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('day_interval','month_interval','sec_interval'), sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true), NULL) AS varchar(40)) AS INTERVAL_TYPE,\n"
    6484             :                 "  cast(CASE a.\"type\" WHEN 'day_interval' THEN 0 WHEN 'month_interval' THEN 0 WHEN 'sec_interval' THEN (sys.ifthenelse(a.\"type_digits\" IN (7, 10, 12, 13), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\", 3), 0)) ELSE NULL END AS int) AS INTERVAL_PRECISION,\n"
    6485             :                 "  cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
    6486             :                 "  cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
    6487             :                 "  cast(NULL AS varchar(1)) AS UDT_NAME,\n"
    6488             :                 "  cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
    6489             :                 "  cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
    6490             :                 "  cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
    6491             :                 "  cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
    6492             :                 "  cast(NULL AS varchar(1)) AS DTD_IDENTIFIER,\n"
    6493             :                 "  cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
    6494             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
    6495             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
    6496             :                 "  cast(NULL AS varchar(1)) AS PARAMETER_DEFAULT,\n"
    6497             :                 "  f.\"schema_id\" AS schema_id,\n"
    6498             :                 "  f.\"id\" AS function_id,\n"
    6499             :                 "  a.\"id\" AS arg_id,\n"
    6500             :                 "  f.\"name\" AS function_name,\n"
    6501             :                 "  f.\"type\" AS function_type,\n"
    6502             :                 "  f.\"system\" AS is_system\n"
    6503             :                 " FROM sys.\"args\" a\n"
    6504             :                 " INNER JOIN (SELECT fun.*, (select count(*) from sys.args a0 where a0.inout = 0 and a0.func_id = fun.id) as count_out_cols FROM sys.\"functions\" fun WHERE fun.\"type\" in (1, 2, 5, 7)) f ON f.\"id\" = a.\"func_id\"\n"
    6505             :                 " INNER JOIN sys.\"schemas\" s ON s.\"id\" = f.\"schema_id\"\n"
    6506             :                 " ORDER BY s.\"name\", f.\"name\", f.\"id\", a.\"inout\" DESC, a.\"number\";\n"
    6507             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.PARAMETERS TO PUBLIC WITH GRANT OPTION;\n"
    6508             : 
    6509             :                 "CREATE VIEW INFORMATION_SCHEMA.SEQUENCES AS SELECT\n"
    6510             :                 "  cast(NULL AS varchar(1)) AS SEQUENCE_CATALOG,\n"
    6511             :                 "  s.\"name\" AS SEQUENCE_SCHEMA,\n"
    6512             :                 "  sq.\"name\" AS SEQUENCE_NAME,\n"
    6513             :                 "  cast('BIGINT' AS varchar(16)) AS DATA_TYPE,\n"
    6514             :                 "  cast(64 AS SMALLINT) AS NUMERIC_PRECISION,\n"
    6515             :                 "  cast(2 AS SMALLINT) AS NUMERIC_PRECISION_RADIX,\n"
    6516             :                 "  cast(0 AS SMALLINT) AS NUMERIC_SCALE,\n"
    6517             :                 "  sq.\"start\" AS START_VALUE,\n"
    6518             :                 "  sq.\"minvalue\" AS MINIMUM_VALUE,\n"
    6519             :                 "  sq.\"maxvalue\" AS MAXIMUM_VALUE,\n"
    6520             :                 "  sq.\"increment\" AS INCREMENT,\n"
    6521             :                 "  cast(sys.ifthenelse(sq.\"cycle\", 'YES', 'NO') AS varchar(3)) AS CYCLE_OPTION,\n"
    6522             :                 "  cast(NULL AS varchar(16)) AS DECLARED_DATA_TYPE,\n"
    6523             :                 "  cast(NULL AS SMALLINT) AS DECLARED_NUMERIC_PRECISION,\n"
    6524             :                 "  cast(NULL AS SMALLINT) AS DECLARED_NUMERIC_SCALE,\n"
    6525             :                 "  sq.\"schema_id\" AS schema_id,\n"
    6526             :                 "  sq.\"id\" AS sequence_id,\n"
    6527             :                 "  get_value_for(s.\"name\", sq.\"name\") AS current_value,\n"
    6528             :                 "  sq.\"cacheinc\" AS cacheinc,\n"
    6529             :                 "  cm.\"remark\" AS comments\n"
    6530             :                 " FROM sys.\"sequences\" sq\n"
    6531             :                 " INNER JOIN sys.\"schemas\" s ON sq.\"schema_id\" = s.\"id\"\n"
    6532             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON sq.\"id\" = cm.\"id\"\n"
    6533             :                 " ORDER BY s.\"name\", sq.\"name\";\n"
    6534             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.SEQUENCES TO PUBLIC WITH GRANT OPTION;\n"
    6535             :                 "\n"
    6536             :                 "update sys._tables set system = true where system <> true\n"
    6537             :                 " and schema_id = (select s.id from sys.schemas s where s.name = 'information_schema')\n"
    6538             :                 " and name in ('character_sets','check_constraints','columns','parameters','routines','schemata','sequences','referential_constraints','table_constraints','tables','views');\n";
    6539           8 :                 printf("Running database upgrade commands:\n%s\n", cmds);
    6540           8 :                 fflush(stdout);
    6541           8 :                 err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
    6542             :         }
    6543             : 
    6544             :         /* 77_storage.sql */
    6545         106 :         sql_find_subtype(&tp, "varchar", 0, 0);
    6546             : 
    6547         106 :         if (!sql_bind_func(sql, s->base.name, "persist_unlogged", &tp, &tp, F_UNION, true, true)) {
    6548           8 :                 sql->session->status = 0;
    6549           8 :                 sql->errstr[0] = '\0';
    6550           8 :                 const char *query =
    6551             :                         "CREATE FUNCTION sys.persist_unlogged(sname STRING, tname STRING)\n"
    6552             :                         "RETURNS TABLE(\"table\" STRING, \"table_id\" INT, \"rowcount\" BIGINT)\n"
    6553             :                         "EXTERNAL NAME sql.persist_unlogged;\n"
    6554             :                         "GRANT EXECUTE ON FUNCTION sys.persist_unlogged(string, string) TO PUBLIC;\n"
    6555             :                         "UPDATE sys.functions SET system = true WHERE system <> true AND\n"
    6556             :                         "name = 'persist_unlogged' AND schema_id = 2000 AND type = 5 AND language = 1;\n";
    6557           8 :                 printf("Running database upgrade commands:\n%s\n", query);
    6558           8 :                 fflush(stdout);
    6559           8 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    6560             :         }
    6561             : 
    6562         106 :         return err;
    6563             : }
    6564             : 
    6565             : static str
    6566         106 : sql_update_dec2023_sp1(Client c, mvc *sql, sql_schema *s)
    6567             : {
    6568         106 :         char *err;
    6569         106 :         res_table *output;
    6570         106 :         BAT *b;
    6571             : 
    6572         106 :         (void) sql;
    6573         106 :         (void) s;
    6574             : 
    6575             :         /* json.isvalid(json) has been fixed to return NULL on NULL input */
    6576         106 :         err = SQLstatementIntern(c, "SELECT f.id FROM sys.functions f WHERE f.name = 'isvalid' AND f.schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = 'json') AND EXISTS (SELECT * FROM sys.args a WHERE a.func_id = f.id AND a.number = 1 AND a.type = 'json') AND f.func LIKE '%begin return true%';\n", "update", true, false, &output);
    6577         106 :         if (err)
    6578             :                 return err;
    6579         106 :         b = BATdescriptor(output->cols[0].b);
    6580         106 :         if (b) {
    6581         106 :                 if (BATcount(b) > 0) {
    6582           8 :                         const char *query = "drop function json.isvalid(json);\n"
    6583             :                                 "create function json.isvalid(js json)\n"
    6584             :                                 "returns bool begin return case when js is NULL then NULL else true end; end;\n"
    6585             :                                 "GRANT EXECUTE ON FUNCTION json.isvalid(json) TO PUBLIC;\n"
    6586             :                                 "update sys.functions set system = true where system <> true and name = 'isvalid' and schema_id = (select id from sys.schemas where name = 'json');\n";
    6587           8 :                         assert(BATcount(b) == 1);
    6588           8 :                         printf("Running database upgrade commands:\n%s\n", query);
    6589           8 :                         fflush(stdout);
    6590           8 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    6591             :                 }
    6592         106 :                 BBPunfix(b->batCacheid);
    6593             :         }
    6594         106 :         res_table_destroy(output);
    6595         106 :         return err;
    6596             : }
    6597             : 
    6598             : static str
    6599         106 : sql_update_default(Client c, mvc *sql, sql_schema *s)
    6600             : {
    6601         106 :         char *err;
    6602         106 :         res_table *output;
    6603         106 :         BAT *b;
    6604             : 
    6605         106 :         (void) sql;
    6606         106 :         (void) s;
    6607         106 :         err = SQLstatementIntern(c, "SELECT id FROM sys.functions WHERE schema_id = 2000 AND name = 'describe_type' AND func LIKE '%sql_datatype%';\n", "update", true, false, &output);
    6608         106 :         if (err)
    6609             :                 return err;
    6610         106 :         b = BATdescriptor(output->cols[0].b);
    6611         106 :         if (b) {
    6612         106 :                 if (BATcount(b) == 0) {
    6613             :                         /* do update */
    6614          16 :                         sql_table *t;
    6615          16 :                         const char *query =
    6616             :                                 "update sys._columns set type_digits = 7 where type = 'tinyint' and type_digits <> 7;\n"
    6617             :                                 "update sys._columns set type_digits = 15 where type = 'smallint' and type_digits <> 15;\n"
    6618             :                                 "update sys._columns set type_digits = 31 where type = 'int' and type_digits <> 31;\n"
    6619             :                                 "update sys._columns set type_digits = 63 where type = 'bigint' and type_digits <> 63;\n"
    6620             :                                 "update sys._columns set type_digits = 127 where type = 'hugeint' and type_digits <> 127;\n"
    6621             :                                 "update sys._columns set type = 'varchar' where type in ('clob', 'char') and table_id in (select id from sys._tables where system and name <> 'netcdf_files');\n"
    6622             :                                 "update sys.args set type_digits = 7 where type = 'tinyint' and type_digits <> 7;\n"
    6623             :                                 "update sys.args set type_digits = 15 where type = 'smallint' and type_digits <> 15;\n"
    6624             :                                 "update sys.args set type_digits = 31 where type = 'int' and type_digits <> 31;\n"
    6625             :                                 "update sys.args set type_digits = 63 where type = 'bigint' and type_digits <> 63;\n"
    6626             :                                 "update sys.args set type_digits = 127 where type = 'hugeint' and type_digits <> 127;\n"
    6627             :                                 "update sys.args set type = 'varchar' where type in ('clob', 'char');\n"
    6628             :                                 "drop aggregate median(decimal);\n"
    6629             :                                 "drop aggregate median_avg(decimal);\n"
    6630             :                                 "drop aggregate quantile(decimal, double);\n"
    6631             :                                 "drop aggregate quantile_avg(decimal, double);\n"
    6632             :                                 "create aggregate median(val DECIMAL(2)) returns DECIMAL(2)\n"
    6633             :                                 " external name \"aggr\".\"median\";\n"
    6634             :                                 "GRANT EXECUTE ON AGGREGATE median(DECIMAL(2)) TO PUBLIC;\n"
    6635             :                                 "create aggregate median(val DECIMAL(4)) returns DECIMAL(4)\n"
    6636             :                                 " external name \"aggr\".\"median\";\n"
    6637             :                                 "GRANT EXECUTE ON AGGREGATE median(DECIMAL(4)) TO PUBLIC;\n"
    6638             :                                 "create aggregate median(val DECIMAL(9)) returns DECIMAL(9)\n"
    6639             :                                 " external name \"aggr\".\"median\";\n"
    6640             :                                 "GRANT EXECUTE ON AGGREGATE median(DECIMAL(9)) TO PUBLIC;\n"
    6641             :                                 "create aggregate median(val DECIMAL(18)) returns DECIMAL(18)\n"
    6642             :                                 " external name \"aggr\".\"median\";\n"
    6643             :                                 "GRANT EXECUTE ON AGGREGATE median(DECIMAL(18)) TO PUBLIC;\n"
    6644             : #ifdef HAVE_HGE
    6645             :                                 "create aggregate median(val DECIMAL(38)) returns DECIMAL(38)\n"
    6646             :                                 " external name \"aggr\".\"median\";\n"
    6647             :                                 "GRANT EXECUTE ON AGGREGATE median(DECIMAL(38)) TO PUBLIC;\n"
    6648             : #endif
    6649             :                                 "create aggregate median_avg(val DECIMAL(2)) returns DOUBLE\n"
    6650             :                                 " external name \"aggr\".\"median_avg\";\n"
    6651             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(2)) TO PUBLIC;\n"
    6652             :                                 "create aggregate median_avg(val DECIMAL(4)) returns DOUBLE\n"
    6653             :                                 " external name \"aggr\".\"median_avg\";\n"
    6654             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(4)) TO PUBLIC;\n"
    6655             :                                 "create aggregate median_avg(val DECIMAL(9)) returns DOUBLE\n"
    6656             :                                 " external name \"aggr\".\"median_avg\";\n"
    6657             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(9)) TO PUBLIC;\n"
    6658             :                                 "create aggregate median_avg(val DECIMAL(18)) returns DOUBLE\n"
    6659             :                                 " external name \"aggr\".\"median_avg\";\n"
    6660             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(18)) TO PUBLIC;\n"
    6661             : #ifdef HAVE_HGE
    6662             :                                 "create aggregate median_avg(val DECIMAL(38)) returns DOUBLE\n"
    6663             :                                 " external name \"aggr\".\"median_avg\";\n"
    6664             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(38)) TO PUBLIC;\n"
    6665             : #endif
    6666             :                                 "create aggregate quantile(val DECIMAL(2), q DOUBLE) returns DECIMAL(2)\n"
    6667             :                                 " external name \"aggr\".\"quantile\";\n"
    6668             :                                 "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(2), DOUBLE) TO PUBLIC;\n"
    6669             :                                 "create aggregate quantile(val DECIMAL(4), q DOUBLE) returns DECIMAL(4)\n"
    6670             :                                 " external name \"aggr\".\"quantile\";\n"
    6671             :                                 "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(4), DOUBLE) TO PUBLIC;\n"
    6672             :                                 "create aggregate quantile(val DECIMAL(9), q DOUBLE) returns DECIMAL(9)\n"
    6673             :                                 " external name \"aggr\".\"quantile\";\n"
    6674             :                                 "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(9), DOUBLE) TO PUBLIC;\n"
    6675             :                                 "create aggregate quantile(val DECIMAL(18), q DOUBLE) returns DECIMAL(18)\n"
    6676             :                                 " external name \"aggr\".\"quantile\";\n"
    6677             :                                 "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(18), DOUBLE) TO PUBLIC;\n"
    6678             : #ifdef HAVE_HGE
    6679             :                                 "create aggregate quantile(val DECIMAL(38), q DOUBLE) returns DECIMAL(38)\n"
    6680             :                                 " external name \"aggr\".\"quantile\";\n"
    6681             :                                 "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(38), DOUBLE) TO PUBLIC;\n"
    6682             : #endif
    6683             :                                 "create aggregate quantile_avg(val DECIMAL(2), q DOUBLE) returns DOUBLE\n"
    6684             :                                 " external name \"aggr\".\"quantile_avg\";\n"
    6685             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(2), DOUBLE) TO PUBLIC;\n"
    6686             :                                 "create aggregate quantile_avg(val DECIMAL(4), q DOUBLE) returns DOUBLE\n"
    6687             :                                 " external name \"aggr\".\"quantile_avg\";\n"
    6688             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(4), DOUBLE) TO PUBLIC;\n"
    6689             :                                 "create aggregate quantile_avg(val DECIMAL(9), q DOUBLE) returns DOUBLE\n"
    6690             :                                 " external name \"aggr\".\"quantile_avg\";\n"
    6691             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(9), DOUBLE) TO PUBLIC;\n"
    6692             :                                 "create aggregate quantile_avg(val DECIMAL(18), q DOUBLE) returns DOUBLE\n"
    6693             :                                 " external name \"aggr\".\"quantile_avg\";\n"
    6694             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(18), DOUBLE) TO PUBLIC;\n"
    6695             : #ifdef HAVE_HGE
    6696             :                                 "create aggregate quantile_avg(val DECIMAL(38), q DOUBLE) returns DOUBLE\n"
    6697             :                                 " external name \"aggr\".\"quantile_avg\";\n"
    6698             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(38), DOUBLE) TO PUBLIC;\n"
    6699             : #endif
    6700             :                                 "drop function if exists sys.time_to_str(time with time zone, string) cascade;\n"
    6701             :                                 "drop function if exists sys.timestamp_to_str(timestamp with time zone, string) cascade;\n"
    6702             :                                 "create function time_to_str(d time, format string) returns string\n"
    6703             :                                 " external name mtime.\"time_to_str\";\n"
    6704             :                                 "create function time_to_str(d time with time zone, format string) returns string\n"
    6705             :                                 " external name mtime.\"timetz_to_str\";\n"
    6706             :                                 "create function timestamp_to_str(d timestamp with time zone, format string) returns string\n"
    6707             :                                 " external name mtime.\"timestamptz_to_str\";\n"
    6708             :                                 "grant execute on function time_to_str(time, string) to public;\n"
    6709             :                                 "grant execute on function time_to_str(time with time zone, string) to public;\n"
    6710             :                                 "grant execute on function timestamp_to_str(timestamp with time zone, string) to public;\n"
    6711             :                                 "update sys.functions set system = true where not system and schema_id = 2000 and name in ('time_to_str', 'timestamp_to_str', 'median', 'median_avg', 'quantile', 'quantile_avg');\n"
    6712             :                                 "drop function if exists sys.dump_database(boolean) cascade;\n"
    6713             :                                 "drop view sys.dump_comments;\n"
    6714             :                                 "drop view sys.dump_tables;\n"
    6715             :                                 "drop view sys.dump_functions;\n"
    6716             :                                 "drop view sys.dump_function_grants;\n"
    6717             :                                 "drop function if exists sys.describe_columns(string, string) cascade;\n"
    6718             :                                 "drop view sys.describe_functions;\n"
    6719             :                                 "drop view sys.describe_privileges;\n"
    6720             :                                 "drop view sys.describe_comments;\n"
    6721             :                                 "drop view sys.fully_qualified_functions;\n"
    6722             :                                 "drop view sys.describe_tables;\n"
    6723             :                                 "drop function if exists sys.describe_type(string, integer, integer) cascade;\n"
    6724             :                                 "CREATE FUNCTION sys.describe_type(ctype string, digits integer, tscale integer)\n"
    6725             :                                 " RETURNS string\n"
    6726             :                                 "BEGIN\n"
    6727             :                                 " RETURN sys.sql_datatype(ctype, digits, tscale, false, false);\n"
    6728             :                                 "END;\n"
    6729             :                                 "CREATE VIEW sys.describe_tables AS\n"
    6730             :                                 " SELECT\n"
    6731             :                                 " t.id o,\n"
    6732             :                                 " s.name sch,\n"
    6733             :                                 " t.name tab,\n"
    6734             :                                 " ts.table_type_name typ,\n"
    6735             :                                 " (SELECT\n"
    6736             :                                 " ' (' ||\n"
    6737             :                                 " GROUP_CONCAT(\n"
    6738             :                                 " sys.DQ(c.name) || ' ' ||\n"
    6739             :                                 " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    6740             :                                 " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    6741             :                                 " , ', ') || ')'\n"
    6742             :                                 " FROM sys._columns c\n"
    6743             :                                 " WHERE c.table_id = t.id) col,\n"
    6744             :                                 " CASE ts.table_type_name\n"
    6745             :                                 " WHEN 'REMOTE TABLE' THEN\n"
    6746             :                                 " sys.get_remote_table_expressions(s.name, t.name)\n"
    6747             :                                 " WHEN 'MERGE TABLE' THEN\n"
    6748             :                                 " sys.get_merge_table_partition_expressions(t.id)\n"
    6749             :                                 " WHEN 'VIEW' THEN\n"
    6750             :                                 " sys.schema_guard(s.name, t.name, t.query)\n"
    6751             :                                 " ELSE\n"
    6752             :                                 " ''\n"
    6753             :                                 " END opt\n"
    6754             :                                 " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    6755             :                                 " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
    6756             :                                 " AND t.system = FALSE\n"
    6757             :                                 " AND s.id = t.schema_id\n"
    6758             :                                 " AND ts.table_type_id = t.type\n"
    6759             :                                 " AND s.name <> 'tmp';\n"
    6760             :                                 "CREATE VIEW sys.fully_qualified_functions AS\n"
    6761             :                                 " WITH fqn(id, tpe, sig, num) AS\n"
    6762             :                                 " (\n"
    6763             :                                 " SELECT\n"
    6764             :                                 " f.id,\n"
    6765             :                                 " ft.function_type_keyword,\n"
    6766             :                                 " CASE WHEN a.type IS NULL THEN\n"
    6767             :                                 " sys.fqn(s.name, f.name) || '()'\n"
    6768             :                                 " ELSE\n"
    6769             :                                 " sys.fqn(s.name, f.name) || '(' || group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ',') OVER (PARTITION BY f.id ORDER BY a.number)  || ')'\n"
    6770             :                                 " END,\n"
    6771             :                                 " a.number\n"
    6772             :                                 " FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
    6773             :                                 " WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
    6774             :                                 " )\n"
    6775             :                                 " SELECT\n"
    6776             :                                 " fqn1.id id,\n"
    6777             :                                 " fqn1.tpe tpe,\n"
    6778             :                                 " fqn1.sig nme\n"
    6779             :                                 " FROM\n"
    6780             :                                 " fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id)  fqn2(id, num)\n"
    6781             :                                 " ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
    6782             :                                 "CREATE VIEW sys.describe_comments AS\n"
    6783             :                                 " SELECT o.id AS id, o.tpe AS tpe, o.nme AS fqn, cm.remark AS rem\n"
    6784             :                                 " FROM (\n"
    6785             :                                 " SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas WHERE NOT system\n"
    6786             :                                 " UNION ALL\n"
    6787             :                                 " SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
    6788             :                                 " FROM sys.schemas s JOIN sys._tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
    6789             :                                 " WHERE NOT t.system\n"
    6790             :                                 " UNION ALL\n"
    6791             :                                 " SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys._tables t, sys.schemas s WHERE NOT t.system AND c.table_id = t.id AND t.schema_id = s.id\n"
    6792             :                                 " UNION ALL\n"
    6793             :                                 " SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE NOT t.system AND idx.table_id = t.id AND t.schema_id = s.id\n"
    6794             :                                 " UNION ALL\n"
    6795             :                                 " SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    6796             :                                 " UNION ALL\n"
    6797             :                                 " SELECT f.id, ft.function_type_keyword, qf.nme FROM sys.functions f, sys.function_types ft, sys.schemas s, sys.fully_qualified_functions qf\n"
    6798             :                                 " WHERE NOT f.system AND f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
    6799             :                                 " ) AS o(id, tpe, nme)\n"
    6800             :                                 " JOIN sys.comments cm ON cm.id = o.id;\n"
    6801             :                                 "CREATE VIEW sys.describe_privileges AS\n"
    6802             :                                 " SELECT\n"
    6803             :                                 " CASE\n"
    6804             :                                 " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
    6805             :                                 " 'COPY FROM'\n"
    6806             :                                 " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
    6807             :                                 " 'COPY INTO'\n"
    6808             :                                 " ELSE\n"
    6809             :                                 " o.nme\n"
    6810             :                                 " END o_nme,\n"
    6811             :                                 " coalesce(o.tpe, 'GLOBAL') o_tpe,\n"
    6812             :                                 " pc.privilege_code_name p_nme,\n"
    6813             :                                 " a.name a_nme,\n"
    6814             :                                 " g.name g_nme,\n"
    6815             :                                 " p.grantable grantable\n"
    6816             :                                 " FROM\n"
    6817             :                                 " sys.privileges p LEFT JOIN\n"
    6818             :                                 " (\n"
    6819             :                                 " SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
    6820             :                                 " from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
    6821             :                                 " UNION ALL\n"
    6822             :                                 " SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
    6823             :                                 " FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
    6824             :                                 " UNION ALL\n"
    6825             :                                 " SELECT f.id, f.nme, f.tpe\n"
    6826             :                                 " FROM sys.fully_qualified_functions f\n"
    6827             :                                 " ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
    6828             :                                 " sys.privilege_codes pc,\n"
    6829             :                                 " auths a, auths g\n"
    6830             :                                 " WHERE\n"
    6831             :                                 " p.privileges = pc.privilege_code_id AND\n"
    6832             :                                 " p.auth_id = a.id AND\n"
    6833             :                                 " p.grantor = g.id;\n"
    6834             :                                 "CREATE VIEW sys.describe_functions AS\n"
    6835             :                                 " WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    6836             :                                 " (\n"
    6837             :                                 " SELECT\n"
    6838             :                                 " func_id,\n"
    6839             :                                 " number,\n"
    6840             :                                 " max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    6841             :                                 " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    6842             :                                 " FROM sys.args\n"
    6843             :                                 " WHERE inout = 1\n"
    6844             :                                 " ),\n"
    6845             :                                 " func_args(func_id, func_arg) AS\n"
    6846             :                                 " (\n"
    6847             :                                 " SELECT func_id, func_arg\n"
    6848             :                                 " FROM func_args_all\n"
    6849             :                                 " WHERE number = max_number\n"
    6850             :                                 " ),\n"
    6851             :                                 " func_rets_all(func_id, number, max_number, func_ret, func_ret_type) AS\n"
    6852             :                                 " (\n"
    6853             :                                 " SELECT\n"
    6854             :                                 " func_id,\n"
    6855             :                                 " number,\n"
    6856             :                                 " max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    6857             :                                 " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number),\n"
    6858             :                                 " group_concat(sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    6859             :                                 " FROM sys.args\n"
    6860             :                                 " WHERE inout = 0\n"
    6861             :                                 " ),\n"
    6862             :                                 " func_rets(func_id, func_ret, func_ret_type) AS\n"
    6863             :                                 " (\n"
    6864             :                                 " SELECT\n"
    6865             :                                 " func_id,\n"
    6866             :                                 " func_ret,\n"
    6867             :                                 " func_ret_type\n"
    6868             :                                 " FROM func_rets_all\n"
    6869             :                                 " WHERE number = max_number\n"
    6870             :                                 " )\n"
    6871             :                                 " SELECT\n"
    6872             :                                 " f.id o,\n"
    6873             :                                 " s.name sch,\n"
    6874             :                                 " f.name fun,\n"
    6875             :                                 " CASE WHEN f.language IN (1, 2) THEN f.func ELSE 'CREATE ' || ft.function_type_keyword || ' ' || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ')' || CASE WHEN f.type = 5 THEN ' RETURNS TABLE (' || coalesce(fr.func_ret, '') || ')' WHEN f.type IN (1,3) THEN ' RETURNS ' || fr.func_ret_type ELSE '' END || CASE WHEN fl.language_keyword IS NULL THEN '' ELSE ' LANGUAGE ' || fl.language_keyword END || ' ' || f.func END def\n"
    6876             :                                 " FROM sys.functions f\n"
    6877             :                                 " LEFT OUTER JOIN func_args fa ON fa.func_id = f.id\n"
    6878             :                                 " LEFT OUTER JOIN func_rets fr ON fr.func_id = f.id\n"
    6879             :                                 " JOIN sys.schemas s ON f.schema_id = s.id\n"
    6880             :                                 " JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
    6881             :                                 " LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
    6882             :                                 " WHERE s.name <> 'tmp' AND NOT f.system;\n"
    6883             :                                 "CREATE FUNCTION sys.describe_columns(schemaName string, tableName string)\n"
    6884             :                                 " RETURNS TABLE(name string, type string, digits integer, scale integer, Nulls boolean, cDefault string, number integer, sqltype string, remark string)\n"
    6885             :                                 "BEGIN\n"
    6886             :                                 " RETURN SELECT c.name, c.\"type\", c.type_digits, c.type_scale, c.\"null\", c.\"default\", c.number, sys.describe_type(c.\"type\", c.type_digits, c.type_scale), com.remark\n"
    6887             :                                 " FROM sys._tables t, sys.schemas s, sys._columns c\n"
    6888             :                                 " LEFT OUTER JOIN sys.comments com ON c.id = com.id\n"
    6889             :                                 " WHERE c.table_id = t.id\n"
    6890             :                                 " AND t.name = tableName\n"
    6891             :                                 " AND t.schema_id = s.id\n"
    6892             :                                 " AND s.name = schemaName\n"
    6893             :                                 " ORDER BY c.number;\n"
    6894             :                                 "END;\n"
    6895             :                                 "CREATE VIEW sys.dump_function_grants AS\n"
    6896             :                                 " WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    6897             :                                 " (SELECT a.func_id,\n"
    6898             :                                 " a.number,\n"
    6899             :                                 " max(a.number) OVER (PARTITION BY a.func_id ORDER BY a.number DESC),\n"
    6900             :                                 " group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ', ') OVER (PARTITION BY a.func_id ORDER BY a.number)\n"
    6901             :                                 " FROM sys.args a\n"
    6902             :                                 " WHERE a.inout = 1),\n"
    6903             :                                 " func_args(func_id, func_arg) AS\n"
    6904             :                                 " (SELECT func_id, func_arg FROM func_args_all WHERE number = max_number)\n"
    6905             :                                 " SELECT\n"
    6906             :                                 " 'GRANT ' || pc.privilege_code_name || ' ON ' || ft.function_type_keyword || ' '\n"
    6907             :                                 " || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ') TO '\n"
    6908             :                                 " || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
    6909             :                                 " || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    6910             :                                 " s.name schema_name,\n"
    6911             :                                 " f.name function_name,\n"
    6912             :                                 " a.name grantee\n"
    6913             :                                 " FROM sys.schemas s,\n"
    6914             :                                 " sys.functions f LEFT OUTER JOIN func_args fa ON f.id = fa.func_id,\n"
    6915             :                                 " sys.auths a,\n"
    6916             :                                 " sys.privileges p,\n"
    6917             :                                 " sys.auths g,\n"
    6918             :                                 " sys.function_types ft,\n"
    6919             :                                 " sys.privilege_codes pc\n"
    6920             :                                 " WHERE s.id = f.schema_id\n"
    6921             :                                 " AND f.id = p.obj_id\n"
    6922             :                                 " AND p.auth_id = a.id\n"
    6923             :                                 " AND p.grantor = g.id\n"
    6924             :                                 " AND p.privileges = pc.privilege_code_id\n"
    6925             :                                 " AND f.type = ft.function_type_id\n"
    6926             :                                 " AND NOT f.system\n"
    6927             :                                 " ORDER BY s.name, f.name, a.name, g.name, p.grantable;\n"
    6928             :                                 "CREATE VIEW sys.dump_functions AS\n"
    6929             :                                 " SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
    6930             :                                 " f.sch schema_name,\n"
    6931             :                                 " f.fun function_name\n"
    6932             :                                 " FROM sys.describe_functions f;\n"
    6933             :                                 "CREATE VIEW sys.dump_tables AS\n"
    6934             :                                 " SELECT\n"
    6935             :                                 " t.o o,\n"
    6936             :                                 " CASE\n"
    6937             :                                 " WHEN t.typ <> 'VIEW' THEN\n"
    6938             :                                 " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    6939             :                                 " ELSE\n"
    6940             :                                 " t.opt\n"
    6941             :                                 " END stmt,\n"
    6942             :                                 " t.sch schema_name,\n"
    6943             :                                 " t.tab table_name\n"
    6944             :                                 " FROM sys.describe_tables t;\n"
    6945             :                                 "CREATE VIEW sys.dump_comments AS\n"
    6946             :                                 " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    6947             :                                 "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    6948             :                                 "BEGIN\n"
    6949             :                                 " SET SCHEMA sys;\n"
    6950             :                                 " TRUNCATE sys.dump_statements;\n"
    6951             :                                 " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    6952             :                                 " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
    6953             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    6954             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    6955             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    6956             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    6957             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    6958             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    6959             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    6960             :                                 "\n"
    6961             :                                 " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    6962             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    6963             :                                 " FROM (\n"
    6964             :                                 " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    6965             :                                 " UNION ALL\n"
    6966             :                                 " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    6967             :                                 " ) AS stmts(o, s);\n"
    6968             :                                 "\n"
    6969             :                                 " -- dump table data before adding constraints and fixing sequences\n"
    6970             :                                 " IF NOT DESCRIBE THEN\n"
    6971             :                                 " CALL sys.dump_table_data();\n"
    6972             :                                 " END IF;\n"
    6973             :                                 "\n"
    6974             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    6975             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    6976             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    6977             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    6978             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    6979             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    6980             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    6981             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    6982             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    6983             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    6984             :                                 " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    6985             :                                 "\n"
    6986             :                                 " --TODO Improve performance of dump_table_data.\n"
    6987             :                                 " --TODO loaders, procedures, window and filter sys.functions.\n"
    6988             :                                 " --TODO look into order dependent group_concat\n"
    6989             :                                 "\n"
    6990             :                                 " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    6991             :                                 "\n"
    6992             :                                 " RETURN sys.dump_statements;\n"
    6993             :                                 "END;\n"
    6994             :                                 "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
    6995             :                                 "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
    6996             :                                 "GRANT SELECT ON sys.fully_qualified_functions TO PUBLIC;\n"
    6997             :                                 "GRANT SELECT ON sys.describe_privileges TO PUBLIC;\n"
    6998             :                                 "GRANT SELECT ON sys.describe_functions TO PUBLIC;\n"
    6999             :                                 "update sys.functions set system = true where not system and schema_id = 2000 and name in ('dump_database', 'describe_columns', 'describe_type');\n"
    7000             :                                 "update sys._tables set system = true where not system and schema_id = 2000 and name in ('dump_comments', 'dump_tables', 'dump_functions', 'dump_function_grants', 'describe_functions', 'describe_privileges', 'describe_comments', 'fully_qualified_functions', 'describe_tables');\n";
    7001          16 :                         if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
    7002          16 :                                 t->system = 0;
    7003          16 :                         if ((t = mvc_bind_table(sql, s, "dump_tables")) != NULL)
    7004          16 :                                 t->system = 0;
    7005          16 :                         if ((t = mvc_bind_table(sql, s, "dump_functions")) != NULL)
    7006          16 :                                 t->system = 0;
    7007          16 :                         if ((t = mvc_bind_table(sql, s, "dump_function_grants")) != NULL)
    7008          16 :                                 t->system = 0;
    7009          16 :                         if ((t = mvc_bind_table(sql, s, "describe_functions")) != NULL)
    7010          16 :                                 t->system = 0;
    7011          16 :                         if ((t = mvc_bind_table(sql, s, "describe_privileges")) != NULL)
    7012          16 :                                 t->system = 0;
    7013          16 :                         if ((t = mvc_bind_table(sql, s, "describe_comments")) != NULL)
    7014          16 :                                 t->system = 0;
    7015          16 :                         if ((t = mvc_bind_table(sql, s, "fully_qualified_functions")) != NULL)
    7016          16 :                                 t->system = 0;
    7017          16 :                         if ((t = mvc_bind_table(sql, s, "describe_tables")) != NULL)
    7018          16 :                                 t->system = 0;
    7019          16 :                         printf("Running database upgrade commands:\n%s\n", query);
    7020          16 :                         fflush(stdout);
    7021          16 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    7022             :                 }
    7023         106 :                 BBPunfix(b->batCacheid);
    7024             :         }
    7025         106 :         res_table_destroy(output);
    7026         106 :         return err;
    7027             : }
    7028             : 
    7029             : int
    7030         109 : SQLupgrades(Client c, mvc *m)
    7031             : {
    7032         109 :         sql_subtype tp;
    7033         109 :         sql_subfunc *f;
    7034         109 :         char *err;
    7035         109 :         sql_schema *s = mvc_bind_schema(m, "sys");
    7036             : 
    7037         109 :         if ((err = check_sys_tables(c, m, s)) != NULL) {
    7038           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7039           0 :                 goto handle_error;
    7040             :         }
    7041             : 
    7042             : #ifdef HAVE_HGE
    7043         109 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    7044         109 :         if (!sql_bind_func(m, s->base.name, "var_pop", &tp, NULL, F_AGGR, true, true)) {
    7045           8 :                 m->session->status = 0; /* if the function was not found clean the error */
    7046           8 :                 m->errstr[0] = '\0';
    7047           8 :                 if ((err = sql_update_hugeint(c, m)) != NULL) {
    7048           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7049           0 :                         goto handle_error;
    7050             :                 }
    7051             :         }
    7052             : #endif
    7053             : 
    7054         109 :         if ((err = sql_update_generator(c)) != NULL) {
    7055           3 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7056           3 :                 goto handle_error;
    7057             :         }
    7058             : 
    7059         106 :         f = sql_bind_func_(m, s->base.name, "env", NULL, F_UNION, true, true);
    7060         106 :         m->session->status = 0; /* if the function was not found clean the error */
    7061         106 :         m->errstr[0] = '\0';
    7062         106 :         sqlstore *store = m->session->tr->store;
    7063         106 :         if (f && sql_privilege(m, ROLE_PUBLIC, f->func->base.id, PRIV_EXECUTE) != PRIV_EXECUTE) {
    7064           0 :                 sql_table *privs = find_sql_table(m->session->tr, s, "privileges");
    7065           0 :                 int pub = ROLE_PUBLIC, p = PRIV_EXECUTE, zero = 0, res;
    7066             : 
    7067           0 :                 if ((res = store->table_api.table_insert(m->session->tr, privs, &f->func->base.id, &pub, &p, &zero, &zero)) != LOG_OK) {
    7068           0 :                         TRC_CRITICAL(SQL_PARSER, "Privilege creation during upgrade failed\n");
    7069           0 :                         return -1;
    7070             :                 }
    7071             :         }
    7072             : 
    7073         106 :         if (sql_bind_func(m, s->base.name, "dependencies_schemas_on_users", NULL, NULL, F_UNION, true, true)
    7074           0 :          && sql_bind_func(m, s->base.name, "dependencies_owners_on_schemas", NULL, NULL, F_UNION, true, true)
    7075           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_views", NULL, NULL, F_UNION, true, true)
    7076           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_indexes", NULL, NULL, F_UNION, true, true)
    7077           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_triggers", NULL, NULL, F_UNION, true, true)
    7078           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_foreignkeys", NULL, NULL, F_UNION, true, true)
    7079           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_functions", NULL, NULL, F_UNION, true, true)
    7080           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_views", NULL, NULL, F_UNION, true, true)
    7081           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_keys", NULL, NULL, F_UNION, true, true)
    7082           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_indexes", NULL, NULL, F_UNION, true, true)
    7083           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_functions", NULL, NULL, F_UNION, true, true)
    7084           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_triggers", NULL, NULL, F_UNION, true, true)
    7085           0 :          && sql_bind_func(m, s->base.name, "dependencies_views_on_functions", NULL, NULL, F_UNION, true, true)
    7086           0 :          && sql_bind_func(m, s->base.name, "dependencies_views_on_triggers", NULL, NULL, F_UNION, true, true)
    7087           0 :          && sql_bind_func(m, s->base.name, "dependencies_functions_on_functions", NULL, NULL, F_UNION, true, true)
    7088           0 :          && sql_bind_func(m, s->base.name, "dependencies_functions_on_triggers", NULL, NULL, F_UNION, true, true)
    7089           0 :          && sql_bind_func(m, s->base.name, "dependencies_keys_on_foreignkeys", NULL, NULL, F_UNION, true, true)) {
    7090           0 :                 if ((err = sql_drop_functions_dependencies_Xs_on_Ys(c)) != NULL) {
    7091           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7092           0 :                         goto handle_error;
    7093             :                 }
    7094             :         } else {
    7095         106 :                 m->session->status = 0; /* if the function was not found clean the error */
    7096         106 :                 m->errstr[0] = '\0';
    7097             :         }
    7098             : 
    7099         106 :         sql_find_subtype(&tp, "varchar", 0, 0);
    7100         106 :         if (!sql_bind_func3(m, s->base.name, "deltas", &tp, &tp, &tp, F_UNION, true)) {
    7101           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    7102           0 :                 m->errstr[0] = '\0';
    7103           0 :                 if ((err = sql_update_nov2019_missing_dependencies(c, m)) != NULL) {
    7104           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7105           0 :                         goto handle_error;
    7106             :                 }
    7107           0 :                 if ((err = sql_update_nov2019(c, m)) != NULL) {
    7108           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7109           0 :                         goto handle_error;
    7110             :                 }
    7111             :         }
    7112             : 
    7113             : #ifdef HAVE_HGE
    7114         106 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    7115         106 :         if (!sql_bind_func(m, s->base.name, "median_avg", &tp, NULL, F_AGGR, true, true)) {
    7116           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    7117           0 :                 m->errstr[0] = '\0';
    7118           0 :                 if ((err = sql_update_nov2019_sp1_hugeint(c, m)) != NULL) {
    7119           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7120           0 :                         goto handle_error;
    7121             :                 }
    7122             :         }
    7123             : #endif
    7124             : 
    7125         106 :         if (!sql_bind_func(m, s->base.name, "suspend_log_flushing", NULL, NULL, F_PROC, true, true)) {
    7126           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    7127           0 :                 m->errstr[0] = '\0';
    7128           0 :                 if ((err = sql_update_jun2020(c, m)) != NULL) {
    7129           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7130           0 :                         goto handle_error;
    7131             :                 }
    7132             :         }
    7133             : 
    7134         106 :         if ((err = sql_update_jun2020_bam(c, m)) != NULL) {
    7135           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7136           0 :                 goto handle_error;
    7137             :         }
    7138             : 
    7139             : #ifdef HAVE_HGE
    7140         106 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    7141         106 :         if (!sql_bind_func(m, s->base.name, "covar_pop", &tp, &tp, F_AGGR, true, true)) {
    7142           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    7143           0 :                 m->errstr[0] = '\0';
    7144           0 :                 if ((err = sql_update_jun2020_sp1_hugeint(c)) != NULL) {
    7145           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7146           0 :                         goto handle_error;
    7147             :                 }
    7148             :         }
    7149             : #endif
    7150             : 
    7151         106 :         sql_find_subtype(&tp, "varchar", 0, 0);
    7152         106 :         if (sql_bind_func(m, s->base.name, "lidarattach", &tp, NULL, F_PROC, true, true)) {
    7153           0 :                 if ((err = sql_update_oscar_lidar(c)) != NULL) {
    7154           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7155           0 :                         goto handle_error;
    7156             :                 }
    7157             :         } else {
    7158         106 :                 m->session->status = 0; /* if the function was not found clean the error */
    7159         106 :                 m->errstr[0] = '\0';
    7160             :         }
    7161             : 
    7162         106 :         if ((err = sql_update_oscar(c, m)) != NULL) {
    7163           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7164           0 :                 goto handle_error;
    7165             :         }
    7166             : 
    7167         106 :         if ((err = sql_update_oct2020(c, m)) != NULL) {
    7168           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7169           0 :                 goto handle_error;
    7170             :         }
    7171             : 
    7172         106 :         if ((err = sql_update_oct2020_sp1(c, m)) != NULL) {
    7173           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7174           0 :                 goto handle_error;
    7175             :         }
    7176             : 
    7177         106 :         if ((err = sql_update_jul2021(c, m)) != NULL) {
    7178           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7179           0 :                 goto handle_error;
    7180             :         }
    7181             : 
    7182         106 :         if ((err = sql_update_jul2021_5(c, m)) != NULL) {
    7183           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7184           0 :                 goto handle_error;
    7185             :         }
    7186             : 
    7187         106 :         if ((err = sql_update_jan2022(c, m)) != NULL) {
    7188           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7189           0 :                 goto handle_error;
    7190             :         }
    7191             : 
    7192         106 :         if ((err = sql_update_sep2022(c, m, s)) != NULL) {
    7193           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7194           0 :                 goto handle_error;
    7195             :         }
    7196             : 
    7197         106 :         if ((err = sql_update_jun2023(c, m, s)) != NULL) {
    7198           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7199           0 :                 goto handle_error;
    7200             :         }
    7201             : 
    7202         106 :         if ((err = sql_update_dec2023_geom(c, m, s)) != NULL) {
    7203           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7204           0 :                 goto handle_error;
    7205             :         }
    7206             : 
    7207         106 :         if ((err = sql_update_jun2023_sp3(c, m, s)) != NULL) {
    7208           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7209           0 :                 goto handle_error;
    7210             :         }
    7211             : 
    7212         106 :         if ((err = sql_update_dec2023(c, m, s)) != NULL) {
    7213           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7214           0 :                 goto handle_error;
    7215             :         }
    7216             : 
    7217         106 :         if ((err = sql_update_dec2023_sp1(c, m, s)) != NULL) {
    7218           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7219           0 :                 goto handle_error;
    7220             :         }
    7221             : 
    7222         106 :         if ((err = sql_update_default(c, m, s)) != NULL) {
    7223           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    7224           0 :                 goto handle_error;
    7225             :         }
    7226             : 
    7227             :         return 0;
    7228             : 
    7229           3 : handle_error:
    7230           3 :         freeException(err);
    7231           3 :         return -1;
    7232             : }

Generated by: LCOV version 1.14