LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql_upgrades.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 491 1418 34.6 %
Date: 2024-04-25 20:03:45 Functions: 20 28 71.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          12 : sql_fix_system_tables(Client c, mvc *sql)
      34             : {
      35          12 :         size_t bufsize = 1000000, pos = 0;
      36          12 :         char *buf = GDKmalloc(bufsize), *err = NULL;
      37          12 :         node *n;
      38          12 :         sql_schema *s;
      39          12 :         static const char *boolnames[2] = {"false", "true"};
      40             : 
      41          12 :         if (buf == NULL)
      42           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
      43          12 :         s = mvc_bind_schema(sql, "sys");
      44             : 
      45          12 :         pos += snprintf(buf + pos, bufsize - pos,
      46             :                         "delete from sys.dependencies where id < 2000;\n");
      47             : 
      48             :         /* recreate internal types */
      49          12 :         pos += snprintf(buf + pos, bufsize - pos,
      50             :                         "delete from sys.types where id < 2000;\n");
      51         396 :         for (n = types->h; n; n = n->next) {
      52         384 :                 sql_type *t = n->data;
      53             : 
      54         384 :                 if (t->base.id >= FUNC_OIDS)
      55           0 :                         continue;
      56             : 
      57         384 :                 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         384 :                                 t->scale, t->radix, (int) t->eclass,
      62         384 :                                 t->s ? t->s->base.id : s->base.id);
      63             :         }
      64             : 
      65             :         /* recreate internal functions */
      66          12 :         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          12 :         sqlstore *store = sql->session->tr->store;
      71       12708 :         for (n = funcs->h; n; n = n->next) {
      72       12696 :                 sql_func *func = n->data;
      73       12696 :                 int number = 0;
      74       12696 :                 sql_arg *arg;
      75       12696 :                 node *m;
      76             : 
      77       12696 :                 if (func->private || func->base.id >= FUNC_OIDS)
      78        2748 :                         continue;
      79             : 
      80       19896 :                 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        9948 :                                 (int) func->type,
      87        9948 :                                 boolnames[func->side_effect],
      88        9948 :                                 boolnames[func->varres],
      89        9948 :                                 boolnames[func->vararg],
      90        9948 :                                 func->s ? func->s->base.id : s->base.id,
      91        9948 :                                 boolnames[func->system],
      92        9948 :                                 boolnames[func->semantics]);
      93        9948 :                 if (func->res) {
      94       19848 :                         for (m = func->res->h; m; m = m->next, number++) {
      95        9924 :                                 arg = m->data;
      96        9924 :                                 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        9924 :                                                 arg->type.type->base.name,
     106             :                                                 arg->type.digits,
     107             :                                                 arg->type.scale,
     108        9924 :                                                 arg->inout, number);
     109             :                         }
     110             :                 }
     111       26832 :                 for (m = func->ops->h; m; m = m->next, number++) {
     112       16884 :                         arg = m->data;
     113       16884 :                         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       16884 :                                 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       16884 :                                                 arg->type.type->base.name,
     137             :                                                 arg->type.digits,
     138             :                                                 arg->type.scale,
     139       16884 :                                                 arg->inout, number);
     140             :                 }
     141             :         }
     142             : 
     143          12 :         assert(pos < bufsize);
     144          12 :         printf("Running database upgrade commands to update system tables.\n\n");
     145          12 :         fflush(stdout);
     146          12 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     147          12 :         GDKfree(buf);
     148          12 :         return err;             /* usually MAL_SUCCEED */
     149             : }
     150             : 
     151             : static str
     152         102 : check_sys_tables(Client c, mvc *m, sql_schema *s)
     153             : {
     154         102 :         struct {
     155             :                 const char *name;
     156             :                 const char *func;
     157             :                 const char *type;
     158             :                 sql_ftype ftype;
     159         102 :         } 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             : 
     170             :         /* if any of the tested function's internal ID does not match the ID
     171             :          * in the sys.functions table, we recreate the internal part of the
     172             :          * system tables */
     173         462 :         for (int i = 0; tests[i].name; i++) {
     174         372 :                 bool needsystabfix = true;
     175         372 :                 sql_subtype tp, *tpp;
     176         372 :                 if (tests[i].type) {
     177         282 :                         sql_find_subtype(&tp, tests[i].type, 0, 0);
     178         282 :                         tpp = &tp;
     179             :                 } else {
     180             :                         tpp = NULL;
     181             :                 }
     182         372 :                 sql_subfunc *f = sql_bind_func(m, s->base.name, tests[i].name, tpp, NULL, tests[i].ftype, true);
     183         372 :                 if (f == NULL)
     184          12 :                         throw(SQL, __func__, "cannot find procedure sys.%s(%s)", tests[i].name, tests[i].type ? tests[i].type : "");
     185         372 :                 sqlid id = f->func->base.id;
     186         372 :                 char buf[256];
     187         372 :                 snprintf(buf, sizeof(buf),
     188             :                                  "select id from sys.functions where name = '%s' and func = '%s' and schema_id = 2000;\n",
     189             :                                  tests[i].name, tests[i].func);
     190         372 :                 res_table *output = NULL;
     191         372 :                 char *err = SQLstatementIntern(c, buf, "update", true, false, &output);
     192         372 :                 if (err)
     193           0 :                         return err;
     194         372 :                 BAT *b;
     195         372 :                 b = BATdescriptor(output->cols[0].b);
     196         372 :                 res_table_destroy(output);
     197         372 :                 if (b == NULL)
     198           0 :                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     199         372 :                 if (BATcount(b) > 0) {
     200         372 :                         BATiter bi = bat_iterator(b);
     201         372 :                         needsystabfix = * (int *) BUNtloc(bi, 0) != id;
     202         372 :                         bat_iterator_end(&bi);
     203             :                 }
     204         372 :                 BBPunfix(b->batCacheid);
     205         372 :                 if (i == 0 && !needsystabfix) {
     206          90 :                         snprintf(buf, sizeof(buf),
     207             :                                          "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");
     208          90 :                         err = SQLstatementIntern(c, buf, "update", true, false, &output);
     209          90 :                         if (err)
     210           0 :                                 return err;
     211          90 :                         b = BATdescriptor(output->cols[0].b);
     212          90 :                         res_table_destroy(output);
     213          90 :                         if (b == NULL)
     214           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     215          90 :                         needsystabfix = BATcount(b) > 0;
     216          90 :                         BBPunfix(b->batCacheid);
     217             :                 }
     218         372 :                 if (needsystabfix)
     219          12 :                         return sql_fix_system_tables(c, m);
     220             :         }
     221             :         return NULL;
     222             : }
     223             : 
     224             : #ifdef HAVE_HGE
     225             : static str
     226           8 : sql_update_hugeint(Client c, mvc *sql)
     227             : {
     228           8 :         size_t bufsize = 8192, pos = 0;
     229           8 :         char *buf, *err;
     230             : 
     231           8 :         (void) sql;
     232           8 :         if ((buf = GDKmalloc(bufsize)) == NULL)
     233           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     234             : 
     235             :         /* 90_generator_hge.sql */
     236           8 :         pos += snprintf(buf + pos, bufsize - pos,
     237             :                         "create function sys.generate_series(first hugeint, \"limit\" hugeint)\n"
     238             :                         "returns table (value hugeint)\n"
     239             :                         "external name generator.series;\n"
     240             :                         "create function sys.generate_series(first hugeint, \"limit\" hugeint, stepsize hugeint)\n"
     241             :                         "returns table (value hugeint)\n"
     242             :                         "external name generator.series;\n");
     243             : 
     244             :         /* 39_analytics_hge.sql */
     245           8 :         pos += snprintf(buf + pos, bufsize - pos,
     246             :                         "create aggregate stddev_samp(val HUGEINT) returns DOUBLE\n"
     247             :                         " external name \"aggr\".\"stdev\";\n"
     248             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(HUGEINT) TO PUBLIC;\n"
     249             :                         "create window stddev_samp(val HUGEINT) returns DOUBLE\n"
     250             :                         " external name \"sql\".\"stdev\";\n"
     251             :                         "GRANT EXECUTE ON WINDOW stddev_samp(HUGEINT) TO PUBLIC;\n"
     252             :                         "create aggregate stddev_pop(val HUGEINT) returns DOUBLE\n"
     253             :                         " external name \"aggr\".\"stdevp\";\n"
     254             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(HUGEINT) TO PUBLIC;\n"
     255             :                         "create window stddev_pop(val HUGEINT) returns DOUBLE\n"
     256             :                         " external name \"sql\".\"stdevp\";\n"
     257             :                         "GRANT EXECUTE ON WINDOW stddev_pop(HUGEINT) TO PUBLIC;\n"
     258             :                         "create aggregate var_samp(val HUGEINT) returns DOUBLE\n"
     259             :                         " external name \"aggr\".\"variance\";\n"
     260             :                         "GRANT EXECUTE ON AGGREGATE var_samp(HUGEINT) TO PUBLIC;\n"
     261             :                         "create window var_samp(val HUGEINT) returns DOUBLE\n"
     262             :                         " external name \"sql\".\"variance\";\n"
     263             :                         "GRANT EXECUTE ON WINDOW var_samp(HUGEINT) TO PUBLIC;\n"
     264             :                         "create aggregate covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     265             :                         " external name \"aggr\".\"covariance\";\n"
     266             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
     267             :                         "create window covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     268             :                         " external name \"sql\".\"covariance\";\n"
     269             :                         "GRANT EXECUTE ON WINDOW covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
     270             :                         "create aggregate var_pop(val HUGEINT) returns DOUBLE\n"
     271             :                         " external name \"aggr\".\"variancep\";\n"
     272             :                         "GRANT EXECUTE ON AGGREGATE var_pop(HUGEINT) TO PUBLIC;\n"
     273             :                         "create window var_pop(val HUGEINT) returns DOUBLE\n"
     274             :                         " external name \"sql\".\"variancep\";\n"
     275             :                         "GRANT EXECUTE ON WINDOW var_pop(HUGEINT) TO PUBLIC;\n"
     276             :                         "create aggregate covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     277             :                         " external name \"aggr\".\"covariancep\";\n"
     278             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
     279             :                         "create window covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     280             :                         " external name \"sql\".\"covariancep\";\n"
     281             :                         "GRANT EXECUTE ON WINDOW covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
     282             :                         "create aggregate median(val HUGEINT) returns HUGEINT\n"
     283             :                         " external name \"aggr\".\"median\";\n"
     284             :                         "GRANT EXECUTE ON AGGREGATE median(HUGEINT) TO PUBLIC;\n"
     285             :                         "create aggregate quantile(val HUGEINT, q DOUBLE) returns HUGEINT\n"
     286             :                         " external name \"aggr\".\"quantile\";\n"
     287             :                         "GRANT EXECUTE ON AGGREGATE quantile(HUGEINT, DOUBLE) TO PUBLIC;\n"
     288             :                         "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
     289             :                         " external name \"aggr\".\"median_avg\";\n"
     290             :                         "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
     291             :                         "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
     292             :                         " external name \"aggr\".\"quantile_avg\";\n"
     293             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n"
     294             :                         "create aggregate corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     295             :                         " external name \"aggr\".\"corr\";\n"
     296             :                         "GRANT EXECUTE ON AGGREGATE corr(HUGEINT, HUGEINT) TO PUBLIC;\n"
     297             :                         "create window corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     298             :                         " external name \"sql\".\"corr\";\n"
     299             :                         "GRANT EXECUTE ON WINDOW corr(HUGEINT, HUGEINT) TO PUBLIC;\n");
     300             : 
     301             :         /* 40_json_hge.sql */
     302           8 :         pos += snprintf(buf + pos, bufsize - pos,
     303             :                         "create function json.filter(js json, name hugeint)\n"
     304             :                         "returns json external name json.filter;\n"
     305             :                         "GRANT EXECUTE ON FUNCTION json.filter(json, hugeint) TO PUBLIC;\n");
     306             : 
     307           8 :         pos += snprintf(buf + pos, bufsize - pos,
     308             :                         "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"
     309             :                         "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"
     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', '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 = 'filter' and schema_id = (select id from sys.schemas where name = 'json') and type = %d;\n",
     312             :                         (int) F_UNION, (int) F_AGGR, (int) F_ANALYTIC, (int) F_FUNC);
     313             : 
     314           8 :         assert(pos < bufsize);
     315             : 
     316           8 :         printf("Running database upgrade commands:\n%s\n", buf);
     317           8 :         fflush(stdout);
     318           8 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     319           8 :         GDKfree(buf);
     320           8 :         return err;             /* usually MAL_SUCCEED */
     321             : }
     322             : #endif
     323             : 
     324             : #ifdef HAVE_SHP
     325             : static str
     326           8 : sql_create_shp(Client c)
     327             : {
     328             :         //Create the new SHPload procedures
     329           8 :         const char *query = "create procedure SHPLoad(fname string, schemaname string, tablename string) external name shp.load;\n"
     330             :                 "create procedure SHPLoad(fname string, tablename string) external name shp.load;\n"
     331             :                 "update sys.functions set system = true where schema_id = 2000 and name in ('shpload');";
     332           8 :         printf("Running database upgrade commands:\n%s\n", query);
     333           8 :         fflush(stdout);
     334           8 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
     335             : }
     336             : #endif
     337             : 
     338             : static str
     339           8 : sql_drop_shp(Client c)
     340             : {
     341             :         //Drop the old SHP procedures (upgrade from version before shpload upgrade)
     342           8 :         const char *query = "drop procedure if exists SHPattach(string) cascade;\n"
     343             :                 "drop procedure if exists SHPload(integer) cascade;\n"
     344             :                 "drop procedure if exists SHPload(integer, geometry) cascade;\n";
     345           8 :         printf("Running database upgrade commands:\n%s\n", query);
     346           8 :         fflush(stdout);
     347           8 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
     348             : }
     349             : 
     350             : static str
     351         102 : sql_update_generator(Client c)
     352             : {
     353         102 :         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"
     354             :                 "update sys.functions set func = replace(func, ' last ', ' \"limit\" ') where schema_id = 2000 and name = 'generate_series' and func like '% last %';\n";
     355         102 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
     356             : }
     357             : 
     358             : static str
     359           0 : sql_drop_functions_dependencies_Xs_on_Ys(Client c)
     360             : {
     361           0 :         size_t bufsize = 1600, pos = 0;
     362           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     363             : 
     364           0 :         if (buf == NULL)
     365           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     366             : 
     367             :         /* remove functions which were created in sql/scripts/21_dependency_functions.sql */
     368           0 :         pos += snprintf(buf + pos, bufsize - pos,
     369             :                         "DROP FUNCTION dependencies_schemas_on_users() CASCADE;\n"
     370             :                         "DROP FUNCTION dependencies_owners_on_schemas() CASCADE;\n"
     371             :                         "DROP FUNCTION dependencies_tables_on_views() CASCADE;\n"
     372             :                         "DROP FUNCTION dependencies_tables_on_indexes() CASCADE;\n"
     373             :                         "DROP FUNCTION dependencies_tables_on_triggers() CASCADE;\n"
     374             :                         "DROP FUNCTION dependencies_tables_on_foreignKeys() CASCADE;\n"
     375             :                         "DROP FUNCTION dependencies_tables_on_functions() CASCADE;\n"
     376             :                         "DROP FUNCTION dependencies_columns_on_views() CASCADE;\n"
     377             :                         "DROP FUNCTION dependencies_columns_on_keys() CASCADE;\n"
     378             :                         "DROP FUNCTION dependencies_columns_on_indexes() CASCADE;\n"
     379             :                         "DROP FUNCTION dependencies_columns_on_functions() CASCADE;\n"
     380             :                         "DROP FUNCTION dependencies_columns_on_triggers() CASCADE;\n"
     381             :                         "DROP FUNCTION dependencies_views_on_functions() CASCADE;\n"
     382             :                         "DROP FUNCTION dependencies_views_on_triggers() CASCADE;\n"
     383             :                         "DROP FUNCTION dependencies_functions_on_functions() CASCADE;\n"
     384             :                         "DROP FUNCTION dependencies_functions_on_triggers() CASCADE;\n"
     385             :                         "DROP FUNCTION dependencies_keys_on_foreignKeys() CASCADE;\n");
     386             : 
     387           0 :         assert(pos < bufsize);
     388             : 
     389           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     390           0 :         fflush(stdout);
     391           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     392           0 :         GDKfree(buf);
     393           0 :         return err;             /* usually MAL_SUCCEED */
     394             : }
     395             : 
     396             : static str
     397           0 : sql_update_storagemodel(Client c, mvc *sql, bool oct2020_upgrade)
     398             : {
     399           0 :         size_t bufsize = 20000, pos = 0;
     400           0 :         char *buf, *err;
     401           0 :         sql_schema *s = mvc_bind_schema(sql, "sys");
     402           0 :         sql_table *t;
     403           0 :         char *day_interval_str = oct2020_upgrade ? " 'day_interval'," : "";
     404             : 
     405           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
     406           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     407             : 
     408             :         /* set views and tables internally to non-system to allow drop commands to succeed without error */
     409           0 :         if ((t = mvc_bind_table(sql, s, "storage")) != NULL)
     410           0 :                 t->system = 0;
     411           0 :         if ((t = mvc_bind_table(sql, s, "storagemodel")) != NULL)
     412           0 :                 t->system = 0;
     413           0 :         if ((t = mvc_bind_table(sql, s, "storagemodelinput")) != NULL)
     414           0 :                 t->system = 0;
     415           0 :         if ((t = mvc_bind_table(sql, s, "tablestoragemodel")) != NULL)
     416           0 :                 t->system = 0;
     417             : 
     418             :         /* new 75_storagemodel.sql */
     419           0 :         pos += snprintf(buf + pos, bufsize - pos,
     420             :                 /* drop objects in reverse order of original creation of old 75_storagemodel.sql */
     421             :                 "drop view if exists sys.tablestoragemodel cascade;\n"
     422             :                 "drop view if exists sys.storagemodel cascade;\n"
     423             :                 "drop function if exists sys.storagemodel() cascade;\n");
     424             : 
     425           0 :         if (oct2020_upgrade) {
     426           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     427             :                         "drop function if exists sys.imprintsize(varchar(1024), bigint) cascade;\n");
     428             :         } else {
     429           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     430             :                         "drop function if exists sys.imprintsize(bigint, clob) cascade;\n");
     431             :         }
     432             : 
     433           0 :         pos += snprintf(buf + pos, bufsize - pos,
     434             :                 "drop function if exists sys.hashsize(boolean, bigint) cascade;\n");
     435             : 
     436           0 :         if (oct2020_upgrade) {
     437           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     438             :                         "drop function if exists sys.columnsize(varchar(1024), bigint) cascade;\n"
     439             :                         "drop function if exists sys.heapsize(varchar(1024), bigint, bigint, int) cascade;\n");
     440             :         } else {
     441           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     442             :                         "drop function if exists sys.columnsize(clob, bigint, bigint) cascade;\n"
     443             :                         "drop function if exists sys.heapsize(clob, bigint, int) cascade;\n");
     444             :         }
     445             : 
     446           0 :         pos += snprintf(buf + pos, bufsize - pos,
     447             :                 "drop procedure if exists sys.storagemodelinit() cascade;\n"
     448             :                 "drop table if exists sys.storagemodelinput cascade;\n"
     449             :                 "drop view if exists sys.\"storage\" cascade;\n");
     450             : 
     451           0 :         if (oct2020_upgrade) {
     452           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     453             :                         "drop function if exists sys.\"storage\"(varchar(1024), varchar(1024), varchar(1024)) cascade;\n"
     454             :                         "drop function if exists sys.\"storage\"(varchar(1024), varchar(1024)) cascade;\n"
     455             :                         "drop function if exists sys.\"storage\"(varchar(1024)) cascade;\n");
     456             :         } else {
     457           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     458             :                         "drop function if exists sys.\"storage\"(clob, clob, clob) cascade;\n"
     459             :                         "drop function if exists sys.\"storage\"(clob, clob) cascade;\n"
     460             :                         "drop function if exists sys.\"storage\"(clob) cascade;\n");
     461             :         }
     462             : 
     463             :         /* new 75_storagemodel.sql */
     464           0 :         pos += snprintf(buf + pos, bufsize - pos,
     465             :                 "drop function if exists sys.\"storage\"() cascade;\n"
     466             :                 "create function sys.\"storage\"()\n"
     467             :                 "returns table (\n"
     468             :                 "  \"schema\" varchar(1024),\n"
     469             :                 "  \"table\" varchar(1024),\n"
     470             :                 "  \"column\" varchar(1024),\n"
     471             :                 "  \"type\" varchar(1024),\n"
     472             :                 "  \"mode\" varchar(15),\n"
     473             :                 "  location varchar(1024),\n"
     474             :                 "  \"count\" bigint,\n"
     475             :                 "  typewidth int,\n"
     476             :                 "  columnsize bigint,\n"
     477             :                 "  heapsize bigint,\n"
     478             :                 "  hashes bigint,\n"
     479             :                 "  phash boolean,\n"
     480             :                 "  \"imprints\" bigint,\n"
     481             :                 "  sorted boolean,\n"
     482             :                 "  revsorted boolean,\n"
     483             :                 "  \"unique\" boolean,\n"
     484             :                 "  orderidx bigint\n"
     485             :                 ")\n"
     486             :                 "external name sql.\"storage\";\n"
     487             :                 "create view sys.\"storage\" as\n"
     488             :                 "select * from sys.\"storage\"()\n"
     489             :                 " where (\"schema\", \"table\") in (\n"
     490             :                 "  SELECT sch.\"name\", tbl.\"name\"\n"
     491             :                 "    FROM sys.\"tables\" AS tbl JOIN sys.\"schemas\" AS sch ON tbl.schema_id = sch.id\n"
     492             :                 "   WHERE tbl.\"system\" = FALSE)\n"
     493             :                 "order by \"schema\", \"table\", \"column\";\n"
     494             :                 "create view sys.\"tablestorage\" as\n"
     495             :                 "select \"schema\", \"table\",\n"
     496             :                 "  max(\"count\") as \"rowcount\",\n"
     497             :                 "  count(*) as \"storages\",\n"
     498             :                 "  sum(columnsize) as columnsize,\n"
     499             :                 "  sum(heapsize) as heapsize,\n"
     500             :                 "  sum(hashes) as hashsize,\n"
     501             :                 "  sum(\"imprints\") as imprintsize,\n"
     502             :                 "  sum(orderidx) as orderidxsize\n"
     503             :                 " from sys.\"storage\"\n"
     504             :                 "group by \"schema\", \"table\"\n"
     505             :                 "order by \"schema\", \"table\";\n"
     506             :                 "create view sys.\"schemastorage\" as\n"
     507             :                 "select \"schema\",\n"
     508             :                 "  count(*) as \"storages\",\n"
     509             :                 "  sum(columnsize) as columnsize,\n"
     510             :                 "  sum(heapsize) as heapsize,\n"
     511             :                 "  sum(hashes) as hashsize,\n"
     512             :                 "  sum(\"imprints\") as imprintsize,\n"
     513             :                 "  sum(orderidx) as orderidxsize\n"
     514             :                 " from sys.\"storage\"\n"
     515             :                 "group by \"schema\"\n"
     516             :                 "order by \"schema\";\n"
     517             :                 "create function sys.\"storage\"(sname varchar(1024))\n"
     518             :                 "returns table (\n"
     519             :                 "  \"schema\" varchar(1024),\n"
     520             :                 "  \"table\" varchar(1024),\n"
     521             :                 "  \"column\" varchar(1024),\n"
     522             :                 "  \"type\" varchar(1024),\n"
     523             :                 "  \"mode\" varchar(15),\n"
     524             :                 "  location varchar(1024),\n"
     525             :                 "  \"count\" bigint,\n"
     526             :                 "  typewidth int,\n"
     527             :                 "  columnsize bigint,\n"
     528             :                 "  heapsize bigint,\n"
     529             :                 "  hashes bigint,\n"
     530             :                 "  phash boolean,\n"
     531             :                 "  \"imprints\" bigint,\n"
     532             :                 "  sorted boolean,\n"
     533             :                 "  revsorted boolean,\n"
     534             :                 "  \"unique\" boolean,\n"
     535             :                 "  orderidx bigint\n"
     536             :                 ")\n"
     537             :                 "external name sql.\"storage\";\n"
     538             :                 "create function sys.\"storage\"(sname varchar(1024), tname varchar(1024))\n"
     539             :                 "returns table (\n"
     540             :                 "  \"schema\" varchar(1024),\n"
     541             :                 "  \"table\" varchar(1024),\n"
     542             :                 "  \"column\" varchar(1024),\n"
     543             :                 "  \"type\" varchar(1024),\n"
     544             :                 "  \"mode\" varchar(15),\n"
     545             :                 "  location varchar(1024),\n"
     546             :                 "  \"count\" bigint,\n"
     547             :                 "  typewidth int,\n"
     548             :                 "  columnsize bigint,\n"
     549             :                 "  heapsize bigint,\n"
     550             :                 "  hashes bigint,\n"
     551             :                 "  phash boolean,\n"
     552             :                 "  \"imprints\" bigint,\n"
     553             :                 "  sorted boolean,\n"
     554             :                 "  revsorted boolean,\n"
     555             :                 "  \"unique\" boolean,\n"
     556             :                 "  orderidx bigint\n"
     557             :                 ")\n"
     558             :                 "external name sql.\"storage\";\n"
     559             :                 "create function sys.\"storage\"(sname varchar(1024), tname varchar(1024), cname varchar(1024))\n"
     560             :                 "returns table (\n"
     561             :                 "  \"schema\" varchar(1024),\n"
     562             :                 "  \"table\" varchar(1024),\n"
     563             :                 "  \"column\" varchar(1024),\n"
     564             :                 "  \"type\" varchar(1024),\n"
     565             :                 "  \"mode\" varchar(15),\n"
     566             :                 "  location varchar(1024),\n"
     567             :                 "  \"count\" bigint,\n"
     568             :                 "  typewidth int,\n"
     569             :                 "  columnsize bigint,\n"
     570             :                 "  heapsize bigint,\n"
     571             :                 "  hashes bigint,\n"
     572             :                 "  phash boolean,\n"
     573             :                 "  \"imprints\" bigint,\n"
     574             :                 "  sorted boolean,\n"
     575             :                 "  revsorted boolean,\n"
     576             :                 "  \"unique\" boolean,\n"
     577             :                 "  orderidx bigint\n"
     578             :                 ")\n"
     579             :                 "external name sql.\"storage\";\n"
     580             :                 "create table sys.storagemodelinput(\n"
     581             :                 "  \"schema\" varchar(1024) NOT NULL,\n"
     582             :                 "  \"table\" varchar(1024) NOT NULL,\n"
     583             :                 "  \"column\" varchar(1024) NOT NULL,\n"
     584             :                 "  \"type\" varchar(1024) NOT NULL,\n"
     585             :                 "  typewidth int NOT NULL,\n"
     586             :                 "  \"count\" bigint NOT NULL,\n"
     587             :                 "  \"distinct\" bigint NOT NULL,\n"
     588             :                 "  atomwidth int NOT NULL,\n"
     589             :                 "  reference boolean NOT NULL DEFAULT FALSE,\n"
     590             :                 "  sorted boolean,\n"
     591             :                 "  \"unique\" boolean,\n"
     592             :                 "  isacolumn boolean NOT NULL DEFAULT TRUE\n"
     593             :                 ");\n"
     594             :                 "create procedure sys.storagemodelinit()\n"
     595             :                 "begin\n"
     596             :                 "  delete from sys.storagemodelinput;\n"
     597             :                 "  insert into sys.storagemodelinput\n"
     598             :                 "  select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
     599             :                 "          case when (\"unique\" or \"type\" IN ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
     600             :                 "                  then \"count\" else 0 end,\n"
     601             :                 "          case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
     602             :                 "                  then cast((heapsize - 8192) / \"count\" as bigint)\n"
     603             :                 "          when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
     604             :                 "                  then cast((heapsize - 32) / \"count\" as bigint)\n"
     605             :                 "          else typewidth end,\n"
     606             :                 "          FALSE, case sorted when true then true else false end, \"unique\", TRUE\n"
     607             :                 "    from sys.\"storage\";\n"
     608             :                 "  update sys.storagemodelinput\n"
     609             :                 "     set reference = TRUE\n"
     610             :                 "   where (\"schema\", \"table\", \"column\") in (\n"
     611             :                 "          SELECT fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
     612             :                 "            FROM  sys.\"keys\" AS fkkey,\n"
     613             :                 "                  sys.\"objects\" AS fkkeycol,\n"
     614             :                 "                  sys.\"tables\" AS fktable,\n"
     615             :                 "                  sys.\"schemas\" AS fkschema\n"
     616             :                 "          WHERE fktable.\"id\" = fkkey.\"table_id\"\n"
     617             :                 "            AND fkkey.\"id\" = fkkeycol.\"id\"\n"
     618             :                 "            AND fkschema.\"id\" = fktable.\"schema_id\"\n"
     619             :                 "            AND fkkey.\"rkey\" > -1 );\n"
     620             :                 "  update sys.storagemodelinput\n"
     621             :                 "     set isacolumn = FALSE\n"
     622             :                 "   where (\"schema\", \"table\", \"column\") NOT in (\n"
     623             :                 "          SELECT sch.\"name\", tbl.\"name\", col.\"name\"\n"
     624             :                 "            FROM sys.\"schemas\" AS sch,\n"
     625             :                 "                  sys.\"tables\" AS tbl,\n"
     626             :                 "                  sys.\"columns\" AS col\n"
     627             :                 "          WHERE sch.\"id\" = tbl.\"schema_id\"\n"
     628             :                 "            AND tbl.\"id\" = col.\"table_id\");\n"
     629             :                 "end;\n"
     630             :                 "create function sys.columnsize(tpe varchar(1024), count bigint)\n"
     631             :                 "returns bigint\n"
     632             :                 "begin\n"
     633             :                 "  if tpe in ('tinyint', 'boolean')\n"
     634             :                 "          then return count;\n"
     635             :                 "  end if;\n"
     636             :                 "  if tpe = 'smallint'\n"
     637             :                 "          then return 2 * count;\n"
     638             :                 "  end if;\n"
     639             :                 "  if tpe in ('int', 'real', 'date', 'time', 'timetz', 'sec_interval',%s 'month_interval')\n"
     640             :                 "          then return 4 * count;\n"
     641             :                 "  end if;\n"
     642             :                 "  if tpe in ('bigint', 'double', 'timestamp', 'timestamptz', 'inet', 'oid')\n"
     643             :                 "          then return 8 * count;\n"
     644             :                 "  end if;\n"
     645             :                 "  if tpe in ('hugeint', 'decimal', 'uuid', 'mbr')\n"
     646             :                 "          then return 16 * count;\n"
     647             :                 "  end if;\n"
     648             :                 "  if tpe in ('varchar', 'char', 'clob', 'json', 'url')\n"
     649             :                 "          then return 4 * count;\n"
     650             :                 "  end if;\n"
     651             :                 "  if tpe in ('blob', 'geometry', 'geometrya')\n"
     652             :                 "          then return 8 * count;\n"
     653             :                 "  end if;\n"
     654             :                 "  return 8 * count;\n"
     655             :                 "end;\n"
     656             :                 "create function sys.heapsize(tpe varchar(1024), count bigint, distincts bigint, avgwidth int)\n"
     657             :                 "returns bigint\n"
     658             :                 "begin\n"
     659             :                 "  if tpe in ('varchar', 'char', 'clob', 'json', 'url')\n"
     660             :                 "          then return 8192 + ((avgwidth + 8) * distincts);\n"
     661             :                 "  end if;\n"
     662             :                 "  if tpe in ('blob', 'geometry', 'geometrya')\n"
     663             :                 "          then return 32 + (avgwidth * count);\n"
     664             :                 "  end if;\n"
     665             :                 "  return 0;\n"
     666             :                 "end;\n"
     667             :                 "create function sys.hashsize(b boolean, count bigint)\n"
     668             :                 "returns bigint\n"
     669             :                 "begin\n"
     670             :                 "  if b = true\n"
     671             :                 "          then return 8 * count;\n"
     672             :                 "  end if;\n"
     673             :                 "  return 0;\n"
     674             :                 "end;\n"
     675             :                 "create function sys.imprintsize(tpe varchar(1024), count bigint)\n"
     676             :                 "returns bigint\n"
     677             :                 "begin\n"
     678             :                 "  if tpe in ('tinyint', 'boolean')\n"
     679             :                 "          then return cast(0.2 * count as bigint);\n"
     680             :                 "  end if;\n"
     681             :                 "  if tpe = 'smallint'\n"
     682             :                 "          then return cast(0.4 * count as bigint);\n"
     683             :                 "  end if;\n"
     684             :                 "  if tpe in ('int', 'real', 'date', 'time', 'timetz', 'sec_interval',%s 'month_interval')\n"
     685             :                 "          then return cast(0.8 * count as bigint);\n"
     686             :                 "  end if;\n"
     687             :                 "  if tpe in ('bigint', 'double', 'timestamp', 'timestamptz', 'inet', 'oid')\n"
     688             :                 "          then return cast(1.6 * count as bigint);\n"
     689             :                 "  end if;\n"
     690             :                 "  if tpe in ('hugeint', 'decimal', 'uuid', 'mbr')\n"
     691             :                 "          then return cast(3.2 * count as bigint);\n"
     692             :                 "  end if;\n"
     693             :                 "  return 0;\n"
     694             :                 "end;\n"
     695             :                 "create view sys.storagemodel as\n"
     696             :                 "select \"schema\", \"table\", \"column\", \"type\", \"count\",\n"
     697             :                 "  sys.columnsize(\"type\", \"count\") as columnsize,\n"
     698             :                 "  sys.heapsize(\"type\", \"count\", \"distinct\", \"atomwidth\") as heapsize,\n"
     699             :                 "  sys.hashsize(\"reference\", \"count\") as hashsize,\n"
     700             :                 "  case when isacolumn then sys.imprintsize(\"type\", \"count\") else 0 end as imprintsize,\n"
     701             :                 "  case when (isacolumn and not sorted) then cast(8 * \"count\" as bigint) else 0 end as orderidxsize,\n"
     702             :                 "  sorted, \"unique\", isacolumn\n"
     703             :                 " from sys.storagemodelinput\n"
     704             :                 "order by \"schema\", \"table\", \"column\";\n"
     705             :                 "create view sys.tablestoragemodel as\n"
     706             :                 "select \"schema\", \"table\",\n"
     707             :                 "  max(\"count\") as \"rowcount\",\n"
     708             :                 "  count(*) as \"storages\",\n"
     709             :                 "  sum(sys.columnsize(\"type\", \"count\")) as columnsize,\n"
     710             :                 "  sum(sys.heapsize(\"type\", \"count\", \"distinct\", \"atomwidth\")) as heapsize,\n"
     711             :                 "  sum(sys.hashsize(\"reference\", \"count\")) as hashsize,\n"
     712             :                 "  sum(case when isacolumn then sys.imprintsize(\"type\", \"count\") else 0 end) as imprintsize,\n"
     713             :                 "  sum(case when (isacolumn and not sorted) then cast(8 * \"count\" as bigint) else 0 end) as orderidxsize\n"
     714             :                 " from sys.storagemodelinput\n"
     715             :                 "group by \"schema\", \"table\"\n"
     716             :                 "order by \"schema\", \"table\";\n", day_interval_str, day_interval_str);
     717           0 :         assert(pos < bufsize);
     718             : 
     719           0 :         pos += snprintf(buf + pos, bufsize - pos,
     720             :                 "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
     721             :                 " and name in ('storage', 'tablestorage', 'schemastorage', 'storagemodelinput', 'storagemodel', 'tablestoragemodel');\n");
     722           0 :         pos += snprintf(buf + pos, bufsize - pos,
     723             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     724             :                 " and name in ('storage') and type = %d;\n", (int) F_UNION);
     725           0 :         pos += snprintf(buf + pos, bufsize - pos,
     726             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     727             :                 " and name in ('storagemodelinit') and type = %d;\n", (int) F_PROC);
     728           0 :         pos += snprintf(buf + pos, bufsize - pos,
     729             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     730             :                 " and name in ('columnsize', 'heapsize', 'hashsize', 'imprintsize') and type = %d;\n", (int) F_FUNC);
     731             : 
     732           0 :         assert(pos < bufsize);
     733             : 
     734           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     735           0 :         fflush(stdout);
     736           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     737           0 :         GDKfree(buf);
     738           0 :         return err;             /* usually MAL_SUCCEED */
     739             : }
     740             : 
     741             : #define FLUSH_INSERTS_IF_BUFFERFILLED                                                                   \
     742             :         do {                                                                                                                            \
     743             :                 /* Each new value should add about 20 bytes to the buffer, */   \
     744             :                 /* "flush" when is 200 bytes from being full */                                       \
     745             :                 if (pos > 7900) {                                                                                            \
     746             :                         pos += snprintf(buf + pos, bufsize - pos,                                       \
     747             :                                                         ") as t1(c1,c2,c3) where t1.c1 not in (select \"id\" from sys.dependencies where depend_id = t1.c2);\n"); \
     748             :                         assert(pos < bufsize);                                                                               \
     749             :                         printf("Running database upgrade commands:\n%s\n", buf);      \
     750             :                         fflush(stdout);                                                                                         \
     751             :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL); \
     752             :                         if (err)                                                                                                        \
     753             :                                 goto bailout;                                                                                   \
     754             :                         pos = 0;                                                                                                        \
     755             :                         pos += snprintf(buf + pos, bufsize - pos, "insert into sys.dependencies select c1, c2, c3 from (values"); \
     756             :                         ppos = pos;                                                                                                     \
     757             :                         first = true;                                                                                           \
     758             :                 }                                                                                                                               \
     759             :         } while (0)
     760             : 
     761             : static str
     762           0 : sql_update_nov2019_missing_dependencies(Client c, mvc *sql)
     763             : {
     764           0 :         size_t bufsize = 8192, pos = 0, ppos;
     765           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     766           0 :         sql_allocator *old_sa = sql->sa;
     767           0 :         bool first = true;
     768           0 :         sql_trans *tr = sql->session->tr;
     769           0 :         struct os_iter si;
     770             : 
     771           0 :         if (buf == NULL)
     772           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     773             : 
     774           0 :         if (!(sql->sa = sa_create(sql->pa))) {
     775           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     776           0 :                 goto bailout;
     777             :         }
     778             : 
     779           0 :         pos += snprintf(buf + pos, bufsize - pos, "insert into sys.dependencies select c1, c2, c3 from (values");
     780           0 :         ppos = pos; /* later check if found updatable database objects */
     781             : 
     782           0 :         os_iterator(&si, sql->session->tr->cat->schemas, sql->session->tr, NULL);
     783           0 :         for (sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
     784           0 :                 sql_schema *s = (sql_schema*)b;
     785             : 
     786           0 :                 struct os_iter oi;
     787           0 :                 os_iterator(&oi, s->funcs, sql->session->tr, NULL);
     788           0 :                 for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
     789           0 :                         sql_func *f = (sql_func*)b;
     790             : 
     791           0 :                         if (f->query && f->lang == FUNC_LANG_SQL) {
     792           0 :                                 char *relt;
     793           0 :                                 sql_rel *r = NULL;
     794             : 
     795           0 :                                 if (!(relt = sa_strdup(sql->sa, f->query))) {
     796           0 :                                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     797           0 :                                         goto bailout;
     798             :                                 }
     799             : 
     800           0 :                                 r = rel_parse(sql, s, relt, m_deps);
     801           0 :                                 if (r)
     802           0 :                                         r = sql_processrelation(sql, r, 0, 0, 0, 0);
     803           0 :                                 if (r) {
     804           0 :                                         list *id_l = rel_dependencies(sql, r);
     805             : 
     806           0 :                                         for (node *o = id_l->h ; o ; o = o->next) {
     807           0 :                                                 sqlid next = ((sql_base*) o->data)->id;
     808           0 :                                                 if (next != f->base.id) {
     809           0 :                                                         pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",", next,
     810           0 :                                                                                         f->base.id, (int)(!IS_PROC(f) ? FUNC_DEPENDENCY : PROC_DEPENDENCY));
     811           0 :                                                         first = false;
     812           0 :                                                         FLUSH_INSERTS_IF_BUFFERFILLED;
     813             :                                                 }
     814             :                                         }
     815           0 :                                 } else if (sql->session->status == -1) {
     816           0 :                                         sql->session->status = 0;
     817           0 :                                         sql->errstr[0] = 0;
     818             :                                 }
     819             :                         }
     820             :                 }
     821           0 :                 if (s->tables) {
     822           0 :                         struct os_iter oi;
     823           0 :                         os_iterator(&oi, s->tables, tr, NULL);
     824           0 :                         for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
     825           0 :                                 sql_table *t = (sql_table*) b;
     826             : 
     827           0 :                                 if (t->query && isView(t)) {
     828           0 :                                         char *relt;
     829           0 :                                         sql_rel *r = NULL;
     830             : 
     831           0 :                                         if (!(relt = sa_strdup(sql->sa, t->query))) {
     832           0 :                                                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     833           0 :                                                 goto bailout;
     834             :                                         }
     835             : 
     836           0 :                                         r = rel_parse(sql, s, relt, m_deps);
     837           0 :                                         if (r)
     838           0 :                                                 r = sql_processrelation(sql, r, 0, 0, 0, 0);
     839           0 :                                         if (r) {
     840           0 :                                                 list *id_l = rel_dependencies(sql, r);
     841             : 
     842           0 :                                                 for (node *o = id_l->h ; o ; o = o->next) {
     843           0 :                                                         sqlid next = ((sql_base*) o->data)->id;
     844           0 :                                                         if (next != t->base.id) {
     845           0 :                                                                 pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",",
     846             :                                                                                                 next, t->base.id, (int) VIEW_DEPENDENCY);
     847           0 :                                                                 first = false;
     848           0 :                                                                 FLUSH_INSERTS_IF_BUFFERFILLED;
     849             :                                                         }
     850             :                                                 }
     851             :                                         }
     852             :                                 }
     853           0 :                                 if (t->triggers)
     854           0 :                                         for (node *mm = ol_first_node(t->triggers); mm; mm = mm->next) {
     855           0 :                                                 sql_trigger *tr = (sql_trigger*) mm->data;
     856           0 :                                                 char *relt;
     857           0 :                                                 sql_rel *r = NULL;
     858             : 
     859           0 :                                                 if (!(relt = sa_strdup(sql->sa, tr->statement))) {
     860           0 :                                                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     861           0 :                                                         goto bailout;
     862             :                                                 }
     863             : 
     864           0 :                                                 r = rel_parse(sql, s, relt, m_deps);
     865           0 :                                                 if (r)
     866           0 :                                                         r = sql_processrelation(sql, r, 0, 0, 0, 0);
     867           0 :                                                 if (r) {
     868           0 :                                                         list *id_l = rel_dependencies(sql, r);
     869             : 
     870           0 :                                                         for (node *o = id_l->h ; o ; o = o->next) {
     871           0 :                                                                 sqlid next = ((sql_base*) o->data)->id;
     872           0 :                                                                 if (next != tr->base.id) {
     873           0 :                                                                         pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",",
     874             :                                                                                                         next, tr->base.id, (int) TRIGGER_DEPENDENCY);
     875           0 :                                                                         first = false;
     876           0 :                                                                         FLUSH_INSERTS_IF_BUFFERFILLED;
     877             :                                                                 }
     878             :                                                         }
     879             :                                                 }
     880             :                                         }
     881             :                         }
     882             :                 }
     883             :         }
     884             : 
     885           0 :         if (ppos != pos) { /* found updatable functions */
     886           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     887             :                                                 ") as t1(c1,c2,c3) where t1.c1 not in (select \"id\" from sys.dependencies where depend_id = t1.c2);\n");
     888             : 
     889           0 :                 assert(pos < bufsize);
     890           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
     891           0 :                 fflush(stdout);
     892           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     893             :         }
     894             : 
     895           0 : bailout:
     896           0 :         if (sql->sa)
     897           0 :                 sa_destroy(sql->sa);
     898           0 :         sql->sa = old_sa;
     899           0 :         GDKfree(buf);
     900           0 :         return err;
     901             : }
     902             : 
     903             : static str
     904           0 : sql_update_nov2019(Client c, mvc *sql)
     905             : {
     906           0 :         size_t bufsize = 16384, pos = 0;
     907           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     908             : 
     909           0 :         (void) sql;
     910           0 :         if (buf == NULL)
     911           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     912             : 
     913           0 :         pos += snprintf(buf + pos, bufsize - pos,
     914             :                         "create function sys.deltas (\"schema\" string)"
     915             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     916             :                         " external name \"sql\".\"deltas\";\n"
     917             :                         "create function sys.deltas (\"schema\" string, \"table\" string)"
     918             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     919             :                         " external name \"sql\".\"deltas\";\n"
     920             :                         "create function sys.deltas (\"schema\" string, \"table\" string, \"column\" string)"
     921             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     922             :                         " external name \"sql\".\"deltas\";\n"
     923             :                         "create aggregate median_avg(val TINYINT) returns DOUBLE\n"
     924             :                         " external name \"aggr\".\"median_avg\";\n"
     925             :                         "GRANT EXECUTE ON AGGREGATE median_avg(TINYINT) TO PUBLIC;\n"
     926             :                         "create aggregate median_avg(val SMALLINT) returns DOUBLE\n"
     927             :                         " external name \"aggr\".\"median_avg\";\n"
     928             :                         "GRANT EXECUTE ON AGGREGATE median_avg(SMALLINT) TO PUBLIC;\n"
     929             :                         "create aggregate median_avg(val INTEGER) returns DOUBLE\n"
     930             :                         " external name \"aggr\".\"median_avg\";\n"
     931             :                         "GRANT EXECUTE ON AGGREGATE median_avg(INTEGER) TO PUBLIC;\n"
     932             :                         "create aggregate median_avg(val BIGINT) returns DOUBLE\n"
     933             :                         " external name \"aggr\".\"median_avg\";\n"
     934             :                         "GRANT EXECUTE ON AGGREGATE median_avg(BIGINT) TO PUBLIC;\n"
     935             :                         "create aggregate median_avg(val DECIMAL) returns DOUBLE\n"
     936             :                         " external name \"aggr\".\"median_avg\";\n"
     937             :                         "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL) TO PUBLIC;\n"
     938             :                         "create aggregate median_avg(val REAL) returns DOUBLE\n"
     939             :                         " external name \"aggr\".\"median_avg\";\n"
     940             :                         "GRANT EXECUTE ON AGGREGATE median_avg(REAL) TO PUBLIC;\n"
     941             :                         "create aggregate median_avg(val DOUBLE) returns DOUBLE\n"
     942             :                         " external name \"aggr\".\"median_avg\";\n"
     943             :                         "GRANT EXECUTE ON AGGREGATE median_avg(DOUBLE) TO PUBLIC;\n"
     944             :                         "create aggregate quantile_avg(val TINYINT, q DOUBLE) returns DOUBLE\n"
     945             :                         " external name \"aggr\".\"quantile_avg\";\n"
     946             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(TINYINT, DOUBLE) TO PUBLIC;\n"
     947             :                         "create aggregate quantile_avg(val SMALLINT, q DOUBLE) returns DOUBLE\n"
     948             :                         " external name \"aggr\".\"quantile_avg\";\n"
     949             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(SMALLINT, DOUBLE) TO PUBLIC;\n"
     950             :                         "create aggregate quantile_avg(val INTEGER, q DOUBLE) returns DOUBLE\n"
     951             :                         " external name \"aggr\".\"quantile_avg\";\n"
     952             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(INTEGER, DOUBLE) TO PUBLIC;\n"
     953             :                         "create aggregate quantile_avg(val BIGINT, q DOUBLE) returns DOUBLE\n"
     954             :                         " external name \"aggr\".\"quantile_avg\";\n"
     955             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(BIGINT, DOUBLE) TO PUBLIC;\n"
     956             :                         "create aggregate quantile_avg(val DECIMAL, q DOUBLE) returns DOUBLE\n"
     957             :                         " external name \"aggr\".\"quantile_avg\";\n"
     958             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL, DOUBLE) TO PUBLIC;\n"
     959             :                         "create aggregate quantile_avg(val REAL, q DOUBLE) returns DOUBLE\n"
     960             :                         " external name \"aggr\".\"quantile_avg\";\n"
     961             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(REAL, DOUBLE) TO PUBLIC;\n"
     962             :                         "create aggregate quantile_avg(val DOUBLE, q DOUBLE) returns DOUBLE\n"
     963             :                         " external name \"aggr\".\"quantile_avg\";\n"
     964             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(DOUBLE, DOUBLE) TO PUBLIC;\n");
     965             : #ifdef HAVE_HGE
     966           0 :         pos += snprintf(buf + pos, bufsize - pos,
     967             :                                 "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
     968             :                                 " external name \"aggr\".\"median_avg\";\n"
     969             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
     970             :                                 "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
     971             :                                 " external name \"aggr\".\"quantile_avg\";\n"
     972             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n");
     973             : #endif
     974             :         /* 60/61_wlcr signatures migrations */
     975           0 :         pos += snprintf(buf + pos, bufsize - pos,
     976             :                         "drop procedure master() cascade;\n"
     977             :                         "drop procedure master(string) cascade;\n"
     978             :                         "drop procedure stopmaster() cascade;\n"
     979             :                         "drop procedure masterbeat(int) cascade;\n"
     980             :                         "drop function masterClock() cascade;\n"
     981             :                         "drop function masterTick() cascade;\n"
     982             :                         "drop procedure replicate() cascade;\n"
     983             :                         "drop procedure replicate(timestamp) cascade;\n"
     984             :                         "drop procedure replicate(string) cascade;\n"
     985             :                         "drop procedure replicate(string, timestamp) cascade;\n"
     986             :                         "drop procedure replicate(string, tinyint) cascade;\n"
     987             :                         "drop procedure replicate(string, smallint) cascade;\n"
     988             :                         "drop procedure replicate(string, integer) cascade;\n"
     989             :                         "drop procedure replicate(string, bigint) cascade;\n"
     990             :                         "drop procedure replicabeat(integer) cascade;\n"
     991             :                         "drop function replicaClock() cascade;\n"
     992             :                         "drop function replicaTick() cascade;\n"
     993             :                 );
     994             : 
     995           0 :         pos += snprintf(buf + pos, bufsize - pos,
     996             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     997             :                         " and name in ('deltas') and type = %d;\n", (int) F_UNION);
     998           0 :         pos += snprintf(buf + pos, bufsize - pos,
     999             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1000             :                         " and name in ('median_avg', 'quantile_avg') and type = %d;\n", (int) F_AGGR);
    1001             : 
    1002             :         /* 39_analytics.sql */
    1003           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1004             :                         "create aggregate stddev_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1005             :                         "external name \"aggr\".\"stdev\";\n"
    1006             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1007             :                         "create aggregate stddev_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1008             :                         "external name \"aggr\".\"stdev\";\n"
    1009             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1010             : 
    1011             :                         "create aggregate stddev_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1012             :                         "external name \"aggr\".\"stdevp\";\n"
    1013             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1014             :                         "create aggregate stddev_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1015             :                         "external name \"aggr\".\"stdevp\";\n"
    1016             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1017             : 
    1018             :                         "create aggregate var_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1019             :                         "external name \"aggr\".\"variance\";\n"
    1020             :                         "GRANT EXECUTE ON AGGREGATE var_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1021             :                         "create aggregate var_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1022             :                         "external name \"aggr\".\"variance\";\n"
    1023             :                         "GRANT EXECUTE ON AGGREGATE var_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1024             : 
    1025             :                         "create aggregate var_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1026             :                         "external name \"aggr\".\"variancep\";\n"
    1027             :                         "GRANT EXECUTE ON AGGREGATE var_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1028             :                         "create aggregate var_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1029             :                         "external name \"aggr\".\"variancep\";\n"
    1030             :                         "GRANT EXECUTE ON AGGREGATE var_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1031             : 
    1032             :                         "create aggregate median(val INTERVAL SECOND) returns INTERVAL SECOND\n"
    1033             :                         "external name \"aggr\".\"median\";\n"
    1034             :                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL SECOND) TO PUBLIC;\n"
    1035             :                         "create aggregate median(val INTERVAL MONTH) returns INTERVAL MONTH\n"
    1036             :                         "external name \"aggr\".\"median\";\n"
    1037             :                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL MONTH) TO PUBLIC;\n"
    1038             : 
    1039             :                         "create aggregate quantile(val INTERVAL SECOND, q DOUBLE) returns INTERVAL SECOND\n"
    1040             :                         "external name \"aggr\".\"quantile\";\n"
    1041             :                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL SECOND, DOUBLE) TO PUBLIC;\n"
    1042             :                         "create aggregate quantile(val INTERVAL MONTH, q DOUBLE) returns INTERVAL MONTH\n"
    1043             :                         "external name \"aggr\".\"quantile\";\n"
    1044             :                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL MONTH, DOUBLE) TO PUBLIC;\n"
    1045             :                 );
    1046             : 
    1047           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1048             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1049             :                         " and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'var_pop', 'median', 'quantile') and type = %d;\n", (int) F_AGGR);
    1050             : 
    1051             :         /* The MAL implementation of functions json.text(string) and json.text(int) do not exist */
    1052           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1053             :                         "drop function json.text(string) cascade;\n"
    1054             :                         "drop function json.text(int) cascade;\n");
    1055             : 
    1056             :         /* The first argument to copyfrom is a PTR type */
    1057           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1058             :                         "update sys.args set type = 'ptr' where"
    1059             :                         " 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);
    1060             : 
    1061           0 :         assert(pos < bufsize);
    1062             : 
    1063           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1064           0 :         fflush(stdout);
    1065           0 :         err = SQLstatementIntern(c, buf, "update", 1, 0, NULL);
    1066           0 :         GDKfree(buf);
    1067           0 :         return err;             /* usually MAL_SUCCEED */
    1068             : }
    1069             : 
    1070             : #ifdef HAVE_HGE
    1071             : static str
    1072           0 : sql_update_nov2019_sp1_hugeint(Client c, mvc *sql)
    1073             : {
    1074           0 :         size_t bufsize = 1024, pos = 0;
    1075           0 :         char *buf, *err;
    1076             : 
    1077           0 :         (void) sql;
    1078           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1079           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1080             : 
    1081             :         /* 39_analytics_hge.sql */
    1082           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1083             :                         "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
    1084             :                         " external name \"aggr\".\"median_avg\";\n"
    1085             :                         "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
    1086             :                         "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
    1087             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1088             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n");
    1089             : 
    1090           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1091             :                         "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);
    1092             : 
    1093           0 :         assert(pos < bufsize);
    1094             : 
    1095           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1096           0 :         fflush(stdout);
    1097           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1098           0 :         GDKfree(buf);
    1099           0 :         return err;             /* usually MAL_SUCCEED */
    1100             : }
    1101             : #endif
    1102             : 
    1103             : static str
    1104           0 : sql_update_jun2020(Client c, mvc *sql)
    1105             : {
    1106           0 :         sql_table *t;
    1107           0 :         size_t bufsize = 32768, pos = 0;
    1108           0 :         char *err = NULL, *buf = NULL;
    1109           0 :         sql_schema *sys = mvc_bind_schema(sql, "sys");
    1110             : 
    1111           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1112           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1113             : 
    1114             :         /* convert old PYTHON2 and PYTHON2_MAP to PYTHON and PYTHON_MAP
    1115             :          * see also function load_func() in store.c */
    1116           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1117             :                         "update sys.functions set language = language - 2 where language in (8, 9);\n");
    1118             : 
    1119           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1120             :                         "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");
    1121           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1122             :                         "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");
    1123             : 
    1124             :         /* 12_url */
    1125           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1126             :                         "drop function isaURL(url) cascade;\n"
    1127             :                         "CREATE function isaURL(theUrl string) RETURNS BOOL\n"
    1128             :                         " EXTERNAL NAME url.\"isaURL\";\n"
    1129             :                         "GRANT EXECUTE ON FUNCTION isaURL(string) TO PUBLIC;\n"
    1130             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1131             :                         " and name = 'isaurl' and type = %d;\n", (int) F_FUNC);
    1132             : 
    1133             :         /* 13_date.sql */
    1134           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1135             :                         "drop function str_to_time(string, string) cascade;\n"
    1136             :                         "drop function time_to_str(time, string) cascade;\n"
    1137             :                         "drop function str_to_timestamp(string, string) cascade;\n"
    1138             :                         "drop function timestamp_to_str(timestamp, string) cascade;\n"
    1139             :                         "create function str_to_time(s string, format string) returns time with time zone\n"
    1140             :                         " external name mtime.\"str_to_time\";\n"
    1141             :                         "create function time_to_str(d time with time zone, format string) returns string\n"
    1142             :                         " external name mtime.\"time_to_str\";\n"
    1143             :                         "create function str_to_timestamp(s string, format string) returns timestamp with time zone\n"
    1144             :                         " external name mtime.\"str_to_timestamp\";\n"
    1145             :                         "create function timestamp_to_str(d timestamp with time zone, format string) returns string\n"
    1146             :                         " external name mtime.\"timestamp_to_str\";\n"
    1147             :                         "grant execute on function str_to_time to public;\n"
    1148             :                         "grant execute on function time_to_str to public;\n"
    1149             :                         "grant execute on function str_to_timestamp to public;\n"
    1150             :                         "grant execute on function timestamp_to_str to public;\n"
    1151             :                         "update sys.functions set system = true where system <> true and name in"
    1152             :                         " ('str_to_time', 'str_to_timestamp', 'time_to_str', 'timestamp_to_str')"
    1153             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_FUNC);
    1154             : 
    1155             :         /* 16_tracelog */
    1156           0 :         t = mvc_bind_table(sql, sys, "tracelog");
    1157           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1158           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1159             :                         "drop view sys.tracelog cascade;\n"
    1160             :                         "drop function sys.tracelog() cascade;\n"
    1161             :                         "create function sys.tracelog()\n"
    1162             :                         " returns table (\n"
    1163             :                         "  ticks bigint, -- time in microseconds\n"
    1164             :                         "  stmt string  -- actual statement executed\n"
    1165             :                         " )\n"
    1166             :                         " external name sql.dump_trace;\n"
    1167             :                         "create view sys.tracelog as select * from sys.tracelog();\n"
    1168             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1169             :                         " and name = 'tracelog' and type = %d;\n", (int) F_UNION);
    1170           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1171             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1172             :                         " and name = 'tracelog';\n");
    1173             : 
    1174             :         /* 17_temporal.sql */
    1175           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1176             :                         "drop function sys.epoch(bigint) cascade;\n"
    1177             :                         "drop function sys.epoch(int) cascade;\n"
    1178             :                         "drop function sys.epoch(timestamp) cascade;\n"
    1179             :                         "drop function sys.epoch(timestamp with time zone) cascade;\n"
    1180             :                         "create function sys.epoch(sec BIGINT) returns TIMESTAMP WITH TIME ZONE\n"
    1181             :                         " external name mtime.epoch;\n"
    1182             :                         "create function sys.epoch(sec INT) returns TIMESTAMP WITH TIME ZONE\n"
    1183             :                         " external name mtime.epoch;\n"
    1184             :                         "create function sys.epoch(ts TIMESTAMP WITH TIME ZONE) returns INT\n"
    1185             :                         " external name mtime.epoch;\n"
    1186             :                         "create function sys.date_trunc(txt string, t timestamp with time zone)\n"
    1187             :                         "returns timestamp with time zone\n"
    1188             :                         "external name sql.date_trunc;\n"
    1189             :                         "grant execute on function sys.date_trunc(string, timestamp with time zone) to public;\n"
    1190             :                         "grant execute on function sys.epoch (BIGINT) to public;\n"
    1191             :                         "grant execute on function sys.epoch (INT) to public;\n"
    1192             :                         "grant execute on function sys.epoch (TIMESTAMP WITH TIME ZONE) to public;\n"
    1193             :                         "update sys.functions set system = true where system <> true and name in"
    1194             :                         " ('epoch', 'date_trunc')"
    1195             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_FUNC);
    1196             : 
    1197             :         /* 22_clients */
    1198           0 :         t = mvc_bind_table(sql, sys, "sessions");
    1199           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1200             : 
    1201           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1202             :                         "drop view sys.sessions cascade;\n"
    1203             :                         "drop function sys.sessions cascade;\n"
    1204             :                         "create function sys.sessions()\n"
    1205             :                         "returns table(\n"
    1206             :                                 "\"sessionid\" int,\n"
    1207             :                                 "\"username\" string,\n"
    1208             :                                 "\"login\" timestamp,\n"
    1209             :                                 "\"idle\" timestamp,\n"
    1210             :                                 "\"optimizer\" string,\n"
    1211             :                                 "\"sessiontimeout\" int,\n"
    1212             :                                 "\"querytimeout\" int,\n"
    1213             :                                 "\"workerlimit\" int,\n"
    1214             :                                 "\"memorylimit\" int)\n"
    1215             :                         " external name sql.sessions;\n"
    1216             :                         "create view sys.sessions as select * from sys.sessions();\n");
    1217             : 
    1218           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1219             :                         "grant execute on procedure sys.settimeout(bigint) to public;\n"
    1220             :                         "grant execute on procedure sys.settimeout(bigint,bigint) to public;\n"
    1221             :                         "grant execute on procedure sys.setsession(bigint) to public;\n");
    1222             : 
    1223           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1224             :                         "create procedure sys.setoptimizer(\"optimizer\" string)\n"
    1225             :                         " external name clients.setoptimizer;\n"
    1226             :                         "grant execute on procedure sys.setoptimizer(string) to public;\n"
    1227             :                         "create procedure sys.setquerytimeout(\"query\" int)\n"
    1228             :                         " external name clients.setquerytimeout;\n"
    1229             :                         "grant execute on procedure sys.setquerytimeout(int) to public;\n"
    1230             :                         "create procedure sys.setsessiontimeout(\"timeout\" int)\n"
    1231             :                         " external name clients.setsessiontimeout;\n"
    1232             :                         "grant execute on procedure sys.setsessiontimeout(int) to public;\n"
    1233             :                         "create procedure sys.setworkerlimit(\"limit\" int)\n"
    1234             :                         " external name clients.setworkerlimit;\n"
    1235             :                         "grant execute on procedure sys.setworkerlimit(int) to public;\n"
    1236             :                         "create procedure sys.setmemorylimit(\"limit\" int)\n"
    1237             :                         " external name clients.setmemorylimit;\n"
    1238             :                         "grant execute on procedure sys.setmemorylimit(int) to public;\n"
    1239             :                         "create procedure sys.setoptimizer(\"sessionid\" int, \"optimizer\" string)\n"
    1240             :                         " external name clients.setoptimizer;\n"
    1241             :                         "create procedure sys.setquerytimeout(\"sessionid\" int, \"query\" int)\n"
    1242             :                         " external name clients.setquerytimeout;\n"
    1243             :                         "create procedure sys.setsessiontimeout(\"sessionid\" int, \"query\" int)\n"
    1244             :                         " external name clients.setsessiontimeout;\n"
    1245             :                         "create procedure sys.setworkerlimit(\"sessionid\" int, \"limit\" int)\n"
    1246             :                         " external name clients.setworkerlimit;\n"
    1247             :                         "create procedure sys.setmemorylimit(\"sessionid\" int, \"limit\" int)\n"
    1248             :                         " external name clients.setmemorylimit;\n"
    1249             :                         "create procedure sys.stopsession(\"sessionid\" int)\n"
    1250             :                         " external name clients.stopsession;\n");
    1251             : 
    1252           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1253             :                         "create function sys.prepared_statements()\n"
    1254             :                         "returns table(\n"
    1255             :                         "\"sessionid\" int,\n"
    1256             :                         "\"username\" string,\n"
    1257             :                         "\"statementid\" int,\n"
    1258             :                         "\"statement\" string,\n"
    1259             :                         "\"created\" timestamp)\n"
    1260             :                         " external name sql.prepared_statements;\n"
    1261             :                         "grant execute on function sys.prepared_statements to public;\n"
    1262             :                         "create view sys.prepared_statements as select * from sys.prepared_statements();\n"
    1263             :                         "grant select on sys.prepared_statements to public;\n"
    1264             :                         "create function sys.prepared_statements_args()\n"
    1265             :                         "returns table(\n"
    1266             :                         "\"statementid\" int,\n"
    1267             :                         "\"type\" string,\n"
    1268             :                         "\"type_digits\" int,\n"
    1269             :                         "\"type_scale\" int,\n"
    1270             :                         "\"inout\" tinyint,\n"
    1271             :                         "\"number\" int,\n"
    1272             :                         "\"schema\" string,\n"
    1273             :                         "\"table\" string,\n"
    1274             :                         "\"column\" string)\n"
    1275             :                         " external name sql.prepared_statements_args;\n"
    1276             :                         "grant execute on function sys.prepared_statements_args to public;\n"
    1277             :                         "create view sys.prepared_statements_args as select * from sys.prepared_statements_args();\n"
    1278             :                         "grant select on sys.prepared_statements_args to public;\n");
    1279             : 
    1280           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1281             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1282             :                         " and name in ('sessions', 'prepared_statements', 'prepared_statements_args') and type = %d;\n", (int) F_UNION);
    1283           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1284             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1285             :                         " and name in ('sessions', 'prepared_statements', 'prepared_statements_args');\n");
    1286           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1287             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1288             :                         " and name in ('setoptimizer', 'setquerytimeout', 'setsessiontimeout', 'setworkerlimit', 'setmemorylimit', 'setoptimizer', 'stopsession') and type = %d;\n", (int) F_PROC);
    1289             : 
    1290             :         /* 25_debug */
    1291           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1292             :                         "create procedure sys.suspend_log_flushing()\n"
    1293             :                         " external name sql.suspend_log_flushing;\n"
    1294             :                         "create procedure sys.resume_log_flushing()\n"
    1295             :                         " external name sql.resume_log_flushing;\n"
    1296             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1297             :                         " and name in ('suspend_log_flushing', 'resume_log_flushing') and type = %d;\n", (int) F_PROC);
    1298             : 
    1299           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1300             :                         "create function sys.debug(flag string) returns integer\n"
    1301             :                         " external name mdb.\"setDebug\";\n"
    1302             :                         "create function sys.debugflags()\n"
    1303             :                         " returns table(flag string, val bool)\n"
    1304             :                         " external name mdb.\"getDebugFlags\";\n");
    1305           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1306             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1307             :                         " and name in ('debug') and type = %d;\n"
    1308             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1309             :                         " and name in ('debugflags') and type = %d;\n",
    1310             :                         (int) F_FUNC, (int) F_UNION);
    1311             : 
    1312             :         /* 26_sysmon */
    1313           0 :         t = mvc_bind_table(sql, sys, "queue");
    1314           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1315             : 
    1316           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1317             :                         "drop view sys.queue cascade;\n"
    1318             :                         "drop function sys.queue cascade;\n"
    1319             :                         "create function sys.queue()\n"
    1320             :                         "returns table(\n"
    1321             :                         "\"tag\" bigint,\n"
    1322             :                         "\"sessionid\" int,\n"
    1323             :                         "\"username\" string,\n"
    1324             :                         "\"started\" timestamp,\n"
    1325             :                         "\"status\" string,\n"
    1326             :                         "\"query\" string,\n"
    1327             :                         "\"progress\" int,\n"
    1328             :                         "\"maxworkers\" int,\n"
    1329             :                         "\"footprint\" int)\n"
    1330             :                         " external name sysmon.queue;\n"
    1331             :                         "grant execute on function sys.queue to public;\n"
    1332             :                         "create view sys.queue as select * from sys.queue();\n"
    1333             :                         "grant select on sys.queue to public;\n"
    1334             : 
    1335             :                         "drop procedure sys.pause(int) cascade;\n"
    1336             :                         "drop procedure sys.resume(int) cascade;\n"
    1337             :                         "drop procedure sys.stop(int) cascade;\n"
    1338             : 
    1339             :                         "grant execute on procedure sys.pause(bigint) to public;\n"
    1340             :                         "grant execute on procedure sys.resume(bigint) to public;\n"
    1341             :                         "grant execute on procedure sys.stop(bigint) to public;\n");
    1342             : 
    1343           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1344             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1345             :                         " and name = 'queue' and type = %d;\n", (int) F_UNION);
    1346           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1347             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1348             :                         " and name = 'queue';\n");
    1349             : 
    1350             :         /* 39_analytics.sql */
    1351           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1352             :                         "create window stddev_samp(val TINYINT) returns DOUBLE\n"
    1353             :                         " external name \"sql\".\"stdev\";\n"
    1354             :                         "GRANT EXECUTE ON WINDOW stddev_samp(TINYINT) TO PUBLIC;\n"
    1355             :                         "create window stddev_samp(val SMALLINT) returns DOUBLE"
    1356             :                         " external name \"sql\".\"stdev\";\n"
    1357             :                         "GRANT EXECUTE ON WINDOW stddev_samp(SMALLINT) TO PUBLIC;\n"
    1358             :                         "create window stddev_samp(val INTEGER) returns DOUBLE\n"
    1359             :                         " external name \"sql\".\"stdev\";\n"
    1360             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTEGER) TO PUBLIC;\n"
    1361             :                         "create window stddev_samp(val BIGINT) returns DOUBLE\n"
    1362             :                         " external name \"sql\".\"stdev\";\n"
    1363             :                         "GRANT EXECUTE ON WINDOW stddev_samp(BIGINT) TO PUBLIC;\n"
    1364             :                         "create window stddev_samp(val REAL) returns DOUBLE\n"
    1365             :                         " external name \"sql\".\"stdev\";\n"
    1366             :                         "GRANT EXECUTE ON WINDOW stddev_samp(REAL) TO PUBLIC;\n"
    1367             :                         "create window stddev_samp(val DOUBLE) returns DOUBLE\n"
    1368             :                         " external name \"sql\".\"stdev\";\n"
    1369             :                         "GRANT EXECUTE ON WINDOW stddev_samp(DOUBLE) TO PUBLIC;\n"
    1370             :                         "create window stddev_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1371             :                         " external name \"sql\".\"stdev\";\n"
    1372             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1373             :                         "create window stddev_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1374             :                         " external name \"sql\".\"stdev\";\n"
    1375             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1376             :                         "create window stddev_pop(val TINYINT) returns DOUBLE\n"
    1377             :                         " external name \"sql\".\"stdevp\";\n"
    1378             :                         "GRANT EXECUTE ON WINDOW stddev_pop(TINYINT) TO PUBLIC;\n"
    1379             :                         "create window stddev_pop(val SMALLINT) returns DOUBLE\n"
    1380             :                         " external name \"sql\".\"stdevp\";\n"
    1381             :                         "GRANT EXECUTE ON WINDOW stddev_pop(SMALLINT) TO PUBLIC;\n"
    1382             :                         "create window stddev_pop(val INTEGER) returns DOUBLE\n"
    1383             :                         " external name \"sql\".\"stdevp\";\n"
    1384             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTEGER) TO PUBLIC;\n"
    1385             :                         "create window stddev_pop(val BIGINT) returns DOUBLE\n"
    1386             :                         " external name \"sql\".\"stdevp\";\n"
    1387             :                         "GRANT EXECUTE ON WINDOW stddev_pop(BIGINT) TO PUBLIC;\n"
    1388             :                         "create window stddev_pop(val REAL) returns DOUBLE\n"
    1389             :                         " external name \"sql\".\"stdevp\";\n"
    1390             :                         "GRANT EXECUTE ON WINDOW stddev_pop(REAL) TO PUBLIC;\n"
    1391             :                         "create window stddev_pop(val DOUBLE) returns DOUBLE\n"
    1392             :                         " external name \"sql\".\"stdevp\";\n"
    1393             :                         "GRANT EXECUTE ON WINDOW stddev_pop(DOUBLE) TO PUBLIC;\n"
    1394             :                         "create window stddev_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1395             :                         " external name \"sql\".\"stdevp\";\n"
    1396             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1397             :                         "create window stddev_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1398             :                         " external name \"sql\".\"stdevp\";\n"
    1399             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1400             :                         "create window var_samp(val TINYINT) returns DOUBLE\n"
    1401             :                         " external name \"sql\".\"variance\";\n"
    1402             :                         "GRANT EXECUTE ON WINDOW var_samp(TINYINT) TO PUBLIC;\n"
    1403             :                         "create window var_samp(val SMALLINT) returns DOUBLE\n"
    1404             :                         " external name \"sql\".\"variance\";\n"
    1405             :                         "GRANT EXECUTE ON WINDOW var_samp(SMALLINT) TO PUBLIC;\n"
    1406             :                         "create window var_samp(val INTEGER) returns DOUBLE\n"
    1407             :                         " external name \"sql\".\"variance\";\n"
    1408             :                         "GRANT EXECUTE ON WINDOW var_samp(INTEGER) TO PUBLIC;\n"
    1409             :                         "create window var_samp(val BIGINT) returns DOUBLE\n"
    1410             :                         " external name \"sql\".\"variance\";\n"
    1411             :                         "GRANT EXECUTE ON WINDOW var_samp(BIGINT) TO PUBLIC;\n"
    1412             :                         "create window var_samp(val REAL) returns DOUBLE\n"
    1413             :                         " external name \"sql\".\"variance\";\n"
    1414             :                         "GRANT EXECUTE ON WINDOW var_samp(REAL) TO PUBLIC;\n"
    1415             :                         "create window var_samp(val DOUBLE) returns DOUBLE\n"
    1416             :                         " external name \"sql\".\"variance\";\n"
    1417             :                         "GRANT EXECUTE ON WINDOW var_samp(DOUBLE) TO PUBLIC;\n"
    1418             :                         "create window var_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1419             :                         " external name \"sql\".\"variance\";\n"
    1420             :                         "GRANT EXECUTE ON WINDOW var_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1421             :                         "create window var_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1422             :                         " external name \"sql\".\"variance\";\n"
    1423             :                         "GRANT EXECUTE ON WINDOW var_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1424             :                         "create window var_pop(val TINYINT) returns DOUBLE\n"
    1425             :                         " external name \"sql\".\"variancep\";\n"
    1426             :                         "GRANT EXECUTE ON WINDOW var_pop(TINYINT) TO PUBLIC;\n"
    1427             :                         "create window var_pop(val SMALLINT) returns DOUBLE\n"
    1428             :                         " external name \"sql\".\"variancep\";\n"
    1429             :                         "GRANT EXECUTE ON WINDOW var_pop(SMALLINT) TO PUBLIC;\n"
    1430             :                         "create window var_pop(val INTEGER) returns DOUBLE\n"
    1431             :                         " external name \"sql\".\"variancep\";\n"
    1432             :                         "GRANT EXECUTE ON WINDOW var_pop(INTEGER) TO PUBLIC;\n"
    1433             :                         "create window var_pop(val BIGINT) returns DOUBLE\n"
    1434             :                         " external name \"sql\".\"variancep\";\n"
    1435             :                         "GRANT EXECUTE ON WINDOW var_pop(BIGINT) TO PUBLIC;\n"
    1436             :                         "create window var_pop(val REAL) returns DOUBLE\n"
    1437             :                         " external name \"sql\".\"variancep\";\n"
    1438             :                         "GRANT EXECUTE ON WINDOW var_pop(REAL) TO PUBLIC;\n"
    1439             :                         "create window var_pop(val DOUBLE) returns DOUBLE\n"
    1440             :                         " external name \"sql\".\"variancep\";\n"
    1441             :                         "GRANT EXECUTE ON WINDOW var_pop(DOUBLE) TO PUBLIC;\n"
    1442             :                         "create window var_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1443             :                         " external name \"sql\".\"variancep\";\n"
    1444             :                         "GRANT EXECUTE ON WINDOW var_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1445             :                         "create window var_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1446             :                         " external name \"sql\".\"variancep\";\n"
    1447             :                         "GRANT EXECUTE ON WINDOW var_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1448             :                         "create aggregate covar_samp(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1449             :                         " external name \"aggr\".\"covariance\";\n"
    1450             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(TINYINT, TINYINT) TO PUBLIC;\n"
    1451             :                         "create aggregate covar_samp(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1452             :                         " external name \"aggr\".\"covariance\";\n"
    1453             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1454             :                         "create aggregate covar_samp(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1455             :                         " external name \"aggr\".\"covariance\";\n"
    1456             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTEGER, INTEGER) TO PUBLIC;\n"
    1457             :                         "create aggregate covar_samp(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1458             :                         " external name \"aggr\".\"covariance\";\n"
    1459             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(BIGINT, BIGINT) TO PUBLIC;\n"
    1460             :                         "create aggregate covar_samp(e1 REAL, e2 REAL) returns DOUBLE\n"
    1461             :                         " external name \"aggr\".\"covariance\";\n"
    1462             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(REAL, REAL) TO PUBLIC;\n"
    1463             :                         "create aggregate covar_samp(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1464             :                         " external name \"aggr\".\"covariance\";\n"
    1465             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1466             :                         "create aggregate covar_samp(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1467             :                         " external name \"aggr\".\"covariance\";\n"
    1468             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1469             :                         "create aggregate covar_samp(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1470             :                         " external name \"aggr\".\"covariance\";\n"
    1471             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1472             :                         "create window covar_samp(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1473             :                         " external name \"sql\".\"covariance\";\n"
    1474             :                         "GRANT EXECUTE ON WINDOW covar_samp(TINYINT, TINYINT) TO PUBLIC;\n"
    1475             :                         "create window covar_samp(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1476             :                         " external name \"sql\".\"covariance\";\n"
    1477             :                         "GRANT EXECUTE ON WINDOW covar_samp(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1478             :                         "create window covar_samp(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1479             :                         " external name \"sql\".\"covariance\";\n"
    1480             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTEGER, INTEGER) TO PUBLIC;\n"
    1481             :                         "create window covar_samp(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1482             :                         " external name \"sql\".\"covariance\";\n"
    1483             :                         "GRANT EXECUTE ON WINDOW covar_samp(BIGINT, BIGINT) TO PUBLIC;\n"
    1484             :                         "create window covar_samp(e1 REAL, e2 REAL) returns DOUBLE\n"
    1485             :                         " external name \"sql\".\"covariance\";\n"
    1486             :                         "GRANT EXECUTE ON WINDOW covar_samp(REAL, REAL) TO PUBLIC;\n"
    1487             :                         "create window covar_samp(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1488             :                         " external name \"sql\".\"covariance\";\n"
    1489             :                         "GRANT EXECUTE ON WINDOW covar_samp(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1490             :                         "create window covar_samp(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1491             :                         " external name \"sql\".\"covariance\";\n"
    1492             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1493             :                         "create window covar_samp(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1494             :                         " external name \"sql\".\"covariance\";\n"
    1495             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1496             :                         "create aggregate covar_pop(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1497             :                         " external name \"aggr\".\"covariancep\";\n"
    1498             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(TINYINT, TINYINT) TO PUBLIC;\n"
    1499             :                         "create aggregate covar_pop(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1500             :                         " external name \"aggr\".\"covariancep\";\n"
    1501             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1502             :                         "create aggregate covar_pop(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1503             :                         " external name \"aggr\".\"covariancep\";\n"
    1504             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTEGER, INTEGER) TO PUBLIC;\n"
    1505             :                         "create aggregate covar_pop(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1506             :                         " external name \"aggr\".\"covariancep\";\n"
    1507             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(BIGINT, BIGINT) TO PUBLIC;\n"
    1508             :                         "create aggregate covar_pop(e1 REAL, e2 REAL) returns DOUBLE\n"
    1509             :                         " external name \"aggr\".\"covariancep\";\n"
    1510             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(REAL, REAL) TO PUBLIC;\n"
    1511             :                         "create aggregate covar_pop(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1512             :                         " external name \"aggr\".\"covariancep\";\n"
    1513             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1514             :                         "create aggregate covar_pop(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1515             :                         " external name \"aggr\".\"covariancep\";\n"
    1516             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1517             :                         "create aggregate covar_pop(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1518             :                         " external name \"aggr\".\"covariancep\";\n"
    1519             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1520             :                         "create window covar_pop(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1521             :                         " external name \"sql\".\"covariancep\";\n"
    1522             :                         "GRANT EXECUTE ON WINDOW covar_pop(TINYINT, TINYINT) TO PUBLIC;\n"
    1523             :                         "create window covar_pop(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1524             :                         " external name \"sql\".\"covariancep\";\n"
    1525             :                         "GRANT EXECUTE ON WINDOW covar_pop(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1526             :                         "create window covar_pop(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1527             :                         " external name \"sql\".\"covariancep\";\n"
    1528             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTEGER, INTEGER) TO PUBLIC;\n"
    1529             :                         "create window covar_pop(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1530             :                         " external name \"sql\".\"covariancep\";\n"
    1531             :                         "GRANT EXECUTE ON WINDOW covar_pop(BIGINT, BIGINT) TO PUBLIC;\n"
    1532             :                         "create window covar_pop(e1 REAL, e2 REAL) returns DOUBLE\n"
    1533             :                         " external name \"sql\".\"covariancep\";\n"
    1534             :                         "GRANT EXECUTE ON WINDOW covar_pop(REAL, REAL) TO PUBLIC;\n"
    1535             :                         "create window covar_pop(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1536             :                         " external name \"sql\".\"covariancep\";\n"
    1537             :                         "GRANT EXECUTE ON WINDOW covar_pop(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1538             :                         "create window covar_pop(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1539             :                         " external name \"sql\".\"covariancep\";\n"
    1540             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1541             :                         "create window covar_pop(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1542             :                         " external name \"sql\".\"covariancep\";\n"
    1543             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1544             :                         "create aggregate corr(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1545             :                         " external name \"aggr\".\"corr\";\n"
    1546             :                         "GRANT EXECUTE ON AGGREGATE corr(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1547             :                         "create aggregate corr(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1548             :                         " external name \"aggr\".\"corr\";\n"
    1549             :                         "GRANT EXECUTE ON AGGREGATE corr(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1550             :                         "create window corr(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1551             :                         " external name \"sql\".\"corr\";\n"
    1552             :                         "GRANT EXECUTE ON WINDOW corr(TINYINT, TINYINT) TO PUBLIC;\n"
    1553             :                         "create window corr(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1554             :                         " external name \"sql\".\"corr\";\n"
    1555             :                         "GRANT EXECUTE ON WINDOW corr(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1556             :                         "create window corr(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1557             :                         " external name \"sql\".\"corr\";\n"
    1558             :                         "GRANT EXECUTE ON WINDOW corr(INTEGER, INTEGER) TO PUBLIC;\n"
    1559             :                         "create window corr(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1560             :                         " external name \"sql\".\"corr\";\n"
    1561             :                         "GRANT EXECUTE ON WINDOW corr(BIGINT, BIGINT) TO PUBLIC;\n"
    1562             :                         "create window corr(e1 REAL, e2 REAL) returns DOUBLE\n"
    1563             :                         " external name \"sql\".\"corr\";\n"
    1564             :                         "GRANT EXECUTE ON WINDOW corr(REAL, REAL) TO PUBLIC;\n"
    1565             :                         "create window corr(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1566             :                         " external name \"sql\".\"corr\";\n"
    1567             :                         "GRANT EXECUTE ON WINDOW corr(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1568             :                         "create window corr(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1569             :                         " external name \"sql\".\"corr\";\n"
    1570             :                         "GRANT EXECUTE ON WINDOW corr(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1571             :                         "create window corr(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1572             :                         " external name \"sql\".\"corr\";\n"
    1573             :                         "GRANT EXECUTE ON WINDOW corr(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n");
    1574             : 
    1575           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1576             :                 "create window sys.group_concat(str STRING) returns STRING\n"
    1577             :                 " external name \"sql\".\"str_group_concat\";\n"
    1578             :                 "GRANT EXECUTE ON WINDOW sys.group_concat(STRING) TO PUBLIC;\n"
    1579             :                 "create window sys.group_concat(str STRING, sep STRING) returns STRING\n"
    1580             :                 " external name \"sql\".\"str_group_concat\";\n"
    1581             :                 "GRANT EXECUTE ON WINDOW sys.group_concat(STRING, STRING) TO PUBLIC;\n");
    1582             : 
    1583           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1584             :                         "update sys.functions set system = true where system <> true and name in"
    1585             :                         " ('stddev_samp', 'stddev_pop', 'var_samp', 'var_pop', 'covar_samp', 'covar_pop', 'corr', 'group_concat')"
    1586             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type in (%d, %d);\n", (int) F_ANALYTIC, (int) F_AGGR);
    1587             : 
    1588           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1589             :                         "DROP AGGREGATE stddev_samp(date) CASCADE;\n"
    1590             :                         "DROP AGGREGATE stddev_samp(time) CASCADE;\n"
    1591             :                         "DROP AGGREGATE stddev_samp(timestamp) CASCADE;\n"
    1592             :                         "DROP AGGREGATE stddev_pop(date) CASCADE;\n"
    1593             :                         "DROP AGGREGATE stddev_pop(time) CASCADE;\n"
    1594             :                         "DROP AGGREGATE stddev_pop(timestamp) CASCADE;\n"
    1595             :                         "DROP AGGREGATE var_samp(date) CASCADE;\n"
    1596             :                         "DROP AGGREGATE var_samp(time) CASCADE;\n"
    1597             :                         "DROP AGGREGATE var_samp(timestamp) CASCADE;\n"
    1598             :                         "DROP AGGREGATE var_pop(date) CASCADE;\n"
    1599             :                         "DROP AGGREGATE var_pop(time) CASCADE;\n"
    1600             :                         "DROP AGGREGATE var_pop(timestamp) CASCADE;\n");
    1601             : 
    1602             :         /* 51_sys_schema_extensions */
    1603           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1604             :                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    1605             :                         "DELETE FROM sys.keywords where keyword IN ('NOCYCLE','NOMAXVALUE','NOMINVALUE');\n"
    1606             :                         "insert into sys.keywords values ('ANALYZE'),('AT'),('AUTHORIZATION'),('CACHE'),('CENTURY'),('COLUMN'),('CLIENT'),"
    1607             :                         "('CUBE'),('CYCLE'),('DATA'),('DATE'),('DEBUG'),('DECADE'),('DEALLOCATE'),('DIAGNOSTICS'),('DISTINCT'),"
    1608             :                         "('DOW'),('DOY'),('EXEC'),('EXECUTE'),('EXPLAIN'),('FIRST'),('FWF'),('GROUPING'),('GROUPS'),('INCREMENT'),"
    1609             :                         "('INTERVAL'),('KEY'),('LANGUAGE'),('LARGE'),('LAST'),('LATERAL'),('LEVEL'),('LOADER'),('MATCH'),('MATCHED'),('MAXVALUE'),"
    1610             :                         "('MINVALUE'),('NAME'),('NO'),('NULLS'),('OBJECT'),('OPTIONS'),('PASSWORD'),('PLAN'),('PRECISION'),('PREP'),('PREPARE'),"
    1611             :                         "('QUARTER'),('RELEASE'),('REPLACE'),('ROLLUP'),('SCHEMA'),('SEED'),('SERVER'),('SESSION'),('SETS'),('SIZE'),"
    1612             :                         "('STATEMENT'),('TABLE'),('TEMP'),('TEMPORARY'),('TEXT'),('TIME'),('TIMESTAMP'),('TRACE'),('TYPE'),"
    1613             :                         "('WEEK'),('YEAR'),('ZONE');\n");
    1614           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1615             :                         "ALTER TABLE sys.function_languages SET READ WRITE;\n"
    1616             :                         "DELETE FROM sys.function_languages where language_keyword IN ('PYTHON2','PYTHON2_MAP');\n");
    1617             : 
    1618             :         /* 58_hot_snapshot */
    1619           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1620             :                         "create procedure sys.hot_snapshot(tarfile string)\n"
    1621             :                         " external name sql.hot_snapshot;\n"
    1622             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1623             :                         " and name in ('hot_snapshot') and type = %d;\n", (int) F_PROC);
    1624             : 
    1625             :         /* 81_tracer.sql */
    1626           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1627             :                         "CREATE SCHEMA logging;\n"
    1628             :                         "CREATE PROCEDURE logging.flush()\n"
    1629             :                         " EXTERNAL NAME logging.flush;\n"
    1630             :                         "CREATE PROCEDURE logging.setcomplevel(comp_id STRING, lvl_id STRING)\n"
    1631             :                         " EXTERNAL NAME logging.setcomplevel;\n"
    1632             :                         "CREATE PROCEDURE logging.resetcomplevel(comp_id STRING)\n"
    1633             :                         " EXTERNAL NAME logging.resetcomplevel;\n"
    1634             :                         "CREATE PROCEDURE logging.setlayerlevel(layer_id STRING, lvl_id STRING)\n"
    1635             :                         " EXTERNAL NAME logging.setlayerlevel;\n"
    1636             :                         "CREATE PROCEDURE logging.resetlayerlevel(layer_id STRING)\n"
    1637             :                         " EXTERNAL NAME logging.resetlayerlevel;\n"
    1638             :                         "CREATE PROCEDURE logging.setflushlevel(lvl_id STRING)\n"
    1639             :                         " EXTERNAL NAME logging.setflushlevel;\n"
    1640             :                         "CREATE PROCEDURE logging.resetflushlevel()\n"
    1641             :                         " EXTERNAL NAME logging.resetflushlevel;\n"
    1642             :                         "CREATE PROCEDURE logging.setadapter(adapter_id STRING)\n"
    1643             :                         " EXTERNAL NAME logging.setadapter;\n"
    1644             :                         "CREATE PROCEDURE logging.resetadapter()\n"
    1645             :                         " EXTERNAL NAME logging.resetadapter;\n"
    1646             :                         "CREATE FUNCTION logging.compinfo()\n"
    1647             :                         "RETURNS TABLE(\n"
    1648             :                         " \"id\" int,\n"
    1649             :                         " \"component\" string,\n"
    1650             :                         " \"log_level\" string\n"
    1651             :                         ")\n"
    1652             :                         "EXTERNAL NAME logging.compinfo;\n"
    1653             :                         "GRANT EXECUTE ON FUNCTION logging.compinfo TO public;\n"
    1654             :                         "CREATE view logging.compinfo AS SELECT * FROM logging.compinfo();\n"
    1655             :                         "GRANT SELECT ON logging.compinfo TO public;\n");
    1656           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1657             :                         "update sys.schemas set system = true where name = 'logging';\n"
    1658             :                         "update sys.functions set system = true where system <> true and name in"
    1659             :                         " ('flush', 'setcomplevel', 'resetcomplevel', 'setlayerlevel', 'resetlayerlevel', 'setflushlevel', 'resetflushlevel', 'setadapter', 'resetadapter')"
    1660             :                         " and schema_id = (select id from sys.schemas where name = 'logging') and type = %d;\n"
    1661             :                         "update sys.functions set system = true where system <> true and name in"
    1662             :                         " ('compinfo')"
    1663             :                         " and schema_id = (select id from sys.schemas where name = 'logging') and type = %d;\n"
    1664             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'logging')"
    1665             :                         " and name = 'compinfo';\n",
    1666             :                         (int) F_PROC, (int) F_UNION);
    1667             : 
    1668           0 :         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    1669           0 :         assert(pos < bufsize);
    1670             : 
    1671           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1672           0 :         fflush(stdout);
    1673           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1674           0 :         if (err == MAL_SUCCEED) {
    1675           0 :                 pos = snprintf(buf, bufsize,
    1676             :                                "ALTER TABLE sys.keywords SET READ ONLY;\n"
    1677             :                                "ALTER TABLE sys.function_languages SET READ ONLY;\n");
    1678           0 :                 assert(pos < bufsize);
    1679           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    1680           0 :                 fflush(stdout);
    1681           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1682             :         }
    1683           0 :         GDKfree(buf);
    1684           0 :         return err;             /* usually MAL_SUCCEED */
    1685             : }
    1686             : 
    1687             : static str
    1688          99 : sql_update_jun2020_bam(Client c, mvc *m)
    1689             : {
    1690          99 :         size_t bufsize = 10240, pos = 0;
    1691          99 :         char *err = NULL, *buf;
    1692          99 :         res_table *output;
    1693          99 :         BAT *b;
    1694          99 :         sql_schema *s = mvc_bind_schema(m, "bam");
    1695          99 :         sql_table *t;
    1696             : 
    1697          99 :         if (s == NULL || !s->system)
    1698             :                 return NULL;    /* no system schema "bam": nothing to do */
    1699             : 
    1700           0 :         buf = GDKmalloc(bufsize);
    1701           0 :         if (buf == NULL)
    1702           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1703             : 
    1704           0 :         s->system = 0;
    1705           0 :         if ((t = mvc_bind_table(m, s, "files")) != NULL)
    1706           0 :                 t->system = 0;
    1707           0 :         if ((t = mvc_bind_table(m, s, "sq")) != NULL)
    1708           0 :                 t->system = 0;
    1709           0 :         if ((t = mvc_bind_table(m, s, "rg")) != NULL)
    1710           0 :                 t->system = 0;
    1711           0 :         if ((t = mvc_bind_table(m, s, "pg")) != NULL)
    1712           0 :                 t->system = 0;
    1713           0 :         if ((t = mvc_bind_table(m, s, "export")) != NULL)
    1714           0 :                 t->system = 0;
    1715             : 
    1716             :         /* check if any of the tables in the bam schema have any content */
    1717           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1718             :                         "select sum(count) from sys.storage('bam');\n");
    1719           0 :         err = SQLstatementIntern(c, buf, "update", 1, 0, &output);
    1720           0 :         if (err) {
    1721           0 :                 GDKfree(buf);
    1722           0 :                 return err;
    1723             :         }
    1724           0 :         b = BATdescriptor(output->cols[0].b);
    1725           0 :         res_table_destroy(output);
    1726           0 :         if (b == NULL) {
    1727           0 :                 GDKfree(buf);
    1728           0 :                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1729             :         }
    1730           0 :         pos = 0;
    1731           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1732             :                         "update sys.schemas set system = false where name = 'bam';\n"
    1733             :                         "update sys._tables set system = false where schema_id in (select id from sys.schemas where name = 'bam');\n"
    1734             :                         "drop procedure bam.bam_loader_repos cascade;\n"
    1735             :                         "drop procedure bam.bam_loader_files cascade;\n"
    1736             :                         "drop procedure bam.bam_loader_file cascade;\n"
    1737             :                         "drop procedure bam.bam_drop_file cascade;\n"
    1738             :                         "drop function bam.bam_flag cascade;\n"
    1739             :                         "drop function bam.reverse_seq cascade;\n"
    1740             :                         "drop function bam.reverse_qual cascade;\n"
    1741             :                         "drop function bam.seq_length cascade;\n"
    1742             :                         "drop function bam.seq_char cascade;\n"
    1743             :                         "drop procedure bam.sam_export cascade;\n"
    1744             :                         "drop procedure bam.bam_export cascade;\n");
    1745           0 :         if (BATcount(b) > 0 && *(lng *) Tloc(b, 0) == 0) {
    1746             :                 /* tables in bam schema are empty: drop them */
    1747           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    1748             :                                                 "drop table bam.sq cascade;\n"
    1749             :                                                 "drop table bam.rg cascade;\n"
    1750             :                                                 "drop table bam.pg cascade;\n"
    1751             :                                                 "drop table bam.export cascade;\n"
    1752             :                                                 "drop table bam.files cascade;\n"
    1753             :                                                 "drop schema bam cascade;\n");
    1754             :         }
    1755           0 :         BBPunfix(b->batCacheid);
    1756             : 
    1757           0 :         assert(pos < bufsize);
    1758             : 
    1759           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1760           0 :         fflush(stdout);
    1761           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1762             : 
    1763           0 :         GDKfree(buf);
    1764           0 :         return err;
    1765             : }
    1766             : 
    1767             : #ifdef HAVE_HGE
    1768             : static str
    1769           0 : sql_update_jun2020_sp1_hugeint(Client c)
    1770             : {
    1771           0 :         size_t bufsize = 8192, pos = 0;
    1772           0 :         char *buf, *err;
    1773             : 
    1774           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1775           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1776             : 
    1777             :         /* 39_analytics_hge.sql */
    1778           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1779             :                         "create window stddev_samp(val HUGEINT) returns DOUBLE\n"
    1780             :                         " external name \"sql\".\"stdev\";\n"
    1781             :                         "GRANT EXECUTE ON WINDOW stddev_samp(HUGEINT) TO PUBLIC;\n"
    1782             :                         "create window stddev_pop(val HUGEINT) returns DOUBLE\n"
    1783             :                         " external name \"sql\".\"stdevp\";\n"
    1784             :                         "GRANT EXECUTE ON WINDOW stddev_pop(HUGEINT) TO PUBLIC;\n"
    1785             :                         "create window var_samp(val HUGEINT) returns DOUBLE\n"
    1786             :                         " external name \"sql\".\"variance\";\n"
    1787             :                         "GRANT EXECUTE ON WINDOW var_samp(HUGEINT) TO PUBLIC;\n"
    1788             :                         "create aggregate covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1789             :                         " external name \"aggr\".\"covariance\";\n"
    1790             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1791             :                         "create window covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1792             :                         " external name \"sql\".\"covariance\";\n"
    1793             :                         "GRANT EXECUTE ON WINDOW covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1794             :                         "create window var_pop(val HUGEINT) returns DOUBLE\n"
    1795             :                         " external name \"sql\".\"variancep\";\n"
    1796             :                         "GRANT EXECUTE ON WINDOW var_pop(HUGEINT) TO PUBLIC;\n"
    1797             :                         "create aggregate covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1798             :                         " external name \"aggr\".\"covariancep\";\n"
    1799             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1800             :                         "create window covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1801             :                         " external name \"sql\".\"covariancep\";\n"
    1802             :                         "GRANT EXECUTE ON WINDOW covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1803             :                         "create window corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1804             :                         " external name \"sql\".\"corr\";\n"
    1805             :                         "GRANT EXECUTE ON WINDOW corr(HUGEINT, HUGEINT) TO PUBLIC;\n");
    1806             : 
    1807           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1808             :                         "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"
    1809             :                         "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",
    1810             :                         (int) F_AGGR, (int) F_ANALYTIC);
    1811             : 
    1812           0 :         assert(pos < bufsize);
    1813             : 
    1814           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1815           0 :         fflush(stdout);
    1816           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1817           0 :         GDKfree(buf);
    1818           0 :         return err;             /* usually MAL_SUCCEED */
    1819             : }
    1820             : #endif
    1821             : 
    1822             : static str
    1823           0 : sql_update_oscar_lidar(Client c)
    1824             : {
    1825           0 :         char *query =
    1826             :                 "drop procedure sys.lidarattach(string) cascade;\n"
    1827             :                 "drop procedure sys.lidarload(string) cascade;\n"
    1828             :                 "drop procedure sys.lidarexport(string, string, string) cascade;\n";
    1829           0 :         printf("Running database upgrade commands:\n%s\n", query);
    1830           0 :         fflush(stdout);
    1831           0 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
    1832             : }
    1833             : 
    1834             : static str
    1835          99 : sql_update_oscar(Client c, mvc *sql)
    1836             : {
    1837          99 :         size_t bufsize = 8192, pos = 0;
    1838          99 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    1839          99 :         sql_schema *sys = mvc_bind_schema(sql, "sys");
    1840          99 :         res_table *output;
    1841          99 :         BAT *b;
    1842             : 
    1843          99 :         if (buf == NULL)
    1844           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1845             : 
    1846             :         /* if column 6 of sys.queue is named "progress" we need to update */
    1847          99 :         pos += snprintf(buf + pos, bufsize - pos,
    1848             :                         "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");
    1849          99 :         err = SQLstatementIntern(c, buf, "update", true, false, &output);
    1850          99 :         if (err) {
    1851           0 :                 GDKfree(buf);
    1852           0 :                 return err;
    1853             :         }
    1854          99 :         b = BATdescriptor(output->cols[0].b);
    1855          99 :         if (b) {
    1856          99 :                 BATiter bi = bat_iterator_nolock(b);
    1857          99 :                 if (BATcount(b) > 0 && strcmp(BUNtail(bi, 0), "progress") == 0) {
    1858           0 :                         pos = 0;
    1859             : 
    1860             :                         /* the real update of sys.env() has happened
    1861             :                          * in load_func, here we merely update the
    1862             :                          * sys.functions table */
    1863           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1864             :                                         "update sys.functions set"
    1865             :                                         " mod = 'inspect',"
    1866             :                                         " func = 'CREATE FUNCTION env() RETURNS TABLE( name varchar(1024), value varchar(2048)) EXTERNAL NAME inspect.\"getEnvironment\";'"
    1867             :                                         " where schema_id = (select id from sys.schemas where name = 'sys')"
    1868             :                                         " and name = 'env' and type = %d;\n",
    1869             :                                         (int) F_UNION);
    1870             : 
    1871             :                         /* 26_sysmon */
    1872           0 :                         sql_table *t;
    1873           0 :                         t = mvc_bind_table(sql, sys, "queue");
    1874           0 :                         t->system = 0; /* make it non-system else the drop view will fail */
    1875             : 
    1876           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1877             :                                         "drop view sys.queue cascade;\n"
    1878             :                                         "drop function sys.queue cascade;\n"
    1879             :                                         "create function sys.queue()\n"
    1880             :                                         "returns table(\n"
    1881             :                                         "\"tag\" bigint,\n"
    1882             :                                         "\"sessionid\" int,\n"
    1883             :                                         "\"username\" string,\n"
    1884             :                                         "\"started\" timestamp,\n"
    1885             :                                         "\"status\" string,\n"
    1886             :                                         "\"query\" string,\n"
    1887             :                                         "\"finished\" timestamp,\n"
    1888             :                                         "\"workers\" int,\n"
    1889             :                                         "\"memory\" int)\n"
    1890             :                                         " external name sysmon.queue;\n"
    1891             :                                         "grant execute on function sys.queue to public;\n"
    1892             :                                         "create view sys.queue as select * from sys.queue();\n"
    1893             :                                         "grant select on sys.queue to public;\n"
    1894             :                                         "drop procedure sys.pause(bigint) cascade;\n"
    1895             :                                         "drop procedure sys.resume(bigint) cascade;\n"
    1896             :                                         "drop procedure sys.stop(bigint) cascade;\n"
    1897             :                                         "create procedure sys.pause(tag bigint)\n"
    1898             :                                         "external name sysmon.pause;\n"
    1899             :                                         "grant execute on procedure sys.pause(bigint) to public;\n"
    1900             :                                         "create procedure sys.resume(tag bigint)\n"
    1901             :                                         "external name sysmon.resume;\n"
    1902             :                                         "grant execute on procedure sys.resume(bigint) to public;\n"
    1903             :                                         "create procedure sys.stop(tag bigint)\n"
    1904             :                                         "external name sysmon.stop;\n"
    1905             :                                         "grant execute on procedure sys.stop(bigint) to public;\n");
    1906             : 
    1907           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1908             :                                         "update sys.functions set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1909             :                                         " and name = 'queue' and type = %d;\n"
    1910             :                                         "update sys.functions set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1911             :                                         " and name in ('pause', 'resume', 'stop') and type = %d;\n",
    1912             :                                         (int) F_UNION, (int) F_PROC);
    1913           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1914             :                                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1915             :                                         " and name = 'queue';\n");
    1916             : 
    1917             :                         /* scoping branch changes */
    1918           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1919             :                                         "drop function \"sys\".\"var\"() cascade;\n"
    1920             :                                         "create function \"sys\".\"var\"() "
    1921             :                                         "returns table("
    1922             :                                         "\"schema\" string, "
    1923             :                                         "\"name\" string, "
    1924             :                                         "\"type\" string, "
    1925             :                                         "\"value\" string) "
    1926             :                                         "external name \"sql\".\"sql_variables\";\n"
    1927             :                                         "grant execute on function \"sys\".\"var\" to public;\n");
    1928             : 
    1929           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1930             :                                         "update sys.functions set system = true"
    1931             :                                         " where name = 'var' and schema_id = (select id from sys.schemas where name = 'sys');\n"
    1932             :                                         "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",
    1933             :                                         (int) F_UNION);
    1934             : 
    1935           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1936             :                                         "create procedure sys.hot_snapshot(tarfile string, onserver bool)\n"
    1937             :                                         "external name sql.hot_snapshot;\n"
    1938             :                                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1939             :                                         " and name in ('hot_snapshot') and type = %d;\n",
    1940             :                                         (int) F_PROC);
    1941             :                         /* .snapshot user */
    1942           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1943             :                                 "create user \".snapshot\"\n"
    1944             :                                 " with encrypted password '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'\n"
    1945             :                                 " name 'Snapshot User'\n"
    1946             :                                 " schema sys;\n"
    1947             :                                 "grant execute on procedure sys.hot_snapshot(string) to \".snapshot\";\n"
    1948             :                                 "grant execute on procedure sys.hot_snapshot(string, bool) to \".snapshot\";\n"
    1949             :                         );
    1950             : 
    1951             :                         /* SQL functions without backend implementations */
    1952           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1953             :                                         "DROP FUNCTION \"sys\".\"getcontent\"(url) CASCADE;\n"
    1954             :                                         "DROP AGGREGATE \"json\".\"output\"(json) CASCADE;\n");
    1955             : 
    1956             :                         /* 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 */
    1957           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1958             :                                         "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");
    1959             : 
    1960           0 :                         assert(pos < bufsize);
    1961             : 
    1962           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    1963           0 :                         fflush(stdout);
    1964           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1965             :                 }
    1966          99 :                 BBPunfix(b->batCacheid);
    1967             :         } else {
    1968           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1969             :         }
    1970          99 :         res_table_destroy(output);
    1971          99 :         GDKfree(buf);
    1972          99 :         return err;             /* usually MAL_SUCCEED */
    1973             : }
    1974             : 
    1975             : static str
    1976          99 : sql_update_oct2020(Client c, mvc *sql)
    1977             : {
    1978          99 :         size_t bufsize = 4096, pos = 0;
    1979          99 :         char *buf = NULL, *err = NULL;
    1980          99 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    1981          99 :         sql_table *t;
    1982          99 :         res_table *output = NULL;
    1983          99 :         BAT *b = NULL;
    1984             : 
    1985          99 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1986           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1987             : 
    1988             :         /* if view sys.var_values mentions the query cache ('cache') we need
    1989             :            to update */
    1990          99 :         pos += snprintf(buf + pos, bufsize - pos,
    1991             :                                         "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");
    1992          99 :         err = SQLstatementIntern(c, buf, "update", true, false, &output);
    1993          99 :         if (err) {
    1994           0 :                 GDKfree(buf);
    1995           0 :                 return err;
    1996             :         }
    1997          99 :         b = BATdescriptor(output->cols[0].b);
    1998          99 :         if (b) {
    1999          99 :                 if (BATcount(b) > 0) {
    2000           0 :                         pos = 0;
    2001             : 
    2002             :                         /* 51_sys_schema_extensions.sql */
    2003           0 :                         t = mvc_bind_table(sql, s, "var_values");
    2004           0 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    2005           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2006             :                                                         "DROP VIEW sys.var_values CASCADE;\n"
    2007             :                                                         "CREATE VIEW sys.var_values (var_name, value) AS\n"
    2008             :                                                         "SELECT 'current_role', current_role UNION ALL\n"
    2009             :                                                         "SELECT 'current_schema', current_schema UNION ALL\n"
    2010             :                                                         "SELECT 'current_timezone', current_timezone UNION ALL\n"
    2011             :                                                         "SELECT 'current_user', current_user UNION ALL\n"
    2012             :                                                         "SELECT 'debug', debug UNION ALL\n"
    2013             :                                                         "SELECT 'last_id', last_id UNION ALL\n"
    2014             :                                                         "SELECT 'optimizer', optimizer UNION ALL\n"
    2015             :                                                         "SELECT 'pi', pi() UNION ALL\n"
    2016             :                                                         "SELECT 'rowcnt', rowcnt;\n"
    2017             :                                                         "UPDATE sys._tables SET system = true WHERE name = 'var_values' AND schema_id = (SELECT id FROM sys.schemas WHERE name = 'sys');\n"
    2018             :                                                         "GRANT SELECT ON sys.var_values TO PUBLIC;\n");
    2019             :                         /* 26_sysmon.sql */
    2020           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2021             :                                         "create function sys.user_statistics()\n"
    2022             :                                         "returns table(\n"
    2023             :                                                 " username string,\n"
    2024             :                                                 " querycount bigint,\n"
    2025             :                                                 " totalticks bigint,\n"
    2026             :                                                 " started timestamp,\n"
    2027             :                                                 " finished timestamp,\n"
    2028             :                                                 " maxticks bigint,\n"
    2029             :                                                 " maxquery string\n"
    2030             :                                         ")\n"
    2031             :                                         "external name sysmon.user_statistics;\n"
    2032             :                                         "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);
    2033             : 
    2034             :                         /* Remove entries on sys.args table without correspondents on sys.functions table */
    2035           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2036             :                                         "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");
    2037             : 
    2038             :                         /* 39_analytics.sql */
    2039           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2040             :                                         "DROP AGGREGATE stddev_samp(INTERVAL SECOND) CASCADE;\n"
    2041             :                                         "DROP AGGREGATE stddev_samp(INTERVAL MONTH) CASCADE;\n"
    2042             :                                         "DROP WINDOW stddev_samp(INTERVAL SECOND) CASCADE;\n"
    2043             :                                         "DROP WINDOW stddev_samp(INTERVAL MONTH) CASCADE;\n"
    2044             :                                         "DROP AGGREGATE stddev_pop(INTERVAL SECOND) CASCADE;\n"
    2045             :                                         "DROP AGGREGATE stddev_pop(INTERVAL MONTH) CASCADE;\n"
    2046             :                                         "DROP WINDOW stddev_pop(INTERVAL SECOND) CASCADE;\n"
    2047             :                                         "DROP WINDOW stddev_pop(INTERVAL MONTH) CASCADE;\n"
    2048             :                                         "DROP AGGREGATE var_samp(INTERVAL SECOND) CASCADE;\n"
    2049             :                                         "DROP AGGREGATE var_samp(INTERVAL MONTH) CASCADE;\n"
    2050             :                                         "DROP WINDOW var_samp(INTERVAL SECOND) CASCADE;\n"
    2051             :                                         "DROP WINDOW var_samp(INTERVAL MONTH) CASCADE;\n"
    2052             :                                         "DROP AGGREGATE var_pop(INTERVAL SECOND) CASCADE;\n"
    2053             :                                         "DROP AGGREGATE var_pop(INTERVAL MONTH) CASCADE;\n"
    2054             :                                         "DROP WINDOW var_pop(INTERVAL SECOND) CASCADE;\n"
    2055             :                                         "DROP WINDOW var_pop(INTERVAL MONTH) CASCADE;\n"
    2056             :                                         "DROP AGGREGATE covar_samp(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2057             :                                         "DROP AGGREGATE covar_samp(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2058             :                                         "DROP WINDOW covar_samp(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2059             :                                         "DROP WINDOW covar_samp(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2060             :                                         "DROP AGGREGATE covar_pop(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2061             :                                         "DROP AGGREGATE covar_pop(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2062             :                                         "DROP WINDOW covar_pop(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2063             :                                         "DROP WINDOW covar_pop(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2064             :                                         "DROP AGGREGATE corr(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2065             :                                         "DROP AGGREGATE corr(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2066             :                                         "DROP WINDOW corr(INTERVAL SECOND,INTERVAL SECOND) CASCADE;\n"
    2067             :                                         "DROP WINDOW corr(INTERVAL MONTH,INTERVAL MONTH) CASCADE;\n"
    2068             :                                         "create aggregate median(val INTERVAL DAY) returns INTERVAL DAY\n"
    2069             :                                         " external name \"aggr\".\"median\";\n"
    2070             :                                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL DAY) TO PUBLIC;\n"
    2071             :                                         "create aggregate quantile(val INTERVAL DAY, q DOUBLE) returns INTERVAL DAY\n"
    2072             :                                         " external name \"aggr\".\"quantile\";\n"
    2073             :                                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL DAY, DOUBLE) TO PUBLIC;\n"
    2074             :                                         "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);
    2075             : 
    2076             :                         /* 90_generator.sql */
    2077           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2078             :                                         "create function sys.generate_series(first timestamp, \"limit\" timestamp, stepsize interval day) returns table (value timestamp)\n"
    2079             :                                         " external name generator.series;\n"
    2080             :                                         "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);
    2081             : 
    2082             :                         /* 51_sys_schema_extensions.sql */
    2083           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2084             :                                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    2085             :                                         "insert into sys.keywords values ('EPOCH');\n");
    2086             : 
    2087           0 :                         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    2088             : 
    2089           0 :                         assert(pos < bufsize);
    2090           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    2091           0 :                         fflush(stdout);
    2092           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2093           0 :                         if (err != MAL_SUCCEED)
    2094           0 :                                 goto bailout;
    2095             : 
    2096           0 :                         pos = snprintf(buf, bufsize,
    2097             :                                         "ALTER TABLE sys.keywords SET READ ONLY;\n");
    2098           0 :                         assert(pos < bufsize);
    2099           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    2100           0 :                         fflush(stdout);
    2101           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2102           0 :                         if (err != MAL_SUCCEED)
    2103           0 :                                 goto bailout;
    2104           0 :                         err = sql_update_storagemodel(c, sql, true); /* because of day interval addition, we have to recreate the storagmodel views */
    2105             :                 }
    2106             :         } else {
    2107           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2108             :         }
    2109             : 
    2110           0 : bailout:
    2111          99 :         BBPreclaim(b);
    2112          99 :         if (output)
    2113          99 :                 res_table_destroy(output);
    2114          99 :         GDKfree(buf);
    2115          99 :         return err;             /* usually MAL_SUCCEED */
    2116             : }
    2117             : 
    2118             : static str
    2119          99 : sql_update_oct2020_sp1(Client c, mvc *sql)
    2120             : {
    2121          99 :         size_t bufsize = 1024, pos = 0;
    2122          99 :         char *buf = NULL, *err = NULL;
    2123             : 
    2124          99 :         if (!sql_bind_func(sql, "sys", "uuid", sql_bind_localtype("int"), NULL, F_FUNC, true)) {
    2125           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    2126           0 :                 sql->errstr[0] = '\0';
    2127             : 
    2128           0 :                 if ((buf = GDKmalloc(bufsize)) == NULL)
    2129           0 :                         throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2130             : 
    2131             :                 /* 45_uuid.sql */
    2132           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    2133             :                         "create function sys.uuid(d int) returns uuid\n"
    2134             :                         " external name uuid.\"new\";\n"
    2135             :                         "GRANT EXECUTE ON FUNCTION sys.uuid(int) TO PUBLIC;\n"
    2136             :                         "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);
    2137             : 
    2138           0 :                 assert(pos < bufsize);
    2139           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    2140           0 :                 fflush(stdout);
    2141           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2142             :         }
    2143           0 :         GDKfree(buf);
    2144          99 :         return err;             /* usually MAL_SUCCEED */
    2145             : }
    2146             : 
    2147             : static str
    2148          99 : sql_update_jul2021(Client c, mvc *sql)
    2149             : {
    2150          99 :         size_t bufsize = 65536, pos = 0;
    2151          99 :         char *buf = NULL, *err = NULL;
    2152          99 :         res_table *output = NULL;
    2153          99 :         BAT *b = NULL;
    2154          99 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    2155          99 :         sql_table *t;
    2156             : 
    2157          99 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    2158           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2159             : 
    2160             :         /* if the keyword STREAM is in the list of keywords, upgrade */
    2161          99 :         pos += snprintf(buf + pos, bufsize - pos,
    2162             :                                         "select keyword from sys.keywords where keyword = 'STREAM';\n");
    2163          99 :         assert(pos < bufsize);
    2164          99 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    2165           0 :                 goto bailout;
    2166          99 :         if ((b = BATdescriptor(output->cols[0].b))) {
    2167          99 :                 if (BATcount(b) == 1) {
    2168             :                         /* 20_vacuum.sql */
    2169           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2170             :                                                         "drop procedure sys.shrink(string, string) cascade;\n"
    2171             :                                                         "drop procedure sys.reuse(string, string) cascade;\n"
    2172             :                                                         "drop procedure sys.vacuum(string, string) cascade;\n");
    2173             : 
    2174             :                         /* 22_clients.sql */
    2175           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2176             :                                                         "create function sys.current_sessionid() returns int\n"
    2177             :                                                         "external name clients.current_sessionid;\n"
    2178             :                                                         "grant execute on function sys.current_sessionid to public;\n"
    2179             :                                                         "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'current_sessionid' and type = %d;\n", (int) F_FUNC);
    2180             : 
    2181             :                         /* 25_debug.sql */
    2182           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2183             :                                                         "drop procedure sys.flush_log() cascade;\n");
    2184             : 
    2185           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2186             :                                                         "drop function sys.deltas(string) cascade;\n"
    2187             :                                                         "drop function sys.deltas(string, string) cascade;\n"
    2188             :                                                         "drop function sys.deltas(string, string, string) cascade;\n");
    2189           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2190             :                                                         "create function sys.deltas (\"schema\" string)\n"
    2191             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2192             :                                                         "external name \"sql\".\"deltas\";\n"
    2193             :                                                         "create function sys.deltas (\"schema\" string, \"table\" string)\n"
    2194             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2195             :                                                         "external name \"sql\".\"deltas\";\n"
    2196             :                                                         "create function sys.deltas (\"schema\" string, \"table\" string, \"column\" string)\n"
    2197             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2198             :                                                         "external name \"sql\".\"deltas\";\n"
    2199             :                                                         "update sys.functions set system = true"
    2200             :                                                         " where schema_id = 2000 and name = 'deltas';\n");
    2201             : 
    2202             :                         /* 26_sysmon */
    2203           0 :                         t = mvc_bind_table(sql, s, "queue");
    2204           0 :                         t->system = 0; /* make it non-system else the drop view will fail */
    2205             : 
    2206           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2207             :                                                         "drop view sys.queue cascade;\n"
    2208             :                                                         "drop function sys.queue cascade;\n"
    2209             :                                                         "create function sys.queue()\n"
    2210             :                                                         "returns table(\n"
    2211             :                                                         "\"tag\" bigint,\n"
    2212             :                                                         "\"sessionid\" int,\n"
    2213             :                                                         "\"username\" string,\n"
    2214             :                                                         "\"started\" timestamp,\n"
    2215             :                                                         "\"status\" string,\n"
    2216             :                                                         "\"query\" string,\n"
    2217             :                                                         "\"finished\" timestamp,\n"
    2218             :                                                         "\"maxworkers\" int,\n"
    2219             :                                                         "\"footprint\" int\n"
    2220             :                                                         ")\n"
    2221             :                                                         "external name sysmon.queue;\n"
    2222             :                                                         "grant execute on function sys.queue to public;\n"
    2223             :                                                         "create view sys.queue as select * from sys.queue();\n"
    2224             :                                                         "grant select on sys.queue to public;\n");
    2225           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2226             :                                                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    2227             :                                                         " and name = 'queue' and type = %d;\n", (int) F_UNION);
    2228           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2229             :                                                         "update sys._tables set system = true where schema_id = 2000"
    2230             :                                                         " and name = 'queue';\n");
    2231             : 
    2232             :                         /* fix up dependencies for function getproj4 (if it exists) */
    2233           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2234             :                                                         "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");
    2235             : 
    2236             :                         /* 41_json.sql */
    2237           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2238             :                                                         "drop function json.isobject(string) cascade;\n"
    2239             :                                                         "drop function json.isarray(string) cascade;\n"
    2240             :                                                         "drop function json.isvalid(json) cascade;\n"
    2241             :                                                         "create function json.isvalid(js json)\n"
    2242             :                                                         "returns bool begin return true; end;\n"
    2243             :                                                         "grant execute on function json.isvalid(json) to public;\n"
    2244             :                                                         "update sys.functions set system = true"
    2245             :                                                         " where schema_id = (select id from sys.schemas where name = 'json')"
    2246             :                                                         " and name = 'isvalid';\n");
    2247             : 
    2248             :                         /* 51_sys_schema_extensions, remove stream table entries and update window function description */
    2249           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2250             :                                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    2251             :                                         "DELETE FROM sys.keywords where keyword = 'STREAM';\n"
    2252             :                                         "INSERT INTO sys.keywords VALUES ('BIG'), ('LITTLE'), ('NATIVE'), ('ENDIAN'), ('CURRENT_SCHEMA'), ('CURRENT_TIMEZONE'), ('IMPRINTS'), ('ORDERED'), ('PATH'), ('ROLE'), ('ROW'), ('VALUE');\n"
    2253             :                                         "ALTER TABLE sys.table_types SET READ WRITE;\n"
    2254             :                                         "DELETE FROM sys.table_types where table_type_id = 4;\n"
    2255             :                                         "ALTER TABLE sys.function_types SET READ WRITE;\n"
    2256             :                                         "UPDATE sys.function_types SET function_type_keyword = 'WINDOW' WHERE function_type_id = 6;\n");
    2257             : 
    2258             :                         /* 52_describe.sql */
    2259           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2260             :                                         "CREATE FUNCTION sys.describe_type(ctype string, digits integer, tscale integer)\n"
    2261             :                                         "  RETURNS string\n"
    2262             :                                         "BEGIN\n"
    2263             :                                         "  RETURN\n"
    2264             :                                         "    CASE ctype\n"
    2265             :                                         "      WHEN 'bigint' THEN 'BIGINT'\n"
    2266             :                                         "      WHEN 'blob' THEN\n"
    2267             :                                         "        CASE digits\n"
    2268             :                                         "          WHEN 0 THEN 'BINARY LARGE OBJECT'\n"
    2269             :                                         "          ELSE 'BINARY LARGE OBJECT(' || digits || ')'\n"
    2270             :                                         "        END\n"
    2271             :                                         "      WHEN 'boolean' THEN 'BOOLEAN'\n"
    2272             :                                         "      WHEN 'char' THEN\n"
    2273             :                                         "        CASE digits\n"
    2274             :                                         "          WHEN 1 THEN 'CHARACTER'\n"
    2275             :                                         "          ELSE 'CHARACTER(' || digits || ')'\n"
    2276             :                                         "        END\n"
    2277             :                                         "      WHEN 'clob' THEN\n"
    2278             :                                         "    CASE digits\n"
    2279             :                                         "      WHEN 0 THEN 'CHARACTER LARGE OBJECT'\n"
    2280             :                                         "      ELSE 'CHARACTER LARGE OBJECT(' || digits || ')'\n"
    2281             :                                         "    END\n"
    2282             :                                         "      WHEN 'date' THEN 'DATE'\n"
    2283             :                                         "      WHEN 'day_interval' THEN 'INTERVAL DAY'\n"
    2284             :                                         "      WHEN ctype = 'decimal' THEN\n"
    2285             :                                         "      CASE\n"
    2286             :                                         "      WHEN (digits = 1 AND tscale = 0) OR digits = 0 THEN 'DECIMAL'\n"
    2287             :                                         "      WHEN tscale = 0 THEN 'DECIMAL(' || digits || ')'\n"
    2288             :                                         "      WHEN digits = 39 THEN 'DECIMAL(' || 38 || ',' || tscale || ')'\n"
    2289             :                                         "      WHEN digits = 19 AND (SELECT COUNT(*) = 0 FROM sys.types WHERE sqlname = 'hugeint' ) THEN 'DECIMAL(' || 18 || ',' || tscale || ')'\n"
    2290             :                                         "      ELSE 'DECIMAL(' || digits || ',' || tscale || ')'\n"
    2291             :                                         "    END\n"
    2292             :                                         "      WHEN 'double' THEN\n"
    2293             :                                         "    CASE\n"
    2294             :                                         "      WHEN digits = 53 and tscale = 0 THEN 'DOUBLE'\n"
    2295             :                                         "      WHEN tscale = 0 THEN 'FLOAT(' || digits || ')'\n"
    2296             :                                         "      ELSE 'FLOAT(' || digits || ',' || tscale || ')'\n"
    2297             :                                         "    END\n"
    2298             :                                         "      WHEN 'geometry' THEN\n"
    2299             :                                         "    CASE digits\n"
    2300             :                                         "      WHEN 4 THEN 'GEOMETRY(POINT' ||\n"
    2301             :                                         "            CASE tscale\n"
    2302             :                                         "              WHEN 0 THEN ''\n"
    2303             :                                         "              ELSE ',' || tscale\n"
    2304             :                                         "            END || ')'\n"
    2305             :                                         "      WHEN 8 THEN 'GEOMETRY(LINESTRING' ||\n"
    2306             :                                         "            CASE tscale\n"
    2307             :                                         "              WHEN 0 THEN ''\n"
    2308             :                                         "              ELSE ',' || tscale\n"
    2309             :                                         "            END || ')'\n"
    2310             :                                         "      WHEN 16 THEN 'GEOMETRY(POLYGON' ||\n"
    2311             :                                         "            CASE tscale\n"
    2312             :                                         "              WHEN 0 THEN ''\n"
    2313             :                                         "              ELSE ',' || tscale\n"
    2314             :                                         "            END || ')'\n"
    2315             :                                         "      WHEN 20 THEN 'GEOMETRY(MULTIPOINT' ||\n"
    2316             :                                         "            CASE tscale\n"
    2317             :                                         "              WHEN 0 THEN ''\n"
    2318             :                                         "              ELSE ',' || tscale\n"
    2319             :                                         "            END || ')'\n"
    2320             :                                         "      WHEN 24 THEN 'GEOMETRY(MULTILINESTRING' ||\n"
    2321             :                                         "            CASE tscale\n"
    2322             :                                         "              WHEN 0 THEN ''\n"
    2323             :                                         "              ELSE ',' || tscale\n"
    2324             :                                         "            END || ')'\n"
    2325             :                                         "      WHEN 28 THEN 'GEOMETRY(MULTIPOLYGON' ||\n"
    2326             :                                         "            CASE tscale\n"
    2327             :                                         "              WHEN 0 THEN ''\n"
    2328             :                                         "              ELSE ',' || tscale\n"
    2329             :                                         "            END || ')'\n"
    2330             :                                         "      WHEN 32 THEN 'GEOMETRY(GEOMETRYCOLLECTION' ||\n"
    2331             :                                         "            CASE tscale\n"
    2332             :                                         "              WHEN 0 THEN ''\n"
    2333             :                                         "              ELSE ',' || tscale\n"
    2334             :                                         "            END || ')'\n"
    2335             :                                         "      ELSE 'GEOMETRY'\n"
    2336             :                                         "        END\n"
    2337             :                                         "      WHEN 'hugeint' THEN 'HUGEINT'\n"
    2338             :                                         "      WHEN 'int' THEN 'INTEGER'\n"
    2339             :                                         "      WHEN 'month_interval' THEN\n"
    2340             :                                         "    CASE digits\n"
    2341             :                                         "      WHEN 1 THEN 'INTERVAL YEAR'\n"
    2342             :                                         "      WHEN 2 THEN 'INTERVAL YEAR TO MONTH'\n"
    2343             :                                         "      WHEN 3 THEN 'INTERVAL MONTH'\n"
    2344             :                                         "    END\n"
    2345             :                                         "      WHEN 'real' THEN\n"
    2346             :                                         "    CASE\n"
    2347             :                                         "      WHEN digits = 24 and tscale = 0 THEN 'REAL'\n"
    2348             :                                         "      WHEN tscale = 0 THEN 'FLOAT(' || digits || ')'\n"
    2349             :                                         "      ELSE 'FLOAT(' || digits || ',' || tscale || ')'\n"
    2350             :                                         "    END\n"
    2351             :                                         "      WHEN 'sec_interval' THEN\n"
    2352             :                                         "    CASE digits\n"
    2353             :                                         "      WHEN 4 THEN 'INTERVAL DAY'\n"
    2354             :                                         "      WHEN 5 THEN 'INTERVAL DAY TO HOUR'\n"
    2355             :                                         "      WHEN 6 THEN 'INTERVAL DAY TO MINUTE'\n"
    2356             :                                         "      WHEN 7 THEN 'INTERVAL DAY TO SECOND'\n"
    2357             :                                         "      WHEN 8 THEN 'INTERVAL HOUR'\n"
    2358             :                                         "      WHEN 9 THEN 'INTERVAL HOUR TO MINUTE'\n"
    2359             :                                         "      WHEN 10 THEN 'INTERVAL HOUR TO SECOND'\n"
    2360             :                                         "      WHEN 11 THEN 'INTERVAL MINUTE'\n"
    2361             :                                         "      WHEN 12 THEN 'INTERVAL MINUTE TO SECOND'\n"
    2362             :                                         "      WHEN 13 THEN 'INTERVAL SECOND'\n"
    2363             :                                         "    END\n"
    2364             :                                         "      WHEN 'smallint' THEN 'SMALLINT'\n"
    2365             :                                         "      WHEN 'time' THEN\n"
    2366             :                                         "    CASE digits\n"
    2367             :                                         "      WHEN 1 THEN 'TIME'\n"
    2368             :                                         "      ELSE 'TIME(' || (digits - 1) || ')'\n"
    2369             :                                         "    END\n"
    2370             :                                         "      WHEN 'timestamp' THEN\n"
    2371             :                                         "    CASE digits\n"
    2372             :                                         "      WHEN 7 THEN 'TIMESTAMP'\n"
    2373             :                                         "      ELSE 'TIMESTAMP(' || (digits - 1) || ')'\n"
    2374             :                                         "    END\n"
    2375             :                                         "      WHEN 'timestamptz' THEN\n"
    2376             :                                         "    CASE digits\n"
    2377             :                                         "      WHEN 7 THEN 'TIMESTAMP'\n"
    2378             :                                         "      ELSE 'TIMESTAMP(' || (digits - 1) || ')'\n"
    2379             :                                         "    END || ' WITH TIME ZONE'\n"
    2380             :                                         "      WHEN 'timetz' THEN\n"
    2381             :                                         "    CASE digits\n"
    2382             :                                         "      WHEN 1 THEN 'TIME'\n"
    2383             :                                         "      ELSE 'TIME(' || (digits - 1) || ')'\n"
    2384             :                                         "    END || ' WITH TIME ZONE'\n"
    2385             :                                         "      WHEN 'tinyint' THEN 'TINYINT'\n"
    2386             :                                         "      WHEN 'varchar' THEN 'CHARACTER VARYING(' || digits || ')'\n"
    2387             :                                         "      ELSE\n"
    2388             :                                         "        CASE\n"
    2389             :                                         "          WHEN lower(ctype) = ctype THEN upper(ctype)\n"
    2390             :                                         "          ELSE '\"' || ctype || '\"'\n"
    2391             :                                         "        END || CASE digits\n"
    2392             :                                         "      WHEN 0 THEN ''\n"
    2393             :                                         "          ELSE '(' || digits || CASE tscale\n"
    2394             :                                         "        WHEN 0 THEN ''\n"
    2395             :                                         "            ELSE ',' || tscale\n"
    2396             :                                         "          END || ')'\n"
    2397             :                                         "    END\n"
    2398             :                                         "    END;\n"
    2399             :                                         "END;\n"
    2400             :                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN ' ''' || sys.replace(s,'''','''''') || ''' '; END;\n"
    2401             :                                         "CREATE FUNCTION sys.DQ (s STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(s,'\"','\"\"') || '\"'; END; --TODO: Figure out why this breaks with the space\n"
    2402             :                                         "CREATE FUNCTION sys.FQN(s STRING, t STRING) RETURNS STRING BEGIN RETURN sys.DQ(s) || '.' || sys.DQ(t); END;\n"
    2403             :                                         "CREATE FUNCTION sys.ALTER_TABLE(s STRING, t STRING) RETURNS STRING BEGIN RETURN 'ALTER TABLE ' || sys.FQN(s, t) || ' '; END;\n"
    2404             :                                         "--We need pcre to implement a header guard which means adding the schema of an object explicitely to its identifier.\n"
    2405             :                                         "CREATE FUNCTION sys.replace_first(ori STRING, pat STRING, rep STRING, flg STRING) RETURNS STRING EXTERNAL NAME \"pcre\".\"replace_first\";\n"
    2406             :                                         "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
    2407             :                                         "RETURN\n"
    2408             :                                         "    SELECT sys.replace_first(stmt, '(\\\\s*\"?' || sch ||  '\"?\\\\s*\\\\.|)\\\\s*\"?' || nme || '\"?\\\\s*', ' ' || sys.FQN(sch, nme) || ' ', 'imsx');\n"
    2409             :                                         "END;\n"
    2410             :                                         "CREATE VIEW sys.describe_constraints AS\n"
    2411             :                                         "    SELECT\n"
    2412             :                                         "        s.name sch,\n"
    2413             :                                         "        t.name tbl,\n"
    2414             :                                         "        kc.name col,\n"
    2415             :                                         "        k.name con,\n"
    2416             :                                         "        CASE WHEN k.type = 0 THEN 'PRIMARY KEY' WHEN k.type = 1 THEN 'UNIQUE' END tpe\n"
    2417             :                                         "    FROM sys.schemas s, sys._tables t, sys.objects kc, sys.keys k\n"
    2418             :                                         "    WHERE kc.id = k.id\n"
    2419             :                                         "        AND k.table_id = t.id\n"
    2420             :                                         "        AND s.id = t.schema_id\n"
    2421             :                                         "        AND t.system = FALSE\n"
    2422             :                                         "        AND k.type in (0, 1)\n"
    2423             :                                         "        AND t.type IN (0, 6);\n"
    2424             :                                         "CREATE VIEW sys.describe_indices AS\n"
    2425             :                                         "    WITH it (id, idx) AS (VALUES (0, 'INDEX'), (4, 'IMPRINTS INDEX'), (5, 'ORDERED INDEX')) --UNIQUE INDEX wraps to INDEX.\n"
    2426             :                                         "    SELECT\n"
    2427             :                                         "        i.name ind,\n"
    2428             :                                         "        s.name sch,\n"
    2429             :                                         "        t.name tbl,\n"
    2430             :                                         "        c.name col,\n"
    2431             :                                         "        it.idx tpe\n"
    2432             :                                         "    FROM\n"
    2433             :                                         "        sys.idxs AS i LEFT JOIN sys.keys AS k ON i.name = k.name,\n"
    2434             :                                         "        sys.objects AS kc,\n"
    2435             :                                         "        sys._columns AS c,\n"
    2436             :                                         "        sys.schemas s,\n"
    2437             :                                         "        sys._tables AS t,\n"
    2438             :                                         "        it\n"
    2439             :                                         "    WHERE\n"
    2440             :                                         "        i.table_id = t.id\n"
    2441             :                                         "        AND i.id = kc.id\n"
    2442             :                                         "        AND kc.name = c.name\n"
    2443             :                                         "        AND t.id = c.table_id\n"
    2444             :                                         "        AND t.schema_id = s.id\n"
    2445             :                                         "        AND k.type IS NULL\n"
    2446             :                                         "        AND i.type = it.id\n"
    2447             :                                         "    ORDER BY i.name, kc.nr;\n"
    2448             :                                         "CREATE VIEW sys.describe_column_defaults AS\n"
    2449             :                                         "    SELECT\n"
    2450             :                                         "        s.name sch,\n"
    2451             :                                         "        t.name tbl,\n"
    2452             :                                         "        c.name col,\n"
    2453             :                                         "        c.\"default\" def\n"
    2454             :                                         "    FROM sys.schemas s, sys.tables t, sys.columns c\n"
    2455             :                                         "    WHERE\n"
    2456             :                                         "        s.id = t.schema_id AND\n"
    2457             :                                         "        t.id = c.table_id AND\n"
    2458             :                                         "        s.name <> 'tmp' AND\n"
    2459             :                                         "        NOT t.system AND\n"
    2460             :                                         "        c.\"default\" IS NOT NULL;\n"
    2461             :                                         "CREATE VIEW sys.describe_foreign_keys AS\n"
    2462             :                                         "        WITH action_type (id, act) AS (VALUES\n"
    2463             :                                         "            (0, 'NO ACTION'),\n"
    2464             :                                         "            (1, 'CASCADE'),\n"
    2465             :                                         "            (2, 'RESTRICT'),\n"
    2466             :                                         "            (3, 'SET NULL'),\n"
    2467             :                                         "            (4, 'SET DEFAULT'))\n"
    2468             :                                         "        SELECT\n"
    2469             :                                         "            fs.name fk_s,\n"
    2470             :                                         "            fkt.name fk_t,\n"
    2471             :                                         "            fkkc.name fk_c,\n"
    2472             :                                         "            fkkc.nr o,\n"
    2473             :                                         "            fkk.name fk,\n"
    2474             :                                         "            ps.name pk_s,\n"
    2475             :                                         "            pkt.name pk_t,\n"
    2476             :                                         "            pkkc.name pk_c,\n"
    2477             :                                         "            ou.act on_update,\n"
    2478             :                                         "            od.act on_delete\n"
    2479             :                                         "        FROM sys._tables fkt,\n"
    2480             :                                         "            sys.objects fkkc,\n"
    2481             :                                         "            sys.keys fkk,\n"
    2482             :                                         "            sys._tables pkt,\n"
    2483             :                                         "            sys.objects pkkc,\n"
    2484             :                                         "            sys.keys pkk,\n"
    2485             :                                         "            sys.schemas ps,\n"
    2486             :                                         "            sys.schemas fs,\n"
    2487             :                                         "            action_type ou,\n"
    2488             :                                         "            action_type od\n"
    2489             :                                         "        WHERE fkt.id = fkk.table_id\n"
    2490             :                                         "        AND pkt.id = pkk.table_id\n"
    2491             :                                         "        AND fkk.id = fkkc.id\n"
    2492             :                                         "        AND pkk.id = pkkc.id\n"
    2493             :                                         "        AND fkk.rkey = pkk.id\n"
    2494             :                                         "        AND fkkc.nr = pkkc.nr\n"
    2495             :                                         "        AND pkt.schema_id = ps.id\n"
    2496             :                                         "        AND fkt.schema_id = fs.id\n"
    2497             :                                         "        AND (fkk.\"action\" & 255)         = od.id\n"
    2498             :                                         "        AND ((fkk.\"action\" >> 8) & 255)  = ou.id\n"
    2499             :                                         "        ORDER BY fkk.name, fkkc.nr;\n"
    2500             :                                         "--TODO: CRASHES when this function gets inlined into describe_tables\n"
    2501             :                                         "CREATE FUNCTION sys.get_merge_table_partition_expressions(tid INT) RETURNS STRING\n"
    2502             :                                         "BEGIN\n"
    2503             :                                         "    RETURN\n"
    2504             :                                         "        SELECT\n"
    2505             :                                         "            CASE WHEN tp.table_id IS NOT NULL THEN    --updatable merge table\n"
    2506             :                                         "                ' PARTITION BY ' ||\n"
    2507             :                                         "                CASE\n"
    2508             :                                         "                    WHEN bit_and(tp.type, 2) = 2\n"
    2509             :                                         "                    THEN 'VALUES '\n"
    2510             :                                         "                    ELSE 'RANGE '\n"
    2511             :                                         "                END ||\n"
    2512             :                                         "                CASE\n"
    2513             :                                         "                    WHEN bit_and(tp.type, 4) = 4 --column expression\n"
    2514             :                                         "                    THEN 'ON ' || '(' || (SELECT sys.DQ(c.name) || ')' FROM sys.columns c WHERE c.id = tp.column_id)\n"
    2515             :                                         "                    ELSE 'USING ' || '(' || tp.expression || ')' --generic expression\n"
    2516             :                                         "                END\n"
    2517             :                                         "            ELSE                                    --read only partition merge table.\n"
    2518             :                                         "                ''\n"
    2519             :                                         "            END\n"
    2520             :                                         "        FROM (VALUES (tid)) t(id) LEFT JOIN sys.table_partitions tp ON t.id = tp.table_id;\n"
    2521             :                                         "END;\n"
    2522             :                                         "--TODO: gives mergejoin errors when inlined\n"
    2523             :                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    2524             :                                         "    RETURN SELECT ' ON ' || sys.SQ(uri) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(\"hash\") FROM sys.remote_table_credentials(s ||'.' || t);\n"
    2525             :                                         "END;\n"
    2526             :                                         "CREATE VIEW sys.describe_tables AS\n"
    2527             :                                         "    SELECT\n"
    2528             :                                         "        t.id o,\n"
    2529             :                                         "        s.name sch,\n"
    2530             :                                         "        t.name tab,\n"
    2531             :                                         "        ts.table_type_name typ,\n"
    2532             :                                         "        (SELECT\n"
    2533             :                                         "            ' (' ||\n"
    2534             :                                         "            GROUP_CONCAT(\n"
    2535             :                                         "                sys.DQ(c.name) || ' ' ||\n"
    2536             :                                         "                sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    2537             :                                         "                ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    2538             :                                         "            , ', ') || ')'\n"
    2539             :                                         "        FROM sys._columns c\n"
    2540             :                                         "        WHERE c.table_id = t.id) col,\n"
    2541             :                                         "        CASE\n"
    2542             :                                         "            WHEN ts.table_type_name = 'REMOTE TABLE' THEN\n"
    2543             :                                         "                sys.get_remote_table_expressions(s.name, t.name)\n"
    2544             :                                         "            WHEN ts.table_type_name = 'MERGE TABLE' THEN\n"
    2545             :                                         "                sys.get_merge_table_partition_expressions(t.id)\n"
    2546             :                                         "            WHEN ts.table_type_name = 'VIEW' THEN\n"
    2547             :                                         "                sys.schema_guard(s.name, t.name, t.query)\n"
    2548             :                                         "            ELSE\n"
    2549             :                                         "                ''\n"
    2550             :                                         "        END opt\n"
    2551             :                                         "    FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    2552             :                                         "    WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE')\n"
    2553             :                                         "        AND t.system = FALSE\n"
    2554             :                                         "        AND s.id = t.schema_id\n"
    2555             :                                         "        AND ts.table_type_id = t.type\n"
    2556             :                                         "        AND s.name <> 'tmp';\n"
    2557             :                                         "CREATE VIEW sys.describe_triggers AS\n"
    2558             :                                         "        SELECT\n"
    2559             :                                         "            s.name sch,\n"
    2560             :                                         "            t.name tab,\n"
    2561             :                                         "            tr.name tri,\n"
    2562             :                                         "            tr.statement def\n"
    2563             :                                         "        FROM sys.schemas s, sys.tables t, sys.triggers tr\n"
    2564             :                                         "        WHERE s.id = t.schema_id AND t.id = tr.table_id AND NOT t.system;\n"
    2565             :                                         "CREATE VIEW sys.describe_comments AS\n"
    2566             :                                         "        SELECT\n"
    2567             :                                         "            o.id id,\n"
    2568             :                                         "            o.tpe tpe,\n"
    2569             :                                         "            o.nme fqn,\n"
    2570             :                                         "            c.remark rem\n"
    2571             :                                         "        FROM (\n"
    2572             :                                         "            SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas\n"
    2573             :                                         "            UNION ALL\n"
    2574             :                                         "            SELECT t.id, CASE WHEN ts.table_type_name = 'VIEW' THEN 'VIEW' ELSE 'TABLE' END, sys.FQN(s.name, t.name)\n"
    2575             :                                         "            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"
    2576             :                                         "            WHERE NOT s.name <> 'tmp'\n"
    2577             :                                         "            UNION ALL\n"
    2578             :                                         "            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"
    2579             :                                         "            UNION ALL\n"
    2580             :                                         "            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"
    2581             :                                         "            UNION ALL\n"
    2582             :                                         "            SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    2583             :                                         "            UNION ALL\n"
    2584             :                                         "            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"
    2585             :                                         "            ) AS o(id, tpe, nme)\n"
    2586             :                                         "            JOIN sys.comments c ON c.id = o.id;\n"
    2587             :                                         "CREATE VIEW sys.fully_qualified_functions AS\n"
    2588             :                                         "    WITH fqn(id, tpe, sig, num) AS\n"
    2589             :                                         "    (\n"
    2590             :                                         "        SELECT\n"
    2591             :                                         "            f.id,\n"
    2592             :                                         "            ft.function_type_keyword,\n"
    2593             :                                         "            CASE WHEN a.type IS NULL THEN\n"
    2594             :                                         "                s.name || '.' || f.name || '()'\n"
    2595             :                                         "            ELSE\n"
    2596             :                                         "                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"
    2597             :                                         "            END,\n"
    2598             :                                         "            a.number\n"
    2599             :                                         "        FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
    2600             :                                         "        WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
    2601             :                                         "    )\n"
    2602             :                                         "    SELECT\n"
    2603             :                                         "        fqn1.id id,\n"
    2604             :                                         "        fqn1.tpe tpe,\n"
    2605             :                                         "        fqn1.sig nme\n"
    2606             :                                         "    FROM\n"
    2607             :                                         "        fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id)  fqn2(id, num)\n"
    2608             :                                         "        ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
    2609             :                                         "CREATE VIEW sys.describe_privileges AS\n"
    2610             :                                         "    SELECT\n"
    2611             :                                         "        CASE\n"
    2612             :                                         "            WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
    2613             :                                         "                'COPY FROM'\n"
    2614             :                                         "            WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
    2615             :                                         "                'COPY INTO'\n"
    2616             :                                         "            ELSE\n"
    2617             :                                         "                o.nme\n"
    2618             :                                         "        END o_nme,\n"
    2619             :                                         "        CASE\n"
    2620             :                                         "            WHEN o.tpe IS NOT NULL THEN\n"
    2621             :                                         "                o.tpe\n"
    2622             :                                         "            ELSE\n"
    2623             :                                         "                'GLOBAL'\n"
    2624             :                                         "        END o_tpe,\n"
    2625             :                                         "        pc.privilege_code_name p_nme,\n"
    2626             :                                         "        a.name a_nme,\n"
    2627             :                                         "        g.name g_nme,\n"
    2628             :                                         "        p.grantable grantable\n"
    2629             :                                         "    FROM\n"
    2630             :                                         "        sys.privileges p LEFT JOIN\n"
    2631             :                                         "        (\n"
    2632             :                                         "        SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
    2633             :                                         "            from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
    2634             :                                         "        UNION ALL\n"
    2635             :                                         "            SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
    2636             :                                         "            FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
    2637             :                                         "        UNION ALL\n"
    2638             :                                         "            SELECT f.id, f.nme, f.tpe\n"
    2639             :                                         "            FROM sys.fully_qualified_functions f\n"
    2640             :                                         "        ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
    2641             :                                         "        sys.privilege_codes pc,\n"
    2642             :                                         "        auths a, auths g\n"
    2643             :                                         "    WHERE\n"
    2644             :                                         "        p.privileges = pc.privilege_code_id AND\n"
    2645             :                                         "        p.auth_id = a.id AND\n"
    2646             :                                         "        p.grantor = g.id;\n"
    2647             :                                         "CREATE FUNCTION sys.describe_table(schemaName string, tableName string)\n"
    2648             :                                         "  RETURNS TABLE(name string, query string, type string, id integer, remark string)\n"
    2649             :                                         "BEGIN\n"
    2650             :                                         "    RETURN SELECT t.name, t.query, tt.table_type_name, t.id, c.remark\n"
    2651             :                                         "        FROM sys.schemas s, sys.table_types tt, sys._tables t\n"
    2652             :                                         "        LEFT OUTER JOIN sys.comments c ON t.id = c.id\n"
    2653             :                                         "            WHERE s.name = schemaName\n"
    2654             :                                         "            AND t.schema_id = s.id\n"
    2655             :                                         "            AND t.name = tableName\n"
    2656             :                                         "            AND t.type = tt.table_type_id;\n"
    2657             :                                         "END;\n"
    2658             :                                         "CREATE VIEW sys.describe_user_defined_types AS\n"
    2659             :                                         "    SELECT\n"
    2660             :                                         "        s.name sch,\n"
    2661             :                                         "        t.sqlname sql_tpe,\n"
    2662             :                                         "        t.systemname ext_tpe\n"
    2663             :                                         "    FROM sys.types t JOIN sys.schemas s ON t.schema_id = s.id\n"
    2664             :                                         "    WHERE\n"
    2665             :                                         "        t.eclass = 18 AND\n"
    2666             :                                         "        (\n"
    2667             :                                         "            (s.name = 'sys' AND t.sqlname not in ('geometrya', 'mbr', 'url', 'inet', 'json', 'uuid', 'xml')) OR\n"
    2668             :                                         "            (s.name <> 'sys')\n"
    2669             :                                         "        );\n"
    2670             :                                         "CREATE VIEW sys.describe_partition_tables AS\n"
    2671             :                                         "    SELECT \n"
    2672             :                                         "        m_sch,\n"
    2673             :                                         "        m_tbl,\n"
    2674             :                                         "        p_sch,\n"
    2675             :                                         "        p_tbl,\n"
    2676             :                                         "        CASE\n"
    2677             :                                         "            WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    2678             :                                         "            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"
    2679             :                                         "            ELSE p_raw_type\n"
    2680             :                                         "        END AS tpe,\n"
    2681             :                                         "        pvalues,\n"
    2682             :                                         "        minimum,\n"
    2683             :                                         "        maximum,\n"
    2684             :                                         "        with_nulls\n"
    2685             :                                         "    FROM \n"
    2686             :                                         "    (WITH\n"
    2687             :                                         "        tp(\"type\", table_id) AS\n"
    2688             :                                         "        (SELECT CASE WHEN (table_partitions.\"type\" & 2) = 2 THEN 'VALUES' ELSE 'RANGE' END, table_partitions.table_id FROM sys.table_partitions),\n"
    2689             :                                         "        subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    2690             :                                         "        (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    2691             :                                         "        FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    2692             :                                         "        WHERE m_t.\"type\" IN (3, 6)\n"
    2693             :                                         "            AND m_t.schema_id = m_s.id\n"
    2694             :                                         "            AND m_s.name <> 'tmp'\n"
    2695             :                                         "            AND m_t.system = FALSE\n"
    2696             :                                         "            AND m_t.id = d.depend_id\n"
    2697             :                                         "            AND d.id = p_m.id\n"
    2698             :                                         "            AND p_m.schema_id = p_s.id\n"
    2699             :                                         "        ORDER BY m_t.id, p_m.id)\n"
    2700             :                                         "    SELECT\n"
    2701             :                                         "        subq.m_sch,\n"
    2702             :                                         "        subq.m_tbl,\n"
    2703             :                                         "        subq.p_sch,\n"
    2704             :                                         "        subq.p_tbl,\n"
    2705             :                                         "        tp.\"type\" AS p_raw_type,\n"
    2706             :                                         "        CASE WHEN tp.\"type\" = 'VALUES'\n"
    2707             :                                         "            THEN (SELECT GROUP_CONCAT(vp.value, ',')FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid)\n"
    2708             :                                         "            ELSE NULL\n"
    2709             :                                         "        END AS pvalues,\n"
    2710             :                                         "        CASE WHEN tp.\"type\" = 'RANGE'\n"
    2711             :                                         "            THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2712             :                                         "            ELSE NULL\n"
    2713             :                                         "        END AS minimum,\n"
    2714             :                                         "        CASE WHEN tp.\"type\" = 'RANGE'\n"
    2715             :                                         "            THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2716             :                                         "            ELSE NULL\n"
    2717             :                                         "        END AS maximum,\n"
    2718             :                                         "        CASE WHEN tp.\"type\" = 'VALUES'\n"
    2719             :                                         "            THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    2720             :                                         "            ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2721             :                                         "        END AS with_nulls\n"
    2722             :                                         "    FROM \n"
    2723             :                                         "        subq LEFT OUTER JOIN tp\n"
    2724             :                                         "        ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    2725             :                                         "CREATE VIEW sys.describe_sequences AS\n"
    2726             :                                         "    SELECT\n"
    2727             :                                         "        s.name as sch,\n"
    2728             :                                         "        seq.name as seq,\n"
    2729             :                                         "        seq.\"start\" s,\n"
    2730             :                                         "        get_value_for(s.name, seq.name) AS rs,\n"
    2731             :                                         "        seq.\"minvalue\" mi,\n"
    2732             :                                         "        seq.\"maxvalue\" ma,\n"
    2733             :                                         "        seq.\"increment\" inc,\n"
    2734             :                                         "        seq.\"cacheinc\" cache,\n"
    2735             :                                         "        seq.\"cycle\" cycle\n"
    2736             :                                         "    FROM sys.sequences seq, sys.schemas s\n"
    2737             :                                         "    WHERE s.id = seq.schema_id\n"
    2738             :                                         "    AND s.name <> 'tmp'\n"
    2739             :                                         "    ORDER BY s.name, seq.name;\n"
    2740             :                                         "CREATE VIEW sys.describe_functions AS\n"
    2741             :                                         "    SELECT\n"
    2742             :                                         "        f.id o,\n"
    2743             :                                         "        s.name sch,\n"
    2744             :                                         "        f.name fun,\n"
    2745             :                                         "        f.func def\n"
    2746             :                                         "    FROM sys.functions f JOIN sys.schemas s ON f.schema_id = s.id WHERE s.name <> 'tmp' AND NOT f.system;\n"
    2747             :                                         "CREATE FUNCTION sys.describe_columns(schemaName string, tableName string)\n"
    2748             :                                         "    RETURNS TABLE(name string, type string, digits integer, scale integer, Nulls boolean, cDefault string, number integer, sqltype string, remark string)\n"
    2749             :                                         "BEGIN\n"
    2750             :                                         "    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"
    2751             :                                         "        FROM sys._tables t, sys.schemas s, sys._columns c\n"
    2752             :                                         "        LEFT OUTER JOIN sys.comments com ON c.id = com.id\n"
    2753             :                                         "            WHERE c.table_id = t.id\n"
    2754             :                                         "            AND t.name = tableName\n"
    2755             :                                         "            AND t.schema_id = s.id\n"
    2756             :                                         "            AND s.name = schemaName\n"
    2757             :                                         "        ORDER BY c.number;\n"
    2758             :                                         "END;\n"
    2759             :                                         "CREATE FUNCTION sys.describe_function(schemaName string, functionName string)\n"
    2760             :                                         "    RETURNS TABLE(id integer, name string, type string, language string, remark string)\n"
    2761             :                                         "BEGIN\n"
    2762             :                                         "    RETURN SELECT f.id, f.name, ft.function_type_keyword, fl.language_keyword, c.remark\n"
    2763             :                                         "        FROM sys.functions f\n"
    2764             :                                         "        JOIN sys.schemas s ON f.schema_id = s.id\n"
    2765             :                                         "        JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
    2766             :                                         "        LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
    2767             :                                         "        LEFT OUTER JOIN sys.comments c ON f.id = c.id\n"
    2768             :                                         "        WHERE f.name=functionName AND s.name = schemaName;\n"
    2769             :                                         "END;\n");
    2770             : 
    2771             :                         /* 75_storagemodel.sql not changed but dependencies changed
    2772             :                          * since sys.objects has a new column */
    2773           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2774             :                                         "drop procedure sys.storagemodelinit() cascade;\n"
    2775             :                                         "create procedure sys.storagemodelinit()\n"
    2776             :                                         "begin\n"
    2777             :                                         "    delete from sys.storagemodelinput;\n"
    2778             :                                         "    insert into sys.storagemodelinput\n"
    2779             :                                         "    select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
    2780             :                                         "        -- assume all variable size types contain distinct values\n"
    2781             :                                         "        case when (\"unique\" or \"type\" IN ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
    2782             :                                         "            then \"count\" else 0 end,\n"
    2783             :                                         "        case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
    2784             :                                         "            -- string heaps have a header of 8192\n"
    2785             :                                         "            then cast((heapsize - 8192) / \"count\" as bigint)\n"
    2786             :                                         "        when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
    2787             :                                         "            -- binary data heaps have a header of 32\n"
    2788             :                                         "            then cast((heapsize - 32) / \"count\" as bigint)\n"
    2789             :                                         "        else typewidth end,\n"
    2790             :                                         "        FALSE, case sorted when true then true else false end, \"unique\", TRUE\n"
    2791             :                                         "      from sys.\"storage\";  -- view sys.\"storage\" excludes system tables (as those are not useful to be modeled for storagesize by application users)\n"
    2792             :                                         "    update sys.storagemodelinput\n"
    2793             :                                         "       set reference = TRUE\n"
    2794             :                                         "     where (\"schema\", \"table\", \"column\") in (\n"
    2795             :                                         "        SELECT fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
    2796             :                                         "          FROM    sys.\"keys\" AS fkkey,\n"
    2797             :                                         "            sys.\"objects\" AS fkkeycol,\n"
    2798             :                                         "            sys.\"tables\" AS fktable,\n"
    2799             :                                         "            sys.\"schemas\" AS fkschema\n"
    2800             :                                         "        WHERE fktable.\"id\" = fkkey.\"table_id\"\n"
    2801             :                                         "          AND fkkey.\"id\" = fkkeycol.\"id\"\n"
    2802             :                                         "          AND fkschema.\"id\" = fktable.\"schema_id\"\n"
    2803             :                                         "          AND fkkey.\"rkey\" > -1 );\n"
    2804             :                                         "    update sys.storagemodelinput\n"
    2805             :                                         "       set isacolumn = FALSE\n"
    2806             :                                         "     where (\"schema\", \"table\", \"column\") NOT in (\n"
    2807             :                                         "        SELECT sch.\"name\", tbl.\"name\", col.\"name\"\n"
    2808             :                                         "          FROM sys.\"schemas\" AS sch,\n"
    2809             :                                         "            sys.\"tables\" AS tbl,\n"
    2810             :                                         "            sys.\"columns\" AS col\n"
    2811             :                                         "        WHERE sch.\"id\" = tbl.\"schema_id\"\n"
    2812             :                                         "          AND tbl.\"id\" = col.\"table_id\");\n"
    2813             :                                         "end;\n"
    2814             :                                         "update sys.functions set system = true where name = 'storagemodelinit' and schema_id = 2000;\n");
    2815             : 
    2816             :                         /* 76_dump.sql */
    2817           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2818             :                                         "CREATE VIEW sys.dump_create_roles AS\n"
    2819             :                                         "    SELECT\n"
    2820             :                                         "        'CREATE ROLE ' || sys.dq(name) || ';' stmt FROM sys.auths\n"
    2821             :                                         "    WHERE name NOT IN (SELECT name FROM sys.db_user_info)\n"
    2822             :                                         "    AND grantor <> 0;\n"
    2823             :                                         "CREATE VIEW sys.dump_create_users AS\n"
    2824             :                                         "    SELECT\n"
    2825             :                                         "        'CREATE USER ' ||  sys.dq(ui.name) ||  ' WITH ENCRYPTED PASSWORD ' ||\n"
    2826             :                                         "        sys.sq(sys.password_hash(ui.name)) ||\n"
    2827             :                                         "    ' NAME ' || sys.sq(ui.fullname) ||  ' SCHEMA sys;' stmt\n"
    2828             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    2829             :                                         "    WHERE ui.default_schema = s.id\n"
    2830             :                                         "        AND ui.name <> 'monetdb'\n"
    2831             :                                         "        AND ui.name <> '.snapshot';\n"
    2832             :                                         "CREATE VIEW sys.dump_create_schemas AS\n"
    2833             :                                         "    SELECT\n"
    2834             :                                         "        'CREATE SCHEMA ' ||  sys.dq(s.name) || ifthenelse(a.name <> 'sysadmin', ' AUTHORIZATION ' || a.name, ' ') || ';' stmt\n"
    2835             :                                         "    FROM sys.schemas s, sys.auths a\n"
    2836             :                                         "    WHERE s.authorization = a.id AND s.system = FALSE;\n"
    2837             :                                         "CREATE VIEW sys.dump_add_schemas_to_users AS\n"
    2838             :                                         "    SELECT\n"
    2839             :                                         "        'ALTER USER ' || sys.dq(ui.name) || ' SET SCHEMA ' || sys.dq(s.name) || ';' stmt\n"
    2840             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    2841             :                                         "    WHERE ui.default_schema = s.id\n"
    2842             :                                         "        AND ui.name <> 'monetdb'\n"
    2843             :                                         "        AND ui.name <> '.snapshot'\n"
    2844             :                                         "        AND s.name <> 'sys';\n"
    2845             :                                         "CREATE VIEW sys.dump_grant_user_privileges AS\n"
    2846             :                                         "    SELECT\n"
    2847             :                                         "        'GRANT ' || sys.dq(a2.name) || ' ' || ifthenelse(a1.name = 'public', 'PUBLIC', sys.dq(a1.name)) || ';' stmt\n"
    2848             :                                         "    FROM sys.auths a1, sys.auths a2, sys.user_role ur\n"
    2849             :                                         "    WHERE a1.id = ur.login_id AND a2.id = ur.role_id;\n"
    2850             :                                         "CREATE VIEW sys.dump_table_constraint_type AS\n"
    2851             :                                         "    SELECT\n"
    2852             :                                         "        'ALTER TABLE ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    2853             :                                         "        ' ADD CONSTRAINT ' || sys.DQ(con) || ' '||\n"
    2854             :                                         "        tpe || ' (' || GROUP_CONCAT(sys.DQ(col), ', ') || ');' stmt\n"
    2855             :                                         "    FROM sys.describe_constraints GROUP BY sch, tbl, con, tpe;\n"
    2856             :                                         "CREATE VIEW sys.dump_indices AS\n"
    2857             :                                         "    SELECT\n"
    2858             :                                         "        'CREATE ' || tpe || ' ' ||\n"
    2859             :                                         "        sys.DQ(ind) || ' ON ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    2860             :                                         "        '(' || GROUP_CONCAT(col) || ');' stmt\n"
    2861             :                                         "    FROM sys.describe_indices GROUP BY ind, tpe, sch, tbl;\n"
    2862             :                                         "CREATE VIEW sys.dump_column_defaults AS\n"
    2863             :                                         "    SELECT 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ALTER COLUMN ' || sys.DQ(col) || ' SET DEFAULT ' || def || ';' stmt\n"
    2864             :                                         "    FROM sys.describe_column_defaults;\n"
    2865             :                                         "CREATE VIEW sys.dump_foreign_keys AS\n"
    2866             :                                         "    SELECT\n"
    2867             :                                         "        'ALTER TABLE ' || sys.DQ(fk_s) || '.'|| sys.DQ(fk_t) || ' ADD CONSTRAINT ' || sys.DQ(fk) || ' ' ||\n"
    2868             :                                         "        'FOREIGN KEY(' || GROUP_CONCAT(sys.DQ(fk_c), ',') ||') ' ||\n"
    2869             :                                         "        'REFERENCES ' || sys.DQ(pk_s) || '.' || sys.DQ(pk_t) || '(' || GROUP_CONCAT(sys.DQ(pk_c), ',') || ') ' ||\n"
    2870             :                                         "        'ON DELETE ' || on_delete || ' ON UPDATE ' || on_update ||\n"
    2871             :                                         "        ';' stmt\n"
    2872             :                                         "    FROM sys.describe_foreign_keys GROUP BY fk_s, fk_t, pk_s, pk_t, fk, on_delete, on_update;\n"
    2873             :                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    2874             :                                         "    SELECT\n"
    2875             :                                         "        sys.ALTER_TABLE(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    2876             :                                         "        CASE \n"
    2877             :                                         "            WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    2878             :                                         "            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"
    2879             :                                         "            WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    2880             :                                         "            ELSE '' --'READ ONLY'\n"
    2881             :                                         "        END ||\n"
    2882             :                                         "        CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    2883             :                                         "        ';' stmt\n"
    2884             :                                         "    FROM sys.describe_partition_tables;\n"
    2885             :                                         "CREATE VIEW sys.dump_sequences AS\n"
    2886             :                                         "    SELECT\n"
    2887             :                                         "        'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT ' ||\n"
    2888             :                                         "        CASE WHEN \"s\" <> 0 THEN 'START WITH ' || \"rs\" ELSE '' END ||\n"
    2889             :                                         "        CASE WHEN \"inc\" <> 1 THEN ' INCREMENT BY ' || \"inc\" ELSE '' END ||\n"
    2890             :                                         "        CASE WHEN \"mi\" <> 0 THEN ' MINVALUE ' || \"mi\" ELSE '' END ||\n"
    2891             :                                         "        CASE WHEN \"ma\" <> 0 THEN ' MAXVALUE ' || \"ma\" ELSE '' END ||\n"
    2892             :                                         "        CASE WHEN \"cache\" <> 1 THEN ' CACHE ' || \"cache\" ELSE '' END ||\n"
    2893             :                                         "        CASE WHEN \"cycle\" THEN ' CYCLE' ELSE '' END || ';' stmt\n"
    2894             :                                         "    FROM sys.describe_sequences;\n"
    2895             :                                         "CREATE VIEW sys.dump_start_sequences AS\n"
    2896             :                                         "    SELECT\n"
    2897             :                                         "        'UPDATE sys.sequences seq SET start = ' || s  ||\n"
    2898             :                                         "        ' WHERE name = ' || sys.SQ(seq) ||\n"
    2899             :                                         "        ' AND schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = ' || sys.SQ(sch) || ');' stmt\n"
    2900             :                                         "    FROM sys.describe_sequences;\n"
    2901             :                                         "CREATE VIEW sys.dump_functions AS\n"
    2902             :                                         "    SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt FROM sys.describe_functions f;\n"
    2903             :                                         "CREATE VIEW sys.dump_tables AS\n"
    2904             :                                         "    SELECT\n"
    2905             :                                         "        t.o o,\n"
    2906             :                                         "        CASE\n"
    2907             :                                         "            WHEN t.typ <> 'VIEW' THEN\n"
    2908             :                                         "                'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    2909             :                                         "            ELSE\n"
    2910             :                                         "                t.opt\n"
    2911             :                                         "        END stmt\n"
    2912             :                                         "    FROM sys.describe_tables t;\n"
    2913             :                                         "CREATE VIEW sys.dump_triggers AS\n"
    2914             :                                         "    SELECT sys.schema_guard(sch, tab, def) stmt FROM sys.describe_triggers;\n"
    2915             :                                         "CREATE VIEW sys.dump_comments AS\n"
    2916             :                                         "    SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    2917             :                                         "CREATE VIEW sys.dump_user_defined_types AS\n"
    2918             :                                         "        SELECT 'CREATE TYPE ' || sys.FQN(sch, sql_tpe) || ' EXTERNAL NAME ' || sys.DQ(ext_tpe) || ';' stmt FROM sys.describe_user_defined_types;\n"
    2919             :                                         "CREATE VIEW sys.dump_privileges AS\n"
    2920             :                                         "    SELECT\n"
    2921             :                                         "        'INSERT INTO sys.privileges VALUES (' ||\n"
    2922             :                                         "            CASE\n"
    2923             :                                         "                WHEN dp.o_tpe = 'GLOBAL' THEN\n"
    2924             :                                         "                    '0,'\n"
    2925             :                                         "                WHEN dp.o_tpe = 'TABLE' THEN\n"
    2926             :                                         "                    '(SELECT t.id FROM sys.schemas s, sys.tables t WHERE s.id = t.schema_id' ||\n"
    2927             :                                         "                        ' AND s.name || ''.'' || t.name =' || sys.SQ(dp.o_nme) || '),'\n"
    2928             :                                         "                WHEN dp.o_tpe = 'COLUMN' THEN\n"
    2929             :                                         "                    '(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"
    2930             :                                         "                        ' AND s.name || ''.'' || t.name || ''.'' || c.name =' || sys.SQ(dp.o_nme) || '),'\n"
    2931             :                                         "                ELSE -- FUNCTION-LIKE\n"
    2932             :                                         "                    '(SELECT fqn.id FROM sys.fully_qualified_functions fqn WHERE' ||\n"
    2933             :                                         "                        ' fqn.nme = ' || sys.SQ(dp.o_nme) || ' AND fqn.tpe = ' || sys.SQ(dp.o_tpe) || '),'\n"
    2934             :                                         "            END ||\n"
    2935             :                                         "            '(SELECT id FROM sys.auths a WHERE a.name = ' || sys.SQ(dp.a_nme) || '),' ||\n"
    2936             :                                         "            '(SELECT pc.privilege_code_id FROM sys.privilege_codes pc WHERE pc.privilege_code_name = ' || sys.SQ(p_nme) || '),'\n"
    2937             :                                         "            '(SELECT id FROM sys.auths g WHERE g.name = ' || sys.SQ(dp.g_nme) || '),' ||\n"
    2938             :                                         "            dp.grantable ||\n"
    2939             :                                         "        ');' stmt\n"
    2940             :                                         "    FROM sys.describe_privileges dp;\n"
    2941             :                                         "CREATE PROCEDURE sys.EVAL(stmt STRING) EXTERNAL NAME sql.eval;\n"
    2942             :                                         "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"
    2943             :                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    2944             :                                         "BEGIN\n"
    2945             :                                         "    RETURN\n"
    2946             :                                         "        CASE\n"
    2947             :                                         "            WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    2948             :                                         "                'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    2949             :                                         "            ELSE\n"
    2950             :                                         "                'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    2951             :                                         "        END;\n"
    2952             :                                         "END;\n"
    2953             :                                         "CREATE TABLE sys.dump_statements(o INT, s STRING);\n"
    2954             :                                         "CREATE PROCEDURE sys._dump_table_data(sch STRING, tbl STRING) BEGIN\n"
    2955             :                                         "    DECLARE k INT;\n"
    2956             :                                         "    SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    2957             :                                         "    IF k IS NOT NULL THEN\n"
    2958             :                                         "        DECLARE cname STRING;\n"
    2959             :                                         "        DECLARE ctype STRING;\n"
    2960             :                                         "        SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    2961             :                                         "        SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    2962             :                                         "        DECLARE COPY_INTO_STMT STRING;\n"
    2963             :                                         "        DECLARE _cnt INT;\n"
    2964             :                                         "        SET _cnt = (SELECT MIN(s.count) FROM sys.storage() s WHERE s.schema = sch AND s.table = tbl);\n"
    2965             :                                         "        IF _cnt > 0 THEN\n"
    2966             :                                         "            SET COPY_INTO_STMT = 'COPY ' || _cnt ||  ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    2967             :                                         "            DECLARE SELECT_DATA_STMT STRING;\n"
    2968             :                                         "            SET SELECT_DATA_STMT = 'SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    2969             :                                         "            DECLARE M INT;\n"
    2970             :                                         "            SET M = (SELECT MAX(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    2971             :                                         "            WHILE (k < M) DO\n"
    2972             :                                         "                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"
    2973             :                                         "                SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    2974             :                                         "                SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    2975             :                                         "                SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    2976             :                                         "                SET SELECT_DATA_STMT = SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype);\n"
    2977             :                                         "            END WHILE;\n"
    2978             :                                         "            SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    2979             :                                         "            SET SELECT_DATA_STMT =  SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl);\n"
    2980             :                                         "            insert into sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    2981             :                                         "            CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    2982             :                                         "        END IF;\n"
    2983             :                                         "    END IF;\n"
    2984             :                                         "END;\n"
    2985             :                                         "CREATE PROCEDURE sys.dump_table_data() BEGIN\n"
    2986             :                                         "    DECLARE i INT;\n"
    2987             :                                         "    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"
    2988             :                                         "    IF i IS NOT NULL THEN\n"
    2989             :                                         "        DECLARE M INT;\n"
    2990             :                                         "        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"
    2991             :                                         "        DECLARE sch STRING;\n"
    2992             :                                         "        DECLARE tbl STRING;\n"
    2993             :                                         "        WHILE i < M DO\n"
    2994             :                                         "            set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    2995             :                                         "            set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    2996             :                                         "            CALL sys._dump_table_data(sch, tbl);\n"
    2997             :                                         "            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"
    2998             :                                         "        END WHILE;\n"
    2999             :                                         "        set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3000             :                                         "        set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3001             :                                         "        CALL sys._dump_table_data(sch, tbl);\n"
    3002             :                                         "    END IF;\n"
    3003             :                                         "END;\n"
    3004             :                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    3005             :                                         "BEGIN\n"
    3006             :                                         "    SET SCHEMA sys;\n"
    3007             :                                         "    TRUNCATE sys.dump_statements;\n"
    3008             :                                         "    INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    3009             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'SET SCHEMA \"sys\";');\n"
    3010             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    3011             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    3012             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    3013             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    3014             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    3015             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    3016             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    3017             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    3018             :                                         "    --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    3019             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    3020             :                                         "    FROM (\n"
    3021             :                                         "            SELECT * FROM sys.dump_functions f\n"
    3022             :                                         "            UNION\n"
    3023             :                                         "            SELECT * FROM sys.dump_tables t\n"
    3024             :                                         "        ) AS stmts(o, s);\n"
    3025             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    3026             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    3027             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    3028             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    3029             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    3030             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    3031             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    3032             :                                         "    --We are dumping ALL privileges so we need to erase existing privileges on the receiving side;\n"
    3033             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'TRUNCATE sys.privileges;');\n"
    3034             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_privileges;\n"
    3035             :                                         "    IF NOT DESCRIBE THEN\n"
    3036             :                                         "        CALL sys.dump_table_data();\n"
    3037             :                                         "    END IF;\n"
    3038             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    3039             :                                         "    RETURN sys.dump_statements;\n"
    3040             :                                         "END;\n");
    3041             : 
    3042             :                         // Set the system flag for the new dump and describe SQL objects.
    3043           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3044             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3045             :                                         "    system <> true AND\n"
    3046             :                                         "    schema_id = 2000 AND\n"
    3047             :                                         "    type = %d AND\n"
    3048             :                                         "    name in (\n"
    3049             :                                         "        'describe_columns',\n"
    3050             :                                         "        'describe_function',\n"
    3051             :                                         "        'describe_table',\n"
    3052             :                                         "        'dump_database'\n"
    3053             :                                         "    );\n",
    3054             :                                         F_UNION);
    3055           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3056             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3057             :                                         "    system <> true AND\n"
    3058             :                                         "    schema_id = 2000 AND\n"
    3059             :                                         "    type = %d AND\n"
    3060             :                                         "    name in (\n"
    3061             :                                         "        'alter_table',\n"
    3062             :                                         "        'describe_type',\n"
    3063             :                                         "        'dq',\n"
    3064             :                                         "        'esc',\n"
    3065             :                                         "        'fqn',\n"
    3066             :                                         "        'get_merge_table_partition_expressions',\n"
    3067             :                                         "        'get_remote_table_expressions',\n"
    3068             :                                         "        'prepare_esc',\n"
    3069             :                                         "        'replace_first',\n"
    3070             :                                         "        'schema_guard',\n"
    3071             :                                         "        'sq'\n"
    3072             :                                         "    );\n",
    3073             :                                         F_FUNC);
    3074           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3075             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3076             :                                         "    system <> true AND\n"
    3077             :                                         "    schema_id = 2000 AND\n"
    3078             :                                         "    type = %d AND\n"
    3079             :                                         "    name in (\n"
    3080             :                                         "        '_dump_table_data',\n"
    3081             :                                         "        'dump_table_data',\n"
    3082             :                                         "        'eval'\n"
    3083             :                                         "    );\n",
    3084             :                                         F_PROC);
    3085           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3086             :                                         "UPDATE sys._tables SET system = true WHERE\n"
    3087             :                                         "    system <> true AND\n"
    3088             :                                         "    schema_id = 2000 AND\n"
    3089             :                                         "    type = %d AND\n"
    3090             :                                         "    name = 'dump_statements';\n",
    3091             :                                         (int) tt_table);
    3092           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3093             :                                         "UPDATE sys._tables SET system = true WHERE\n"
    3094             :                                         "    system <> true AND\n"
    3095             :                                         "    schema_id = 2000 AND\n"
    3096             :                                         "    type = %d AND\n"
    3097             :                                         "    name in (\n"
    3098             :                                         "        'describe_column_defaults',\n"
    3099             :                                         "        'describe_comments',\n"
    3100             :                                         "        'describe_constraints',\n"
    3101             :                                         "        'describe_foreign_keys',\n"
    3102             :                                         "        'describe_functions',\n"
    3103             :                                         "        'describe_indices',\n"
    3104             :                                         "        'describe_partition_tables',\n"
    3105             :                                         "        'describe_privileges',\n"
    3106             :                                         "        'describe_sequences',\n"
    3107             :                                         "        'describe_tables',\n"
    3108             :                                         "        'describe_triggers',\n"
    3109             :                                         "        'describe_user_defined_types',\n"
    3110             :                                         "        'dump_add_schemas_to_users',\n"
    3111             :                                         "        'dump_column_defaults',\n"
    3112             :                                         "        'dump_comments',\n"
    3113             :                                         "        'dump_create_roles',\n"
    3114             :                                         "        'dump_create_schemas',\n"
    3115             :                                         "        'dump_create_users',\n"
    3116             :                                         "        'dump_foreign_keys',\n"
    3117             :                                         "        'dump_functions',\n"
    3118             :                                         "        'dump_grant_user_privileges',\n"
    3119             :                                         "        'dump_indices',\n"
    3120             :                                         "        'dump_partition_tables',\n"
    3121             :                                         "        'dump_privileges',\n"
    3122             :                                         "        'dump_sequences',\n"
    3123             :                                         "        'dump_start_sequences',\n"
    3124             :                                         "        'dump_statements',\n"
    3125             :                                         "        'dump_table_constraint_type',\n"
    3126             :                                         "        'dump_tables',\n"
    3127             :                                         "        'dump_triggers',\n"
    3128             :                                         "        'dump_user_defined_types',\n"
    3129             :                                         "        'fully_qualified_functions'\n"
    3130             :                                         "    );\n",
    3131             :                                         (int) tt_view);
    3132             : 
    3133             :                         /* scoping2 branch changes, the 'users' view has to be re-created because of the 'schema_path' addition on 'db_user_info' table
    3134             :                            However 'dependency_schemas_on_users' has a dependency on 'users', so it has to be re-created as well */
    3135           0 :                         t = mvc_bind_table(sql, s, "users");
    3136           0 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    3137           0 :                         t = mvc_bind_table(sql, s, "dependency_schemas_on_users");
    3138           0 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    3139           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    3140             :                                         "DROP VIEW sys.dependency_schemas_on_users CASCADE;\n"
    3141             :                                         "DROP VIEW sys.users CASCADE;\n"
    3142             : 
    3143             :                                         "ALTER TABLE sys.db_user_info ADD COLUMN schema_path CLOB;\n"
    3144             :                                         "UPDATE sys.db_user_info SET schema_path = '\"sys\"';\n"
    3145             : 
    3146             :                                         "CREATE VIEW sys.users AS\n"
    3147             :                                         "SELECT u.\"name\" AS \"name\", ui.\"fullname\", ui.\"default_schema\", ui.\"schema_path\"\n"
    3148             :                                         " FROM sys.db_users() AS u\n"
    3149             :                                         " LEFT JOIN \"sys\".\"db_user_info\" AS ui ON u.\"name\" = ui.\"name\";\n"
    3150             :                                         "CREATE VIEW sys.dependency_schemas_on_users AS\n"
    3151             :                                         " SELECT s.id AS schema_id, s.name AS schema_name, u.name AS user_name, CAST(6 AS smallint) AS depend_type\n"
    3152             :                                         " FROM sys.users AS u, sys.schemas AS s\n"
    3153             :                                         " WHERE u.default_schema = s.id\n"
    3154             :                                         " ORDER BY s.name, u.name;\n"
    3155             :                                         "GRANT SELECT ON sys.dependency_schemas_on_users TO PUBLIC;\n"
    3156             :                                         "update sys._tables set system = true where system <> true and name in ('users','dependency_schemas_on_users')"
    3157             :                                         " and schema_id = 2000 and type = %d;\n", (int) tt_view);
    3158             : 
    3159           0 :                         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    3160             : 
    3161           0 :                         assert(pos < bufsize);
    3162           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    3163           0 :                         fflush(stdout);
    3164           0 :                         if ((err = SQLstatementIntern(c, buf, "update", true, false, NULL)) != MAL_SUCCEED)
    3165           0 :                                 goto bailout;
    3166             : 
    3167           0 :                         pos = snprintf(buf, bufsize,
    3168             :                                         "ALTER TABLE sys.keywords SET READ ONLY;\n"
    3169             :                                         "ALTER TABLE sys.table_types SET READ ONLY;\n"
    3170             :                                         "ALTER TABLE sys.function_types SET READ ONLY;\n");
    3171           0 :                         assert(pos < bufsize);
    3172           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    3173           0 :                         fflush(stdout);
    3174           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    3175             :                 }
    3176             :         } else {
    3177           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3178             :         }
    3179             : 
    3180           0 : bailout:
    3181          99 :         BBPreclaim(b);
    3182          99 :         if (output)
    3183          99 :                 res_table_destroy(output);
    3184          99 :         GDKfree(buf);
    3185          99 :         return err;             /* usually MAL_SUCCEED */
    3186             : }
    3187             : 
    3188             : /* upgrades after Jul2021_3 build */
    3189             : static str
    3190          99 : sql_update_jul2021_5(Client c, mvc *sql)
    3191             : {
    3192          99 :         size_t bufsize = 65536, pos = 0;
    3193          99 :         char *buf = NULL, *err = NULL;
    3194          99 :         res_table *output = NULL;
    3195          99 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    3196          99 :         sql_table *t;
    3197             : 
    3198          99 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    3199           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3200             : 
    3201             :         /* if the string 'partition of merge table' is not in the sys.ids
    3202             :          * query, upgrade */
    3203          99 :         pos += snprintf(buf + pos, bufsize - pos,
    3204             :                                         "select query from sys._tables where name = 'ids' and schema_id = 2000 and query like '%%partition of merge table%%';\n");
    3205          99 :         assert(pos < bufsize);
    3206          99 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)) == NULL) {
    3207          99 :                 BAT *b;
    3208          99 :                 if ((b = BATdescriptor(output->cols[0].b))) {
    3209          99 :                         if (BATcount(b) == 0) {
    3210             :                                 /* 21_dependency_views.sql */
    3211           0 :                                 t = mvc_bind_table(sql, s, "ids");
    3212           0 :                                 t->system = 0;       /* make it non-system else the drop view will fail */
    3213           0 :                                 t = mvc_bind_table(sql, s, "dependencies_vw");
    3214           0 :                                 t->system = 0;       /* make it non-system else the drop view will fail */
    3215           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3216             :                                                                 "drop view sys.dependencies_vw cascade;\n"
    3217             :                                                                 "drop view sys.ids cascade;\n");
    3218           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3219             :                                                                 "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table) AS\n"
    3220             :                                                                 "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"
    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, 'schema', 'sys.schemas' FROM sys.schemas UNION ALL\n"
    3222             :                                                                 "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"
    3223             :                                                                 "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"
    3224             :                                                                 "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"
    3225             :                                                                 "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"
    3226             :                                                                 "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"
    3227             :                                                                 "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"
    3228             :                                                                 "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"
    3229             :                                                                 "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"
    3230             :                                                                 "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"
    3231             :                                                                 "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"
    3232             :                                                                 "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"
    3233             :                                                                 "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"
    3234             :                                                                 "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"
    3235             :                                                                 "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"
    3236             :                                                                 "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"
    3237             :                                                                 " ORDER BY id;\n"
    3238             :                                                                 "GRANT SELECT ON sys.ids TO PUBLIC;\n");
    3239           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3240             :                                                                 "CREATE VIEW sys.dependencies_vw AS\n"
    3241             :                                                                 "SELECT d.id, i1.obj_type, i1.name,\n"
    3242             :                                                                 "       d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
    3243             :                                                                 "       d.depend_type, dt.dependency_type_name\n"
    3244             :                                                                 "  FROM sys.dependencies d\n"
    3245             :                                                                 "  JOIN sys.ids i1 ON d.id = i1.id\n"
    3246             :                                                                 "  JOIN sys.ids i2 ON d.depend_id = i2.id\n"
    3247             :                                                                 "  JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
    3248             :                                                                 " ORDER BY id, depend_id;\n"
    3249             :                                                                 "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n");
    3250           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3251             :                                                                 "UPDATE sys._tables SET system = true WHERE name in ('ids', 'dependencies_vw') AND schema_id = 2000;\n");
    3252             : 
    3253           0 :                                 assert(pos < bufsize);
    3254           0 :                                 printf("Running database upgrade commands:\n%s\n", buf);
    3255           0 :                                 fflush(stdout);
    3256           0 :                                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    3257             :                         }
    3258          99 :                         BBPunfix(b->batCacheid);
    3259             :                 } else {
    3260           0 :                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3261             :                 }
    3262          99 :                 res_table_destroy(output);
    3263             :         }
    3264             : 
    3265          99 :         GDKfree(buf);
    3266          99 :         return err;             /* usually MAL_SUCCEED */
    3267             : }
    3268             : 
    3269             : static str
    3270          99 : sql_update_jan2022(Client c, mvc *sql)
    3271             : {
    3272          99 :         sql_subtype tp;
    3273          99 :         size_t bufsize = 65536, pos = 0;
    3274          99 :         char *buf = NULL, *err = NULL;
    3275          99 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    3276          99 :         sql_table *t;
    3277             : 
    3278             :         /* this bit of code is to upgrade from a Jan2022 RC to the Jan2022 release */
    3279          99 :         sql_allocator *old_sa = sql->sa;
    3280          99 :         if ((sql->sa = sa_create(sql->pa)) != NULL) {
    3281          99 :                 list *l;
    3282          99 :                 if ((l = sa_list(sql->sa)) != NULL) {
    3283          99 :                         sql_find_subtype(&tp, "varchar", 0, 0);
    3284          99 :                         list_append(l, &tp);
    3285          99 :                         list_append(l, &tp);
    3286          99 :                         list_append(l, &tp);
    3287          99 :                         if (sql_bind_func_(sql, s->base.name, "strimp_create", l, F_PROC, true)) {
    3288             :                                 /* do the upgrade by removing the two functions */
    3289           0 :                                 const char *query =
    3290             :                                         "drop filter function sys.strimp_filter(string, string) cascade;\n"
    3291             :                                         "drop procedure sys.strimp_create(string, string, string) cascade;\n";
    3292           0 :                                 printf("Running database upgrade commands:\n%s\n", query);
    3293           0 :                                 fflush(stdout);
    3294           0 :                                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    3295             :                         }
    3296          99 :                         sql->session->status = 0; /* if the function was not found clean the error */
    3297          99 :                         sql->errstr[0] = '\0';
    3298             :                 }
    3299          99 :                 sa_destroy(sql->sa);
    3300          99 :                 if (err)
    3301             :                         return err;
    3302             :         }
    3303          99 :         sql->sa = old_sa;
    3304             : 
    3305          99 :         sql_find_subtype(&tp, "bigint", 0, 0);
    3306          99 :         if (!sql_bind_func(sql, s->base.name, "epoch", &tp, NULL, F_FUNC, true)) {
    3307          99 :                 sql->session->status = 0; /* if the function was not found clean the error */
    3308          99 :                 sql->errstr[0] = '\0';
    3309             :                 /* nothing to do */
    3310          99 :                 return NULL;
    3311             :         }
    3312             : 
    3313           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    3314           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3315             : 
    3316             :         /* sys.epoch_ms now returns a decimal(18,3) */
    3317           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3318             :                                         "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");
    3319             : 
    3320             :         /* 16_tracelog */
    3321           0 :         t = mvc_bind_table(sql, s, "tracelog");
    3322           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    3323           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3324             :                         "drop view sys.tracelog cascade;\n"
    3325             :                         "drop function sys.tracelog() cascade;\n"
    3326             :                         "create function sys.tracelog()\n"
    3327             :                         " returns table (\n"
    3328             :                         "  ticks bigint, -- time in microseconds\n"
    3329             :                         "  stmt string,  -- actual statement executed\n"
    3330             :                         "  event string  -- profiler event executed\n"
    3331             :                         " )\n"
    3332             :                         " external name sql.dump_trace;\n"
    3333             :                         "create view sys.tracelog as select * from sys.tracelog();\n"
    3334             :                         "update sys._tables set system = true where system <> true and schema_id = 2000"
    3335             :                         " and name = 'tracelog';\n"
    3336             :                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    3337             :                         " and name = 'tracelog' and type = %d;\n", (int) F_UNION);
    3338             : 
    3339             :         /* 17_temporal.sql */
    3340           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3341             :                                         "drop function sys.epoch(bigint) cascade;\n");
    3342           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3343             :                                         "create function sys.epoch(sec DECIMAL(18,3)) "
    3344             :                                         "returns TIMESTAMP WITH TIME ZONE\n"
    3345             :                                         "external name mtime.epoch;\n"
    3346             :                                         "grant execute on function sys.epoch (DECIMAL(18,3)) to public;\n"
    3347             :                                         "update sys.functions set system = true where system <> true and name in ('epoch') and schema_id = 2000 and type = %d;\n", F_FUNC);
    3348             : 
    3349             :         /* 25_debug.sql */
    3350           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3351             :                                         "drop function sys.malfunctions() cascade;\n"
    3352             :                                         "create function sys.malfunctions()\n"
    3353             :                                         " returns table(\"module\" string, \"function\" string, \"signature\" string, \"address\" string, \"comment\" string)\n"
    3354             :                                         " external name \"manual\".\"functions\";\n"
    3355             :                                         "create view sys.malfunctions as select * from sys.malfunctions();\n"
    3356             :                                         "update sys._tables set system = true where system <> true and schema_id = 2000"
    3357             :                                         " and name = 'malfunctions';\n"
    3358             :                                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    3359             :                                         " and name = 'malfunctions';\n");
    3360             : 
    3361             :         /* 21_dependency_views.sql */
    3362           0 :         t = mvc_bind_table(sql, s, "ids");
    3363           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    3364           0 :         t = mvc_bind_table(sql, s, "dependencies_vw");
    3365           0 :         t->system = 0;       /* make it non-system else the drop view will fail */
    3366           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3367             :                                         "drop view sys.dependencies_vw cascade;\n" /* depends on sys.ids */
    3368             :                                         "drop view sys.ids cascade;\n"
    3369             :                                         "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table, system) AS\n"
    3370             :                                         "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"
    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, ifthenelse(system, 'system schema', 'schema'), 'sys.schemas', system FROM sys.schemas UNION ALL\n"
    3372             :                                         "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"
    3373             :                                         "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"
    3374             :                                         "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"
    3375             :                                         "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"
    3376             :                                         "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"
    3377             :                                         "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"
    3378             :                                         "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"
    3379             :                                         "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"
    3380             :                                         "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"
    3381             :                                         "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"
    3382             :                                         "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"
    3383             :                                         "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"
    3384             :                                         "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"
    3385             :                                         "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"
    3386             :                                         " ORDER BY id;\n"
    3387             :                                         "GRANT SELECT ON sys.ids TO PUBLIC;\n");
    3388           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3389             :                                         "CREATE VIEW sys.dependencies_vw AS\n"
    3390             :                                         "SELECT d.id, i1.obj_type, i1.name,\n"
    3391             :                                         "       d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
    3392             :                                         "       d.depend_type, dt.dependency_type_name\n"
    3393             :                                         "  FROM sys.dependencies d\n"
    3394             :                                         "  JOIN sys.ids i1 ON d.id = i1.id\n"
    3395             :                                         "  JOIN sys.ids i2 ON d.depend_id = i2.id\n"
    3396             :                                         "  JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
    3397             :                                         " ORDER BY id, depend_id;\n"
    3398             :                                         "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n");
    3399           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3400             :                                         "UPDATE sys._tables SET system = true WHERE name in ('ids', 'dependencies_vw') AND schema_id = 2000;\n");
    3401             : 
    3402             :         /* 52_describe.sql; but we need to drop most everything from
    3403             :          * 76_dump.sql first */
    3404           0 :         t = mvc_bind_table(sql, s, "describe_comments");
    3405           0 :         t->system = 0;
    3406           0 :         t = mvc_bind_table(sql, s, "describe_constraints");
    3407           0 :         t->system = 0;
    3408           0 :         t = mvc_bind_table(sql, s, "describe_functions");
    3409           0 :         t->system = 0;
    3410           0 :         t = mvc_bind_table(sql, s, "describe_partition_tables");
    3411           0 :         t->system = 0;
    3412           0 :         t = mvc_bind_table(sql, s, "describe_privileges");
    3413           0 :         t->system = 0;
    3414           0 :         t = mvc_bind_table(sql, s, "describe_sequences");
    3415           0 :         t->system = 0;
    3416           0 :         t = mvc_bind_table(sql, s, "describe_tables");
    3417           0 :         t->system = 0;
    3418           0 :         t = mvc_bind_table(sql, s, "dump_add_schemas_to_users");
    3419           0 :         t->system = 0;
    3420           0 :         t = mvc_bind_table(sql, s, "dump_column_defaults");
    3421           0 :         t->system = 0;
    3422           0 :         t = mvc_bind_table(sql, s, "dump_comments");
    3423           0 :         t->system = 0;
    3424           0 :         t = mvc_bind_table(sql, s, "dump_create_roles");
    3425           0 :         t->system = 0;
    3426           0 :         t = mvc_bind_table(sql, s, "dump_create_schemas");
    3427           0 :         t->system = 0;
    3428           0 :         t = mvc_bind_table(sql, s, "dump_create_users");
    3429           0 :         t->system = 0;
    3430           0 :         t = mvc_bind_table(sql, s, "dump_foreign_keys");
    3431           0 :         t->system = 0;
    3432           0 :         t = mvc_bind_table(sql, s, "dump_functions");
    3433           0 :         t->system = 0;
    3434           0 :         t = mvc_bind_table(sql, s, "dump_grant_user_privileges");
    3435           0 :         t->system = 0;
    3436           0 :         t = mvc_bind_table(sql, s, "dump_indices");
    3437           0 :         t->system = 0;
    3438           0 :         t = mvc_bind_table(sql, s, "dump_partition_tables");
    3439           0 :         t->system = 0;
    3440           0 :         t = mvc_bind_table(sql, s, "dump_privileges");
    3441           0 :         t->system = 0;
    3442           0 :         t = mvc_bind_table(sql, s, "dump_sequences");
    3443           0 :         t->system = 0;
    3444           0 :         t = mvc_bind_table(sql, s, "dump_start_sequences");
    3445           0 :         t->system = 0;
    3446           0 :         t = mvc_bind_table(sql, s, "dump_table_constraint_type");
    3447           0 :         t->system = 0;
    3448           0 :         t = mvc_bind_table(sql, s, "dump_tables");
    3449           0 :         t->system = 0;
    3450           0 :         t = mvc_bind_table(sql, s, "dump_triggers");
    3451           0 :         t->system = 0;
    3452           0 :         t = mvc_bind_table(sql, s, "dump_user_defined_types");
    3453           0 :         t->system = 0;
    3454           0 :         t = mvc_bind_table(sql, s, "fully_qualified_functions");
    3455           0 :         t->system = 0;
    3456           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3457             :                                         /* drop dependant stuff from 76_dump.sql */
    3458             :                                         "drop function sys.dump_database(boolean) cascade;\n"
    3459             :                                         "drop procedure sys.dump_table_data() cascade;\n"
    3460             :                                         "drop procedure sys._dump_table_data(string, string) cascade;\n"
    3461             :                                         "drop function sys.prepare_esc(string, string) cascade;\n"
    3462             :                                         "drop function sys.esc(string) cascade;\n"
    3463             :                                         "drop view sys.dump_privileges cascade;\n"
    3464             :                                         "drop view sys.dump_user_defined_types cascade;\n"
    3465             :                                         "drop view sys.dump_comments cascade;\n"
    3466             :                                         "drop view sys.dump_triggers cascade;\n"
    3467             :                                         "drop view sys.dump_tables cascade;\n"
    3468             :                                         "drop view sys.dump_functions cascade;\n"
    3469             :                                         "drop view sys.dump_start_sequences cascade;\n"
    3470             :                                         "drop view sys.dump_sequences cascade;\n"
    3471             :                                         "drop view sys.dump_partition_tables cascade;\n"
    3472             :                                         "drop view sys.dump_foreign_keys cascade;\n"
    3473             :                                         "drop view sys.dump_column_defaults cascade;\n"
    3474             :                                         "drop view sys.dump_indices cascade;\n"
    3475             :                                         "drop view sys.dump_table_constraint_type cascade;\n"
    3476             :                                         "drop view sys.dump_grant_user_privileges cascade;\n"
    3477             :                                         "drop view sys.dump_add_schemas_to_users cascade;\n"
    3478             :                                         "drop view sys.dump_create_schemas cascade;\n"
    3479             :                                         "drop view sys.dump_create_users cascade;\n"
    3480             :                                         "drop view sys.dump_create_roles cascade;\n"
    3481             : 
    3482             :                                         "drop view sys.describe_functions cascade;\n"
    3483             :                                         "drop view sys.describe_partition_tables cascade;\n"
    3484             :                                         "drop view sys.describe_privileges cascade;\n"
    3485             :                                         "drop view sys.fully_qualified_functions cascade;\n"
    3486             :                                         "drop view sys.describe_comments cascade;\n"
    3487             :                                         "drop view sys.describe_tables cascade;\n"
    3488             :                                         "drop view sys.describe_sequences cascade;\n"
    3489             :                                         "drop function sys.schema_guard(string, string, string) cascade;\n"
    3490             :                                         "drop function sys.get_remote_table_expressions(string, string) cascade;\n"
    3491             :                                         "drop function sys.get_merge_table_partition_expressions(int) cascade;\n"
    3492             :                                         "drop view sys.describe_constraints cascade;\n"
    3493             :                                         "drop function sys.alter_table(string, string) cascade;\n"
    3494             :                                         "drop function sys.FQN(string, string) cascade;\n"
    3495             :                                         "drop function sys.sq(string) cascade;\n");
    3496           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3497             :                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN '''' || sys.replace(s,'''','''''') || ''''; END;\n"
    3498             :                                         "CREATE FUNCTION sys.FQN(s STRING, t STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(s,'\"','\"\"') || '\".\"' || sys.replace(t,'\"','\"\"') || '\"'; END;\n"
    3499             :                                         "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
    3500             :                                         "RETURN\n"
    3501             :                                         "    SELECT sys.replace_first(stmt, '(\\\\s*\"?' || sch ||  '\"?\\\\s*\\\\.|)\\\\s*\"?' || nme || '\"?\\\\s*', ' ' || sys.FQN(sch, nme) || ' ', 'imsx');\n"
    3502             :                                         "END;\n"
    3503             :                                         "CREATE VIEW sys.describe_constraints AS\n"
    3504             :                                         "  SELECT\n"
    3505             :                                         "          s.name sch,\n"
    3506             :                                         "          t.name tbl,\n"
    3507             :                                         "          kc.name col,\n"
    3508             :                                         "          k.name con,\n"
    3509             :                                         "          CASE k.type WHEN 0 THEN 'PRIMARY KEY' WHEN 1 THEN 'UNIQUE' END tpe\n"
    3510             :                                         "  FROM sys.schemas s, sys._tables t, sys.objects kc, sys.keys k\n"
    3511             :                                         "  WHERE kc.id = k.id\n"
    3512             :                                         "          AND k.table_id = t.id\n"
    3513             :                                         "          AND s.id = t.schema_id\n"
    3514             :                                         "          AND t.system = FALSE\n"
    3515             :                                         "          AND k.type in (0, 1);\n"
    3516             :                                         "CREATE FUNCTION sys.get_merge_table_partition_expressions(tid INT) RETURNS STRING\n"
    3517             :                                         "BEGIN\n"
    3518             :                                         "  RETURN\n"
    3519             :                                         "          SELECT\n"
    3520             :                                         "                  CASE WHEN tp.table_id IS NOT NULL THEN\n"
    3521             :                                         "                          ' PARTITION BY ' ||\n"
    3522             :                                         "                          ifthenelse(bit_and(tp.type, 2) = 2, 'VALUES ', 'RANGE ') ||\n"
    3523             :                                         "                          CASE\n"
    3524             :                                         "                                  WHEN bit_and(tp.type, 4) = 4\n"
    3525             :                                         "                                  THEN 'ON ' || '(' || (SELECT sys.DQ(c.name) || ')' FROM sys.columns c WHERE c.id = tp.column_id)\n"
    3526             :                                         "                                  ELSE 'USING ' || '(' || tp.expression || ')'\n"
    3527             :                                         "                          END\n"
    3528             :                                         "                  ELSE\n"
    3529             :                                         "                          ''\n"
    3530             :                                         "                  END\n"
    3531             :                                         "          FROM (VALUES (tid)) t(id) LEFT JOIN sys.table_partitions tp ON t.id = tp.table_id;\n"
    3532             :                                         "END;\n"
    3533             :                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    3534             :                                         "  RETURN SELECT ' ON ' || sys.SQ(uri) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(\"hash\") FROM sys.remote_table_credentials(s ||'.' || t);\n"
    3535             :                                         "END;\n"
    3536             :                                         "CREATE VIEW sys.describe_tables AS\n"
    3537             :                                         "  SELECT\n"
    3538             :                                         "          t.id o,\n"
    3539             :                                         "          s.name sch,\n"
    3540             :                                         "          t.name tab,\n"
    3541             :                                         "          ts.table_type_name typ,\n"
    3542             :                                         "          (SELECT\n"
    3543             :                                         "                  ' (' ||\n"
    3544             :                                         "                  GROUP_CONCAT(\n"
    3545             :                                         "                          sys.DQ(c.name) || ' ' ||\n"
    3546             :                                         "                          sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    3547             :                                         "                          ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    3548             :                                         "                  , ', ') || ')'\n"
    3549             :                                         "          FROM sys._columns c\n"
    3550             :                                         "          WHERE c.table_id = t.id) col,\n"
    3551             :                                         "          CASE ts.table_type_name\n"
    3552             :                                         "                  WHEN 'REMOTE TABLE' THEN\n"
    3553             :                                         "                          sys.get_remote_table_expressions(s.name, t.name)\n"
    3554             :                                         "                  WHEN 'MERGE TABLE' THEN\n"
    3555             :                                         "                          sys.get_merge_table_partition_expressions(t.id)\n"
    3556             :                                         "                  WHEN 'VIEW' THEN\n"
    3557             :                                         "                          sys.schema_guard(s.name, t.name, t.query)\n"
    3558             :                                         "                  ELSE\n"
    3559             :                                         "                          ''\n"
    3560             :                                         "          END opt\n"
    3561             :                                         "  FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    3562             :                                         "  WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE')\n"
    3563             :                                         "          AND t.system = FALSE\n"
    3564             :                                         "          AND s.id = t.schema_id\n"
    3565             :                                         "          AND ts.table_type_id = t.type\n"
    3566             :                                         "          AND s.name <> 'tmp';\n"
    3567             :                                         "CREATE VIEW sys.fully_qualified_functions AS\n"
    3568             :                                         "  WITH fqn(id, tpe, sig, num) AS\n"
    3569             :                                         "  (\n"
    3570             :                                         "          SELECT\n"
    3571             :                                         "                  f.id,\n"
    3572             :                                         "                  ft.function_type_keyword,\n"
    3573             :                                         "                  CASE WHEN a.type IS NULL THEN\n"
    3574             :                                         "                          sys.fqn(s.name, f.name) || '()'\n"
    3575             :                                         "                  ELSE\n"
    3576             :                                         "                          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"
    3577             :                                         "                  END,\n"
    3578             :                                         "                  a.number\n"
    3579             :                                         "          FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
    3580             :                                         "          WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
    3581             :                                         "  )\n"
    3582             :                                         "  SELECT\n"
    3583             :                                         "          fqn1.id id,\n"
    3584             :                                         "          fqn1.tpe tpe,\n"
    3585             :                                         "          fqn1.sig nme\n"
    3586             :                                         "  FROM\n"
    3587             :                                         "          fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id)  fqn2(id, num)\n"
    3588             :                                         "          ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
    3589             :                                         "CREATE VIEW sys.describe_comments AS\n"
    3590             :                                         "          SELECT\n"
    3591             :                                         "                  o.id id,\n"
    3592             :                                         "                  o.tpe tpe,\n"
    3593             :                                         "                  o.nme fqn,\n"
    3594             :                                         "                  c.remark rem\n"
    3595             :                                         "          FROM (\n"
    3596             :                                         "                  SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas\n"
    3597             :                                         "                  UNION ALL\n"
    3598             :                                         "                  SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
    3599             :                                         "                  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"
    3600             :                                         "                  WHERE s.name <> 'tmp'\n"
    3601             :                                         "                  UNION ALL\n"
    3602             :                                         "                  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"
    3603             :                                         "                  UNION ALL\n"
    3604             :                                         "                  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"
    3605             :                                         "                  UNION ALL\n"
    3606             :                                         "                  SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    3607             :                                         "                  UNION ALL\n"
    3608             :                                         "                  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"
    3609             :                                         "                  ) AS o(id, tpe, nme)\n"
    3610             :                                         "                  JOIN sys.comments c ON c.id = o.id;\n"
    3611             :                                         "CREATE VIEW sys.describe_privileges AS\n"
    3612             :                                         "  SELECT\n"
    3613             :                                         "          CASE\n"
    3614             :                                         "                  WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
    3615             :                                         "                          'COPY FROM'\n"
    3616             :                                         "                  WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
    3617             :                                         "                          'COPY INTO'\n"
    3618             :                                         "                  ELSE\n"
    3619             :                                         "                          o.nme\n"
    3620             :                                         "          END o_nme,\n"
    3621             :                                         "          coalesce(o.tpe, 'GLOBAL') o_tpe,\n"
    3622             :                                         "          pc.privilege_code_name p_nme,\n"
    3623             :                                         "          a.name a_nme,\n"
    3624             :                                         "          g.name g_nme,\n"
    3625             :                                         "          p.grantable grantable\n"
    3626             :                                         "  FROM\n"
    3627             :                                         "          sys.privileges p LEFT JOIN\n"
    3628             :                                         "          (\n"
    3629             :                                         "          SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
    3630             :                                         "                  from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
    3631             :                                         "          UNION ALL\n"
    3632             :                                         "                  SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
    3633             :                                         "                  FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
    3634             :                                         "          UNION ALL\n"
    3635             :                                         "                  SELECT f.id, f.nme, f.tpe\n"
    3636             :                                         "                  FROM sys.fully_qualified_functions f\n"
    3637             :                                         "          ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
    3638             :                                         "          sys.privilege_codes pc,\n"
    3639             :                                         "          auths a, auths g\n"
    3640             :                                         "  WHERE\n"
    3641             :                                         "          p.privileges = pc.privilege_code_id AND\n"
    3642             :                                         "          p.auth_id = a.id AND\n"
    3643             :                                         "          p.grantor = g.id;\n"
    3644             :                                         "CREATE VIEW sys.describe_partition_tables AS\n"
    3645             :                                         "  SELECT \n"
    3646             :                                         "          m_sch,\n"
    3647             :                                         "          m_tbl,\n"
    3648             :                                         "          p_sch,\n"
    3649             :                                         "          p_tbl,\n"
    3650             :                                         "          CASE\n"
    3651             :                                         "                  WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    3652             :                                         "                  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"
    3653             :                                         "                  ELSE p_raw_type\n"
    3654             :                                         "          END AS tpe,\n"
    3655             :                                         "          pvalues,\n"
    3656             :                                         "          minimum,\n"
    3657             :                                         "          maximum,\n"
    3658             :                                         "          with_nulls\n"
    3659             :                                         "  FROM \n"
    3660             :                                         "    (WITH\n"
    3661             :                                         "          tp(\"type\", table_id) AS\n"
    3662             :                                         "          (SELECT ifthenelse((table_partitions.\"type\" & 2) = 2, 'VALUES', 'RANGE'), table_partitions.table_id FROM sys.table_partitions),\n"
    3663             :                                         "          subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    3664             :                                         "          (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    3665             :                                         "          FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    3666             :                                         "          WHERE m_t.\"type\" IN (3, 6)\n"
    3667             :                                         "                  AND m_t.schema_id = m_s.id\n"
    3668             :                                         "                  AND m_s.name <> 'tmp'\n"
    3669             :                                         "                  AND m_t.system = FALSE\n"
    3670             :                                         "                  AND m_t.id = d.depend_id\n"
    3671             :                                         "                  AND d.id = p_m.id\n"
    3672             :                                         "                  AND p_m.schema_id = p_s.id\n"
    3673             :                                         "          ORDER BY m_t.id, p_m.id)\n"
    3674             :                                         "  SELECT\n"
    3675             :                                         "          subq.m_sch,\n"
    3676             :                                         "          subq.m_tbl,\n"
    3677             :                                         "          subq.p_sch,\n"
    3678             :                                         "          subq.p_tbl,\n"
    3679             :                                         "          tp.\"type\" AS p_raw_type,\n"
    3680             :                                         "          CASE WHEN tp.\"type\" = 'VALUES'\n"
    3681             :                                         "                  THEN (SELECT GROUP_CONCAT(vp.value, ',') FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid)\n"
    3682             :                                         "                  ELSE NULL\n"
    3683             :                                         "          END AS pvalues,\n"
    3684             :                                         "          CASE WHEN tp.\"type\" = 'RANGE'\n"
    3685             :                                         "                  THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3686             :                                         "                  ELSE NULL\n"
    3687             :                                         "          END AS minimum,\n"
    3688             :                                         "          CASE WHEN tp.\"type\" = 'RANGE'\n"
    3689             :                                         "                  THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3690             :                                         "                  ELSE NULL\n"
    3691             :                                         "          END AS maximum,\n"
    3692             :                                         "          CASE WHEN tp.\"type\" = 'VALUES'\n"
    3693             :                                         "                  THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    3694             :                                         "                  ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3695             :                                         "          END AS with_nulls\n"
    3696             :                                         "  FROM \n"
    3697             :                                         "          subq LEFT OUTER JOIN tp\n"
    3698             :                                         "          ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    3699             :                                         "CREATE VIEW sys.describe_functions AS\n"
    3700             :                                         "  WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    3701             :                                         "  (\n"
    3702             :                                         "          SELECT\n"
    3703             :                                         "                  func_id,\n"
    3704             :                                         "                  number,\n"
    3705             :                                         "                  max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    3706             :                                         "                  group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    3707             :                                         "          FROM sys.args\n"
    3708             :                                         "          WHERE inout = 1\n"
    3709             :                                         "  ),\n"
    3710             :                                         "  func_args(func_id, func_arg) AS\n"
    3711             :                                         "  (\n"
    3712             :                                         "          SELECT func_id, func_arg\n"
    3713             :                                         "          FROM func_args_all\n"
    3714             :                                         "          WHERE number = max_number\n"
    3715             :                                         "  ),\n"
    3716             :                                         "  func_rets_all(func_id, number, max_number, func_ret, func_ret_type) AS\n"
    3717             :                                         "  (\n"
    3718             :                                         "          SELECT\n"
    3719             :                                         "                  func_id,\n"
    3720             :                                         "                  number,\n"
    3721             :                                         "                  max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    3722             :                                         "                  group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number),\n"
    3723             :                                         "                  group_concat(sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    3724             :                                         "          FROM sys.args\n"
    3725             :                                         "          WHERE inout = 0\n"
    3726             :                                         "  ),\n"
    3727             :                                         "  func_rets(func_id, func_ret, func_ret_type) AS\n"
    3728             :                                         "  (\n"
    3729             :                                         "          SELECT\n"
    3730             :                                         "                  func_id,\n"
    3731             :                                         "                  func_ret,\n"
    3732             :                                         "                  func_ret_type\n"
    3733             :                                         "          FROM func_rets_all\n"
    3734             :                                         "          WHERE number = max_number\n"
    3735             :                                         "  )\n"
    3736             :                                         "  SELECT\n"
    3737             :                                         "          f.id o,\n"
    3738             :                                         "          s.name sch,\n"
    3739             :                                         "          f.name fun,\n"
    3740             :                                         "          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"
    3741             :                                         "  FROM sys.functions f\n"
    3742             :                                         "          LEFT OUTER JOIN func_args fa ON fa.func_id = f.id\n"
    3743             :                                         "          LEFT OUTER JOIN func_rets fr ON fr.func_id = f.id\n"
    3744             :                                         "          JOIN sys.schemas s ON f.schema_id = s.id\n"
    3745             :                                         "          JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
    3746             :                                         "          LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
    3747             :                                         "  WHERE s.name <> 'tmp' AND NOT f.system;\n"
    3748             :                                         "CREATE VIEW sys.describe_sequences AS\n"
    3749             :                                         "  SELECT\n"
    3750             :                                         "          s.name sch,\n"
    3751             :                                         "          seq.name seq,\n"
    3752             :                                         "          seq.\"start\" s,\n"
    3753             :                                         "          get_value_for(s.name, seq.name) rs,\n"
    3754             :                                         "          seq.\"minvalue\" mi,\n"
    3755             :                                         "          seq.\"maxvalue\" ma,\n"
    3756             :                                         "          seq.\"increment\" inc,\n"
    3757             :                                         "          seq.\"cacheinc\" cache,\n"
    3758             :                                         "          seq.\"cycle\" cycle,\n"
    3759             :                                         "          CASE WHEN seq.\"minvalue\" = -9223372036854775807 AND seq.\"increment\" > 0 AND seq.\"start\" =  1 THEN TRUE ELSE FALSE END nomin,\n"
    3760             :                                         "          CASE WHEN seq.\"maxvalue\" =  9223372036854775807 AND seq.\"increment\" < 0 AND seq.\"start\" = -1 THEN TRUE ELSE FALSE END nomax,\n"
    3761             :                                         "          CASE\n"
    3762             :                                         "                  WHEN seq.\"minvalue\" = 0 AND seq.\"increment\" > 0 THEN NULL\n"
    3763             :                                         "                  WHEN seq.\"minvalue\" <> -9223372036854775807 THEN seq.\"minvalue\"\n"
    3764             :                                         "                  ELSE\n"
    3765             :                                         "                          CASE\n"
    3766             :                                         "                                  WHEN seq.\"increment\" < 0  THEN NULL\n"
    3767             :                                         "                                  ELSE CASE WHEN seq.\"start\" = 1 THEN NULL ELSE seq.\"maxvalue\" END\n"
    3768             :                                         "                          END\n"
    3769             :                                         "          END rmi,\n"
    3770             :                                         "          CASE\n"
    3771             :                                         "                  WHEN seq.\"maxvalue\" = 0 AND seq.\"increment\" < 0 THEN NULL\n"
    3772             :                                         "                  WHEN seq.\"maxvalue\" <> 9223372036854775807 THEN seq.\"maxvalue\"\n"
    3773             :                                         "                  ELSE\n"
    3774             :                                         "                          CASE\n"
    3775             :                                         "                                  WHEN seq.\"increment\" > 0  THEN NULL\n"
    3776             :                                         "                                  ELSE CASE WHEN seq.\"start\" = -1 THEN NULL ELSE seq.\"maxvalue\" END\n"
    3777             :                                         "                          END\n"
    3778             :                                         "          END rma\n"
    3779             :                                         "  FROM sys.sequences seq, sys.schemas s\n"
    3780             :                                         "  WHERE s.id = seq.schema_id\n"
    3781             :                                         "  AND s.name <> 'tmp'\n"
    3782             :                                         "  ORDER BY s.name, seq.name;\n"
    3783             :                                         "GRANT SELECT ON sys.describe_constraints TO PUBLIC;\n"
    3784             :                                         "GRANT SELECT ON sys.describe_indices TO PUBLIC;\n"
    3785             :                                         "GRANT SELECT ON sys.describe_column_defaults TO PUBLIC;\n"
    3786             :                                         "GRANT SELECT ON sys.describe_foreign_keys TO PUBLIC;\n"
    3787             :                                         "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
    3788             :                                         "GRANT SELECT ON sys.describe_triggers TO PUBLIC;\n"
    3789             :                                         "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
    3790             :                                         "GRANT SELECT ON sys.fully_qualified_functions TO PUBLIC;\n"
    3791             :                                         "GRANT SELECT ON sys.describe_privileges TO PUBLIC;\n"
    3792             :                                         "GRANT SELECT ON sys.describe_user_defined_types TO PUBLIC;\n"
    3793             :                                         "GRANT SELECT ON sys.describe_partition_tables TO PUBLIC;\n"
    3794             :                                         "GRANT SELECT ON sys.describe_sequences TO PUBLIC;\n"
    3795             :                                         "GRANT SELECT ON sys.describe_functions TO PUBLIC;\n");
    3796           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3797             :                                         "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);
    3798           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3799             :                                         "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");
    3800             : 
    3801             :         /* 76_dump.sql (most everything already dropped) */
    3802           0 :         pos += snprintf(buf + pos, bufsize - pos,
    3803             :                                         "CREATE VIEW sys.dump_create_roles AS\n"
    3804             :                                         "  SELECT\n"
    3805             :                                         "    'CREATE ROLE ' || sys.dq(name) || ';' stmt,\n"
    3806             :                                         "    name user_name\n"
    3807             :                                         "    FROM sys.auths\n"
    3808             :                                         "   WHERE name NOT IN (SELECT name FROM sys.db_user_info)\n"
    3809             :                                         "     AND grantor <> 0;\n"
    3810             :                                         "CREATE VIEW sys.dump_create_users AS\n"
    3811             :                                         "  SELECT\n"
    3812             :                                         "    'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
    3813             :                                         "      sys.sq(sys.password_hash(ui.name)) ||\n"
    3814             :                                         "      ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
    3815             :                                         "    ui.name user_name\n"
    3816             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3817             :                                         "   WHERE ui.default_schema = s.id\n"
    3818             :                                         "     AND ui.name <> 'monetdb'\n"
    3819             :                                         "     AND ui.name <> '.snapshot';\n"
    3820             :                                         "CREATE VIEW sys.dump_create_schemas AS\n"
    3821             :                                         "  SELECT\n"
    3822             :                                         "    'CREATE SCHEMA ' || sys.dq(s.name) || ifthenelse(a.name <> 'sysadmin', ' AUTHORIZATION ' || sys.dq(a.name), ' ') || ';' stmt,\n"
    3823             :                                         "    s.name schema_name\n"
    3824             :                                         "    FROM sys.schemas s, sys.auths a\n"
    3825             :                                         "   WHERE s.authorization = a.id AND s.system = FALSE;\n"
    3826             :                                         "CREATE VIEW sys.dump_add_schemas_to_users AS\n"
    3827             :                                         "  SELECT\n"
    3828             :                                         "    'ALTER USER ' || sys.dq(ui.name) || ' SET SCHEMA ' || sys.dq(s.name) || ';' stmt,\n"
    3829             :                                         "    s.name schema_name,\n"
    3830             :                                         "    ui.name user_name\n"
    3831             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3832             :                                         "   WHERE ui.default_schema = s.id\n"
    3833             :                                         "     AND ui.name <> 'monetdb'\n"
    3834             :                                         "     AND ui.name <> '.snapshot'\n"
    3835             :                                         "     AND s.name <> 'sys';\n"
    3836             :                                         "CREATE VIEW sys.dump_grant_user_privileges AS\n"
    3837             :                                         "  SELECT\n"
    3838             :                                         "    'GRANT ' || sys.dq(a2.name) || ' ' || ifthenelse(a1.name = 'public', 'PUBLIC', sys.dq(a1.name)) || ';' stmt,\n"
    3839             :                                         "    a2.name grantee,\n"
    3840             :                                         "    a1.name grantor\n"
    3841             :                                         "    FROM sys.auths a1, sys.auths a2, sys.user_role ur\n"
    3842             :                                         "   WHERE a1.id = ur.login_id AND a2.id = ur.role_id;\n"
    3843             :                                         "CREATE VIEW sys.dump_table_constraint_type AS\n"
    3844             :                                         "  SELECT\n"
    3845             :                                         "    'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ADD CONSTRAINT ' || sys.DQ(con) || ' '||\n"
    3846             :                                         "      tpe || ' (' || GROUP_CONCAT(sys.DQ(col), ', ') || ');' stmt,\n"
    3847             :                                         "    sch schema_name,\n"
    3848             :                                         "    tbl table_name,\n"
    3849             :                                         "    con constraint_name\n"
    3850             :                                         "    FROM sys.describe_constraints GROUP BY sch, tbl, con, tpe;\n"
    3851             :                                         "CREATE VIEW sys.dump_table_grants AS\n"
    3852             :                                         "  WITH table_grants (sname, tname, grantee, grants, grantor, grantable)\n"
    3853             :                                         "  AS (SELECT s.name, t.name, a.name, sum(p.privileges), g.name, p.grantable\n"
    3854             :                                         "  FROM sys.schemas s, sys.tables t, sys.auths a, sys.privileges p, sys.auths g\n"
    3855             :                                         "       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"
    3856             :                                         "       GROUP BY s.name, t.name, a.name, g.name, p.grantable\n"
    3857             :                                         "       ORDER BY s.name, t.name, a.name, g.name, p.grantable)\n"
    3858             :                                         "  SELECT\n"
    3859             :                                         "    'GRANT ' || pc.privilege_code_name || ' ON TABLE ' || sys.FQN(sname, tname)\n"
    3860             :                                         "      || ' TO ' || ifthenelse(grantee = 'public', 'PUBLIC', sys.dq(grantee))\n"
    3861             :                                         "      || CASE WHEN grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3862             :                                         "    sname schema_name,\n"
    3863             :                                         "    tname table_name,\n"
    3864             :                                         "    grantee\n"
    3865             :                                         "    FROM table_grants LEFT OUTER JOIN sys.privilege_codes pc ON grants = pc.privilege_code_id;\n"
    3866             :                                         "CREATE VIEW sys.dump_column_grants AS\n"
    3867             :                                         "  SELECT\n"
    3868             :                                         "    'GRANT ' || pc.privilege_code_name || '(' || sys.dq(c.name) || ') ON ' || sys.FQN(s.name, t.name)\n"
    3869             :                                         "      || ' TO ' || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
    3870             :                                         "      || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3871             :                                         "    s.name schema_name,\n"
    3872             :                                         "    t.name table_name,\n"
    3873             :                                         "    c.name column_name,\n"
    3874             :                                         "    a.name grantee\n"
    3875             :                                         "    FROM sys.schemas s,\n"
    3876             :                                         "   sys.tables t,\n"
    3877             :                                         "   sys.columns c,\n"
    3878             :                                         "   sys.auths a,\n"
    3879             :                                         "   sys.privileges p,\n"
    3880             :                                         "   sys.auths g,\n"
    3881             :                                         "   sys.privilege_codes pc\n"
    3882             :                                         "   WHERE p.obj_id = c.id\n"
    3883             :                                         "     AND c.table_id = t.id\n"
    3884             :                                         "     AND p.auth_id = a.id\n"
    3885             :                                         "     AND t.schema_id = s.id\n"
    3886             :                                         "     AND NOT t.system\n"
    3887             :                                         "     AND p.grantor = g.id\n"
    3888             :                                         "     AND p.privileges = pc.privilege_code_id\n"
    3889             :                                         "   ORDER BY s.name, t.name, c.name, a.name, g.name, p.grantable;\n"
    3890             :                                         "CREATE VIEW sys.dump_function_grants AS\n"
    3891             :                                         "  WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    3892             :                                         "  (SELECT a.func_id,\n"
    3893             :                                         "    a.number,\n"
    3894             :                                         "    max(a.number) OVER (PARTITION BY a.func_id ORDER BY a.number DESC),\n"
    3895             :                                         "    group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ', ') OVER (PARTITION BY a.func_id ORDER BY a.number)\n"
    3896             :                                         "     FROM sys.args a\n"
    3897             :                                         "    WHERE a.inout = 1),\n"
    3898             :                                         "  func_args(func_id, func_arg) AS\n"
    3899             :                                         "  (SELECT func_id, func_arg FROM func_args_all WHERE number = max_number)\n"
    3900             :                                         "  SELECT\n"
    3901             :                                         "    'GRANT ' || pc.privilege_code_name || ' ON ' || ft.function_type_keyword || ' '\n"
    3902             :                                         "      || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ') TO '\n"
    3903             :                                         "      || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
    3904             :                                         "      || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3905             :                                         "    s.name schema_name,\n"
    3906             :                                         "    f.name function_name,\n"
    3907             :                                         "    a.name grantee\n"
    3908             :                                         "    FROM sys.schemas s,\n"
    3909             :                                         "   sys.functions f LEFT OUTER JOIN func_args fa ON f.id = fa.func_id,\n"
    3910             :                                         "   sys.auths a,\n"
    3911             :                                         "   sys.privileges p,\n"
    3912             :                                         "   sys.auths g,\n"
    3913             :                                         "   sys.function_types ft,\n"
    3914             :                                         "   sys.privilege_codes pc\n"
    3915             :                                         "   WHERE s.id = f.schema_id\n"
    3916             :                                         "     AND f.id = p.obj_id\n"
    3917             :                                         "     AND p.auth_id = a.id\n"
    3918             :                                         "     AND p.grantor = g.id\n"
    3919             :                                         "     AND p.privileges = pc.privilege_code_id\n"
    3920             :                                         "     AND f.type = ft.function_type_id\n"
    3921             :                                         "     AND NOT f.system\n"
    3922             :                                         "   ORDER BY s.name, f.name, a.name, g.name, p.grantable;\n"
    3923             :                                         "CREATE VIEW sys.dump_indices AS\n"
    3924             :                                         "  SELECT\n"
    3925             :                                         "    'CREATE ' || tpe || ' ' || sys.DQ(ind) || ' ON ' || sys.FQN(sch, tbl) || '(' || GROUP_CONCAT(col) || ');' stmt,\n"
    3926             :                                         "    sch schema_name,\n"
    3927             :                                         "    tbl table_name,\n"
    3928             :                                         "    ind index_name\n"
    3929             :                                         "    FROM sys.describe_indices GROUP BY ind, tpe, sch, tbl;\n"
    3930             :                                         "CREATE VIEW sys.dump_column_defaults AS\n"
    3931             :                                         "  SELECT 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ALTER COLUMN ' || sys.DQ(col) || ' SET DEFAULT ' || def || ';' stmt,\n"
    3932             :                                         "   sch schema_name,\n"
    3933             :                                         "   tbl table_name,\n"
    3934             :                                         "   col column_name\n"
    3935             :                                         "    FROM sys.describe_column_defaults;\n"
    3936             :                                         "CREATE VIEW sys.dump_foreign_keys AS\n"
    3937             :                                         "  SELECT\n"
    3938             :                                         "    'ALTER TABLE ' || sys.FQN(fk_s, fk_t) || ' ADD CONSTRAINT ' || sys.DQ(fk) || ' ' ||\n"
    3939             :                                         "      'FOREIGN KEY(' || GROUP_CONCAT(sys.DQ(fk_c), ',') ||') ' ||\n"
    3940             :                                         "      'REFERENCES ' || sys.FQN(pk_s, pk_t) || '(' || GROUP_CONCAT(sys.DQ(pk_c), ',') || ') ' ||\n"
    3941             :                                         "      'ON DELETE ' || on_delete || ' ON UPDATE ' || on_update ||\n"
    3942             :                                         "      ';' stmt,\n"
    3943             :                                         "    fk_s foreign_schema_name,\n"
    3944             :                                         "    fk_t foreign_table_name,\n"
    3945             :                                         "    pk_s primary_schema_name,\n"
    3946             :                                         "    pk_t primary_table_name,\n"
    3947             :                                         "    fk key_name\n"
    3948             :                                         "    FROM sys.describe_foreign_keys GROUP BY fk_s, fk_t, pk_s, pk_t, fk, on_delete, on_update;\n"
    3949             :                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    3950             :                                         "  SELECT\n"
    3951             :                                         "    'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    3952             :                                         "      CASE \n"
    3953             :                                         "      WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    3954             :                                         "      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"
    3955             :                                         "      WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    3956             :                                         "      ELSE ''\n"
    3957             :                                         "      END ||\n"
    3958             :                                         "      CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    3959             :                                         "      ';' stmt,\n"
    3960             :                                         "    m_sch merge_schema_name,\n"
    3961             :                                         "    m_tbl merge_table_name,\n"
    3962             :                                         "    p_sch partition_schema_name,\n"
    3963             :                                         "    p_tbl partition_table_name\n"
    3964             :                                         "    FROM sys.describe_partition_tables;\n"
    3965             :                                         "CREATE VIEW sys.dump_sequences AS\n"
    3966             :                                         "  SELECT\n"
    3967             :                                         "    'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT ' ||\n"
    3968             :                                         "    CASE WHEN \"s\" <> 0 THEN 'START WITH ' ||  \"rs\" ELSE '' END ||\n"
    3969             :                                         "    CASE WHEN \"inc\" <> 1 THEN ' INCREMENT BY ' ||  \"inc\" ELSE '' END ||\n"
    3970             :                                         "    CASE\n"
    3971             :                                         "      WHEN nomin THEN ' NO MINVALUE'\n"
    3972             :                                         "      WHEN rmi IS NOT NULL THEN ' MINVALUE ' || rmi\n"
    3973             :                                         "      ELSE ''\n"
    3974             :                                         "    END ||\n"
    3975             :                                         "    CASE\n"
    3976             :                                         "      WHEN nomax THEN ' NO MAXVALUE'\n"
    3977             :                                         "      WHEN rma IS NOT NULL THEN ' MAXVALUE ' || rma\n"
    3978             :                                         "      ELSE ''\n"
    3979             :                                         "    END ||\n"
    3980             :                                         "    CASE WHEN \"cache\" <> 1 THEN ' CACHE ' ||  \"cache\" ELSE '' END ||\n"
    3981             :                                         "    CASE WHEN \"cycle\" THEN ' CYCLE' ELSE '' END ||\n"
    3982             :                                         "    ';' stmt,\n"
    3983             :                                         "    sch schema_name,\n"
    3984             :                                         "    seq seqname\n"
    3985             :                                         "    FROM sys.describe_sequences;\n"
    3986             :                                         "CREATE VIEW sys.dump_start_sequences AS\n"
    3987             :                                         "  SELECT\n"
    3988             :                                         "    'UPDATE sys.sequences seq SET start = ' || s ||\n"
    3989             :                                         "      ' WHERE name = ' || sys.SQ(seq) ||\n"
    3990             :                                         "      ' AND schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = ' || sys.SQ(sch) || ');' stmt,\n"
    3991             :                                         "    sch schema_name,\n"
    3992             :                                         "    seq sequence_name\n"
    3993             :                                         "    FROM sys.describe_sequences;\n"
    3994             :                                         "CREATE VIEW sys.dump_functions AS\n"
    3995             :                                         "  SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
    3996             :                                         "   f.sch schema_name,\n"
    3997             :                                         "   f.fun function_name\n"
    3998             :                                         "    FROM sys.describe_functions f;\n"
    3999             :                                         "CREATE VIEW sys.dump_tables AS\n"
    4000             :                                         "  SELECT\n"
    4001             :                                         "    t.o o,\n"
    4002             :                                         "    CASE\n"
    4003             :                                         "      WHEN t.typ <> 'VIEW' THEN\n"
    4004             :                                         "      'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    4005             :                                         "      ELSE\n"
    4006             :                                         "      t.opt\n"
    4007             :                                         "      END stmt,\n"
    4008             :                                         "    t.sch schema_name,\n"
    4009             :                                         "    t.tab table_name\n"
    4010             :                                         "    FROM sys.describe_tables t;\n"
    4011             :                                         "CREATE VIEW sys.dump_triggers AS\n"
    4012             :                                         "  SELECT sys.schema_guard(sch, tab, def) stmt,\n"
    4013             :                                         "   sch schema_name,\n"
    4014             :                                         "   tab table_name,\n"
    4015             :                                         "   tri trigger_name\n"
    4016             :                                         "    FROM sys.describe_triggers;\n"
    4017             :                                         "CREATE VIEW sys.dump_comments AS\n"
    4018             :                                         "  SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    4019             :                                         "CREATE VIEW sys.dump_user_defined_types AS\n"
    4020             :                                         "  SELECT 'CREATE TYPE ' || sys.FQN(sch, sql_tpe) || ' EXTERNAL NAME ' || sys.DQ(ext_tpe) || ';' stmt,\n"
    4021             :                                         "   sch schema_name,\n"
    4022             :                                         "   sql_tpe type_name\n"
    4023             :                                         "    FROM sys.describe_user_defined_types;\n"
    4024             :                                         "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"
    4025             :                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    4026             :                                         "BEGIN\n"
    4027             :                                         "  RETURN\n"
    4028             :                                         "    CASE\n"
    4029             :                                         "    WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    4030             :                                         "    'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    4031             :                                         "    ELSE\n"
    4032             :                                         "    'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    4033             :                                         "    END;\n"
    4034             :                                         "END;\n"
    4035             :                                         "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
    4036             :                                         "BEGIN\n"
    4037             :                                         "  DECLARE k INT;\n"
    4038             :                                         "  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"
    4039             :                                         "  IF k IS NOT NULL THEN\n"
    4040             :                                         "    DECLARE cname STRING;\n"
    4041             :                                         "    DECLARE ctype STRING;\n"
    4042             :                                         "    SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4043             :                                         "    SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4044             :                                         "    DECLARE COPY_INTO_STMT STRING;\n"
    4045             :                                         "    DECLARE _cnt INT;\n"
    4046             :                                         "    SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
    4047             :                                         "    IF _cnt > 0 THEN\n"
    4048             :                                         "      SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    4049             :                                         "      DECLARE SELECT_DATA_STMT STRING;\n"
    4050             :                                         "      SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    4051             :                                         "      DECLARE M INT;\n"
    4052             :                                         "      SET M = (SELECT MAX(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    4053             :                                         "      WHILE (k < M) DO\n"
    4054             :                                         "  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"
    4055             :                                         "        SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4056             :                                         "  SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4057             :                                         "  SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    4058             :                                         "  SET SELECT_DATA_STMT = SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype);\n"
    4059             :                                         "      END WHILE;\n"
    4060             :                                         "      SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    4061             :                                         "      SET SELECT_DATA_STMT = SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl);\n"
    4062             :                                         "      insert into sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    4063             :                                         "      CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    4064             :                                         "    END IF;\n"
    4065             :                                         "  END IF;\n"
    4066             :                                         "END;\n"
    4067             :                                         "CREATE PROCEDURE sys.dump_table_data()\n"
    4068             :                                         "BEGIN\n"
    4069             :                                         "  DECLARE i INT;\n"
    4070             :                                         "  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"
    4071             :                                         "  IF i IS NOT NULL THEN\n"
    4072             :                                         "    DECLARE M INT;\n"
    4073             :                                         "    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"
    4074             :                                         "    DECLARE sch STRING;\n"
    4075             :                                         "    DECLARE tbl STRING;\n"
    4076             :                                         "    WHILE i < M DO\n"
    4077             :                                         "      set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4078             :                                         "      set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4079             :                                         "      CALL sys.dump_table_data(sch, tbl);\n"
    4080             :                                         "      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"
    4081             :                                         "    END WHILE;\n"
    4082             :                                         "    set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4083             :                                         "    set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4084             :                                         "    CALL sys.dump_table_data(sch, tbl);\n"
    4085             :                                         "  END IF;\n"
    4086             :                                         "END;\n"
    4087             :                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    4088             :                                         "BEGIN\n"
    4089             :                                         "  SET SCHEMA sys;\n"
    4090             :                                         "  TRUNCATE sys.dump_statements;\n"
    4091             :                                         "  INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    4092             :                                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'SET SCHEMA \"sys\";');\n"
    4093             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    4094             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    4095             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    4096             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    4097             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    4098             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    4099             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    4100             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    4101             :                                         "                              FROM (\n"
    4102             :                                         "                                SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    4103             :                                         "                                 UNION\n"
    4104             :                                         "                                SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    4105             :                                         "                              ) AS stmts(o, s);\n"
    4106             :                                         "  IF NOT DESCRIBE THEN\n"
    4107             :                                         "    CALL sys.dump_table_data();\n"
    4108             :                                         "  END IF;\n"
    4109             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    4110             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    4111             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    4112             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    4113             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    4114             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    4115             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    4116             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    4117             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    4118             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    4119             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    4120             :                                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    4121             :                                         "  RETURN sys.dump_statements;\n"
    4122             :                                         "END;\n");
    4123           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4124             :                                         "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);
    4125           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4126             :                                         "update sys.functions set system = true where system <> true and name in ('dump_database') and schema_id = 2000 and type = %d;\n", F_UNION);
    4127           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4128             :                                         "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);
    4129           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4130             :                                         "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");
    4131             : 
    4132             :         /* 80_udf.sql (removed) */
    4133           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4134             :                                         "drop function sys.reverse(string) cascade;\n"
    4135             :                                         "drop all function sys.fuse cascade;\n");
    4136             : 
    4137             :         /* 26_sysmon.sql */
    4138           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4139             :                                         "create procedure sys.vacuum(sname string, tname string, cname string)\n"
    4140             :                                         "  external name sql.vacuum;\n"
    4141             :                                         "create procedure sys.vacuum(sname string, tname string, cname string, interval int)\n"
    4142             :                                         "  external name sql.vacuum;\n"
    4143             :                                         "create procedure sys.stop_vacuum(sname string, tname string, cname string)\n"
    4144             :                                         "  external name sql.stop_vacuum;\n");
    4145           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4146             :                                         "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);
    4147             : 
    4148             :         /* 10_sys_schema_extension.sql */
    4149           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4150             :                                         "CREATE TABLE sys.fkey_actions (\n"
    4151             :                                         "    action_id   SMALLINT NOT NULL PRIMARY KEY,\n"
    4152             :                                         "    action_name VARCHAR(15) NOT NULL);\n"
    4153             :                                         "INSERT INTO sys.fkey_actions (action_id, action_name) VALUES\n"
    4154             :                                         "  (0, 'NO ACTION'),\n"
    4155             :                                         "  (1, 'CASCADE'),\n"
    4156             :                                         "  (2, 'RESTRICT'),\n"
    4157             :                                         "  (3, 'SET NULL'),\n"
    4158             :                                         "  (4, 'SET DEFAULT');\n"
    4159             :                                         "ALTER TABLE sys.fkey_actions SET READ ONLY;\n"
    4160             :                                         "GRANT SELECT ON sys.fkey_actions TO PUBLIC;\n"
    4161             :                                         "CREATE VIEW sys.fkeys AS\n"
    4162             :                                         "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"
    4163             :                                         " 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"
    4164             :                                         " UNION ALL\n"
    4165             :                                         " 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"
    4166             :                                         ") AS fks\n"
    4167             :                                         "JOIN sys.fkey_actions upd ON fks.update_action_id = upd.action_id\n"
    4168             :                                         "JOIN sys.fkey_actions del ON fks.delete_action_id = del.action_id;\n"
    4169             :                                         "GRANT SELECT ON sys.fkeys TO PUBLIC;\n"
    4170             :                                         );
    4171           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4172             :                                         "update sys._tables set system = true where name in ('fkey_actions', 'fkeys') AND schema_id = 2000;\n");
    4173             : 
    4174             :         /* recreate SQL functions that just need to be recompiled since the
    4175             :          * MAL functions's "unsafe" property was changed */
    4176           0 :         sql_schema *lg = mvc_bind_schema(sql, "logging");
    4177           0 :         t = mvc_bind_table(sql, lg, "compinfo");
    4178           0 :         t->system = 0;
    4179           0 :         t = mvc_bind_table(sql, s, "schemastorage");
    4180           0 :         t->system = 0;
    4181           0 :         t = mvc_bind_table(sql, s, "tablestorage");
    4182           0 :         t->system = 0;
    4183           0 :         t = mvc_bind_table(sql, s, "storage");
    4184           0 :         t->system = 0;
    4185           0 :         t = mvc_bind_table(sql, s, "rejects");
    4186           0 :         t->system = 0;
    4187           0 :         t = mvc_bind_table(sql, s, "queue");
    4188           0 :         t->system = 0;
    4189           0 :         t = mvc_bind_table(sql, s, "optimizers");
    4190           0 :         t->system = 0;
    4191           0 :         t = mvc_bind_table(sql, s, "prepared_statements_args");
    4192           0 :         t->system = 0;
    4193           0 :         t = mvc_bind_table(sql, s, "prepared_statements");
    4194           0 :         t->system = 0;
    4195           0 :         t = mvc_bind_table(sql, s, "sessions");
    4196           0 :         t->system = 0;
    4197           0 :         t = mvc_bind_table(sql, s, "querylog_calls");
    4198           0 :         t->system = 0;
    4199           0 :         t = mvc_bind_table(sql, s, "querylog_history");
    4200           0 :         t->system = 0;
    4201           0 :         t = mvc_bind_table(sql, s, "querylog_catalog");
    4202           0 :         t->system = 0;
    4203           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4204             :                                         "drop view logging.compinfo cascade;\n"
    4205             :                                         "drop function logging.compinfo cascade;\n"
    4206             :                                         "drop procedure sys.storagemodelinit() cascade;\n"
    4207             :                                         "drop view sys.schemastorage cascade;\n"
    4208             :                                         "drop view sys.tablestorage cascade;\n"
    4209             :                                         "drop view sys.storage cascade;\n"
    4210             :                                         "drop function sys.storage() cascade;\n"
    4211             :                                         "drop function if exists wlr.tick cascade;\n"
    4212             :                                         "drop function if exists wlr.clock cascade;\n"
    4213             :                                         "drop function if exists wlc.tick cascade;\n"
    4214             :                                         "drop function if exists wlc.clock cascade;\n"
    4215             :                                         "drop function profiler.getlimit cascade;\n"
    4216             :                                         "drop view sys.rejects cascade;\n"
    4217             :                                         "drop function sys.rejects cascade;\n"
    4218             :                                         "drop function sys.user_statistics cascade;\n"
    4219             :                                         "drop view sys.queue cascade;\n"
    4220             :                                         "drop function sys.queue cascade;\n"
    4221             :                                         "drop function sys.debugflags cascade;\n"
    4222             :                                         "drop function sys.bbp cascade;\n"
    4223             :                                         "drop view sys.optimizers cascade;\n"
    4224             :                                         "drop function sys.optimizers cascade;\n"
    4225             :                                         "drop function sys.querycache cascade;\n"
    4226             :                                         "drop function sys.optimizer_stats cascade;\n"
    4227             :                                         "drop function sys.current_sessionid cascade;\n"
    4228             :                                         "drop view sys.prepared_statements_args cascade;\n"
    4229             :                                         "drop function sys.prepared_statements_args cascade;\n"
    4230             :                                         "drop view sys.prepared_statements cascade;\n"
    4231             :                                         "drop function sys.prepared_statements cascade;\n"
    4232             :                                         "drop view sys.sessions cascade;\n"
    4233             :                                         "drop function sys.sessions cascade;\n"
    4234             :                                         "drop view sys.querylog_history cascade;\n"
    4235             :                                         "drop view sys.querylog_calls cascade;\n"
    4236             :                                         "drop function sys.querylog_calls cascade;\n"
    4237             :                                         "drop view sys.querylog_catalog cascade;\n"
    4238             :                                         "drop function sys.querylog_catalog cascade;\n"
    4239             :                                         "create function sys.querylog_catalog()\n"
    4240             :                                         "returns table(\n"
    4241             :                                         " id oid,\n"
    4242             :                                         " owner string,\n"
    4243             :                                         " defined timestamp,\n"
    4244             :                                         " query string,\n"
    4245             :                                         " pipe string,\n"
    4246             :                                         " \"plan\" string,\n"
    4247             :                                         " mal int,\n"
    4248             :                                         " optimize bigint\n"
    4249             :                                         ")\n"
    4250             :                                         "external name sql.querylog_catalog;\n"
    4251             :                                         "create view sys.querylog_catalog as select * from sys.querylog_catalog();\n"
    4252             :                                         "create function sys.querylog_calls()\n"
    4253             :                                         "returns table(\n"
    4254             :                                         " id oid,\n"
    4255             :                                         " \"start\" timestamp,\n"
    4256             :                                         " \"stop\" timestamp,\n"
    4257             :                                         " arguments string,\n"
    4258             :                                         " tuples bigint,\n"
    4259             :                                         " run bigint,\n"
    4260             :                                         " ship bigint,\n"
    4261             :                                         " cpu int,\n"
    4262             :                                         " io int\n"
    4263             :                                         ")\n"
    4264             :                                         "external name sql.querylog_calls;\n"
    4265             :                                         "create view sys.querylog_calls as select * from sys.querylog_calls();\n"
    4266             :                                         "create view sys.querylog_history as\n"
    4267             :                                         "select qd.*, ql.\"start\",ql.\"stop\", ql.arguments, ql.tuples, ql.run, ql.ship, ql.cpu, ql.io\n"
    4268             :                                         "from sys.querylog_catalog() qd, sys.querylog_calls() ql\n"
    4269             :                                         "where qd.id = ql.id and qd.owner = user;\n"
    4270             :                                         "create function sys.sessions()\n"
    4271             :                                         "returns table(\n"
    4272             :                                         " \"sessionid\" int,\n"
    4273             :                                         " \"username\" string,\n"
    4274             :                                         " \"login\" timestamp,\n"
    4275             :                                         " \"idle\" timestamp,\n"
    4276             :                                         " \"optimizer\" string,\n"
    4277             :                                         " \"sessiontimeout\" int,\n"
    4278             :                                         " \"querytimeout\" int,\n"
    4279             :                                         " \"workerlimit\" int,\n"
    4280             :                                         " \"memorylimit\" int\n"
    4281             :                                         ")\n"
    4282             :                                         "external name sql.sessions;\n"
    4283             :                                         "create view sys.sessions as select * from sys.sessions();\n"
    4284             :                                         "create function sys.prepared_statements()\n"
    4285             :                                         "returns table(\n"
    4286             :                                         " \"sessionid\" int,\n"
    4287             :                                         " \"username\" string,\n"
    4288             :                                         " \"statementid\" int,\n"
    4289             :                                         " \"statement\" string,\n"
    4290             :                                         " \"created\" timestamp\n"
    4291             :                                         ")\n"
    4292             :                                         "external name sql.prepared_statements;\n"
    4293             :                                         "grant execute on function sys.prepared_statements to public;\n"
    4294             :                                         "create view sys.prepared_statements as select * from sys.prepared_statements();\n"
    4295             :                                         "grant select on sys.prepared_statements to public;\n"
    4296             :                                         "create function sys.prepared_statements_args()\n"
    4297             :                                         "returns table(\n"
    4298             :                                         " \"statementid\" int,\n"
    4299             :                                         " \"type\" string,\n"
    4300             :                                         " \"type_digits\" int,\n"
    4301             :                                         " \"type_scale\" int,\n"
    4302             :                                         " \"inout\" tinyint,\n"
    4303             :                                         " \"number\" int,\n"
    4304             :                                         " \"schema\" string,\n"
    4305             :                                         " \"table\" string,\n"
    4306             :                                         " \"column\" string\n"
    4307             :                                         ")\n"
    4308             :                                         "external name sql.prepared_statements_args;\n"
    4309             :                                         "grant execute on function sys.prepared_statements_args to public;\n"
    4310             :                                         "create view sys.prepared_statements_args as select * from sys.prepared_statements_args();\n"
    4311             :                                         "grant select on sys.prepared_statements_args to public;\n"
    4312             :                                         "create function sys.current_sessionid() returns int\n"
    4313             :                                         "external name clients.current_sessionid;\n"
    4314             :                                         "grant execute on function sys.current_sessionid to public;\n"
    4315             :                                         "create function sys.optimizer_stats()\n"
    4316             :                                         " returns table (optname string, count int, timing bigint)\n"
    4317             :                                         " external name inspect.optimizer_stats;\n"
    4318             :                                         "create function sys.querycache()\n"
    4319             :                                         " returns table (query string, count int)\n"
    4320             :                                         " external name sql.dump_cache;\n"
    4321             :                                         "create function sys.optimizers ()\n"
    4322             :                                         " returns table (name string, def string, status string)\n"
    4323             :                                         " external name sql.optimizers;\n"
    4324             :                                         "create view sys.optimizers as select * from sys.optimizers();\n"
    4325             :                                         "create function sys.bbp ()\n"
    4326             :                                         " returns table (id int, name string,\n"
    4327             :                                         " ttype string, count bigint, refcnt int, lrefcnt int,\n"
    4328             :                                         " location string, heat int, dirty string,\n"
    4329             :                                         " status string, kind string)\n"
    4330             :                                         " external name bbp.get;\n"
    4331             :                                         "create function sys.debugflags()\n"
    4332             :                                         " returns table(flag string, val bool)\n"
    4333             :                                         " external name mdb.\"getDebugFlags\";\n"
    4334             :                                         "create function sys.queue()\n"
    4335             :                                         "returns table(\n"
    4336             :                                         " \"tag\" bigint,\n"
    4337             :                                         " \"sessionid\" int,\n"
    4338             :                                         " \"username\" string,\n"
    4339             :                                         " \"started\" timestamp,\n"
    4340             :                                         " \"status\" string,\n"
    4341             :                                         " \"query\" string,\n"
    4342             :                                         " \"finished\" timestamp,\n"
    4343             :                                         " \"maxworkers\" int,\n"
    4344             :                                         " \"footprint\" int\n"
    4345             :                                         ")\n"
    4346             :                                         "external name sysmon.queue;\n"
    4347             :                                         "grant execute on function sys.queue to public;\n"
    4348             :                                         "create view sys.queue as select * from sys.queue();\n"
    4349             :                                         "grant select on sys.queue to public;\n"
    4350             :                                         "create function sys.user_statistics()\n"
    4351             :                                         "returns table(\n"
    4352             :                                         " username string,\n"
    4353             :                                         " querycount bigint,\n"
    4354             :                                         " totalticks bigint,\n"
    4355             :                                         " started timestamp,\n"
    4356             :                                         " finished timestamp,\n"
    4357             :                                         " maxticks bigint,\n"
    4358             :                                         " maxquery string\n"
    4359             :                                         ")\n"
    4360             :                                         "external name sysmon.user_statistics;\n"
    4361             :                                         "create function sys.rejects()\n"
    4362             :                                         "returns table(\n"
    4363             :                                         " rowid bigint,\n"
    4364             :                                         " fldid int,\n"
    4365             :                                         " \"message\" string,\n"
    4366             :                                         " \"input\" string\n"
    4367             :                                         ")\n"
    4368             :                                         "external name sql.copy_rejects;\n"
    4369             :                                         "grant execute on function rejects to public;\n"
    4370             :                                         "create view sys.rejects as select * from sys.rejects();\n"
    4371             :                                         "create function profiler.getlimit() returns integer external name profiler.getlimit;\n"
    4372             :                                         "create function sys.\"storage\"()\n"
    4373             :                                         "returns table (\n"
    4374             :                                         " \"schema\" varchar(1024),\n"
    4375             :                                         " \"table\" varchar(1024),\n"
    4376             :                                         " \"column\" varchar(1024),\n"
    4377             :                                         " \"type\" varchar(1024),\n"
    4378             :                                         " \"mode\" varchar(15),\n"
    4379             :                                         " location varchar(1024),\n"
    4380             :                                         " \"count\" bigint,\n"
    4381             :                                         " typewidth int,\n"
    4382             :                                         " columnsize bigint,\n"
    4383             :                                         " heapsize bigint,\n"
    4384             :                                         " hashes bigint,\n"
    4385             :                                         " phash boolean,\n"
    4386             :                                         " \"imprints\" bigint,\n"
    4387             :                                         " sorted boolean,\n"
    4388             :                                         " revsorted boolean,\n"
    4389             :                                         " \"unique\" boolean,\n"
    4390             :                                         " orderidx bigint\n"
    4391             :                                         ")\n"
    4392             :                                         "external name sql.\"storage\";\n"
    4393             :                                         "create view sys.\"storage\" as\n"
    4394             :                                         "select * from sys.\"storage\"()\n"
    4395             :                                         " where (\"schema\", \"table\") in (\n"
    4396             :                                         " select sch.\"name\", tbl.\"name\"\n"
    4397             :                                         " from sys.\"tables\" as tbl join sys.\"schemas\" as sch on tbl.schema_id = sch.id\n"
    4398             :                                         " where tbl.\"system\" = false)\n"
    4399             :                                         "order by \"schema\", \"table\", \"column\";\n"
    4400             :                                         "create view sys.\"tablestorage\" as\n"
    4401             :                                         "select \"schema\", \"table\",\n"
    4402             :                                         " max(\"count\") as \"rowcount\",\n"
    4403             :                                         " count(*) as \"storages\",\n"
    4404             :                                         " sum(columnsize) as columnsize,\n"
    4405             :                                         " sum(heapsize) as heapsize,\n"
    4406             :                                         " sum(hashes) as hashsize,\n"
    4407             :                                         " sum(\"imprints\") as imprintsize,\n"
    4408             :                                         " sum(orderidx) as orderidxsize\n"
    4409             :                                         " from sys.\"storage\"\n"
    4410             :                                         "group by \"schema\", \"table\"\n"
    4411             :                                         "order by \"schema\", \"table\";\n"
    4412             :                                         "create view sys.\"schemastorage\" as\n"
    4413             :                                         "select \"schema\",\n"
    4414             :                                         " count(*) as \"storages\",\n"
    4415             :                                         " sum(columnsize) as columnsize,\n"
    4416             :                                         " sum(heapsize) as heapsize,\n"
    4417             :                                         " sum(hashes) as hashsize,\n"
    4418             :                                         " sum(\"imprints\") as imprintsize,\n"
    4419             :                                         " sum(orderidx) as orderidxsize\n"
    4420             :                                         " from sys.\"storage\"\n"
    4421             :                                         "group by \"schema\"\n"
    4422             :                                         "order by \"schema\";\n"
    4423             :                                         "create procedure sys.storagemodelinit()\n"
    4424             :                                         "begin\n"
    4425             :                                         " delete from sys.storagemodelinput;\n"
    4426             :                                         " insert into sys.storagemodelinput\n"
    4427             :                                         " select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
    4428             :                                         " case when (\"unique\" or \"type\" in ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
    4429             :                                         " then \"count\" else 0 end,\n"
    4430             :                                         " case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
    4431             :                                         " then cast((heapsize - 8192) / \"count\" as bigint)\n"
    4432             :                                         " when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
    4433             :                                         " then cast((heapsize - 32) / \"count\" as bigint)\n"
    4434             :                                         " else typewidth end,\n"
    4435             :                                         " false, case sorted when true then true else false end, \"unique\", true\n"
    4436             :                                         " from sys.\"storage\";\n"
    4437             :                                         " update sys.storagemodelinput\n"
    4438             :                                         " set reference = true\n"
    4439             :                                         " where (\"schema\", \"table\", \"column\") in (\n"
    4440             :                                         " select fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
    4441             :                                         " from sys.\"keys\" as fkkey,\n"
    4442             :                                         " sys.\"objects\" as fkkeycol,\n"
    4443             :                                         " sys.\"tables\" as fktable,\n"
    4444             :                                         " sys.\"schemas\" as fkschema\n"
    4445             :                                         " where fktable.\"id\" = fkkey.\"table_id\"\n"
    4446             :                                         " and fkkey.\"id\" = fkkeycol.\"id\"\n"
    4447             :                                         " and fkschema.\"id\" = fktable.\"schema_id\"\n"
    4448             :                                         " and fkkey.\"rkey\" > -1 );\n"
    4449             :                                         " update sys.storagemodelinput\n"
    4450             :                                         " set isacolumn = false\n"
    4451             :                                         " where (\"schema\", \"table\", \"column\") not in (\n"
    4452             :                                         " select sch.\"name\", tbl.\"name\", col.\"name\"\n"
    4453             :                                         " from sys.\"schemas\" as sch,\n"
    4454             :                                         " sys.\"tables\" as tbl,\n"
    4455             :                                         " sys.\"columns\" as col\n"
    4456             :                                         " where sch.\"id\" = tbl.\"schema_id\"\n"
    4457             :                                         " and tbl.\"id\" = col.\"table_id\");\n"
    4458             :                                         "end;\n"
    4459             :                                         "create function logging.compinfo()\n"
    4460             :                                         "returns table(\n"
    4461             :                                         " \"id\" int,\n"
    4462             :                                         " \"component\" string,\n"
    4463             :                                         " \"log_level\" string\n"
    4464             :                                         ")\n"
    4465             :                                         "external name logging.compinfo;\n"
    4466             :                                         "grant execute on function logging.compinfo to public;\n"
    4467             :                                         "create view logging.compinfo as select * from logging.compinfo();\n"
    4468             :                                         "grant select on logging.compinfo to public;\n"
    4469             :                                         "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"
    4470             :                                         "update sys._tables set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'logging') and name = 'compinfo';\n"
    4471             :                                         "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"
    4472             :                                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'logging') and name = 'compinfo';\n"
    4473             :                                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'profiler') and name = 'getlimit';\n"
    4474             :                 );
    4475             : 
    4476             :         /* 99_system.sql */
    4477           0 :         t = mvc_bind_table(sql, s, "systemfunctions");
    4478           0 :         t->system = 0;
    4479           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4480             :                         "drop view sys.systemfunctions cascade;\n");
    4481             : 
    4482             :         /* 80_statistics.sql */
    4483           0 :         t = mvc_bind_table(sql, s, "statistics");
    4484           0 :         t->system = 0;
    4485           0 :         pos += snprintf(buf + pos, bufsize - pos,
    4486             :                                 "drop table sys.statistics cascade;\n"
    4487             :                                 "drop procedure sys.analyze(int,bigint) cascade;\n"
    4488             :                                 "drop procedure sys.analyze(int,bigint,string) cascade;\n"
    4489             :                                 "drop procedure sys.analyze(int,bigint,string,string) cascade;\n"
    4490             :                                 "drop procedure sys.analyze(int,bigint,string,string,string) cascade;\n"
    4491             :                                 "create procedure sys.\"analyze\"()\n"
    4492             :                                 "external name sql.\"analyze\";\n"
    4493             :                                 "grant execute on procedure sys.\"analyze\"() to public;\n"
    4494             :                                 "create procedure sys.\"analyze\"(\"sname\" varchar(1024))\n"
    4495             :                                 "external name sql.\"analyze\";\n"
    4496             :                                 "grant execute on procedure sys.\"analyze\"(varchar(1024)) to public;\n"
    4497             :                                 "create procedure sys.\"analyze\"(\"sname\" varchar(1024), \"tname\" varchar(1024))\n"
    4498             :                                 "external name sql.\"analyze\";\n"
    4499             :                                 "grant execute on procedure sys.\"analyze\"(varchar(1024),varchar(1024)) to public;\n"
    4500             :                                 "create procedure sys.\"analyze\"(\"sname\" varchar(1024), \"tname\" varchar(1024), \"cname\" varchar(1024))\n"
    4501             :                                 "external name sql.\"analyze\";\n"
    4502             :                                 "grant execute on procedure sys.\"analyze\"(varchar(1024),varchar(1024),varchar(1024)) to public;\n"
    4503             :                                 "create function sys.\"statistics\"()\n"
    4504             :                                 "returns table (\n"
    4505             :                                 "  \"column_id\" integer,\n"
    4506             :                                 "  \"schema\" varchar(1024),\n"
    4507             :                                 "  \"table\" varchar(1024),\n"
    4508             :                                 "  \"column\" varchar(1024),\n"
    4509             :                                 "  \"type\" varchar(1024),\n"
    4510             :                                 "  \"width\" integer,\n"
    4511             :                                 "  \"count\" bigint,\n"
    4512             :                                 "  \"unique\" boolean,\n"
    4513             :                                 "  \"nils\" boolean,\n"
    4514             :                                 "  \"minval\" string,\n"
    4515             :                                 "  \"maxval\" string,\n"
    4516             :                                 "  \"sorted\" boolean,\n"
    4517             :                                 "  \"revsorted\" boolean\n"
    4518             :                                 ")\n"
    4519             :                                 "external name sql.\"statistics\";\n"
    4520             :                                 "grant execute on function sys.\"statistics\"() to public;\n"
    4521             :                                 "create view sys.\"statistics\" as\n"
    4522             :                                 "select * from sys.\"statistics\"()\n"
    4523             :                                 "-- exclude system tables\n"
    4524             :                                 "where (\"schema\", \"table\") in (\n"
    4525             :                                 "  SELECT sch.\"name\", tbl.\"name\"\n"
    4526             :                                 "  FROM sys.\"tables\" AS tbl JOIN sys.\"schemas\" AS sch ON tbl.schema_id = sch.id\n"
    4527             :                                 "  WHERE tbl.\"system\" = FALSE)\n"
    4528             :                                 "order by \"schema\", \"table\", \"column\";\n"
    4529             :                                 "grant select on sys.\"statistics\" to public;\n"
    4530             :                                 "create function sys.\"statistics\"(\"sname\" varchar(1024))\n"
    4531             :                                 "returns table (\n"
    4532             :                                 "  \"column_id\" integer,\n"
    4533             :                                 "  \"schema\" varchar(1024),\n"
    4534             :                                 "  \"table\" varchar(1024),\n"
    4535             :                                 "  \"column\" varchar(1024),\n"
    4536             :                                 "  \"type\" varchar(1024),\n"
    4537             :                                 "  \"width\" integer,\n"
    4538             :                                 "  \"count\" bigint,\n"
    4539             :                                 "  \"unique\" boolean,\n"
    4540             :                                 "  \"nils\" boolean,\n"
    4541             :                                 "  \"minval\" string,\n"
    4542             :                                 "  \"maxval\" string,\n"
    4543             :                                 "  \"sorted\" boolean,\n"
    4544             :                                 "  \"revsorted\" boolean\n"
    4545             :                                 ")\n"
    4546             :                                 "external name sql.\"statistics\";\n"
    4547             :                                 "grant execute on function sys.\"statistics\"(varchar(1024)) to public;\n"
    4548             :                                 "create function sys.\"statistics\"(\"sname\" varchar(1024), \"tname\" varchar(1024))\n"
    4549             :                                 "returns table (\n"
    4550             :                                 "  \"column_id\" integer,\n"
    4551             :                                 "  \"schema\" varchar(1024),\n"
    4552             :                                 "  \"table\" varchar(1024),\n"
    4553             :                                 "  \"column\" varchar(1024),\n"
    4554             :                                 "  \"type\" varchar(1024),\n"
    4555             :                                 "  \"width\" integer,\n"
    4556             :                                 "  \"count\" bigint,\n"
    4557             :                                 "  \"unique\" boolean,\n"
    4558             :                                 "  \"nils\" boolean,\n"
    4559             :                                 "  \"minval\" string,\n"
    4560             :                                 "  \"maxval\" string,\n"
    4561             :                                 "  \"sorted\" boolean,\n"
    4562             :                                 "  \"revsorted\" boolean\n"
    4563             :                                 ")\n"
    4564             :                                 "external name sql.\"statistics\";\n"
    4565             :                                 "grant execute on function sys.\"statistics\"(varchar(1024),varchar(1024)) to public;\n"
    4566             :                                 "create function sys.\"statistics\"(\"sname\" varchar(1024), \"tname\" varchar(1024), \"cname\" varchar(1024))\n"
    4567             :                                 "returns table (\n"
    4568             :                                 "  \"column_id\" integer,\n"
    4569             :                                 "  \"schema\" varchar(1024),\n"
    4570             :                                 "  \"table\" varchar(1024),\n"
    4571             :                                 "  \"column\" varchar(1024),\n"
    4572             :                                 "  \"type\" varchar(1024),\n"
    4573             :                                 "  \"width\" integer,\n"
    4574             :                                 "  \"count\" bigint,\n"
    4575             :                                 "  \"unique\" boolean,\n"
    4576             :                                 "  \"nils\" boolean,\n"
    4577             :                                 "  \"minval\" string,\n"
    4578             :                                 "  \"maxval\" string,\n"
    4579             :                                 "  \"sorted\" boolean,\n"
    4580             :                                 "  \"revsorted\" boolean\n"
    4581             :                                 ")\n"
    4582             :                                 "external name sql.\"statistics\";\n"
    4583             :                                 "grant execute on function sys.\"statistics\"(varchar(1024),varchar(1024),varchar(1024)) to public;\n"
    4584             :                                 "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'statistics';\n"
    4585             :                                 "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('analyze','statistics');\n");
    4586             : 
    4587           0 :         assert(pos < bufsize);
    4588           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    4589           0 :         fflush(stdout);
    4590           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    4591             : 
    4592           0 :         GDKfree(buf);
    4593           0 :         return err;             /* usually MAL_SUCCEED */
    4594             : }
    4595             : 
    4596             : static str
    4597          99 : sql_update_sep2022(Client c, mvc *sql, sql_schema *s)
    4598             : {
    4599          99 :         size_t bufsize = 65536, pos = 0;
    4600          99 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    4601          99 :         res_table *output;
    4602          99 :         BAT *b;
    4603             : 
    4604          99 :         if (buf == NULL)
    4605           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4606             : 
    4607             :         /* if sys.db_user_info does not have a column password, we need to
    4608             :          * add a bunch of columns */
    4609          99 :         sql_table *db_user_info = find_sql_table(sql->session->tr, s, "db_user_info");
    4610          99 :         if (find_sql_column(db_user_info, "password") == NULL) {
    4611           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4612             :                                                 "alter table sys.db_user_info add column max_memory bigint;\n"
    4613             :                                                 "alter table sys.db_user_info add column max_workers int;\n"
    4614             :                                                 "alter table sys.db_user_info add column optimizer varchar(1024);\n"
    4615             :                                                 "alter table sys.db_user_info add column default_role int;\n"
    4616             :                                                 "alter table sys.db_user_info add column password varchar(256);\n");
    4617           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4618             :                                                 "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");
    4619           0 :                 int endprint = (int) pos;
    4620           0 :                 bat bid;
    4621           0 :                 BAT *u = NULL, *p = NULL, *d = NULL;
    4622           0 :                 if ((bid = BBPindex("M5system_auth_user")) == 0 ||
    4623           0 :                         (u = BATdescriptor(bid)) == NULL ||
    4624           0 :                         (bid = BBPindex("M5system_auth_passwd_v2")) == 0 ||
    4625           0 :                         (p = BATdescriptor(bid)) == NULL ||
    4626           0 :                         (bid = BBPindex("M5system_auth_deleted")) == 0 ||
    4627           0 :                         (d = BATdescriptor(bid)) == NULL) {
    4628           0 :                         BBPreclaim(u);
    4629           0 :                         BBPreclaim(p);
    4630           0 :                         BBPreclaim(d);
    4631           0 :                         throw(SQL, __func__, INTERNAL_BAT_ACCESS);
    4632             :                 }
    4633           0 :                 BATiter ui = bat_iterator(u);
    4634           0 :                 BATiter pi = bat_iterator(p);
    4635           0 :                 for (oid i = 0; i < ui.count; i++) {
    4636           0 :                         if (BUNfnd(d, &i) == BUN_NONE) {
    4637           0 :                                 const char *user = BUNtvar(ui, i);
    4638           0 :                                 const char *pass = BUNtvar(pi, i);
    4639           0 :                                 if (pos + 4 * (strlen(user) + strlen(pass)) + 64 >= bufsize) {
    4640           0 :                                         char *nbuf = GDKrealloc(buf, bufsize + 65536);
    4641           0 :                                         if (nbuf == NULL) {
    4642           0 :                                                 err = createException(SQL, __func__, MAL_MALLOC_FAIL);
    4643           0 :                                                 break;
    4644             :                                         }
    4645             :                                         buf = nbuf;
    4646             :                                         bufsize += 65536;
    4647             :                                 }
    4648           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    4649             :                                                                 "update sys.db_user_info set password = e'");
    4650           0 :                                 for (const char *p = pass; *p; p++) {
    4651           0 :                                         if (*p < '\040' || *p >= '\177') {
    4652             :                                                 /* control character or high bit set */
    4653           0 :                                                 pos += snprintf(buf + pos, bufsize - pos,
    4654             :                                                                                 "\\%03o", (unsigned char) *p);
    4655             :                                         } else {
    4656           0 :                                                 if (*p == '\\' || *p == '\'')
    4657           0 :                                                         buf[pos++] = *p;
    4658           0 :                                                 buf[pos++] = *p;
    4659             :                                         }
    4660             :                                 }
    4661           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    4662             :                                                                 "' where name = e'");
    4663           0 :                                 for (const char *p = user; *p; p++) {
    4664           0 :                                         if (*p < '\040' || *p >= '\177') {
    4665             :                                                 /* control character or high bit set */
    4666           0 :                                                 pos += snprintf(buf + pos, bufsize - pos,
    4667             :                                                                                 "\\%03o", (unsigned char) *p);
    4668             :                                         } else {
    4669           0 :                                                 if (*p == '\\' || *p == '\'')
    4670           0 :                                                         buf[pos++] = *p;
    4671           0 :                                                 buf[pos++] = *p;
    4672             :                                         }
    4673             :                                 }
    4674           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    4675             :                                                                 "';\n");
    4676             :                         }
    4677             :                 }
    4678           0 :                 if (err == MAL_SUCCEED) {
    4679           0 :                         assert(pos < bufsize);
    4680           0 :                         printf("Running database upgrade commands:\n%.*s-- and copying passwords\n\n", endprint, buf);
    4681           0 :                         fflush(stdout);
    4682           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    4683             :                 }
    4684           0 :                 bat_iterator_end(&ui);
    4685           0 :                 bat_iterator_end(&pi);
    4686           0 :                 bat authbats[4];
    4687           0 :                 authbats[0] = 0;
    4688           0 :                 authbats[1] = u->batCacheid;
    4689           0 :                 authbats[2] = p->batCacheid;
    4690           0 :                 authbats[3] = d->batCacheid;
    4691           0 :                 if (err == MAL_SUCCEED &&
    4692           0 :                         (BATmode(u, true) != GDK_SUCCEED ||
    4693           0 :                          BATmode(p, true) != GDK_SUCCEED ||
    4694           0 :                          BATmode(d, true) != GDK_SUCCEED ||
    4695           0 :                          BBPrename(u, NULL) != 0 ||
    4696           0 :                          BBPrename(p, NULL) != 0 ||
    4697           0 :                          BBPrename(d, NULL) != 0 ||
    4698           0 :                          TMsubcommit_list(authbats, NULL, 4, -1, -1) != GDK_SUCCEED)) {
    4699           0 :                                 fprintf(stderr, "Committing removal of old user/password BATs failed\n");
    4700             :                 }
    4701           0 :                 BBPunfix(u->batCacheid);
    4702           0 :                 BBPunfix(p->batCacheid);
    4703           0 :                 BBPunfix(d->batCacheid);
    4704             : 
    4705           0 :                 if (err == MAL_SUCCEED) {
    4706           0 :                         sql_schema *s = mvc_bind_schema(sql, "sys");
    4707           0 :                         sql_table *t = mvc_bind_table(sql, s, "roles");
    4708           0 :                         t->system = 0;
    4709           0 :                         t = mvc_bind_table(sql, s, "users");
    4710           0 :                         t->system = 0;
    4711           0 :                         t = mvc_bind_table(sql, s, "dependency_schemas_on_users");
    4712           0 :                         t->system = 0;
    4713           0 :                         pos = 0;
    4714           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    4715             :                                                         "drop view sys.dependency_schemas_on_users cascade;\n"
    4716             :                                                         "drop view sys.roles cascade;\n"
    4717             :                                                         "drop view sys.users cascade;\n"
    4718             :                                                         "drop function sys.db_users() cascade;\n"
    4719             :                                                         "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"
    4720             :                                                         "GRANT SELECT ON sys.roles TO PUBLIC;\n"
    4721             :                                                         "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"
    4722             :                                                         "GRANT SELECT ON sys.users TO PUBLIC;\n"
    4723             :                                                         "CREATE FUNCTION sys.db_users() RETURNS TABLE(name varchar(2048)) RETURN SELECT name FROM sys.db_user_info;\n"
    4724             :                                                         "CREATE VIEW sys.dependency_schemas_on_users AS\n"
    4725             :                                                         "SELECT s.id AS schema_id, s.name AS schema_name, u.name AS user_name, CAST(6 AS smallint) AS depend_type\n"
    4726             :                                                         " FROM sys.db_user_info AS u, sys.schemas AS s\n"
    4727             :                                                         " WHERE u.default_schema = s.id\n"
    4728             :                                                         " ORDER BY s.name, u.name;\n"
    4729             :                                                         "GRANT SELECT ON sys.dependency_schemas_on_users TO PUBLIC;\n"
    4730             :                                                         "update sys._tables set system = true where name in ('users', 'roles', 'dependency_schemas_on_users') AND schema_id = 2000;\n"
    4731             :                                                         "update sys.functions set system = true where system <> true and name in ('db_users') and schema_id = 2000 and type = %d;\n", F_UNION);
    4732           0 :                         assert(pos < bufsize);
    4733           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    4734           0 :                         fflush(stdout);
    4735           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    4736             :                 }
    4737             :         }
    4738          99 :         if (err != MAL_SUCCEED) {
    4739           0 :                 GDKfree(buf);
    4740           0 :                 return err;
    4741             :         }
    4742             : 
    4743             :         /* if 'describe_partition_tables' system view doesn't use 'vals'
    4744             :          * CTE, re-create it; while we're at it, also update the sequence
    4745             :          * dumping code */
    4746          99 :         pos = snprintf(buf, bufsize,
    4747             :                         "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");
    4748          99 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output))) {
    4749           0 :                 GDKfree(buf);
    4750           0 :                 return err;
    4751             :         }
    4752             : 
    4753          99 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) > 0) {
    4754             :                 /* 52_describe.sql; but we need to drop dependencies from 76_dump.sql first */
    4755           0 :                 sql_schema *s = mvc_bind_schema(sql, "sys");
    4756           0 :                 sql_table *t = mvc_bind_table(sql, s, "describe_partition_tables");
    4757           0 :                 t->system = 0;
    4758           0 :                 t = mvc_bind_table(sql, s, "dump_partition_tables");
    4759           0 :                 t->system = 0;
    4760           0 :                 t = mvc_bind_table(sql, s, "dump_sequences");
    4761           0 :                 t->system = 0;
    4762           0 :                 t = mvc_bind_table(sql, s, "dump_start_sequences");
    4763           0 :                 t->system = 0;
    4764           0 :                 t = mvc_bind_table(sql, s, "describe_tables");
    4765           0 :                 t->system = 0;
    4766           0 :                 t = mvc_bind_table(sql, s, "dump_tables");
    4767           0 :                 t->system = 0;
    4768           0 :                 t = mvc_bind_table(sql, s, "dump_create_users");
    4769           0 :                 t->system = 0;
    4770           0 :                 t = mvc_bind_table(sql, s, "dump_functions");
    4771           0 :                 t->system = 0;
    4772           0 :                 t = mvc_bind_table(sql, s, "dump_triggers");
    4773           0 :                 t->system = 0;
    4774             : 
    4775           0 :                 pos = 0;
    4776           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4777             :                         /* drop dependent stuff from 76_dump.sql */
    4778             :                         "drop function sys.dump_database(boolean) cascade;\n"
    4779             :                         "drop procedure sys.dump_table_data() cascade;\n"
    4780             :                         "drop procedure sys.dump_table_data(string, string) cascade;\n"
    4781             :                         "drop view sys.dump_partition_tables cascade;\n"
    4782             :                         "drop view sys.describe_partition_tables cascade;\n"
    4783             :                         "drop view sys.dump_sequences cascade;\n"
    4784             :                         "drop view sys.dump_start_sequences cascade;\n"
    4785             :                         "drop view sys.dump_tables cascade;\n"
    4786             :                         "drop view sys.describe_tables cascade;\n"
    4787             :                         "drop view sys.dump_create_users cascade;\n"
    4788             :                         "drop view sys.dump_functions cascade;\n"
    4789             :                         "drop view sys.dump_triggers cascade;\n"
    4790             :                         "drop function sys.schema_guard cascade;\n"
    4791             :                         "drop function sys.replace_first(string, string, string, string) cascade;\n");
    4792             : 
    4793           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4794             :                         "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
    4795             :                         "RETURN\n"
    4796             :                         " SELECT 'SET SCHEMA ' || sys.dq(sch) || '; ' || stmt;\n"
    4797             :                         "END;\n"
    4798             :                         "CREATE VIEW sys.dump_functions AS\n"
    4799             :                         " SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
    4800             :                         " f.sch schema_name,\n"
    4801             :                         " f.fun function_name\n"
    4802             :                         " FROM sys.describe_functions f;\n"
    4803             :                         "CREATE VIEW sys.dump_triggers AS\n"
    4804             :                         " SELECT sys.schema_guard(sch, tab, def) stmt,\n"
    4805             :                         " sch schema_name,\n"
    4806             :                         " tab table_name,\n"
    4807             :                         " tri trigger_name\n"
    4808             :                         " FROM sys.describe_triggers;\n"
    4809             :                         "CREATE VIEW sys.describe_partition_tables AS\n"
    4810             :                         " SELECT\n"
    4811             :                         " m_sch,\n"
    4812             :                         " m_tbl,\n"
    4813             :                         " p_sch,\n"
    4814             :                         " p_tbl,\n"
    4815             :                         " CASE\n"
    4816             :                         " WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    4817             :                         " 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"
    4818             :                         " ELSE p_raw_type\n"
    4819             :                         " END AS tpe,\n"
    4820             :                         " pvalues,\n"
    4821             :                         " minimum,\n"
    4822             :                         " maximum,\n"
    4823             :                         " with_nulls\n"
    4824             :                         " FROM\n"
    4825             :                         " (WITH\n"
    4826             :                         " tp(\"type\", table_id) AS\n"
    4827             :                         " (SELECT ifthenelse((table_partitions.\"type\" & 2) = 2, 'VALUES', 'RANGE'), table_partitions.table_id FROM sys.table_partitions),\n"
    4828             :                         " subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    4829             :                         " (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    4830             :                         " FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    4831             :                         " WHERE m_t.\"type\" IN (3, 6)\n"
    4832             :                         " AND m_t.schema_id = m_s.id\n"
    4833             :                         " AND m_s.name <> 'tmp'\n"
    4834             :                         " AND m_t.system = FALSE\n"
    4835             :                         " AND m_t.id = d.depend_id\n"
    4836             :                         " AND d.id = p_m.id\n"
    4837             :                         " AND p_m.schema_id = p_s.id\n"
    4838             :                         " ORDER BY m_t.id, p_m.id),\n"
    4839             :                         " vals(id,vals) as\n"
    4840             :                         " (SELECT vp.table_id, GROUP_CONCAT(vp.value, ',') FROM sys.value_partitions vp GROUP BY vp.table_id)\n"
    4841             :                         " SELECT\n"
    4842             :                         " subq.m_sch,\n"
    4843             :                         " subq.m_tbl,\n"
    4844             :                         " subq.p_sch,\n"
    4845             :                         " subq.p_tbl,\n"
    4846             :                         " tp.\"type\" AS p_raw_type,\n"
    4847             :                         " CASE WHEN tp.\"type\" = 'VALUES'\n"
    4848             :                         " THEN (SELECT vals.vals FROM vals WHERE vals.id = subq.p_mid)\n"
    4849             :                         " ELSE NULL\n"
    4850             :                         " END AS pvalues,\n"
    4851             :                         " CASE WHEN tp.\"type\" = 'RANGE'\n"
    4852             :                         " THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    4853             :                         " ELSE NULL\n"
    4854             :                         " END AS minimum,\n"
    4855             :                         " CASE WHEN tp.\"type\" = 'RANGE'\n"
    4856             :                         " THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    4857             :                         " ELSE NULL\n"
    4858             :                         " END AS maximum,\n"
    4859             :                         " CASE WHEN tp.\"type\" = 'VALUES'\n"
    4860             :                         " THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    4861             :                         " ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    4862             :                         " END AS with_nulls\n"
    4863             :                         " FROM\n"
    4864             :                         " subq LEFT OUTER JOIN tp\n"
    4865             :                         " ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    4866             :                         "GRANT SELECT ON sys.describe_partition_tables TO PUBLIC;\n"
    4867             :                         "CREATE VIEW sys.dump_partition_tables AS\n"
    4868             :                         "SELECT\n"
    4869             :                         " 'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    4870             :                         " CASE\n"
    4871             :                         " WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    4872             :                         " 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"
    4873             :                         " WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    4874             :                         " ELSE '' --'READ ONLY'\n"
    4875             :                         " END ||\n"
    4876             :                         " CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    4877             :                         " ';' stmt,\n"
    4878             :                         " m_sch merge_schema_name,\n"
    4879             :                         " m_tbl merge_table_name,\n"
    4880             :                         " p_sch partition_schema_name,\n"
    4881             :                         " p_tbl partition_table_name\n"
    4882             :                         " FROM sys.describe_partition_tables;\n"
    4883             :                         "CREATE VIEW sys.dump_sequences AS\n"
    4884             :                         " SELECT\n"
    4885             :                         " 'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT;' stmt,\n"
    4886             :                         " sch schema_name,\n"
    4887             :                         " seq seqname\n"
    4888             :                         " FROM sys.describe_sequences;\n"
    4889             :                         "CREATE VIEW sys.dump_start_sequences AS\n"
    4890             :                         " SELECT 'ALTER SEQUENCE ' || sys.FQN(sch, seq) ||\n"
    4891             :                         " CASE WHEN s = 0 THEN '' ELSE ' RESTART WITH ' || rs END ||\n"
    4892             :                         " CASE WHEN inc = 1 THEN '' ELSE ' INCREMENT BY ' || inc END ||\n"
    4893             :                         " CASE WHEN nomin THEN ' NO MINVALUE' WHEN rmi IS NULL THEN '' ELSE ' MINVALUE ' || rmi END ||\n"
    4894             :                         " CASE WHEN nomax THEN ' NO MAXVALUE' WHEN rma IS NULL THEN '' ELSE ' MAXVALUE ' || rma END ||\n"
    4895             :                         " CASE WHEN \"cache\" = 1 THEN '' ELSE ' CACHE ' || \"cache\" END ||\n"
    4896             :                         " CASE WHEN \"cycle\" THEN '' ELSE ' NO' END || ' CYCLE;' stmt,\n"
    4897             :                         " sch schema_name,\n"
    4898             :                         " seq sequence_name\n"
    4899             :                         " FROM sys.describe_sequences;\n"
    4900             :                         "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
    4901             :                         "BEGIN\n"
    4902             :                         " DECLARE tid INT;\n"
    4903             :                         " 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"
    4904             :                         " IF tid IS NOT NULL THEN\n"
    4905             :                         " DECLARE k INT;\n"
    4906             :                         " DECLARE m INT;\n"
    4907             :                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    4908             :                         " SET m = (SELECT MAX(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    4909             :                         " IF k IS NOT NULL AND m IS NOT NULL THEN\n"
    4910             :                         " DECLARE cname STRING;\n"
    4911             :                         " DECLARE ctype STRING;\n"
    4912             :                         " DECLARE _cnt INT;\n"
    4913             :                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4914             :                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4915             :                         " SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
    4916             :                         " IF _cnt > 0 THEN\n"
    4917             :                         " DECLARE COPY_INTO_STMT STRING;\n"
    4918             :                         " DECLARE SELECT_DATA_STMT STRING;\n"
    4919             :                         " SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    4920             :                         " SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    4921             :                         " WHILE (k < m) DO\n"
    4922             :                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid AND c.id > k);\n"
    4923             :                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4924             :                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4925             :                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    4926             :                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype));\n"
    4927             :                         " END WHILE;\n"
    4928             :                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    4929             :                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl));\n"
    4930             :                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    4931             :                         " CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    4932             :                         " END IF;\n"
    4933             :                         " END IF;\n"
    4934             :                         " END IF;\n"
    4935             :                         "END;\n"
    4936             :                         "CREATE PROCEDURE sys.dump_table_data()\n"
    4937             :                         "BEGIN\n"
    4938             :                         " DECLARE i INT;\n"
    4939             :                         " 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"
    4940             :                         " IF i IS NOT NULL THEN\n"
    4941             :                         " DECLARE M INT;\n"
    4942             :                         " 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"
    4943             :                         " DECLARE sch STRING;\n"
    4944             :                         " DECLARE tbl STRING;\n"
    4945             :                         " WHILE i IS NOT NULL AND i <= M DO\n"
    4946             :                         " SET sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4947             :                         " SET tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4948             :                         " CALL sys.dump_table_data(sch, tbl);\n"
    4949             :                         " 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"
    4950             :                         " END WHILE;\n"
    4951             :                         " END IF;\n"
    4952             :                         "END;\n"
    4953             :                         "CREATE VIEW sys.dump_create_users AS\n"
    4954             :                         " SELECT\n"
    4955             :                         " 'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
    4956             :                         " sys.sq(sys.password_hash(ui.name)) ||\n"
    4957             :                         " ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
    4958             :                         " ui.name user_name\n"
    4959             :                         " FROM sys.db_user_info ui, sys.schemas s\n"
    4960             :                         " WHERE ui.default_schema = s.id\n"
    4961             :                         " AND ui.name <> 'monetdb'\n"
    4962             :                         " AND ui.name <> '.snapshot';\n");
    4963             : 
    4964           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    4965             :                         "CREATE VIEW sys.describe_tables AS\n"
    4966             :                         " SELECT\n"
    4967             :                         " t.id o,\n"
    4968             :                         " s.name sch,\n"
    4969             :                         " t.name tab,\n"
    4970             :                         " ts.table_type_name typ,\n"
    4971             :                         " (SELECT\n"
    4972             :                         " ' (' ||\n"
    4973             :                         " GROUP_CONCAT(\n"
    4974             :                         " sys.DQ(c.name) || ' ' ||\n"
    4975             :                         " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    4976             :                         " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    4977             :                         " , ', ') || ')'\n"
    4978             :                         " FROM sys._columns c\n"
    4979             :                         " WHERE c.table_id = t.id) col,\n"
    4980             :                         " CASE ts.table_type_name\n"
    4981             :                         " WHEN 'REMOTE TABLE' THEN\n"
    4982             :                         " sys.get_remote_table_expressions(s.name, t.name)\n"
    4983             :                         " WHEN 'MERGE TABLE' THEN\n"
    4984             :                         " sys.get_merge_table_partition_expressions(t.id)\n"
    4985             :                         " WHEN 'VIEW' THEN\n"
    4986             :                         " sys.schema_guard(s.name, t.name, t.query)\n"
    4987             :                         " ELSE\n"
    4988             :                         " ''\n"
    4989             :                         " END opt\n"
    4990             :                         " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    4991             :                         " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
    4992             :                         " AND t.system = FALSE\n"
    4993             :                         " AND s.id = t.schema_id\n"
    4994             :                         " AND ts.table_type_id = t.type\n"
    4995             :                         " AND s.name <> 'tmp';\n"
    4996             :                         "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
    4997             :                         "CREATE VIEW sys.dump_tables AS\n"
    4998             :                         " SELECT\n"
    4999             :                         " t.o o,\n"
    5000             :                         " CASE\n"
    5001             :                         " WHEN t.typ <> 'VIEW' THEN\n"
    5002             :                         " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    5003             :                         " ELSE\n"
    5004             :                         " t.opt\n"
    5005             :                         " END stmt,\n"
    5006             :                         " t.sch schema_name,\n"
    5007             :                         " t.tab table_name\n"
    5008             :                         " FROM sys.describe_tables t;\n"
    5009             :                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    5010             :                         "BEGIN\n"
    5011             :                         " SET SCHEMA sys;\n"
    5012             :                         " TRUNCATE sys.dump_statements;\n"
    5013             :                         " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    5014             :                         " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
    5015             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    5016             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    5017             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    5018             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    5019             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    5020             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    5021             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    5022             :                         " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    5023             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    5024             :                         " FROM (\n"
    5025             :                         " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    5026             :                         " UNION ALL\n"
    5027             :                         " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    5028             :                         " ) AS stmts(o, s);\n"
    5029             :                         " -- dump table data before adding constraints and fixing sequences\n"
    5030             :                         " IF NOT DESCRIBE THEN\n"
    5031             :                         " CALL sys.dump_table_data();\n"
    5032             :                         " END IF;\n"
    5033             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    5034             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    5035             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    5036             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    5037             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    5038             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    5039             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    5040             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    5041             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    5042             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    5043             :                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    5044             :                         " --TODO Improve performance of dump_table_data.\n"
    5045             :                         " --TODO loaders ,procedures, window and filter sys.functions.\n"
    5046             :                         " --TODO look into order dependent group_concat\n"
    5047             :                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    5048             :                         " RETURN sys.dump_statements;\n"
    5049             :                         "END;\n");
    5050           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5051             :                         "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");
    5052           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5053             :                         "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);
    5054           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5055             :                         "update sys.functions set system = true where system <> true and name in ('dump_database') and schema_id = 2000 and type = %d;\n", F_UNION);
    5056           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5057             :                         "update sys.functions set system = true where system <> true and name in ('schema_guard') and schema_id = 2000 and type = %d;\n", F_FUNC);
    5058             : 
    5059             :                 /* 12_url.sql */
    5060           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    5061             :                                                 "CREATE function sys.url_extract_host(url string, no_www bool) RETURNS STRING\n"
    5062             :                                                 "EXTERNAL NAME url.\"extractURLHost\";\n"
    5063             :                                                 "GRANT EXECUTE ON FUNCTION url_extract_host(string, bool) TO PUBLIC;\n"
    5064             :                                                 "update sys.functions set system = true where system <> true and name = 'url_extract_host' and schema_id = 2000 and type = %d;\n", F_FUNC);
    5065             : 
    5066           0 :                 assert(pos < bufsize);
    5067           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5068           0 :                 fflush(stdout);
    5069           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5070             :         }
    5071          99 :         res_table_destroy(output);
    5072          99 :         output = NULL;
    5073          99 :         if (err != MAL_SUCCEED) {
    5074           0 :                 GDKfree(buf);
    5075           0 :                 return err;
    5076             :         }
    5077             : 
    5078             :         /* 10_sys_schema_extensions */
    5079             :         /* if the keyword LOCKED is in the list of keywords, upgrade */
    5080          99 :         pos = snprintf(buf, bufsize, "select keyword from sys.keywords where keyword = 'LOCKED';\n");
    5081          99 :         assert(pos < bufsize);
    5082          99 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    5083           0 :                 goto bailout;
    5084          99 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) > 0) {
    5085           0 :                 pos = snprintf(buf, bufsize,
    5086             :                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    5087             :                         "DELETE FROM sys.keywords WHERE keyword IN ('LOCKED');\n");
    5088           0 :                 assert(pos < bufsize);
    5089           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5090           0 :                 fflush(stdout);
    5091           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5092           0 :                 if (err == MAL_SUCCEED) {
    5093           0 :                         pos = snprintf(buf, bufsize, "ALTER TABLE sys.keywords SET READ ONLY;\n");
    5094           0 :                         assert(pos < bufsize);
    5095           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5096           0 :                         fflush(stdout);
    5097           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5098             :                 }
    5099             :         }
    5100          99 :         res_table_destroy(output);
    5101          99 :         output = NULL;
    5102          99 :         if (err != MAL_SUCCEED) {
    5103           0 :                 GDKfree(buf);
    5104           0 :                 return err;
    5105             :         }
    5106             : 
    5107             :         /* if the table type UNLOGGED TABLE is not in the list of table
    5108             :          * types, upgrade */
    5109          99 :         pos = snprintf(buf, bufsize, "select table_type_name from sys.table_types where table_type_name = 'UNLOGGED TABLE';\n");
    5110          99 :         assert(pos < bufsize);
    5111          99 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    5112           0 :                 goto bailout;
    5113          99 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
    5114           0 :                 pos = snprintf(buf, bufsize,
    5115             :                                 "ALTER TABLE sys.table_types SET READ WRITE;\n"
    5116             :                                 "INSERT INTO sys.table_types VALUES (7, 'UNLOGGED TABLE');\n");
    5117           0 :                 assert(pos < bufsize);
    5118           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5119           0 :                 fflush(stdout);
    5120           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5121           0 :                 if (err == MAL_SUCCEED) {
    5122           0 :                         pos = snprintf(buf, bufsize, "ALTER TABLE sys.table_types SET READ ONLY;\n");
    5123           0 :                         assert(pos < bufsize);
    5124           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5125           0 :                         fflush(stdout);
    5126           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5127             :                 }
    5128             :         }
    5129          99 :         res_table_destroy(output);
    5130          99 :         output = NULL;
    5131             : 
    5132             :         /* 16_tracelog */
    5133          99 :         pos = snprintf(buf, bufsize,
    5134             :                                    "select f.id "
    5135             :                                    "from sys.schemas s, "
    5136             :                                                 "sys.functions f, "
    5137             :                                                 "sys.auths a, "
    5138             :                                                 "sys.privileges p, "
    5139             :                                                 "sys.auths g, "
    5140             :                                                 "sys.function_types ft, "
    5141             :                                                 "sys.privilege_codes pc "
    5142             :                                    "where s.id = f.schema_id "
    5143             :                                          "and f.id = p.obj_id "
    5144             :                                          "and p.auth_id = a.id "
    5145             :                                          "and p.grantor = g.id "
    5146             :                                          "and p.privileges = pc.privilege_code_id "
    5147             :                                          "and f.type = ft.function_type_id "
    5148             :                                          "and s.name = 'sys' "
    5149             :                                          "and f.name = 'tracelog' "
    5150             :                                          "and ft.function_type_keyword = 'FUNCTION';\n");
    5151          99 :         assert(pos < bufsize);
    5152          99 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    5153           0 :                 goto bailout;
    5154          99 :         if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
    5155           0 :                 pos = snprintf(buf, bufsize,
    5156             :                                            "grant execute on function sys.tracelog to public;\n"
    5157             :                                            "grant select on sys.tracelog to public;\n");
    5158           0 :                 assert(pos < bufsize);
    5159           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5160           0 :                 fflush(stdout);
    5161           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5162             :         }
    5163          99 :         res_table_destroy(output);
    5164          99 :         output = NULL;
    5165          99 :         if (err != MAL_SUCCEED) {
    5166           0 :                 GDKfree(buf);
    5167           0 :                 return err;
    5168             :         }
    5169             : 
    5170          99 : bailout:
    5171          99 :         if (output)
    5172           0 :                 res_table_destroy(output);
    5173          99 :         GDKfree(buf);
    5174          99 :         return err;             /* usually MAL_SUCCEED */
    5175             : }
    5176             : 
    5177             : static str
    5178          99 : sql_update_jun2023(Client c, mvc *sql, sql_schema *s)
    5179             : {
    5180          99 :         size_t bufsize = 65536, pos = 0;
    5181          99 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    5182          99 :         res_table *output;
    5183          99 :         BAT *b;
    5184          99 :         sql_subtype t1, t2;
    5185             : 
    5186          99 :         (void) sql;
    5187          99 :         if (buf == NULL)
    5188           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5189             : 
    5190             :         /* wlc/wlr support was removed */
    5191             :         {
    5192          99 :                 sql_schema *wl = mvc_bind_schema(sql, "wlc");
    5193          99 :                 sql_schema *wr = mvc_bind_schema(sql, "wlr");
    5194          99 :                 if (wl != NULL || wr != NULL) {
    5195           0 :                         if (wl)
    5196           0 :                                 wl->system = 0;
    5197           0 :                         if (wr)
    5198           0 :                                 wr->system = 0;
    5199             : 
    5200           0 :                         const char *query =
    5201             :                                 "drop procedure if exists wlc.master() cascade;\n"
    5202             :                                 "drop procedure if exists wlc.master(string) cascade;\n"
    5203             :                                 "drop procedure if exists wlc.stop() cascade;\n"
    5204             :                                 "drop procedure if exists wlc.flush() cascade;\n"
    5205             :                                 "drop procedure if exists wlc.beat(int) cascade;\n"
    5206             :                                 "drop function if exists wlc.clock() cascade;\n"
    5207             :                                 "drop function if exists wlc.tick() cascade;\n"
    5208             :                                 "drop procedure if exists wlr.master(string) cascade;\n"
    5209             :                                 "drop procedure if exists wlr.stop() cascade;\n"
    5210             :                                 "drop procedure if exists wlr.accept() cascade;\n"
    5211             :                                 "drop procedure if exists wlr.replicate() cascade;\n"
    5212             :                                 "drop procedure if exists wlr.replicate(timestamp) cascade;\n"
    5213             :                                 "drop procedure if exists wlr.replicate(tinyint) cascade;\n"
    5214             :                                 "drop procedure if exists wlr.replicate(smallint) cascade;\n"
    5215             :                                 "drop procedure if exists wlr.replicate(integer) cascade;\n"
    5216             :                                 "drop procedure if exists wlr.replicate(bigint) cascade;\n"
    5217             :                                 "drop procedure if exists wlr.beat(integer) cascade;\n"
    5218             :                                 "drop function if exists wlr.clock() cascade;\n"
    5219             :                                 "drop function if exists wlr.tick() cascade;\n"
    5220             :                                 "drop schema if exists wlc cascade;\n"
    5221             :                                 "drop schema if exists wlr cascade;\n";
    5222           0 :                         printf("Running database upgrade commands:\n%s\n", query);
    5223           0 :                         fflush(stdout);
    5224           0 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5225             :                 }
    5226             :         }
    5227             : 
    5228             :         /* new function sys.regexp_replace */
    5229          99 :         sql_allocator *old_sa = sql->sa;
    5230          99 :         if ((sql->sa = sa_create(sql->pa)) != NULL) {
    5231          99 :                 list *l;
    5232          99 :                 if ((l = sa_list(sql->sa)) != NULL) {
    5233          99 :                         sql_subtype tp;
    5234          99 :                         sql_find_subtype(&tp, "varchar", 0, 0);
    5235          99 :                         list_append(l, &tp);
    5236          99 :                         list_append(l, &tp);
    5237          99 :                         list_append(l, &tp);
    5238          99 :                         list_append(l, &tp);
    5239          99 :                         if (!sql_bind_func_(sql, s->base.name, "regexp_replace", l, F_FUNC, true)) {
    5240           0 :                                 pos = snprintf(buf, bufsize,
    5241             :                                                            "create function sys.regexp_replace(ori string, pat string, rep string, flg string)\n"
    5242             :                                                            "returns string external name pcre.replace;\n"
    5243             :                                                            "grant execute on function regexp_replace(string, string, string, string) to public;\n"
    5244             :                                                            "create function sys.regexp_replace(ori string, pat string, rep string)\n"
    5245             :                                                            "returns string\n"
    5246             :                                                            "begin\n"
    5247             :                                                            " return sys.regexp_replace(ori, pat, rep, '');\n"
    5248             :                                                            "end;\n"
    5249             :                                                            "grant execute on function regexp_replace(string, string, string) to public;\n"
    5250             :                                                            "update sys.functions set system = true where system <> true and name = 'regexp_replace' and schema_id = 2000 and type = %d;\n",
    5251             :                                                            F_FUNC);
    5252           0 :                                 assert(pos < bufsize);
    5253           0 :                                 sql->session->status = 0;
    5254           0 :                                 sql->errstr[0] = '\0';
    5255           0 :                                 printf("Running database upgrade commands:\n%s\n", buf);
    5256           0 :                                 fflush(stdout);
    5257           0 :                                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5258             :                         }
    5259          99 :                         sa_destroy(sql->sa);
    5260             :                 }
    5261             :         }
    5262          99 :         sql->sa = old_sa;
    5263             : 
    5264             :         /* fixes for handling single quotes in strings so that we can run
    5265             :          * with raw_strings after having created a database without (and
    5266             :          * v.v.) */
    5267          99 :         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) {
    5268          99 :                 if (((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) || find_sql_table(sql->session->tr, s, "remote_user_info") == NULL) {
    5269           0 :                         sql_table *t;
    5270           0 :                         if ((t = mvc_bind_table(sql, s, "describe_tables")) != NULL)
    5271           0 :                                 t->system = 0;
    5272           0 :                         if ((t = mvc_bind_table(sql, s, "dump_create_users")) != NULL)
    5273           0 :                                 t->system = 0;
    5274           0 :                         if ((t = mvc_bind_table(sql, s, "dump_partition_tables")) != NULL)
    5275           0 :                                 t->system = 0;
    5276           0 :                         if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
    5277           0 :                                 t->system = 0;
    5278           0 :                         if ((t = mvc_bind_table(sql, s, "dump_tables")) != NULL)
    5279           0 :                                 t->system = 0;
    5280           0 :                         pos = 0;
    5281           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    5282             :                                                         "drop function if exists sys.dump_database(boolean) cascade;\n"
    5283             :                                                         "drop procedure if exists sys.dump_table_data() cascade;\n"
    5284             :                                                         "drop procedure if exists sys.dump_table_data(string, string) cascade;\n"
    5285             :                                                         "drop view if exists sys.dump_tables cascade;\n"
    5286             :                                                         "drop view if exists sys.dump_comments cascade;\n"
    5287             :                                                         "drop function if exists sys.prepare_esc(string, string) cascade;\n"
    5288             :                                                         "drop view if exists sys.dump_partition_tables cascade;\n"
    5289             :                                                         "drop view if exists sys.dump_create_users cascade;\n"
    5290             :                                                         "drop view if exists sys.describe_tables cascade;\n"
    5291             :                                                         "drop function if exists sys.get_remote_table_expressions(string, string) cascade;\n"
    5292             :                                                         "drop function if exists sys.remote_table_credentials(string) cascade;\n"
    5293             :                                                         "drop function if exists sys.sq(string) cascade;\n");
    5294           0 :                         if (find_sql_table(sql->session->tr, s, "remote_user_info") == NULL) {
    5295           0 :                                 pos += snprintf(buf + pos, bufsize - pos,
    5296             :                                                                 "create table sys.remote_user_info (table_id int, username varchar(1024), password varchar(256));\n"
    5297             :                                                                 "create function sys.decypher (cypher string) returns string external name sql.decypher;\n"
    5298             :                                                                 "update sys.functions set system = true where system <> true and name = 'decypher' and schema_id = 2000 and type = %d;\n"
    5299             :                                                                 "update sys._tables set system = true where system <> true and name = 'remote_user_info' and schema_id = 2000;\n",
    5300             :                                                                 F_FUNC);
    5301             :                         }
    5302           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    5303             :                                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN '''' || sys.replace(s,'''','''''') || ''''; END;\n"
    5304             :                                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    5305             :                                                         " 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"
    5306             :                                                         "END;\n"
    5307             :                                                         "CREATE VIEW sys.describe_tables AS\n"
    5308             :                                                         " SELECT\n"
    5309             :                                                         " t.id o,\n"
    5310             :                                                         " s.name sch,\n"
    5311             :                                                         " t.name tab,\n"
    5312             :                                                         " ts.table_type_name typ,\n"
    5313             :                                                         " (SELECT\n"
    5314             :                                                         " ' (' ||\n"
    5315             :                                                         " GROUP_CONCAT(\n"
    5316             :                                                         " sys.DQ(c.name) || ' ' ||\n"
    5317             :                                                         " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    5318             :                                                         " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    5319             :                                                         " , ', ') || ')'\n"
    5320             :                                                         " FROM sys._columns c\n"
    5321             :                                                         " WHERE c.table_id = t.id) col,\n"
    5322             :                                                         " CASE ts.table_type_name\n"
    5323             :                                                         " WHEN 'REMOTE TABLE' THEN\n"
    5324             :                                                         " sys.get_remote_table_expressions(s.name, t.name)\n"
    5325             :                                                         " WHEN 'MERGE TABLE' THEN\n"
    5326             :                                                         " sys.get_merge_table_partition_expressions(t.id)\n"
    5327             :                                                         " WHEN 'VIEW' THEN\n"
    5328             :                                                         " sys.schema_guard(s.name, t.name, t.query)\n"
    5329             :                                                         " ELSE\n"
    5330             :                                                         " ''\n"
    5331             :                                                         " END opt\n"
    5332             :                                                         " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    5333             :                                                         " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
    5334             :                                                         " AND t.system = FALSE\n"
    5335             :                                                         " AND s.id = t.schema_id\n"
    5336             :                                                         " AND ts.table_type_id = t.type\n"
    5337             :                                                         " AND s.name <> 'tmp';\n"
    5338             :                                                         "CREATE VIEW sys.dump_create_users AS\n"
    5339             :                                                         " SELECT\n"
    5340             :                                                         " 'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
    5341             :                                                         " sys.sq(sys.password_hash(ui.name)) ||\n"
    5342             :                                                         " ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
    5343             :                                                         " ui.name user_name\n"
    5344             :                                                         " FROM sys.db_user_info ui, sys.schemas s\n"
    5345             :                                                         " WHERE ui.default_schema = s.id\n"
    5346             :                                                         " AND ui.name <> 'monetdb'\n"
    5347             :                                                         " AND ui.name <> '.snapshot';\n"
    5348             :                                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    5349             :                                                         " SELECT\n"
    5350             :                                                         " 'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    5351             :                                                         " CASE\n"
    5352             :                                                         " WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    5353             :                                                         " 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"
    5354             :                                                         " WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    5355             :                                                         " ELSE '' --'READ ONLY'\n"
    5356             :                                                         " END ||\n"
    5357             :                                                         " CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    5358             :                                                         " ';' stmt,\n"
    5359             :                                                         " m_sch merge_schema_name,\n"
    5360             :                                                         " m_tbl merge_table_name,\n"
    5361             :                                                         " p_sch partition_schema_name,\n"
    5362             :                                                         " p_tbl partition_table_name\n"
    5363             :                                                         " FROM sys.describe_partition_tables;\n"
    5364             :                                                         "CREATE VIEW sys.dump_tables AS\n"
    5365             :                                                         " SELECT\n"
    5366             :                                                         " t.o o,\n"
    5367             :                                                         " CASE\n"
    5368             :                                                         " WHEN t.typ <> 'VIEW' THEN\n"
    5369             :                                                         " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    5370             :                                                         " ELSE\n"
    5371             :                                                         " t.opt\n"
    5372             :                                                         " END stmt,\n"
    5373             :                                                         " t.sch schema_name,\n"
    5374             :                                                         " t.tab table_name\n"
    5375             :                                                         " FROM sys.describe_tables t;\n"
    5376             :                                                         "CREATE VIEW sys.dump_comments AS\n"
    5377             :                                                         " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    5378             :                                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    5379             :                                                         "BEGIN\n"
    5380             :                                                         " RETURN\n"
    5381             :                                                         " CASE\n"
    5382             :                                                         " WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    5383             :                                                         " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    5384             :                                                         " ELSE\n"
    5385             :                                                         " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    5386             :                                                         " END;\n"
    5387             :                                                         "END;\n"
    5388             :                                                         "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
    5389             :                                                         "BEGIN\n"
    5390             :                                                         " DECLARE tid INT;\n"
    5391             :                                                         " 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"
    5392             :                                                         " IF tid IS NOT NULL THEN\n"
    5393             :                                                         " DECLARE k INT;\n"
    5394             :                                                         " DECLARE m INT;\n"
    5395             :                                                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    5396             :                                                         " SET m = (SELECT MAX(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
    5397             :                                                         " IF k IS NOT NULL AND m IS NOT NULL THEN\n"
    5398             :                                                         " DECLARE cname STRING;\n"
    5399             :                                                         " DECLARE ctype STRING;\n"
    5400             :                                                         " DECLARE _cnt INT;\n"
    5401             :                                                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    5402             :                                                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    5403             :                                                         " SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
    5404             :                                                         " IF _cnt > 0 THEN\n"
    5405             :                                                         " DECLARE COPY_INTO_STMT STRING;\n"
    5406             :                                                         " DECLARE SELECT_DATA_STMT STRING;\n"
    5407             :                                                         " SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    5408             :                                                         " SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    5409             :                                                         " WHILE (k < m) DO\n"
    5410             :                                                         " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid AND c.id > k);\n"
    5411             :                                                         " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    5412             :                                                         " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    5413             :                                                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    5414             :                                                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype));\n"
    5415             :                                                         " END WHILE;\n"
    5416             :                                                         " SET COPY_INTO_STMT = (COPY_INTO_STMT || R') FROM STDIN USING DELIMITERS ''|'',E''\\n'',''\"'';');\n"
    5417             :                                                         " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl));\n"
    5418             :                                                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    5419             :                                                         " CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    5420             :                                                         " END IF;\n"
    5421             :                                                         " END IF;\n"
    5422             :                                                         " END IF;\n"
    5423             :                                                         " END;\n"
    5424             :                                                         "CREATE PROCEDURE sys.dump_table_data()\n"
    5425             :                                                         "BEGIN\n"
    5426             :                                                         " DECLARE i INT;\n"
    5427             :                                                         " 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"
    5428             :                                                         " IF i IS NOT NULL THEN\n"
    5429             :                                                         " DECLARE M INT;\n"
    5430             :                                                         " 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"
    5431             :                                                         " DECLARE sch STRING;\n"
    5432             :                                                         " DECLARE tbl STRING;\n"
    5433             :                                                         " WHILE i IS NOT NULL AND i <= M DO\n"
    5434             :                                                         " SET sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    5435             :                                                         " SET tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    5436             :                                                         " CALL sys.dump_table_data(sch, tbl);\n"
    5437             :                                                         " 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"
    5438             :                                                         " END WHILE;\n"
    5439             :                                                         " END IF;\n"
    5440             :                                                         "END;\n"
    5441             :                                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    5442             :                                                         "BEGIN\n"
    5443             :                                                         " SET SCHEMA sys;\n"
    5444             :                                                         " TRUNCATE sys.dump_statements;\n"
    5445             :                                                         " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    5446             :                                                         " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
    5447             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    5448             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    5449             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    5450             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    5451             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    5452             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    5453             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    5454             :                                                         " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    5455             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    5456             :                                                         " FROM (\n"
    5457             :                                                         " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    5458             :                                                         " UNION ALL\n"
    5459             :                                                         " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    5460             :                                                         " ) AS stmts(o, s);\n"
    5461             :                                                         " -- dump table data before adding constraints and fixing sequences\n"
    5462             :                                                         " IF NOT DESCRIBE THEN\n"
    5463             :                                                         " CALL sys.dump_table_data();\n"
    5464             :                                                         " END IF;\n"
    5465             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    5466             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    5467             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    5468             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    5469             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    5470             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    5471             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    5472             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    5473             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    5474             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    5475             :                                                         " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    5476             :                                                         " --TODO Improve performance of dump_table_data.\n"
    5477             :                                                         " --TODO loaders ,procedures, window and filter sys.functions.\n"
    5478             :                                                         " --TODO look into order dependent group_concat\n"
    5479             :                                                         " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    5480             :                                                         " RETURN sys.dump_statements;\n"
    5481             :                                                         "END;\n"
    5482             :                                                         "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"
    5483             :                                                         "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"
    5484             :                                                         "update sys.functions set system = true where system <> true and name = 'dump_table_data' and schema_id = 2000 and type = %d;\n"
    5485             :                                                         "update sys.functions set system = true where system <> true and name = 'dump_database' and schema_id = 2000 and type = %d;\n"
    5486             :                                                         "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n",
    5487             :                                                         F_FUNC, F_PROC, F_UNION);
    5488           0 :                         assert(pos < bufsize);
    5489           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5490           0 :                         fflush(stdout);
    5491           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5492             :                 }
    5493          99 :                 res_table_destroy(output);
    5494          99 :                 output = NULL;
    5495             :         }
    5496             : 
    5497             :         /* Add new column 'function_id' to views
    5498             :          * sys.dependency_tables_on_functions and dependency_views_on_functions */
    5499             :         {
    5500          99 :                 sql_table *t = find_sql_table(sql->session->tr, s, "dependency_tables_on_functions");
    5501          99 :                 if (t != NULL && find_sql_column(t, "function_id") == NULL) {
    5502           0 :                         t->system = 0;               /* sys.dependency_tables_on_functions */
    5503           0 :                         if ((t = mvc_bind_table(sql, s, "dependency_views_on_functions")) != NULL)
    5504           0 :                                 t->system = 0;
    5505           0 :                         pos = 0;
    5506           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    5507             :                                                         "drop view if exists sys.dependency_tables_on_functions cascade;\n"
    5508             :                                                         "drop view if exists sys.dependency_views_on_functions cascade;\n"
    5509             :                                                         "CREATE VIEW sys.dependency_tables_on_functions AS\n"
    5510             :                                                         "SELECT t.schema_id AS table_schema_id, t.id AS table_id, t.name AS table_name,"
    5511             :                                                         " f.id AS function_id, f.name AS function_name, f.type AS function_type, dep.depend_type AS depend_type\n"
    5512             :                                                         "  FROM sys.functions AS f, sys.tables AS t, sys.dependencies AS dep\n"
    5513             :                                                         " WHERE t.id = dep.id AND f.id = dep.depend_id\n"
    5514             :                                                         "   AND dep.depend_type = 7 AND f.type <> 2 AND t.type NOT IN (1, 11)\n"
    5515             :                                                         " ORDER BY t.name, t.schema_id, f.name, f.id;\n"
    5516             :                                                         "GRANT SELECT ON sys.dependency_tables_on_functions TO PUBLIC;\n"
    5517             :                                                         "CREATE VIEW sys.dependency_views_on_functions AS\n"
    5518             :                                                         "SELECT v.schema_id AS view_schema_id, v.id AS view_id, v.name AS view_name,"
    5519             :                                                         " f.id AS function_id, f.name AS function_name, f.type AS function_type, dep.depend_type AS depend_type\n"
    5520             :                                                         "  FROM sys.functions AS f, sys.tables AS v, sys.dependencies AS dep\n"
    5521             :                                                         " WHERE v.id = dep.id AND f.id = dep.depend_id\n"
    5522             :                                                         "   AND dep.depend_type = 7 AND f.type <> 2 AND v.type IN (1, 11)\n"
    5523             :                                                         " ORDER BY v.name, v.schema_id, f.name, f.id;\n"
    5524             :                                                         "GRANT SELECT ON sys.dependency_views_on_functions TO PUBLIC;\n"
    5525             :                                                         "update sys._tables set system = true where system <> true and name in "
    5526             :                                                         "('dependency_tables_on_functions','dependency_views_on_functions') and schema_id = 2000;\n");
    5527           0 :                         assert(pos < bufsize);
    5528           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    5529           0 :                         fflush(stdout);
    5530           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5531             :                 }
    5532             :         }
    5533             : 
    5534          99 :         if (!sql_bind_func(sql, "sys", "database", NULL, NULL, F_FUNC, true)) {
    5535           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5536           0 :                 sql->errstr[0] = '\0';
    5537           0 :                 pos = snprintf(buf, bufsize,
    5538             :                                            "create function sys.database ()\n"
    5539             :                                            "returns string\n"
    5540             :                                            "external name inspect.\"getDatabaseName\";\n"
    5541             :                                            "grant execute on function sys.database() to public;\n"
    5542             :                                            "update sys.functions set system = true where system <> true and name = 'database' and schema_id = 2000 and type = %d;\n",
    5543             :                                            (int) F_FUNC);
    5544           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5545           0 :                 fflush(stdout);
    5546           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5547             :         }
    5548             : 
    5549             :         /* Add new sysadmin procedure calls: stop, pause and resume with two
    5550             :            arguments, first arg is query OID and second the user username that
    5551             :            the query in bound to. */
    5552          99 :         sql_find_subtype(&t1, "bigint", 64, 0);
    5553          99 :         sql_find_subtype(&t2, "varchar", 0, 0);
    5554          99 :         if (!sql_bind_func(sql, "sys", "pause", &t1, &t2, F_PROC, true)) {
    5555           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5556           0 :                 sql->errstr[0] = '\0';
    5557           0 :                 const char *query =
    5558             :                         "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"
    5559             :                         "create procedure sys.pause(tag bigint, username string) external name sysmon.pause;\n"
    5560             :                         "create procedure sys.resume(tag bigint, username string) external name sysmon.resume;\n"
    5561             :                         "create procedure sys.stop(tag bigint, username string) external name sysmon.stop;\n"
    5562             :                         "update sys.functions set system = true where system <> true and mod = 'sysmon' and name in ('stop', 'pause', 'resume', 'queue');\n";
    5563           0 :                 printf("Running database upgrade commands:\n%s\n", query);
    5564           0 :                 fflush(stdout);
    5565           0 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5566             :         }
    5567             : 
    5568             :         /* sys.settimeout and sys.setsession where removed */
    5569          99 :         if (sql_bind_func(sql, "sys", "settimeout", &t1, NULL, F_PROC, true)) {
    5570           0 :                 const char *query =
    5571             :                         "drop procedure sys.settimeout(bigint) cascade;\n"
    5572             :                         "drop procedure sys.settimeout(bigint, bigint) cascade;\n"
    5573             :                         "drop procedure sys.setsession(bigint) cascade;\n";
    5574           0 :                 printf("Running database upgrade commands:\n%s\n", query);
    5575           0 :                 fflush(stdout);
    5576           0 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5577             :         }
    5578          99 :         sql->session->status = 0; /* if the function was not found clean the error */
    5579          99 :         sql->errstr[0] = '\0';
    5580             : 
    5581          99 :         if (!sql_bind_func(sql, "sys", "jarowinkler", &t2, &t2, F_FUNC, true)) {
    5582           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5583           0 :                 sql->errstr[0] = '\0';
    5584           0 :                 pos = snprintf(buf, bufsize,
    5585             :                                            "create function sys.levenshtein(x string, y string)\n"
    5586             :                                            "returns int external name txtsim.levenshtein;\n"
    5587             :                                            "grant execute on function levenshtein(string, string) to public;\n"
    5588             :                                            "create function sys.levenshtein(x string, y string, insdel int, rep int)\n"
    5589             :                                            "returns int external name txtsim.levenshtein;\n"
    5590             :                                            "grant execute on function levenshtein(string, string, int, int) to public;\n"
    5591             :                                            "create function sys.levenshtein(x string, y string, insdel int, rep int, trans int)\n"
    5592             :                                            "returns int external name txtsim.levenshtein;\n"
    5593             :                                            "grant execute on function levenshtein(string, string, int, int, int) to public;\n"
    5594             :                                            "create filter function sys.maxlevenshtein(x string, y string, k int)\n"
    5595             :                                            "external name txtsim.maxlevenshtein;\n"
    5596             :                                            "grant execute on filter function maxlevenshtein(string, string, int) to public;\n"
    5597             :                                            "create filter function sys.maxlevenshtein(x string, y string, k int, insdel int, rep int)\n"
    5598             :                                            "external name txtsim.maxlevenshtein;\n"
    5599             :                                            "grant execute on filter function maxlevenshtein(string, string, int, int, int) to public;\n"
    5600             :                                            "create function sys.jarowinkler(x string, y string)\n"
    5601             :                                            "returns double external name txtsim.jarowinkler;\n"
    5602             :                                            "grant execute on function jarowinkler(string, string) to public;\n"
    5603             :                                            "create filter function minjarowinkler(x string, y string, threshold double)\n"
    5604             :                                            "external name txtsim.minjarowinkler;\n"
    5605             :                                            "grant execute on filter function minjarowinkler(string, string, double) to public;\n"
    5606             :                                            "create function sys.dameraulevenshtein(x string, y string)\n"
    5607             :                                            "returns int external name txtsim.dameraulevenshtein;\n"
    5608             :                                            "grant execute on function dameraulevenshtein(string, string) to public;\n"
    5609             :                                            "create function sys.dameraulevenshtein(x string, y string, insdel int, rep int, trans int)\n"
    5610             :                                            "returns int external name txtsim.dameraulevenshtein;\n"
    5611             :                                            "grant execute on function dameraulevenshtein(string, string, int, int, int) to public;\n"
    5612             : 
    5613             :                                            "create function sys.editdistance(x string, y string)\n"
    5614             :                                            "returns int external name txtsim.editdistance;\n"
    5615             :                                            "grant execute on function editdistance(string, string) to public;\n"
    5616             :                                            "create function sys.editdistance2(x string, y string)\n"
    5617             :                                            "returns int external name txtsim.editdistance2;\n"
    5618             :                                            "grant execute on function editdistance2(string, string) to public;\n"
    5619             :                                            "create function sys.soundex(x string)\n"
    5620             :                                            "returns string external name txtsim.soundex;\n"
    5621             :                                            "grant execute on function soundex(string) to public;\n"
    5622             :                                            "create function sys.difference(x string, y string)\n"
    5623             :                                            "returns int external name txtsim.stringdiff;\n"
    5624             :                                            "grant execute on function difference(string, string) to public;\n"
    5625             :                                            "create function sys.qgramnormalize(x string)\n"
    5626             :                                            "returns string external name txtsim.qgramnormalize;\n"
    5627             :                                            "grant execute on function qgramnormalize(string) to public;\n"
    5628             : 
    5629             :                                            "create function asciify(x string)\n"
    5630             :                                            "returns string external name str.asciify;\n"
    5631             :                                            "grant execute on function asciify(string) to public;\n"
    5632             :                                            "create function sys.startswith(x string, y string)\n"
    5633             :                                            "returns boolean external name str.startswith;\n"
    5634             :                                            "grant execute on function startswith(string, string) to public;\n"
    5635             :                                            "create function sys.startswith(x string, y string, icase boolean)\n"
    5636             :                                            "returns boolean external name str.startswith;\n"
    5637             :                                            "grant execute on function startswith(string, string, boolean) to public;\n"
    5638             :                                            "create filter function sys.startswith(x string, y string)\n"
    5639             :                                            "external name str.startswith;\n"
    5640             :                                            "grant execute on filter function startswith(string, string) to public;\n"
    5641             :                                            "create filter function sys.startswith(x string, y string, icase boolean)\n"
    5642             :                                            "external name str.startswith;\n"
    5643             :                                            "grant execute on filter function startswith(string, string, boolean) to public;\n"
    5644             :                                            "create function sys.endswith(x string, y string)\n"
    5645             :                                            "returns boolean external name str.endswith;\n"
    5646             :                                            "grant execute on function endswith(string, string) to public;\n"
    5647             :                                            "create function sys.endswith(x string, y string, icase boolean)\n"
    5648             :                                            "returns boolean external name str.endswith;\n"
    5649             :                                            "grant execute on function endswith(string, string, boolean) to public;\n"
    5650             :                                            "create filter function sys.endswith(x string, y string)\n"
    5651             :                                            "external name str.endswith;\n"
    5652             :                                            "grant execute on filter function endswith(string, string) to public;\n"
    5653             :                                            "create filter function sys.endswith(x string, y string, icase boolean)\n"
    5654             :                                            "external name str.endswith;\n"
    5655             :                                            "grant execute on filter function endswith(string, string, boolean) to public;\n"
    5656             :                                            "create function sys.contains(x string, y string)\n"
    5657             :                                            "returns boolean external name str.contains;\n"
    5658             :                                            "grant execute on function contains(string, string) to public;\n"
    5659             :                                            "create function sys.contains(x string, y string, icase boolean)\n"
    5660             :                                            "returns boolean external name str.contains;\n"
    5661             :                                            "grant execute on function contains(string, string, boolean) to public;\n"
    5662             :                                            "create filter function sys.contains(x string, y string)\n"
    5663             :                                            "external name str.contains;\n"
    5664             :                                            "grant execute on filter function contains(string, string) to public;\n"
    5665             :                                            "create filter function sys.contains(x string, y string, icase boolean)\n"
    5666             :                                            "external name str.contains;\n"
    5667             :                                            "grant execute on filter function contains(string, string, boolean) to public;\n"
    5668             : 
    5669             :                                            "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"
    5670             :                                            "update sys.functions set system = true where system <> true and name in ('maxlevenshtein', 'minjarowinkler') and schema_id = 2000 and type = %d;\n"
    5671             :                                            "update sys.functions set system = true where system <> true and name in ('asciify', 'startswith', 'endswith', 'contains') and schema_id = 2000 and type = %d;\n"
    5672             :                                            "update sys.functions set system = true where system <> true and name in ('startswith', 'endswith', 'contains') and schema_id = 2000 and type = %d;\n"
    5673             : 
    5674             :                                            "delete from sys.triggers where name = 'system_update_tables' and table_id = 2067;\n",
    5675             :                                            F_FUNC, F_FILT, F_FUNC, F_FILT);
    5676           0 :                 assert(pos < bufsize);
    5677           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    5678           0 :                 fflush(stdout);
    5679           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    5680             :         }
    5681             : 
    5682             :         /* remote credentials where moved */
    5683          99 :         sql_trans *tr = sql->session->tr;
    5684          99 :         sqlstore *store = tr->store;
    5685          99 :         sql_table *remote_user_info = find_sql_table(tr, s, "remote_user_info");
    5686          99 :         sql_column *remote_user_info_id = find_sql_column(remote_user_info, "table_id");
    5687          99 :         BAT *rt_key = NULL, *rt_username = NULL, *rt_pwhash = NULL, *rt_uri = NULL, *rt_deleted = NULL;
    5688          99 :         if (!err && store->storage_api.count_col(tr, remote_user_info_id, 0) == 0 && BBPindex("M5system_auth_rt_key")) {
    5689             : 
    5690           0 :                 rt_key = BATdescriptor(BBPindex("M5system_auth_rt_key"));
    5691           0 :                 rt_uri = BATdescriptor(BBPindex("M5system_auth_rt_uri"));
    5692           0 :                 rt_username = BATdescriptor(BBPindex("M5system_auth_rt_remoteuser"));
    5693           0 :                 rt_pwhash = BATdescriptor(BBPindex("M5system_auth_rt_hashedpwd"));
    5694           0 :                 rt_deleted = BATdescriptor(BBPindex("M5system_auth_rt_deleted"));
    5695           0 :                 if (rt_key == NULL || rt_username == NULL || rt_pwhash == NULL || rt_uri == NULL || rt_deleted == NULL) {
    5696             :                         /* cleanup remainders and continue or full stop ? */
    5697           0 :                         BBPreclaim(rt_key);
    5698           0 :                         BBPreclaim(rt_uri);
    5699           0 :                         BBPreclaim(rt_username);
    5700           0 :                         BBPreclaim(rt_pwhash);
    5701           0 :                         BBPreclaim(rt_deleted);
    5702           0 :                         throw(SQL, __func__, "cannot find M5system_auth bats");
    5703             :                 }
    5704             : 
    5705           0 :                 BATiter ik = bat_iterator(rt_key);
    5706           0 :                 BATiter iu = bat_iterator(rt_username);
    5707           0 :                 BATiter ip = bat_iterator(rt_pwhash);
    5708           0 :                 for (oid p = 0; p < ik.count; p++) {
    5709           0 :                         if (BUNfnd(rt_deleted, &p) == BUN_NONE) {
    5710           0 :                                 char *key = GDKstrdup(BUNtvar(ik, p));
    5711           0 :                                 char *username = BUNtvar(iu, p);
    5712           0 :                                 char *pwhash = BUNtvar(ip, p);
    5713             : 
    5714           0 :                                 if (!key) {
    5715           0 :                                         bat_iterator_end(&ik);
    5716           0 :                                         bat_iterator_end(&iu);
    5717           0 :                                         bat_iterator_end(&ip);
    5718           0 :                                         BBPunfix(rt_key->batCacheid);
    5719           0 :                                         BBPunfix(rt_username->batCacheid);
    5720           0 :                                         BBPunfix(rt_pwhash->batCacheid);
    5721           0 :                                         BBPunfix(rt_deleted->batCacheid);
    5722           0 :                                         throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5723             :                                 }
    5724           0 :                                 char *d = strchr(key, '.');
    5725             :                                 /* . not found simply skip */
    5726           0 :                                 if (d) {
    5727           0 :                                         *d++ = '\0';
    5728           0 :                                         sql_schema *s = find_sql_schema(tr, key);
    5729           0 :                                         if (s) {
    5730           0 :                                                 sql_table *t = find_sql_table(tr, s, d);
    5731           0 :                                                 if (t && store->table_api.table_insert(tr, remote_user_info, &t->base.id, &username, &pwhash) != LOG_OK) {
    5732           0 :                                                         bat_iterator_end(&ik);
    5733           0 :                                                         bat_iterator_end(&iu);
    5734           0 :                                                         bat_iterator_end(&ip);
    5735           0 :                                                         BBPunfix(rt_key->batCacheid);
    5736           0 :                                                         BBPunfix(rt_username->batCacheid);
    5737           0 :                                                         BBPunfix(rt_pwhash->batCacheid);
    5738           0 :                                                         BBPunfix(rt_deleted->batCacheid);
    5739           0 :                                                         GDKfree(key);
    5740           0 :                                                         throw(SQL, __func__, "Failed to insert remote credentials during upgrade");
    5741             :                                                 }
    5742             :                                         }
    5743             :                                 }
    5744           0 :                                 GDKfree(key);
    5745             :                         }
    5746             :                 }
    5747           0 :                 bat_iterator_end(&ik);
    5748           0 :                 bat_iterator_end(&iu);
    5749           0 :                 bat_iterator_end(&ip);
    5750             :         }
    5751          99 :         if (!err && rt_key) {
    5752           0 :                 bat rtauthbats[6];
    5753             : 
    5754           0 :                 rtauthbats[0] = 0;
    5755           0 :                 rtauthbats[1] = rt_key->batCacheid;
    5756           0 :                 rtauthbats[2] = rt_uri->batCacheid;
    5757           0 :                 rtauthbats[3] = rt_username->batCacheid;
    5758           0 :                 rtauthbats[4] = rt_pwhash->batCacheid;
    5759           0 :                 rtauthbats[5] = rt_deleted->batCacheid;
    5760             : 
    5761           0 :                 if (BATmode(rt_key, true) != GDK_SUCCEED ||
    5762           0 :                         BBPrename(rt_key, NULL) != 0 ||
    5763           0 :                         BATmode(rt_username, true) != GDK_SUCCEED ||
    5764           0 :                         BBPrename(rt_username, NULL) != 0 ||
    5765           0 :                         BATmode(rt_pwhash, true) != GDK_SUCCEED ||
    5766           0 :                         BBPrename(rt_pwhash, NULL) != 0 ||
    5767           0 :                         BATmode(rt_uri, true) != GDK_SUCCEED ||
    5768           0 :                         BBPrename(rt_uri, NULL) != 0 ||
    5769           0 :                         BATmode(rt_deleted, true) != GDK_SUCCEED ||
    5770           0 :                         BBPrename(rt_deleted, NULL) != 0 ||
    5771           0 :                         TMsubcommit_list(rtauthbats, NULL, 6, -1, -1) != GDK_SUCCEED) {
    5772           0 :                         fprintf(stderr, "Committing removal of old remote user/password BATs failed\n");
    5773             :                 }
    5774           0 :                 BBPunfix(rt_key->batCacheid);
    5775           0 :                 BBPunfix(rt_username->batCacheid);
    5776           0 :                 BBPunfix(rt_pwhash->batCacheid);
    5777           0 :                 BBPunfix(rt_uri->batCacheid);
    5778           0 :                 BBPunfix(rt_deleted->batCacheid);
    5779             :         }
    5780             : 
    5781          99 :         GDKfree(buf);
    5782          99 :         return err;             /* usually MAL_SUCCEED */
    5783             : }
    5784             : 
    5785             : static str
    5786          99 : sql_update_jun2023_sp3(Client c, mvc *sql, sql_schema *s)
    5787             : {
    5788          99 :         (void)s;
    5789          99 :         char *err = NULL;
    5790          99 :         sql_subtype t1, t2;
    5791             : 
    5792          99 :         sql_find_subtype(&t1, "timestamp", 0, 0);
    5793          99 :         sql_find_subtype(&t2, "varchar", 0, 0);
    5794             : 
    5795          99 :         if (!sql_bind_func(sql, "sys", "timestamp_to_str", &t1, &t2, F_FUNC, true)) {
    5796           0 :                 sql->session->status = 0;
    5797           0 :                 sql->errstr[0] = '\0';
    5798             : 
    5799           0 :                 char *query = GDKmalloc(512);
    5800           0 :                 if (query == NULL)
    5801           0 :                         throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5802             : 
    5803           0 :                 snprintf(query, 512, "CREATE FUNCTION timestamp_to_str(d TIMESTAMP, format STRING) RETURNS STRING "
    5804             :                                  "EXTERNAL NAME mtime.\"timestamp_to_str\";\n"
    5805             :                                  "GRANT EXECUTE ON FUNCTION timestamp_to_str(TIMESTAMP, STRING) TO PUBLIC;\n"
    5806             :                                  "UPDATE sys.functions SET system = true WHERE system <> true AND name = 'timestamp_to_str' "
    5807             :                                  "AND schema_id = 2000 and type = %d;\n", F_FUNC);
    5808             : 
    5809           0 :                 printf("Running database upgrade commands:\n%s\n", query);
    5810           0 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5811           0 :                 GDKfree(query);
    5812             :         }
    5813             : 
    5814             :         return err;             /* usually MAL_SUCCEED */
    5815             : }
    5816             : 
    5817             : static str
    5818          99 : sql_update_dec2023_geom(Client c, mvc *sql, sql_schema *s)
    5819             : {
    5820          99 :         sql_subtype tp;
    5821          99 :         char *err = NULL;
    5822             : 
    5823             :         /* the shp module was changed: drop the old stuff if it exists, only
    5824             :          * add the new stuff if the appropriate module is available */
    5825          99 :         sql_find_subtype(&tp, "varchar", 0, 0);
    5826             :         /* Drop old SHP procedures */
    5827          99 :         if (sql_bind_func(sql, s->base.name, "shpattach", &tp, NULL, F_PROC, true)) {
    5828           8 :                 if ((err = sql_drop_shp(c)) != NULL)
    5829             :                         return err;
    5830             :         }
    5831          99 :         sql->session->status = 0; /* if the shpattach function was not found clean the error */
    5832          99 :         sql->errstr[0] = '\0';
    5833             : #ifdef HAVE_GEOM
    5834          99 :         if (backend_has_module(&(int){0}, "geom")) {
    5835             : #ifdef HAVE_SHP
    5836          99 :                 if (backend_has_module(&(int){0}, "shp")) {
    5837             :                         /* if shpload with two varchar args does not exist, add the
    5838             :                          * procedures */
    5839          99 :                         if (!sql_bind_func(sql, s->base.name, "shpload", &tp, &tp, F_PROC, true)) {
    5840           8 :                                 sql->session->status = 0;
    5841           8 :                                 sql->errstr[0] = '\0';
    5842           8 :                                 if ((err = sql_create_shp(c)) != NULL)
    5843           0 :                                         return err;
    5844             :                         }
    5845             :                 }
    5846             : #endif
    5847          99 :                 sql_find_subtype(&tp, "geometry", 0, 0);
    5848          99 :                 if (!sql_bind_func(sql, s->base.name, "st_intersects_noindex", &tp, &tp, F_FILT, true)) {
    5849           8 :                         sql->session->status = 0;
    5850           8 :                         sql->errstr[0] = '\0';
    5851           8 :                         sql_table *t;
    5852           8 :                         if ((t = mvc_bind_table(sql, s, "geometry_columns")) != NULL)
    5853           8 :                                 t->system = 0;
    5854           8 :                         const char *query =
    5855             :                                 "drop function if exists sys.st_intersects(geometry, geometry) cascade;\n"
    5856             :                                 "drop function if exists sys.st_dwithin(geometry, geometry, double) cascade;\n"
    5857             :                                 "drop view if exists sys.geometry_columns cascade;\n"
    5858             :                                 "drop function if exists sys.st_collect(geometry, geometry) cascade;\n"
    5859             :                                 "drop aggregate if exists sys.st_collect(geometry) cascade;\n"
    5860             :                                 "drop aggregate if exists sys.st_makeline(geometry) cascade;\n"
    5861             :                                 "create view sys.geometry_columns as\n"
    5862             :                                 " select cast(null as varchar(1)) as f_table_catalog,\n"
    5863             :                                 "  s.name as f_table_schema,\n"
    5864             :                                 "  t.name as f_table_name,\n"
    5865             :                                 "  c.name as f_geometry_column,\n"
    5866             :                                 "  cast(has_z(c.type_digits) + has_m(c.type_digits) +2 as integer) as coord_dimension,\n"
    5867             :                                 "  c.type_scale as srid,\n"
    5868             :                                 "  get_type(c.type_digits, 0) as geometry_type\n"
    5869             :                                 " from sys.columns c, sys.tables t, sys.schemas s\n"
    5870             :                                 " where c.table_id = t.id and t.schema_id = s.id\n"
    5871             :                                 "  and c.type in (select sqlname from sys.types where systemname in ('wkb', 'wkba'));\n"
    5872             :                                 "GRANT SELECT ON sys.geometry_columns TO PUBLIC;\n"
    5873             :                                 "CREATE FUNCTION ST_Collect(geom1 Geometry, geom2 Geometry) RETURNS Geometry EXTERNAL NAME geom.\"Collect\";\n"
    5874             :                                 "GRANT EXECUTE ON FUNCTION ST_Collect(Geometry, Geometry) TO PUBLIC;\n"
    5875             :                                 "CREATE AGGREGATE ST_Collect(geom Geometry) RETURNS Geometry external name aggr.\"Collect\";\n"
    5876             :                                 "GRANT EXECUTE ON AGGREGATE ST_Collect(Geometry) TO PUBLIC;\n"
    5877             :                                 "CREATE FUNCTION ST_DistanceGeographic(geom1 Geometry, geom2 Geometry) RETURNS double EXTERNAL NAME geom.\"DistanceGeographic\";\n"
    5878             :                                 "GRANT EXECUTE ON FUNCTION ST_DistanceGeographic(Geometry, Geometry) TO PUBLIC;\n"
    5879             :                                 "CREATE FILTER FUNCTION ST_DWithinGeographic(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME geom.\"DWithinGeographic\";\n"
    5880             :                                 "GRANT EXECUTE ON FILTER ST_DWithinGeographic(Geometry, Geometry, double) TO PUBLIC;\n"
    5881             :                                 "CREATE FILTER FUNCTION ST_DWithin(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME rtree.\"DWithin\";\n"
    5882             :                                 "GRANT EXECUTE ON FILTER ST_DWithin(Geometry, Geometry, double) TO PUBLIC;\n"
    5883             :                                 "CREATE FILTER FUNCTION ST_DWithin_NoIndex(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME geom.\"DWithin_noindex\";\n"
    5884             :                                 "GRANT EXECUTE ON FILTER ST_DWithin_NoIndex(Geometry, Geometry, double) TO PUBLIC;\n"
    5885             :                                 "CREATE FUNCTION ST_DWithin2(geom1 Geometry, geom2 Geometry, bbox1 mbr, bbox2 mbr, dst double) RETURNS boolean EXTERNAL NAME geom.\"DWithin2\";\n"
    5886             :                                 "GRANT EXECUTE ON FUNCTION ST_DWithin2(Geometry, Geometry, mbr, mbr, double) TO PUBLIC;\n"
    5887             :                                 "CREATE FILTER FUNCTION ST_IntersectsGeographic(geom1 Geometry, geom2 Geometry) EXTERNAL NAME geom.\"IntersectsGeographic\";\n"
    5888             :                                 "GRANT EXECUTE ON FILTER ST_IntersectsGeographic(Geometry, Geometry) TO PUBLIC;\n"
    5889             :                                 "CREATE FILTER FUNCTION ST_Intersects(geom1 Geometry, geom2 Geometry) EXTERNAL NAME rtree.\"Intersects\";\n"
    5890             :                                 "GRANT EXECUTE ON FILTER ST_Intersects(Geometry, Geometry) TO PUBLIC;\n"
    5891             :                                 "CREATE FILTER FUNCTION ST_Intersects_NoIndex(geom1 Geometry, geom2 Geometry) EXTERNAL NAME geom.\"Intersects_noindex\";\n"
    5892             :                                 "GRANT EXECUTE ON FILTER ST_Intersects_NoIndex(Geometry, Geometry) TO PUBLIC;\n"
    5893             :                                 "CREATE AGGREGATE ST_MakeLine(geom Geometry) RETURNS Geometry external name aggr.\"MakeLine\";\n"
    5894             :                                 "GRANT EXECUTE ON AGGREGATE ST_MakeLine(Geometry) TO PUBLIC;\n"
    5895             :                                 "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"
    5896             :                                 "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'geometry_columns';\n";
    5897           8 :                         printf("Running database upgrade commands:\n%s\n", query);
    5898           8 :                         fflush(stdout);
    5899           8 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5900             :                 }
    5901             :         }
    5902             : #endif
    5903          99 :         return err;
    5904             : }
    5905             : 
    5906             : static str
    5907          99 : sql_update_dec2023(Client c, mvc *sql, sql_schema *s)
    5908             : {
    5909          99 :         sql_subtype tp;
    5910          99 :         sql_schema *info;
    5911          99 :         char *err = NULL;
    5912          99 :         res_table *output = NULL;
    5913             : 
    5914          99 :         sql_find_subtype(&tp, "varchar", 0, 0);
    5915          99 :         if (sql_bind_func(sql, s->base.name, "similarity", &tp, &tp, F_FUNC, true)) {
    5916           8 :                 const char *query = "drop function sys.similarity(string, string) cascade;\n";
    5917           8 :                 printf("Running database upgrade commands:\n%s\n", query);
    5918           8 :                 fflush(stdout);
    5919           8 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5920             :         } else {
    5921          91 :                 sql->session->status = 0; /* if the function was not found clean the error */
    5922          91 :                 sql->errstr[0] = '\0';
    5923             :         }
    5924             : 
    5925          99 :         if (mvc_bind_table(sql, s, "describe_accessible_tables") == NULL) {
    5926           8 :                 sql->session->status = 0; /* if the view was not found clean the error */
    5927           8 :                 sql->errstr[0] = '\0';
    5928           8 :                 const char *query =
    5929             :                 "CREATE VIEW sys.describe_accessible_tables AS\n"
    5930             :                 " SELECT\n"
    5931             :                 " schemas.name AS schema,\n"
    5932             :                 " tables.name  AS table,\n"
    5933             :                 " tt.table_type_name AS table_type,\n"
    5934             :                 " pc.privilege_code_name AS privs,\n"
    5935             :                 " p.privileges AS privs_code\n"
    5936             :                 " FROM privileges p\n"
    5937             :                 " JOIN sys.roles ON p.auth_id = roles.id\n"
    5938             :                 " JOIN sys.tables ON p.obj_id = tables.id\n"
    5939             :                 " JOIN sys.table_types tt ON tables.type = tt.table_type_id\n"
    5940             :                 " JOIN sys.schemas ON tables.schema_id = schemas.id\n"
    5941             :                 " JOIN sys.privilege_codes pc ON p.privileges = pc.privilege_code_id\n"
    5942             :                 " WHERE roles.name = current_role;\n"
    5943             :                 "GRANT SELECT ON sys.describe_accessible_tables TO PUBLIC;\n"
    5944             :                 "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'describe_accessible_tables';\n"
    5945             : 
    5946             :                         /* PYTHON_MAP and PYTHON3_MAP have been removed */
    5947             :                         "alter table sys.function_languages set read write;\n"
    5948             :                         "delete from sys.function_languages where language_keyword like 'PYTHON%_MAP';\n"
    5949             :                         /* for these two, also see load_func() */
    5950             :                         "update sys.functions set language = language - 1 where language in (7, 11);\n"
    5951             :                         "update sys.functions set mod = 'pyapi3' where mod in ('pyapi', 'pyapi3map');\n"
    5952             :                         "commit;\n";
    5953           8 :                 printf("Running database upgrade commands:\n%s\n", query);
    5954           8 :                 fflush(stdout);
    5955           8 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5956           8 :                 if (err == MAL_SUCCEED) {
    5957           8 :                         query = "alter table sys.function_languages set read only;\n";
    5958           8 :                         printf("Running database upgrade commands:\n%s\n", query);
    5959           8 :                         fflush(stdout);
    5960           8 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    5961             :                 }
    5962             :         }
    5963             : 
    5964             :         /* 52_describe.sql changes to update sys.describe_comments view */
    5965          99 :         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) {
    5966          99 :                 BAT *b;
    5967          99 :                 if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
    5968           8 :                         sql_table *t;
    5969             :                         /* set views internally to non-system to allow drop commands to succeed without error */
    5970           8 :                         if ((t = mvc_bind_table(sql, s, "describe_comments")) != NULL)
    5971           8 :                                 t->system = 0;
    5972           8 :                         if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
    5973           8 :                                 t->system = 0;
    5974             : 
    5975           8 :                         const char *cmds =
    5976             :                         "DROP FUNCTION IF EXISTS sys.dump_database(BOOLEAN) CASCADE;\n"
    5977             :                         "DROP VIEW IF EXISTS sys.dump_comments CASCADE;\n"
    5978             :                         "DROP VIEW IF EXISTS sys.describe_comments CASCADE;\n"
    5979             :                         "CREATE VIEW sys.describe_comments AS\n"
    5980             :                         "  SELECT o.id AS id, o.tpe AS tpe, o.nme AS fqn, cm.remark AS rem\n"
    5981             :                         "  FROM (\n"
    5982             :                         "          SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas WHERE NOT system\n"
    5983             :                         "          UNION ALL\n"
    5984             :                         "          SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
    5985             :                         "            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"
    5986             :                         "           WHERE NOT t.system\n"
    5987             :                         "          UNION ALL\n"
    5988             :                         "          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"
    5989             :                         "          UNION ALL\n"
    5990             :                         "          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"
    5991             :                         "          UNION ALL\n"
    5992             :                         "          SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    5993             :                         "          UNION ALL\n"
    5994             :                         "          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"
    5995             :                         "           WHERE NOT f.system AND f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
    5996             :                         "          ) AS o(id, tpe, nme)\n"
    5997             :                         "  JOIN sys.comments cm ON cm.id = o.id;\n"
    5998             :                         "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
    5999             :                         "CREATE VIEW sys.dump_comments AS\n"
    6000             :                         "  SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    6001             :                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    6002             :                         "BEGIN\n"
    6003             :                         "  SET SCHEMA sys;\n"
    6004             :                         "  TRUNCATE sys.dump_statements;\n"
    6005             :                         "  INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    6006             :                         "  INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
    6007             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    6008             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    6009             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    6010             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    6011             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    6012             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    6013             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    6014             :                         "  --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    6015             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    6016             :                         "                              FROM (\n"
    6017             :                         "                                SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    6018             :                         "                                 UNION ALL\n"
    6019             :                         "                                SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    6020             :                         "                              ) AS stmts(o, s);\n"
    6021             :                         "  IF NOT DESCRIBE THEN\n"
    6022             :                         "    CALL sys.dump_table_data();\n"
    6023             :                         "  END IF;\n"
    6024             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    6025             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    6026             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    6027             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    6028             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    6029             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    6030             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    6031             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    6032             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    6033             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    6034             :                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    6035             :                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    6036             :                         "  RETURN sys.dump_statements;\n"
    6037             :                         "END;\n"
    6038             :                         "update sys._tables set system = true where schema_id = 2000 and name in ('describe_comments','dump_comments');\n"
    6039             :                         "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'dump_database' and type = 5;\n";
    6040             : 
    6041           8 :                         printf("Running database upgrade commands:\n%s\n", cmds);
    6042           8 :                         fflush(stdout);
    6043           8 :                         err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
    6044             :                 }
    6045          99 :                 res_table_destroy(output);
    6046          99 :                 output = NULL;
    6047             :         }
    6048             : 
    6049             :         /* 52_describe.sql New function sys.sql_datatype(mtype varchar(999), digits integer, tscale integer, nameonly boolean, shortname boolean) */
    6050          99 :         sql_allocator *old_sa = sql->sa;
    6051          99 :         if ((sql->sa = sa_create(sql->pa)) != NULL) {
    6052          99 :                 list *l;
    6053          99 :                 if ((l = sa_list(sql->sa)) != NULL) {
    6054          99 :                         sql_subtype t1, t2;
    6055          99 :                         sql_find_subtype(&t1, "int", 0, 0);
    6056          99 :                         sql_find_subtype(&t2, "boolean", 0, 0);
    6057          99 :                         list_append(l, &tp);
    6058          99 :                         list_append(l, &t1);
    6059          99 :                         list_append(l, &t1);
    6060          99 :                         list_append(l, &t2);
    6061          99 :                         list_append(l, &t2);
    6062          99 :                         if (!sql_bind_func_(sql, s->base.name, "sql_datatype", l, F_FUNC, true)) {
    6063           8 :                                 const char *cmds =
    6064             :                                 "CREATE FUNCTION sys.sql_datatype(mtype varchar(999), digits integer, tscale integer, nameonly boolean, shortname boolean)\n"
    6065             :                                 "  RETURNS varchar(1024)\n"
    6066             :                                 "BEGIN\n"
    6067             :                                 "  RETURN\n"
    6068             :                                 "    CASE mtype\n"
    6069             :                                 "    WHEN 'char' THEN sys.ifthenelse(nameonly OR digits <= 1, sys.ifthenelse(shortname, 'CHAR', 'CHARACTER'), sys.ifthenelse(shortname, 'CHAR(', 'CHARACTER(') || digits || ')')\n"
    6070             :                                 "    WHEN 'varchar' THEN sys.ifthenelse(nameonly OR digits = 0, sys.ifthenelse(shortname, 'VARCHAR', 'CHARACTER VARYING'), sys.ifthenelse(shortname, 'VARCHAR(', 'CHARACTER VARYING(') || digits || ')')\n"
    6071             :                                 "    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"
    6072             :                                 "    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"
    6073             :                                 "    WHEN 'int' THEN 'INTEGER'\n"
    6074             :                                 "    WHEN 'bigint' THEN 'BIGINT'\n"
    6075             :                                 "    WHEN 'smallint' THEN 'SMALLINT'\n"
    6076             :                                 "    WHEN 'tinyint' THEN 'TINYINT'\n"
    6077             :                                 "    WHEN 'hugeint' THEN 'HUGEINT'\n"
    6078             :                                 "    WHEN 'boolean' THEN 'BOOLEAN'\n"
    6079             :                                 "    WHEN 'date' THEN 'DATE'\n"
    6080             :                                 "    WHEN 'time' THEN sys.ifthenelse(nameonly OR digits = 1, 'TIME', 'TIME(' || (digits -1) || ')')\n"
    6081             :                                 "    WHEN 'timestamp' THEN sys.ifthenelse(nameonly OR digits = 7, 'TIMESTAMP', 'TIMESTAMP(' || (digits -1) || ')')\n"
    6082             :                                 "    WHEN 'timestamptz' THEN sys.ifthenelse(nameonly OR digits = 7, 'TIMESTAMP WITH TIME ZONE', 'TIMESTAMP(' || (digits -1) || ') WITH TIME ZONE')\n"
    6083             :                                 "    WHEN 'timetz' THEN sys.ifthenelse(nameonly OR digits = 1, 'TIME WITH TIME ZONE', 'TIME(' || (digits -1) || ') WITH TIME ZONE')\n"
    6084             :                                 "    WHEN 'decimal' THEN sys.ifthenelse(nameonly OR digits = 0, 'DECIMAL', 'DECIMAL(' || digits || sys.ifthenelse(tscale = 0, '', ',' || tscale) || ')')\n"
    6085             :                                 "    WHEN 'double' THEN sys.ifthenelse(nameonly OR (digits = 53 AND tscale = 0), sys.ifthenelse(shortname, 'DOUBLE', 'DOUBLE PRECISION'), 'FLOAT(' || digits || ')')\n"
    6086             :                                 "    WHEN 'real' THEN sys.ifthenelse(nameonly OR (digits = 24 AND tscale = 0), 'REAL', 'FLOAT(' || digits || ')')\n"
    6087             :                                 "    WHEN 'day_interval' THEN 'INTERVAL DAY'\n"
    6088             :                                 "    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"
    6089             :                                 "    WHEN 'sec_interval' THEN\n"
    6090             :                                 "  CASE digits\n"
    6091             :                                 "  WHEN 4 THEN 'INTERVAL DAY'\n"
    6092             :                                 "  WHEN 5 THEN 'INTERVAL DAY TO HOUR'\n"
    6093             :                                 "  WHEN 6 THEN 'INTERVAL DAY TO MINUTE'\n"
    6094             :                                 "  WHEN 7 THEN 'INTERVAL DAY TO SECOND'\n"
    6095             :                                 "  WHEN 8 THEN 'INTERVAL HOUR'\n"
    6096             :                                 "  WHEN 9 THEN 'INTERVAL HOUR TO MINUTE'\n"
    6097             :                                 "  WHEN 10 THEN 'INTERVAL HOUR TO SECOND'\n"
    6098             :                                 "  WHEN 11 THEN 'INTERVAL MINUTE'\n"
    6099             :                                 "  WHEN 12 THEN 'INTERVAL MINUTE TO SECOND'\n"
    6100             :                                 "  WHEN 13 THEN 'INTERVAL SECOND'\n"
    6101             :                                 "  END\n"
    6102             :                                 "    WHEN 'oid' THEN 'OID'\n"
    6103             :                                 "    WHEN 'json' THEN sys.ifthenelse(nameonly OR digits = 0, 'JSON', 'JSON(' || digits || ')')\n"
    6104             :                                 "    WHEN 'url' THEN sys.ifthenelse(nameonly OR digits = 0, 'URL', 'URL(' || digits || ')')\n"
    6105             :                                 "    WHEN 'xml' THEN sys.ifthenelse(nameonly OR digits = 0, 'XML', 'XML(' || digits || ')')\n"
    6106             :                                 "    WHEN 'geometry' THEN\n"
    6107             :                                 "  sys.ifthenelse(nameonly, 'GEOMETRY',\n"
    6108             :                                 "  CASE digits\n"
    6109             :                                 "  WHEN 4 THEN 'GEOMETRY(POINT' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6110             :                                 "  WHEN 8 THEN 'GEOMETRY(LINESTRING' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6111             :                                 "  WHEN 16 THEN 'GEOMETRY(POLYGON' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6112             :                                 "  WHEN 20 THEN 'GEOMETRY(MULTIPOINT' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6113             :                                 "  WHEN 24 THEN 'GEOMETRY(MULTILINESTRING' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6114             :                                 "  WHEN 28 THEN 'GEOMETRY(MULTIPOLYGON' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6115             :                                 "  WHEN 32 THEN 'GEOMETRY(GEOMETRYCOLLECTION' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
    6116             :                                 "  ELSE 'GEOMETRY'\n"
    6117             :                                 "        END)\n"
    6118             :                                 "    ELSE sys.ifthenelse(mtype = lower(mtype), upper(mtype), '\"' || mtype || '\"') || sys.ifthenelse(nameonly OR digits = 0, '', '(' || digits || sys.ifthenelse(tscale = 0, '', ',' || tscale) || ')')\n"
    6119             :                                 "    END;\n"
    6120             :                                 "END;\n"
    6121             :                                 "GRANT EXECUTE ON FUNCTION sys.sql_datatype(varchar(999), integer, integer, boolean, boolean) TO PUBLIC;\n"
    6122             :                                 "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'sql_datatype' and type = 1 and language = 2;\n";
    6123             : 
    6124           8 :                                 sql->session->status = 0;
    6125           8 :                                 sql->errstr[0] = '\0';
    6126           8 :                                 printf("Running database upgrade commands:\n%s\n", cmds);
    6127           8 :                                 fflush(stdout);
    6128           8 :                                 err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
    6129             :                         }
    6130             :                 }
    6131          99 :                 sa_destroy(sql->sa);
    6132             :         }
    6133          99 :         sql->sa = old_sa;
    6134             : 
    6135             : 
    6136             :         /* 91_information_schema.sql */
    6137          99 :         info = mvc_bind_schema(sql, "information_schema");
    6138          99 :         if (info == NULL) {
    6139           8 :                 sql->session->status = 0; /* if the schema was not found clean the error */
    6140           8 :                 sql->errstr[0] = '\0';
    6141           8 :                 const char *cmds =
    6142             :                 "CREATE SCHEMA INFORMATION_SCHEMA;\n"
    6143             :                 "COMMENT ON SCHEMA INFORMATION_SCHEMA IS 'ISO/IEC 9075-11 SQL/Schemata';\n"
    6144             :                 "update sys.schemas set system = true where name = 'information_schema';\n"
    6145             : 
    6146             :                 "CREATE VIEW INFORMATION_SCHEMA.CHARACTER_SETS AS SELECT\n"
    6147             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6148             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6149             :                 "  cast('UTF-8' AS varchar(16)) AS CHARACTER_SET_NAME,\n"
    6150             :                 "  cast('ISO/IEC 10646:2021' AS varchar(20)) AS CHARACTER_REPERTOIRE,\n"
    6151             :                 "  cast('UTF-8' AS varchar(16)) AS FORM_OF_USE,\n"
    6152             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_CATALOG,\n"
    6153             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_SCHEMA,\n"
    6154             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_NAME;\n"
    6155             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.CHARACTER_SETS TO PUBLIC WITH GRANT OPTION;\n"
    6156             : 
    6157             :                 "CREATE VIEW INFORMATION_SCHEMA.SCHEMATA AS SELECT\n"
    6158             :                 "  cast(NULL AS varchar(1)) AS CATALOG_NAME,\n"
    6159             :                 "  s.\"name\" AS SCHEMA_NAME,\n"
    6160             :                 "  a.\"name\" AS SCHEMA_OWNER,\n"
    6161             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_CHARACTER_SET_CATALOG,\n"
    6162             :                 "  cast(NULL AS varchar(1)) AS DEFAULT_CHARACTER_SET_SCHEMA,\n"
    6163             :                 "  cast('UTF-8' AS varchar(16)) AS DEFAULT_CHARACTER_SET_NAME,\n"
    6164             :                 "  cast(NULL AS varchar(1)) AS SQL_PATH,\n"
    6165             :                 "  s.\"id\" AS schema_id,\n"
    6166             :                 "  s.\"system\" AS is_system,\n"
    6167             :                 "  cm.\"remark\" AS comments\n"
    6168             :                 " FROM sys.\"schemas\" s\n"
    6169             :                 " INNER JOIN sys.\"auths\" a ON s.\"owner\" = a.\"id\"\n"
    6170             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON s.\"id\" = cm.\"id\"\n"
    6171             :                 " ORDER BY s.\"name\";\n"
    6172             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.SCHEMATA TO PUBLIC WITH GRANT OPTION;\n"
    6173             : 
    6174             :                 "CREATE VIEW INFORMATION_SCHEMA.TABLES AS SELECT\n"
    6175             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6176             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6177             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6178             :                 "  tt.\"table_type_name\" AS TABLE_TYPE,\n"
    6179             :                 "  cast(NULL AS varchar(1)) AS SELF_REFERENCING_COLUMN_NAME,\n"
    6180             :                 "  cast(NULL AS varchar(1)) AS REFERENCE_GENERATION,\n"
    6181             :                 "  cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_CATALOG,\n"
    6182             :                 "  cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_SCHEMA,\n"
    6183             :                 "  cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_NAME,\n"
    6184             :                 "  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"
    6185             :                 "  cast('NO' AS varchar(3)) AS IS_TYPED,\n"
    6186             :                 "  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"
    6187             :                 "  t.\"schema_id\" AS schema_id,\n"
    6188             :                 "  t.\"id\" AS table_id,\n"
    6189             :                 "  t.\"type\" AS table_type_id,\n"
    6190             :                 "  st.\"count\" AS row_count,\n"
    6191             :                 "  t.\"system\" AS is_system,\n"
    6192             :                 "  sys.ifthenelse(t.\"type\" IN (1, 11), TRUE, FALSE) AS is_view,\n"
    6193             :                 "  t.\"query\" AS query_def,\n"
    6194             :                 "  cm.\"remark\" AS comments\n"
    6195             :                 " FROM sys.\"tables\" t\n"
    6196             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6197             :                 " INNER JOIN sys.\"table_types\" tt ON t.\"type\" = tt.\"table_type_id\"\n"
    6198             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON t.\"id\" = cm.\"id\"\n"
    6199             :                 " LEFT OUTER JOIN (SELECT DISTINCT \"schema\", \"table\", \"count\" FROM sys.\"statistics\"()) st ON (s.\"name\" = st.\"schema\" AND t.\"name\" = st.\"table\")\n"
    6200             :                 " ORDER BY s.\"name\", t.\"name\";\n"
    6201             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.TABLES TO PUBLIC WITH GRANT OPTION;\n"
    6202             : 
    6203             :                 "CREATE VIEW INFORMATION_SCHEMA.VIEWS AS SELECT\n"
    6204             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6205             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6206             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6207             :                 "  t.\"query\" AS VIEW_DEFINITION,\n"
    6208             :                 "  cast('NONE' AS varchar(10)) AS CHECK_OPTION,\n"
    6209             :                 "  cast('NO' AS varchar(3)) AS IS_UPDATABLE,\n"
    6210             :                 "  cast('NO' AS varchar(3)) AS INSERTABLE_INTO,\n"
    6211             :                 "  cast('NO' AS varchar(3)) AS IS_TRIGGER_UPDATABLE,\n"
    6212             :                 "  cast('NO' AS varchar(3)) AS IS_TRIGGER_DELETABLE,\n"
    6213             :                 "  cast('NO' AS varchar(3)) AS IS_TRIGGER_INSERTABLE_INTO,\n"
    6214             :                 "  t.\"schema_id\" AS schema_id,\n"
    6215             :                 "  t.\"id\" AS table_id,\n"
    6216             :                 "  cast(sys.ifthenelse(t.\"system\", t.\"type\" + 10 , t.\"type\") AS smallint) AS table_type_id,\n"
    6217             :                 "  t.\"system\" AS is_system,\n"
    6218             :                 "  cm.\"remark\" AS comments\n"
    6219             :                 " FROM sys.\"_tables\" t\n"
    6220             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6221             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON t.\"id\" = cm.\"id\"\n"
    6222             :                 " WHERE t.\"type\" = 1\n"
    6223             :                 " ORDER BY s.\"name\", t.\"name\";\n"
    6224             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.VIEWS TO PUBLIC WITH GRANT OPTION;\n"
    6225             : 
    6226             :                 "CREATE VIEW INFORMATION_SCHEMA.COLUMNS AS SELECT\n"
    6227             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6228             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6229             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6230             :                 "  c.\"name\" AS COLUMN_NAME,\n"
    6231             :                 "  cast(1 + c.\"number\" AS int) AS ORDINAL_POSITION,\n"
    6232             :                 "  c.\"default\" AS COLUMN_DEFAULT,\n"
    6233             :                 "  cast(sys.ifthenelse(c.\"null\", 'YES', 'NO') AS varchar(3)) AS IS_NULLABLE,\n"
    6234             :                 "  cast(sys.\"sql_datatype\"(c.\"type\", c.\"type_digits\", c.\"type_scale\", true, true) AS varchar(1024)) AS DATA_TYPE,\n"
    6235             :                 "  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"
    6236             :                 "  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"
    6237             :                 "  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"
    6238             :                 "  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"
    6239             :                 "  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"
    6240             :                 "  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"
    6241             :                 "  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"
    6242             :                 "  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"
    6243             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6244             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6245             :                 "  cast(sys.ifthenelse(c.\"type\" IN ('varchar','clob','char','json','url','xml'), 'UTF-8', NULL) AS varchar(16)) AS CHARACTER_SET_NAME,\n"
    6246             :                 "  cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
    6247             :                 "  cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
    6248             :                 "  cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
    6249             :                 "  cast(NULL AS varchar(1)) AS DOMAIN_CATALOG,\n"
    6250             :                 "  cast(NULL AS varchar(1)) AS DOMAIN_SCHEMA,\n"
    6251             :                 "  cast(NULL AS varchar(1)) AS DOMAIN_NAME,\n"
    6252             :                 "  cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
    6253             :                 "  cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
    6254             :                 "  cast(NULL AS varchar(1)) AS UDT_NAME,\n"
    6255             :                 "  cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
    6256             :                 "  cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
    6257             :                 "  cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
    6258             :                 "  cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
    6259             :                 "  cast(NULL AS varchar(1)) AS DTD_IDENTIFIER,\n"
    6260             :                 "  cast('NO' AS varchar(3)) AS IS_SELF_REFERENCING,\n"
    6261             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL OR c.\"null\", 'NO', 'YES') AS varchar(3)) AS IS_IDENTITY,\n"
    6262             :                 "  seq.\"name\" AS IDENTITY_GENERATION,\n"
    6263             :                 "  seq.\"start\" AS IDENTITY_START,\n"
    6264             :                 "  seq.\"increment\" AS IDENTITY_INCREMENT,\n"
    6265             :                 "  seq.\"maxvalue\" AS IDENTITY_MAXIMUM,\n"
    6266             :                 "  seq.\"minvalue\" AS IDENTITY_MINIMUM,\n"
    6267             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL, NULL, sys.ifthenelse(seq.\"cycle\", 'YES', 'NO')) AS varchar(3)) AS IDENTITY_CYCLE,\n"
    6268             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL, 'NO', 'YES') AS varchar(3)) AS IS_GENERATED,\n"
    6269             :                 "  cast(sys.ifthenelse(seq.\"name\" IS NULL, NULL, c.\"default\") AS varchar(1024)) AS GENERATION_EXPRESSION,\n"
    6270             :                 "  cast('NO' AS varchar(3)) AS IS_SYSTEM_TIME_PERIOD_START,\n"
    6271             :                 "  cast('NO' AS varchar(3)) AS IS_SYSTEM_TIME_PERIOD_END,\n"
    6272             :                 "  cast('NO' AS varchar(3)) AS SYSTEM_TIME_PERIOD_TIMESTAMP_GENERATION,\n"
    6273             :                 "  cast(sys.ifthenelse(t.\"type\" IN (0,3,7,20,30), 'YES', 'NO') AS varchar(3)) AS IS_UPDATABLE,\n"
    6274             :                 "  cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
    6275             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
    6276             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
    6277             :                 "  t.\"schema_id\" AS schema_id,\n"
    6278             :                 "  c.\"table_id\" AS table_id,\n"
    6279             :                 "  c.\"id\" AS column_id,\n"
    6280             :                 "  seq.\"id\" AS sequence_id,\n"
    6281             :                 "  t.\"system\" AS is_system,\n"
    6282             :                 "  cm.\"remark\" AS comments\n"
    6283             :                 " FROM sys.\"columns\" c\n"
    6284             :                 " INNER JOIN sys.\"tables\" t ON c.\"table_id\" = t.\"id\"\n"
    6285             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6286             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON c.\"id\" = cm.\"id\"\n"
    6287             :                 " LEFT OUTER JOIN sys.\"sequences\" seq ON ((seq.\"name\"||'\"') = substring(c.\"default\", 3 + sys.\"locate\"('\".\"seq_',c.\"default\",14)))\n"
    6288             :                 " ORDER BY s.\"name\", t.\"name\", c.\"number\";\n"
    6289             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.COLUMNS TO PUBLIC WITH GRANT OPTION;\n"
    6290             : 
    6291             :                 "CREATE VIEW INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS SELECT\n"
    6292             :                 "  cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
    6293             :                 "  cast(NULL AS varchar(1024)) AS CONSTRAINT_SCHEMA,\n"
    6294             :                 "  cast(NULL AS varchar(1024)) AS CONSTRAINT_NAME,\n"
    6295             :                 "  cast(NULL AS varchar(1024)) AS CHECK_CLAUSE\n"
    6296             :                 " WHERE 1=0;\n"
    6297             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.CHECK_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
    6298             : 
    6299             :                 "CREATE VIEW INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS SELECT\n"
    6300             :                 "  cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
    6301             :                 "  s.\"name\" AS CONSTRAINT_SCHEMA,\n"
    6302             :                 "  k.\"name\" AS CONSTRAINT_NAME,\n"
    6303             :                 "  cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
    6304             :                 "  s.\"name\" AS TABLE_SCHEMA,\n"
    6305             :                 "  t.\"name\" AS TABLE_NAME,\n"
    6306             :                 "  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"
    6307             :                 "  cast('NO' AS varchar(3)) AS IS_DEFERRABLE,\n"
    6308             :                 "  cast('NO' AS varchar(3)) AS INITIALLY_DEFERRED,\n"
    6309             :                 "  cast('YES' AS varchar(3)) AS ENFORCED,\n"
    6310             :                 "  t.\"schema_id\" AS schema_id,\n"
    6311             :                 "  t.\"id\" AS table_id,\n"
    6312             :                 "  k.\"id\" AS key_id,\n"
    6313             :                 "  k.\"type\" AS key_type,\n"
    6314             :                 "  t.\"system\" AS is_system\n"
    6315             :                 " 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"
    6316             :                 " INNER JOIN (SELECT st.\"id\", st.\"schema_id\", st.\"name\", st.\"system\" FROM sys.\"_tables\" st UNION ALL"
    6317             :                         " SELECT tt.\"id\", tt.\"schema_id\", tt.\"name\", tt.\"system\" FROM tmp.\"_tables\" tt) t ON k.\"table_id\" = t.\"id\"\n"
    6318             :                 " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
    6319             :                 " ORDER BY s.\"name\", t.\"name\", k.\"name\";\n"
    6320             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.TABLE_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
    6321             : 
    6322             :                 "CREATE VIEW INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS SELECT\n"
    6323             :                 "  cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
    6324             :                 "  s.\"name\" AS CONSTRAINT_SCHEMA,\n"
    6325             :                 "  fk.\"name\" AS CONSTRAINT_NAME,\n"
    6326             :                 "  cast(NULL AS varchar(1)) AS UNIQUE_CONSTRAINT_CATALOG,\n"
    6327             :                 "  uks.\"name\" AS UNIQUE_CONSTRAINT_SCHEMA,\n"
    6328             :                 "  uk.\"name\" AS UNIQUE_CONSTRAINT_NAME,\n"
    6329             :                 "  cast('FULL' AS varchar(7)) AS MATCH_OPTION,\n"
    6330             :                 "  fk.\"update_action\" AS UPDATE_RULE,\n"
    6331             :                 "  fk.\"delete_action\" AS DELETE_RULE,\n"
    6332             :                 "  t.\"schema_id\" AS fk_schema_id,\n"
    6333             :                 "  t.\"id\" AS fk_table_id,\n"
    6334             :                 "  t.\"name\" AS fk_table_name,\n"
    6335             :                 "  fk.\"id\" AS fk_key_id,\n"
    6336             :                 "  ukt.\"schema_id\" AS uc_schema_id,\n"
    6337             :                 "  uk.\"table_id\" AS uc_table_id,\n"
    6338             :                 "  ukt.\"name\" AS uc_table_name,\n"
    6339             :                 "  uk.\"id\" AS uc_key_id\n"
    6340             :                 " FROM sys.\"fkeys\" fk\n"
    6341             :                 " INNER JOIN sys.\"tables\" t ON t.\"id\" = fk.\"table_id\"\n"
    6342             :                 " INNER JOIN sys.\"schemas\" s ON s.\"id\" = t.\"schema_id\"\n"
    6343             :                 " LEFT OUTER JOIN sys.\"keys\" uk ON uk.\"id\" = fk.\"rkey\"\n"
    6344             :                 " LEFT OUTER JOIN sys.\"tables\" ukt ON ukt.\"id\" = uk.\"table_id\"\n"
    6345             :                 " LEFT OUTER JOIN sys.\"schemas\" uks ON uks.\"id\" = ukt.\"schema_id\"\n"
    6346             :                 " ORDER BY s.\"name\", t.\"name\", fk.\"name\";\n"
    6347             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
    6348             : 
    6349             :                 "CREATE VIEW INFORMATION_SCHEMA.ROUTINES AS SELECT\n"
    6350             :                 "  cast(NULL AS varchar(1)) AS SPECIFIC_CATALOG,\n"
    6351             :                 "  s.\"name\" AS SPECIFIC_SCHEMA,\n"
    6352             :                 "  cast(f.\"name\"||'('||f.\"id\"||')' AS varchar(270)) AS SPECIFIC_NAME,\n"
    6353             :                 "  cast(NULL AS varchar(1)) AS ROUTINE_CATALOG,\n"
    6354             :                 "  s.\"name\" AS ROUTINE_SCHEMA,\n"
    6355             :                 "  f.\"name\" AS ROUTINE_NAME,\n"
    6356             :                 "  ft.\"function_type_keyword\" AS ROUTINE_TYPE,\n"
    6357             :                 "  cast(NULL AS varchar(1)) AS MODULE_CATALOG,\n"
    6358             :                 "  cast(NULL AS varchar(1)) AS MODULE_SCHEMA,\n"
    6359             :                 "  cast(f.\"mod\" AS varchar(128)) AS MODULE_NAME,\n"
    6360             :                 "  cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
    6361             :                 "  cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
    6362             :                 "  cast(NULL AS varchar(1)) AS UDT_NAME,\n"
    6363             :                 "  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"
    6364             :                 "  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"
    6365             :                 "  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"
    6366             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6367             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6368             :                 "  'UTF-8' AS CHARACTER_SET_NAME,\n"
    6369             :                 "  cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
    6370             :                 "  cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
    6371             :                 "  cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
    6372             :                 "  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"
    6373             :                 "  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"
    6374             :                 "  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"
    6375             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), a.\"type_scale\" -1, NULL) AS int) AS DATETIME_PRECISION,\n"
    6376             :                 "  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"
    6377             :                 "  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"
    6378             :                 "  cast(NULL AS varchar(1)) AS TYPE_UDT_CATALOG,\n"
    6379             :                 "  cast(NULL AS varchar(1)) AS TYPE_UDT_SCHEMA,\n"
    6380             :                 "  cast(NULL AS varchar(1)) AS TYPE_UDT_NAME,\n"
    6381             :                 "  cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
    6382             :                 "  cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
    6383             :                 "  cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
    6384             :                 "  cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
    6385             :                 "  cast(NULL AS int) AS DTD_IDENTIFIER,\n"
    6386             :                 "  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"
    6387             :                 "  f.\"func\" AS ROUTINE_DEFINITION,\n"
    6388             :                 "  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"
    6389             :                 "  fl.\"language_keyword\" AS EXTERNAL_LANGUAGE,\n"
    6390             :                 "  'GENERAL' AS PARAMETER_STYLE,\n"
    6391             :                 "  'YES' AS IS_DETERMINISTIC,\n"
    6392             :                 "  cast(sys.ifthenelse(f.\"side_effect\", 'MODIFIES', 'READ') AS varchar(10)) AS SQL_DATA_ACCESS,\n"
    6393             :                 "  cast(CASE f.\"type\" WHEN 2 THEN NULL ELSE 'NO' END AS varchar(3)) AS IS_NULL_CALL,\n"
    6394             :                 "  cast(NULL AS varchar(1)) AS SQL_PATH,\n"
    6395             :                 "  cast(NULL AS varchar(1)) AS SCHEMA_LEVEL_ROUTINE,\n"
    6396             :                 "  cast(NULL AS int) AS MAX_DYNAMIC_RESULT_SETS,\n"
    6397             :                 "  cast(NULL AS varchar(1)) AS IS_USER_DEFINED_CAST,\n"
    6398             :                 "  cast(NULL AS varchar(1)) AS IS_IMPLICITLY_INVOCABLE,\n"
    6399             :                 "  cast(NULL AS varchar(1)) AS SECURITY_TYPE,\n"
    6400             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_CATALOG,\n"
    6401             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_SCHEMA,\n"
    6402             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_NAME,\n"
    6403             :                 "  cast(NULL AS varchar(1)) AS AS_LOCATOR,\n"
    6404             :                 "  cast(NULL AS timestamp) AS CREATED,\n"
    6405             :                 "  cast(NULL AS timestamp) AS LAST_ALTERED,\n"
    6406             :                 "  cast(NULL AS varchar(1)) AS NEW_SAVEPOINT_LEVEL,\n"
    6407             :                 "  cast(NULL AS varchar(1)) AS IS_UDT_DEPENDENT,\n"
    6408             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_FROM_DATA_TYPE,\n"
    6409             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_AS_LOCATOR,\n"
    6410             :                 "  cast(NULL AS int) AS RESULT_CAST_CHAR_MAX_LENGTH,\n"
    6411             :                 "  cast(NULL AS int) AS RESULT_CAST_CHAR_OCTET_LENGTH,\n"
    6412             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_CHAR_SET_CATALOG,\n"
    6413             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_CHAR_SET_SCHEMA,\n"
    6414             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_CHARACTER_SET_NAME,\n"
    6415             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_CATALOG,\n"
    6416             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_SCHEMA,\n"
    6417             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_NAME,\n"
    6418             :                 "  cast(NULL AS int) AS RESULT_CAST_NUMERIC_PRECISION,\n"
    6419             :                 "  cast(NULL AS int) AS RESULT_CAST_NUMERIC_RADIX,\n"
    6420             :                 "  cast(NULL AS int) AS RESULT_CAST_NUMERIC_SCALE,\n"
    6421             :                 "  cast(NULL AS int) AS RESULT_CAST_DATETIME_PRECISION,\n"
    6422             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_INTERVAL_TYPE,\n"
    6423             :                 "  cast(NULL AS int) AS RESULT_CAST_INTERVAL_PRECISION,\n"
    6424             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_CATALOG,\n"
    6425             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_SCHEMA,\n"
    6426             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_NAME,\n"
    6427             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_CATALOG,\n"
    6428             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_SCHEMA,\n"
    6429             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_NAME,\n"
    6430             :                 "  cast(NULL AS int) AS RESULT_CAST_MAX_CARDINALITY,\n"
    6431             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_DTD_IDENTIFIER,\n"
    6432             :                 "  cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
    6433             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
    6434             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
    6435             :                 "  cast(NULL AS varchar(1)) AS RESULT_CAST_FROM_DECLARED_DATA_TYPE,\n"
    6436             :                 "  cast(NULL AS int) AS RESULT_CAST_DECLARED_NUMERIC_PRECISION,\n"
    6437             :                 "  cast(NULL AS int) AS RESULT_CAST_DECLARED_NUMERIC_SCALE,\n"
    6438             :                 "  f.\"schema_id\" AS schema_id,\n"
    6439             :                 "  f.\"id\" AS function_id,\n"
    6440             :                 "  f.\"type\" AS function_type,\n"
    6441             :                 "  f.\"language\" AS function_language,\n"
    6442             :                 "  f.\"system\" AS is_system,\n"
    6443             :                 "  cm.\"remark\" AS comments\n"
    6444             :                 " FROM sys.\"functions\" f\n"
    6445             :                 " INNER JOIN sys.\"schemas\" s ON s.\"id\" = f.\"schema_id\"\n"
    6446             :                 " INNER JOIN sys.\"function_types\" ft ON ft.\"function_type_id\" = f.\"type\"\n"
    6447             :                 " INNER JOIN sys.\"function_languages\" fl ON fl.\"language_id\" = f.\"language\"\n"
    6448             :                 " LEFT OUTER JOIN sys.\"args\" a ON a.\"func_id\" = f.\"id\" and a.\"inout\" = 0 and a.\"number\" = 0\n"
    6449             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON cm.\"id\" = f.\"id\"\n"
    6450             :                 " WHERE f.\"type\" in (1, 2, 5, 7)\n"
    6451             :                 " ORDER BY s.\"name\", f.\"name\";\n"
    6452             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.ROUTINES TO PUBLIC WITH GRANT OPTION;\n"
    6453             : 
    6454             :                 "CREATE VIEW INFORMATION_SCHEMA.PARAMETERS AS SELECT\n"
    6455             :                 "  cast(NULL AS varchar(1)) AS SPECIFIC_CATALOG,\n"
    6456             :                 "  s.\"name\" AS SPECIFIC_SCHEMA,\n"
    6457             :                 "  cast(f.\"name\"||'('||f.\"id\"||')' AS varchar(270)) AS SPECIFIC_NAME,\n"
    6458             :                 "  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"
    6459             :                 "  cast(sys.ifthenelse(a.\"inout\" = 0, 'OUT', sys.ifthenelse(a.\"inout\" = 1, 'IN', 'INOUT')) as varchar(5)) AS PARAMETER_MODE,\n"
    6460             :                 "  cast(sys.ifthenelse(a.\"inout\" = 0, 'YES', 'NO') as varchar(3)) AS IS_RESULT,\n"
    6461             :                 "  cast(NULL AS varchar(1)) AS AS_LOCATOR,\n"
    6462             :                 "  a.\"name\" AS PARAMETER_NAME,\n"
    6463             :                 "  cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_CATALOG,\n"
    6464             :                 "  cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_SCHEMA,\n"
    6465             :                 "  cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_NAME,\n"
    6466             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_CATALOG,\n"
    6467             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_SCHEMA,\n"
    6468             :                 "  cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_NAME,\n"
    6469             :                 "  cast(sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true) AS varchar(1024)) AS DATA_TYPE,\n"
    6470             :                 "  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"
    6471             :                 "  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"
    6472             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
    6473             :                 "  cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
    6474             :                 "  cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml'), 'UTF-8', NULL) AS varchar(16)) AS CHARACTER_SET_NAME,\n"
    6475             :                 "  cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
    6476             :                 "  cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
    6477             :                 "  cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
    6478             :                 "  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"
    6479             :                 "  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"
    6480             :                 "  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"
    6481             :                 "  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"
    6482             :                 "  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"
    6483             :                 "  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"
    6484             :                 "  cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
    6485             :                 "  cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
    6486             :                 "  cast(NULL AS varchar(1)) AS UDT_NAME,\n"
    6487             :                 "  cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
    6488             :                 "  cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
    6489             :                 "  cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
    6490             :                 "  cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
    6491             :                 "  cast(NULL AS varchar(1)) AS DTD_IDENTIFIER,\n"
    6492             :                 "  cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
    6493             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
    6494             :                 "  cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
    6495             :                 "  cast(NULL AS varchar(1)) AS PARAMETER_DEFAULT,\n"
    6496             :                 "  f.\"schema_id\" AS schema_id,\n"
    6497             :                 "  f.\"id\" AS function_id,\n"
    6498             :                 "  a.\"id\" AS arg_id,\n"
    6499             :                 "  f.\"name\" AS function_name,\n"
    6500             :                 "  f.\"type\" AS function_type,\n"
    6501             :                 "  f.\"system\" AS is_system\n"
    6502             :                 " FROM sys.\"args\" a\n"
    6503             :                 " 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"
    6504             :                 " INNER JOIN sys.\"schemas\" s ON s.\"id\" = f.\"schema_id\"\n"
    6505             :                 " ORDER BY s.\"name\", f.\"name\", f.\"id\", a.\"inout\" DESC, a.\"number\";\n"
    6506             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.PARAMETERS TO PUBLIC WITH GRANT OPTION;\n"
    6507             : 
    6508             :                 "CREATE VIEW INFORMATION_SCHEMA.SEQUENCES AS SELECT\n"
    6509             :                 "  cast(NULL AS varchar(1)) AS SEQUENCE_CATALOG,\n"
    6510             :                 "  s.\"name\" AS SEQUENCE_SCHEMA,\n"
    6511             :                 "  sq.\"name\" AS SEQUENCE_NAME,\n"
    6512             :                 "  cast('BIGINT' AS varchar(16)) AS DATA_TYPE,\n"
    6513             :                 "  cast(64 AS SMALLINT) AS NUMERIC_PRECISION,\n"
    6514             :                 "  cast(2 AS SMALLINT) AS NUMERIC_PRECISION_RADIX,\n"
    6515             :                 "  cast(0 AS SMALLINT) AS NUMERIC_SCALE,\n"
    6516             :                 "  sq.\"start\" AS START_VALUE,\n"
    6517             :                 "  sq.\"minvalue\" AS MINIMUM_VALUE,\n"
    6518             :                 "  sq.\"maxvalue\" AS MAXIMUM_VALUE,\n"
    6519             :                 "  sq.\"increment\" AS INCREMENT,\n"
    6520             :                 "  cast(sys.ifthenelse(sq.\"cycle\", 'YES', 'NO') AS varchar(3)) AS CYCLE_OPTION,\n"
    6521             :                 "  cast(NULL AS varchar(16)) AS DECLARED_DATA_TYPE,\n"
    6522             :                 "  cast(NULL AS SMALLINT) AS DECLARED_NUMERIC_PRECISION,\n"
    6523             :                 "  cast(NULL AS SMALLINT) AS DECLARED_NUMERIC_SCALE,\n"
    6524             :                 "  sq.\"schema_id\" AS schema_id,\n"
    6525             :                 "  sq.\"id\" AS sequence_id,\n"
    6526             :                 "  get_value_for(s.\"name\", sq.\"name\") AS current_value,\n"
    6527             :                 "  sq.\"cacheinc\" AS cacheinc,\n"
    6528             :                 "  cm.\"remark\" AS comments\n"
    6529             :                 " FROM sys.\"sequences\" sq\n"
    6530             :                 " INNER JOIN sys.\"schemas\" s ON sq.\"schema_id\" = s.\"id\"\n"
    6531             :                 " LEFT OUTER JOIN sys.\"comments\" cm ON sq.\"id\" = cm.\"id\"\n"
    6532             :                 " ORDER BY s.\"name\", sq.\"name\";\n"
    6533             :                 "GRANT SELECT ON TABLE INFORMATION_SCHEMA.SEQUENCES TO PUBLIC WITH GRANT OPTION;\n"
    6534             :                 "\n"
    6535             :                 "update sys._tables set system = true where system <> true\n"
    6536             :                 " and schema_id = (select s.id from sys.schemas s where s.name = 'information_schema')\n"
    6537             :                 " and name in ('character_sets','check_constraints','columns','parameters','routines','schemata','sequences','referential_constraints','table_constraints','tables','views');\n";
    6538           8 :                 printf("Running database upgrade commands:\n%s\n", cmds);
    6539           8 :                 fflush(stdout);
    6540           8 :                 err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
    6541             :         }
    6542             : 
    6543             :         /* 77_storage.sql */
    6544          99 :         sql_find_subtype(&tp, "varchar", 0, 0);
    6545             : 
    6546          99 :         if (!sql_bind_func(sql, s->base.name, "persist_unlogged", &tp, &tp, F_UNION, true)) {
    6547           8 :                 sql->session->status = 0;
    6548           8 :                 sql->errstr[0] = '\0';
    6549           8 :                 const char *query =
    6550             :                         "CREATE FUNCTION sys.persist_unlogged(sname STRING, tname STRING)\n"
    6551             :                         "RETURNS TABLE(\"table\" STRING, \"table_id\" INT, \"rowcount\" BIGINT)\n"
    6552             :                         "EXTERNAL NAME sql.persist_unlogged;\n"
    6553             :                         "GRANT EXECUTE ON FUNCTION sys.persist_unlogged(string, string) TO PUBLIC;\n"
    6554             :                         "UPDATE sys.functions SET system = true WHERE system <> true AND\n"
    6555             :                         "name = 'persist_unlogged' AND schema_id = 2000 AND type = 5 AND language = 1;\n";
    6556           8 :                 printf("Running database upgrade commands:\n%s\n", query);
    6557           8 :                 fflush(stdout);
    6558           8 :                 err = SQLstatementIntern(c, query, "update", true, false, NULL);
    6559             :         }
    6560             : 
    6561          99 :         return err;
    6562             : }
    6563             : 
    6564             : static str
    6565          99 : sql_update_dec2023_sp1(Client c, mvc *sql, sql_schema *s)
    6566             : {
    6567          99 :         char *err;
    6568          99 :         res_table *output;
    6569          99 :         BAT *b;
    6570             : 
    6571          99 :         (void) sql;
    6572          99 :         (void) s;
    6573             : 
    6574             :         /* json.isvalid(json) has been fixed to return NULL on NULL input */
    6575          99 :         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);
    6576          99 :         if (err)
    6577             :                 return err;
    6578          99 :         b = BATdescriptor(output->cols[0].b);
    6579          99 :         if (b) {
    6580          99 :                 if (BATcount(b) > 0) {
    6581           8 :                         const char *query = "drop function json.isvalid(json);\n"
    6582             :                                 "create function json.isvalid(js json)\n"
    6583             :                                 "returns bool begin return case when js is NULL then NULL else true end; end;\n"
    6584             :                                 "GRANT EXECUTE ON FUNCTION json.isvalid(json) TO PUBLIC;\n"
    6585             :                                 "update sys.functions set system = true where system <> true and name = 'isvalid' and schema_id = (select id from sys.schemas where name = 'json');\n";
    6586           8 :                         assert(BATcount(b) == 1);
    6587           8 :                         printf("Running database upgrade commands:\n%s\n", query);
    6588           8 :                         fflush(stdout);
    6589           8 :                         err = SQLstatementIntern(c, query, "update", true, false, NULL);
    6590             :                 }
    6591          99 :                 BBPunfix(b->batCacheid);
    6592             :         }
    6593          99 :         res_table_destroy(output);
    6594          99 :         return err;
    6595             : }
    6596             : 
    6597             : int
    6598         102 : SQLupgrades(Client c, mvc *m)
    6599             : {
    6600         102 :         sql_subtype tp;
    6601         102 :         sql_subfunc *f;
    6602         102 :         char *err;
    6603         102 :         sql_schema *s = mvc_bind_schema(m, "sys");
    6604             : 
    6605         102 :         if ((err = check_sys_tables(c, m, s)) != NULL) {
    6606           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6607           0 :                 goto handle_error;
    6608             :         }
    6609             : 
    6610             : #ifdef HAVE_HGE
    6611         102 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    6612         102 :         if (!sql_bind_func(m, s->base.name, "var_pop", &tp, NULL, F_AGGR, true)) {
    6613           8 :                 m->session->status = 0; /* if the function was not found clean the error */
    6614           8 :                 m->errstr[0] = '\0';
    6615           8 :                 if ((err = sql_update_hugeint(c, m)) != NULL) {
    6616           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6617           0 :                         goto handle_error;
    6618             :                 }
    6619             :         }
    6620             : #endif
    6621             : 
    6622         102 :         if ((err = sql_update_generator(c)) != NULL) {
    6623           3 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6624           3 :                 goto handle_error;
    6625             :         }
    6626             : 
    6627          99 :         f = sql_bind_func_(m, s->base.name, "env", NULL, F_UNION, true);
    6628          99 :         m->session->status = 0; /* if the function was not found clean the error */
    6629          99 :         m->errstr[0] = '\0';
    6630          99 :         sqlstore *store = m->session->tr->store;
    6631          99 :         if (f && sql_privilege(m, ROLE_PUBLIC, f->func->base.id, PRIV_EXECUTE) != PRIV_EXECUTE) {
    6632           0 :                 sql_table *privs = find_sql_table(m->session->tr, s, "privileges");
    6633           0 :                 int pub = ROLE_PUBLIC, p = PRIV_EXECUTE, zero = 0, res;
    6634             : 
    6635           0 :                 if ((res = store->table_api.table_insert(m->session->tr, privs, &f->func->base.id, &pub, &p, &zero, &zero)) != LOG_OK) {
    6636           0 :                         TRC_CRITICAL(SQL_PARSER, "Privilege creation during upgrade failed\n");
    6637           0 :                         return -1;
    6638             :                 }
    6639             :         }
    6640             : 
    6641          99 :         if (sql_bind_func(m, s->base.name, "dependencies_schemas_on_users", NULL, NULL, F_UNION, true)
    6642           0 :          && sql_bind_func(m, s->base.name, "dependencies_owners_on_schemas", NULL, NULL, F_UNION, true)
    6643           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_views", NULL, NULL, F_UNION, true)
    6644           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_indexes", NULL, NULL, F_UNION, true)
    6645           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_triggers", NULL, NULL, F_UNION, true)
    6646           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_foreignkeys", NULL, NULL, F_UNION, true)
    6647           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_functions", NULL, NULL, F_UNION, true)
    6648           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_views", NULL, NULL, F_UNION, true)
    6649           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_keys", NULL, NULL, F_UNION, true)
    6650           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_indexes", NULL, NULL, F_UNION, true)
    6651           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_functions", NULL, NULL, F_UNION, true)
    6652           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_triggers", NULL, NULL, F_UNION, true)
    6653           0 :          && sql_bind_func(m, s->base.name, "dependencies_views_on_functions", NULL, NULL, F_UNION, true)
    6654           0 :          && sql_bind_func(m, s->base.name, "dependencies_views_on_triggers", NULL, NULL, F_UNION, true)
    6655           0 :          && sql_bind_func(m, s->base.name, "dependencies_functions_on_functions", NULL, NULL, F_UNION, true)
    6656           0 :          && sql_bind_func(m, s->base.name, "dependencies_functions_on_triggers", NULL, NULL, F_UNION, true)
    6657           0 :          && sql_bind_func(m, s->base.name, "dependencies_keys_on_foreignkeys", NULL, NULL, F_UNION, true)) {
    6658           0 :                 if ((err = sql_drop_functions_dependencies_Xs_on_Ys(c)) != NULL) {
    6659           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6660           0 :                         goto handle_error;
    6661             :                 }
    6662             :         } else {
    6663          99 :                 m->session->status = 0; /* if the function was not found clean the error */
    6664          99 :                 m->errstr[0] = '\0';
    6665             :         }
    6666             : 
    6667          99 :         sql_find_subtype(&tp, "varchar", 0, 0);
    6668          99 :         if (!sql_bind_func3(m, s->base.name, "deltas", &tp, &tp, &tp, F_UNION, true)) {
    6669           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    6670           0 :                 m->errstr[0] = '\0';
    6671           0 :                 if ((err = sql_update_nov2019_missing_dependencies(c, m)) != NULL) {
    6672           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6673           0 :                         goto handle_error;
    6674             :                 }
    6675           0 :                 if ((err = sql_update_nov2019(c, m)) != NULL) {
    6676           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6677           0 :                         goto handle_error;
    6678             :                 }
    6679             :         }
    6680             : 
    6681             : #ifdef HAVE_HGE
    6682          99 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    6683          99 :         if (!sql_bind_func(m, s->base.name, "median_avg", &tp, NULL, F_AGGR, true)) {
    6684           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    6685           0 :                 m->errstr[0] = '\0';
    6686           0 :                 if ((err = sql_update_nov2019_sp1_hugeint(c, m)) != NULL) {
    6687           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6688           0 :                         goto handle_error;
    6689             :                 }
    6690             :         }
    6691             : #endif
    6692             : 
    6693          99 :         if (!sql_bind_func(m, s->base.name, "suspend_log_flushing", NULL, NULL, F_PROC, true)) {
    6694           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    6695           0 :                 m->errstr[0] = '\0';
    6696           0 :                 if ((err = sql_update_jun2020(c, m)) != NULL) {
    6697           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6698           0 :                         goto handle_error;
    6699             :                 }
    6700             :         }
    6701             : 
    6702          99 :         if ((err = sql_update_jun2020_bam(c, m)) != NULL) {
    6703           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6704           0 :                 goto handle_error;
    6705             :         }
    6706             : 
    6707             : #ifdef HAVE_HGE
    6708          99 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    6709          99 :         if (!sql_bind_func(m, s->base.name, "covar_pop", &tp, &tp, F_AGGR, true)) {
    6710           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    6711           0 :                 m->errstr[0] = '\0';
    6712           0 :                 if ((err = sql_update_jun2020_sp1_hugeint(c)) != NULL) {
    6713           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6714           0 :                         goto handle_error;
    6715             :                 }
    6716             :         }
    6717             : #endif
    6718             : 
    6719          99 :         sql_find_subtype(&tp, "varchar", 0, 0);
    6720          99 :         if (sql_bind_func(m, s->base.name, "lidarattach", &tp, NULL, F_PROC, true)) {
    6721           0 :                 if ((err = sql_update_oscar_lidar(c)) != NULL) {
    6722           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6723           0 :                         goto handle_error;
    6724             :                 }
    6725             :         } else {
    6726          99 :                 m->session->status = 0; /* if the function was not found clean the error */
    6727          99 :                 m->errstr[0] = '\0';
    6728             :         }
    6729             : 
    6730          99 :         if ((err = sql_update_oscar(c, m)) != NULL) {
    6731           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6732           0 :                 goto handle_error;
    6733             :         }
    6734             : 
    6735          99 :         if ((err = sql_update_oct2020(c, m)) != NULL) {
    6736           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6737           0 :                 goto handle_error;
    6738             :         }
    6739             : 
    6740          99 :         if ((err = sql_update_oct2020_sp1(c, m)) != NULL) {
    6741           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6742           0 :                 goto handle_error;
    6743             :         }
    6744             : 
    6745          99 :         if ((err = sql_update_jul2021(c, m)) != NULL) {
    6746           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6747           0 :                 goto handle_error;
    6748             :         }
    6749             : 
    6750          99 :         if ((err = sql_update_jul2021_5(c, m)) != NULL) {
    6751           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6752           0 :                 goto handle_error;
    6753             :         }
    6754             : 
    6755          99 :         if ((err = sql_update_jan2022(c, m)) != NULL) {
    6756           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6757           0 :                 goto handle_error;
    6758             :         }
    6759             : 
    6760          99 :         if ((err = sql_update_sep2022(c, m, s)) != NULL) {
    6761           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6762           0 :                 goto handle_error;
    6763             :         }
    6764             : 
    6765          99 :         if ((err = sql_update_jun2023(c, m, s)) != NULL) {
    6766           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6767           0 :                 goto handle_error;
    6768             :         }
    6769             : 
    6770          99 :         if ((err = sql_update_dec2023_geom(c, m, s)) != NULL) {
    6771           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6772           0 :                 goto handle_error;
    6773             :         }
    6774             : 
    6775          99 :         if ((err = sql_update_jun2023_sp3(c, m, s)) != NULL) {
    6776           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6777           0 :                 goto handle_error;
    6778             :         }
    6779             : 
    6780          99 :         if ((err = sql_update_dec2023(c, m, s)) != NULL) {
    6781           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6782           0 :                 goto handle_error;
    6783             :         }
    6784             : 
    6785          99 :         if ((err = sql_update_dec2023_sp1(c, m, s)) != NULL) {
    6786           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    6787           0 :                 goto handle_error;
    6788             :         }
    6789             : 
    6790             :         return 0;
    6791             : 
    6792           3 : handle_error:
    6793           3 :         freeException(err);
    6794           3 :         return -1;
    6795             : }

Generated by: LCOV version 1.14