LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql_upgrades.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 336 888 37.8 %
Date: 2021-10-13 02:24:04 Functions: 12 22 54.5 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * SQL upgrade code
      11             :  * N. Nes, M.L. Kersten, S. Mullender
      12             :  */
      13             : #include "monetdb_config.h"
      14             : #include "mal_backend.h"
      15             : #include "sql_execute.h"
      16             : #include "sql_mvc.h"
      17             : #include "gdk_time.h"
      18             : #include <unistd.h>
      19             : #include "sql_upgrades.h"
      20             : #include "rel_rel.h"
      21             : #include "rel_semantic.h"
      22             : #include "rel_unnest.h"
      23             : #include "rel_optimizer.h"
      24             : 
      25             : #include "rel_remote.h"
      26             : #include "mal_authorize.h"
      27             : 
      28             : /* this function can be used to recreate the system tables (types,
      29             :  * functions, args) when internal types and/or functions have changed
      30             :  * (i.e. the ones in sql_types.c) */
      31             : static str
      32          12 : sql_fix_system_tables(Client c, mvc *sql, const char *prev_schema)
      33             : {
      34             :         size_t bufsize = 1000000, pos = 0;
      35          12 :         char *buf = GDKmalloc(bufsize), *err = NULL;
      36             :         node *n;
      37             :         sql_schema *s;
      38             :         static const char *boolnames[2] = {"false", "true"};
      39             : 
      40          12 :         if (buf == NULL)
      41           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
      42          12 :         s = mvc_bind_schema(sql, "sys");
      43          12 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
      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       12804 :         for (n = funcs->h; n; n = n->next) {
      72       12792 :                 sql_func *func = n->data;
      73             :                 int number = 0;
      74             :                 sql_arg *arg;
      75             :                 node *m;
      76             : 
      77       12792 :                 if (func->base.id >= FUNC_OIDS)
      78           0 :                         continue;
      79             : 
      80       12792 :                 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             :                                 func->imp, func->mod, (int) FUNC_LANG_INT,
      86       12792 :                                 (int) func->type,
      87       12792 :                                 boolnames[func->side_effect],
      88       12792 :                                 boolnames[func->varres],
      89       12792 :                                 boolnames[func->vararg],
      90       12792 :                                 func->s ? func->s->base.id : s->base.id,
      91       12792 :                                 boolnames[func->system],
      92       12792 :                                 boolnames[func->semantics]);
      93       12792 :                 if (func->res) {
      94       25536 :                         for (m = func->res->h; m; m = m->next, number++) {
      95       12768 :                                 arg = m->data;
      96       12768 :                                 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       12768 :                                                 arg->type.type->base.name,
     106             :                                                 arg->type.digits,
     107             :                                                 arg->type.scale,
     108       12768 :                                                 arg->inout, number);
     109             :                         }
     110             :                 }
     111       36468 :                 for (m = func->ops->h; m; m = m->next, number++) {
     112       23676 :                         arg = m->data;
     113       23676 :                         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       23676 :                                 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       23676 :                                                 arg->type.type->base.name,
     137             :                                                 arg->type.digits,
     138             :                                                 arg->type.scale,
     139       23676 :                                                 arg->inout, number);
     140             :                 }
     141             :         }
     142             : 
     143          12 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
     144             : 
     145          12 :         assert(pos < bufsize);
     146          12 :         printf("Running database upgrade commands:\n%s\n", buf);
     147          12 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     148          12 :         GDKfree(buf);
     149          12 :         return err;             /* usually MAL_SUCCEED */
     150             : }
     151             : 
     152             : #ifdef HAVE_HGE
     153             : static str
     154           8 : sql_update_hugeint(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
     155             : {
     156             :         size_t bufsize = 8192, pos = 0;
     157             :         char *buf, *err;
     158             : 
     159          16 :         if (!*systabfixed &&
     160           8 :             (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL)
     161             :                 return err;
     162           8 :         *systabfixed = true;
     163             : 
     164           8 :         if ((buf = GDKmalloc(bufsize)) == NULL)
     165           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     166             : 
     167           8 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
     168             : 
     169             :         /* 90_generator_hge.sql */
     170           8 :         pos += snprintf(buf + pos, bufsize - pos,
     171             :                         "create function sys.generate_series(first hugeint, \"limit\" hugeint)\n"
     172             :                         "returns table (value hugeint)\n"
     173             :                         "external name generator.series;\n"
     174             :                         "create function sys.generate_series(first hugeint, \"limit\" hugeint, stepsize hugeint)\n"
     175             :                         "returns table (value hugeint)\n"
     176             :                         "external name generator.series;\n");
     177             : 
     178             :         /* 39_analytics_hge.sql */
     179           8 :         pos += snprintf(buf + pos, bufsize - pos,
     180             :                         "create aggregate stddev_samp(val HUGEINT) returns DOUBLE\n"
     181             :                         " external name \"aggr\".\"stdev\";\n"
     182             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(HUGEINT) TO PUBLIC;\n"
     183             :                         "create window stddev_samp(val HUGEINT) returns DOUBLE\n"
     184             :                         " external name \"sql\".\"stdev\";\n"
     185             :                         "GRANT EXECUTE ON WINDOW stddev_samp(HUGEINT) TO PUBLIC;\n"
     186             :                         "create aggregate stddev_pop(val HUGEINT) returns DOUBLE\n"
     187             :                         " external name \"aggr\".\"stdevp\";\n"
     188             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(HUGEINT) TO PUBLIC;\n"
     189             :                         "create window stddev_pop(val HUGEINT) returns DOUBLE\n"
     190             :                         " external name \"sql\".\"stdevp\";\n"
     191             :                         "GRANT EXECUTE ON WINDOW stddev_pop(HUGEINT) TO PUBLIC;\n"
     192             :                         "create aggregate var_samp(val HUGEINT) returns DOUBLE\n"
     193             :                         " external name \"aggr\".\"variance\";\n"
     194             :                         "GRANT EXECUTE ON AGGREGATE var_samp(HUGEINT) TO PUBLIC;\n"
     195             :                         "create window var_samp(val HUGEINT) returns DOUBLE\n"
     196             :                         " external name \"sql\".\"variance\";\n"
     197             :                         "GRANT EXECUTE ON WINDOW var_samp(HUGEINT) TO PUBLIC;\n"
     198             :                         "create aggregate covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     199             :                         " external name \"aggr\".\"covariance\";\n"
     200             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
     201             :                         "create window covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     202             :                         " external name \"sql\".\"covariance\";\n"
     203             :                         "GRANT EXECUTE ON WINDOW covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
     204             :                         "create aggregate var_pop(val HUGEINT) returns DOUBLE\n"
     205             :                         " external name \"aggr\".\"variancep\";\n"
     206             :                         "GRANT EXECUTE ON AGGREGATE var_pop(HUGEINT) TO PUBLIC;\n"
     207             :                         "create window var_pop(val HUGEINT) returns DOUBLE\n"
     208             :                         " external name \"sql\".\"variancep\";\n"
     209             :                         "GRANT EXECUTE ON WINDOW var_pop(HUGEINT) TO PUBLIC;\n"
     210             :                         "create aggregate covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     211             :                         " external name \"aggr\".\"covariancep\";\n"
     212             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
     213             :                         "create window covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     214             :                         " external name \"sql\".\"covariancep\";\n"
     215             :                         "GRANT EXECUTE ON WINDOW covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
     216             :                         "create aggregate median(val HUGEINT) returns HUGEINT\n"
     217             :                         " external name \"aggr\".\"median\";\n"
     218             :                         "GRANT EXECUTE ON AGGREGATE median(HUGEINT) TO PUBLIC;\n"
     219             :                         "create aggregate quantile(val HUGEINT, q DOUBLE) returns HUGEINT\n"
     220             :                         " external name \"aggr\".\"quantile\";\n"
     221             :                         "GRANT EXECUTE ON AGGREGATE quantile(HUGEINT, DOUBLE) TO PUBLIC;\n"
     222             :                         "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
     223             :                         " external name \"aggr\".\"median_avg\";\n"
     224             :                         "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
     225             :                         "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
     226             :                         " external name \"aggr\".\"quantile_avg\";\n"
     227             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n"
     228             :                         "create aggregate corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     229             :                         " external name \"aggr\".\"corr\";\n"
     230             :                         "GRANT EXECUTE ON AGGREGATE corr(HUGEINT, HUGEINT) TO PUBLIC;\n"
     231             :                         "create window corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
     232             :                         " external name \"sql\".\"corr\";\n"
     233             :                         "GRANT EXECUTE ON WINDOW corr(HUGEINT, HUGEINT) TO PUBLIC;\n");
     234             : 
     235             :         /* 40_json_hge.sql */
     236           8 :         pos += snprintf(buf + pos, bufsize - pos,
     237             :                         "create function json.filter(js json, name hugeint)\n"
     238             :                         "returns json external name json.filter;\n"
     239             :                         "GRANT EXECUTE ON FUNCTION json.filter(json, hugeint) TO PUBLIC;\n");
     240             : 
     241           8 :         pos += snprintf(buf + pos, bufsize - pos,
     242             :                         "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"
     243             :                         "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"
     244             :                         "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"
     245             :                         "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",
     246             :                         (int) F_UNION, (int) F_AGGR, (int) F_ANALYTIC, (int) F_FUNC);
     247             : 
     248           8 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
     249           8 :         assert(pos < bufsize);
     250             : 
     251           8 :         printf("Running database upgrade commands:\n%s\n", buf);
     252           8 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     253           8 :         GDKfree(buf);
     254           8 :         return err;             /* usually MAL_SUCCEED */
     255             : }
     256             : #endif
     257             : 
     258             : static str
     259           0 : sql_drop_functions_dependencies_Xs_on_Ys(Client c, const char *prev_schema)
     260             : {
     261             :         size_t bufsize = 1600, pos = 0;
     262           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     263             : 
     264           0 :         if (buf == NULL)
     265           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     266             : 
     267             :         /* remove functions which were created in sql/scripts/21_dependency_functions.sql */
     268           0 :         pos += snprintf(buf + pos, bufsize - pos,
     269             :                         "set schema \"sys\";\n"
     270             :                         "DROP FUNCTION dependencies_schemas_on_users();\n"
     271             :                         "DROP FUNCTION dependencies_owners_on_schemas();\n"
     272             :                         "DROP FUNCTION dependencies_tables_on_views();\n"
     273             :                         "DROP FUNCTION dependencies_tables_on_indexes();\n"
     274             :                         "DROP FUNCTION dependencies_tables_on_triggers();\n"
     275             :                         "DROP FUNCTION dependencies_tables_on_foreignKeys();\n"
     276             :                         "DROP FUNCTION dependencies_tables_on_functions();\n"
     277             :                         "DROP FUNCTION dependencies_columns_on_views();\n"
     278             :                         "DROP FUNCTION dependencies_columns_on_keys();\n"
     279             :                         "DROP FUNCTION dependencies_columns_on_indexes();\n"
     280             :                         "DROP FUNCTION dependencies_columns_on_functions();\n"
     281             :                         "DROP FUNCTION dependencies_columns_on_triggers();\n"
     282             :                         "DROP FUNCTION dependencies_views_on_functions();\n"
     283             :                         "DROP FUNCTION dependencies_views_on_triggers();\n"
     284             :                         "DROP FUNCTION dependencies_functions_on_functions();\n"
     285             :                         "DROP FUNCTION dependencies_functions_on_triggers();\n"
     286             :                         "DROP FUNCTION dependencies_keys_on_foreignKeys();\n");
     287             : 
     288           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
     289           0 :         assert(pos < bufsize);
     290             : 
     291           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     292           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     293           0 :         GDKfree(buf);
     294           0 :         return err;             /* usually MAL_SUCCEED */
     295             : }
     296             : 
     297             : static str
     298           0 : sql_update_apr2019(Client c, mvc *sql, const char *prev_schema)
     299             : {
     300             :         size_t bufsize = 3000, pos = 0;
     301             :         char *buf, *err;
     302           0 :         sql_schema *s = mvc_bind_schema(sql, "sys");
     303             :         sql_table *t;
     304             : 
     305           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
     306           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     307             : 
     308           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema sys;\n");
     309             : 
     310             :         /* 15_querylog.sql */
     311           0 :         pos += snprintf(buf + pos, bufsize - pos,
     312             :                         "drop procedure sys.querylog_enable(smallint);\n"
     313             :                         "create procedure sys.querylog_enable(threshold integer) external name sql.querylog_enable;\n"
     314             :                         "update sys.functions set system = true where system <> true and name = 'querylog_enable' and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n",
     315             :                         (int) F_PROC);
     316             : 
     317             :         /* 17_temporal.sql */
     318           0 :         pos += snprintf(buf + pos, bufsize - pos,
     319             :                         "create function sys.date_trunc(txt string, t timestamp)\n"
     320             :                         "returns timestamp\n"
     321             :                         "external name sql.date_trunc;\n"
     322             :                         "grant execute on function sys.date_trunc(string, timestamp) to public;\n"
     323             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys') and name = 'date_trunc' and type = %d;\n", (int) F_FUNC);
     324             : 
     325             :         /* 22_clients.sql */
     326           0 :         pos += snprintf(buf + pos, bufsize - pos,
     327             :                         "create procedure sys.setprinttimeout(\"timeout\" integer)\n"
     328             :                         "external name clients.setprinttimeout;\n"
     329             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys') and name = 'setprinttimeout' and type = %d;\n", (int) F_PROC);
     330             : 
     331             :         /* 26_sysmon.sql */
     332           0 :         pos += snprintf(buf + pos, bufsize - pos,
     333             :                         "grant execute on function sys.queue to public;\n"
     334             :                         "grant select on sys.queue to public;\n");
     335             : 
     336             :         /* 51_sys_schema_extensions.sql */
     337           0 :         pos += snprintf(buf + pos, bufsize - pos,
     338             :                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
     339             :                         "INSERT INTO sys.keywords VALUES ('WINDOW');\n"
     340             :                 );
     341           0 :         t = mvc_bind_table(sql, s, "var_values");
     342           0 :         t->system = 0;       /* make it non-system else the drop view will fail */
     343           0 :         pos += snprintf(buf + pos, bufsize - pos,
     344             :                         "DROP VIEW sys.var_values;\n"
     345             :                         "CREATE VIEW sys.var_values (var_name, value) AS\n"
     346             :                         "SELECT 'cache' AS var_name, convert(cache, varchar(10)) AS value UNION ALL\n"
     347             :                         "SELECT 'current_role', current_role UNION ALL\n"
     348             :                         "SELECT 'current_schema', current_schema UNION ALL\n"
     349             :                         "SELECT 'current_timezone', current_timezone UNION ALL\n"
     350             :                         "SELECT 'current_user', current_user UNION ALL\n"
     351             :                         "SELECT 'debug', debug UNION ALL\n"
     352             :                         "SELECT 'last_id', last_id UNION ALL\n"
     353             :                         "SELECT 'optimizer', optimizer UNION ALL\n"
     354             :                         "SELECT 'pi', pi() UNION ALL\n"
     355             :                         "SELECT 'rowcnt', rowcnt;\n"
     356             :                         "UPDATE sys._tables SET system = true WHERE name = 'var_values' AND schema_id = (SELECT id FROM sys.schemas WHERE name = 'sys');\n"
     357             :                         "GRANT SELECT ON sys.var_values TO PUBLIC;\n");
     358             : 
     359             :         /* 99_system.sql */
     360           0 :         t = mvc_bind_table(sql, s, "systemfunctions");
     361           0 :         t->system = 0;
     362           0 :         pos += snprintf(buf + pos, bufsize - pos,
     363             :                         "drop table sys.systemfunctions;\n"
     364             :                         "create view sys.systemfunctions as select id as function_id from sys.functions where system;\n"
     365             :                         "grant select on sys.systemfunctions to public;\n"
     366             :                         "update sys._tables set system = true where name = 'systemfunctions' and schema_id = (select id from sys.schemas where name = 'sys');\n");
     367             :         /* update type of "query" attribute of tables sys._tables and
     368             :          * tmp_tables from varchar(2048) to varchar(1048576) */
     369           0 :         pos += snprintf(buf + pos, bufsize - pos,
     370             :                         "update sys._columns set type_digits = 1048576 where name = 'query' and table_id in (select id from sys._tables t where t.name = '_tables' and t.schema_id in (select id from sys.schemas s where s.name in ('sys', 'tmp')));\n");
     371           0 :         pos += snprintf(buf + pos, bufsize - pos,
     372             :                         "update sys._columns set type_digits = 1048576 where name = 'query' and table_id in (select id from sys._tables t where t.name = 'tables' and t.schema_id in (select id from sys.schemas s where s.name = 'sys'));\n");
     373             : 
     374           0 :         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
     375           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
     376             : 
     377           0 :         assert(pos < bufsize);
     378           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     379           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     380           0 :         if (err == MAL_SUCCEED) {
     381           0 :                 pos = snprintf(buf, bufsize, "set schema \"sys\";\n"
     382             :                                "ALTER TABLE sys.keywords SET READ ONLY;\n");
     383             : 
     384           0 :                 pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
     385           0 :                 assert(pos < bufsize);
     386           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
     387           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     388             :         }
     389             : 
     390           0 :         GDKfree(buf);
     391           0 :         return err;             /* usually MAL_SUCCEED */
     392             : }
     393             : 
     394             : static str
     395           0 : sql_update_storagemodel(Client c, mvc *sql, const char *prev_schema, bool oct2020_upgrade)
     396             : {
     397             :         size_t bufsize = 20000, pos = 0;
     398             :         char *buf, *err;
     399           0 :         sql_schema *s = mvc_bind_schema(sql, "sys");
     400             :         sql_table *t;
     401           0 :         char *day_interval_str = oct2020_upgrade ? " 'day_interval'," : "";
     402             : 
     403           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
     404           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     405             : 
     406             :         /* set views and tables internally to non-system to allow drop commands to succeed without error */
     407           0 :         if ((t = mvc_bind_table(sql, s, "storage")) != NULL)
     408           0 :                 t->system = 0;
     409           0 :         if ((t = mvc_bind_table(sql, s, "storagemodel")) != NULL)
     410           0 :                 t->system = 0;
     411           0 :         if ((t = mvc_bind_table(sql, s, "storagemodelinput")) != NULL)
     412           0 :                 t->system = 0;
     413           0 :         if ((t = mvc_bind_table(sql, s, "tablestoragemodel")) != NULL)
     414           0 :                 t->system = 0;
     415             : 
     416             :         /* new 75_storagemodel.sql */
     417           0 :         pos += snprintf(buf + pos, bufsize - pos,
     418             :                 "set schema sys;\n"
     419             :                 /* drop objects in reverse order of original creation of old 75_storagemodel.sql */
     420             :                 "drop view if exists sys.tablestoragemodel;\n"
     421             :                 "drop view if exists sys.storagemodel cascade;\n"
     422             :                 "drop function if exists sys.storagemodel() cascade;\n");
     423             : 
     424           0 :         if (oct2020_upgrade) {
     425           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     426             :                         "drop function if exists sys.imprintsize(varchar(1024), bigint) cascade;\n");
     427             :         } else {
     428           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     429             :                         "drop function if exists sys.imprintsize(bigint, clob) cascade;\n");
     430             :         }
     431             : 
     432           0 :         pos += snprintf(buf + pos, bufsize - pos,
     433             :                 "drop function if exists sys.hashsize(boolean, bigint) cascade;\n");
     434             : 
     435           0 :         if (oct2020_upgrade) {
     436           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     437             :                         "drop function if exists sys.columnsize(varchar(1024), bigint) cascade;\n"
     438             :                         "drop function if exists sys.heapsize(varchar(1024), bigint, bigint, int) cascade;\n");
     439             :         } else {
     440           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     441             :                         "drop function if exists sys.columnsize(clob, bigint, bigint) cascade;\n"
     442             :                         "drop function if exists sys.heapsize(clob, bigint, int) cascade;\n");
     443             :         }
     444             : 
     445           0 :         pos += snprintf(buf + pos, bufsize - pos,
     446             :                 "drop procedure if exists sys.storagemodelinit();\n"
     447             :                 "drop table if exists sys.storagemodelinput cascade;\n"
     448             :                 "drop view if exists sys.\"storage\" cascade;\n");
     449             : 
     450           0 :         if (oct2020_upgrade) {
     451           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     452             :                         "drop function if exists sys.\"storage\"(varchar(1024), varchar(1024), varchar(1024)) cascade;\n"
     453             :                         "drop function if exists sys.\"storage\"(varchar(1024), varchar(1024)) cascade;\n"
     454             :                         "drop function if exists sys.\"storage\"(varchar(1024)) cascade;\n");
     455             :         } else {
     456           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     457             :                         "drop function if exists sys.\"storage\"(clob, clob, clob) cascade;\n"
     458             :                         "drop function if exists sys.\"storage\"(clob, clob) cascade;\n"
     459             :                         "drop function if exists sys.\"storage\"(clob) cascade;\n");
     460             :         }
     461             : 
     462             :         /* new 75_storagemodel.sql */
     463           0 :         pos += snprintf(buf + pos, bufsize - pos,
     464             :                 "drop function if exists sys.\"storage\"() cascade;\n"
     465             :                 "create function sys.\"storage\"()\n"
     466             :                 "returns table (\n"
     467             :                 "  \"schema\" varchar(1024),\n"
     468             :                 "  \"table\" varchar(1024),\n"
     469             :                 "  \"column\" varchar(1024),\n"
     470             :                 "  \"type\" varchar(1024),\n"
     471             :                 "  \"mode\" varchar(15),\n"
     472             :                 "  location varchar(1024),\n"
     473             :                 "  \"count\" bigint,\n"
     474             :                 "  typewidth int,\n"
     475             :                 "  columnsize bigint,\n"
     476             :                 "  heapsize bigint,\n"
     477             :                 "  hashes bigint,\n"
     478             :                 "  phash boolean,\n"
     479             :                 "  \"imprints\" bigint,\n"
     480             :                 "  sorted boolean,\n"
     481             :                 "  revsorted boolean,\n"
     482             :                 "  \"unique\" boolean,\n"
     483             :                 "  orderidx bigint\n"
     484             :                 ")\n"
     485             :                 "external name sql.\"storage\";\n"
     486             :                 "create view sys.\"storage\" as\n"
     487             :                 "select * from sys.\"storage\"()\n"
     488             :                 " where (\"schema\", \"table\") in (\n"
     489             :                 "  SELECT sch.\"name\", tbl.\"name\"\n"
     490             :                 "    FROM sys.\"tables\" AS tbl JOIN sys.\"schemas\" AS sch ON tbl.schema_id = sch.id\n"
     491             :                 "   WHERE tbl.\"system\" = FALSE)\n"
     492             :                 "order by \"schema\", \"table\", \"column\";\n"
     493             :                 "create view sys.\"tablestorage\" as\n"
     494             :                 "select \"schema\", \"table\",\n"
     495             :                 "  max(\"count\") as \"rowcount\",\n"
     496             :                 "  count(*) as \"storages\",\n"
     497             :                 "  sum(columnsize) as columnsize,\n"
     498             :                 "  sum(heapsize) as heapsize,\n"
     499             :                 "  sum(hashes) as hashsize,\n"
     500             :                 "  sum(\"imprints\") as imprintsize,\n"
     501             :                 "  sum(orderidx) as orderidxsize\n"
     502             :                 " from sys.\"storage\"\n"
     503             :                 "group by \"schema\", \"table\"\n"
     504             :                 "order by \"schema\", \"table\";\n"
     505             :                 "create view sys.\"schemastorage\" as\n"
     506             :                 "select \"schema\",\n"
     507             :                 "  count(*) as \"storages\",\n"
     508             :                 "  sum(columnsize) as columnsize,\n"
     509             :                 "  sum(heapsize) as heapsize,\n"
     510             :                 "  sum(hashes) as hashsize,\n"
     511             :                 "  sum(\"imprints\") as imprintsize,\n"
     512             :                 "  sum(orderidx) as orderidxsize\n"
     513             :                 " from sys.\"storage\"\n"
     514             :                 "group by \"schema\"\n"
     515             :                 "order by \"schema\";\n"
     516             :                 "create function sys.\"storage\"(sname varchar(1024))\n"
     517             :                 "returns table (\n"
     518             :                 "  \"schema\" varchar(1024),\n"
     519             :                 "  \"table\" varchar(1024),\n"
     520             :                 "  \"column\" varchar(1024),\n"
     521             :                 "  \"type\" varchar(1024),\n"
     522             :                 "  \"mode\" varchar(15),\n"
     523             :                 "  location varchar(1024),\n"
     524             :                 "  \"count\" bigint,\n"
     525             :                 "  typewidth int,\n"
     526             :                 "  columnsize bigint,\n"
     527             :                 "  heapsize bigint,\n"
     528             :                 "  hashes bigint,\n"
     529             :                 "  phash boolean,\n"
     530             :                 "  \"imprints\" bigint,\n"
     531             :                 "  sorted boolean,\n"
     532             :                 "  revsorted boolean,\n"
     533             :                 "  \"unique\" boolean,\n"
     534             :                 "  orderidx bigint\n"
     535             :                 ")\n"
     536             :                 "external name sql.\"storage\";\n"
     537             :                 "create function sys.\"storage\"(sname varchar(1024), tname varchar(1024))\n"
     538             :                 "returns table (\n"
     539             :                 "  \"schema\" varchar(1024),\n"
     540             :                 "  \"table\" varchar(1024),\n"
     541             :                 "  \"column\" varchar(1024),\n"
     542             :                 "  \"type\" varchar(1024),\n"
     543             :                 "  \"mode\" varchar(15),\n"
     544             :                 "  location varchar(1024),\n"
     545             :                 "  \"count\" bigint,\n"
     546             :                 "  typewidth int,\n"
     547             :                 "  columnsize bigint,\n"
     548             :                 "  heapsize bigint,\n"
     549             :                 "  hashes bigint,\n"
     550             :                 "  phash boolean,\n"
     551             :                 "  \"imprints\" bigint,\n"
     552             :                 "  sorted boolean,\n"
     553             :                 "  revsorted boolean,\n"
     554             :                 "  \"unique\" boolean,\n"
     555             :                 "  orderidx bigint\n"
     556             :                 ")\n"
     557             :                 "external name sql.\"storage\";\n"
     558             :                 "create function sys.\"storage\"(sname varchar(1024), tname varchar(1024), cname varchar(1024))\n"
     559             :                 "returns table (\n"
     560             :                 "  \"schema\" varchar(1024),\n"
     561             :                 "  \"table\" varchar(1024),\n"
     562             :                 "  \"column\" varchar(1024),\n"
     563             :                 "  \"type\" varchar(1024),\n"
     564             :                 "  \"mode\" varchar(15),\n"
     565             :                 "  location varchar(1024),\n"
     566             :                 "  \"count\" bigint,\n"
     567             :                 "  typewidth int,\n"
     568             :                 "  columnsize bigint,\n"
     569             :                 "  heapsize bigint,\n"
     570             :                 "  hashes bigint,\n"
     571             :                 "  phash boolean,\n"
     572             :                 "  \"imprints\" bigint,\n"
     573             :                 "  sorted boolean,\n"
     574             :                 "  revsorted boolean,\n"
     575             :                 "  \"unique\" boolean,\n"
     576             :                 "  orderidx bigint\n"
     577             :                 ")\n"
     578             :                 "external name sql.\"storage\";\n"
     579             :                 "create table sys.storagemodelinput(\n"
     580             :                 "  \"schema\" varchar(1024) NOT NULL,\n"
     581             :                 "  \"table\" varchar(1024) NOT NULL,\n"
     582             :                 "  \"column\" varchar(1024) NOT NULL,\n"
     583             :                 "  \"type\" varchar(1024) NOT NULL,\n"
     584             :                 "  typewidth int NOT NULL,\n"
     585             :                 "  \"count\" bigint NOT NULL,\n"
     586             :                 "  \"distinct\" bigint NOT NULL,\n"
     587             :                 "  atomwidth int NOT NULL,\n"
     588             :                 "  reference boolean NOT NULL DEFAULT FALSE,\n"
     589             :                 "  sorted boolean,\n"
     590             :                 "  \"unique\" boolean,\n"
     591             :                 "  isacolumn boolean NOT NULL DEFAULT TRUE\n"
     592             :                 ");\n"
     593             :                 "create procedure sys.storagemodelinit()\n"
     594             :                 "begin\n"
     595             :                 "  delete from sys.storagemodelinput;\n"
     596             :                 "  insert into sys.storagemodelinput\n"
     597             :                 "  select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
     598             :                 "          case when (\"unique\" or \"type\" IN ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
     599             :                 "                  then \"count\" else 0 end,\n"
     600             :                 "          case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
     601             :                 "                  then cast((heapsize - 8192) / \"count\" as bigint)\n"
     602             :                 "          when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
     603             :                 "                  then cast((heapsize - 32) / \"count\" as bigint)\n"
     604             :                 "          else typewidth end,\n"
     605             :                 "          FALSE, case sorted when true then true else false end, \"unique\", TRUE\n"
     606             :                 "    from sys.\"storage\";\n"
     607             :                 "  update sys.storagemodelinput\n"
     608             :                 "     set reference = TRUE\n"
     609             :                 "   where (\"schema\", \"table\", \"column\") in (\n"
     610             :                 "          SELECT fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
     611             :                 "            FROM  sys.\"keys\" AS fkkey,\n"
     612             :                 "                  sys.\"objects\" AS fkkeycol,\n"
     613             :                 "                  sys.\"tables\" AS fktable,\n"
     614             :                 "                  sys.\"schemas\" AS fkschema\n"
     615             :                 "          WHERE fktable.\"id\" = fkkey.\"table_id\"\n"
     616             :                 "            AND fkkey.\"id\" = fkkeycol.\"id\"\n"
     617             :                 "            AND fkschema.\"id\" = fktable.\"schema_id\"\n"
     618             :                 "            AND fkkey.\"rkey\" > -1 );\n"
     619             :                 "  update sys.storagemodelinput\n"
     620             :                 "     set isacolumn = FALSE\n"
     621             :                 "   where (\"schema\", \"table\", \"column\") NOT in (\n"
     622             :                 "          SELECT sch.\"name\", tbl.\"name\", col.\"name\"\n"
     623             :                 "            FROM sys.\"schemas\" AS sch,\n"
     624             :                 "                  sys.\"tables\" AS tbl,\n"
     625             :                 "                  sys.\"columns\" AS col\n"
     626             :                 "          WHERE sch.\"id\" = tbl.\"schema_id\"\n"
     627             :                 "            AND tbl.\"id\" = col.\"table_id\");\n"
     628             :                 "end;\n"
     629             :                 "create function sys.columnsize(tpe varchar(1024), count bigint)\n"
     630             :                 "returns bigint\n"
     631             :                 "begin\n"
     632             :                 "  if tpe in ('tinyint', 'boolean')\n"
     633             :                 "          then return count;\n"
     634             :                 "  end if;\n"
     635             :                 "  if tpe = 'smallint'\n"
     636             :                 "          then return 2 * count;\n"
     637             :                 "  end if;\n"
     638             :                 "  if tpe in ('int', 'real', 'date', 'time', 'timetz', 'sec_interval',%s 'month_interval')\n"
     639             :                 "          then return 4 * count;\n"
     640             :                 "  end if;\n"
     641             :                 "  if tpe in ('bigint', 'double', 'timestamp', 'timestamptz', 'inet', 'oid')\n"
     642             :                 "          then return 8 * count;\n"
     643             :                 "  end if;\n"
     644             :                 "  if tpe in ('hugeint', 'decimal', 'uuid', 'mbr')\n"
     645             :                 "          then return 16 * count;\n"
     646             :                 "  end if;\n"
     647             :                 "  if tpe in ('varchar', 'char', 'clob', 'json', 'url')\n"
     648             :                 "          then return 4 * count;\n"
     649             :                 "  end if;\n"
     650             :                 "  if tpe in ('blob', 'geometry', 'geometrya')\n"
     651             :                 "          then return 8 * count;\n"
     652             :                 "  end if;\n"
     653             :                 "  return 8 * count;\n"
     654             :                 "end;\n"
     655             :                 "create function sys.heapsize(tpe varchar(1024), count bigint, distincts bigint, avgwidth int)\n"
     656             :                 "returns bigint\n"
     657             :                 "begin\n"
     658             :                 "  if tpe in ('varchar', 'char', 'clob', 'json', 'url')\n"
     659             :                 "          then return 8192 + ((avgwidth + 8) * distincts);\n"
     660             :                 "  end if;\n"
     661             :                 "  if tpe in ('blob', 'geometry', 'geometrya')\n"
     662             :                 "          then return 32 + (avgwidth * count);\n"
     663             :                 "  end if;\n"
     664             :                 "  return 0;\n"
     665             :                 "end;\n"
     666             :                 "create function sys.hashsize(b boolean, count bigint)\n"
     667             :                 "returns bigint\n"
     668             :                 "begin\n"
     669             :                 "  if b = true\n"
     670             :                 "          then return 8 * count;\n"
     671             :                 "  end if;\n"
     672             :                 "  return 0;\n"
     673             :                 "end;\n"
     674             :                 "create function sys.imprintsize(tpe varchar(1024), count bigint)\n"
     675             :                 "returns bigint\n"
     676             :                 "begin\n"
     677             :                 "  if tpe in ('tinyint', 'boolean')\n"
     678             :                 "          then return cast(0.2 * count as bigint);\n"
     679             :                 "  end if;\n"
     680             :                 "  if tpe = 'smallint'\n"
     681             :                 "          then return cast(0.4 * count as bigint);\n"
     682             :                 "  end if;\n"
     683             :                 "  if tpe in ('int', 'real', 'date', 'time', 'timetz', 'sec_interval',%s 'month_interval')\n"
     684             :                 "          then return cast(0.8 * count as bigint);\n"
     685             :                 "  end if;\n"
     686             :                 "  if tpe in ('bigint', 'double', 'timestamp', 'timestamptz', 'inet', 'oid')\n"
     687             :                 "          then return cast(1.6 * count as bigint);\n"
     688             :                 "  end if;\n"
     689             :                 "  if tpe in ('hugeint', 'decimal', 'uuid', 'mbr')\n"
     690             :                 "          then return cast(3.2 * count as bigint);\n"
     691             :                 "  end if;\n"
     692             :                 "  return 0;\n"
     693             :                 "end;\n"
     694             :                 "create view sys.storagemodel as\n"
     695             :                 "select \"schema\", \"table\", \"column\", \"type\", \"count\",\n"
     696             :                 "  sys.columnsize(\"type\", \"count\") as columnsize,\n"
     697             :                 "  sys.heapsize(\"type\", \"count\", \"distinct\", \"atomwidth\") as heapsize,\n"
     698             :                 "  sys.hashsize(\"reference\", \"count\") as hashsize,\n"
     699             :                 "  case when isacolumn then sys.imprintsize(\"type\", \"count\") else 0 end as imprintsize,\n"
     700             :                 "  case when (isacolumn and not sorted) then cast(8 * \"count\" as bigint) else 0 end as orderidxsize,\n"
     701             :                 "  sorted, \"unique\", isacolumn\n"
     702             :                 " from sys.storagemodelinput\n"
     703             :                 "order by \"schema\", \"table\", \"column\";\n"
     704             :                 "create view sys.tablestoragemodel as\n"
     705             :                 "select \"schema\", \"table\",\n"
     706             :                 "  max(\"count\") as \"rowcount\",\n"
     707             :                 "  count(*) as \"storages\",\n"
     708             :                 "  sum(sys.columnsize(\"type\", \"count\")) as columnsize,\n"
     709             :                 "  sum(sys.heapsize(\"type\", \"count\", \"distinct\", \"atomwidth\")) as heapsize,\n"
     710             :                 "  sum(sys.hashsize(\"reference\", \"count\")) as hashsize,\n"
     711             :                 "  sum(case when isacolumn then sys.imprintsize(\"type\", \"count\") else 0 end) as imprintsize,\n"
     712             :                 "  sum(case when (isacolumn and not sorted) then cast(8 * \"count\" as bigint) else 0 end) as orderidxsize\n"
     713             :                 " from sys.storagemodelinput\n"
     714             :                 "group by \"schema\", \"table\"\n"
     715             :                 "order by \"schema\", \"table\";\n", day_interval_str, day_interval_str);
     716           0 :         assert(pos < bufsize);
     717             : 
     718           0 :         pos += snprintf(buf + pos, bufsize - pos,
     719             :                 "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
     720             :                 " and name in ('storage', 'tablestorage', 'schemastorage', 'storagemodelinput', 'storagemodel', 'tablestoragemodel');\n");
     721           0 :         pos += snprintf(buf + pos, bufsize - pos,
     722             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     723             :                 " and name in ('storage') and type = %d;\n", (int) F_UNION);
     724           0 :         pos += snprintf(buf + pos, bufsize - pos,
     725             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     726             :                 " and name in ('storagemodelinit') and type = %d;\n", (int) F_PROC);
     727           0 :         pos += snprintf(buf + pos, bufsize - pos,
     728             :                 "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
     729             :                 " and name in ('columnsize', 'heapsize', 'hashsize', 'imprintsize') and type = %d;\n", (int) F_FUNC);
     730             : 
     731           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
     732           0 :         assert(pos < bufsize);
     733             : 
     734           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     735           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     736           0 :         GDKfree(buf);
     737           0 :         return err;             /* usually MAL_SUCCEED */
     738             : }
     739             : 
     740             : static str
     741          69 : sql_update_apr2019_sp1(Client c)
     742             : {
     743             :         char *err, *qry = "select c.id from sys.dependency_types dt, sys._columns c, sys.keys k, sys.objects o "
     744             :                 "where k.id = o.id and o.name = c.name and c.table_id = k.table_id and dt.dependency_type_name = 'KEY' and k.type = 1 "
     745             :                 "and not exists (select d.id from sys.dependencies d where d.id = c.id and d.depend_id = k.id and d.depend_type = dt.dependency_type_id);";
     746          69 :         res_table *output = NULL;
     747             : 
     748             :         /* Determine if missing dependency table entry for unique keys
     749             :          * is required */
     750          69 :         err = SQLstatementIntern(c, qry, "update", true, false, &output);
     751          69 :         if (err == NULL) {
     752          69 :                 BAT *b = BATdescriptor(output->cols[0].b);
     753          69 :                 if (b) {
     754          69 :                         if (BATcount(b) > 0) {
     755             :                                 /* required update for changeset 23e1231ada99 */
     756             :                                 qry = "insert into sys.dependencies (select c.id as id, k.id as depend_id, dt.dependency_type_id as depend_type from sys.dependency_types dt, sys._columns c, sys.keys k, sys.objects o where k.id = o.id and o.name = c.name and c.table_id = k.table_id and dt.dependency_type_name = 'KEY' and k.type = 1 and not exists (select d.id from sys.dependencies d where d.id = c.id and d.depend_id = k.id and d.depend_type = dt.dependency_type_id));\n";
     757           0 :                                 printf("Running database upgrade commands:\n%s\n", qry);
     758           0 :                                 err = SQLstatementIntern(c, qry, "update", true, false, NULL);
     759             :                         }
     760          69 :                         BBPunfix(b->batCacheid);
     761             :                 }
     762          69 :                 res_tables_destroy(output);
     763             :         }
     764             : 
     765          69 :         return err;             /* usually MAL_SUCCEED */
     766             : }
     767             : 
     768             : static str
     769           0 : sql_update_apr2019_sp2(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
     770             : {
     771             :         size_t bufsize = 1000, pos = 0;
     772           0 :         char *buf = GDKmalloc(bufsize), *err;
     773             : 
     774           0 :         if (buf == NULL)
     775           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     776             : 
     777           0 :         if (!*systabfixed) {
     778           0 :                 sql_fix_system_tables(c, sql, prev_schema);
     779           0 :                 *systabfixed = true;
     780             :         }
     781             : 
     782           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema sys;\n");
     783             : 
     784             :         /* 11_times.sql */
     785           0 :         pos += snprintf(buf + pos, bufsize - pos,
     786             :                         "drop procedure sys.times();\n");
     787             : 
     788           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
     789             : 
     790           0 :         assert(pos < bufsize);
     791           0 :         printf("Running database upgrade commands:\n%s\n", buf);
     792           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     793           0 :         GDKfree(buf);
     794           0 :         return err;             /* usually MAL_SUCCEED */
     795             : }
     796             : 
     797             : #define FLUSH_INSERTS_IF_BUFFERFILLED /* Each new value should add about 20 bytes to the buffer, "flush" when is 200 bytes from being full */ \
     798             :         if (pos > 7900) { \
     799             :                 pos += snprintf(buf + pos, bufsize - pos, \
     800             :                                                 ") as t1(c1,c2,c3) where t1.c1 not in (select \"id\" from sys.dependencies where depend_id = t1.c2);\n"); \
     801             :                 assert(pos < bufsize); \
     802             :                 printf("Running database upgrade commands:\n%s\n", buf); \
     803             :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL); \
     804             :                 if (err) \
     805             :                         goto bailout; \
     806             :                 pos = 0; \
     807             :                 pos += snprintf(buf + pos, bufsize - pos, "insert into sys.dependencies select c1, c2, c3 from (values"); \
     808             :                 ppos = pos; \
     809             :                 first = true; \
     810             :         }
     811             : 
     812             : static str
     813           0 : sql_update_nov2019_missing_dependencies(Client c, mvc *sql)
     814             : {
     815             :         size_t bufsize = 8192, pos = 0, ppos;
     816           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     817           0 :         sql_allocator *old_sa = sql->sa;
     818             :         bool first = true;
     819           0 :         sql_trans *tr = sql->session->tr;
     820             :         struct os_iter si;
     821             : 
     822           0 :         if (buf == NULL)
     823           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     824             : 
     825           0 :         if (!(sql->sa = sa_create(sql->pa))) {
     826           0 :                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     827           0 :                 goto bailout;
     828             :         }
     829             : 
     830           0 :         pos += snprintf(buf + pos, bufsize - pos, "insert into sys.dependencies select c1, c2, c3 from (values");
     831             :         ppos = pos; /* later check if found updatable database objects */
     832             : 
     833           0 :         os_iterator(&si, sql->session->tr->cat->schemas, sql->session->tr, NULL);
     834           0 :         for (sql_base *b = oi_next(&si); b; oi_next(&si)) {
     835             :                 sql_schema *s = (sql_schema*)b;
     836             : 
     837             :                 struct os_iter oi;
     838           0 :                 os_iterator(&oi, s->funcs, sql->session->tr, NULL);
     839           0 :                 for (sql_base *b = oi_next(&oi); b; oi_next(&oi)) {
     840             :                         sql_func *f = (sql_func*)b;
     841             : 
     842           0 :                         if (f->query && f->lang == FUNC_LANG_SQL) {
     843             :                                 char *relt;
     844             :                                 sql_rel *r = NULL;
     845             : 
     846           0 :                                 if (!(relt = sa_strdup(sql->sa, f->query))) {
     847           0 :                                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     848           0 :                                         goto bailout;
     849             :                                 }
     850             : 
     851           0 :                                 r = rel_parse(sql, s, relt, m_deps);
     852           0 :                                 if (r)
     853           0 :                                         r = sql_processrelation(sql, r, 0, 0);
     854           0 :                                 if (r) {
     855           0 :                                         list *id_l = rel_dependencies(sql, r);
     856             : 
     857           0 :                                         for (node *o = id_l->h ; o ; o = o->next) {
     858           0 :                                                 sqlid next = ((sql_base*) o->data)->id;
     859           0 :                                                 if (next != f->base.id) {
     860           0 :                                                         pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",", next,
     861           0 :                                                                                         f->base.id, (int)(!IS_PROC(f) ? FUNC_DEPENDENCY : PROC_DEPENDENCY));
     862             :                                                         first = false;
     863           0 :                                                         FLUSH_INSERTS_IF_BUFFERFILLED
     864             :                                                 }
     865             :                                         }
     866           0 :                                 } else if (sql->session->status == -1) {
     867           0 :                                         sql->session->status = 0;
     868           0 :                                         sql->errstr[0] = 0;
     869             :                                 }
     870             :                         }
     871             :                 }
     872           0 :                 if (s->tables) {
     873             :                         struct os_iter oi;
     874           0 :                         os_iterator(&oi, s->tables, tr, NULL);
     875           0 :                         for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
     876             :                                 sql_table *t = (sql_table*) b;
     877             : 
     878           0 :                                 if (t->query && isView(t)) {
     879             :                                         char *relt;
     880             :                                         sql_rel *r = NULL;
     881             : 
     882           0 :                                         if (!(relt = sa_strdup(sql->sa, t->query))) {
     883           0 :                                                 err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     884           0 :                                                 goto bailout;
     885             :                                         }
     886             : 
     887           0 :                                         r = rel_parse(sql, s, relt, m_deps);
     888           0 :                                         if (r)
     889           0 :                                                 r = sql_processrelation(sql, r, 0, 0);
     890           0 :                                         if (r) {
     891           0 :                                                 list *id_l = rel_dependencies(sql, r);
     892             : 
     893           0 :                                                 for (node *o = id_l->h ; o ; o = o->next) {
     894           0 :                                                         sqlid next = ((sql_base*) o->data)->id;
     895           0 :                                                         if (next != t->base.id) {
     896           0 :                                                                 pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",",
     897             :                                                                                                 next, t->base.id, (int) VIEW_DEPENDENCY);
     898             :                                                                 first = false;
     899           0 :                                                                 FLUSH_INSERTS_IF_BUFFERFILLED
     900             :                                                         }
     901             :                                                 }
     902             :                                         }
     903             :                                 }
     904           0 :                                 if (t->triggers)
     905           0 :                                         for (node *mm = ol_first_node(t->triggers); mm; mm = mm->next) {
     906           0 :                                                 sql_trigger *tr = (sql_trigger*) mm->data;
     907             :                                                 char *relt;
     908             :                                                 sql_rel *r = NULL;
     909             : 
     910           0 :                                                 if (!(relt = sa_strdup(sql->sa, tr->statement))) {
     911           0 :                                                         err = createException(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     912           0 :                                                         goto bailout;
     913             :                                                 }
     914             : 
     915           0 :                                                 r = rel_parse(sql, s, relt, m_deps);
     916           0 :                                                 if (r)
     917           0 :                                                         r = sql_processrelation(sql, r, 0, 0);
     918           0 :                                                 if (r) {
     919           0 :                                                         list *id_l = rel_dependencies(sql, r);
     920             : 
     921           0 :                                                         for (node *o = id_l->h ; o ; o = o->next) {
     922           0 :                                                                 sqlid next = ((sql_base*) o->data)->id;
     923           0 :                                                                 if (next != tr->base.id) {
     924           0 :                                                                         pos += snprintf(buf + pos, bufsize - pos, "%s(%d,%d,%d)", first ? "" : ",",
     925             :                                                                                                         next, tr->base.id, (int) TRIGGER_DEPENDENCY);
     926             :                                                                         first = false;
     927           0 :                                                                         FLUSH_INSERTS_IF_BUFFERFILLED
     928             :                                                                 }
     929             :                                                         }
     930             :                                                 }
     931             :                                         }
     932             :                         }
     933             :                 }
     934             :         }
     935             : 
     936           0 :         if (ppos != pos) { /* found updatable functions */
     937           0 :                 pos += snprintf(buf + pos, bufsize - pos,
     938             :                                                 ") as t1(c1,c2,c3) where t1.c1 not in (select \"id\" from sys.dependencies where depend_id = t1.c2);\n");
     939             : 
     940           0 :                 assert(pos < bufsize);
     941           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
     942           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
     943             :         }
     944             : 
     945           0 : bailout:
     946           0 :         if (sql->sa)
     947           0 :                 sa_destroy(sql->sa);
     948           0 :         sql->sa = old_sa;
     949           0 :         GDKfree(buf);
     950           0 :         return err;
     951             : }
     952             : 
     953             : static str
     954           0 : sql_update_nov2019(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
     955             : {
     956             :         size_t bufsize = 16384, pos = 0;
     957           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
     958             :         res_table *output;
     959             :         BAT *b;
     960             : 
     961           0 :         if (buf == NULL)
     962           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     963             : 
     964           0 :         pos += snprintf(buf + pos, bufsize - pos,
     965             :                         "select id from sys.args where func_id in (select id from sys.functions where schema_id = (select id from sys.schemas where name = 'sys') and name = 'second' and func = 'sql_seconds') and number = 0 and type_scale = 3;\n");
     966           0 :         err = SQLstatementIntern(c, buf, "update", 1, 0, &output);
     967           0 :         if (err) {
     968           0 :                 GDKfree(buf);
     969           0 :                 return err;
     970             :         }
     971           0 :         b = BATdescriptor(output->cols[0].b);
     972           0 :         if (b) {
     973           0 :                 if (BATcount(b) > 0 && !*systabfixed) {
     974           0 :                         err = sql_fix_system_tables(c, sql, prev_schema);
     975           0 :                         *systabfixed = true;
     976             :                 }
     977           0 :                 BBPunfix(b->batCacheid);
     978             :         }
     979           0 :         res_table_destroy(output);
     980           0 :         if (err) {
     981           0 :                 GDKfree(buf);
     982           0 :                 return err;
     983             :         }
     984             : 
     985             :         pos = 0;
     986           0 :         pos += snprintf(buf + pos, bufsize - pos,
     987             :                         "set schema \"sys\";\n"
     988             :                         "create function sys.deltas (\"schema\" string)"
     989             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     990             :                         " external name \"sql\".\"deltas\";\n"
     991             :                         "create function sys.deltas (\"schema\" string, \"table\" string)"
     992             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     993             :                         " external name \"sql\".\"deltas\";\n"
     994             :                         "create function sys.deltas (\"schema\" string, \"table\" string, \"column\" string)"
     995             :                         " returns table (\"id\" int, \"cleared\" boolean, \"immutable\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)"
     996             :                         " external name \"sql\".\"deltas\";\n"
     997             :                         "create aggregate median_avg(val TINYINT) returns DOUBLE\n"
     998             :                         " external name \"aggr\".\"median_avg\";\n"
     999             :                         "GRANT EXECUTE ON AGGREGATE median_avg(TINYINT) TO PUBLIC;\n"
    1000             :                         "create aggregate median_avg(val SMALLINT) returns DOUBLE\n"
    1001             :                         " external name \"aggr\".\"median_avg\";\n"
    1002             :                         "GRANT EXECUTE ON AGGREGATE median_avg(SMALLINT) TO PUBLIC;\n"
    1003             :                         "create aggregate median_avg(val INTEGER) returns DOUBLE\n"
    1004             :                         " external name \"aggr\".\"median_avg\";\n"
    1005             :                         "GRANT EXECUTE ON AGGREGATE median_avg(INTEGER) TO PUBLIC;\n"
    1006             :                         "create aggregate median_avg(val BIGINT) returns DOUBLE\n"
    1007             :                         " external name \"aggr\".\"median_avg\";\n"
    1008             :                         "GRANT EXECUTE ON AGGREGATE median_avg(BIGINT) TO PUBLIC;\n"
    1009             :                         "create aggregate median_avg(val DECIMAL) returns DOUBLE\n"
    1010             :                         " external name \"aggr\".\"median_avg\";\n"
    1011             :                         "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL) TO PUBLIC;\n"
    1012             :                         "create aggregate median_avg(val REAL) returns DOUBLE\n"
    1013             :                         " external name \"aggr\".\"median_avg\";\n"
    1014             :                         "GRANT EXECUTE ON AGGREGATE median_avg(REAL) TO PUBLIC;\n"
    1015             :                         "create aggregate median_avg(val DOUBLE) returns DOUBLE\n"
    1016             :                         " external name \"aggr\".\"median_avg\";\n"
    1017             :                         "GRANT EXECUTE ON AGGREGATE median_avg(DOUBLE) TO PUBLIC;\n"
    1018             :                         "create aggregate quantile_avg(val TINYINT, q DOUBLE) returns DOUBLE\n"
    1019             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1020             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(TINYINT, DOUBLE) TO PUBLIC;\n"
    1021             :                         "create aggregate quantile_avg(val SMALLINT, q DOUBLE) returns DOUBLE\n"
    1022             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1023             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(SMALLINT, DOUBLE) TO PUBLIC;\n"
    1024             :                         "create aggregate quantile_avg(val INTEGER, q DOUBLE) returns DOUBLE\n"
    1025             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1026             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(INTEGER, DOUBLE) TO PUBLIC;\n"
    1027             :                         "create aggregate quantile_avg(val BIGINT, q DOUBLE) returns DOUBLE\n"
    1028             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1029             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(BIGINT, DOUBLE) TO PUBLIC;\n"
    1030             :                         "create aggregate quantile_avg(val DECIMAL, q DOUBLE) returns DOUBLE\n"
    1031             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1032             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL, DOUBLE) TO PUBLIC;\n"
    1033             :                         "create aggregate quantile_avg(val REAL, q DOUBLE) returns DOUBLE\n"
    1034             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1035             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(REAL, DOUBLE) TO PUBLIC;\n"
    1036             :                         "create aggregate quantile_avg(val DOUBLE, q DOUBLE) returns DOUBLE\n"
    1037             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1038             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(DOUBLE, DOUBLE) TO PUBLIC;\n");
    1039             : #ifdef HAVE_HGE
    1040           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1041             :                                 "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
    1042             :                                 " external name \"aggr\".\"median_avg\";\n"
    1043             :                                 "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
    1044             :                                 "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
    1045             :                                 " external name \"aggr\".\"quantile_avg\";\n"
    1046             :                                 "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n");
    1047             : #endif
    1048             :         /* 60/61_wlcr signatures migrations */
    1049           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1050             :                         "drop procedure master();\n"
    1051             :                         "drop procedure master(string);\n"
    1052             :                         "drop procedure stopmaster();\n"
    1053             :                         "drop procedure masterbeat(int);\n"
    1054             :                         "drop function masterClock();\n"
    1055             :                         "drop function masterTick();\n"
    1056             :                         "drop procedure replicate();\n"
    1057             :                         "drop procedure replicate(timestamp);\n"
    1058             :                         "drop procedure replicate(string);\n"
    1059             :                         "drop procedure replicate(string, timestamp);\n"
    1060             :                         "drop procedure replicate(string, tinyint);\n"
    1061             :                         "drop procedure replicate(string, smallint);\n"
    1062             :                         "drop procedure replicate(string, integer);\n"
    1063             :                         "drop procedure replicate(string, bigint);\n"
    1064             :                         "drop procedure replicabeat(integer);\n"
    1065             :                         "drop function replicaClock();\n"
    1066             :                         "drop function replicaTick();\n"
    1067             : 
    1068             :                         "create schema wlc;\n"
    1069             :                         "create procedure wlc.master()\n"
    1070             :                         "external name wlc.master;\n"
    1071             :                         "create procedure wlc.master(path string)\n"
    1072             :                         "external name wlc.master;\n"
    1073             :                         "create procedure wlc.stop()\n"
    1074             :                         "external name wlc.stop;\n"
    1075             :                         "create procedure wlc.flush()\n"
    1076             :                         "external name wlc.flush;\n"
    1077             :                         "create procedure wlc.beat( duration int)\n"
    1078             :                         "external name wlc.\"setbeat\";\n"
    1079             :                         "create function wlc.clock() returns string\n"
    1080             :                         "external name wlc.\"getclock\";\n"
    1081             :                         "create function wlc.tick() returns bigint\n"
    1082             :                         "external name wlc.\"gettick\";\n"
    1083             : 
    1084             :                         "create schema wlr;\n"
    1085             :                         "create procedure wlr.master(dbname string)\n"
    1086             :                         "external name wlr.master;\n"
    1087             :                         "create procedure wlr.stop()\n"
    1088             :                         "external name wlr.stop;\n"
    1089             :                         "create procedure wlr.accept()\n"
    1090             :                         "external name wlr.accept;\n"
    1091             :                         "create procedure wlr.replicate()\n"
    1092             :                         "external name wlr.replicate;\n"
    1093             :                         "create procedure wlr.replicate(pointintime timestamp)\n"
    1094             :                         "external name wlr.replicate;\n"
    1095             :                         "create procedure wlr.replicate(id tinyint)\n"
    1096             :                         "external name wlr.replicate;\n"
    1097             :                         "create procedure wlr.replicate(id smallint)\n"
    1098             :                         "external name wlr.replicate;\n"
    1099             :                         "create procedure wlr.replicate(id integer)\n"
    1100             :                         "external name wlr.replicate;\n"
    1101             :                         "create procedure wlr.replicate(id bigint)\n"
    1102             :                         "external name wlr.replicate;\n"
    1103             :                         "create procedure wlr.beat(duration integer)\n"
    1104             :                         "external name wlr.\"setbeat\";\n"
    1105             :                         "create function wlr.clock() returns string\n"
    1106             :                         "external name wlr.\"getclock\";\n"
    1107             :                         "create function wlr.tick() returns bigint\n"
    1108             :                         "external name wlr.\"gettick\";\n"
    1109             :                 );
    1110             : 
    1111           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1112             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1113             :                         " and name in ('deltas') and type = %d;\n", (int) F_UNION);
    1114           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1115             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1116             :                         " and name in ('median_avg', 'quantile_avg') and type = %d;\n", (int) F_AGGR);
    1117           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1118             :                         "update sys.schemas set system = true where name in ('wlc', 'wlr');\n");
    1119           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1120             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'wlc')"
    1121             :                         " and name in ('clock', 'tick') and type = %d;\n", (int) F_FUNC);
    1122           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1123             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'wlc')"
    1124             :                         " and name in ('master', 'stop', 'flush', 'beat') and type = %d;\n", (int) F_PROC);
    1125           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1126             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'wlr')"
    1127             :                         " and name in ('clock', 'tick') and type = %d;\n", (int) F_FUNC);
    1128           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1129             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'wlr')"
    1130             :                         " and name in ('master', 'stop', 'accept', 'replicate', 'beat') and type = %d;\n", (int) F_PROC);
    1131             : 
    1132             :         /* 39_analytics.sql */
    1133           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1134             :                         "create aggregate stddev_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1135             :                         "external name \"aggr\".\"stdev\";\n"
    1136             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1137             :                         "create aggregate stddev_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1138             :                         "external name \"aggr\".\"stdev\";\n"
    1139             :                         "GRANT EXECUTE ON AGGREGATE stddev_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1140             : 
    1141             :                         "create aggregate stddev_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1142             :                         "external name \"aggr\".\"stdevp\";\n"
    1143             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1144             :                         "create aggregate stddev_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1145             :                         "external name \"aggr\".\"stdevp\";\n"
    1146             :                         "GRANT EXECUTE ON AGGREGATE stddev_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1147             : 
    1148             :                         "create aggregate var_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1149             :                         "external name \"aggr\".\"variance\";\n"
    1150             :                         "GRANT EXECUTE ON AGGREGATE var_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1151             :                         "create aggregate var_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1152             :                         "external name \"aggr\".\"variance\";\n"
    1153             :                         "GRANT EXECUTE ON AGGREGATE var_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1154             : 
    1155             :                         "create aggregate var_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1156             :                         "external name \"aggr\".\"variancep\";\n"
    1157             :                         "GRANT EXECUTE ON AGGREGATE var_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1158             :                         "create aggregate var_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1159             :                         "external name \"aggr\".\"variancep\";\n"
    1160             :                         "GRANT EXECUTE ON AGGREGATE var_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1161             : 
    1162             :                         "create aggregate median(val INTERVAL SECOND) returns INTERVAL SECOND\n"
    1163             :                         "external name \"aggr\".\"median\";\n"
    1164             :                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL SECOND) TO PUBLIC;\n"
    1165             :                         "create aggregate median(val INTERVAL MONTH) returns INTERVAL MONTH\n"
    1166             :                         "external name \"aggr\".\"median\";\n"
    1167             :                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL MONTH) TO PUBLIC;\n"
    1168             : 
    1169             :                         "create aggregate quantile(val INTERVAL SECOND, q DOUBLE) returns INTERVAL SECOND\n"
    1170             :                         "external name \"aggr\".\"quantile\";\n"
    1171             :                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL SECOND, DOUBLE) TO PUBLIC;\n"
    1172             :                         "create aggregate quantile(val INTERVAL MONTH, q DOUBLE) returns INTERVAL MONTH\n"
    1173             :                         "external name \"aggr\".\"quantile\";\n"
    1174             :                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL MONTH, DOUBLE) TO PUBLIC;\n"
    1175             :                 );
    1176             : 
    1177           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1178             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1179             :                         " and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'var_pop', 'median', 'quantile') and type = %d;\n", (int) F_AGGR);
    1180             : 
    1181             :         /* The MAL implementation of functions json.text(string) and json.text(int) do not exist */
    1182           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1183             :                         "drop function json.text(string);\n"
    1184             :                         "drop function json.text(int);\n");
    1185             : 
    1186             :         /* The first argument to copyfrom is a PTR type */
    1187           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1188             :                         "update sys.args set type = 'ptr' where"
    1189             :                         " 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);
    1190             : 
    1191           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    1192           0 :         assert(pos < bufsize);
    1193             : 
    1194           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1195           0 :         err = SQLstatementIntern(c, buf, "update", 1, 0, NULL);
    1196           0 :         GDKfree(buf);
    1197           0 :         return err;             /* usually MAL_SUCCEED */
    1198             : }
    1199             : 
    1200             : #ifdef HAVE_HGE
    1201             : static str
    1202           0 : sql_update_nov2019_sp1_hugeint(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    1203             : {
    1204             :         size_t bufsize = 1024, pos = 0;
    1205             :         char *buf, *err;
    1206             : 
    1207           0 :         if (!*systabfixed &&
    1208           0 :             (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL)
    1209             :                 return err;
    1210           0 :         *systabfixed = true;
    1211             : 
    1212           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1213           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1214             : 
    1215           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
    1216             : 
    1217             :         /* 39_analytics_hge.sql */
    1218           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1219             :                         "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
    1220             :                         " external name \"aggr\".\"median_avg\";\n"
    1221             :                         "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
    1222             :                         "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
    1223             :                         " external name \"aggr\".\"quantile_avg\";\n"
    1224             :                         "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n");
    1225             : 
    1226           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1227             :                         "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);
    1228             : 
    1229           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    1230           0 :         assert(pos < bufsize);
    1231             : 
    1232           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1233           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1234           0 :         GDKfree(buf);
    1235           0 :         return err;             /* usually MAL_SUCCEED */
    1236             : }
    1237             : #endif
    1238             : 
    1239             : static str
    1240           0 : sql_update_jun2020(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    1241             : {
    1242             :         sql_table *t;
    1243             :         size_t bufsize = 32768, pos = 0;
    1244           0 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    1245           0 :         sql_schema *sys = mvc_bind_schema(sql, "sys");
    1246             : 
    1247           0 :         if (!*systabfixed &&
    1248           0 :             (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL)
    1249             :                 return err;
    1250           0 :         *systabfixed = true;
    1251             : 
    1252           0 :         if (buf == NULL)
    1253           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1254             : 
    1255           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1256             :                         "set schema \"sys\";\n");
    1257             : 
    1258             :         /* convert old PYTHON2 and PYTHON2_MAP to PYTHON and PYTHON_MAP
    1259             :          * see also function load_func() in store.c */
    1260           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1261             :                         "update sys.functions set language = language - 2 where language in (8, 9);\n");
    1262             : 
    1263           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1264             :                         "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");
    1265           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1266             :                         "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");
    1267             : 
    1268             :         /* 12_url */
    1269           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1270             :                         "drop function isaURL(url);\n"
    1271             :                         "CREATE function isaURL(theUrl string) RETURNS BOOL\n"
    1272             :                         " EXTERNAL NAME url.\"isaURL\";\n"
    1273             :                         "GRANT EXECUTE ON FUNCTION isaURL(string) TO PUBLIC;\n"
    1274             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1275             :                         " and name = 'isaurl' and type = %d;\n", (int) F_FUNC);
    1276             : 
    1277             :         /* 13_date.sql */
    1278           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1279             :                         "drop function str_to_time(string, string);\n"
    1280             :                         "drop function time_to_str(time, string);\n"
    1281             :                         "drop function str_to_timestamp(string, string);\n"
    1282             :                         "drop function timestamp_to_str(timestamp, string);\n"
    1283             :                         "create function str_to_time(s string, format string) returns time with time zone\n"
    1284             :                         " external name mtime.\"str_to_time\";\n"
    1285             :                         "create function time_to_str(d time with time zone, format string) returns string\n"
    1286             :                         " external name mtime.\"time_to_str\";\n"
    1287             :                         "create function str_to_timestamp(s string, format string) returns timestamp with time zone\n"
    1288             :                         " external name mtime.\"str_to_timestamp\";\n"
    1289             :                         "create function timestamp_to_str(d timestamp with time zone, format string) returns string\n"
    1290             :                         " external name mtime.\"timestamp_to_str\";\n"
    1291             :                         "grant execute on function str_to_time to public;\n"
    1292             :                         "grant execute on function time_to_str to public;\n"
    1293             :                         "grant execute on function str_to_timestamp to public;\n"
    1294             :                         "grant execute on function timestamp_to_str to public;\n"
    1295             :                         "update sys.functions set system = true where system <> true and name in"
    1296             :                         " ('str_to_time', 'str_to_timestamp', 'time_to_str', 'timestamp_to_str')"
    1297             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_FUNC);
    1298             : 
    1299             :         /* 16_tracelog */
    1300           0 :         t = mvc_bind_table(sql, sys, "tracelog");
    1301           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1302           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1303             :                         "drop view sys.tracelog;\n"
    1304             :                         "drop function sys.tracelog();\n"
    1305             :                         "create function sys.tracelog()\n"
    1306             :                         " returns table (\n"
    1307             :                         "  ticks bigint, -- time in microseconds\n"
    1308             :                         "  stmt string  -- actual statement executed\n"
    1309             :                         " )\n"
    1310             :                         " external name sql.dump_trace;\n"
    1311             :                         "create view sys.tracelog as select * from sys.tracelog();\n"
    1312             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1313             :                         " and name = 'tracelog' and type = %d;\n", (int) F_UNION);
    1314           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1315             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1316             :                         " and name = 'tracelog';\n");
    1317             : 
    1318             :         /* 17_temporal.sql */
    1319           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1320             :                         "drop function sys.epoch(bigint);\n"
    1321             :                         "drop function sys.epoch(int);\n"
    1322             :                         "drop function sys.epoch(timestamp);\n"
    1323             :                         "drop function sys.epoch(timestamp with time zone);\n"
    1324             :                         "create function sys.epoch(sec BIGINT) returns TIMESTAMP WITH TIME ZONE\n"
    1325             :                         " external name mtime.epoch;\n"
    1326             :                         "create function sys.epoch(sec INT) returns TIMESTAMP WITH TIME ZONE\n"
    1327             :                         " external name mtime.epoch;\n"
    1328             :                         "create function sys.epoch(ts TIMESTAMP WITH TIME ZONE) returns INT\n"
    1329             :                         " external name mtime.epoch;\n"
    1330             :                         "create function sys.date_trunc(txt string, t timestamp with time zone)\n"
    1331             :                         "returns timestamp with time zone\n"
    1332             :                         "external name sql.date_trunc;\n"
    1333             :                         "grant execute on function sys.date_trunc(string, timestamp with time zone) to public;\n"
    1334             :                         "grant execute on function sys.epoch (BIGINT) to public;\n"
    1335             :                         "grant execute on function sys.epoch (INT) to public;\n"
    1336             :                         "grant execute on function sys.epoch (TIMESTAMP WITH TIME ZONE) to public;\n"
    1337             :                         "update sys.functions set system = true where system <> true and name in"
    1338             :                         " ('epoch', 'date_trunc')"
    1339             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n", (int) F_FUNC);
    1340             : 
    1341             :         /* 22_clients */
    1342           0 :         t = mvc_bind_table(sql, sys, "sessions");
    1343           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1344             : 
    1345           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1346             :                         "drop view sys.sessions;\n"
    1347             :                         "drop function sys.sessions;\n"
    1348             :                         "create function sys.sessions()\n"
    1349             :                         "returns table(\n"
    1350             :                                 "\"sessionid\" int,\n"
    1351             :                                 "\"username\" string,\n"
    1352             :                                 "\"login\" timestamp,\n"
    1353             :                                 "\"idle\" timestamp,\n"
    1354             :                                 "\"optimizer\" string,\n"
    1355             :                                 "\"sessiontimeout\" int,\n"
    1356             :                                 "\"querytimeout\" int,\n"
    1357             :                                 "\"workerlimit\" int,\n"
    1358             :                                 "\"memorylimit\" int)\n"
    1359             :                         " external name sql.sessions;\n"
    1360             :                         "create view sys.sessions as select * from sys.sessions();\n");
    1361             : 
    1362           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1363             :                         "grant execute on procedure sys.settimeout(bigint) to public;\n"
    1364             :                         "grant execute on procedure sys.settimeout(bigint,bigint) to public;\n"
    1365             :                         "grant execute on procedure sys.setsession(bigint) to public;\n");
    1366             : 
    1367           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1368             :                         "create procedure sys.setoptimizer(\"optimizer\" string)\n"
    1369             :                         " external name clients.setoptimizer;\n"
    1370             :                         "grant execute on procedure sys.setoptimizer(string) to public;\n"
    1371             :                         "create procedure sys.setquerytimeout(\"query\" int)\n"
    1372             :                         " external name clients.setquerytimeout;\n"
    1373             :                         "grant execute on procedure sys.setquerytimeout(int) to public;\n"
    1374             :                         "create procedure sys.setsessiontimeout(\"timeout\" int)\n"
    1375             :                         " external name clients.setsessiontimeout;\n"
    1376             :                         "grant execute on procedure sys.setsessiontimeout(int) to public;\n"
    1377             :                         "create procedure sys.setworkerlimit(\"limit\" int)\n"
    1378             :                         " external name clients.setworkerlimit;\n"
    1379             :                         "grant execute on procedure sys.setworkerlimit(int) to public;\n"
    1380             :                         "create procedure sys.setmemorylimit(\"limit\" int)\n"
    1381             :                         " external name clients.setmemorylimit;\n"
    1382             :                         "grant execute on procedure sys.setmemorylimit(int) to public;\n"
    1383             :                         "create procedure sys.setoptimizer(\"sessionid\" int, \"optimizer\" string)\n"
    1384             :                         " external name clients.setoptimizer;\n"
    1385             :                         "create procedure sys.setquerytimeout(\"sessionid\" int, \"query\" int)\n"
    1386             :                         " external name clients.setquerytimeout;\n"
    1387             :                         "create procedure sys.setsessiontimeout(\"sessionid\" int, \"query\" int)\n"
    1388             :                         " external name clients.setsessiontimeout;\n"
    1389             :                         "create procedure sys.setworkerlimit(\"sessionid\" int, \"limit\" int)\n"
    1390             :                         " external name clients.setworkerlimit;\n"
    1391             :                         "create procedure sys.setmemorylimit(\"sessionid\" int, \"limit\" int)\n"
    1392             :                         " external name clients.setmemorylimit;\n"
    1393             :                         "create procedure sys.stopsession(\"sessionid\" int)\n"
    1394             :                         " external name clients.stopsession;\n");
    1395             : 
    1396           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1397             :                         "create function sys.prepared_statements()\n"
    1398             :                         "returns table(\n"
    1399             :                         "\"sessionid\" int,\n"
    1400             :                         "\"username\" string,\n"
    1401             :                         "\"statementid\" int,\n"
    1402             :                         "\"statement\" string,\n"
    1403             :                         "\"created\" timestamp)\n"
    1404             :                         " external name sql.prepared_statements;\n"
    1405             :                         "grant execute on function sys.prepared_statements to public;\n"
    1406             :                         "create view sys.prepared_statements as select * from sys.prepared_statements();\n"
    1407             :                         "grant select on sys.prepared_statements to public;\n"
    1408             :                         "create function sys.prepared_statements_args()\n"
    1409             :                         "returns table(\n"
    1410             :                         "\"statementid\" int,\n"
    1411             :                         "\"type\" string,\n"
    1412             :                         "\"type_digits\" int,\n"
    1413             :                         "\"type_scale\" int,\n"
    1414             :                         "\"inout\" tinyint,\n"
    1415             :                         "\"number\" int,\n"
    1416             :                         "\"schema\" string,\n"
    1417             :                         "\"table\" string,\n"
    1418             :                         "\"column\" string)\n"
    1419             :                         " external name sql.prepared_statements_args;\n"
    1420             :                         "grant execute on function sys.prepared_statements_args to public;\n"
    1421             :                         "create view sys.prepared_statements_args as select * from sys.prepared_statements_args();\n"
    1422             :                         "grant select on sys.prepared_statements_args to public;\n");
    1423             : 
    1424           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1425             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1426             :                         " and name in ('sessions', 'prepared_statements', 'prepared_statements_args') and type = %d;\n", (int) F_UNION);
    1427           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1428             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1429             :                         " and name in ('sessions', 'prepared_statements', 'prepared_statements_args');\n");
    1430           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1431             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1432             :                         " and name in ('setoptimizer', 'setquerytimeout', 'setsessiontimeout', 'setworkerlimit', 'setmemorylimit', 'setoptimizer', 'stopsession') and type = %d;\n", (int) F_PROC);
    1433             : 
    1434             :         /* 25_debug */
    1435           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1436             :                         "create procedure sys.suspend_log_flushing()\n"
    1437             :                         " external name sql.suspend_log_flushing;\n"
    1438             :                         "create procedure sys.resume_log_flushing()\n"
    1439             :                         " external name sql.resume_log_flushing;\n"
    1440             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1441             :                         " and name in ('suspend_log_flushing', 'resume_log_flushing') and type = %d;\n", (int) F_PROC);
    1442             : 
    1443           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1444             :                         "create function sys.debug(flag string) returns integer\n"
    1445             :                         " external name mdb.\"setDebug\";\n"
    1446             :                         "create function sys.debugflags()\n"
    1447             :                         " returns table(flag string, val bool)\n"
    1448             :                         " external name mdb.\"getDebugFlags\";\n");
    1449           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1450             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1451             :                         " and name in ('debug') and type = %d;\n"
    1452             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1453             :                         " and name in ('debugflags') and type = %d;\n",
    1454             :                         (int) F_FUNC, (int) F_UNION);
    1455             : 
    1456             :         /* 26_sysmon */
    1457           0 :         t = mvc_bind_table(sql, sys, "queue");
    1458           0 :         t->system = 0; /* make it non-system else the drop view will fail */
    1459             : 
    1460           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1461             :                         "drop view sys.queue;\n"
    1462             :                         "drop function sys.queue;\n"
    1463             :                         "create function sys.queue()\n"
    1464             :                         "returns table(\n"
    1465             :                         "\"tag\" bigint,\n"
    1466             :                         "\"sessionid\" int,\n"
    1467             :                         "\"username\" string,\n"
    1468             :                         "\"started\" timestamp,\n"
    1469             :                         "\"status\" string,\n"
    1470             :                         "\"query\" string,\n"
    1471             :                         "\"progress\" int,\n"
    1472             :                         "\"maxworkers\" int,\n"
    1473             :                         "\"footprint\" int)\n"
    1474             :                         " external name sysmon.queue;\n"
    1475             :                         "grant execute on function sys.queue to public;\n"
    1476             :                         "create view sys.queue as select * from sys.queue();\n"
    1477             :                         "grant select on sys.queue to public;\n"
    1478             : 
    1479             :                         "drop procedure sys.pause(int);\n"
    1480             :                         "drop procedure sys.resume(int);\n"
    1481             :                         "drop procedure sys.stop(int);\n"
    1482             : 
    1483             :                         "grant execute on procedure sys.pause(bigint) to public;\n"
    1484             :                         "grant execute on procedure sys.resume(bigint) to public;\n"
    1485             :                         "grant execute on procedure sys.stop(bigint) to public;\n");
    1486             : 
    1487           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1488             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1489             :                         " and name = 'queue' and type = %d;\n", (int) F_UNION);
    1490           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1491             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    1492             :                         " and name = 'queue';\n");
    1493             : 
    1494             :         /* 39_analytics.sql */
    1495           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1496             :                         "create window stddev_samp(val TINYINT) returns DOUBLE\n"
    1497             :                         " external name \"sql\".\"stdev\";\n"
    1498             :                         "GRANT EXECUTE ON WINDOW stddev_samp(TINYINT) TO PUBLIC;\n"
    1499             :                         "create window stddev_samp(val SMALLINT) returns DOUBLE"
    1500             :                         " external name \"sql\".\"stdev\";\n"
    1501             :                         "GRANT EXECUTE ON WINDOW stddev_samp(SMALLINT) TO PUBLIC;\n"
    1502             :                         "create window stddev_samp(val INTEGER) returns DOUBLE\n"
    1503             :                         " external name \"sql\".\"stdev\";\n"
    1504             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTEGER) TO PUBLIC;\n"
    1505             :                         "create window stddev_samp(val BIGINT) returns DOUBLE\n"
    1506             :                         " external name \"sql\".\"stdev\";\n"
    1507             :                         "GRANT EXECUTE ON WINDOW stddev_samp(BIGINT) TO PUBLIC;\n"
    1508             :                         "create window stddev_samp(val REAL) returns DOUBLE\n"
    1509             :                         " external name \"sql\".\"stdev\";\n"
    1510             :                         "GRANT EXECUTE ON WINDOW stddev_samp(REAL) TO PUBLIC;\n"
    1511             :                         "create window stddev_samp(val DOUBLE) returns DOUBLE\n"
    1512             :                         " external name \"sql\".\"stdev\";\n"
    1513             :                         "GRANT EXECUTE ON WINDOW stddev_samp(DOUBLE) TO PUBLIC;\n"
    1514             :                         "create window stddev_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1515             :                         " external name \"sql\".\"stdev\";\n"
    1516             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1517             :                         "create window stddev_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1518             :                         " external name \"sql\".\"stdev\";\n"
    1519             :                         "GRANT EXECUTE ON WINDOW stddev_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1520             :                         "create window stddev_pop(val TINYINT) returns DOUBLE\n"
    1521             :                         " external name \"sql\".\"stdevp\";\n"
    1522             :                         "GRANT EXECUTE ON WINDOW stddev_pop(TINYINT) TO PUBLIC;\n"
    1523             :                         "create window stddev_pop(val SMALLINT) returns DOUBLE\n"
    1524             :                         " external name \"sql\".\"stdevp\";\n"
    1525             :                         "GRANT EXECUTE ON WINDOW stddev_pop(SMALLINT) TO PUBLIC;\n"
    1526             :                         "create window stddev_pop(val INTEGER) returns DOUBLE\n"
    1527             :                         " external name \"sql\".\"stdevp\";\n"
    1528             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTEGER) TO PUBLIC;\n"
    1529             :                         "create window stddev_pop(val BIGINT) returns DOUBLE\n"
    1530             :                         " external name \"sql\".\"stdevp\";\n"
    1531             :                         "GRANT EXECUTE ON WINDOW stddev_pop(BIGINT) TO PUBLIC;\n"
    1532             :                         "create window stddev_pop(val REAL) returns DOUBLE\n"
    1533             :                         " external name \"sql\".\"stdevp\";\n"
    1534             :                         "GRANT EXECUTE ON WINDOW stddev_pop(REAL) TO PUBLIC;\n"
    1535             :                         "create window stddev_pop(val DOUBLE) returns DOUBLE\n"
    1536             :                         " external name \"sql\".\"stdevp\";\n"
    1537             :                         "GRANT EXECUTE ON WINDOW stddev_pop(DOUBLE) TO PUBLIC;\n"
    1538             :                         "create window stddev_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1539             :                         " external name \"sql\".\"stdevp\";\n"
    1540             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1541             :                         "create window stddev_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1542             :                         " external name \"sql\".\"stdevp\";\n"
    1543             :                         "GRANT EXECUTE ON WINDOW stddev_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1544             :                         "create window var_samp(val TINYINT) returns DOUBLE\n"
    1545             :                         " external name \"sql\".\"variance\";\n"
    1546             :                         "GRANT EXECUTE ON WINDOW var_samp(TINYINT) TO PUBLIC;\n"
    1547             :                         "create window var_samp(val SMALLINT) returns DOUBLE\n"
    1548             :                         " external name \"sql\".\"variance\";\n"
    1549             :                         "GRANT EXECUTE ON WINDOW var_samp(SMALLINT) TO PUBLIC;\n"
    1550             :                         "create window var_samp(val INTEGER) returns DOUBLE\n"
    1551             :                         " external name \"sql\".\"variance\";\n"
    1552             :                         "GRANT EXECUTE ON WINDOW var_samp(INTEGER) TO PUBLIC;\n"
    1553             :                         "create window var_samp(val BIGINT) returns DOUBLE\n"
    1554             :                         " external name \"sql\".\"variance\";\n"
    1555             :                         "GRANT EXECUTE ON WINDOW var_samp(BIGINT) TO PUBLIC;\n"
    1556             :                         "create window var_samp(val REAL) returns DOUBLE\n"
    1557             :                         " external name \"sql\".\"variance\";\n"
    1558             :                         "GRANT EXECUTE ON WINDOW var_samp(REAL) TO PUBLIC;\n"
    1559             :                         "create window var_samp(val DOUBLE) returns DOUBLE\n"
    1560             :                         " external name \"sql\".\"variance\";\n"
    1561             :                         "GRANT EXECUTE ON WINDOW var_samp(DOUBLE) TO PUBLIC;\n"
    1562             :                         "create window var_samp(val INTERVAL SECOND) returns DOUBLE\n"
    1563             :                         " external name \"sql\".\"variance\";\n"
    1564             :                         "GRANT EXECUTE ON WINDOW var_samp(INTERVAL SECOND) TO PUBLIC;\n"
    1565             :                         "create window var_samp(val INTERVAL MONTH) returns DOUBLE\n"
    1566             :                         " external name \"sql\".\"variance\";\n"
    1567             :                         "GRANT EXECUTE ON WINDOW var_samp(INTERVAL MONTH) TO PUBLIC;\n"
    1568             :                         "create window var_pop(val TINYINT) returns DOUBLE\n"
    1569             :                         " external name \"sql\".\"variancep\";\n"
    1570             :                         "GRANT EXECUTE ON WINDOW var_pop(TINYINT) TO PUBLIC;\n"
    1571             :                         "create window var_pop(val SMALLINT) returns DOUBLE\n"
    1572             :                         " external name \"sql\".\"variancep\";\n"
    1573             :                         "GRANT EXECUTE ON WINDOW var_pop(SMALLINT) TO PUBLIC;\n"
    1574             :                         "create window var_pop(val INTEGER) returns DOUBLE\n"
    1575             :                         " external name \"sql\".\"variancep\";\n"
    1576             :                         "GRANT EXECUTE ON WINDOW var_pop(INTEGER) TO PUBLIC;\n"
    1577             :                         "create window var_pop(val BIGINT) returns DOUBLE\n"
    1578             :                         " external name \"sql\".\"variancep\";\n"
    1579             :                         "GRANT EXECUTE ON WINDOW var_pop(BIGINT) TO PUBLIC;\n"
    1580             :                         "create window var_pop(val REAL) returns DOUBLE\n"
    1581             :                         " external name \"sql\".\"variancep\";\n"
    1582             :                         "GRANT EXECUTE ON WINDOW var_pop(REAL) TO PUBLIC;\n"
    1583             :                         "create window var_pop(val DOUBLE) returns DOUBLE\n"
    1584             :                         " external name \"sql\".\"variancep\";\n"
    1585             :                         "GRANT EXECUTE ON WINDOW var_pop(DOUBLE) TO PUBLIC;\n"
    1586             :                         "create window var_pop(val INTERVAL SECOND) returns DOUBLE\n"
    1587             :                         " external name \"sql\".\"variancep\";\n"
    1588             :                         "GRANT EXECUTE ON WINDOW var_pop(INTERVAL SECOND) TO PUBLIC;\n"
    1589             :                         "create window var_pop(val INTERVAL MONTH) returns DOUBLE\n"
    1590             :                         " external name \"sql\".\"variancep\";\n"
    1591             :                         "GRANT EXECUTE ON WINDOW var_pop(INTERVAL MONTH) TO PUBLIC;\n"
    1592             :                         "create aggregate covar_samp(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1593             :                         " external name \"aggr\".\"covariance\";\n"
    1594             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(TINYINT, TINYINT) TO PUBLIC;\n"
    1595             :                         "create aggregate covar_samp(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1596             :                         " external name \"aggr\".\"covariance\";\n"
    1597             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1598             :                         "create aggregate covar_samp(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1599             :                         " external name \"aggr\".\"covariance\";\n"
    1600             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTEGER, INTEGER) TO PUBLIC;\n"
    1601             :                         "create aggregate covar_samp(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1602             :                         " external name \"aggr\".\"covariance\";\n"
    1603             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(BIGINT, BIGINT) TO PUBLIC;\n"
    1604             :                         "create aggregate covar_samp(e1 REAL, e2 REAL) returns DOUBLE\n"
    1605             :                         " external name \"aggr\".\"covariance\";\n"
    1606             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(REAL, REAL) TO PUBLIC;\n"
    1607             :                         "create aggregate covar_samp(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1608             :                         " external name \"aggr\".\"covariance\";\n"
    1609             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1610             :                         "create aggregate covar_samp(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1611             :                         " external name \"aggr\".\"covariance\";\n"
    1612             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1613             :                         "create aggregate covar_samp(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1614             :                         " external name \"aggr\".\"covariance\";\n"
    1615             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1616             :                         "create window covar_samp(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1617             :                         " external name \"sql\".\"covariance\";\n"
    1618             :                         "GRANT EXECUTE ON WINDOW covar_samp(TINYINT, TINYINT) TO PUBLIC;\n"
    1619             :                         "create window covar_samp(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1620             :                         " external name \"sql\".\"covariance\";\n"
    1621             :                         "GRANT EXECUTE ON WINDOW covar_samp(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1622             :                         "create window covar_samp(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1623             :                         " external name \"sql\".\"covariance\";\n"
    1624             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTEGER, INTEGER) TO PUBLIC;\n"
    1625             :                         "create window covar_samp(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1626             :                         " external name \"sql\".\"covariance\";\n"
    1627             :                         "GRANT EXECUTE ON WINDOW covar_samp(BIGINT, BIGINT) TO PUBLIC;\n"
    1628             :                         "create window covar_samp(e1 REAL, e2 REAL) returns DOUBLE\n"
    1629             :                         " external name \"sql\".\"covariance\";\n"
    1630             :                         "GRANT EXECUTE ON WINDOW covar_samp(REAL, REAL) TO PUBLIC;\n"
    1631             :                         "create window covar_samp(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1632             :                         " external name \"sql\".\"covariance\";\n"
    1633             :                         "GRANT EXECUTE ON WINDOW covar_samp(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1634             :                         "create window covar_samp(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1635             :                         " external name \"sql\".\"covariance\";\n"
    1636             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1637             :                         "create window covar_samp(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1638             :                         " external name \"sql\".\"covariance\";\n"
    1639             :                         "GRANT EXECUTE ON WINDOW covar_samp(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1640             :                         "create aggregate covar_pop(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1641             :                         " external name \"aggr\".\"covariancep\";\n"
    1642             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(TINYINT, TINYINT) TO PUBLIC;\n"
    1643             :                         "create aggregate covar_pop(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1644             :                         " external name \"aggr\".\"covariancep\";\n"
    1645             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1646             :                         "create aggregate covar_pop(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1647             :                         " external name \"aggr\".\"covariancep\";\n"
    1648             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTEGER, INTEGER) TO PUBLIC;\n"
    1649             :                         "create aggregate covar_pop(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1650             :                         " external name \"aggr\".\"covariancep\";\n"
    1651             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(BIGINT, BIGINT) TO PUBLIC;\n"
    1652             :                         "create aggregate covar_pop(e1 REAL, e2 REAL) returns DOUBLE\n"
    1653             :                         " external name \"aggr\".\"covariancep\";\n"
    1654             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(REAL, REAL) TO PUBLIC;\n"
    1655             :                         "create aggregate covar_pop(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1656             :                         " external name \"aggr\".\"covariancep\";\n"
    1657             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1658             :                         "create aggregate covar_pop(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1659             :                         " external name \"aggr\".\"covariancep\";\n"
    1660             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1661             :                         "create aggregate covar_pop(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1662             :                         " external name \"aggr\".\"covariancep\";\n"
    1663             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1664             :                         "create window covar_pop(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1665             :                         " external name \"sql\".\"covariancep\";\n"
    1666             :                         "GRANT EXECUTE ON WINDOW covar_pop(TINYINT, TINYINT) TO PUBLIC;\n"
    1667             :                         "create window covar_pop(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1668             :                         " external name \"sql\".\"covariancep\";\n"
    1669             :                         "GRANT EXECUTE ON WINDOW covar_pop(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1670             :                         "create window covar_pop(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1671             :                         " external name \"sql\".\"covariancep\";\n"
    1672             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTEGER, INTEGER) TO PUBLIC;\n"
    1673             :                         "create window covar_pop(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1674             :                         " external name \"sql\".\"covariancep\";\n"
    1675             :                         "GRANT EXECUTE ON WINDOW covar_pop(BIGINT, BIGINT) TO PUBLIC;\n"
    1676             :                         "create window covar_pop(e1 REAL, e2 REAL) returns DOUBLE\n"
    1677             :                         " external name \"sql\".\"covariancep\";\n"
    1678             :                         "GRANT EXECUTE ON WINDOW covar_pop(REAL, REAL) TO PUBLIC;\n"
    1679             :                         "create window covar_pop(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1680             :                         " external name \"sql\".\"covariancep\";\n"
    1681             :                         "GRANT EXECUTE ON WINDOW covar_pop(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1682             :                         "create window covar_pop(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1683             :                         " external name \"sql\".\"covariancep\";\n"
    1684             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1685             :                         "create window covar_pop(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1686             :                         " external name \"sql\".\"covariancep\";\n"
    1687             :                         "GRANT EXECUTE ON WINDOW covar_pop(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1688             :                         "create aggregate corr(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1689             :                         " external name \"aggr\".\"corr\";\n"
    1690             :                         "GRANT EXECUTE ON AGGREGATE corr(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1691             :                         "create aggregate corr(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1692             :                         " external name \"aggr\".\"corr\";\n"
    1693             :                         "GRANT EXECUTE ON AGGREGATE corr(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n"
    1694             :                         "create window corr(e1 TINYINT, e2 TINYINT) returns DOUBLE\n"
    1695             :                         " external name \"sql\".\"corr\";\n"
    1696             :                         "GRANT EXECUTE ON WINDOW corr(TINYINT, TINYINT) TO PUBLIC;\n"
    1697             :                         "create window corr(e1 SMALLINT, e2 SMALLINT) returns DOUBLE\n"
    1698             :                         " external name \"sql\".\"corr\";\n"
    1699             :                         "GRANT EXECUTE ON WINDOW corr(SMALLINT, SMALLINT) TO PUBLIC;\n"
    1700             :                         "create window corr(e1 INTEGER, e2 INTEGER) returns DOUBLE\n"
    1701             :                         " external name \"sql\".\"corr\";\n"
    1702             :                         "GRANT EXECUTE ON WINDOW corr(INTEGER, INTEGER) TO PUBLIC;\n"
    1703             :                         "create window corr(e1 BIGINT, e2 BIGINT) returns DOUBLE\n"
    1704             :                         " external name \"sql\".\"corr\";\n"
    1705             :                         "GRANT EXECUTE ON WINDOW corr(BIGINT, BIGINT) TO PUBLIC;\n"
    1706             :                         "create window corr(e1 REAL, e2 REAL) returns DOUBLE\n"
    1707             :                         " external name \"sql\".\"corr\";\n"
    1708             :                         "GRANT EXECUTE ON WINDOW corr(REAL, REAL) TO PUBLIC;\n"
    1709             :                         "create window corr(e1 DOUBLE, e2 DOUBLE) returns DOUBLE\n"
    1710             :                         " external name \"sql\".\"corr\";\n"
    1711             :                         "GRANT EXECUTE ON WINDOW corr(DOUBLE, DOUBLE) TO PUBLIC;\n"
    1712             :                         "create window corr(e1 INTERVAL SECOND, e2 INTERVAL SECOND) returns DOUBLE\n"
    1713             :                         " external name \"sql\".\"corr\";\n"
    1714             :                         "GRANT EXECUTE ON WINDOW corr(INTERVAL SECOND, INTERVAL SECOND) TO PUBLIC;\n"
    1715             :                         "create window corr(e1 INTERVAL MONTH, e2 INTERVAL MONTH) returns DOUBLE\n"
    1716             :                         " external name \"sql\".\"corr\";\n"
    1717             :                         "GRANT EXECUTE ON WINDOW corr(INTERVAL MONTH, INTERVAL MONTH) TO PUBLIC;\n");
    1718             : 
    1719           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1720             :                 "create window sys.group_concat(str STRING) returns STRING\n"
    1721             :                 " external name \"sql\".\"str_group_concat\";\n"
    1722             :                 "GRANT EXECUTE ON WINDOW sys.group_concat(STRING) TO PUBLIC;\n"
    1723             :                 "create window sys.group_concat(str STRING, sep STRING) returns STRING\n"
    1724             :                 " external name \"sql\".\"str_group_concat\";\n"
    1725             :                 "GRANT EXECUTE ON WINDOW sys.group_concat(STRING, STRING) TO PUBLIC;\n");
    1726             : 
    1727           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1728             :                         "update sys.functions set system = true where system <> true and name in"
    1729             :                         " ('stddev_samp', 'stddev_pop', 'var_samp', 'var_pop', 'covar_samp', 'covar_pop', 'corr', 'group_concat')"
    1730             :                         " and schema_id = (select id from sys.schemas where name = 'sys') and type in (%d, %d);\n", (int) F_ANALYTIC, (int) F_AGGR);
    1731             : 
    1732           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1733             :                         "DROP AGGREGATE stddev_samp(date);\n"
    1734             :                         "DROP AGGREGATE stddev_samp(time);\n"
    1735             :                         "DROP AGGREGATE stddev_samp(timestamp);\n"
    1736             :                         "DROP AGGREGATE stddev_pop(date);\n"
    1737             :                         "DROP AGGREGATE stddev_pop(time);\n"
    1738             :                         "DROP AGGREGATE stddev_pop(timestamp);\n"
    1739             :                         "DROP AGGREGATE var_samp(date);\n"
    1740             :                         "DROP AGGREGATE var_samp(time);\n"
    1741             :                         "DROP AGGREGATE var_samp(timestamp);\n"
    1742             :                         "DROP AGGREGATE var_pop(date);\n"
    1743             :                         "DROP AGGREGATE var_pop(time);\n"
    1744             :                         "DROP AGGREGATE var_pop(timestamp);\n");
    1745             : 
    1746             :         /* 51_sys_schema_extensions */
    1747           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1748             :                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    1749             :                         "DELETE FROM sys.keywords where keyword IN ('NOCYCLE','NOMAXVALUE','NOMINVALUE');\n"
    1750             :                         "insert into sys.keywords values ('ANALYZE'),('AT'),('AUTHORIZATION'),('CACHE'),('CENTURY'),('COLUMN'),('CLIENT'),"
    1751             :                         "('CUBE'),('CYCLE'),('DATA'),('DATE'),('DEBUG'),('DECADE'),('DEALLOCATE'),('DIAGNOSTICS'),('DISTINCT'),"
    1752             :                         "('DOW'),('DOY'),('EXEC'),('EXECUTE'),('EXPLAIN'),('FIRST'),('FWF'),('GROUPING'),('GROUPS'),('INCREMENT'),"
    1753             :                         "('INTERVAL'),('KEY'),('LANGUAGE'),('LARGE'),('LAST'),('LATERAL'),('LEVEL'),('LOADER'),('MATCH'),('MATCHED'),('MAXVALUE'),"
    1754             :                         "('MINVALUE'),('NAME'),('NO'),('NULLS'),('OBJECT'),('OPTIONS'),('PASSWORD'),('PLAN'),('PRECISION'),('PREP'),('PREPARE'),"
    1755             :                         "('QUARTER'),('RELEASE'),('REPLACE'),('ROLLUP'),('SCHEMA'),('SEED'),('SERVER'),('SESSION'),('SETS'),('SIZE'),"
    1756             :                         "('STATEMENT'),('TABLE'),('TEMP'),('TEMPORARY'),('TEXT'),('TIME'),('TIMESTAMP'),('TRACE'),('TYPE'),"
    1757             :                         "('WEEK'),('YEAR'),('ZONE');\n");
    1758           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1759             :                         "ALTER TABLE sys.function_languages SET READ WRITE;\n"
    1760             :                         "DELETE FROM sys.function_languages where language_keyword IN ('PYTHON2','PYTHON2_MAP');\n");
    1761             : 
    1762             :         /* 58_hot_snapshot */
    1763           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1764             :                         "create procedure sys.hot_snapshot(tarfile string)\n"
    1765             :                         " external name sql.hot_snapshot;\n"
    1766             :                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    1767             :                         " and name in ('hot_snapshot') and type = %d;\n", (int) F_PROC);
    1768             : 
    1769             :         /* 81_tracer.sql */
    1770           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1771             :                         "CREATE SCHEMA logging;\n"
    1772             :                         "CREATE PROCEDURE logging.flush()\n"
    1773             :                         " EXTERNAL NAME logging.flush;\n"
    1774             :                         "CREATE PROCEDURE logging.setcomplevel(comp_id STRING, lvl_id STRING)\n"
    1775             :                         " EXTERNAL NAME logging.setcomplevel;\n"
    1776             :                         "CREATE PROCEDURE logging.resetcomplevel(comp_id STRING)\n"
    1777             :                         " EXTERNAL NAME logging.resetcomplevel;\n"
    1778             :                         "CREATE PROCEDURE logging.setlayerlevel(layer_id STRING, lvl_id STRING)\n"
    1779             :                         " EXTERNAL NAME logging.setlayerlevel;\n"
    1780             :                         "CREATE PROCEDURE logging.resetlayerlevel(layer_id STRING)\n"
    1781             :                         " EXTERNAL NAME logging.resetlayerlevel;\n"
    1782             :                         "CREATE PROCEDURE logging.setflushlevel(lvl_id STRING)\n"
    1783             :                         " EXTERNAL NAME logging.setflushlevel;\n"
    1784             :                         "CREATE PROCEDURE logging.resetflushlevel()\n"
    1785             :                         " EXTERNAL NAME logging.resetflushlevel;\n"
    1786             :                         "CREATE PROCEDURE logging.setadapter(adapter_id STRING)\n"
    1787             :                         " EXTERNAL NAME logging.setadapter;\n"
    1788             :                         "CREATE PROCEDURE logging.resetadapter()\n"
    1789             :                         " EXTERNAL NAME logging.resetadapter;\n"
    1790             :                         "CREATE FUNCTION logging.compinfo()\n"
    1791             :                         "RETURNS TABLE(\n"
    1792             :                         " \"id\" int,\n"
    1793             :                         " \"component\" string,\n"
    1794             :                         " \"log_level\" string\n"
    1795             :                         ")\n"
    1796             :                         "EXTERNAL NAME logging.compinfo;\n"
    1797             :                         "GRANT EXECUTE ON FUNCTION logging.compinfo TO public;\n"
    1798             :                         "CREATE view logging.compinfo AS SELECT * FROM logging.compinfo();\n"
    1799             :                         "GRANT SELECT ON logging.compinfo TO public;\n");
    1800           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1801             :                         "update sys.schemas set system = true where name = 'logging';\n"
    1802             :                         "update sys.functions set system = true where system <> true and name in"
    1803             :                         " ('flush', 'setcomplevel', 'resetcomplevel', 'setlayerlevel', 'resetlayerlevel', 'setflushlevel', 'resetflushlevel', 'setadapter', 'resetadapter')"
    1804             :                         " and schema_id = (select id from sys.schemas where name = 'logging') and type = %d;\n"
    1805             :                         "update sys.functions set system = true where system <> true and name in"
    1806             :                         " ('compinfo')"
    1807             :                         " and schema_id = (select id from sys.schemas where name = 'logging') and type = %d;\n"
    1808             :                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'logging')"
    1809             :                         " and name = 'compinfo';\n",
    1810             :                         (int) F_PROC, (int) F_UNION);
    1811             : 
    1812           0 :         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    1813           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    1814           0 :         assert(pos < bufsize);
    1815             : 
    1816           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1817           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1818           0 :         if (err == MAL_SUCCEED) {
    1819           0 :                 pos = snprintf(buf, bufsize, "set schema \"sys\";\n"
    1820             :                                "ALTER TABLE sys.keywords SET READ ONLY;\n"
    1821             :                                "ALTER TABLE sys.function_languages SET READ ONLY;\n");
    1822           0 :                 pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    1823           0 :                 assert(pos < bufsize);
    1824           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    1825           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1826             :         }
    1827           0 :         GDKfree(buf);
    1828           0 :         return err;             /* usually MAL_SUCCEED */
    1829             : }
    1830             : 
    1831             : static str
    1832          69 : sql_update_jun2020_bam(Client c, mvc *m, const char *prev_schema)
    1833             : {
    1834             :         size_t bufsize = 10240, pos = 0;
    1835             :         char *err = NULL, *buf;
    1836             :         res_table *output;
    1837             :         BAT *b;
    1838          69 :         sql_schema *s = mvc_bind_schema(m, "bam");
    1839             :         sql_table *t;
    1840             : 
    1841          69 :         if (s == NULL || !s->system)
    1842             :                 return NULL;    /* no system schema "bam": nothing to do */
    1843             : 
    1844           0 :         buf = GDKmalloc(bufsize);
    1845           0 :         if (buf == NULL)
    1846           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1847             : 
    1848           0 :         s->system = 0;
    1849           0 :         if ((t = mvc_bind_table(m, s, "files")) != NULL)
    1850           0 :                 t->system = 0;
    1851           0 :         if ((t = mvc_bind_table(m, s, "sq")) != NULL)
    1852           0 :                 t->system = 0;
    1853           0 :         if ((t = mvc_bind_table(m, s, "rg")) != NULL)
    1854           0 :                 t->system = 0;
    1855           0 :         if ((t = mvc_bind_table(m, s, "pg")) != NULL)
    1856           0 :                 t->system = 0;
    1857           0 :         if ((t = mvc_bind_table(m, s, "export")) != NULL)
    1858           0 :                 t->system = 0;
    1859             : 
    1860             :         /* check if any of the tables in the bam schema have any content */
    1861           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1862             :                         "select sum(count) from sys.storage('bam');\n");
    1863           0 :         err = SQLstatementIntern(c, buf, "update", 1, 0, &output);
    1864           0 :         if (err) {
    1865           0 :                 GDKfree(buf);
    1866           0 :                 return err;
    1867             :         }
    1868           0 :         b = BATdescriptor(output->cols[0].b);
    1869             :         pos = 0;
    1870           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1871             :                         "set schema sys;\n"
    1872             :                         "update sys.schemas set system = false where name = 'bam';\n"
    1873             :                         "update sys._tables set system = false where schema_id in (select id from sys.schemas where name = 'bam');\n"
    1874             :                         "drop procedure bam.bam_loader_repos;\n"
    1875             :                         "drop procedure bam.bam_loader_files;\n"
    1876             :                         "drop procedure bam.bam_loader_file;\n"
    1877             :                         "drop procedure bam.bam_drop_file;\n"
    1878             :                         "drop function bam.bam_flag;\n"
    1879             :                         "drop function bam.reverse_seq;\n"
    1880             :                         "drop function bam.reverse_qual;\n"
    1881             :                         "drop function bam.seq_length;\n"
    1882             :                         "drop function bam.seq_char;\n"
    1883             :                         "drop procedure bam.sam_export;\n"
    1884             :                         "drop procedure bam.bam_export;\n");
    1885           0 :         if (b) {
    1886           0 :                 if (BATcount(b) > 0 && *(lng *) Tloc(b, 0) == 0) {
    1887             :                         /* tables in bam schema are empty: drop them */
    1888           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    1889             :                                         "drop table bam.sq;\n"
    1890             :                                         "drop table bam.rg;\n"
    1891             :                                         "drop table bam.pg;\n"
    1892             :                                         "drop table bam.export;\n"
    1893             :                                         "drop table bam.files;\n"
    1894             :                                         "drop schema bam;\n");
    1895             :                 }
    1896           0 :                 BBPunfix(b->batCacheid);
    1897             :         }
    1898           0 :         res_table_destroy(output);
    1899             : 
    1900           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    1901           0 :         assert(pos < bufsize);
    1902             : 
    1903           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1904           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1905             : 
    1906           0 :         GDKfree(buf);
    1907           0 :         return err;
    1908             : }
    1909             : 
    1910             : #ifdef HAVE_HGE
    1911             : static str
    1912           0 : sql_update_jun2020_sp1_hugeint(Client c, const char *prev_schema)
    1913             : {
    1914             :         size_t bufsize = 8192, pos = 0;
    1915             :         char *buf, *err;
    1916             : 
    1917           0 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    1918           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1919             : 
    1920           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
    1921             : 
    1922             :         /* 39_analytics_hge.sql */
    1923           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1924             :                         "create window stddev_samp(val HUGEINT) returns DOUBLE\n"
    1925             :                         " external name \"sql\".\"stdev\";\n"
    1926             :                         "GRANT EXECUTE ON WINDOW stddev_samp(HUGEINT) TO PUBLIC;\n"
    1927             :                         "create window stddev_pop(val HUGEINT) returns DOUBLE\n"
    1928             :                         " external name \"sql\".\"stdevp\";\n"
    1929             :                         "GRANT EXECUTE ON WINDOW stddev_pop(HUGEINT) TO PUBLIC;\n"
    1930             :                         "create window var_samp(val HUGEINT) returns DOUBLE\n"
    1931             :                         " external name \"sql\".\"variance\";\n"
    1932             :                         "GRANT EXECUTE ON WINDOW var_samp(HUGEINT) TO PUBLIC;\n"
    1933             :                         "create aggregate covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1934             :                         " external name \"aggr\".\"covariance\";\n"
    1935             :                         "GRANT EXECUTE ON AGGREGATE covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1936             :                         "create window covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1937             :                         " external name \"sql\".\"covariance\";\n"
    1938             :                         "GRANT EXECUTE ON WINDOW covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1939             :                         "create window var_pop(val HUGEINT) returns DOUBLE\n"
    1940             :                         " external name \"sql\".\"variancep\";\n"
    1941             :                         "GRANT EXECUTE ON WINDOW var_pop(HUGEINT) TO PUBLIC;\n"
    1942             :                         "create aggregate covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1943             :                         " external name \"aggr\".\"covariancep\";\n"
    1944             :                         "GRANT EXECUTE ON AGGREGATE covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1945             :                         "create window covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1946             :                         " external name \"sql\".\"covariancep\";\n"
    1947             :                         "GRANT EXECUTE ON WINDOW covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
    1948             :                         "create window corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
    1949             :                         " external name \"sql\".\"corr\";\n"
    1950             :                         "GRANT EXECUTE ON WINDOW corr(HUGEINT, HUGEINT) TO PUBLIC;\n");
    1951             : 
    1952           0 :         pos += snprintf(buf + pos, bufsize - pos,
    1953             :                         "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"
    1954             :                         "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",
    1955             :                         (int) F_AGGR, (int) F_ANALYTIC);
    1956             : 
    1957           0 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    1958           0 :         assert(pos < bufsize);
    1959             : 
    1960           0 :         printf("Running database upgrade commands:\n%s\n", buf);
    1961           0 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    1962           0 :         GDKfree(buf);
    1963           0 :         return err;             /* usually MAL_SUCCEED */
    1964             : }
    1965             : #endif
    1966             : 
    1967             : static str
    1968          69 : sql_update_jun2020_sp2(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    1969             : {
    1970             :         /* we need to update the system tables, but only if we haven't done
    1971             :          * so already, and if we are actually upgrading the database */
    1972          69 :         if (*systabfixed)
    1973             :                 return MAL_SUCCEED;             /* already done */
    1974             : 
    1975             :         char *buf = "select id from sys.functions where name = 'nullif' and schema_id = (select id from sys.schemas where name = 'sys');\n";
    1976             :         res_table *output;
    1977          61 :         char *err = SQLstatementIntern(c, buf, "update", 1, 0, &output);
    1978          61 :         if (err == NULL) {
    1979          61 :                 BAT *b = BATdescriptor(output->cols[0].b);
    1980          61 :                 if (b) {
    1981          61 :                         if (BATcount(b) == 0) {
    1982           0 :                                 err = sql_fix_system_tables(c, sql, prev_schema);
    1983           0 :                                 *systabfixed = true;
    1984             :                         }
    1985          61 :                         BBPunfix(b->batCacheid);
    1986             :                 }
    1987          61 :                 res_table_destroy(output);
    1988             :         }
    1989             :         return err;
    1990             : }
    1991             : 
    1992             : static str
    1993           0 : sql_update_oscar_lidar(Client c)
    1994             : {
    1995             :         char *query =
    1996             :                 "drop procedure sys.lidarattach(string);\n"
    1997             :                 "drop procedure sys.lidarload(string);\n"
    1998             :                 "drop procedure sys.lidarexport(string, string, string);\n";
    1999           0 :         printf("Running database upgrade commands:\n%s\n", query);
    2000           0 :         return SQLstatementIntern(c, query, "update", true, false, NULL);
    2001             : }
    2002             : 
    2003             : static str
    2004          69 : sql_update_oscar(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    2005             : {
    2006             :         size_t bufsize = 8192, pos = 0;
    2007          69 :         char *err = NULL, *buf = GDKmalloc(bufsize);
    2008          69 :         sql_schema *sys = mvc_bind_schema(sql, "sys");
    2009             :         res_table *output;
    2010             :         BAT *b;
    2011             : 
    2012          69 :         if (buf == NULL)
    2013           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2014             : 
    2015             :         /* if column 6 of sys.queue is named "progress" we need to update */
    2016          69 :         pos += snprintf(buf + pos, bufsize - pos,
    2017             :                         "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");
    2018          69 :         err = SQLstatementIntern(c, buf, "update", true, false, &output);
    2019          69 :         if (err) {
    2020           0 :                 GDKfree(buf);
    2021           0 :                 return err;
    2022             :         }
    2023          69 :         b = BATdescriptor(output->cols[0].b);
    2024          69 :         if (b) {
    2025          69 :                 BATiter bi = bat_iterator_nolock(b);
    2026          69 :                 if (BATcount(b) > 0 && strcmp(BUNtail(bi, 0), "progress") == 0) {
    2027           0 :                         if (!*systabfixed &&
    2028           0 :                                 (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL)
    2029           0 :                                 return err;
    2030           0 :                         *systabfixed = true;
    2031             : 
    2032             :                         pos = 0;
    2033           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2034             :                                         "set schema \"sys\";\n");
    2035             : 
    2036             :                         /* the real update of sys.env() has happened
    2037             :                          * in load_func, here we merely update the
    2038             :                          * sys.functions table */
    2039           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2040             :                                         "update sys.functions set"
    2041             :                                         " mod = 'inspect',"
    2042             :                                         " func = 'CREATE FUNCTION env() RETURNS TABLE( name varchar(1024), value varchar(2048)) EXTERNAL NAME inspect.\"getEnvironment\";'"
    2043             :                                         " where schema_id = (select id from sys.schemas where name = 'sys')"
    2044             :                                         " and name = 'env' and type = %d;\n",
    2045             :                                         (int) F_UNION);
    2046             : 
    2047             :                         /* 26_sysmon */
    2048             :                         sql_table *t;
    2049           0 :                         t = mvc_bind_table(sql, sys, "queue");
    2050           0 :                         t->system = 0; /* make it non-system else the drop view will fail */
    2051             : 
    2052           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2053             :                                         "drop view sys.queue;\n"
    2054             :                                         "drop function sys.queue;\n"
    2055             :                                         "create function sys.queue()\n"
    2056             :                                         "returns table(\n"
    2057             :                                         "\"tag\" bigint,\n"
    2058             :                                         "\"sessionid\" int,\n"
    2059             :                                         "\"username\" string,\n"
    2060             :                                         "\"started\" timestamp,\n"
    2061             :                                         "\"status\" string,\n"
    2062             :                                         "\"query\" string,\n"
    2063             :                                         "\"finished\" timestamp,\n"
    2064             :                                         "\"workers\" int,\n"
    2065             :                                         "\"memory\" int)\n"
    2066             :                                         " external name sysmon.queue;\n"
    2067             :                                         "grant execute on function sys.queue to public;\n"
    2068             :                                         "create view sys.queue as select * from sys.queue();\n"
    2069             :                                         "grant select on sys.queue to public;\n"
    2070             :                                         "drop procedure sys.pause(bigint);\n"
    2071             :                                         "drop procedure sys.resume(bigint);\n"
    2072             :                                         "drop procedure sys.stop(bigint);\n"
    2073             :                                         "create procedure sys.pause(tag bigint)\n"
    2074             :                                         "external name sysmon.pause;\n"
    2075             :                                         "grant execute on procedure sys.pause(bigint) to public;\n"
    2076             :                                         "create procedure sys.resume(tag bigint)\n"
    2077             :                                         "external name sysmon.resume;\n"
    2078             :                                         "grant execute on procedure sys.resume(bigint) to public;\n"
    2079             :                                         "create procedure sys.stop(tag bigint)\n"
    2080             :                                         "external name sysmon.stop;\n"
    2081             :                                         "grant execute on procedure sys.stop(bigint) to public;\n");
    2082             : 
    2083           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2084             :                                         "update sys.functions set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    2085             :                                         " and name = 'queue' and type = %d;\n"
    2086             :                                         "update sys.functions set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    2087             :                                         " and name in ('pause', 'resume', 'stop') and type = %d;\n",
    2088             :                                         (int) F_UNION, (int) F_PROC);
    2089           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2090             :                                         "update sys._tables set system = true where schema_id = (select id from sys.schemas where name = 'sys')"
    2091             :                                         " and name = 'queue';\n");
    2092             : 
    2093             :                         /* scoping branch changes */
    2094           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2095             :                                         "drop function \"sys\".\"var\"();\n"
    2096             :                                         "create function \"sys\".\"var\"() "
    2097             :                                         "returns table("
    2098             :                                         "\"schema\" string, "
    2099             :                                         "\"name\" string, "
    2100             :                                         "\"type\" string, "
    2101             :                                         "\"value\" string) "
    2102             :                                         "external name \"sql\".\"sql_variables\";\n"
    2103             :                                         "grant execute on function \"sys\".\"var\" to public;\n");
    2104             : 
    2105           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2106             :                                         "create procedure sys.hot_snapshot(tarfile string, onserver bool)\n"
    2107             :                                         "external name sql.hot_snapshot;\n"
    2108             :                                         "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'sys')"
    2109             :                                         " and name in ('hot_snapshot') and type = %d;\n",
    2110             :                                         (int) F_PROC);
    2111             :                         /* .snapshot user */
    2112           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2113             :                                 "create user \".snapshot\"\n"
    2114             :                                 " with encrypted password '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'\n"
    2115             :                                 " name 'Snapshot User'\n"
    2116             :                                 " schema sys;\n"
    2117             :                                 "grant execute on procedure sys.hot_snapshot(string) to \".snapshot\";\n"
    2118             :                                 "grant execute on procedure sys.hot_snapshot(string, bool) to \".snapshot\";\n"
    2119             :                         );
    2120             : 
    2121             :                         /* update system tables so that the content
    2122             :                          * looks more like what it would be if sys.var
    2123             :                          * had been defined by the C code in
    2124             :                          * sql_create_env() */
    2125           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2126             :                                         "update sys.functions set system = true,"
    2127             :                                         //" func = 'CREATE FUNCTION \"sys\".\"var\"() RETURNS TABLE(\"schema\" string, \"name\" string, \"type\" string, \"value\" string) EXTERNAL NAME \"sql\".\"sql_variables\";',"
    2128             :                                         " language = 2, side_effect = false where name = 'var' and schema_id = (select id from sys.schemas where name = 'sys') and type = %d;\n"
    2129             :                                         "update sys.args set type = 'char' where func_id = (select id from sys.functions where name = 'var' and schema_id = (select id from sys.schemas where name = 'sys') and type = %d) and type = 'clob';\n"
    2130             :                                         "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",
    2131             :                                         (int) F_UNION,
    2132             :                                         (int) F_UNION,
    2133             :                                         (int) F_UNION);
    2134             : 
    2135             :                         /* SQL functions without backend implementations */
    2136           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2137             :                                         "DROP FUNCTION \"sys\".\"getcontent\"(url);\n"
    2138             :                                         "DROP AGGREGATE \"json\".\"output\"(json);\n");
    2139             : 
    2140             :                         /* 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 */
    2141           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2142             :                                         "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");
    2143             : 
    2144           0 :                         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    2145           0 :                         assert(pos < bufsize);
    2146             : 
    2147           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    2148           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2149             :                 }
    2150          69 :                 BBPunfix(b->batCacheid);
    2151             :         }
    2152          69 :         res_table_destroy(output);
    2153          69 :         GDKfree(buf);
    2154          69 :         return err;             /* usually MAL_SUCCEED */
    2155             : }
    2156             : 
    2157             : static str
    2158          69 : sql_update_oct2020(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    2159             : {
    2160             :         size_t bufsize = 4096, pos = 0;
    2161             :         char *buf = NULL, *err = NULL;
    2162          69 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    2163             :         sql_table *t;
    2164          69 :         res_table *output = NULL;
    2165             :         BAT *b = NULL;
    2166             : 
    2167          69 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    2168           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2169             : 
    2170             :         /* if view sys.var_values mentions the query cache ('cache') we need
    2171             :            to update */
    2172          69 :         pos += snprintf(buf + pos, bufsize - pos,
    2173             :                                         "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");
    2174          69 :         err = SQLstatementIntern(c, buf, "update", true, false, &output);
    2175          69 :         if (err) {
    2176           0 :                 GDKfree(buf);
    2177           0 :                 return err;
    2178             :         }
    2179          69 :         b = BATdescriptor(output->cols[0].b);
    2180          69 :         if (b) {
    2181          69 :                 if (BATcount(b) > 0) {
    2182           0 :                         if (!*systabfixed &&
    2183           0 :                                 (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL)
    2184             :                                 return err;
    2185           0 :                         *systabfixed = true;
    2186             : 
    2187             :                         pos = 0;
    2188           0 :                         pos += snprintf(buf + pos, bufsize - pos, "set schema sys;\n");
    2189             : 
    2190             :                         /* 51_sys_schema_extensions.sql */
    2191           0 :                         t = mvc_bind_table(sql, s, "var_values");
    2192           0 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    2193           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2194             :                                                         "DROP VIEW sys.var_values;\n"
    2195             :                                                         "CREATE VIEW sys.var_values (var_name, value) AS\n"
    2196             :                                                         "SELECT 'current_role', current_role UNION ALL\n"
    2197             :                                                         "SELECT 'current_schema', current_schema UNION ALL\n"
    2198             :                                                         "SELECT 'current_timezone', current_timezone UNION ALL\n"
    2199             :                                                         "SELECT 'current_user', current_user UNION ALL\n"
    2200             :                                                         "SELECT 'debug', debug UNION ALL\n"
    2201             :                                                         "SELECT 'last_id', last_id UNION ALL\n"
    2202             :                                                         "SELECT 'optimizer', optimizer UNION ALL\n"
    2203             :                                                         "SELECT 'pi', pi() UNION ALL\n"
    2204             :                                                         "SELECT 'rowcnt', rowcnt;\n"
    2205             :                                                         "UPDATE sys._tables SET system = true WHERE name = 'var_values' AND schema_id = (SELECT id FROM sys.schemas WHERE name = 'sys');\n"
    2206             :                                                         "GRANT SELECT ON sys.var_values TO PUBLIC;\n");
    2207             :                         /* 26_sysmon.sql */
    2208           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2209             :                                         "create function sys.user_statistics()\n"
    2210             :                                         "returns table(\n"
    2211             :                                                 " username string,\n"
    2212             :                                                 " querycount bigint,\n"
    2213             :                                                 " totalticks bigint,\n"
    2214             :                                                 " started timestamp,\n"
    2215             :                                                 " finished timestamp,\n"
    2216             :                                                 " maxticks bigint,\n"
    2217             :                                                 " maxquery string\n"
    2218             :                                         ")\n"
    2219             :                                         "external name sysmon.user_statistics;\n"
    2220             :                                         "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);
    2221             : 
    2222             :                         /* Remove entries on sys.args table without correspondents on sys.functions table */
    2223           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2224             :                                         "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");
    2225             : 
    2226             :                         /* 39_analytics.sql */
    2227           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2228             :                                         "DROP AGGREGATE stddev_samp(INTERVAL SECOND);\n"
    2229             :                                         "DROP AGGREGATE stddev_samp(INTERVAL MONTH);\n"
    2230             :                                         "DROP WINDOW stddev_samp(INTERVAL SECOND);\n"
    2231             :                                         "DROP WINDOW stddev_samp(INTERVAL MONTH);\n"
    2232             :                                         "DROP AGGREGATE stddev_pop(INTERVAL SECOND);\n"
    2233             :                                         "DROP AGGREGATE stddev_pop(INTERVAL MONTH);\n"
    2234             :                                         "DROP WINDOW stddev_pop(INTERVAL SECOND);\n"
    2235             :                                         "DROP WINDOW stddev_pop(INTERVAL MONTH);\n"
    2236             :                                         "DROP AGGREGATE var_samp(INTERVAL SECOND);\n"
    2237             :                                         "DROP AGGREGATE var_samp(INTERVAL MONTH);\n"
    2238             :                                         "DROP WINDOW var_samp(INTERVAL SECOND);\n"
    2239             :                                         "DROP WINDOW var_samp(INTERVAL MONTH);\n"
    2240             :                                         "DROP AGGREGATE var_pop(INTERVAL SECOND);\n"
    2241             :                                         "DROP AGGREGATE var_pop(INTERVAL MONTH);\n"
    2242             :                                         "DROP WINDOW var_pop(INTERVAL SECOND);\n"
    2243             :                                         "DROP WINDOW var_pop(INTERVAL MONTH);\n"
    2244             :                                         "DROP AGGREGATE covar_samp(INTERVAL SECOND,INTERVAL SECOND);\n"
    2245             :                                         "DROP AGGREGATE covar_samp(INTERVAL MONTH,INTERVAL MONTH);\n"
    2246             :                                         "DROP WINDOW covar_samp(INTERVAL SECOND,INTERVAL SECOND);\n"
    2247             :                                         "DROP WINDOW covar_samp(INTERVAL MONTH,INTERVAL MONTH);\n"
    2248             :                                         "DROP AGGREGATE covar_pop(INTERVAL SECOND,INTERVAL SECOND);\n"
    2249             :                                         "DROP AGGREGATE covar_pop(INTERVAL MONTH,INTERVAL MONTH);\n"
    2250             :                                         "DROP WINDOW covar_pop(INTERVAL SECOND,INTERVAL SECOND);\n"
    2251             :                                         "DROP WINDOW covar_pop(INTERVAL MONTH,INTERVAL MONTH);\n"
    2252             :                                         "DROP AGGREGATE corr(INTERVAL SECOND,INTERVAL SECOND);\n"
    2253             :                                         "DROP AGGREGATE corr(INTERVAL MONTH,INTERVAL MONTH);\n"
    2254             :                                         "DROP WINDOW corr(INTERVAL SECOND,INTERVAL SECOND);\n"
    2255             :                                         "DROP WINDOW corr(INTERVAL MONTH,INTERVAL MONTH);\n"
    2256             :                                         "create aggregate median(val INTERVAL DAY) returns INTERVAL DAY\n"
    2257             :                                         " external name \"aggr\".\"median\";\n"
    2258             :                                         "GRANT EXECUTE ON AGGREGATE median(INTERVAL DAY) TO PUBLIC;\n"
    2259             :                                         "create aggregate quantile(val INTERVAL DAY, q DOUBLE) returns INTERVAL DAY\n"
    2260             :                                         " external name \"aggr\".\"quantile\";\n"
    2261             :                                         "GRANT EXECUTE ON AGGREGATE quantile(INTERVAL DAY, DOUBLE) TO PUBLIC;\n"
    2262             :                                         "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);
    2263             : 
    2264             :                         /* 90_generator.sql */
    2265           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2266             :                                         "create function sys.generate_series(first timestamp, \"limit\" timestamp, stepsize interval day) returns table (value timestamp)\n"
    2267             :                                         " external name generator.series;\n"
    2268             :                                         "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);
    2269             : 
    2270             :                         /* 51_sys_schema_extensions.sql */
    2271           0 :                         pos += snprintf(buf + pos, bufsize - pos,
    2272             :                                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    2273             :                                         "insert into sys.keywords values ('EPOCH');\n");
    2274             : 
    2275           0 :                         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    2276           0 :                         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    2277             : 
    2278           0 :                         assert(pos < bufsize);
    2279           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    2280           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2281           0 :                         if (err != MAL_SUCCEED)
    2282           0 :                                 goto bailout;
    2283             : 
    2284           0 :                         pos = snprintf(buf, bufsize, "set schema \"sys\";\n"
    2285             :                                         "ALTER TABLE sys.keywords SET READ ONLY;\n");
    2286           0 :                         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    2287           0 :                         assert(pos < bufsize);
    2288           0 :                         printf("Running database upgrade commands:\n%s\n", buf);
    2289           0 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2290           0 :                         if (err != MAL_SUCCEED)
    2291           0 :                                 goto bailout;
    2292           0 :                         err = sql_update_storagemodel(c, sql, prev_schema, true); /* because of day interval addition, we have to recreate the storagmodel views */
    2293             :                 }
    2294             :         }
    2295             : 
    2296          69 : bailout:
    2297          69 :         if (b)
    2298          69 :                 BBPunfix(b->batCacheid);
    2299          69 :         if (output)
    2300          69 :                 res_table_destroy(output);
    2301          69 :         GDKfree(buf);
    2302          69 :         return err;             /* usually MAL_SUCCEED */
    2303             : }
    2304             : 
    2305             : static str
    2306          69 : sql_update_oct2020_sp1(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    2307             : {
    2308             :         size_t bufsize = 1024, pos = 0;
    2309             :         char *buf = NULL, *err = NULL;
    2310             : 
    2311          69 :         if (!sql_bind_func(sql, "sys", "uuid", sql_bind_localtype("int"), NULL, F_FUNC)) {
    2312           0 :                 sql->session->status = 0; /* if the function was not found clean the error */
    2313           0 :                 sql->errstr[0] = '\0';
    2314             : 
    2315           0 :                 if ((buf = GDKmalloc(bufsize)) == NULL)
    2316           0 :                         throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2317             : 
    2318           0 :                 if (!*systabfixed && (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL) {
    2319           0 :                         GDKfree(buf);
    2320           0 :                         return err;
    2321             :                 }
    2322           0 :                 *systabfixed = true;
    2323             : 
    2324           0 :                 pos += snprintf(buf + pos, bufsize - pos, "set schema sys;\n");
    2325             :                 /* 45_uuid.sql */
    2326           0 :                 pos += snprintf(buf + pos, bufsize - pos,
    2327             :                         "create function sys.uuid(d int) returns uuid\n"
    2328             :                         " external name uuid.\"new\";\n"
    2329             :                         "GRANT EXECUTE ON FUNCTION sys.uuid(int) TO PUBLIC;\n"
    2330             :                         "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);
    2331             : 
    2332           0 :                 pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    2333           0 :                 assert(pos < bufsize);
    2334           0 :                 printf("Running database upgrade commands:\n%s\n", buf);
    2335           0 :                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    2336             :         }
    2337          69 :         GDKfree(buf);
    2338          69 :         return err;             /* usually MAL_SUCCEED */
    2339             : }
    2340             : 
    2341             : static str
    2342          69 : sql_update_jul2021(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    2343             : {
    2344             :         size_t bufsize = 65536, pos = 0;
    2345             :         char *buf = NULL, *err = NULL;
    2346          69 :         res_table *output = NULL;
    2347             :         BAT *b = NULL;
    2348          69 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    2349             :         sql_table *t;
    2350             : 
    2351          69 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    2352           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2353             : 
    2354             :         /* if the keyword STREAM is in the list of keywords, upgrade */
    2355          69 :         pos += snprintf(buf + pos, bufsize - pos,
    2356             :                                         "select keyword from sys.keywords where keyword = 'STREAM';\n");
    2357          69 :         assert(pos < bufsize);
    2358          69 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
    2359           0 :                 goto bailout;
    2360          69 :         if ((b = BATdescriptor(output->cols[0].b))) {
    2361          69 :                 if (BATcount(b) == 1) {
    2362           8 :                         if (!*systabfixed && (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL)
    2363           0 :                                 goto bailout;
    2364           8 :                         *systabfixed = true;
    2365             : 
    2366           8 :                         pos = snprintf(buf, bufsize, "set schema \"sys\";\n");
    2367             : 
    2368             :                         /* 20_vacuum.sql */
    2369           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2370             :                                                         "drop procedure sys.shrink(string, string);\n"
    2371             :                                                         "drop procedure sys.reuse(string, string);\n"
    2372             :                                                         "drop procedure sys.vacuum(string, string);\n");
    2373             : 
    2374             :                         /* 22_clients.sql */
    2375           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2376             :                                                         "create function sys.current_sessionid() returns int\n"
    2377             :                                                         "external name clients.current_sessionid;\n"
    2378             :                                                         "grant execute on function sys.current_sessionid to public;\n"
    2379             :                                                         "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'current_sessionid' and type = %d;\n", (int) F_FUNC);
    2380             : 
    2381             :                         /* 25_debug.sql */
    2382           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2383             :                                                         "drop procedure sys.flush_log();\n");
    2384             : 
    2385           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2386             :                                                         "drop function sys.deltas(string);\n"
    2387             :                                                         "drop function sys.deltas(string, string);\n"
    2388             :                                                         "drop function sys.deltas(string, string, string);\n");
    2389           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2390             :                                                         "create function sys.deltas (\"schema\" string)\n"
    2391             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2392             :                                                         "external name \"sql\".\"deltas\";\n"
    2393             :                                                         "create function sys.deltas (\"schema\" string, \"table\" string)\n"
    2394             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2395             :                                                         "external name \"sql\".\"deltas\";\n"
    2396             :                                                         "create function sys.deltas (\"schema\" string, \"table\" string, \"column\" string)\n"
    2397             :                                                         "returns table (\"id\" int, \"segments\" bigint, \"all\" bigint, \"inserted\" bigint, \"updates\" bigint, \"deletes\" bigint, \"level\" int)\n"
    2398             :                                                         "external name \"sql\".\"deltas\";\n"
    2399             :                                                         "update sys.functions set system = true"
    2400             :                                                         " where schema_id = 2000 and name = 'deltas';\n");
    2401             : 
    2402             :                         /* 26_sysmon */
    2403           8 :                         t = mvc_bind_table(sql, s, "queue");
    2404           8 :                         t->system = 0; /* make it non-system else the drop view will fail */
    2405             : 
    2406           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2407             :                                                         "drop view sys.queue;\n"
    2408             :                                                         "drop function sys.queue;\n"
    2409             :                                                         "create function sys.queue()\n"
    2410             :                                                         "returns table(\n"
    2411             :                                                         "\"tag\" bigint,\n"
    2412             :                                                         "\"sessionid\" int,\n"
    2413             :                                                         "\"username\" string,\n"
    2414             :                                                         "\"started\" timestamp,\n"
    2415             :                                                         "\"status\" string,\n"
    2416             :                                                         "\"query\" string,\n"
    2417             :                                                         "\"finished\" timestamp,\n"
    2418             :                                                         "\"maxworkers\" int,\n"
    2419             :                                                         "\"footprint\" int\n"
    2420             :                                                         ")\n"
    2421             :                                                         "external name sysmon.queue;\n"
    2422             :                                                         "grant execute on function sys.queue to public;\n"
    2423             :                                                         "create view sys.queue as select * from sys.queue();\n"
    2424             :                                                         "grant select on sys.queue to public;\n");
    2425           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2426             :                                                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    2427             :                                                         " and name = 'queue' and type = %d;\n", (int) F_UNION);
    2428           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2429             :                                                         "update sys._tables set system = true where schema_id = 2000"
    2430             :                                                         " and name = 'queue';\n");
    2431             : 
    2432             :                         /* fix up dependencies for function getproj4 (if it exists) */
    2433           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2434             :                                                         "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");
    2435             : 
    2436             :                         /* 41_json.sql */
    2437           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2438             :                                                         "drop function json.isobject(string);\n"
    2439             :                                                         "drop function json.isarray(string);\n"
    2440             :                                                         "drop function json.isvalid(json);\n"
    2441             :                                                         "create function json.isvalid(js json)\n"
    2442             :                                                         "returns bool begin return true; end;\n"
    2443             :                                                         "grant execute on function json.isvalid(json) to public;\n"
    2444             :                                                         "update sys.functions set system = true"
    2445             :                                                         " where schema_id = (select id from sys.schemas where name = 'json')"
    2446             :                                                         " and name = 'isvalid';\n");
    2447             : 
    2448             :                         /* 51_sys_schema_extensions, remove stream table entries and update window function description */
    2449           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2450             :                                         "ALTER TABLE sys.keywords SET READ WRITE;\n"
    2451             :                                         "DELETE FROM sys.keywords where keyword = 'STREAM';\n"
    2452             :                                         "INSERT INTO sys.keywords VALUES ('BIG'), ('LITTLE'), ('NATIVE'), ('ENDIAN'), ('CURRENT_SCHEMA'), ('CURRENT_TIMEZONE'), ('IMPRINTS'), ('ORDERED'), ('PATH'), ('ROLE'), ('ROW'), ('VALUE');\n"
    2453             :                                         "ALTER TABLE sys.table_types SET READ WRITE;\n"
    2454             :                                         "DELETE FROM sys.table_types where table_type_id = 4;\n"
    2455             :                                         "ALTER TABLE sys.function_types SET READ WRITE;\n"
    2456             :                                         "UPDATE sys.function_types SET function_type_keyword = 'WINDOW' WHERE function_type_id = 6;\n");
    2457             : 
    2458             :                         /* 52_describe.sql */
    2459           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2460             :                                         "CREATE FUNCTION sys.describe_type(ctype string, digits integer, tscale integer)\n"
    2461             :                                         "  RETURNS string\n"
    2462             :                                         "BEGIN\n"
    2463             :                                         "  RETURN\n"
    2464             :                                         "    CASE ctype\n"
    2465             :                                         "      WHEN 'bigint' THEN 'BIGINT'\n"
    2466             :                                         "      WHEN 'blob' THEN\n"
    2467             :                                         "        CASE digits\n"
    2468             :                                         "          WHEN 0 THEN 'BINARY LARGE OBJECT'\n"
    2469             :                                         "          ELSE 'BINARY LARGE OBJECT(' || digits || ')'\n"
    2470             :                                         "        END\n"
    2471             :                                         "      WHEN 'boolean' THEN 'BOOLEAN'\n"
    2472             :                                         "      WHEN 'char' THEN\n"
    2473             :                                         "        CASE digits\n"
    2474             :                                         "          WHEN 1 THEN 'CHARACTER'\n"
    2475             :                                         "          ELSE 'CHARACTER(' || digits || ')'\n"
    2476             :                                         "        END\n"
    2477             :                                         "      WHEN 'clob' THEN\n"
    2478             :                                         "    CASE digits\n"
    2479             :                                         "      WHEN 0 THEN 'CHARACTER LARGE OBJECT'\n"
    2480             :                                         "      ELSE 'CHARACTER LARGE OBJECT(' || digits || ')'\n"
    2481             :                                         "    END\n"
    2482             :                                         "      WHEN 'date' THEN 'DATE'\n"
    2483             :                                         "      WHEN 'day_interval' THEN 'INTERVAL DAY'\n"
    2484             :                                         "      WHEN ctype = 'decimal' THEN\n"
    2485             :                                         "      CASE\n"
    2486             :                                         "      WHEN (digits = 1 AND tscale = 0) OR digits = 0 THEN 'DECIMAL'\n"
    2487             :                                         "      WHEN tscale = 0 THEN 'DECIMAL(' || digits || ')'\n"
    2488             :                                         "      WHEN digits = 39 THEN 'DECIMAL(' || 38 || ',' || tscale || ')'\n"
    2489             :                                         "      WHEN digits = 19 AND (SELECT COUNT(*) = 0 FROM sys.types WHERE sqlname = 'hugeint' ) THEN 'DECIMAL(' || 18 || ',' || tscale || ')'\n"
    2490             :                                         "      ELSE 'DECIMAL(' || digits || ',' || tscale || ')'\n"
    2491             :                                         "    END\n"
    2492             :                                         "      WHEN 'double' THEN\n"
    2493             :                                         "    CASE\n"
    2494             :                                         "      WHEN digits = 53 and tscale = 0 THEN 'DOUBLE'\n"
    2495             :                                         "      WHEN tscale = 0 THEN 'FLOAT(' || digits || ')'\n"
    2496             :                                         "      ELSE 'FLOAT(' || digits || ',' || tscale || ')'\n"
    2497             :                                         "    END\n"
    2498             :                                         "      WHEN 'geometry' THEN\n"
    2499             :                                         "    CASE digits\n"
    2500             :                                         "      WHEN 4 THEN 'GEOMETRY(POINT' ||\n"
    2501             :                                         "            CASE tscale\n"
    2502             :                                         "              WHEN 0 THEN ''\n"
    2503             :                                         "              ELSE ',' || tscale\n"
    2504             :                                         "            END || ')'\n"
    2505             :                                         "      WHEN 8 THEN 'GEOMETRY(LINESTRING' ||\n"
    2506             :                                         "            CASE tscale\n"
    2507             :                                         "              WHEN 0 THEN ''\n"
    2508             :                                         "              ELSE ',' || tscale\n"
    2509             :                                         "            END || ')'\n"
    2510             :                                         "      WHEN 16 THEN 'GEOMETRY(POLYGON' ||\n"
    2511             :                                         "            CASE tscale\n"
    2512             :                                         "              WHEN 0 THEN ''\n"
    2513             :                                         "              ELSE ',' || tscale\n"
    2514             :                                         "            END || ')'\n"
    2515             :                                         "      WHEN 20 THEN 'GEOMETRY(MULTIPOINT' ||\n"
    2516             :                                         "            CASE tscale\n"
    2517             :                                         "              WHEN 0 THEN ''\n"
    2518             :                                         "              ELSE ',' || tscale\n"
    2519             :                                         "            END || ')'\n"
    2520             :                                         "      WHEN 24 THEN 'GEOMETRY(MULTILINESTRING' ||\n"
    2521             :                                         "            CASE tscale\n"
    2522             :                                         "              WHEN 0 THEN ''\n"
    2523             :                                         "              ELSE ',' || tscale\n"
    2524             :                                         "            END || ')'\n"
    2525             :                                         "      WHEN 28 THEN 'GEOMETRY(MULTIPOLYGON' ||\n"
    2526             :                                         "            CASE tscale\n"
    2527             :                                         "              WHEN 0 THEN ''\n"
    2528             :                                         "              ELSE ',' || tscale\n"
    2529             :                                         "            END || ')'\n"
    2530             :                                         "      WHEN 32 THEN 'GEOMETRY(GEOMETRYCOLLECTION' ||\n"
    2531             :                                         "            CASE tscale\n"
    2532             :                                         "              WHEN 0 THEN ''\n"
    2533             :                                         "              ELSE ',' || tscale\n"
    2534             :                                         "            END || ')'\n"
    2535             :                                         "      ELSE 'GEOMETRY'\n"
    2536             :                                         "        END\n"
    2537             :                                         "      WHEN 'hugeint' THEN 'HUGEINT'\n"
    2538             :                                         "      WHEN 'int' THEN 'INTEGER'\n"
    2539             :                                         "      WHEN 'month_interval' THEN\n"
    2540             :                                         "    CASE digits\n"
    2541             :                                         "      WHEN 1 THEN 'INTERVAL YEAR'\n"
    2542             :                                         "      WHEN 2 THEN 'INTERVAL YEAR TO MONTH'\n"
    2543             :                                         "      WHEN 3 THEN 'INTERVAL MONTH'\n"
    2544             :                                         "    END\n"
    2545             :                                         "      WHEN 'real' THEN\n"
    2546             :                                         "    CASE\n"
    2547             :                                         "      WHEN digits = 24 and tscale = 0 THEN 'REAL'\n"
    2548             :                                         "      WHEN tscale = 0 THEN 'FLOAT(' || digits || ')'\n"
    2549             :                                         "      ELSE 'FLOAT(' || digits || ',' || tscale || ')'\n"
    2550             :                                         "    END\n"
    2551             :                                         "      WHEN 'sec_interval' THEN\n"
    2552             :                                         "    CASE digits\n"
    2553             :                                         "      WHEN 4 THEN 'INTERVAL DAY'\n"
    2554             :                                         "      WHEN 5 THEN 'INTERVAL DAY TO HOUR'\n"
    2555             :                                         "      WHEN 6 THEN 'INTERVAL DAY TO MINUTE'\n"
    2556             :                                         "      WHEN 7 THEN 'INTERVAL DAY TO SECOND'\n"
    2557             :                                         "      WHEN 8 THEN 'INTERVAL HOUR'\n"
    2558             :                                         "      WHEN 9 THEN 'INTERVAL HOUR TO MINUTE'\n"
    2559             :                                         "      WHEN 10 THEN 'INTERVAL HOUR TO SECOND'\n"
    2560             :                                         "      WHEN 11 THEN 'INTERVAL MINUTE'\n"
    2561             :                                         "      WHEN 12 THEN 'INTERVAL MINUTE TO SECOND'\n"
    2562             :                                         "      WHEN 13 THEN 'INTERVAL SECOND'\n"
    2563             :                                         "    END\n"
    2564             :                                         "      WHEN 'smallint' THEN 'SMALLINT'\n"
    2565             :                                         "      WHEN 'time' THEN\n"
    2566             :                                         "    CASE digits\n"
    2567             :                                         "      WHEN 1 THEN 'TIME'\n"
    2568             :                                         "      ELSE 'TIME(' || (digits - 1) || ')'\n"
    2569             :                                         "    END\n"
    2570             :                                         "      WHEN 'timestamp' THEN\n"
    2571             :                                         "    CASE digits\n"
    2572             :                                         "      WHEN 7 THEN 'TIMESTAMP'\n"
    2573             :                                         "      ELSE 'TIMESTAMP(' || (digits - 1) || ')'\n"
    2574             :                                         "    END\n"
    2575             :                                         "      WHEN 'timestamptz' THEN\n"
    2576             :                                         "    CASE digits\n"
    2577             :                                         "      WHEN 7 THEN 'TIMESTAMP'\n"
    2578             :                                         "      ELSE 'TIMESTAMP(' || (digits - 1) || ')'\n"
    2579             :                                         "    END || ' WITH TIME ZONE'\n"
    2580             :                                         "      WHEN 'timetz' THEN\n"
    2581             :                                         "    CASE digits\n"
    2582             :                                         "      WHEN 1 THEN 'TIME'\n"
    2583             :                                         "      ELSE 'TIME(' || (digits - 1) || ')'\n"
    2584             :                                         "    END || ' WITH TIME ZONE'\n"
    2585             :                                         "      WHEN 'tinyint' THEN 'TINYINT'\n"
    2586             :                                         "      WHEN 'varchar' THEN 'CHARACTER VARYING(' || digits || ')'\n"
    2587             :                                         "      ELSE\n"
    2588             :                                         "        CASE\n"
    2589             :                                         "          WHEN lower(ctype) = ctype THEN upper(ctype)\n"
    2590             :                                         "          ELSE '\"' || ctype || '\"'\n"
    2591             :                                         "        END || CASE digits\n"
    2592             :                                         "      WHEN 0 THEN ''\n"
    2593             :                                         "          ELSE '(' || digits || CASE tscale\n"
    2594             :                                         "        WHEN 0 THEN ''\n"
    2595             :                                         "            ELSE ',' || tscale\n"
    2596             :                                         "          END || ')'\n"
    2597             :                                         "    END\n"
    2598             :                                         "    END;\n"
    2599             :                                         "END;\n"
    2600             :                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN ' ''' || sys.replace(s,'''','''''') || ''' '; END;\n"
    2601             :                                         "CREATE FUNCTION sys.DQ (s STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(s,'\"','\"\"') || '\"'; END; --TODO: Figure out why this breaks with the space\n"
    2602             :                                         "CREATE FUNCTION sys.FQN(s STRING, t STRING) RETURNS STRING BEGIN RETURN sys.DQ(s) || '.' || sys.DQ(t); END;\n"
    2603             :                                         "CREATE FUNCTION sys.ALTER_TABLE(s STRING, t STRING) RETURNS STRING BEGIN RETURN 'ALTER TABLE ' || sys.FQN(s, t) || ' '; END;\n"
    2604             :                                         "--We need pcre to implement a header guard which means adding the schema of an object explicitely to its identifier.\n"
    2605             :                                         "CREATE FUNCTION sys.replace_first(ori STRING, pat STRING, rep STRING, flg STRING) RETURNS STRING EXTERNAL NAME \"pcre\".\"replace_first\";\n"
    2606             :                                         "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
    2607             :                                         "RETURN\n"
    2608             :                                         "    SELECT sys.replace_first(stmt, '(\\\\s*\"?' || sch ||  '\"?\\\\s*\\\\.|)\\\\s*\"?' || nme || '\"?\\\\s*', ' ' || sys.FQN(sch, nme) || ' ', 'imsx');\n"
    2609             :                                         "END;\n"
    2610             :                                         "CREATE VIEW sys.describe_constraints AS\n"
    2611             :                                         "    SELECT\n"
    2612             :                                         "        s.name sch,\n"
    2613             :                                         "        t.name tbl,\n"
    2614             :                                         "        kc.name col,\n"
    2615             :                                         "        k.name con,\n"
    2616             :                                         "        CASE WHEN k.type = 0 THEN 'PRIMARY KEY' WHEN k.type = 1 THEN 'UNIQUE' END tpe\n"
    2617             :                                         "    FROM sys.schemas s, sys._tables t, sys.objects kc, sys.keys k\n"
    2618             :                                         "    WHERE kc.id = k.id\n"
    2619             :                                         "        AND k.table_id = t.id\n"
    2620             :                                         "        AND s.id = t.schema_id\n"
    2621             :                                         "        AND t.system = FALSE\n"
    2622             :                                         "        AND k.type in (0, 1)\n"
    2623             :                                         "        AND t.type IN (0, 6);\n"
    2624             :                                         "CREATE VIEW sys.describe_indices AS\n"
    2625             :                                         "    WITH it (id, idx) AS (VALUES (0, 'INDEX'), (4, 'IMPRINTS INDEX'), (5, 'ORDERED INDEX')) --UNIQUE INDEX wraps to INDEX.\n"
    2626             :                                         "    SELECT\n"
    2627             :                                         "        i.name ind,\n"
    2628             :                                         "        s.name sch,\n"
    2629             :                                         "        t.name tbl,\n"
    2630             :                                         "        c.name col,\n"
    2631             :                                         "        it.idx tpe\n"
    2632             :                                         "    FROM\n"
    2633             :                                         "        sys.idxs AS i LEFT JOIN sys.keys AS k ON i.name = k.name,\n"
    2634             :                                         "        sys.objects AS kc,\n"
    2635             :                                         "        sys._columns AS c,\n"
    2636             :                                         "        sys.schemas s,\n"
    2637             :                                         "        sys._tables AS t,\n"
    2638             :                                         "        it\n"
    2639             :                                         "    WHERE\n"
    2640             :                                         "        i.table_id = t.id\n"
    2641             :                                         "        AND i.id = kc.id\n"
    2642             :                                         "        AND kc.name = c.name\n"
    2643             :                                         "        AND t.id = c.table_id\n"
    2644             :                                         "        AND t.schema_id = s.id\n"
    2645             :                                         "        AND k.type IS NULL\n"
    2646             :                                         "        AND i.type = it.id\n"
    2647             :                                         "    ORDER BY i.name, kc.nr;\n"
    2648             :                                         "CREATE VIEW sys.describe_column_defaults AS\n"
    2649             :                                         "    SELECT\n"
    2650             :                                         "        s.name sch,\n"
    2651             :                                         "        t.name tbl,\n"
    2652             :                                         "        c.name col,\n"
    2653             :                                         "        c.\"default\" def\n"
    2654             :                                         "    FROM sys.schemas s, sys.tables t, sys.columns c\n"
    2655             :                                         "    WHERE\n"
    2656             :                                         "        s.id = t.schema_id AND\n"
    2657             :                                         "        t.id = c.table_id AND\n"
    2658             :                                         "        s.name <> 'tmp' AND\n"
    2659             :                                         "        NOT t.system AND\n"
    2660             :                                         "        c.\"default\" IS NOT NULL;\n"
    2661             :                                         "CREATE VIEW sys.describe_foreign_keys AS\n"
    2662             :                                         "        WITH action_type (id, act) AS (VALUES\n"
    2663             :                                         "            (0, 'NO ACTION'),\n"
    2664             :                                         "            (1, 'CASCADE'),\n"
    2665             :                                         "            (2, 'RESTRICT'),\n"
    2666             :                                         "            (3, 'SET NULL'),\n"
    2667             :                                         "            (4, 'SET DEFAULT'))\n"
    2668             :                                         "        SELECT\n"
    2669             :                                         "            fs.name fk_s,\n"
    2670             :                                         "            fkt.name fk_t,\n"
    2671             :                                         "            fkkc.name fk_c,\n"
    2672             :                                         "            fkkc.nr o,\n"
    2673             :                                         "            fkk.name fk,\n"
    2674             :                                         "            ps.name pk_s,\n"
    2675             :                                         "            pkt.name pk_t,\n"
    2676             :                                         "            pkkc.name pk_c,\n"
    2677             :                                         "            ou.act on_update,\n"
    2678             :                                         "            od.act on_delete\n"
    2679             :                                         "        FROM sys._tables fkt,\n"
    2680             :                                         "            sys.objects fkkc,\n"
    2681             :                                         "            sys.keys fkk,\n"
    2682             :                                         "            sys._tables pkt,\n"
    2683             :                                         "            sys.objects pkkc,\n"
    2684             :                                         "            sys.keys pkk,\n"
    2685             :                                         "            sys.schemas ps,\n"
    2686             :                                         "            sys.schemas fs,\n"
    2687             :                                         "            action_type ou,\n"
    2688             :                                         "            action_type od\n"
    2689             :                                         "        WHERE fkt.id = fkk.table_id\n"
    2690             :                                         "        AND pkt.id = pkk.table_id\n"
    2691             :                                         "        AND fkk.id = fkkc.id\n"
    2692             :                                         "        AND pkk.id = pkkc.id\n"
    2693             :                                         "        AND fkk.rkey = pkk.id\n"
    2694             :                                         "        AND fkkc.nr = pkkc.nr\n"
    2695             :                                         "        AND pkt.schema_id = ps.id\n"
    2696             :                                         "        AND fkt.schema_id = fs.id\n"
    2697             :                                         "        AND (fkk.\"action\" & 255)         = od.id\n"
    2698             :                                         "        AND ((fkk.\"action\" >> 8) & 255)  = ou.id\n"
    2699             :                                         "        ORDER BY fkk.name, fkkc.nr;\n"
    2700             :                                         "--TODO: CRASHES when this function gets inlined into describe_tables\n"
    2701             :                                         "CREATE FUNCTION sys.get_merge_table_partition_expressions(tid INT) RETURNS STRING\n"
    2702             :                                         "BEGIN\n"
    2703             :                                         "    RETURN\n"
    2704             :                                         "        SELECT\n"
    2705             :                                         "            CASE WHEN tp.table_id IS NOT NULL THEN    --updatable merge table\n"
    2706             :                                         "                ' PARTITION BY ' ||\n"
    2707             :                                         "                CASE\n"
    2708             :                                         "                    WHEN bit_and(tp.type, 2) = 2\n"
    2709             :                                         "                    THEN 'VALUES '\n"
    2710             :                                         "                    ELSE 'RANGE '\n"
    2711             :                                         "                END ||\n"
    2712             :                                         "                CASE\n"
    2713             :                                         "                    WHEN bit_and(tp.type, 4) = 4 --column expression\n"
    2714             :                                         "                    THEN 'ON ' || '(' || (SELECT sys.DQ(c.name) || ')' FROM sys.columns c WHERE c.id = tp.column_id)\n"
    2715             :                                         "                    ELSE 'USING ' || '(' || tp.expression || ')' --generic expression\n"
    2716             :                                         "                END\n"
    2717             :                                         "            ELSE                                    --read only partition merge table.\n"
    2718             :                                         "                ''\n"
    2719             :                                         "            END\n"
    2720             :                                         "        FROM (VALUES (tid)) t(id) LEFT JOIN sys.table_partitions tp ON t.id = tp.table_id;\n"
    2721             :                                         "END;\n"
    2722             :                                         "--TODO: gives mergejoin errors when inlined\n"
    2723             :                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    2724             :                                         "    RETURN SELECT ' ON ' || sys.SQ(uri) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(\"hash\") FROM sys.remote_table_credentials(s ||'.' || t);\n"
    2725             :                                         "END;\n"
    2726             :                                         "CREATE VIEW sys.describe_tables AS\n"
    2727             :                                         "    SELECT\n"
    2728             :                                         "        t.id o,\n"
    2729             :                                         "        s.name sch,\n"
    2730             :                                         "        t.name tab,\n"
    2731             :                                         "        ts.table_type_name typ,\n"
    2732             :                                         "        (SELECT\n"
    2733             :                                         "            ' (' ||\n"
    2734             :                                         "            GROUP_CONCAT(\n"
    2735             :                                         "                sys.DQ(c.name) || ' ' ||\n"
    2736             :                                         "                sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    2737             :                                         "                ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    2738             :                                         "            , ', ') || ')'\n"
    2739             :                                         "        FROM sys._columns c\n"
    2740             :                                         "        WHERE c.table_id = t.id) col,\n"
    2741             :                                         "        CASE\n"
    2742             :                                         "            WHEN ts.table_type_name = 'REMOTE TABLE' THEN\n"
    2743             :                                         "                sys.get_remote_table_expressions(s.name, t.name)\n"
    2744             :                                         "            WHEN ts.table_type_name = 'MERGE TABLE' THEN\n"
    2745             :                                         "                sys.get_merge_table_partition_expressions(t.id)\n"
    2746             :                                         "            WHEN ts.table_type_name = 'VIEW' THEN\n"
    2747             :                                         "                sys.schema_guard(s.name, t.name, t.query)\n"
    2748             :                                         "            ELSE\n"
    2749             :                                         "                ''\n"
    2750             :                                         "        END opt\n"
    2751             :                                         "    FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    2752             :                                         "    WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE')\n"
    2753             :                                         "        AND t.system = FALSE\n"
    2754             :                                         "        AND s.id = t.schema_id\n"
    2755             :                                         "        AND ts.table_type_id = t.type\n"
    2756             :                                         "        AND s.name <> 'tmp';\n"
    2757             :                                         "CREATE VIEW sys.describe_triggers AS\n"
    2758             :                                         "        SELECT\n"
    2759             :                                         "            s.name sch,\n"
    2760             :                                         "            t.name tab,\n"
    2761             :                                         "            tr.name tri,\n"
    2762             :                                         "            tr.statement def\n"
    2763             :                                         "        FROM sys.schemas s, sys.tables t, sys.triggers tr\n"
    2764             :                                         "        WHERE s.id = t.schema_id AND t.id = tr.table_id AND NOT t.system;\n"
    2765             :                                         "CREATE VIEW sys.describe_comments AS\n"
    2766             :                                         "        SELECT\n"
    2767             :                                         "            o.id id,\n"
    2768             :                                         "            o.tpe tpe,\n"
    2769             :                                         "            o.nme fqn,\n"
    2770             :                                         "            c.remark rem\n"
    2771             :                                         "        FROM (\n"
    2772             :                                         "            SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas\n"
    2773             :                                         "            UNION ALL\n"
    2774             :                                         "            SELECT t.id, CASE WHEN ts.table_type_name = 'VIEW' THEN 'VIEW' ELSE 'TABLE' END, sys.FQN(s.name, t.name)\n"
    2775             :                                         "            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"
    2776             :                                         "            WHERE NOT s.name <> 'tmp'\n"
    2777             :                                         "            UNION ALL\n"
    2778             :                                         "            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"
    2779             :                                         "            UNION ALL\n"
    2780             :                                         "            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"
    2781             :                                         "            UNION ALL\n"
    2782             :                                         "            SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    2783             :                                         "            UNION ALL\n"
    2784             :                                         "            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"
    2785             :                                         "            ) AS o(id, tpe, nme)\n"
    2786             :                                         "            JOIN sys.comments c ON c.id = o.id;\n"
    2787             :                                         "CREATE VIEW sys.fully_qualified_functions AS\n"
    2788             :                                         "    WITH fqn(id, tpe, sig, num) AS\n"
    2789             :                                         "    (\n"
    2790             :                                         "        SELECT\n"
    2791             :                                         "            f.id,\n"
    2792             :                                         "            ft.function_type_keyword,\n"
    2793             :                                         "            CASE WHEN a.type IS NULL THEN\n"
    2794             :                                         "                s.name || '.' || f.name || '()'\n"
    2795             :                                         "            ELSE\n"
    2796             :                                         "                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"
    2797             :                                         "            END,\n"
    2798             :                                         "            a.number\n"
    2799             :                                         "        FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
    2800             :                                         "        WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
    2801             :                                         "    )\n"
    2802             :                                         "    SELECT\n"
    2803             :                                         "        fqn1.id id,\n"
    2804             :                                         "        fqn1.tpe tpe,\n"
    2805             :                                         "        fqn1.sig nme\n"
    2806             :                                         "    FROM\n"
    2807             :                                         "        fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id)  fqn2(id, num)\n"
    2808             :                                         "        ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
    2809             :                                         "CREATE VIEW sys.describe_privileges AS\n"
    2810             :                                         "    SELECT\n"
    2811             :                                         "        CASE\n"
    2812             :                                         "            WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
    2813             :                                         "                'COPY FROM'\n"
    2814             :                                         "            WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
    2815             :                                         "                'COPY INTO'\n"
    2816             :                                         "            ELSE\n"
    2817             :                                         "                o.nme\n"
    2818             :                                         "        END o_nme,\n"
    2819             :                                         "        CASE\n"
    2820             :                                         "            WHEN o.tpe IS NOT NULL THEN\n"
    2821             :                                         "                o.tpe\n"
    2822             :                                         "            ELSE\n"
    2823             :                                         "                'GLOBAL'\n"
    2824             :                                         "        END o_tpe,\n"
    2825             :                                         "        pc.privilege_code_name p_nme,\n"
    2826             :                                         "        a.name a_nme,\n"
    2827             :                                         "        g.name g_nme,\n"
    2828             :                                         "        p.grantable grantable\n"
    2829             :                                         "    FROM\n"
    2830             :                                         "        sys.privileges p LEFT JOIN\n"
    2831             :                                         "        (\n"
    2832             :                                         "        SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
    2833             :                                         "            from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
    2834             :                                         "        UNION ALL\n"
    2835             :                                         "            SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
    2836             :                                         "            FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
    2837             :                                         "        UNION ALL\n"
    2838             :                                         "            SELECT f.id, f.nme, f.tpe\n"
    2839             :                                         "            FROM sys.fully_qualified_functions f\n"
    2840             :                                         "        ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
    2841             :                                         "        sys.privilege_codes pc,\n"
    2842             :                                         "        auths a, auths g\n"
    2843             :                                         "    WHERE\n"
    2844             :                                         "        p.privileges = pc.privilege_code_id AND\n"
    2845             :                                         "        p.auth_id = a.id AND\n"
    2846             :                                         "        p.grantor = g.id;\n"
    2847             :                                         "CREATE FUNCTION sys.describe_table(schemaName string, tableName string)\n"
    2848             :                                         "  RETURNS TABLE(name string, query string, type string, id integer, remark string)\n"
    2849             :                                         "BEGIN\n"
    2850             :                                         "    RETURN SELECT t.name, t.query, tt.table_type_name, t.id, c.remark\n"
    2851             :                                         "        FROM sys.schemas s, sys.table_types tt, sys._tables t\n"
    2852             :                                         "        LEFT OUTER JOIN sys.comments c ON t.id = c.id\n"
    2853             :                                         "            WHERE s.name = schemaName\n"
    2854             :                                         "            AND t.schema_id = s.id\n"
    2855             :                                         "            AND t.name = tableName\n"
    2856             :                                         "            AND t.type = tt.table_type_id;\n"
    2857             :                                         "END;\n"
    2858             :                                         "CREATE VIEW sys.describe_user_defined_types AS\n"
    2859             :                                         "    SELECT\n"
    2860             :                                         "        s.name sch,\n"
    2861             :                                         "        t.sqlname sql_tpe,\n"
    2862             :                                         "        t.systemname ext_tpe\n"
    2863             :                                         "    FROM sys.types t JOIN sys.schemas s ON t.schema_id = s.id\n"
    2864             :                                         "    WHERE\n"
    2865             :                                         "        t.eclass = 18 AND\n"
    2866             :                                         "        (\n"
    2867             :                                         "            (s.name = 'sys' AND t.sqlname not in ('geometrya', 'mbr', 'url', 'inet', 'json', 'uuid', 'xml')) OR\n"
    2868             :                                         "            (s.name <> 'sys')\n"
    2869             :                                         "        );\n"
    2870             :                                         "CREATE VIEW sys.describe_partition_tables AS\n"
    2871             :                                         "    SELECT \n"
    2872             :                                         "        m_sch,\n"
    2873             :                                         "        m_tbl,\n"
    2874             :                                         "        p_sch,\n"
    2875             :                                         "        p_tbl,\n"
    2876             :                                         "        CASE\n"
    2877             :                                         "            WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    2878             :                                         "            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"
    2879             :                                         "            ELSE p_raw_type\n"
    2880             :                                         "        END AS tpe,\n"
    2881             :                                         "        pvalues,\n"
    2882             :                                         "        minimum,\n"
    2883             :                                         "        maximum,\n"
    2884             :                                         "        with_nulls\n"
    2885             :                                         "    FROM \n"
    2886             :                                         "    (WITH\n"
    2887             :                                         "        tp(\"type\", table_id) AS\n"
    2888             :                                         "        (SELECT CASE WHEN (table_partitions.\"type\" & 2) = 2 THEN 'VALUES' ELSE 'RANGE' END, table_partitions.table_id FROM sys.table_partitions),\n"
    2889             :                                         "        subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    2890             :                                         "        (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    2891             :                                         "        FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    2892             :                                         "        WHERE m_t.\"type\" IN (3, 6)\n"
    2893             :                                         "            AND m_t.schema_id = m_s.id\n"
    2894             :                                         "            AND m_s.name <> 'tmp'\n"
    2895             :                                         "            AND m_t.system = FALSE\n"
    2896             :                                         "            AND m_t.id = d.depend_id\n"
    2897             :                                         "            AND d.id = p_m.id\n"
    2898             :                                         "            AND p_m.schema_id = p_s.id\n"
    2899             :                                         "        ORDER BY m_t.id, p_m.id)\n"
    2900             :                                         "    SELECT\n"
    2901             :                                         "        subq.m_sch,\n"
    2902             :                                         "        subq.m_tbl,\n"
    2903             :                                         "        subq.p_sch,\n"
    2904             :                                         "        subq.p_tbl,\n"
    2905             :                                         "        tp.\"type\" AS p_raw_type,\n"
    2906             :                                         "        CASE WHEN tp.\"type\" = 'VALUES'\n"
    2907             :                                         "            THEN (SELECT GROUP_CONCAT(vp.value, ',')FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid)\n"
    2908             :                                         "            ELSE NULL\n"
    2909             :                                         "        END AS pvalues,\n"
    2910             :                                         "        CASE WHEN tp.\"type\" = 'RANGE'\n"
    2911             :                                         "            THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2912             :                                         "            ELSE NULL\n"
    2913             :                                         "        END AS minimum,\n"
    2914             :                                         "        CASE WHEN tp.\"type\" = 'RANGE'\n"
    2915             :                                         "            THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2916             :                                         "            ELSE NULL\n"
    2917             :                                         "        END AS maximum,\n"
    2918             :                                         "        CASE WHEN tp.\"type\" = 'VALUES'\n"
    2919             :                                         "            THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    2920             :                                         "            ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    2921             :                                         "        END AS with_nulls\n"
    2922             :                                         "    FROM \n"
    2923             :                                         "        subq LEFT OUTER JOIN tp\n"
    2924             :                                         "        ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    2925             :                                         "CREATE VIEW sys.describe_sequences AS\n"
    2926             :                                         "    SELECT\n"
    2927             :                                         "        s.name as sch,\n"
    2928             :                                         "        seq.name as seq,\n"
    2929             :                                         "        seq.\"start\" s,\n"
    2930             :                                         "        get_value_for(s.name, seq.name) AS rs,\n"
    2931             :                                         "        seq.\"minvalue\" mi,\n"
    2932             :                                         "        seq.\"maxvalue\" ma,\n"
    2933             :                                         "        seq.\"increment\" inc,\n"
    2934             :                                         "        seq.\"cacheinc\" cache,\n"
    2935             :                                         "        seq.\"cycle\" cycle\n"
    2936             :                                         "    FROM sys.sequences seq, sys.schemas s\n"
    2937             :                                         "    WHERE s.id = seq.schema_id\n"
    2938             :                                         "    AND s.name <> 'tmp'\n"
    2939             :                                         "    ORDER BY s.name, seq.name;\n"
    2940             :                                         "CREATE VIEW sys.describe_functions AS\n"
    2941             :                                         "    SELECT\n"
    2942             :                                         "        f.id o,\n"
    2943             :                                         "        s.name sch,\n"
    2944             :                                         "        f.name fun,\n"
    2945             :                                         "        f.func def\n"
    2946             :                                         "    FROM sys.functions f JOIN sys.schemas s ON f.schema_id = s.id WHERE s.name <> 'tmp' AND NOT f.system;\n"
    2947             :                                         "CREATE FUNCTION sys.describe_columns(schemaName string, tableName string)\n"
    2948             :                                         "    RETURNS TABLE(name string, type string, digits integer, scale integer, Nulls boolean, cDefault string, number integer, sqltype string, remark string)\n"
    2949             :                                         "BEGIN\n"
    2950             :                                         "    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"
    2951             :                                         "        FROM sys._tables t, sys.schemas s, sys._columns c\n"
    2952             :                                         "        LEFT OUTER JOIN sys.comments com ON c.id = com.id\n"
    2953             :                                         "            WHERE c.table_id = t.id\n"
    2954             :                                         "            AND t.name = tableName\n"
    2955             :                                         "            AND t.schema_id = s.id\n"
    2956             :                                         "            AND s.name = schemaName\n"
    2957             :                                         "        ORDER BY c.number;\n"
    2958             :                                         "END;\n"
    2959             :                                         "CREATE FUNCTION sys.describe_function(schemaName string, functionName string)\n"
    2960             :                                         "    RETURNS TABLE(id integer, name string, type string, language string, remark string)\n"
    2961             :                                         "BEGIN\n"
    2962             :                                         "    RETURN SELECT f.id, f.name, ft.function_type_keyword, fl.language_keyword, c.remark\n"
    2963             :                                         "        FROM sys.functions f\n"
    2964             :                                         "        JOIN sys.schemas s ON f.schema_id = s.id\n"
    2965             :                                         "        JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
    2966             :                                         "        LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
    2967             :                                         "        LEFT OUTER JOIN sys.comments c ON f.id = c.id\n"
    2968             :                                         "        WHERE f.name=functionName AND s.name = schemaName;\n"
    2969             :                                         "END;\n");
    2970             : 
    2971             :                         /* 75_storagemodel.sql not changed but dependencies changed
    2972             :                          * since sys.objects has a new column */
    2973           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    2974             :                                         "drop procedure sys.storagemodelinit();\n"
    2975             :                                         "create procedure sys.storagemodelinit()\n"
    2976             :                                         "begin\n"
    2977             :                                         "    delete from sys.storagemodelinput;\n"
    2978             :                                         "    insert into sys.storagemodelinput\n"
    2979             :                                         "    select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
    2980             :                                         "        -- assume all variable size types contain distinct values\n"
    2981             :                                         "        case when (\"unique\" or \"type\" IN ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
    2982             :                                         "            then \"count\" else 0 end,\n"
    2983             :                                         "        case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
    2984             :                                         "            -- string heaps have a header of 8192\n"
    2985             :                                         "            then cast((heapsize - 8192) / \"count\" as bigint)\n"
    2986             :                                         "        when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
    2987             :                                         "            -- binary data heaps have a header of 32\n"
    2988             :                                         "            then cast((heapsize - 32) / \"count\" as bigint)\n"
    2989             :                                         "        else typewidth end,\n"
    2990             :                                         "        FALSE, case sorted when true then true else false end, \"unique\", TRUE\n"
    2991             :                                         "      from sys.\"storage\";  -- view sys.\"storage\" excludes system tables (as those are not useful to be modeled for storagesize by application users)\n"
    2992             :                                         "    update sys.storagemodelinput\n"
    2993             :                                         "       set reference = TRUE\n"
    2994             :                                         "     where (\"schema\", \"table\", \"column\") in (\n"
    2995             :                                         "        SELECT fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
    2996             :                                         "          FROM    sys.\"keys\" AS fkkey,\n"
    2997             :                                         "            sys.\"objects\" AS fkkeycol,\n"
    2998             :                                         "            sys.\"tables\" AS fktable,\n"
    2999             :                                         "            sys.\"schemas\" AS fkschema\n"
    3000             :                                         "        WHERE fktable.\"id\" = fkkey.\"table_id\"\n"
    3001             :                                         "          AND fkkey.\"id\" = fkkeycol.\"id\"\n"
    3002             :                                         "          AND fkschema.\"id\" = fktable.\"schema_id\"\n"
    3003             :                                         "          AND fkkey.\"rkey\" > -1 );\n"
    3004             :                                         "    update sys.storagemodelinput\n"
    3005             :                                         "       set isacolumn = FALSE\n"
    3006             :                                         "     where (\"schema\", \"table\", \"column\") NOT in (\n"
    3007             :                                         "        SELECT sch.\"name\", tbl.\"name\", col.\"name\"\n"
    3008             :                                         "          FROM sys.\"schemas\" AS sch,\n"
    3009             :                                         "            sys.\"tables\" AS tbl,\n"
    3010             :                                         "            sys.\"columns\" AS col\n"
    3011             :                                         "        WHERE sch.\"id\" = tbl.\"schema_id\"\n"
    3012             :                                         "          AND tbl.\"id\" = col.\"table_id\");\n"
    3013             :                                         "end;\n"
    3014             :                                         "update sys.functions set system = true where name = 'storagemodelinit' and schema_id = 2000;\n");
    3015             : 
    3016             :                         /* 76_dump.sql */
    3017           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    3018             :                                         "CREATE VIEW sys.dump_create_roles AS\n"
    3019             :                                         "    SELECT\n"
    3020             :                                         "        'CREATE ROLE ' || sys.dq(name) || ';' stmt FROM sys.auths\n"
    3021             :                                         "    WHERE name NOT IN (SELECT name FROM sys.db_user_info)\n"
    3022             :                                         "    AND grantor <> 0;\n"
    3023             :                                         "CREATE VIEW sys.dump_create_users AS\n"
    3024             :                                         "    SELECT\n"
    3025             :                                         "        'CREATE USER ' ||  sys.dq(ui.name) ||  ' WITH ENCRYPTED PASSWORD ' ||\n"
    3026             :                                         "        sys.sq(sys.password_hash(ui.name)) ||\n"
    3027             :                                         "    ' NAME ' || sys.sq(ui.fullname) ||  ' SCHEMA sys;' stmt\n"
    3028             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3029             :                                         "    WHERE ui.default_schema = s.id\n"
    3030             :                                         "        AND ui.name <> 'monetdb'\n"
    3031             :                                         "        AND ui.name <> '.snapshot';\n"
    3032             :                                         "CREATE VIEW sys.dump_create_schemas AS\n"
    3033             :                                         "    SELECT\n"
    3034             :                                         "        'CREATE SCHEMA ' ||  sys.dq(s.name) || ifthenelse(a.name <> 'sysadmin', ' AUTHORIZATION ' || a.name, ' ') || ';' stmt\n"
    3035             :                                         "    FROM sys.schemas s, sys.auths a\n"
    3036             :                                         "    WHERE s.authorization = a.id AND s.system = FALSE;\n"
    3037             :                                         "CREATE VIEW sys.dump_add_schemas_to_users AS\n"
    3038             :                                         "    SELECT\n"
    3039             :                                         "        'ALTER USER ' || sys.dq(ui.name) || ' SET SCHEMA ' || sys.dq(s.name) || ';' stmt\n"
    3040             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3041             :                                         "    WHERE ui.default_schema = s.id\n"
    3042             :                                         "        AND ui.name <> 'monetdb'\n"
    3043             :                                         "        AND ui.name <> '.snapshot'\n"
    3044             :                                         "        AND s.name <> 'sys';\n"
    3045             :                                         "CREATE VIEW sys.dump_grant_user_privileges AS\n"
    3046             :                                         "    SELECT\n"
    3047             :                                         "        'GRANT ' || sys.dq(a2.name) || ' ' || ifthenelse(a1.name = 'public', 'PUBLIC', sys.dq(a1.name)) || ';' stmt\n"
    3048             :                                         "    FROM sys.auths a1, sys.auths a2, sys.user_role ur\n"
    3049             :                                         "    WHERE a1.id = ur.login_id AND a2.id = ur.role_id;\n"
    3050             :                                         "CREATE VIEW sys.dump_table_constraint_type AS\n"
    3051             :                                         "    SELECT\n"
    3052             :                                         "        'ALTER TABLE ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    3053             :                                         "        ' ADD CONSTRAINT ' || sys.DQ(con) || ' '||\n"
    3054             :                                         "        tpe || ' (' || GROUP_CONCAT(sys.DQ(col), ', ') || ');' stmt\n"
    3055             :                                         "    FROM sys.describe_constraints GROUP BY sch, tbl, con, tpe;\n"
    3056             :                                         "CREATE VIEW sys.dump_indices AS\n"
    3057             :                                         "    SELECT\n"
    3058             :                                         "        'CREATE ' || tpe || ' ' ||\n"
    3059             :                                         "        sys.DQ(ind) || ' ON ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    3060             :                                         "        '(' || GROUP_CONCAT(col) || ');' stmt\n"
    3061             :                                         "    FROM sys.describe_indices GROUP BY ind, tpe, sch, tbl;\n"
    3062             :                                         "CREATE VIEW sys.dump_column_defaults AS\n"
    3063             :                                         "    SELECT 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ALTER COLUMN ' || sys.DQ(col) || ' SET DEFAULT ' || def || ';' stmt\n"
    3064             :                                         "    FROM sys.describe_column_defaults;\n"
    3065             :                                         "CREATE VIEW sys.dump_foreign_keys AS\n"
    3066             :                                         "    SELECT\n"
    3067             :                                         "        'ALTER TABLE ' || sys.DQ(fk_s) || '.'|| sys.DQ(fk_t) || ' ADD CONSTRAINT ' || sys.DQ(fk) || ' ' ||\n"
    3068             :                                         "        'FOREIGN KEY(' || GROUP_CONCAT(sys.DQ(fk_c), ',') ||') ' ||\n"
    3069             :                                         "        'REFERENCES ' || sys.DQ(pk_s) || '.' || sys.DQ(pk_t) || '(' || GROUP_CONCAT(sys.DQ(pk_c), ',') || ') ' ||\n"
    3070             :                                         "        'ON DELETE ' || on_delete || ' ON UPDATE ' || on_update ||\n"
    3071             :                                         "        ';' stmt\n"
    3072             :                                         "    FROM sys.describe_foreign_keys GROUP BY fk_s, fk_t, pk_s, pk_t, fk, on_delete, on_update;\n"
    3073             :                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    3074             :                                         "    SELECT\n"
    3075             :                                         "        sys.ALTER_TABLE(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    3076             :                                         "        CASE \n"
    3077             :                                         "            WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    3078             :                                         "            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"
    3079             :                                         "            WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    3080             :                                         "            ELSE '' --'READ ONLY'\n"
    3081             :                                         "        END ||\n"
    3082             :                                         "        CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    3083             :                                         "        ';' stmt\n"
    3084             :                                         "    FROM sys.describe_partition_tables;\n"
    3085             :                                         "CREATE VIEW sys.dump_sequences AS\n"
    3086             :                                         "    SELECT\n"
    3087             :                                         "        'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT ' ||\n"
    3088             :                                         "        CASE WHEN \"s\" <> 0 THEN 'START WITH ' || \"rs\" ELSE '' END ||\n"
    3089             :                                         "        CASE WHEN \"inc\" <> 1 THEN ' INCREMENT BY ' || \"inc\" ELSE '' END ||\n"
    3090             :                                         "        CASE WHEN \"mi\" <> 0 THEN ' MINVALUE ' || \"mi\" ELSE '' END ||\n"
    3091             :                                         "        CASE WHEN \"ma\" <> 0 THEN ' MAXVALUE ' || \"ma\" ELSE '' END ||\n"
    3092             :                                         "        CASE WHEN \"cache\" <> 1 THEN ' CACHE ' || \"cache\" ELSE '' END ||\n"
    3093             :                                         "        CASE WHEN \"cycle\" THEN ' CYCLE' ELSE '' END || ';' stmt\n"
    3094             :                                         "    FROM sys.describe_sequences;\n"
    3095             :                                         "CREATE VIEW sys.dump_start_sequences AS\n"
    3096             :                                         "    SELECT\n"
    3097             :                                         "        'UPDATE sys.sequences seq SET start = ' || s  ||\n"
    3098             :                                         "        ' WHERE name = ' || sys.SQ(seq) ||\n"
    3099             :                                         "        ' AND schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = ' || sys.SQ(sch) || ');' stmt\n"
    3100             :                                         "    FROM sys.describe_sequences;\n"
    3101             :                                         "CREATE VIEW sys.dump_functions AS\n"
    3102             :                                         "    SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt FROM sys.describe_functions f;\n"
    3103             :                                         "CREATE VIEW sys.dump_tables AS\n"
    3104             :                                         "    SELECT\n"
    3105             :                                         "        t.o o,\n"
    3106             :                                         "        CASE\n"
    3107             :                                         "            WHEN t.typ <> 'VIEW' THEN\n"
    3108             :                                         "                'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    3109             :                                         "            ELSE\n"
    3110             :                                         "                t.opt\n"
    3111             :                                         "        END stmt\n"
    3112             :                                         "    FROM sys.describe_tables t;\n"
    3113             :                                         "CREATE VIEW sys.dump_triggers AS\n"
    3114             :                                         "    SELECT sys.schema_guard(sch, tab, def) stmt FROM sys.describe_triggers;\n"
    3115             :                                         "CREATE VIEW sys.dump_comments AS\n"
    3116             :                                         "    SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    3117             :                                         "CREATE VIEW sys.dump_user_defined_types AS\n"
    3118             :                                         "        SELECT 'CREATE TYPE ' || sys.FQN(sch, sql_tpe) || ' EXTERNAL NAME ' || sys.DQ(ext_tpe) || ';' stmt FROM sys.describe_user_defined_types;\n"
    3119             :                                         "CREATE VIEW sys.dump_privileges AS\n"
    3120             :                                         "    SELECT\n"
    3121             :                                         "        'INSERT INTO sys.privileges VALUES (' ||\n"
    3122             :                                         "            CASE\n"
    3123             :                                         "                WHEN dp.o_tpe = 'GLOBAL' THEN\n"
    3124             :                                         "                    '0,'\n"
    3125             :                                         "                WHEN dp.o_tpe = 'TABLE' THEN\n"
    3126             :                                         "                    '(SELECT t.id FROM sys.schemas s, sys.tables t WHERE s.id = t.schema_id' ||\n"
    3127             :                                         "                        ' AND s.name || ''.'' || t.name =' || sys.SQ(dp.o_nme) || '),'\n"
    3128             :                                         "                WHEN dp.o_tpe = 'COLUMN' THEN\n"
    3129             :                                         "                    '(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"
    3130             :                                         "                        ' AND s.name || ''.'' || t.name || ''.'' || c.name =' || sys.SQ(dp.o_nme) || '),'\n"
    3131             :                                         "                ELSE -- FUNCTION-LIKE\n"
    3132             :                                         "                    '(SELECT fqn.id FROM sys.fully_qualified_functions fqn WHERE' ||\n"
    3133             :                                         "                        ' fqn.nme = ' || sys.SQ(dp.o_nme) || ' AND fqn.tpe = ' || sys.SQ(dp.o_tpe) || '),'\n"
    3134             :                                         "            END ||\n"
    3135             :                                         "            '(SELECT id FROM sys.auths a WHERE a.name = ' || sys.SQ(dp.a_nme) || '),' ||\n"
    3136             :                                         "            '(SELECT pc.privilege_code_id FROM sys.privilege_codes pc WHERE pc.privilege_code_name = ' || sys.SQ(p_nme) || '),'\n"
    3137             :                                         "            '(SELECT id FROM sys.auths g WHERE g.name = ' || sys.SQ(dp.g_nme) || '),' ||\n"
    3138             :                                         "            dp.grantable ||\n"
    3139             :                                         "        ');' stmt\n"
    3140             :                                         "    FROM sys.describe_privileges dp;\n"
    3141             :                                         "CREATE PROCEDURE sys.EVAL(stmt STRING) EXTERNAL NAME sql.eval;\n"
    3142             :                                         "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"
    3143             :                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    3144             :                                         "BEGIN\n"
    3145             :                                         "    RETURN\n"
    3146             :                                         "        CASE\n"
    3147             :                                         "            WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    3148             :                                         "                'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    3149             :                                         "            ELSE\n"
    3150             :                                         "                'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    3151             :                                         "        END;\n"
    3152             :                                         "END;\n"
    3153             :                                         "CREATE TABLE sys.dump_statements(o INT, s STRING);\n"
    3154             :                                         "CREATE PROCEDURE sys._dump_table_data(sch STRING, tbl STRING) BEGIN\n"
    3155             :                                         "    DECLARE k INT;\n"
    3156             :                                         "    SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    3157             :                                         "    IF k IS NOT NULL THEN\n"
    3158             :                                         "        DECLARE cname STRING;\n"
    3159             :                                         "        DECLARE ctype STRING;\n"
    3160             :                                         "        SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    3161             :                                         "        SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    3162             :                                         "        DECLARE COPY_INTO_STMT STRING;\n"
    3163             :                                         "        DECLARE _cnt INT;\n"
    3164             :                                         "        SET _cnt = (SELECT MIN(s.count) FROM sys.storage() s WHERE s.schema = sch AND s.table = tbl);\n"
    3165             :                                         "        IF _cnt > 0 THEN\n"
    3166             :                                         "            SET COPY_INTO_STMT = 'COPY ' || _cnt ||  ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    3167             :                                         "            DECLARE SELECT_DATA_STMT STRING;\n"
    3168             :                                         "            SET SELECT_DATA_STMT = 'SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    3169             :                                         "            DECLARE M INT;\n"
    3170             :                                         "            SET M = (SELECT MAX(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    3171             :                                         "            WHILE (k < M) DO\n"
    3172             :                                         "                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"
    3173             :                                         "                SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    3174             :                                         "                SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    3175             :                                         "                SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    3176             :                                         "                SET SELECT_DATA_STMT = SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype);\n"
    3177             :                                         "            END WHILE;\n"
    3178             :                                         "            SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    3179             :                                         "            SET SELECT_DATA_STMT =  SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl);\n"
    3180             :                                         "            insert into sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    3181             :                                         "            CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    3182             :                                         "        END IF;\n"
    3183             :                                         "    END IF;\n"
    3184             :                                         "END;\n"
    3185             :                                         "CREATE PROCEDURE sys.dump_table_data() BEGIN\n"
    3186             :                                         "    DECLARE i INT;\n"
    3187             :                                         "    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"
    3188             :                                         "    IF i IS NOT NULL THEN\n"
    3189             :                                         "        DECLARE M INT;\n"
    3190             :                                         "        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"
    3191             :                                         "        DECLARE sch STRING;\n"
    3192             :                                         "        DECLARE tbl STRING;\n"
    3193             :                                         "        WHILE i < M DO\n"
    3194             :                                         "            set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3195             :                                         "            set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3196             :                                         "            CALL sys._dump_table_data(sch, tbl);\n"
    3197             :                                         "            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"
    3198             :                                         "        END WHILE;\n"
    3199             :                                         "        set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3200             :                                         "        set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    3201             :                                         "        CALL sys._dump_table_data(sch, tbl);\n"
    3202             :                                         "    END IF;\n"
    3203             :                                         "END;\n"
    3204             :                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    3205             :                                         "BEGIN\n"
    3206             :                                         "    SET SCHEMA sys;\n"
    3207             :                                         "    TRUNCATE sys.dump_statements;\n"
    3208             :                                         "    INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    3209             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'SET SCHEMA \"sys\";');\n"
    3210             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    3211             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    3212             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    3213             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    3214             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    3215             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    3216             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    3217             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    3218             :                                         "    --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
    3219             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    3220             :                                         "    FROM (\n"
    3221             :                                         "            SELECT * FROM sys.dump_functions f\n"
    3222             :                                         "            UNION\n"
    3223             :                                         "            SELECT * FROM sys.dump_tables t\n"
    3224             :                                         "        ) AS stmts(o, s);\n"
    3225             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    3226             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    3227             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    3228             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    3229             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    3230             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    3231             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    3232             :                                         "    --We are dumping ALL privileges so we need to erase existing privileges on the receiving side;\n"
    3233             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'TRUNCATE sys.privileges;');\n"
    3234             :                                         "    INSERT INTO sys.dump_statements SELECT  (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_privileges;\n"
    3235             :                                         "    IF NOT DESCRIBE THEN\n"
    3236             :                                         "        CALL sys.dump_table_data();\n"
    3237             :                                         "    END IF;\n"
    3238             :                                         "    INSERT INTO sys.dump_statements VALUES ( (SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    3239             :                                         "    RETURN sys.dump_statements;\n"
    3240             :                                         "END;\n");
    3241             : 
    3242             :                         // Set the system flag for the new dump and describe SQL objects.
    3243           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    3244             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3245             :                                         "    system <> true AND\n"
    3246             :                                         "    schema_id = 2000 AND\n"
    3247             :                                         "    type = %d AND\n"
    3248             :                                         "    name in (\n"
    3249             :                                         "        'describe_columns',\n"
    3250             :                                         "        'describe_function',\n"
    3251             :                                         "        'describe_table',\n"
    3252             :                                         "        'dump_database'\n"
    3253             :                                         "    );\n",
    3254             :                                         F_UNION);
    3255           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    3256             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3257             :                                         "    system <> true AND\n"
    3258             :                                         "    schema_id = 2000 AND\n"
    3259             :                                         "    type = %d AND\n"
    3260             :                                         "    name in (\n"
    3261             :                                         "        'alter_table',\n"
    3262             :                                         "        'describe_type',\n"
    3263             :                                         "        'dq',\n"
    3264             :                                         "        'esc',\n"
    3265             :                                         "        'fqn',\n"
    3266             :                                         "        'get_merge_table_partition_expressions',\n"
    3267             :                                         "        'get_remote_table_expressions',\n"
    3268             :                                         "        'prepare_esc',\n"
    3269             :                                         "        'replace_first',\n"
    3270             :                                         "        'schema_guard',\n"
    3271             :                                         "        'sq'\n"
    3272             :                                         "    );\n",
    3273             :                                         F_FUNC);
    3274           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    3275             :                                         "UPDATE sys.functions SET system = true WHERE\n"
    3276             :                                         "    system <> true AND\n"
    3277             :                                         "    schema_id = 2000 AND\n"
    3278             :                                         "    type = %d AND\n"
    3279             :                                         "    name in (\n"
    3280             :                                         "        '_dump_table_data',\n"
    3281             :                                         "        'dump_table_data',\n"
    3282             :                                         "        'eval'\n"
    3283             :                                         "    );\n",
    3284             :                                         F_PROC);
    3285           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    3286             :                                         "UPDATE sys._tables SET system = true WHERE\n"
    3287             :                                         "    system <> true AND\n"
    3288             :                                         "    schema_id = 2000 AND\n"
    3289             :                                         "    type = %d AND\n"
    3290             :                                         "    name = 'dump_statements';\n",
    3291             :                                         (int) tt_table);
    3292           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    3293             :                                         "UPDATE sys._tables SET system = true WHERE\n"
    3294             :                                         "    system <> true AND\n"
    3295             :                                         "    schema_id = 2000 AND\n"
    3296             :                                         "    type = %d AND\n"
    3297             :                                         "    name in (\n"
    3298             :                                         "        'describe_column_defaults',\n"
    3299             :                                         "        'describe_comments',\n"
    3300             :                                         "        'describe_constraints',\n"
    3301             :                                         "        'describe_foreign_keys',\n"
    3302             :                                         "        'describe_functions',\n"
    3303             :                                         "        'describe_indices',\n"
    3304             :                                         "        'describe_partition_tables',\n"
    3305             :                                         "        'describe_privileges',\n"
    3306             :                                         "        'describe_sequences',\n"
    3307             :                                         "        'describe_tables',\n"
    3308             :                                         "        'describe_triggers',\n"
    3309             :                                         "        'describe_user_defined_types',\n"
    3310             :                                         "        'dump_add_schemas_to_users',\n"
    3311             :                                         "        'dump_column_defaults',\n"
    3312             :                                         "        'dump_comments',\n"
    3313             :                                         "        'dump_create_roles',\n"
    3314             :                                         "        'dump_create_schemas',\n"
    3315             :                                         "        'dump_create_users',\n"
    3316             :                                         "        'dump_foreign_keys',\n"
    3317             :                                         "        'dump_functions',\n"
    3318             :                                         "        'dump_grant_user_privileges',\n"
    3319             :                                         "        'dump_indices',\n"
    3320             :                                         "        'dump_partition_tables',\n"
    3321             :                                         "        'dump_privileges',\n"
    3322             :                                         "        'dump_sequences',\n"
    3323             :                                         "        'dump_start_sequences',\n"
    3324             :                                         "        'dump_statements',\n"
    3325             :                                         "        'dump_table_constraint_type',\n"
    3326             :                                         "        'dump_tables',\n"
    3327             :                                         "        'dump_triggers',\n"
    3328             :                                         "        'dump_user_defined_types',\n"
    3329             :                                         "        'fully_qualified_functions'\n"
    3330             :                                         "    );\n",
    3331             :                                         (int) tt_view);
    3332             : 
    3333             :                         /* scoping2 branch changes, the 'users' view has to be re-created because of the 'schema_path' addition on 'db_user_info' table
    3334             :                            However 'dependency_schemas_on_users' has a dependency on 'users', so it has to be re-created as well */
    3335           8 :                         t = mvc_bind_table(sql, s, "users");
    3336           8 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    3337           8 :                         t = mvc_bind_table(sql, s, "dependency_schemas_on_users");
    3338           8 :                         t->system = 0;       /* make it non-system else the drop view will fail */
    3339           8 :                         pos += snprintf(buf + pos, bufsize - pos,
    3340             :                                         "DROP VIEW sys.dependency_schemas_on_users;\n"
    3341             :                                         "DROP VIEW sys.users;\n"
    3342             : 
    3343             :                                         "ALTER TABLE sys.db_user_info ADD COLUMN schema_path CLOB;\n"
    3344             :                                         "UPDATE sys.db_user_info SET schema_path = '\"sys\"';\n"
    3345             : 
    3346             :                                         "CREATE VIEW sys.users AS\n"
    3347             :                                         "SELECT u.\"name\" AS \"name\", ui.\"fullname\", ui.\"default_schema\", ui.\"schema_path\"\n"
    3348             :                                         " FROM sys.db_users() AS u\n"
    3349             :                                         " LEFT JOIN \"sys\".\"db_user_info\" AS ui ON u.\"name\" = ui.\"name\";\n"
    3350             :                                         "CREATE VIEW sys.dependency_schemas_on_users AS\n"
    3351             :                                         " SELECT s.id AS schema_id, s.name AS schema_name, u.name AS user_name, CAST(6 AS smallint) AS depend_type\n"
    3352             :                                         " FROM sys.users AS u, sys.schemas AS s\n"
    3353             :                                         " WHERE u.default_schema = s.id\n"
    3354             :                                         " ORDER BY s.name, u.name;\n"
    3355             :                                         "GRANT SELECT ON sys.dependency_schemas_on_users TO PUBLIC;\n"
    3356             :                                         "update sys._tables set system = true where system <> true and name in ('users','dependency_schemas_on_users')"
    3357             :                                         " and schema_id = 2000 and type = %d;\n", (int) tt_view);
    3358             : 
    3359           8 :                         pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
    3360           8 :                         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    3361             : 
    3362           8 :                         assert(pos < bufsize);
    3363           8 :                         printf("Running database upgrade commands:\n%s\n", buf);
    3364           8 :                         if ((err = SQLstatementIntern(c, buf, "update", true, false, NULL)) != MAL_SUCCEED)
    3365           0 :                                 goto bailout;
    3366             : 
    3367           8 :                         pos = snprintf(buf, bufsize, "set schema \"sys\";\n"
    3368             :                                         "ALTER TABLE sys.keywords SET READ ONLY;\n"
    3369             :                                         "ALTER TABLE sys.table_types SET READ ONLY;\n"
    3370             :                                         "ALTER TABLE sys.function_types SET READ ONLY;\n");
    3371           8 :                         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    3372           8 :                         assert(pos < bufsize);
    3373           8 :                         printf("Running database upgrade commands:\n%s\n", buf);
    3374           8 :                         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    3375             :                 }
    3376             :         }
    3377             : 
    3378          61 : bailout:
    3379          69 :         if (b)
    3380          69 :                 BBPunfix(b->batCacheid);
    3381          69 :         if (output)
    3382          69 :                 res_table_destroy(output);
    3383          69 :         GDKfree(buf);
    3384          69 :         return err;             /* usually MAL_SUCCEED */
    3385             : }
    3386             : 
    3387             : /* upgrades after Jul2021_3 build */
    3388             : static str
    3389          69 : sql_update_jul2021_5(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    3390             : {
    3391             :         size_t bufsize = 65536, pos = 0;
    3392             :         char *buf = NULL, *err = NULL;
    3393          69 :         res_table *output = NULL;
    3394          69 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    3395             :         sql_table *t;
    3396             : 
    3397             :         (void) systabfixed;
    3398             : 
    3399          69 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    3400           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3401             : 
    3402             :         /* if the string 'partition of merge table' is not in the sys.ids
    3403             :          * query, upgrade */
    3404          69 :         pos += snprintf(buf + pos, bufsize - pos,
    3405             :                                         "select query from sys._tables where name = 'ids' and schema_id = 2000 and query like '%%partition of merge table%%';\n");
    3406          69 :         assert(pos < bufsize);
    3407          69 :         if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)) == NULL) {
    3408             :                 BAT *b;
    3409          69 :                 if ((b = BATdescriptor(output->cols[0].b))) {
    3410          69 :                         if (BATcount(b) == 0) {
    3411           8 :                                 pos = snprintf(buf, bufsize, "set schema \"sys\";\n");
    3412             : 
    3413             :                                 /* 21_dependency_views.sql */
    3414           8 :                                 t = mvc_bind_table(sql, s, "ids");
    3415           8 :                                 t->system = 0;       /* make it non-system else the drop view will fail */
    3416           8 :                                 t = mvc_bind_table(sql, s, "dependencies_vw");
    3417           8 :                                 t->system = 0;       /* make it non-system else the drop view will fail */
    3418           8 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3419             :                                                                 "drop view sys.dependencies_vw;\n"
    3420             :                                                                 "drop view sys.ids;\n");
    3421           8 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3422             :                                                                 "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table) AS\n"
    3423             :                                                                 "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"
    3424             :                                                                 "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"
    3425             :                                                                 "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"
    3426             :                                                                 "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"
    3427             :                                                                 "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"
    3428             :                                                                 "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"
    3429             :                                                                 "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"
    3430             :                                                                 "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"
    3431             :                                                                 "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"
    3432             :                                                                 "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"
    3433             :                                                                 "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"
    3434             :                                                                 "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"
    3435             :                                                                 "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"
    3436             :                                                                 "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"
    3437             :                                                                 "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"
    3438             :                                                                 "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"
    3439             :                                                                 "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"
    3440             :                                                                 " ORDER BY id;\n"
    3441             :                                                                 "GRANT SELECT ON sys.ids TO PUBLIC;\n");
    3442           8 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3443             :                                                                 "CREATE VIEW sys.dependencies_vw AS\n"
    3444             :                                                                 "SELECT d.id, i1.obj_type, i1.name,\n"
    3445             :                                                                 "       d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
    3446             :                                                                 "       d.depend_type, dt.dependency_type_name\n"
    3447             :                                                                 "  FROM sys.dependencies d\n"
    3448             :                                                                 "  JOIN sys.ids i1 ON d.id = i1.id\n"
    3449             :                                                                 "  JOIN sys.ids i2 ON d.depend_id = i2.id\n"
    3450             :                                                                 "  JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
    3451             :                                                                 " ORDER BY id, depend_id;\n"
    3452             :                                                                 "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n");
    3453           8 :                                 pos += snprintf(buf + pos, bufsize - pos,
    3454             :                                                                 "UPDATE sys._tables SET system = true WHERE name in ('ids', 'dependencies_vw') AND schema_id = 2000;\n");
    3455             : 
    3456           8 :                                 pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    3457           8 :                                 assert(pos < bufsize);
    3458           8 :                                 printf("Running database upgrade commands:\n%s\n", buf);
    3459           8 :                                 err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    3460             :                         }
    3461          69 :                         BBPunfix(b->batCacheid);
    3462             :                 }
    3463          69 :                 res_table_destroy(output);
    3464             :         }
    3465             : 
    3466          69 :         GDKfree(buf);
    3467          69 :         return err;             /* usually MAL_SUCCEED */
    3468             : }
    3469             : 
    3470             : static str
    3471          69 : sql_update_default(Client c, mvc *sql, const char *prev_schema, bool *systabfixed)
    3472             : {
    3473             :         sql_subtype tp;
    3474             :         size_t bufsize = 65536, pos = 0;
    3475             :         char *buf = NULL, *err = NULL;
    3476          69 :         sql_schema *s = mvc_bind_schema(sql, "sys");
    3477             :         sql_table *t;
    3478             : 
    3479             :         (void) systabfixed;
    3480          69 :         sql_find_subtype(&tp, "bigint", 0, 0);
    3481          69 :         if (!sql_bind_func(sql, s->base.name, "epoch", &tp, NULL, F_FUNC)) {
    3482          53 :                 sql->session->status = 0; /* if the function was not found clean the error */
    3483          53 :                 sql->errstr[0] = '\0';
    3484             :                 /* nothing to do */
    3485          53 :                 return NULL;
    3486             :         }
    3487             : 
    3488          16 :         if ((buf = GDKmalloc(bufsize)) == NULL)
    3489           0 :                 throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3490             : 
    3491          16 :         pos = snprintf(buf, bufsize, "set schema \"sys\";\n");
    3492             : 
    3493             :         /* sys.epoch_ms now returns a decimal(18,3) */
    3494          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3495             :                                         "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");
    3496             : 
    3497             :         /* 16_tracelog */
    3498          16 :         t = mvc_bind_table(sql, s, "tracelog");
    3499          16 :         t->system = 0; /* make it non-system else the drop view will fail */
    3500          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3501             :                         "drop view sys.tracelog;\n"
    3502             :                         "drop function sys.tracelog();\n"
    3503             :                         "create function sys.tracelog()\n"
    3504             :                         " returns table (\n"
    3505             :                         "  ticks bigint, -- time in microseconds\n"
    3506             :                         "  stmt string,  -- actual statement executed\n"
    3507             :                         "  event string  -- profiler event executed\n"
    3508             :                         " )\n"
    3509             :                         " external name sql.dump_trace;\n"
    3510             :                         "create view sys.tracelog as select * from sys.tracelog();\n"
    3511             :                         "update sys._tables set system = true where system <> true and schema_id = 2000"
    3512             :                         " and name = 'tracelog';\n"
    3513             :                         "update sys.functions set system = true where system <> true and schema_id = 2000"
    3514             :                         " and name = 'tracelog' and type = %d;\n", (int) F_UNION);
    3515             : 
    3516             :         /* 17_temporal.sql */
    3517          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3518             :                                         "drop function sys.epoch(bigint);\n");
    3519          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3520             :                                         "create function sys.epoch(sec DECIMAL(18,3)) "
    3521             :                                         "returns TIMESTAMP WITH TIME ZONE\n"
    3522             :                                         "external name mtime.epoch;\n"
    3523             :                                         "grant execute on function sys.epoch (DECIMAL(18,3)) to public;\n"
    3524             :                                         "update sys.functions set system = true where system <> true and name in ('epoch') and schema_id = 2000 and type = %d;\n", F_FUNC);
    3525             : 
    3526          16 :         pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", prev_schema);
    3527             : 
    3528             :         /* 25_debug.sql */
    3529          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3530             :                                         "create view sys.malfunctions as select * from sys.malfunctions();\n"
    3531             :                                         "update sys._tables set system = true where system <> true and schema_id = 2000"
    3532             :                                         " and name = 'malfunctions';\n");
    3533             : 
    3534             :         /* 21_dependency_views.sql */
    3535          16 :         t = mvc_bind_table(sql, s, "ids");
    3536          16 :         t->system = 0; /* make it non-system else the drop view will fail */
    3537          16 :         t = mvc_bind_table(sql, s, "dependencies_vw");
    3538          16 :         t->system = 0;       /* make it non-system else the drop view will fail */
    3539          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3540             :                                         "drop view sys.dependencies_vw;\n" /* depends on sys.ids */
    3541             :                                         "drop view sys.ids;\n"
    3542             :                                         "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table, system) AS\n"
    3543             :                                         "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"
    3544             :                                         "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"
    3545             :                                         "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"
    3546             :                                         "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"
    3547             :                                         "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"
    3548             :                                         "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"
    3549             :                                         "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"
    3550             :                                         "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"
    3551             :                                         "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"
    3552             :                                         "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"
    3553             :                                         "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"
    3554             :                                         "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"
    3555             :                                         "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"
    3556             :                                         "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"
    3557             :                                         "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"
    3558             :                                         "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"
    3559             :                                         " ORDER BY id;\n"
    3560             :                                         "GRANT SELECT ON sys.ids TO PUBLIC;\n");
    3561          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3562             :                                         "CREATE VIEW sys.dependencies_vw AS\n"
    3563             :                                         "SELECT d.id, i1.obj_type, i1.name,\n"
    3564             :                                         "       d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
    3565             :                                         "       d.depend_type, dt.dependency_type_name\n"
    3566             :                                         "  FROM sys.dependencies d\n"
    3567             :                                         "  JOIN sys.ids i1 ON d.id = i1.id\n"
    3568             :                                         "  JOIN sys.ids i2 ON d.depend_id = i2.id\n"
    3569             :                                         "  JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
    3570             :                                         " ORDER BY id, depend_id;\n"
    3571             :                                         "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n");
    3572          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3573             :                                         "UPDATE sys._tables SET system = true WHERE name in ('ids', 'dependencies_vw') AND schema_id = 2000;\n");
    3574             : 
    3575             :         /* 52_describe.sql; but we need to drop most everything from
    3576             :          * 76_dump.sql first */
    3577          16 :         t = mvc_bind_table(sql, s, "dump_privileges");
    3578          16 :         t->system = 0;
    3579          16 :         t = mvc_bind_table(sql, s, "dump_user_defined_types");
    3580          16 :         t->system = 0;
    3581          16 :         t = mvc_bind_table(sql, s, "dump_comments");
    3582          16 :         t->system = 0;
    3583          16 :         t = mvc_bind_table(sql, s, "dump_triggers");
    3584          16 :         t->system = 0;
    3585          16 :         t = mvc_bind_table(sql, s, "dump_tables");
    3586          16 :         t->system = 0;
    3587          16 :         t = mvc_bind_table(sql, s, "dump_functions");
    3588          16 :         t->system = 0;
    3589          16 :         t = mvc_bind_table(sql, s, "dump_start_sequences");
    3590          16 :         t->system = 0;
    3591          16 :         t = mvc_bind_table(sql, s, "dump_sequences");
    3592          16 :         t->system = 0;
    3593          16 :         t = mvc_bind_table(sql, s, "dump_partition_tables");
    3594          16 :         t->system = 0;
    3595          16 :         t = mvc_bind_table(sql, s, "dump_foreign_keys");
    3596          16 :         t->system = 0;
    3597          16 :         t = mvc_bind_table(sql, s, "dump_column_defaults");
    3598          16 :         t->system = 0;
    3599          16 :         t = mvc_bind_table(sql, s, "dump_indices");
    3600          16 :         t->system = 0;
    3601          16 :         t = mvc_bind_table(sql, s, "dump_table_constraint_type");
    3602          16 :         t->system = 0;
    3603          16 :         t = mvc_bind_table(sql, s, "dump_grant_user_privileges");
    3604          16 :         t->system = 0;
    3605          16 :         t = mvc_bind_table(sql, s, "dump_add_schemas_to_users");
    3606          16 :         t->system = 0;
    3607          16 :         t = mvc_bind_table(sql, s, "dump_create_schemas");
    3608          16 :         t->system = 0;
    3609          16 :         t = mvc_bind_table(sql, s, "dump_create_users");
    3610          16 :         t->system = 0;
    3611          16 :         t = mvc_bind_table(sql, s, "dump_create_roles");
    3612          16 :         t->system = 0;
    3613          16 :         t = mvc_bind_table(sql, s, "describe_constraints");
    3614          16 :         t->system = 0;
    3615          16 :         t = mvc_bind_table(sql, s, "describe_tables");
    3616          16 :         t->system = 0;
    3617          16 :         t = mvc_bind_table(sql, s, "describe_comments");
    3618          16 :         t->system = 0;
    3619          16 :         t = mvc_bind_table(sql, s, "describe_privileges");
    3620          16 :         t->system = 0;
    3621          16 :         t = mvc_bind_table(sql, s, "describe_partition_tables");
    3622          16 :         t->system = 0;
    3623          16 :         t = mvc_bind_table(sql, s, "describe_functions");
    3624          16 :         t->system = 0;
    3625          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3626             :                                         /* drop dependant stuff from 76_dump.sql */
    3627             :                                         "drop function sys.dump_database(boolean);\n"
    3628             :                                         "drop procedure sys.dump_table_data();\n"
    3629             :                                         "drop procedure sys._dump_table_data(string, string);\n"
    3630             :                                         "drop function sys.prepare_esc(string, string);\n"
    3631             :                                         "drop function sys.esc(string);\n"
    3632             :                                         "drop view sys.dump_privileges;\n"
    3633             :                                         "drop view sys.dump_user_defined_types;\n"
    3634             :                                         "drop view sys.dump_comments;\n"
    3635             :                                         "drop view sys.dump_triggers;\n"
    3636             :                                         "drop view sys.dump_tables;\n"
    3637             :                                         "drop view sys.dump_functions;\n"
    3638             :                                         "drop view sys.dump_start_sequences;\n"
    3639             :                                         "drop view sys.dump_sequences;\n"
    3640             :                                         "drop view sys.dump_partition_tables;\n"
    3641             :                                         "drop view sys.dump_foreign_keys;\n"
    3642             :                                         "drop view sys.dump_column_defaults;\n"
    3643             :                                         "drop view sys.dump_indices;\n"
    3644             :                                         "drop view sys.dump_table_constraint_type;\n"
    3645             :                                         "drop view sys.dump_grant_user_privileges;\n"
    3646             :                                         "drop view sys.dump_add_schemas_to_users;\n"
    3647             :                                         "drop view sys.dump_create_schemas;\n"
    3648             :                                         "drop view sys.dump_create_users;\n"
    3649             :                                         "drop view sys.dump_create_roles;\n"
    3650             : 
    3651             :                                         "drop view sys.describe_functions;\n"
    3652             :                                         "drop view sys.describe_partition_tables;\n"
    3653             :                                         "drop view sys.describe_privileges;\n"
    3654             :                                         "drop view sys.describe_comments;\n"
    3655             :                                         "drop view sys.describe_tables;\n"
    3656             :                                         "drop function sys.get_remote_table_expressions(string, string);\n"
    3657             :                                         "drop function sys.get_merge_table_partition_expressions(int);\n"
    3658             :                                         "drop view sys.describe_constraints;\n"
    3659             :                                         "drop function sys.alter_table(string, string);\n"
    3660             :                                         "drop function sys.sq(string);\n");
    3661          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3662             :                                         "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN '''' || sys.replace(s,'''','''''') || ''''; END;\n"
    3663             :                                         "CREATE FUNCTION sys.ALTER_TABLE(s STRING, t STRING) RETURNS STRING BEGIN RETURN 'ALTER TABLE ' || sys.FQN(s, t); END;\n"
    3664             :                                         "CREATE VIEW sys.describe_constraints AS\n"
    3665             :                                         "  SELECT\n"
    3666             :                                         "          s.name sch,\n"
    3667             :                                         "          t.name tbl,\n"
    3668             :                                         "          kc.name col,\n"
    3669             :                                         "          k.name con,\n"
    3670             :                                         "          CASE k.type WHEN 0 THEN 'PRIMARY KEY' WHEN 1 THEN 'UNIQUE' END tpe\n"
    3671             :                                         "  FROM sys.schemas s, sys._tables t, sys.objects kc, sys.keys k\n"
    3672             :                                         "  WHERE kc.id = k.id\n"
    3673             :                                         "          AND k.table_id = t.id\n"
    3674             :                                         "          AND s.id = t.schema_id\n"
    3675             :                                         "          AND t.system = FALSE\n"
    3676             :                                         "          AND k.type in (0, 1);\n"
    3677             :                                         "CREATE FUNCTION sys.get_merge_table_partition_expressions(tid INT) RETURNS STRING\n"
    3678             :                                         "BEGIN\n"
    3679             :                                         "  RETURN\n"
    3680             :                                         "          SELECT\n"
    3681             :                                         "                  CASE WHEN tp.table_id IS NOT NULL THEN\n"
    3682             :                                         "                          ' PARTITION BY ' ||\n"
    3683             :                                         "                          ifthenelse(bit_and(tp.type, 2) = 2, 'VALUES ', 'RANGE ') ||\n"
    3684             :                                         "                          CASE\n"
    3685             :                                         "                                  WHEN bit_and(tp.type, 4) = 4\n"
    3686             :                                         "                                  THEN 'ON ' || '(' || (SELECT sys.DQ(c.name) || ')' FROM sys.columns c WHERE c.id = tp.column_id)\n"
    3687             :                                         "                                  ELSE 'USING ' || '(' || tp.expression || ')'\n"
    3688             :                                         "                          END\n"
    3689             :                                         "                  ELSE\n"
    3690             :                                         "                          ''\n"
    3691             :                                         "                  END\n"
    3692             :                                         "          FROM (VALUES (tid)) t(id) LEFT JOIN sys.table_partitions tp ON t.id = tp.table_id;\n"
    3693             :                                         "END;\n"
    3694             :                                         "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
    3695             :                                         "  RETURN SELECT ' ON ' || sys.SQ(uri) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(\"hash\") FROM sys.remote_table_credentials(s ||'.' || t);\n"
    3696             :                                         "END;\n"
    3697             :                                         "CREATE VIEW sys.describe_tables AS\n"
    3698             :                                         "  SELECT\n"
    3699             :                                         "          t.id o,\n"
    3700             :                                         "          s.name sch,\n"
    3701             :                                         "          t.name tab,\n"
    3702             :                                         "          ts.table_type_name typ,\n"
    3703             :                                         "          (SELECT\n"
    3704             :                                         "                  ' (' ||\n"
    3705             :                                         "                  GROUP_CONCAT(\n"
    3706             :                                         "                          sys.DQ(c.name) || ' ' ||\n"
    3707             :                                         "                          sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
    3708             :                                         "                          ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
    3709             :                                         "                  , ', ') || ')'\n"
    3710             :                                         "          FROM sys._columns c\n"
    3711             :                                         "          WHERE c.table_id = t.id) col,\n"
    3712             :                                         "          CASE ts.table_type_name\n"
    3713             :                                         "                  WHEN 'REMOTE TABLE' THEN\n"
    3714             :                                         "                          sys.get_remote_table_expressions(s.name, t.name)\n"
    3715             :                                         "                  WHEN 'MERGE TABLE' THEN\n"
    3716             :                                         "                          sys.get_merge_table_partition_expressions(t.id)\n"
    3717             :                                         "                  WHEN 'VIEW' THEN\n"
    3718             :                                         "                          sys.schema_guard(s.name, t.name, t.query)\n"
    3719             :                                         "                  ELSE\n"
    3720             :                                         "                          ''\n"
    3721             :                                         "          END opt\n"
    3722             :                                         "  FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
    3723             :                                         "  WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE')\n"
    3724             :                                         "          AND t.system = FALSE\n"
    3725             :                                         "          AND s.id = t.schema_id\n"
    3726             :                                         "          AND ts.table_type_id = t.type\n"
    3727             :                                         "          AND s.name <> 'tmp';\n"
    3728             :                                         "CREATE VIEW sys.describe_comments AS\n"
    3729             :                                         "          SELECT\n"
    3730             :                                         "                  o.id id,\n"
    3731             :                                         "                  o.tpe tpe,\n"
    3732             :                                         "                  o.nme fqn,\n"
    3733             :                                         "                  c.remark rem\n"
    3734             :                                         "          FROM (\n"
    3735             :                                         "                  SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas\n"
    3736             :                                         "                  UNION ALL\n"
    3737             :                                         "                  SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
    3738             :                                         "                  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"
    3739             :                                         "                  WHERE s.name <> 'tmp'\n"
    3740             :                                         "                  UNION ALL\n"
    3741             :                                         "                  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"
    3742             :                                         "                  UNION ALL\n"
    3743             :                                         "                  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"
    3744             :                                         "                  UNION ALL\n"
    3745             :                                         "                  SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
    3746             :                                         "                  UNION ALL\n"
    3747             :                                         "                  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"
    3748             :                                         "                  ) AS o(id, tpe, nme)\n"
    3749             :                                         "                  JOIN sys.comments c ON c.id = o.id;\n"
    3750             :                                         "CREATE VIEW sys.describe_privileges AS\n"
    3751             :                                         "  SELECT\n"
    3752             :                                         "          CASE\n"
    3753             :                                         "                  WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
    3754             :                                         "                          'COPY FROM'\n"
    3755             :                                         "                  WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
    3756             :                                         "                          'COPY INTO'\n"
    3757             :                                         "                  ELSE\n"
    3758             :                                         "                          o.nme\n"
    3759             :                                         "          END o_nme,\n"
    3760             :                                         "          coalesce(o.tpe, 'GLOBAL') o_tpe,\n"
    3761             :                                         "          pc.privilege_code_name p_nme,\n"
    3762             :                                         "          a.name a_nme,\n"
    3763             :                                         "          g.name g_nme,\n"
    3764             :                                         "          p.grantable grantable\n"
    3765             :                                         "  FROM\n"
    3766             :                                         "          sys.privileges p LEFT JOIN\n"
    3767             :                                         "          (\n"
    3768             :                                         "          SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
    3769             :                                         "                  from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
    3770             :                                         "          UNION ALL\n"
    3771             :                                         "                  SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
    3772             :                                         "                  FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
    3773             :                                         "          UNION ALL\n"
    3774             :                                         "                  SELECT f.id, f.nme, f.tpe\n"
    3775             :                                         "                  FROM sys.fully_qualified_functions f\n"
    3776             :                                         "          ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
    3777             :                                         "          sys.privilege_codes pc,\n"
    3778             :                                         "          auths a, auths g\n"
    3779             :                                         "  WHERE\n"
    3780             :                                         "          p.privileges = pc.privilege_code_id AND\n"
    3781             :                                         "          p.auth_id = a.id AND\n"
    3782             :                                         "          p.grantor = g.id;\n"
    3783             :                                         "CREATE VIEW sys.describe_partition_tables AS\n"
    3784             :                                         "  SELECT \n"
    3785             :                                         "          m_sch,\n"
    3786             :                                         "          m_tbl,\n"
    3787             :                                         "          p_sch,\n"
    3788             :                                         "          p_tbl,\n"
    3789             :                                         "          CASE\n"
    3790             :                                         "                  WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
    3791             :                                         "                  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"
    3792             :                                         "                  ELSE p_raw_type\n"
    3793             :                                         "          END AS tpe,\n"
    3794             :                                         "          pvalues,\n"
    3795             :                                         "          minimum,\n"
    3796             :                                         "          maximum,\n"
    3797             :                                         "          with_nulls\n"
    3798             :                                         "  FROM \n"
    3799             :                                         "    (WITH\n"
    3800             :                                         "          tp(\"type\", table_id) AS\n"
    3801             :                                         "          (SELECT ifthenelse((table_partitions.\"type\" & 2) = 2, 'VALUES', 'RANGE'), table_partitions.table_id FROM sys.table_partitions),\n"
    3802             :                                         "          subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
    3803             :                                         "          (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
    3804             :                                         "          FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
    3805             :                                         "          WHERE m_t.\"type\" IN (3, 6)\n"
    3806             :                                         "                  AND m_t.schema_id = m_s.id\n"
    3807             :                                         "                  AND m_s.name <> 'tmp'\n"
    3808             :                                         "                  AND m_t.system = FALSE\n"
    3809             :                                         "                  AND m_t.id = d.depend_id\n"
    3810             :                                         "                  AND d.id = p_m.id\n"
    3811             :                                         "                  AND p_m.schema_id = p_s.id\n"
    3812             :                                         "          ORDER BY m_t.id, p_m.id)\n"
    3813             :                                         "  SELECT\n"
    3814             :                                         "          subq.m_sch,\n"
    3815             :                                         "          subq.m_tbl,\n"
    3816             :                                         "          subq.p_sch,\n"
    3817             :                                         "          subq.p_tbl,\n"
    3818             :                                         "          tp.\"type\" AS p_raw_type,\n"
    3819             :                                         "          CASE WHEN tp.\"type\" = 'VALUES'\n"
    3820             :                                         "                  THEN (SELECT GROUP_CONCAT(vp.value, ',') FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid)\n"
    3821             :                                         "                  ELSE NULL\n"
    3822             :                                         "          END AS pvalues,\n"
    3823             :                                         "          CASE WHEN tp.\"type\" = 'RANGE'\n"
    3824             :                                         "                  THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3825             :                                         "                  ELSE NULL\n"
    3826             :                                         "          END AS minimum,\n"
    3827             :                                         "          CASE WHEN tp.\"type\" = 'RANGE'\n"
    3828             :                                         "                  THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3829             :                                         "                  ELSE NULL\n"
    3830             :                                         "          END AS maximum,\n"
    3831             :                                         "          CASE WHEN tp.\"type\" = 'VALUES'\n"
    3832             :                                         "                  THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
    3833             :                                         "                  ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
    3834             :                                         "          END AS with_nulls\n"
    3835             :                                         "  FROM \n"
    3836             :                                         "          subq LEFT OUTER JOIN tp\n"
    3837             :                                         "          ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
    3838             :                                         "CREATE VIEW sys.describe_functions AS\n"
    3839             :                                         "  WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    3840             :                                         "  (\n"
    3841             :                                         "          SELECT\n"
    3842             :                                         "                  func_id,\n"
    3843             :                                         "                  number,\n"
    3844             :                                         "                  max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    3845             :                                         "                  group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    3846             :                                         "          FROM sys.args\n"
    3847             :                                         "          WHERE inout = 1\n"
    3848             :                                         "  ),\n"
    3849             :                                         "  func_args(func_id, func_arg) AS\n"
    3850             :                                         "  (\n"
    3851             :                                         "          SELECT func_id, func_arg\n"
    3852             :                                         "          FROM func_args_all\n"
    3853             :                                         "          WHERE number = max_number\n"
    3854             :                                         "  ),\n"
    3855             :                                         "  func_rets_all(func_id, number, max_number, func_ret, func_ret_type) AS\n"
    3856             :                                         "  (\n"
    3857             :                                         "          SELECT\n"
    3858             :                                         "                  func_id,\n"
    3859             :                                         "                  number,\n"
    3860             :                                         "                  max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
    3861             :                                         "                  group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number),\n"
    3862             :                                         "                  group_concat(sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
    3863             :                                         "          FROM sys.args\n"
    3864             :                                         "          WHERE inout = 0\n"
    3865             :                                         "  ),\n"
    3866             :                                         "  func_rets(func_id, func_ret, func_ret_type) AS\n"
    3867             :                                         "  (\n"
    3868             :                                         "          SELECT\n"
    3869             :                                         "                  func_id,\n"
    3870             :                                         "                  func_ret,\n"
    3871             :                                         "                  func_ret_type\n"
    3872             :                                         "          FROM func_rets_all\n"
    3873             :                                         "          WHERE number = max_number\n"
    3874             :                                         "  )\n"
    3875             :                                         "  SELECT\n"
    3876             :                                         "          f.id o,\n"
    3877             :                                         "          s.name sch,\n"
    3878             :                                         "          f.name fun,\n"
    3879             :                                         "          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"
    3880             :                                         "  FROM sys.functions f\n"
    3881             :                                         "          LEFT OUTER JOIN func_args fa ON fa.func_id = f.id\n"
    3882             :                                         "          LEFT OUTER JOIN func_rets fr ON fr.func_id = f.id\n"
    3883             :                                         "          JOIN sys.schemas s ON f.schema_id = s.id\n"
    3884             :                                         "          JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
    3885             :                                         "          LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
    3886             :                                         "  WHERE s.name <> 'tmp' AND NOT f.system;\n");
    3887          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3888             :                                         "update sys.functions set system = true where system <> true and name in ('sq', 'alter_table', 'get_merge_table_partition_expressions', 'get_remote_table_expressions') and schema_id = 2000 and type = %d;\n", F_FUNC);
    3889          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3890             :                                 "update sys._tables set system = true where name in ('describe_constraints', 'describe_tables', 'describe_comments', 'describe_privileges', 'describe_partition_tables', 'describe_functions') AND schema_id = 2000;\n");
    3891             : 
    3892             :         /* 76_dump.sql (most everything already dropped) */
    3893          16 :         pos += snprintf(buf + pos, bufsize - pos,
    3894             :                                         "CREATE VIEW sys.dump_create_roles AS\n"
    3895             :                                         "  SELECT\n"
    3896             :                                         "    'CREATE ROLE ' || sys.dq(name) || ';' stmt,\n"
    3897             :                                         "    name user_name\n"
    3898             :                                         "    FROM sys.auths\n"
    3899             :                                         "   WHERE name NOT IN (SELECT name FROM sys.db_user_info)\n"
    3900             :                                         "     AND grantor <> 0;\n"
    3901             :                                         "CREATE VIEW sys.dump_create_users AS\n"
    3902             :                                         "  SELECT\n"
    3903             :                                         "    'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
    3904             :                                         "      sys.sq(sys.password_hash(ui.name)) ||\n"
    3905             :                                         "      ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
    3906             :                                         "    ui.name user_name\n"
    3907             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3908             :                                         "   WHERE ui.default_schema = s.id\n"
    3909             :                                         "     AND ui.name <> 'monetdb'\n"
    3910             :                                         "     AND ui.name <> '.snapshot';\n"
    3911             :                                         "CREATE VIEW sys.dump_create_schemas AS\n"
    3912             :                                         "  SELECT\n"
    3913             :                                         "    'CREATE SCHEMA ' || sys.dq(s.name) || ifthenelse(a.name <> 'sysadmin', ' AUTHORIZATION ' || sys.dq(a.name), ' ') || ';' stmt,\n"
    3914             :                                         "    s.name schema_name\n"
    3915             :                                         "    FROM sys.schemas s, sys.auths a\n"
    3916             :                                         "   WHERE s.authorization = a.id AND s.system = FALSE;\n"
    3917             :                                         "CREATE VIEW sys.dump_add_schemas_to_users AS\n"
    3918             :                                         "  SELECT\n"
    3919             :                                         "    'ALTER USER ' || sys.dq(ui.name) || ' SET SCHEMA ' || sys.dq(s.name) || ';' stmt,\n"
    3920             :                                         "    s.name schema_name,\n"
    3921             :                                         "    ui.name user_name\n"
    3922             :                                         "    FROM sys.db_user_info ui, sys.schemas s\n"
    3923             :                                         "   WHERE ui.default_schema = s.id\n"
    3924             :                                         "     AND ui.name <> 'monetdb'\n"
    3925             :                                         "     AND ui.name <> '.snapshot'\n"
    3926             :                                         "     AND s.name <> 'sys';\n"
    3927             :                                         "CREATE VIEW sys.dump_grant_user_privileges AS\n"
    3928             :                                         "  SELECT\n"
    3929             :                                         "    'GRANT ' || sys.dq(a2.name) || ' ' || ifthenelse(a1.name = 'public', 'PUBLIC', sys.dq(a1.name)) || ';' stmt,\n"
    3930             :                                         "    a2.name grantee,\n"
    3931             :                                         "    a1.name grantor\n"
    3932             :                                         "    FROM sys.auths a1, sys.auths a2, sys.user_role ur\n"
    3933             :                                         "   WHERE a1.id = ur.login_id AND a2.id = ur.role_id;\n"
    3934             :                                         "CREATE VIEW sys.dump_table_constraint_type AS\n"
    3935             :                                         "  SELECT\n"
    3936             :                                         "    'ALTER TABLE ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    3937             :                                         "      ' ADD CONSTRAINT ' || sys.DQ(con) || ' '||\n"
    3938             :                                         "      tpe || ' (' || GROUP_CONCAT(sys.DQ(col), ', ') || ');' stmt,\n"
    3939             :                                         "    sch schema_name,\n"
    3940             :                                         "    tbl table_name,\n"
    3941             :                                         "    con constraint_name\n"
    3942             :                                         "    FROM sys.describe_constraints GROUP BY sch, tbl, con, tpe;\n"
    3943             :                                         "CREATE VIEW sys.dump_table_grants AS\n"
    3944             :                                         "  WITH table_grants (sname, tname, grantee, grants, grantor, grantable)\n"
    3945             :                                         "  AS (SELECT s.name, t.name, a.name, sum(p.privileges), g.name, p.grantable\n"
    3946             :                                         "  FROM sys.schemas s, sys.tables t, sys.auths a, sys.privileges p, sys.auths g\n"
    3947             :                                         "       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 GROUP BY s.name, t.name, a.name, g.name, p.grantable\n"
    3948             :                                         "       ORDER BY s.name, t.name, a.name, g.name, p.grantable)\n"
    3949             :                                         "  SELECT\n"
    3950             :                                         "    'GRANT ' || pc.privilege_code_name || ' ON TABLE ' || sys.dq(sname)\n"
    3951             :                                         "      || '.' || sys.dq(tname) || ' TO ' || ifthenelse(grantee = 'public', 'PUBLIC', sys.dq(grantee))\n"
    3952             :                                         "      || CASE WHEN grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3953             :                                         "    sname schema_name,\n"
    3954             :                                         "    tname table_name,\n"
    3955             :                                         "    grantee\n"
    3956             :                                         "    FROM table_grants LEFT OUTER JOIN sys.privilege_codes pc ON grants = pc.privilege_code_id;\n"
    3957             :                                         "CREATE VIEW sys.dump_column_grants AS\n"
    3958             :                                         "  SELECT\n"
    3959             :                                         "    'GRANT ' || pc.privilege_code_name || '(' || sys.dq(c.name) || ') ON '\n"
    3960             :                                         "      || sys.dq(s.name) || '.' || sys.dq(t.name) || ' TO ' || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
    3961             :                                         "      || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3962             :                                         "    s.name schema_name,\n"
    3963             :                                         "    t.name table_name,\n"
    3964             :                                         "    c.name column_name,\n"
    3965             :                                         "    a.name grantee\n"
    3966             :                                         "    FROM sys.schemas s,\n"
    3967             :                                         "   sys.tables t,\n"
    3968             :                                         "   sys.columns c,\n"
    3969             :                                         "   sys.auths a,\n"
    3970             :                                         "   sys.privileges p,\n"
    3971             :                                         "   sys.auths g,\n"
    3972             :                                         "   sys.privilege_codes pc\n"
    3973             :                                         "   WHERE p.obj_id = c.id\n"
    3974             :                                         "     AND c.table_id = t.id\n"
    3975             :                                         "     AND p.auth_id = a.id\n"
    3976             :                                         "     AND t.schema_id = s.id\n"
    3977             :                                         "     AND NOT t.system\n"
    3978             :                                         "     AND p.grantor = g.id\n"
    3979             :                                         "     AND p.privileges = pc.privilege_code_id\n"
    3980             :                                         "   ORDER BY s.name, t.name, c.name, a.name, g.name, p.grantable;\n"
    3981             :                                         "CREATE VIEW sys.dump_function_grants AS\n"
    3982             :                                         "  WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
    3983             :                                         "  (SELECT a.func_id,\n"
    3984             :                                         "    a.number,\n"
    3985             :                                         "    max(a.number) OVER (PARTITION BY a.func_id ORDER BY a.number DESC),\n"
    3986             :                                         "    group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ', ') OVER (PARTITION BY a.func_id ORDER BY a.number)\n"
    3987             :                                         "     FROM sys.args a\n"
    3988             :                                         "    WHERE a.inout = 1),\n"
    3989             :                                         "  func_args(func_id, func_arg) AS\n"
    3990             :                                         "  (SELECT func_id, func_arg FROM func_args_all WHERE number = max_number)\n"
    3991             :                                         "  SELECT\n"
    3992             :                                         "    'GRANT ' || pc.privilege_code_name || ' ON '\n"
    3993             :                                         "      || ft.function_type_keyword || ' '\n"
    3994             :                                         "      || sys.dq(s.name) || '.' || sys.dq(f.name) || '(' || coalesce(fa.func_arg, '') || ') TO '\n"
    3995             :                                         "      || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
    3996             :                                         "      || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
    3997             :                                         "    s.name schema_name,\n"
    3998             :                                         "    f.name function_name,\n"
    3999             :                                         "    a.name grantee\n"
    4000             :                                         "    FROM sys.schemas s,\n"
    4001             :                                         "   sys.functions f LEFT OUTER JOIN func_args fa ON f.id = fa.func_id,\n"
    4002             :                                         "   sys.auths a,\n"
    4003             :                                         "   sys.privileges p,\n"
    4004             :                                         "   sys.auths g,\n"
    4005             :                                         "   sys.function_types ft,\n"
    4006             :                                         "   sys.privilege_codes pc\n"
    4007             :                                         "   WHERE s.id = f.schema_id\n"
    4008             :                                         "     AND f.id = p.obj_id\n"
    4009             :                                         "     AND p.auth_id = a.id\n"
    4010             :                                         "     AND p.grantor = g.id\n"
    4011             :                                         "     AND p.privileges = pc.privilege_code_id\n"
    4012             :                                         "     AND f.type = ft.function_type_id\n"
    4013             :                                         "     AND NOT f.system\n"
    4014             :                                         "   ORDER BY s.name, f.name, a.name, g.name, p.grantable;\n"
    4015             :                                         "CREATE VIEW sys.dump_indices AS\n"
    4016             :                                         "  SELECT\n"
    4017             :                                         "    'CREATE ' || tpe || ' ' ||\n"
    4018             :                                         "      sys.DQ(ind) || ' ON ' || sys.DQ(sch) || '.' || sys.DQ(tbl) ||\n"
    4019             :                                         "      '(' || GROUP_CONCAT(col) || ');' stmt,\n"
    4020             :                                         "    sch schema_name,\n"
    4021             :                                         "    tbl table_name,\n"
    4022             :                                         "    ind index_name\n"
    4023             :                                         "    FROM sys.describe_indices GROUP BY ind, tpe, sch, tbl;\n"
    4024             :                                         "CREATE VIEW sys.dump_column_defaults AS\n"
    4025             :                                         "  SELECT 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ALTER COLUMN ' || sys.DQ(col) || ' SET DEFAULT ' || def || ';' stmt,\n"
    4026             :                                         "   sch schema_name,\n"
    4027             :                                         "   tbl table_name,\n"
    4028             :                                         "   col column_name\n"
    4029             :                                         "    FROM sys.describe_column_defaults;\n"
    4030             :                                         "CREATE VIEW sys.dump_foreign_keys AS\n"
    4031             :                                         "  SELECT\n"
    4032             :                                         "    'ALTER TABLE ' || sys.DQ(fk_s) || '.'|| sys.DQ(fk_t) || ' ADD CONSTRAINT ' || sys.DQ(fk) || ' ' ||\n"
    4033             :                                         "      'FOREIGN KEY(' || GROUP_CONCAT(sys.DQ(fk_c), ',') ||') ' ||\n"
    4034             :                                         "      'REFERENCES ' || sys.DQ(pk_s) || '.' || sys.DQ(pk_t) || '(' || GROUP_CONCAT(sys.DQ(pk_c), ',') || ') ' ||\n"
    4035             :                                         "      'ON DELETE ' || on_delete || ' ON UPDATE ' || on_update ||\n"
    4036             :                                         "      ';' stmt,\n"
    4037             :                                         "    fk_s foreign_schema_name,\n"
    4038             :                                         "    fk_t foreign_table_name,\n"
    4039             :                                         "    pk_s primary_schema_name,\n"
    4040             :                                         "    pk_t primary_table_name,\n"
    4041             :                                         "    fk key_name\n"
    4042             :                                         "    FROM sys.describe_foreign_keys GROUP BY fk_s, fk_t, pk_s, pk_t, fk, on_delete, on_update;\n"
    4043             :                                         "CREATE VIEW sys.dump_partition_tables AS\n"
    4044             :                                         "  SELECT\n"
    4045             :                                         "    sys.ALTER_TABLE(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
    4046             :                                         "      CASE \n"
    4047             :                                         "      WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
    4048             :                                         "      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"
    4049             :                                         "      WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
    4050             :                                         "      ELSE ''\n"
    4051             :                                         "      END ||\n"
    4052             :                                         "      CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
    4053             :                                         "      ';' stmt,\n"
    4054             :                                         "    m_sch merge_schema_name,\n"
    4055             :                                         "    m_tbl merge_table_name,\n"
    4056             :                                         "    p_sch partition_schema_name,\n"
    4057             :                                         "    p_tbl partition_table_name\n"
    4058             :                                         "    FROM sys.describe_partition_tables;\n"
    4059             :                                         "CREATE VIEW sys.dump_sequences AS\n"
    4060             :                                         "  SELECT\n"
    4061             :                                         "    'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT ' ||\n"
    4062             :                                         "      CASE WHEN \"s\" <> 0 THEN 'START WITH ' || \"rs\" ELSE '' END ||\n"
    4063             :                                         "      CASE WHEN \"inc\" <> 1 THEN ' INCREMENT BY ' || \"inc\" ELSE '' END ||\n"
    4064             :                                         "      CASE WHEN \"mi\" <> 0 THEN ' MINVALUE ' || \"mi\" ELSE '' END ||\n"
    4065             :                                         "      CASE WHEN \"ma\" <> 0 THEN ' MAXVALUE ' || \"ma\" ELSE '' END ||\n"
    4066             :                                         "      CASE WHEN \"cache\" <> 1 THEN ' CACHE ' || \"cache\" ELSE '' END ||\n"
    4067             :                                         "      CASE WHEN \"cycle\" THEN ' CYCLE' ELSE '' END || ';' stmt,\n"
    4068             :                                         "    sch schema_name,\n"
    4069             :                                         "    seq seqname\n"
    4070             :                                         "    FROM sys.describe_sequences;\n"
    4071             :                                         "CREATE VIEW sys.dump_start_sequences AS\n"
    4072             :                                         "  SELECT\n"
    4073             :                                         "    'UPDATE sys.sequences seq SET start = ' || s ||\n"
    4074             :                                         "      ' WHERE name = ' || sys.SQ(seq) ||\n"
    4075             :                                         "      ' AND schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = ' || sys.SQ(sch) || ');' stmt,\n"
    4076             :                                         "    sch schema_name,\n"
    4077             :                                         "    seq sequence_name\n"
    4078             :                                         "    FROM sys.describe_sequences;\n"
    4079             :                                         "CREATE VIEW sys.dump_functions AS\n"
    4080             :                                         "  SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
    4081             :                                         "   f.sch schema_name,\n"
    4082             :                                         "   f.fun function_name\n"
    4083             :                                         "    FROM sys.describe_functions f;\n"
    4084             :                                         "CREATE VIEW sys.dump_tables AS\n"
    4085             :                                         "  SELECT\n"
    4086             :                                         "    t.o o,\n"
    4087             :                                         "    CASE\n"
    4088             :                                         "      WHEN t.typ <> 'VIEW' THEN\n"
    4089             :                                         "      'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
    4090             :                                         "      ELSE\n"
    4091             :                                         "      t.opt\n"
    4092             :                                         "      END stmt,\n"
    4093             :                                         "    t.sch schema_name,\n"
    4094             :                                         "    t.tab table_name\n"
    4095             :                                         "    FROM sys.describe_tables t;\n"
    4096             :                                         "CREATE VIEW sys.dump_triggers AS\n"
    4097             :                                         "  SELECT sys.schema_guard(sch, tab, def) stmt,\n"
    4098             :                                         "   sch schema_name,\n"
    4099             :                                         "   tab table_name,\n"
    4100             :                                         "   tri trigger_name\n"
    4101             :                                         "    FROM sys.describe_triggers;\n"
    4102             :                                         "CREATE VIEW sys.dump_comments AS\n"
    4103             :                                         "  SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
    4104             :                                         "CREATE VIEW sys.dump_user_defined_types AS\n"
    4105             :                                         "  SELECT 'CREATE TYPE ' || sys.FQN(sch, sql_tpe) || ' EXTERNAL NAME ' || sys.DQ(ext_tpe) || ';' stmt,\n"
    4106             :                                         "   sch schema_name,\n"
    4107             :                                         "   sql_tpe type_name\n"
    4108             :                                         "    FROM sys.describe_user_defined_types;\n"
    4109             :                                         "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"
    4110             :                                         "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
    4111             :                                         "BEGIN\n"
    4112             :                                         "  RETURN\n"
    4113             :                                         "    CASE\n"
    4114             :                                         "    WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
    4115             :                                         "    'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
    4116             :                                         "    ELSE\n"
    4117             :                                         "    'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
    4118             :                                         "    END;\n"
    4119             :                                         "END;\n"
    4120             :                                         "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
    4121             :                                         "BEGIN\n"
    4122             :                                         "  DECLARE k INT;\n"
    4123             :                                         "  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"
    4124             :                                         "  IF k IS NOT NULL THEN\n"
    4125             :                                         "    DECLARE cname STRING;\n"
    4126             :                                         "    DECLARE ctype STRING;\n"
    4127             :                                         "    SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4128             :                                         "    SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4129             :                                         "    DECLARE COPY_INTO_STMT STRING;\n"
    4130             :                                         "    DECLARE _cnt INT;\n"
    4131             :                                         "    SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
    4132             :                                         "    IF _cnt > 0 THEN\n"
    4133             :                                         "      SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
    4134             :                                         "      DECLARE SELECT_DATA_STMT STRING;\n"
    4135             :                                         "      SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
    4136             :                                         "      DECLARE M INT;\n"
    4137             :                                         "      SET M = (SELECT MAX(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
    4138             :                                         "      WHILE (k < M) DO\n"
    4139             :                                         "  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"
    4140             :                                         "        SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
    4141             :                                         "  SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
    4142             :                                         "  SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
    4143             :                                         "  SET SELECT_DATA_STMT = SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype);\n"
    4144             :                                         "      END WHILE;\n"
    4145             :                                         "      SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
    4146             :                                         "      SET SELECT_DATA_STMT = SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl);\n"
    4147             :                                         "      insert into sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
    4148             :                                         "      CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
    4149             :                                         "    END IF;\n"
    4150             :                                         "  END IF;\n"
    4151             :                                         "END;\n"
    4152             :                                         "CREATE PROCEDURE sys.dump_table_data()\n"
    4153             :                                         "BEGIN\n"
    4154             :                                         "  DECLARE i INT;\n"
    4155             :                                         "  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"
    4156             :                                         "  IF i IS NOT NULL THEN\n"
    4157             :                                         "    DECLARE M INT;\n"
    4158             :                                         "    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"
    4159             :                                         "    DECLARE sch STRING;\n"
    4160             :                                         "    DECLARE tbl STRING;\n"
    4161             :                                         "    WHILE i < M DO\n"
    4162             :                                         "      set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4163             :                                         "      set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4164             :                                         "      CALL sys.dump_table_data(sch, tbl);\n"
    4165             :                                         "      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"
    4166             :                                         "    END WHILE;\n"
    4167             :                                         "    set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4168             :                                         "    set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
    4169             :                                         "    CALL sys.dump_table_data(sch, tbl);\n"
    4170             :                                         "  END IF;\n"
    4171             :                                         "END;\n"
    4172             :                                         "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
    4173             :                                         "BEGIN\n"
    4174             :                                         "  SET SCHEMA sys;\n"
    4175             :                                         "  TRUNCATE sys.dump_statements;\n"
    4176             :                                         "  INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
    4177             :                                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'SET SCHEMA \"sys\";');\n"
    4178             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
    4179             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
    4180             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
    4181             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
    4182             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
    4183             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
    4184             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
    4185             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
    4186             :                                         "                              FROM (\n"
    4187             :                                         "                                SELECT f.o, f.stmt FROM sys.dump_functions f\n"
    4188             :                                         "                                 UNION\n"
    4189             :                                         "                                SELECT t.o, t.stmt FROM sys.dump_tables t\n"
    4190             :                                         "                              ) AS stmts(o, s);\n"
    4191             :                                         "  IF NOT DESCRIBE THEN\n"
    4192             :                                         "    CALL sys.dump_table_data();\n"
    4193             :                                         "  END IF;\n"
    4194             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
    4195             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
    4196             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
    4197             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
    4198             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
    4199             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
    4200             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
    4201             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
    4202             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
    4203             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
    4204             :                                         "  INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
    4205             :                                         "  INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
    4206             :                                         "  RETURN sys.dump_statements;\n"
    4207             :                                         "END;\n");
    4208          16 :         pos += snprintf(buf + pos, bufsize - pos,
    4209             :                                         "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);
    4210          16 :         pos += snprintf(buf + pos, bufsize - pos,
    4211             :                                         "update sys.functions set system = true where system <> true and name in ('dump_database') and schema_id = 2000 and type = %d;\n", F_UNION);
    4212          16 :         pos += snprintf(buf + pos, bufsize - pos,
    4213             :                                         "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);
    4214          16 :         pos += snprintf(buf + pos, bufsize - pos,
    4215             :                                         "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");
    4216             : 
    4217             :         /* 80_udf.sql (removed) */
    4218          16 :         pos += snprintf(buf + pos, bufsize - pos,
    4219             :                                         "drop function sys.reverse(string);\n"
    4220             :                                         "drop all function sys.fuse;\n");
    4221             : 
    4222             :         /* 26_sysmon.sql */
    4223          16 :         pos += snprintf(buf + pos, bufsize - pos,
    4224             :                                         "create procedure sys.vacuum(sname string, tname string, cname string)\n"
    4225             :                                         "  external name sql.vacuum;\n"
    4226             :                                         "create procedure sys.vacuum(sname string, tname string, cname string, interval int)\n"
    4227             :                                         "  external name sql.vacuum;\n"
    4228             :                                         "create procedure sys.stop_vacuum(sname string, tname string, cname string)\n"
    4229             :                                         "  external name sql.stop_vacuum;\n");
    4230          16 :         pos += snprintf(buf + pos, bufsize - pos,
    4231             :                                         "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);
    4232             : 
    4233          16 :         assert(pos < bufsize);
    4234          16 :         printf("Running database upgrade commands:\n%s\n", buf);
    4235          16 :         err = SQLstatementIntern(c, buf, "update", true, false, NULL);
    4236             : 
    4237          16 :         GDKfree(buf);
    4238          16 :         return err;             /* usually MAL_SUCCEED */
    4239             : }
    4240             : 
    4241             : int
    4242          69 : SQLupgrades(Client c, mvc *m)
    4243             : {
    4244             :         sql_subtype tp;
    4245             :         sql_subfunc *f;
    4246          69 :         char *err, *prev_schema = GDKstrdup(get_string_global_var(m, "current_schema"));
    4247          69 :         sql_schema *s = mvc_bind_schema(m, "sys");
    4248             :         sql_table *t;
    4249          69 :         bool systabfixed = false;
    4250             : 
    4251          69 :         if (prev_schema == NULL) {
    4252           0 :                 TRC_CRITICAL(SQL_PARSER, "Allocation failure while running SQL upgrades\n");
    4253           0 :                 return -1;
    4254             :         }
    4255             : 
    4256             : #ifdef HAVE_HGE
    4257          69 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    4258          69 :         if (!sql_bind_func(m, s->base.name, "var_pop", &tp, NULL, F_AGGR)) {
    4259           8 :                 m->session->status = 0; /* if the function was not found clean the error */
    4260           8 :                 m->errstr[0] = '\0';
    4261           8 :                 if ((err = sql_update_hugeint(c, m, prev_schema, &systabfixed)) != NULL) {
    4262           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4263           0 :                         freeException(err);
    4264           0 :                         GDKfree(prev_schema);
    4265           0 :                         return -1;
    4266             :                 }
    4267             :         }
    4268             : #endif
    4269             : 
    4270          69 :         f = sql_bind_func_(m, s->base.name, "env", NULL, F_UNION);
    4271          69 :         m->session->status = 0; /* if the function was not found clean the error */
    4272          69 :         m->errstr[0] = '\0';
    4273          69 :         sqlstore *store = m->session->tr->store;
    4274          69 :         if (f && sql_privilege(m, ROLE_PUBLIC, f->func->base.id, PRIV_EXECUTE) != PRIV_EXECUTE) {
    4275           0 :                 sql_table *privs = find_sql_table(m->session->tr, s, "privileges");
    4276           0 :                 int pub = ROLE_PUBLIC, p = PRIV_EXECUTE, zero = 0, res;
    4277             : 
    4278           0 :                 if ((res = store->table_api.table_insert(m->session->tr, privs, &f->func->base.id, &pub, &p, &zero, &zero)) != LOG_OK) {
    4279           0 :                         TRC_CRITICAL(SQL_PARSER, "Privilege creation during upgrade failed\n");
    4280           0 :                         GDKfree(prev_schema);
    4281           0 :                         return -1;
    4282             :                 }
    4283             :         }
    4284             : 
    4285          69 :         if (sql_bind_func(m, s->base.name, "dependencies_schemas_on_users", NULL, NULL, F_UNION)
    4286           0 :          && sql_bind_func(m, s->base.name, "dependencies_owners_on_schemas", NULL, NULL, F_UNION)
    4287           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_views", NULL, NULL, F_UNION)
    4288           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_indexes", NULL, NULL, F_UNION)
    4289           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_triggers", NULL, NULL, F_UNION)
    4290           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_foreignkeys", NULL, NULL, F_UNION)
    4291           0 :          && sql_bind_func(m, s->base.name, "dependencies_tables_on_functions", NULL, NULL, F_UNION)
    4292           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_views", NULL, NULL, F_UNION)
    4293           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_keys", NULL, NULL, F_UNION)
    4294           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_indexes", NULL, NULL, F_UNION)
    4295           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_functions", NULL, NULL, F_UNION)
    4296           0 :          && sql_bind_func(m, s->base.name, "dependencies_columns_on_triggers", NULL, NULL, F_UNION)
    4297           0 :          && sql_bind_func(m, s->base.name, "dependencies_views_on_functions", NULL, NULL, F_UNION)
    4298           0 :          && sql_bind_func(m, s->base.name, "dependencies_views_on_triggers", NULL, NULL, F_UNION)
    4299           0 :          && sql_bind_func(m, s->base.name, "dependencies_functions_on_functions", NULL, NULL, F_UNION)
    4300           0 :          && sql_bind_func(m, s->base.name, "dependencies_functions_on_triggers", NULL, NULL, F_UNION)
    4301           0 :          && sql_bind_func(m, s->base.name, "dependencies_keys_on_foreignkeys", NULL, NULL, F_UNION)        ) {
    4302           0 :                 if ((err = sql_drop_functions_dependencies_Xs_on_Ys(c, prev_schema)) != NULL) {
    4303           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4304           0 :                         freeException(err);
    4305           0 :                         GDKfree(prev_schema);
    4306           0 :                         return -1;
    4307             :                 }
    4308             :         } else {
    4309          69 :                 m->session->status = 0; /* if the function was not found clean the error */
    4310          69 :                 m->errstr[0] = '\0';
    4311             :         }
    4312             : 
    4313             : 
    4314          69 :         if ((t = mvc_bind_table(m, s, "systemfunctions")) != NULL &&
    4315          69 :             t->type == tt_table) {
    4316           0 :                 if (!systabfixed &&
    4317           0 :                     (err = sql_fix_system_tables(c, m, prev_schema)) != NULL) {
    4318           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4319           0 :                         freeException(err);
    4320           0 :                         GDKfree(prev_schema);
    4321           0 :                         return -1;
    4322             :                 }
    4323           0 :                 systabfixed = true;
    4324           0 :                 if ((err = sql_update_apr2019(c, m, prev_schema)) != NULL) {
    4325           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4326           0 :                         freeException(err);
    4327           0 :                         GDKfree(prev_schema);
    4328           0 :                         return -1;
    4329             :                 }
    4330             :         }
    4331             : 
    4332             :         /* when function storagemodel() exists and views tablestorage
    4333             :          * and schemastorage don't, then upgrade storagemodel to match
    4334             :          * 75_storagemodel.sql */
    4335          69 :         if (sql_bind_func(m, s->base.name, "storagemodel", NULL, NULL, F_UNION)
    4336           0 :          && (t = mvc_bind_table(m, s, "tablestorage")) == NULL
    4337           0 :          && (t = mvc_bind_table(m, s, "schemastorage")) == NULL ) {
    4338           0 :                 if ((err = sql_update_storagemodel(c, m, prev_schema, false)) != NULL) {
    4339           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4340           0 :                         freeException(err);
    4341           0 :                         GDKfree(prev_schema);
    4342           0 :                         return -1;
    4343             :                 }
    4344             :         } else {
    4345          69 :                 m->session->status = 0; /* if the function was not found clean the error */
    4346          69 :                 m->errstr[0] = '\0';
    4347             :         }
    4348             : 
    4349          69 :         if ((err = sql_update_apr2019_sp1(c)) != NULL) {
    4350           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4351           0 :                 freeException(err);
    4352           0 :                 GDKfree(prev_schema);
    4353           0 :                 return -1;
    4354             :         }
    4355             : 
    4356          69 :         if (sql_bind_func(m, s->base.name, "times", NULL, NULL, F_PROC)) {
    4357           0 :                 if ((err = sql_update_apr2019_sp2(c, m, prev_schema, &systabfixed)) != NULL) {
    4358           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4359           0 :                         freeException(err);
    4360           0 :                         GDKfree(prev_schema);
    4361           0 :                         return -1;
    4362             :                 }
    4363             :         } else {
    4364          69 :                 m->session->status = 0; /* if the function was not found clean the error */
    4365          69 :                 m->errstr[0] = '\0';
    4366             :         }
    4367             : 
    4368          69 :         sql_find_subtype(&tp, "varchar", 0, 0);
    4369          69 :         if (!sql_bind_func3(m, s->base.name, "deltas", &tp, &tp, &tp, F_UNION)) {
    4370           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    4371           0 :                 m->errstr[0] = '\0';
    4372           0 :                 if ((err = sql_update_nov2019_missing_dependencies(c, m)) != NULL) {
    4373           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4374           0 :                         freeException(err);
    4375           0 :                         GDKfree(prev_schema);
    4376           0 :                         return -1;
    4377             :                 }
    4378           0 :                 if (!systabfixed &&
    4379           0 :                     (err = sql_fix_system_tables(c, m, prev_schema)) != NULL) {
    4380           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4381           0 :                         freeException(err);
    4382           0 :                         GDKfree(prev_schema);
    4383           0 :                         return -1;
    4384             :                 }
    4385           0 :                 systabfixed = true;
    4386           0 :                 if ((err = sql_update_nov2019(c, m, prev_schema, &systabfixed)) != NULL) {
    4387           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4388           0 :                         freeException(err);
    4389           0 :                         GDKfree(prev_schema);
    4390           0 :                         return -1;
    4391             :                 }
    4392             :         }
    4393             : 
    4394             : #ifdef HAVE_HGE
    4395          69 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    4396          69 :         if (!sql_bind_func(m, s->base.name, "median_avg", &tp, NULL, F_AGGR)) {
    4397           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    4398           0 :                 m->errstr[0] = '\0';
    4399           0 :                 if ((err = sql_update_nov2019_sp1_hugeint(c, m, prev_schema, &systabfixed)) != NULL) {
    4400           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4401           0 :                         freeException(err);
    4402           0 :                         GDKfree(prev_schema);
    4403           0 :                         return -1;
    4404             :                 }
    4405             :         }
    4406             : #endif
    4407             : 
    4408          69 :         if (!sql_bind_func(m, s->base.name, "suspend_log_flushing", NULL, NULL, F_PROC)) {
    4409           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    4410           0 :                 m->errstr[0] = '\0';
    4411           0 :                 if ((err = sql_update_jun2020(c, m, prev_schema, &systabfixed)) != NULL) {
    4412           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4413           0 :                         freeException(err);
    4414           0 :                         GDKfree(prev_schema);
    4415           0 :                         return -1;
    4416             :                 }
    4417             :         }
    4418             : 
    4419          69 :         if ((err = sql_update_jun2020_bam(c, m, prev_schema)) != NULL) {
    4420           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4421           0 :                 freeException(err);
    4422           0 :                 GDKfree(prev_schema);
    4423           0 :                 return -1;
    4424             :         }
    4425             : 
    4426             : #ifdef HAVE_HGE
    4427          69 :         sql_find_subtype(&tp, "hugeint", 0, 0);
    4428          69 :         if (!sql_bind_func(m, s->base.name, "covar_pop", &tp, &tp, F_AGGR)) {
    4429           0 :                 m->session->status = 0; /* if the function was not found clean the error */
    4430           0 :                 m->errstr[0] = '\0';
    4431           0 :                 if ((err = sql_update_jun2020_sp1_hugeint(c, prev_schema)) != NULL) {
    4432           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4433           0 :                         freeException(err);
    4434           0 :                         GDKfree(prev_schema);
    4435           0 :                         return -1;
    4436             :                 }
    4437             :         }
    4438             : #endif
    4439             : 
    4440          69 :         if ((err = sql_update_jun2020_sp2(c, m, prev_schema, &systabfixed)) != NULL) {
    4441           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4442           0 :                 freeException(err);
    4443           0 :                 GDKfree(prev_schema);
    4444           0 :                 return -1;
    4445             :         }
    4446             : 
    4447          69 :         sql_find_subtype(&tp, "varchar", 0, 0);
    4448          69 :         if (sql_bind_func(m, s->base.name, "lidarattach", &tp, NULL, F_PROC)) {
    4449           0 :                 if ((err = sql_update_oscar_lidar(c)) != NULL) {
    4450           0 :                         TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4451           0 :                         freeException(err);
    4452           0 :                         GDKfree(prev_schema);
    4453           0 :                         return -1;
    4454             :                 }
    4455             :         } else {
    4456          69 :                 m->session->status = 0; /* if the function was not found clean the error */
    4457          69 :                 m->errstr[0] = '\0';
    4458             :         }
    4459             : 
    4460          69 :         if ((err = sql_update_oscar(c, m, prev_schema, &systabfixed)) != NULL) {
    4461           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4462           0 :                 freeException(err);
    4463           0 :                 GDKfree(prev_schema);
    4464           0 :                 return -1;
    4465             :         }
    4466             : 
    4467          69 :         if ((err = sql_update_oct2020(c, m, prev_schema, &systabfixed)) != NULL) {
    4468           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4469           0 :                 freeException(err);
    4470           0 :                 GDKfree(prev_schema);
    4471           0 :                 return -1;
    4472             :         }
    4473             : 
    4474          69 :         if ((err = sql_update_oct2020_sp1(c, m, prev_schema, &systabfixed)) != NULL) {
    4475           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4476           0 :                 freeException(err);
    4477           0 :                 GDKfree(prev_schema);
    4478           0 :                 return -1;
    4479             :         }
    4480             : 
    4481          69 :         if ((err = sql_update_jul2021(c, m, prev_schema, &systabfixed)) != NULL) {
    4482           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4483           0 :                 freeException(err);
    4484           0 :                 GDKfree(prev_schema);
    4485           0 :                 return -1;
    4486             :         }
    4487             : 
    4488          69 :         if ((err = sql_update_jul2021_5(c, m, prev_schema, &systabfixed)) != NULL) {
    4489           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4490           0 :                 freeException(err);
    4491           0 :                 GDKfree(prev_schema);
    4492           0 :                 return -1;
    4493             :         }
    4494             : 
    4495          69 :         if ((err = sql_update_default(c, m, prev_schema, &systabfixed)) != NULL) {
    4496           0 :                 TRC_CRITICAL(SQL_PARSER, "%s\n", err);
    4497           0 :                 freeException(err);
    4498           0 :                 GDKfree(prev_schema);
    4499           0 :                 return -1;
    4500             :         }
    4501             : 
    4502          69 :         GDKfree(prev_schema);
    4503          69 :         return 0;
    4504             : }

Generated by: LCOV version 1.14