Changeset: fb14248942a9 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=fb14248942a9
Modified Files:
        clients/mapiclient/dump.c
        sql/backends/monet5/sql_upgrades.c
        sql/scripts/25_debug.sql
        sql/test/emptydb-upgrade-chain-hge/Tests/check.stable.out.int128
        sql/test/emptydb-upgrade-chain-hge/Tests/upgrade.stable.out.int128
        sql/test/emptydb-upgrade-chain/Tests/check.stable.out
        sql/test/emptydb-upgrade-chain/Tests/check.stable.out.32bit
        sql/test/emptydb-upgrade-chain/Tests/check.stable.out.int128
        sql/test/emptydb-upgrade-chain/Tests/upgrade.stable.out
        sql/test/emptydb-upgrade-chain/Tests/upgrade.stable.out.int128
        sql/test/emptydb-upgrade-hge/Tests/check.stable.out.int128
        sql/test/emptydb-upgrade-hge/Tests/upgrade.stable.out.int128
        sql/test/emptydb-upgrade/Tests/check.stable.out
        sql/test/emptydb-upgrade/Tests/check.stable.out.32bit
        sql/test/emptydb-upgrade/Tests/check.stable.out.int128
        sql/test/emptydb-upgrade/Tests/upgrade.stable.out
        sql/test/emptydb-upgrade/Tests/upgrade.stable.out.int128
        sql/test/emptydb/Tests/check.SQL.py
        sql/test/emptydb/Tests/check.stable.out
        sql/test/emptydb/Tests/check.stable.out.32bit
        sql/test/emptydb/Tests/check.stable.out.int128
        sql/test/testdb-upgrade-chain-hge/Tests/upgrade.stable.out.int128
        sql/test/testdb-upgrade-chain/Tests/upgrade.stable.out
        sql/test/testdb-upgrade-chain/Tests/upgrade.stable.out.int128
        sql/test/testdb-upgrade-hge/Tests/upgrade.stable.out.int128
        sql/test/testdb-upgrade/Tests/upgrade.stable.out
        sql/test/testdb-upgrade/Tests/upgrade.stable.out.int128
Branch: Jul2017
Log Message:

On certain upgrades, fix sys.types, sys.functions, sys.args and some other 
upgrade fixes.

There is now a function to update the system tables after internal
types and/or functions were changed.


diffs (truncated from 6798 to 300 lines):

diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -1231,7 +1231,7 @@ dump_functions(Mapi mid, stream *toConso
                      "s.id = f.schema_id "
                      "%s%s"
                      "%s%s%s%s%s%s"
-               "ORDER BY f.id";
+               "ORDER BY f.func";
        MapiHdl hdl;
        char *q;
        size_t l;
diff --git a/sql/backends/monet5/sql_upgrades.c 
b/sql/backends/monet5/sql_upgrades.c
--- a/sql/backends/monet5/sql_upgrades.c
+++ b/sql/backends/monet5/sql_upgrades.c
@@ -22,9 +22,11 @@
 #define printf(fmt,...) ((void) 0)
 #endif
 
-#ifdef HAVE_HGE
+/* this function can be used to recreate the system tables (types,
+ * functions, args) when internal types and/or functions have changed
+ * (i.e. the ones in sql_types.c) */
 static str
-sql_update_hugeint(Client c, mvc *sql)
+sql_fix_system_tables(Client c, mvc *sql)
 {
        size_t bufsize = 1000000, pos = 0;
        char *buf = GDKmalloc(bufsize), *err = NULL;
@@ -33,36 +35,102 @@ sql_update_hugeint(Client c, mvc *sql)
        sql_schema *s;
 
        if (buf == NULL)
-               throw(SQL, "sql_update_hugeint", MAL_MALLOC_FAIL);
-
+               throw(SQL, "sql_fix_system_tables", MAL_MALLOC_FAIL);
        s = mvc_bind_schema(sql, "sys");
-
        pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
 
-       pos += snprintf(buf + pos, bufsize - pos, "delete from sys.functions 
where id < 2000;\n");
-       pos += snprintf(buf + pos, bufsize - pos, "delete from sys.args where 
func_id not in (select id from sys.functions);\n");
+       pos += snprintf(buf + pos, bufsize - pos,
+                       "delete from sys.dependencies where id < 2000;\n");
+
+       /* recreate internal types */
+       pos += snprintf(buf + pos, bufsize - pos,
+                       "delete from sys.types where id < 2000;\n");
+       for (n = types->h; n; n = n->next) {
+               sql_type *t = n->data;
+
+               if (t->base.id >= 2000)
+                       continue;
+
+               pos += snprintf(buf + pos, bufsize - pos,
+                               "insert into sys.types values"
+                               " (%d, '%s', '%s', %u, %u, %d, %d, %d);\n",
+                               t->base.id, t->base.name, t->sqlname, t->digits,
+                               t->scale, t->radix, t->eclass,
+                               t->s ? t->s->base.id : s->base.id);
+       }
+
+       /* recreate internal functions */
+       pos += snprintf(buf + pos, bufsize - pos,
+                       "delete from sys.functions where id < 2000;\n"
+                       "delete from sys.args where func_id not in"
+                       " (select id from sys.functions);\n");
        for (n = funcs->h; n; n = n->next) {
-               sql_func *f = n->data;
+               sql_func *func = n->data;
                int number = 0;
-               sql_arg *a;
+               sql_arg *arg;
                node *m;
 
-               if (f->base.id >= 2000)
+               if (func->base.id >= 2000)
                        continue;
 
-               pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.functions values (%d, '%s', '%s', '%s', %d, %d, %s, %s, %s, %d);\n", 
f->base.id, f->base.name, f->imp, f->mod, FUNC_LANG_INT, f->type, 
f->side_effect ? "true" : "false", f->varres ? "true" : "false", f->vararg ? 
"true" : "false", f->s ? f->s->base.id : s->base.id);
-               if (f->res) {
-                       for (m = f->res->h; m; m = m->next, number++) {
-                               a = m->data;
-                               pos += snprintf(buf + pos, bufsize - pos, 
"insert into sys.args values (%d, %d, 'res_%d', '%s', %u, %u, %d, %d);\n", 
store_next_oid(), f->base.id, number, a->type.type->sqlname, a->type.digits, 
a->type.scale, a->inout, number);
+               pos += snprintf(buf + pos, bufsize - pos,
+                               "insert into sys.functions values"
+                               " (%d, '%s', '%s', '%s',"
+                               " %d, %d, %s, %s, %s, %d);\n",
+                               func->base.id, func->base.name,
+                               func->imp, func->mod, FUNC_LANG_INT,
+                               func->type,
+                               func->side_effect ? "true" : "false",
+                               func->varres ? "true" : "false",
+                               func->vararg ? "true" : "false",
+                               func->s ? func->s->base.id : s->base.id);
+               if (func->res) {
+                       for (m = func->res->h; m; m = m->next, number++) {
+                               arg = m->data;
+                               pos += snprintf(buf + pos, bufsize - pos,
+                                               "insert into sys.args"
+                                               " values"
+                                               " (%d, %d, 'res_%d',"
+                                               " '%s', %u, %u, %d,"
+                                               " %d);\n",
+                                               store_next_oid(),
+                                               func->base.id,
+                                               number,
+                                               arg->type.type->sqlname,
+                                               arg->type.digits,
+                                               arg->type.scale,
+                                               arg->inout, number);
                        }
                }
-               for (m = f->ops->h; m; m = m->next, number++) {
-                       a = m->data;
-                       if (a->name)
-                               pos += snprintf(buf + pos, bufsize - pos, 
"insert into sys.args values (%d, %d, '%s', '%s', %u, %u, %d, %d);\n", 
store_next_oid(), f->base.id, a->name, a->type.type->sqlname, a->type.digits, 
a->type.scale, a->inout, number);
+               for (m = func->ops->h; m; m = m->next, number++) {
+                       arg = m->data;
+                       if (arg->name)
+                               pos += snprintf(buf + pos, bufsize - pos,
+                                               "insert into sys.args"
+                                               " values"
+                                               " (%d, %d, '%s', '%s',"
+                                               " %u, %u, %d, %d);\n",
+                                               store_next_oid(),
+                                               func->base.id,
+                                               arg->name,
+                                               arg->type.type->sqlname,
+                                               arg->type.digits,
+                                               arg->type.scale,
+                                               arg->inout, number);
                        else
-                               pos += snprintf(buf + pos, bufsize - pos, 
"insert into sys.args values (%d, %d, 'arg_%d', '%s', %u, %u, %d, %d);\n", 
store_next_oid(), f->base.id, number, a->type.type->sqlname, a->type.digits, 
a->type.scale, a->inout, number);
+                               pos += snprintf(buf + pos, bufsize - pos,
+                                               "insert into sys.args"
+                                               " values"
+                                               " (%d, %d, 'arg_%d',"
+                                               " '%s', %u, %u, %d,"
+                                               " %d);\n",
+                                               store_next_oid(),
+                                               func->base.id,
+                                               number,
+                                               arg->type.type->sqlname,
+                                               arg->type.digits,
+                                               arg->type.scale,
+                                               arg->inout, number);
                }
        }
        for (n = aggrs->h; n; n = n->next) {
@@ -72,18 +140,69 @@ sql_update_hugeint(Client c, mvc *sql)
                if (aggr->base.id >= 2000)
                        continue;
 
-               pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.functions values (%d, '%s', '%s', '%s', %d, %d, false, %s, %s, %d);\n", 
aggr->base.id, aggr->base.name, aggr->imp, aggr->mod, FUNC_LANG_INT, 
aggr->type, aggr->varres ? "true" : "false", aggr->vararg ? "true" : "false", 
aggr->s ? aggr->s->base.id : s->base.id);
+               pos += snprintf(buf + pos, bufsize - pos,
+                               "insert into sys.functions values"
+                               " (%d, '%s', '%s', '%s', %d, %d, false,"
+                               " %s, %s, %d);\n",
+                               aggr->base.id, aggr->base.name, aggr->imp,
+                               aggr->mod, FUNC_LANG_INT, aggr->type,
+                               aggr->varres ? "true" : "false",
+                               aggr->vararg ? "true" : "false",
+                               aggr->s ? aggr->s->base.id : s->base.id);
                arg = aggr->res->h->data;
-               pos += snprintf(buf + pos, bufsize - pos, "insert into sys.args 
values (%d, %d, 'res', '%s', %u, %u, %d, 0);\n", store_next_oid(), 
aggr->base.id, arg->type.type->sqlname, arg->type.digits, arg->type.scale, 
arg->inout);
+               pos += snprintf(buf + pos, bufsize - pos,
+                               "insert into sys.args values"
+                               " (%d, %d, 'res', '%s', %u, %u, %d, 0);\n",
+                               store_next_oid(), aggr->base.id,
+                               arg->type.type->sqlname, arg->type.digits,
+                               arg->type.scale, arg->inout);
                if (aggr->ops->h) {
                        arg = aggr->ops->h->data;
-
-                       pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.args values (%d, %d, 'arg', '%s', %u, %u, %d, 1);\n", store_next_oid(), 
aggr->base.id, arg->type.type->sqlname, arg->type.digits, arg->type.scale, 
arg->inout);
+                       pos += snprintf(buf + pos, bufsize - pos,
+                                       "insert into sys.args values"
+                                       " (%d, %d, 'arg', '%s', %u,"
+                                       " %u, %d, 1);\n",
+                                       store_next_oid(), aggr->base.id,
+                                       arg->type.type->sqlname,
+                                       arg->type.digits, arg->type.scale,
+                                       arg->inout);
                }
        }
-       pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.systemfunctions (select id from sys.functions where id < 2000 and id not in 
(select function_id from sys.systemfunctions));\n");
-       pos += snprintf(buf + pos, bufsize - pos, "delete from 
sys.systemfunctions where function_id not in (select id from 
sys.functions);\n");
+       pos += snprintf(buf + pos, bufsize - pos,
+                       "delete from sys.systemfunctions where function_id < 
2000;\n"
+                       "insert into sys.systemfunctions"
+                       " (select id from sys.functions where id < 2000);\n");
 
+       if (schema)
+               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
+
+       assert(pos < bufsize);
+       printf("Running database upgrade commands:\n%s\n", buf);
+       err = SQLstatementIntern(c, &buf, "update", 1, 0, NULL);
+       GDKfree(buf);
+       return err;             /* usually MAL_SUCCEED */
+}
+
+#ifdef HAVE_HGE
+static str
+sql_update_hugeint(Client c, mvc *sql)
+{
+       size_t bufsize = 8192, pos = 0;
+       char *buf, *err;
+       char *schema;
+       sql_schema *s;
+
+       if ((err = sql_fix_system_tables(c, sql)) != NULL)
+               return err;
+
+       if ((buf = GDKmalloc(bufsize)) == NULL)
+               throw(SQL, "sql_update_hugeint", MAL_MALLOC_FAIL);
+
+       schema = stack_get_string(sql, "current_schema");
+
+       s = mvc_bind_schema(sql, "sys");
+
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
 
        pos += snprintf(buf + pos, bufsize - pos,
                        "create function fuse(one bigint, two bigint)\n"
@@ -139,27 +258,11 @@ sql_update_hugeint(Client c, mvc *sql)
                        "insert into sys.systemfunctions (select id from 
sys.functions where name = 'filter' and schema_id = (select id from sys.schemas 
where name = 'json') and id not in (select function_id from 
sys.systemfunctions));\n"
                        "update sys._tables set system = true where name = 
'tablestoragemodel' and schema_id = (select id from sys.schemas where name = 
'sys');\n");
 
-       {
-               node *n;
-               sql_type *t;
+       if (s != NULL) {
+               sql_table *t;
 
-               for (n = types->h; n; n = n->next) {
-                       t = n->data;
-                       if (t->base.id < 2000 &&
-                           strcmp(t->base.name, "hge") == 0)
-                               pos += snprintf(buf + pos, bufsize - pos, 
"insert into sys.types values (%d, '%s', '%s', %u, %u, %d, %d, %d);\n", 
t->base.id, t->base.name, t->sqlname, t->digits, t->scale, t->radix, t->eclass, 
t->s ? t->s->base.id : s->base.id);
-               }
-       }
-
-       {
-               sql_schema *s;
-
-               if ((s = mvc_bind_schema(sql, "sys")) != NULL) {
-                       sql_table *t;
-
-                       if ((t = mvc_bind_table(sql, s, "tablestoragemodel")) 
!= NULL)
-                               t->system = 0;
-               }
+               if ((t = mvc_bind_table(sql, s, "tablestoragemodel")) != NULL)
+                       t->system = 0;
        }
 
        pos += snprintf(buf + pos, bufsize - pos,
@@ -239,70 +342,18 @@ create function sys.\"epoch\"(ts TIMESTA
 static str
 sql_update_jun2016(Client c, mvc *sql)
 {
-       size_t bufsize = 1000000, pos = 0;
-       char *buf = GDKmalloc(bufsize), *err = NULL;
+       size_t bufsize = 10000, pos = 0;
+       char *buf, *err;
        char *schema = stack_get_string(sql, "current_schema");
-       node *n;
        sql_schema *s;
 
-       if (buf == NULL)
-               throw(SQL, "sql_update_jun2016", MAL_MALLOC_FAIL);
-       s = mvc_bind_schema(sql, "sys");
-       pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
-
-       pos += snprintf(buf + pos, bufsize - pos, "delete from sys.dependencies 
where id < 2000;\n");
-       pos += snprintf(buf + pos, bufsize - pos, "delete from sys.types where 
id < 2000;\n");
-       for (n = types->h; n; n = n->next) {
-               sql_type *t = n->data;
-
-               if (t->base.id >= 2000)
-                       continue;
-
-               pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.types values (%d, '%s', '%s', %u, %u, %d, %d, %d);\n", t->base.id, 
t->base.name, t->sqlname, t->digits, t->scale, t->radix, t->eclass, t->s ? 
t->s->base.id : s->base.id);
-       }
-       pos += snprintf(buf + pos, bufsize - pos, "delete from sys.functions 
where id < 2000;\n");
-       pos += snprintf(buf + pos, bufsize - pos, "delete from sys.args where 
func_id not in (select id from sys.functions);\n");
-       for (n = funcs->h; n; n = n->next) {
-               sql_func *f = n->data;
-               int number = 0;
-               sql_arg *a;
-               node *m;
-
-               if (f->base.id >= 2000)
-                       continue;
+       if ((err = sql_fix_system_tables(c, sql)) != NULL)
+               return err;
 
-               pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.functions values (%d, '%s', '%s', '%s', %d, %d, %s, %s, %s, %d);\n", 
f->base.id, f->base.name, f->imp, f->mod, FUNC_LANG_INT, f->type, 
f->side_effect ? "true" : "false", f->varres ? "true" : "false", f->vararg ? 
"true" : "false", f->s ? f->s->base.id : s->base.id);
-               if (f->res) {
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to