LCOV - code coverage report
Current view: top level - sql/storage/bat - bat_logger.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 170 1709 9.9 %
Date: 2024-04-25 20:03:45 Functions: 19 24 79.2 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include "monetdb_config.h"
      14             : #include "bat_logger.h"
      15             : #include "bat_utils.h"
      16             : #include "sql_types.h" /* EC_POS */
      17             : #include "gdk_logger_internals.h"
      18             : #include "mutils.h"
      19             : 
      20             : #define CATALOG_NOV2019 52203   /* first in Apr2019 */
      21             : #define CATALOG_JUN2020 52204   /* first in Jun2020 */
      22             : #define CATALOG_JUN2020_MMT 52206       /* only in Jun2020-mmt */
      23             : #define CATALOG_OCT2020 52205   /* first in Oct2020 */
      24             : #define CATALOG_JUL2021 52300   /* first in Jul2021 */
      25             : #define CATALOG_JAN2022 52301   /* first in Jan2022 */
      26             : 
      27             : /* Note, CATALOG version 52300 is the first one where the basic system
      28             :  * tables (the ones created in store.c) have fixed and unchangeable
      29             :  * ids. */
      30             : 
      31             : /* return GDK_SUCCEED if we can handle the upgrade from oldversion to
      32             :  * newversion */
      33             : static gdk_return
      34           0 : bl_preversion(sqlstore *store, int oldversion, int newversion)
      35             : {
      36           0 :         (void)newversion;
      37             : 
      38             : #ifdef CATALOG_NOV2019
      39           0 :         if (oldversion == CATALOG_NOV2019) {
      40             :                 /* upgrade to default releases */
      41           0 :                 store->catalog_version = oldversion;
      42           0 :                 return GDK_SUCCEED;
      43             :         }
      44             : #endif
      45             : 
      46             : #ifdef CATALOG_JUN2020
      47           0 :         if (oldversion == CATALOG_JUN2020) {
      48             :                 /* upgrade to default releases */
      49           0 :                 store->catalog_version = oldversion;
      50           0 :                 return GDK_SUCCEED;
      51             :         }
      52             : #endif
      53             : 
      54             : #ifdef CATALOG_JUN2020_MMT
      55           0 :         if (oldversion == CATALOG_JUN2020_MMT) {
      56             :                 /* upgrade to default releases */
      57           0 :                 store->catalog_version = oldversion;
      58           0 :                 return GDK_SUCCEED;
      59             :         }
      60             : #endif
      61             : 
      62             : #ifdef CATALOG_OCT2020
      63           0 :         if (oldversion == CATALOG_OCT2020) {
      64             :                 /* upgrade to default releases */
      65           0 :                 store->catalog_version = oldversion;
      66           0 :                 return GDK_SUCCEED;
      67             :         }
      68             : #endif
      69             : 
      70             : #ifdef CATALOG_JUL2021
      71           0 :         if (oldversion == CATALOG_JUL2021) {
      72             :                 /* upgrade to default releases */
      73           0 :                 store->catalog_version = oldversion;
      74           0 :                 return GDK_SUCCEED;
      75             :         }
      76             : #endif
      77             : 
      78             : #ifdef CATALOG_JAN2022
      79           0 :         if (oldversion == CATALOG_JAN2022) {
      80             :                 /* upgrade to default releases */
      81           0 :                 store->catalog_version = oldversion;
      82           0 :                 return GDK_SUCCEED;
      83             :         }
      84             : #endif
      85             : 
      86             :         return GDK_FAIL;
      87             : }
      88             : 
      89             : #if defined CATALOG_JUN2020 || defined CATALOG_OCT2020 || defined CATALOG_JUL2021
      90             : /* replace a column in a system table with a new column
      91             :  * colid is the SQL id for the column, oldcolid is the BAT id of the
      92             :  * to-be-replaced BAT */
      93             : static gdk_return
      94           0 : replace_bat(old_logger *old_lg, logger *lg, int colid, bat oldcolid, BAT *newcol)
      95             : {
      96           0 :         gdk_return rc;
      97           0 :         newcol = BATsetaccess(newcol, BAT_READ);
      98           0 :         if (newcol == NULL)
      99             :                 return GDK_FAIL;
     100           0 :         if (old_lg != NULL) {
     101           0 :                 if ((rc = BUNappend(old_lg->del, &oldcolid, false)) == GDK_SUCCEED &&
     102           0 :                         (rc = BUNappend(old_lg->add, &newcol->batCacheid, false)) == GDK_SUCCEED &&
     103           0 :                         (rc = BUNreplace(lg->catalog_bid, BUNfnd(lg->catalog_id, &colid), &newcol->batCacheid, true)) == GDK_SUCCEED) {
     104           0 :                         BBPretain(newcol->batCacheid);
     105           0 :                         BBPretain(newcol->batCacheid);
     106             :                 }
     107             :         } else {
     108           0 :                 if ((rc = BAThash(lg->catalog_id)) == GDK_SUCCEED) {
     109           0 :                         BATiter cii = bat_iterator_nolock(lg->catalog_id);
     110           0 :                         BUN p;
     111           0 :                         MT_rwlock_rdlock(&cii.b->thashlock);
     112           0 :                         HASHloop_int(cii, cii.b->thash, p, &colid) {
     113           0 :                                 if (BUNfnd(lg->dcatalog, &(oid){(oid)p}) == BUN_NONE) {
     114           0 :                                         if (BUNappend(lg->dcatalog, &(oid){(oid)p}, true) != GDK_SUCCEED ||
     115           0 :                                                 BUNreplace(lg->catalog_lid, (oid) p, &(lng){0}, false) != GDK_SUCCEED) {
     116           0 :                                                 MT_rwlock_rdunlock(&cii.b->thashlock);
     117           0 :                                                 return GDK_FAIL;
     118             :                                         }
     119           0 :                                         break;
     120             :                                 }
     121             :                         }
     122           0 :                         MT_rwlock_rdunlock(&cii.b->thashlock);
     123           0 :                         if ((rc = BUNappend(lg->catalog_id, &colid, true)) == GDK_SUCCEED &&
     124           0 :                                 (rc = BUNappend(lg->catalog_bid, &newcol->batCacheid, true)) == GDK_SUCCEED &&
     125           0 :                                 (rc = BUNappend(lg->catalog_lid, &lng_nil, false)) == GDK_SUCCEED &&
     126           0 :                                 (rc = BUNappend(lg->catalog_cnt, &(lng){BATcount(newcol)}, false)) == GDK_SUCCEED) {
     127           0 :                                 BBPretain(newcol->batCacheid);
     128             :                         }
     129             :                 }
     130             :         }
     131             :         return rc;
     132             : }
     133             : #endif
     134             : 
     135             : static BAT *
     136           0 : log_temp_descriptor(log_bid b)
     137             : {
     138           0 :         if (b <= 0)
     139             :                 return NULL;
     140           0 :         return temp_descriptor(b);
     141             : }
     142             : 
     143             : #if defined CATALOG_JUN2020 || defined CATALOG_OCT2020 || defined CATALOG_JAN2022
     144             : static gdk_return
     145           0 : tabins(logger *lg, old_logger *old_lg, bool first, int tt, int nid, ...)
     146             : {
     147           0 :         va_list va;
     148           0 :         int cid;
     149           0 :         const void *cval;
     150           0 :         gdk_return rc;
     151           0 :         BAT *b;
     152             : 
     153           0 :         va_start(va, nid);
     154           0 :         while ((cid = va_arg(va, int)) != 0) {
     155           0 :                 cval = va_arg(va, void *);
     156           0 :                 if ((b = log_temp_descriptor(log_find_bat(lg, cid))) == NULL) {
     157           0 :                         va_end(va);
     158           0 :                         return GDK_FAIL;
     159             :                 }
     160           0 :                 if (first &&
     161           0 :                         (old_lg == NULL || BUNfnd(old_lg->add, &b->batCacheid) == BUN_NONE)) {
     162           0 :                         BAT *bn = COLcopy(b, b->ttype, true, PERSISTENT);
     163           0 :                         if (bn == NULL) {
     164           0 :                                 va_end(va);
     165           0 :                                 bat_destroy(b);
     166           0 :                                 return GDK_FAIL;
     167             :                         }
     168           0 :                         if (replace_bat(old_lg, lg, cid, b->batCacheid, bn) != GDK_SUCCEED) {
     169           0 :                                 va_end(va);
     170           0 :                                 bat_destroy(b);
     171           0 :                                 bat_destroy(bn);
     172           0 :                                 return GDK_FAIL;
     173             :                         }
     174             :                         /* logical refs of b stay the same: it is moved from catalog_bid to del */
     175           0 :                         bat_destroy(b);
     176           0 :                         b = bn;
     177             :                 }
     178           0 :                 rc = BUNappend(b, cval, true);
     179           0 :                 if (rc == GDK_SUCCEED && old_lg == NULL) {
     180           0 :                         BATiter cii = bat_iterator_nolock(lg->catalog_id);
     181           0 :                         BUN p;
     182           0 :                         MT_rwlock_rdlock(&cii.b->thashlock);
     183           0 :                         rc = GDK_FAIL;          /* the BUNreplace should get executed */
     184           0 :                         HASHloop_int(cii, cii.b->thash, p, &cid) {
     185           0 :                                 if (BUNfnd(lg->dcatalog, &(oid){(oid)p}) == BUN_NONE) {
     186           0 :                                         rc = BUNreplace(lg->catalog_cnt, (oid) p, &(lng){BATcount(b)}, false);
     187           0 :                                         break;
     188             :                                 }
     189             :                         }
     190           0 :                         MT_rwlock_rdunlock(&cii.b->thashlock);
     191             :                 }
     192           0 :                 bat_destroy(b);
     193           0 :                 if (rc != GDK_SUCCEED) {
     194           0 :                         va_end(va);
     195           0 :                         return rc;
     196             :                 }
     197             :         }
     198           0 :         va_end(va);
     199             : 
     200           0 :         if (tt >= 0) {
     201           0 :                 if ((b = COLnew(0, tt, 0, PERSISTENT)) == NULL)
     202             :                         return GDK_FAIL;
     203           0 :                 rc = log_bat_persists(lg, b, nid);
     204           0 :                 bat_destroy(b);
     205           0 :                 if (rc != GDK_SUCCEED)
     206             :                         return rc;
     207             :         }
     208             :         return GDK_SUCCEED;
     209             : }
     210             : #endif
     211             : 
     212             : const struct table {
     213             :         const char *schema;
     214             :         const char *table;
     215             :         const char *column;
     216             :         const char *fullname;
     217             :         int newid;
     218             :         bool hasids;
     219             : } tables[] = {
     220             :         {
     221             :                 .schema = "sys",
     222             :                 .newid = 2000,
     223             :         },
     224             :         {
     225             :                 .schema = "sys",
     226             :                 .table = "schemas",
     227             :                 .fullname = "D_sys_schemas",
     228             :                 .newid = 2001,
     229             :         },
     230             :         {
     231             :                 .schema = "sys",
     232             :                 .table = "schemas",
     233             :                 .column = "id",
     234             :                 .fullname = "sys_schemas_id",
     235             :                 .newid = 2002,
     236             :                 .hasids = true,
     237             :         },
     238             :         {
     239             :                 .schema = "sys",
     240             :                 .table = "schemas",
     241             :                 .column = "name",
     242             :                 .fullname = "sys_schemas_name",
     243             :                 .newid = 2003,
     244             :         },
     245             :         {
     246             :                 .schema = "sys",
     247             :                 .table = "schemas",
     248             :                 .column = "authorization",
     249             :                 .fullname = "sys_schemas_authorization",
     250             :                 .newid = 2004,
     251             :         },
     252             :         {
     253             :                 .schema = "sys",
     254             :                 .table = "schemas",
     255             :                 .column = "owner",
     256             :                 .fullname = "sys_schemas_owner",
     257             :                 .newid = 2005,
     258             :         },
     259             :         {
     260             :                 .schema = "sys",
     261             :                 .table = "schemas",
     262             :                 .column = "system",
     263             :                 .fullname = "sys_schemas_system",
     264             :                 .newid = 2006,
     265             :         },
     266             :         {
     267             :                 .schema = "sys",
     268             :                 .table = "types",
     269             :                 .fullname = "D_sys_types",
     270             :                 .newid = 2007,
     271             :         },
     272             :         {
     273             :                 .schema = "sys",
     274             :                 .table = "types",
     275             :                 .column = "id",
     276             :                 .fullname = "sys_types_id",
     277             :                 .newid = 2008,
     278             :         },
     279             :         {
     280             :                 .schema = "sys",
     281             :                 .table = "types",
     282             :                 .column = "systemname",
     283             :                 .fullname = "sys_types_systemname",
     284             :                 .newid = 2009,
     285             :         },
     286             :         {
     287             :                 .schema = "sys",
     288             :                 .table = "types",
     289             :                 .column = "sqlname",
     290             :                 .fullname = "sys_types_sqlname",
     291             :                 .newid = 2010,
     292             :         },
     293             :         {
     294             :                 .schema = "sys",
     295             :                 .table = "types",
     296             :                 .column = "digits",
     297             :                 .fullname = "sys_types_digits",
     298             :                 .newid = 2011,
     299             :         },
     300             :         {
     301             :                 .schema = "sys",
     302             :                 .table = "types",
     303             :                 .column = "scale",
     304             :                 .fullname = "sys_types_scale",
     305             :                 .newid = 2012,
     306             :         },
     307             :         {
     308             :                 .schema = "sys",
     309             :                 .table = "types",
     310             :                 .column = "radix",
     311             :                 .fullname = "sys_types_radix",
     312             :                 .newid = 2013,
     313             :         },
     314             :         {
     315             :                 .schema = "sys",
     316             :                 .table = "types",
     317             :                 .column = "eclass",
     318             :                 .fullname = "sys_types_eclass",
     319             :                 .newid = 2014,
     320             :         },
     321             :         {
     322             :                 .schema = "sys",
     323             :                 .table = "types",
     324             :                 .column = "schema_id",
     325             :                 .fullname = "sys_types_schema_id",
     326             :                 .newid = 2015,
     327             :                 .hasids = true,
     328             :         },
     329             :         {
     330             :                 .schema = "sys",
     331             :                 .table = "functions",
     332             :                 .fullname = "D_sys_functions",
     333             :                 .newid = 2016,
     334             :         },
     335             :         {
     336             :                 .schema = "sys",
     337             :                 .table = "functions",
     338             :                 .column = "id",
     339             :                 .fullname = "sys_functions_id",
     340             :                 .newid = 2017,
     341             :         },
     342             :         {
     343             :                 .schema = "sys",
     344             :                 .table = "functions",
     345             :                 .column = "name",
     346             :                 .fullname = "sys_functions_name",
     347             :                 .newid = 2018,
     348             :         },
     349             :         {
     350             :                 .schema = "sys",
     351             :                 .table = "functions",
     352             :                 .column = "func",
     353             :                 .fullname = "sys_functions_func",
     354             :                 .newid = 2019,
     355             :         },
     356             :         {
     357             :                 .schema = "sys",
     358             :                 .table = "functions",
     359             :                 .column = "mod",
     360             :                 .fullname = "sys_functions_mod",
     361             :                 .newid = 2020,
     362             :         },
     363             :         {
     364             :                 .schema = "sys",
     365             :                 .table = "functions",
     366             :                 .column = "language",
     367             :                 .fullname = "sys_functions_language",
     368             :                 .newid = 2021,
     369             :         },
     370             :         {
     371             :                 .schema = "sys",
     372             :                 .table = "functions",
     373             :                 .column = "type",
     374             :                 .fullname = "sys_functions_type",
     375             :                 .newid = 2022,
     376             :         },
     377             :         {
     378             :                 .schema = "sys",
     379             :                 .table = "functions",
     380             :                 .column = "side_effect",
     381             :                 .fullname = "sys_functions_side_effect",
     382             :                 .newid = 2023,
     383             :         },
     384             :         {
     385             :                 .schema = "sys",
     386             :                 .table = "functions",
     387             :                 .column = "varres",
     388             :                 .fullname = "sys_functions_varres",
     389             :                 .newid = 2024,
     390             :         },
     391             :         {
     392             :                 .schema = "sys",
     393             :                 .table = "functions",
     394             :                 .column = "vararg",
     395             :                 .fullname = "sys_functions_vararg",
     396             :                 .newid = 2025,
     397             :         },
     398             :         {
     399             :                 .schema = "sys",
     400             :                 .table = "functions",
     401             :                 .column = "schema_id",
     402             :                 .fullname = "sys_functions_schema_id",
     403             :                 .newid = 2026,
     404             :                 .hasids = true,
     405             :         },
     406             :         {
     407             :                 .schema = "sys",
     408             :                 .table = "functions",
     409             :                 .column = "system",
     410             :                 .fullname = "sys_functions_system",
     411             :                 .newid = 2027,
     412             :         },
     413             :         {
     414             :                 .schema = "sys",
     415             :                 .table = "functions",
     416             :                 .column = "semantics",
     417             :                 .fullname = "sys_functions_semantics",
     418             :                 .newid = 2162,
     419             :         },
     420             :         {
     421             :                 .schema = "sys",
     422             :                 .table = "args",
     423             :                 .fullname = "D_sys_args",
     424             :                 .newid = 2028,
     425             :         },
     426             :         {
     427             :                 .schema = "sys",
     428             :                 .table = "args",
     429             :                 .column = "id",
     430             :                 .fullname = "sys_args_id",
     431             :                 .newid = 2029,
     432             :         },
     433             :         {
     434             :                 .schema = "sys",
     435             :                 .table = "args",
     436             :                 .column = "func_id",
     437             :                 .fullname = "sys_args_func_id",
     438             :                 .newid = 2030,
     439             :         },
     440             :         {
     441             :                 .schema = "sys",
     442             :                 .table = "args",
     443             :                 .column = "name",
     444             :                 .fullname = "sys_args_name",
     445             :                 .newid = 2031,
     446             :         },
     447             :         {
     448             :                 .schema = "sys",
     449             :                 .table = "args",
     450             :                 .column = "type",
     451             :                 .fullname = "sys_args_type",
     452             :                 .newid = 2032,
     453             :         },
     454             :         {
     455             :                 .schema = "sys",
     456             :                 .table = "args",
     457             :                 .column = "type_digits",
     458             :                 .fullname = "sys_args_type_digits",
     459             :                 .newid = 2033,
     460             :         },
     461             :         {
     462             :                 .schema = "sys",
     463             :                 .table = "args",
     464             :                 .column = "type_scale",
     465             :                 .fullname = "sys_args_type_scale",
     466             :                 .newid = 2034,
     467             :         },
     468             :         {
     469             :                 .schema = "sys",
     470             :                 .table = "args",
     471             :                 .column = "inout",
     472             :                 .fullname = "sys_args_inout",
     473             :                 .newid = 2035,
     474             :         },
     475             :         {
     476             :                 .schema = "sys",
     477             :                 .table = "args",
     478             :                 .column = "number",
     479             :                 .fullname = "sys_args_number",
     480             :                 .newid = 2036,
     481             :         },
     482             :         {
     483             :                 .schema = "sys",
     484             :                 .table = "sequences",
     485             :                 .fullname = "D_sys_sequences",
     486             :                 .newid = 2037,
     487             :         },
     488             :         {
     489             :                 .schema = "sys",
     490             :                 .table = "sequences",
     491             :                 .column = "id",
     492             :                 .fullname = "sys_sequences_id",
     493             :                 .newid = 2038,
     494             :         },
     495             :         {
     496             :                 .schema = "sys",
     497             :                 .table = "sequences",
     498             :                 .column = "schema_id",
     499             :                 .fullname = "sys_sequences_schema_id",
     500             :                 .newid = 2039,
     501             :                 .hasids = true,
     502             :         },
     503             :         {
     504             :                 .schema = "sys",
     505             :                 .table = "sequences",
     506             :                 .column = "name",
     507             :                 .fullname = "sys_sequences_name",
     508             :                 .newid = 2040,
     509             :         },
     510             :         {
     511             :                 .schema = "sys",
     512             :                 .table = "sequences",
     513             :                 .column = "start",
     514             :                 .fullname = "sys_sequences_start",
     515             :                 .newid = 2041,
     516             :         },
     517             :         {
     518             :                 .schema = "sys",
     519             :                 .table = "sequences",
     520             :                 .column = "minvalue",
     521             :                 .fullname = "sys_sequences_minvalue",
     522             :                 .newid = 2042,
     523             :         },
     524             :         {
     525             :                 .schema = "sys",
     526             :                 .table = "sequences",
     527             :                 .column = "maxvalue",
     528             :                 .fullname = "sys_sequences_maxvalue",
     529             :                 .newid = 2043,
     530             :         },
     531             :         {
     532             :                 .schema = "sys",
     533             :                 .table = "sequences",
     534             :                 .column = "increment",
     535             :                 .fullname = "sys_sequences_increment",
     536             :                 .newid = 2044,
     537             :         },
     538             :         {
     539             :                 .schema = "sys",
     540             :                 .table = "sequences",
     541             :                 .column = "cacheinc",
     542             :                 .fullname = "sys_sequences_cacheinc",
     543             :                 .newid = 2045,
     544             :         },
     545             :         {
     546             :                 .schema = "sys",
     547             :                 .table = "sequences",
     548             :                 .column = "cycle",
     549             :                 .fullname = "sys_sequences_cycle",
     550             :                 .newid = 2046,
     551             :         },
     552             :         {
     553             :                 .schema = "sys",
     554             :                 .table = "table_partitions",
     555             :                 .fullname = "D_sys_table_partitions",
     556             :                 .newid = 2047,
     557             :         },
     558             :         {
     559             :                 .schema = "sys",
     560             :                 .table = "table_partitions",
     561             :                 .column = "id",
     562             :                 .fullname = "sys_table_partitions_id",
     563             :                 .newid = 2048,
     564             :         },
     565             :         {
     566             :                 .schema = "sys",
     567             :                 .table = "table_partitions",
     568             :                 .column = "table_id",
     569             :                 .fullname = "sys_table_partitions_table_id",
     570             :                 .newid = 2049,
     571             :                 .hasids = true,
     572             :         },
     573             :         {
     574             :                 .schema = "sys",
     575             :                 .table = "table_partitions",
     576             :                 .column = "column_id",
     577             :                 .fullname = "sys_table_partitions_column_id",
     578             :                 .newid = 2050,
     579             :                 .hasids = true,
     580             :         },
     581             :         {
     582             :                 .schema = "sys",
     583             :                 .table = "table_partitions",
     584             :                 .column = "expression",
     585             :                 .fullname = "sys_table_partitions_expression",
     586             :                 .newid = 2051,
     587             :         },
     588             :         {
     589             :                 .schema = "sys",
     590             :                 .table = "table_partitions",
     591             :                 .column = "type",
     592             :                 .fullname = "sys_table_partitions_type",
     593             :                 .newid = 2052,
     594             :         },
     595             :         {
     596             :                 .schema = "sys",
     597             :                 .table = "range_partitions",
     598             :                 .fullname = "D_sys_range_partitions",
     599             :                 .newid = 2053,
     600             :         },
     601             :         {
     602             :                 .schema = "sys",
     603             :                 .table = "range_partitions",
     604             :                 .column = "table_id",
     605             :                 .fullname = "sys_range_partitions_table_id",
     606             :                 .newid = 2054,
     607             :                 .hasids = true,
     608             :         },
     609             :         {
     610             :                 .schema = "sys",
     611             :                 .table = "range_partitions",
     612             :                 .column = "partition_id",
     613             :                 .fullname = "sys_range_partitions_partition_id",
     614             :                 .newid = 2055,
     615             :         },
     616             :         {
     617             :                 .schema = "sys",
     618             :                 .table = "range_partitions",
     619             :                 .column = "minimum",
     620             :                 .fullname = "sys_range_partitions_minimum",
     621             :                 .newid = 2056,
     622             :         },
     623             :         {
     624             :                 .schema = "sys",
     625             :                 .table = "range_partitions",
     626             :                 .column = "maximum",
     627             :                 .fullname = "sys_range_partitions_maximum",
     628             :                 .newid = 2057,
     629             :         },
     630             :         {
     631             :                 .schema = "sys",
     632             :                 .table = "range_partitions",
     633             :                 .column = "with_nulls",
     634             :                 .fullname = "sys_range_partitions_with_nulls",
     635             :                 .newid = 2058,
     636             :         },
     637             :         {
     638             :                 .schema = "sys",
     639             :                 .table = "value_partitions",
     640             :                 .fullname = "D_sys_value_partitions",
     641             :                 .newid = 2059,
     642             :         },
     643             :         {
     644             :                 .schema = "sys",
     645             :                 .table = "value_partitions",
     646             :                 .column = "table_id",
     647             :                 .fullname = "sys_value_partitions_table_id",
     648             :                 .newid = 2060,
     649             :                 .hasids = true,
     650             :         },
     651             :         {
     652             :                 .schema = "sys",
     653             :                 .table = "value_partitions",
     654             :                 .column = "partition_id",
     655             :                 .fullname = "sys_value_partitions_partition_id",
     656             :                 .newid = 2061,
     657             :         },
     658             :         {
     659             :                 .schema = "sys",
     660             :                 .table = "value_partitions",
     661             :                 .column = "value",
     662             :                 .fullname = "sys_value_partitions_value",
     663             :                 .newid = 2062,
     664             :         },
     665             :         {
     666             :                 .schema = "sys",
     667             :                 .table = "dependencies",
     668             :                 .fullname = "D_sys_dependencies",
     669             :                 .newid = 2063,
     670             :         },
     671             :         {
     672             :                 .schema = "sys",
     673             :                 .table = "dependencies",
     674             :                 .column = "id",
     675             :                 .fullname = "sys_dependencies_id",
     676             :                 .newid = 2064,
     677             :                 .hasids = true,
     678             :         },
     679             :         {
     680             :                 .schema = "sys",
     681             :                 .table = "dependencies",
     682             :                 .column = "depend_id",
     683             :                 .fullname = "sys_dependencies_depend_id",
     684             :                 .newid = 2065,
     685             :                 .hasids = true,
     686             :         },
     687             :         {
     688             :                 .schema = "sys",
     689             :                 .table = "dependencies",
     690             :                 .column = "depend_type",
     691             :                 .fullname = "sys_dependencies_depend_type",
     692             :                 .newid = 2066,
     693             :         },
     694             :         {
     695             :                 .schema = "sys",
     696             :                 .table = "_tables",
     697             :                 .fullname = "D_sys__tables",
     698             :                 .newid = 2067,
     699             :         },
     700             :         {
     701             :                 .schema = "sys",
     702             :                 .table = "_tables",
     703             :                 .column = "id",
     704             :                 .fullname = "sys__tables_id",
     705             :                 .newid = 2068,
     706             :                 .hasids = true,
     707             :         },
     708             :         {
     709             :                 .schema = "sys",
     710             :                 .table = "_tables",
     711             :                 .column = "name",
     712             :                 .fullname = "sys__tables_name",
     713             :                 .newid = 2069,
     714             :         },
     715             :         {
     716             :                 .schema = "sys",
     717             :                 .table = "_tables",
     718             :                 .column = "schema_id",
     719             :                 .fullname = "sys__tables_schema_id",
     720             :                 .newid = 2070,
     721             :                 .hasids = true,
     722             :         },
     723             :         {
     724             :                 .schema = "sys",
     725             :                 .table = "_tables",
     726             :                 .column = "query",
     727             :                 .fullname = "sys__tables_query",
     728             :                 .newid = 2071,
     729             :         },
     730             :         {
     731             :                 .schema = "sys",
     732             :                 .table = "_tables",
     733             :                 .column = "type",
     734             :                 .fullname = "sys__tables_type",
     735             :                 .newid = 2072,
     736             :         },
     737             :         {
     738             :                 .schema = "sys",
     739             :                 .table = "_tables",
     740             :                 .column = "system",
     741             :                 .fullname = "sys__tables_system",
     742             :                 .newid = 2073,
     743             :         },
     744             :         {
     745             :                 .schema = "sys",
     746             :                 .table = "_tables",
     747             :                 .column = "commit_action",
     748             :                 .fullname = "sys__tables_commit_action",
     749             :                 .newid = 2074,
     750             :         },
     751             :         {
     752             :                 .schema = "sys",
     753             :                 .table = "_tables",
     754             :                 .column = "access",
     755             :                 .fullname = "sys__tables_access",
     756             :                 .newid = 2075,
     757             :         },
     758             :         {
     759             :                 .schema = "sys",
     760             :                 .table = "_columns",
     761             :                 .fullname = "D_sys__columns",
     762             :                 .newid = 2076,
     763             :         },
     764             :         {
     765             :                 .schema = "sys",
     766             :                 .table = "_columns",
     767             :                 .column = "id",
     768             :                 .fullname = "sys__columns_id",
     769             :                 .newid = 2077,
     770             :                 .hasids = true,
     771             :         },
     772             :         {
     773             :                 .schema = "sys",
     774             :                 .table = "_columns",
     775             :                 .column = "name",
     776             :                 .fullname = "sys__columns_name",
     777             :                 .newid = 2078,
     778             :         },
     779             :         {
     780             :                 .schema = "sys",
     781             :                 .table = "_columns",
     782             :                 .column = "type",
     783             :                 .fullname = "sys__columns_type",
     784             :                 .newid = 2079,
     785             :         },
     786             :         {
     787             :                 .schema = "sys",
     788             :                 .table = "_columns",
     789             :                 .column = "type_digits",
     790             :                 .fullname = "sys__columns_type_digits",
     791             :                 .newid = 2080,
     792             :         },
     793             :         {
     794             :                 .schema = "sys",
     795             :                 .table = "_columns",
     796             :                 .column = "type_scale",
     797             :                 .fullname = "sys__columns_type_scale",
     798             :                 .newid = 2081,
     799             :         },
     800             :         {
     801             :                 .schema = "sys",
     802             :                 .table = "_columns",
     803             :                 .column = "table_id",
     804             :                 .fullname = "sys__columns_table_id",
     805             :                 .newid = 2082,
     806             :                 .hasids = true,
     807             :         },
     808             :         {
     809             :                 .schema = "sys",
     810             :                 .table = "_columns",
     811             :                 .column = "default",
     812             :                 .fullname = "sys__columns_default",
     813             :                 .newid = 2083,
     814             :         },
     815             :         {
     816             :                 .schema = "sys",
     817             :                 .table = "_columns",
     818             :                 .column = "null",
     819             :                 .fullname = "sys__columns_null",
     820             :                 .newid = 2084,
     821             :         },
     822             :         {
     823             :                 .schema = "sys",
     824             :                 .table = "_columns",
     825             :                 .column = "number",
     826             :                 .fullname = "sys__columns_number",
     827             :                 .newid = 2085,
     828             :         },
     829             :         {
     830             :                 .schema = "sys",
     831             :                 .table = "_columns",
     832             :                 .column = "storage",
     833             :                 .fullname = "sys__columns_storage",
     834             :                 .newid = 2086,
     835             :         },
     836             :         {
     837             :                 .schema = "sys",
     838             :                 .table = "keys",
     839             :                 .fullname = "D_sys_keys",
     840             :                 .newid = 2087,
     841             :         },
     842             :         {
     843             :                 .schema = "sys",
     844             :                 .table = "keys",
     845             :                 .column = "id",
     846             :                 .fullname = "sys_keys_id",
     847             :                 .newid = 2088,
     848             :         },
     849             :         {
     850             :                 .schema = "sys",
     851             :                 .table = "keys",
     852             :                 .column = "table_id",
     853             :                 .fullname = "sys_keys_table_id",
     854             :                 .newid = 2089,
     855             :                 .hasids = true,
     856             :         },
     857             :         {
     858             :                 .schema = "sys",
     859             :                 .table = "keys",
     860             :                 .column = "type",
     861             :                 .fullname = "sys_keys_type",
     862             :                 .newid = 2090,
     863             :         },
     864             :         {
     865             :                 .schema = "sys",
     866             :                 .table = "keys",
     867             :                 .column = "name",
     868             :                 .fullname = "sys_keys_name",
     869             :                 .newid = 2091,
     870             :         },
     871             :         {
     872             :                 .schema = "sys",
     873             :                 .table = "keys",
     874             :                 .column = "rkey",
     875             :                 .fullname = "sys_keys_rkey",
     876             :                 .newid = 2092,
     877             :         },
     878             :         {
     879             :                 .schema = "sys",
     880             :                 .table = "keys",
     881             :                 .column = "action",
     882             :                 .fullname = "sys_keys_action",
     883             :                 .newid = 2093,
     884             :         },
     885             :         {
     886             :                 .schema = "sys",
     887             :                 .table = "idxs",
     888             :                 .fullname = "D_sys_idxs",
     889             :                 .newid = 2094,
     890             :         },
     891             :         {
     892             :                 .schema = "sys",
     893             :                 .table = "idxs",
     894             :                 .column = "id",
     895             :                 .fullname = "sys_idxs_id",
     896             :                 .newid = 2095,
     897             :         },
     898             :         {
     899             :                 .schema = "sys",
     900             :                 .table = "idxs",
     901             :                 .column = "table_id",
     902             :                 .fullname = "sys_idxs_table_id",
     903             :                 .newid = 2096,
     904             :                 .hasids = true,
     905             :         },
     906             :         {
     907             :                 .schema = "sys",
     908             :                 .table = "idxs",
     909             :                 .column = "type",
     910             :                 .fullname = "sys_idxs_type",
     911             :                 .newid = 2097,
     912             :         },
     913             :         {
     914             :                 .schema = "sys",
     915             :                 .table = "idxs",
     916             :                 .column = "name",
     917             :                 .fullname = "sys_idxs_name",
     918             :                 .newid = 2098,
     919             :         },
     920             :         {
     921             :                 .schema = "sys",
     922             :                 .table = "triggers",
     923             :                 .fullname = "D_sys_triggers",
     924             :                 .newid = 2099,
     925             :         },
     926             :         {
     927             :                 .schema = "sys",
     928             :                 .table = "triggers",
     929             :                 .column = "id",
     930             :                 .fullname = "sys_triggers_id",
     931             :                 .newid = 2100,
     932             :         },
     933             :         {
     934             :                 .schema = "sys",
     935             :                 .table = "triggers",
     936             :                 .column = "name",
     937             :                 .fullname = "sys_triggers_name",
     938             :                 .newid = 2101,
     939             :         },
     940             :         {
     941             :                 .schema = "sys",
     942             :                 .table = "triggers",
     943             :                 .column = "table_id",
     944             :                 .fullname = "sys_triggers_table_id",
     945             :                 .newid = 2102,
     946             :                 .hasids = true,
     947             :         },
     948             :         {
     949             :                 .schema = "sys",
     950             :                 .table = "triggers",
     951             :                 .column = "time",
     952             :                 .fullname = "sys_triggers_time",
     953             :                 .newid = 2103,
     954             :         },
     955             :         {
     956             :                 .schema = "sys",
     957             :                 .table = "triggers",
     958             :                 .column = "orientation",
     959             :                 .fullname = "sys_triggers_orientation",
     960             :                 .newid = 2104,
     961             :         },
     962             :         {
     963             :                 .schema = "sys",
     964             :                 .table = "triggers",
     965             :                 .column = "event",
     966             :                 .fullname = "sys_triggers_event",
     967             :                 .newid = 2105,
     968             :         },
     969             :         {
     970             :                 .schema = "sys",
     971             :                 .table = "triggers",
     972             :                 .column = "old_name",
     973             :                 .fullname = "sys_triggers_old_name",
     974             :                 .newid = 2106,
     975             :         },
     976             :         {
     977             :                 .schema = "sys",
     978             :                 .table = "triggers",
     979             :                 .column = "new_name",
     980             :                 .fullname = "sys_triggers_new_name",
     981             :                 .newid = 2107,
     982             :         },
     983             :         {
     984             :                 .schema = "sys",
     985             :                 .table = "triggers",
     986             :                 .column = "condition",
     987             :                 .fullname = "sys_triggers_condition",
     988             :                 .newid = 2108,
     989             :         },
     990             :         {
     991             :                 .schema = "sys",
     992             :                 .table = "triggers",
     993             :                 .column = "statement",
     994             :                 .fullname = "sys_triggers_statement",
     995             :                 .newid = 2109,
     996             :         },
     997             :         {
     998             :                 .schema = "sys",
     999             :                 .table = "objects",
    1000             :                 .fullname = "D_sys_objects",
    1001             :                 .newid = 2110,
    1002             :         },
    1003             :         {
    1004             :                 .schema = "sys",
    1005             :                 .table = "objects",
    1006             :                 .column = "id",
    1007             :                 .fullname = "sys_objects_id",
    1008             :                 .newid = 2111,
    1009             :         },
    1010             :         {
    1011             :                 .schema = "sys",
    1012             :                 .table = "objects",
    1013             :                 .column = "name",
    1014             :                 .fullname = "sys_objects_name",
    1015             :                 .newid = 2112,
    1016             :         },
    1017             :         {
    1018             :                 .schema = "sys",
    1019             :                 .table = "objects",
    1020             :                 .column = "nr",
    1021             :                 .fullname = "sys_objects_nr",
    1022             :                 .newid = 2113,
    1023             :                 .hasids = true,
    1024             :         },
    1025             :         {
    1026             :                 .schema = "sys",
    1027             :                 .table = "objects",
    1028             :                 .column = "sub",
    1029             :                 .fullname = "sys_objects_sub",
    1030             :                 .newid = 2163,
    1031             :                 .hasids = true,
    1032             :         },
    1033             :         {
    1034             :                 .schema = "tmp",
    1035             :                 .newid = 2114,
    1036             :         },
    1037             :         {
    1038             :                 .schema = "tmp",
    1039             :                 .table = "_tables",
    1040             :                 .fullname = "D_tmp__tables",
    1041             :                 .newid = 2115,
    1042             :         },
    1043             :         {
    1044             :                 .schema = "tmp",
    1045             :                 .table = "_tables",
    1046             :                 .column = "id",
    1047             :                 .fullname = "tmp__tables_id",
    1048             :                 .newid = 2116,
    1049             :         },
    1050             :         {
    1051             :                 .schema = "tmp",
    1052             :                 .table = "_tables",
    1053             :                 .column = "name",
    1054             :                 .fullname = "tmp__tables_name",
    1055             :                 .newid = 2117,
    1056             :         },
    1057             :         {
    1058             :                 .schema = "tmp",
    1059             :                 .table = "_tables",
    1060             :                 .column = "schema_id",
    1061             :                 .fullname = "tmp__tables_schema_id",
    1062             :                 .newid = 2118,
    1063             :         },
    1064             :         {
    1065             :                 .schema = "tmp",
    1066             :                 .table = "_tables",
    1067             :                 .column = "query",
    1068             :                 .fullname = "tmp__tables_query",
    1069             :                 .newid = 2119,
    1070             :         },
    1071             :         {
    1072             :                 .schema = "tmp",
    1073             :                 .table = "_tables",
    1074             :                 .column = "type",
    1075             :                 .fullname = "tmp__tables_type",
    1076             :                 .newid = 2120,
    1077             :         },
    1078             :         {
    1079             :                 .schema = "tmp",
    1080             :                 .table = "_tables",
    1081             :                 .column = "system",
    1082             :                 .fullname = "tmp__tables_system",
    1083             :                 .newid = 2121,
    1084             :         },
    1085             :         {
    1086             :                 .schema = "tmp",
    1087             :                 .table = "_tables",
    1088             :                 .column = "commit_action",
    1089             :                 .fullname = "tmp__tables_commit_action",
    1090             :                 .newid = 2122,
    1091             :         },
    1092             :         {
    1093             :                 .schema = "tmp",
    1094             :                 .table = "_tables",
    1095             :                 .column = "access",
    1096             :                 .fullname = "tmp__tables_access",
    1097             :                 .newid = 2123,
    1098             :         },
    1099             :         {
    1100             :                 .schema = "tmp",
    1101             :                 .table = "_columns",
    1102             :                 .fullname = "D_tmp__columns",
    1103             :                 .newid = 2124,
    1104             :         },
    1105             :         {
    1106             :                 .schema = "tmp",
    1107             :                 .table = "_columns",
    1108             :                 .column = "id",
    1109             :                 .fullname = "tmp__columns_id",
    1110             :                 .newid = 2125,
    1111             :         },
    1112             :         {
    1113             :                 .schema = "tmp",
    1114             :                 .table = "_columns",
    1115             :                 .column = "name",
    1116             :                 .fullname = "tmp__columns_name",
    1117             :                 .newid = 2126,
    1118             :         },
    1119             :         {
    1120             :                 .schema = "tmp",
    1121             :                 .table = "_columns",
    1122             :                 .column = "type",
    1123             :                 .fullname = "tmp__columns_type",
    1124             :                 .newid = 2127,
    1125             :         },
    1126             :         {
    1127             :                 .schema = "tmp",
    1128             :                 .table = "_columns",
    1129             :                 .column = "type_digits",
    1130             :                 .fullname = "tmp__columns_type_digits",
    1131             :                 .newid = 2128,
    1132             :         },
    1133             :         {
    1134             :                 .schema = "tmp",
    1135             :                 .table = "_columns",
    1136             :                 .column = "type_scale",
    1137             :                 .fullname = "tmp__columns_type_scale",
    1138             :                 .newid = 2129,
    1139             :         },
    1140             :         {
    1141             :                 .schema = "tmp",
    1142             :                 .table = "_columns",
    1143             :                 .column = "table_id",
    1144             :                 .fullname = "tmp__columns_table_id",
    1145             :                 .newid = 2130,
    1146             :         },
    1147             :         {
    1148             :                 .schema = "tmp",
    1149             :                 .table = "_columns",
    1150             :                 .column = "default",
    1151             :                 .fullname = "tmp__columns_default",
    1152             :                 .newid = 2131,
    1153             :         },
    1154             :         {
    1155             :                 .schema = "tmp",
    1156             :                 .table = "_columns",
    1157             :                 .column = "null",
    1158             :                 .fullname = "tmp__columns_null",
    1159             :                 .newid = 2132,
    1160             :         },
    1161             :         {
    1162             :                 .schema = "tmp",
    1163             :                 .table = "_columns",
    1164             :                 .column = "number",
    1165             :                 .fullname = "tmp__columns_number",
    1166             :                 .newid = 2133,
    1167             :         },
    1168             :         {
    1169             :                 .schema = "tmp",
    1170             :                 .table = "_columns",
    1171             :                 .column = "storage",
    1172             :                 .fullname = "tmp__columns_storage",
    1173             :                 .newid = 2134,
    1174             :         },
    1175             :         {
    1176             :                 .schema = "tmp",
    1177             :                 .table = "keys",
    1178             :                 .fullname = "D_tmp_keys",
    1179             :                 .newid = 2135,
    1180             :         },
    1181             :         {
    1182             :                 .schema = "tmp",
    1183             :                 .table = "keys",
    1184             :                 .column = "id",
    1185             :                 .fullname = "tmp_keys_id",
    1186             :                 .newid = 2136,
    1187             :         },
    1188             :         {
    1189             :                 .schema = "tmp",
    1190             :                 .table = "keys",
    1191             :                 .column = "table_id",
    1192             :                 .fullname = "tmp_keys_table_id",
    1193             :                 .newid = 2137,
    1194             :         },
    1195             :         {
    1196             :                 .schema = "tmp",
    1197             :                 .table = "keys",
    1198             :                 .column = "type",
    1199             :                 .fullname = "tmp_keys_type",
    1200             :                 .newid = 2138,
    1201             :         },
    1202             :         {
    1203             :                 .schema = "tmp",
    1204             :                 .table = "keys",
    1205             :                 .column = "name",
    1206             :                 .fullname = "tmp_keys_name",
    1207             :                 .newid = 2139,
    1208             :         },
    1209             :         {
    1210             :                 .schema = "tmp",
    1211             :                 .table = "keys",
    1212             :                 .column = "rkey",
    1213             :                 .fullname = "tmp_keys_rkey",
    1214             :                 .newid = 2140,
    1215             :         },
    1216             :         {
    1217             :                 .schema = "tmp",
    1218             :                 .table = "keys",
    1219             :                 .column = "action",
    1220             :                 .fullname = "tmp_keys_action",
    1221             :                 .newid = 2141,
    1222             :         },
    1223             :         {
    1224             :                 .schema = "tmp",
    1225             :                 .table = "idxs",
    1226             :                 .fullname = "D_tmp_idxs",
    1227             :                 .newid = 2142,
    1228             :         },
    1229             :         {
    1230             :                 .schema = "tmp",
    1231             :                 .table = "idxs",
    1232             :                 .column = "id",
    1233             :                 .fullname = "tmp_idxs_id",
    1234             :                 .newid = 2143,
    1235             :         },
    1236             :         {
    1237             :                 .schema = "tmp",
    1238             :                 .table = "idxs",
    1239             :                 .column = "table_id",
    1240             :                 .fullname = "tmp_idxs_table_id",
    1241             :                 .newid = 2144,
    1242             :         },
    1243             :         {
    1244             :                 .schema = "tmp",
    1245             :                 .table = "idxs",
    1246             :                 .column = "type",
    1247             :                 .fullname = "tmp_idxs_type",
    1248             :                 .newid = 2145,
    1249             :         },
    1250             :         {
    1251             :                 .schema = "tmp",
    1252             :                 .table = "idxs",
    1253             :                 .column = "name",
    1254             :                 .fullname = "tmp_idxs_name",
    1255             :                 .newid = 2146,
    1256             :         },
    1257             :         {
    1258             :                 .schema = "tmp",
    1259             :                 .table = "triggers",
    1260             :                 .fullname = "D_tmp_triggers",
    1261             :                 .newid = 2147,
    1262             :         },
    1263             :         {
    1264             :                 .schema = "tmp",
    1265             :                 .table = "triggers",
    1266             :                 .column = "id",
    1267             :                 .fullname = "tmp_triggers_id",
    1268             :                 .newid = 2148,
    1269             :         },
    1270             :         {
    1271             :                 .schema = "tmp",
    1272             :                 .table = "triggers",
    1273             :                 .column = "name",
    1274             :                 .fullname = "tmp_triggers_name",
    1275             :                 .newid = 2149,
    1276             :         },
    1277             :         {
    1278             :                 .schema = "tmp",
    1279             :                 .table = "triggers",
    1280             :                 .column = "table_id",
    1281             :                 .fullname = "tmp_triggers_table_id",
    1282             :                 .newid = 2150,
    1283             :         },
    1284             :         {
    1285             :                 .schema = "tmp",
    1286             :                 .table = "triggers",
    1287             :                 .column = "time",
    1288             :                 .fullname = "tmp_triggers_time",
    1289             :                 .newid = 2151,
    1290             :         },
    1291             :         {
    1292             :                 .schema = "tmp",
    1293             :                 .table = "triggers",
    1294             :                 .column = "orientation",
    1295             :                 .fullname = "tmp_triggers_orientation",
    1296             :                 .newid = 2152,
    1297             :         },
    1298             :         {
    1299             :                 .schema = "tmp",
    1300             :                 .table = "triggers",
    1301             :                 .column = "event",
    1302             :                 .fullname = "tmp_triggers_event",
    1303             :                 .newid = 2153,
    1304             :         },
    1305             :         {
    1306             :                 .schema = "tmp",
    1307             :                 .table = "triggers",
    1308             :                 .column = "old_name",
    1309             :                 .fullname = "tmp_triggers_old_name",
    1310             :                 .newid = 2154,
    1311             :         },
    1312             :         {
    1313             :                 .schema = "tmp",
    1314             :                 .table = "triggers",
    1315             :                 .column = "new_name",
    1316             :                 .fullname = "tmp_triggers_new_name",
    1317             :                 .newid = 2155,
    1318             :         },
    1319             :         {
    1320             :                 .schema = "tmp",
    1321             :                 .table = "triggers",
    1322             :                 .column = "condition",
    1323             :                 .fullname = "tmp_triggers_condition",
    1324             :                 .newid = 2156,
    1325             :         },
    1326             :         {
    1327             :                 .schema = "tmp",
    1328             :                 .table = "triggers",
    1329             :                 .column = "statement",
    1330             :                 .fullname = "tmp_triggers_statement",
    1331             :                 .newid = 2157,
    1332             :         },
    1333             :         {
    1334             :                 .schema = "tmp",
    1335             :                 .table = "objects",
    1336             :                 .fullname = "D_tmp_objects",
    1337             :                 .newid = 2158,
    1338             :         },
    1339             :         {
    1340             :                 .schema = "tmp",
    1341             :                 .table = "objects",
    1342             :                 .column = "id",
    1343             :                 .fullname = "tmp_objects_id",
    1344             :                 .newid = 2159,
    1345             :         },
    1346             :         {
    1347             :                 .schema = "tmp",
    1348             :                 .table = "objects",
    1349             :                 .column = "name",
    1350             :                 .fullname = "tmp_objects_name",
    1351             :                 .newid = 2160,
    1352             :         },
    1353             :         {
    1354             :                 .schema = "tmp",
    1355             :                 .table = "objects",
    1356             :                 .column = "nr",
    1357             :                 .fullname = "tmp_objects_nr",
    1358             :                 .newid = 2161,
    1359             :         },
    1360             :         {
    1361             :                 .schema = "tmp",
    1362             :                 .table = "objects",
    1363             :                 .column = "sub",
    1364             :                 .fullname = "tmp_objects_sub",
    1365             :                 .newid = 2164,
    1366             :         },
    1367             :         {0}
    1368             : };
    1369             : 
    1370             : /* more system tables with schema/table/column ids that need to be remapped */
    1371             : const struct mapids {
    1372             :         // const char *schema;                  /* always "sys" */
    1373             :         const char *table;
    1374             :         const char *column;
    1375             : } mapids[] = {
    1376             :         {
    1377             :                 .table = "comments",
    1378             :                 .column = "id",
    1379             :         },
    1380             :         {
    1381             :                 .table = "db_user_info",
    1382             :                 .column = "default_schema",
    1383             :         },
    1384             :         {
    1385             :                 .table = "privileges",
    1386             :                 .column = "obj_id",
    1387             :         },
    1388             :         {
    1389             :                 .table = "statistics",
    1390             :                 .column = "column_id",
    1391             :         },
    1392             :         {0}
    1393             : };
    1394             : 
    1395             : static gdk_return
    1396           0 : upgrade(old_logger *lg)
    1397             : {
    1398           0 :         gdk_return rc = GDK_FAIL;
    1399           0 :         struct bats {
    1400             :                 BAT *nmbat;
    1401             :                 BAT *idbat;
    1402             :                 BAT *parbat;
    1403             :                 BAT *cands;
    1404             :         } bats[3];
    1405           0 :         BAT *mapold = COLnew(0, TYPE_int, 256, TRANSIENT);
    1406           0 :         BAT *mapnew = COLnew(0, TYPE_int, 256, TRANSIENT);
    1407             : 
    1408           0 :         bats[0].nmbat = log_temp_descriptor(old_logger_find_bat(lg, "sys_schemas_name", 0, 0));
    1409           0 :         bats[0].idbat = log_temp_descriptor(old_logger_find_bat(lg, "sys_schemas_id", 0, 0));
    1410           0 :         bats[0].parbat = NULL;
    1411           0 :         bats[0].cands = log_temp_descriptor(old_logger_find_bat(lg, "D_sys_schemas", 0, 0));
    1412           0 :         bats[1].nmbat = log_temp_descriptor(old_logger_find_bat(lg, "sys__tables_name", 0, 0));
    1413           0 :         bats[1].idbat = log_temp_descriptor(old_logger_find_bat(lg, "sys__tables_id", 0, 0));
    1414           0 :         bats[1].parbat = log_temp_descriptor(old_logger_find_bat(lg, "sys__tables_schema_id", 0, 0));
    1415           0 :         bats[1].cands = log_temp_descriptor(old_logger_find_bat(lg, "D_sys__tables", 0, 0));
    1416           0 :         bats[2].nmbat = log_temp_descriptor(old_logger_find_bat(lg, "sys__columns_name", 0, 0));
    1417           0 :         bats[2].idbat = log_temp_descriptor(old_logger_find_bat(lg, "sys__columns_id", 0, 0));
    1418           0 :         bats[2].parbat = log_temp_descriptor(old_logger_find_bat(lg, "sys__columns_table_id", 0, 0));
    1419           0 :         bats[2].cands = log_temp_descriptor(old_logger_find_bat(lg, "D_sys__columns", 0, 0));
    1420           0 :         if (mapold == NULL || mapnew == NULL)
    1421           0 :                 goto bailout;
    1422           0 :         for (int i = 0; i < 3; i++) {
    1423           0 :                 if (bats[i].nmbat == NULL || bats[i].idbat == NULL || bats[i].cands == NULL)
    1424           0 :                         goto bailout;
    1425           0 :                 if (i > 0 && bats[i].parbat == NULL)
    1426           0 :                         goto bailout;
    1427             :                 /* create a candidate list from the deleted rows bat */
    1428           0 :                 if (BATcount(bats[i].cands) == 0) {
    1429             :                         /* no deleted rows -> no candidate list */
    1430           0 :                         bat_destroy(bats[i].cands);
    1431           0 :                         bats[i].cands = NULL;
    1432             :                 } else {
    1433           0 :                         BAT *b;
    1434           0 :                         if ((rc = BATsort(&b, NULL, NULL, bats[i].cands, NULL, NULL, false, false, false)) != GDK_SUCCEED)
    1435           0 :                                 goto bailout;
    1436           0 :                         rc = GDK_FAIL;
    1437           0 :                         bat_destroy(bats[i].cands);
    1438           0 :                         bats[i].cands = BATnegcands(BATcount(bats[i].nmbat), b);
    1439           0 :                         bat_destroy(b);
    1440           0 :                         if (bats[i].cands == NULL) {
    1441           0 :                                 goto bailout;
    1442             :                         }
    1443             :                 }
    1444             :         }
    1445             : 
    1446             :         /* figure out mapping from old IDs to new stable IDs, result in two
    1447             :          * aligned BATs, mapold and mapnew */
    1448           0 :         int schid, tabid, parid;
    1449           0 :         schid = tabid = parid = 0;      /* restrict search to parent object */
    1450           0 :         for (int i = 0; tables[i].schema != NULL; i++) {
    1451           0 :                 int lookup;                             /* which system table to look the name up in */
    1452           0 :                 const char *name;               /* the name to look up */
    1453           0 :                 if (tables[i].table == NULL) {
    1454             :                         /* it's a schema */
    1455           0 :                         name = tables[i].schema;
    1456           0 :                         lookup = 0;
    1457           0 :                         parid = 0;                      /* no parent object */
    1458           0 :                 } else if (tables[i].column == NULL) {
    1459             :                         /* it's a table */
    1460           0 :                         name = tables[i].table;
    1461           0 :                         lookup = 1;
    1462           0 :                         parid = schid;          /* parent object is last schema */
    1463             :                 } else {
    1464             :                         /* it's a column */
    1465           0 :                         name = tables[i].column;
    1466           0 :                         lookup = 2;
    1467           0 :                         parid = tabid;          /* parent object is last table */
    1468             :                 }
    1469             :                 /* restrict search to non-deleted rows */
    1470           0 :                 BAT *cand = bats[lookup].cands;
    1471           0 :                 if (bats[lookup].parbat != NULL) {
    1472             :                         /* further restrict search to parent object */
    1473           0 :                         cand = BATselect(bats[lookup].parbat, cand, &parid, NULL, true, true, false);
    1474           0 :                         if (cand == NULL)
    1475           0 :                                 goto bailout;
    1476             :                 }
    1477             :                 /* look for name, should be one (or maybe zero) result */
    1478           0 :                 BAT *b = BATselect(bats[lookup].nmbat, cand, name, NULL, true, true, false);
    1479           0 :                 if (cand != bats[lookup].cands)
    1480           0 :                         bat_destroy(cand);
    1481           0 :                 if (b == NULL)
    1482           0 :                         goto bailout;
    1483           0 :                 if (BATcount(b) > 0) {
    1484           0 :                         int oldid = ((int *) bats[lookup].idbat->theap->base)[BUNtoid(b, 0) - bats[lookup].nmbat->hseqbase];
    1485           0 :                         if (oldid != tables[i].newid &&
    1486           0 :                                 ((rc = BUNappend(mapold, &oldid, false)) != GDK_SUCCEED ||
    1487           0 :                                  (rc = BUNappend(mapnew, &tables[i].newid, false)) != GDK_SUCCEED)) {
    1488           0 :                                 bat_destroy(b);
    1489           0 :                                 goto bailout;
    1490             :                         }
    1491           0 :                         rc = GDK_FAIL;
    1492           0 :                         if (tables[i].table == NULL)
    1493           0 :                                 schid = oldid;
    1494           0 :                         else if (tables[i].column == NULL)
    1495           0 :                                 tabid = oldid;
    1496             :                 }
    1497           0 :                 bat_destroy(b);
    1498             :         }
    1499             : 
    1500           0 :         if (BATcount(mapold) == 0) {
    1501             :                 /* skip unnecessary work if there is no need for mapping */
    1502           0 :                 bat_destroy(mapold);
    1503           0 :                 bat_destroy(mapnew);
    1504           0 :                 mapold = NULL;
    1505           0 :                 mapnew = NULL;
    1506             :         }
    1507             : 
    1508             :         /* do the mapping in the system tables: all columns with the .hasids
    1509             :          * flag set may contain IDs that have to be mapped; also add all
    1510             :          * system tables to the new catalog bats and add the new ones to the
    1511             :          * lg->add bat and the old ones that were replaced to the lg->del bat */
    1512           0 :         const char *delname;
    1513           0 :         delname = NULL;
    1514           0 :         int delidx;
    1515           0 :         delidx = -1;
    1516           0 :         for (int i = 0; tables[i].schema != NULL; i++) {
    1517           0 :                 if (tables[i].fullname == NULL) /* schema */
    1518           0 :                         continue;
    1519           0 :                 if (tables[i].column == NULL) { /* table */
    1520           0 :                         delname = tables[i].fullname;
    1521           0 :                         delidx = i;
    1522           0 :                         continue;
    1523             :                 }
    1524           0 :                 BAT *b = log_temp_descriptor(old_logger_find_bat(lg, tables[i].fullname, 0, 0));
    1525           0 :                 if (b == NULL)
    1526           0 :                         continue;
    1527           0 :                 if (delidx >= 0) {
    1528           0 :                         BAT *d = log_temp_descriptor(old_logger_find_bat(lg, delname, 0, 0));
    1529           0 :                         BAT *m = BATconstant(0, TYPE_msk, &(msk){false}, BATcount(b), PERSISTENT);
    1530           0 :                         if (m == NULL) {
    1531           0 :                                 bat_destroy(d);
    1532           0 :                                 bat_destroy(m);
    1533           0 :                                 goto bailout;
    1534             :                         }
    1535           0 :                         if (d != NULL) {
    1536           0 :                                 const oid *dels = (const oid *) Tloc(d, 0);
    1537           0 :                                 for (BUN q = BATcount(d), p = 0; p < q; p++)
    1538           0 :                                         mskSetVal(m, (BUN) dels[p], true);
    1539           0 :                                 BBPretain(d->batCacheid);
    1540             :                         }
    1541           0 :                         if ((rc = BUNappend(lg->add, &m->batCacheid, false)) != GDK_SUCCEED ||
    1542           0 :                                 (rc = BUNappend(lg->lg->catalog_bid, &m->batCacheid, true)) != GDK_SUCCEED ||
    1543           0 :                                 (rc = BUNappend(lg->lg->catalog_id, &tables[delidx].newid, true)) != GDK_SUCCEED ||
    1544           0 :                                 (d != NULL &&
    1545           0 :                                  (rc = BUNappend(lg->del, &d->batCacheid, false)) != GDK_SUCCEED)) {
    1546           0 :                                 bat_destroy(d);
    1547           0 :                                 bat_destroy(m);
    1548           0 :                                 goto bailout;
    1549             :                         }
    1550           0 :                         rc = GDK_FAIL;
    1551           0 :                         BBPretain(m->batCacheid);
    1552           0 :                         BBPretain(m->batCacheid);
    1553           0 :                         bat_destroy(d);
    1554           0 :                         bat_destroy(m);
    1555           0 :                         delidx = -1;
    1556             :                 }
    1557           0 :                 if (tables[i].hasids && mapold) {
    1558           0 :                         BAT *b1, *b2;
    1559           0 :                         BAT *cands = log_temp_descriptor(old_logger_find_bat(lg, delname, 0, 0));
    1560           0 :                         if (cands) {
    1561           0 :                                 if (BATcount(cands) == 0) {
    1562           0 :                                         bat_destroy(cands);
    1563           0 :                                         cands = NULL;
    1564             :                                 } else {
    1565           0 :                                         rc = BATsort(&b1, NULL, NULL, cands, NULL, NULL, false, false, false);
    1566           0 :                                         bat_destroy(cands);
    1567           0 :                                         if (rc != GDK_SUCCEED) {
    1568           0 :                                                 bat_destroy(b);
    1569           0 :                                                 goto bailout;
    1570             :                                         }
    1571           0 :                                         rc = GDK_FAIL;
    1572           0 :                                         cands = BATnegcands(BATcount(b), b1);
    1573           0 :                                         bat_destroy(b1);
    1574           0 :                                         if (cands == NULL) {
    1575           0 :                                                 bat_destroy(b);
    1576           0 :                                                 goto bailout;
    1577             :                                         }
    1578             :                                 }
    1579             :                         }
    1580           0 :                         rc = BATjoin(&b1, &b2, b, mapold, cands, NULL, false, BATcount(mapold));
    1581           0 :                         bat_destroy(cands);
    1582           0 :                         if (rc != GDK_SUCCEED) {
    1583           0 :                                 bat_destroy(b);
    1584           0 :                                 goto bailout;
    1585             :                         }
    1586           0 :                         rc = GDK_FAIL;
    1587           0 :                         if (BATcount(b1) == 0) {
    1588           0 :                                 bat_destroy(b1);
    1589           0 :                                 bat_destroy(b2);
    1590             :                         } else {
    1591           0 :                                 BAT *orig = b;
    1592           0 :                                 b = COLcopy(orig, orig->ttype, true, PERSISTENT);
    1593           0 :                                 if (b == NULL) {
    1594           0 :                                         bat_destroy(orig);
    1595           0 :                                         bat_destroy(b1);
    1596           0 :                                         bat_destroy(b2);
    1597           0 :                                         goto bailout;
    1598             :                                 }
    1599           0 :                                 BAT *b3;
    1600           0 :                                 b3 = BATproject(b2, mapnew);
    1601           0 :                                 bat_destroy(b2);
    1602           0 :                                 if (b3 == NULL) {
    1603           0 :                                         bat_destroy(b1);
    1604           0 :                                         bat_destroy(orig);
    1605           0 :                                         bat_destroy(b);
    1606           0 :                                         goto bailout;
    1607             :                                 }
    1608           0 :                                 rc = BATreplace(b, b1, b3, false);
    1609           0 :                                 bat_destroy(b1);
    1610           0 :                                 bat_destroy(b3);
    1611           0 :                                 if (rc != GDK_SUCCEED) {
    1612           0 :                                         bat_destroy(orig);
    1613           0 :                                         bat_destroy(b);
    1614           0 :                                         goto bailout;
    1615             :                                 }
    1616           0 :                                 if ((rc = BUNappend(lg->del, &orig->batCacheid, false)) != GDK_SUCCEED ||
    1617           0 :                                         (rc = BUNappend(lg->add, &b->batCacheid, false)) != GDK_SUCCEED) {
    1618           0 :                                         bat_destroy(orig);
    1619           0 :                                         bat_destroy(b);
    1620           0 :                                         goto bailout;
    1621             :                                 }
    1622           0 :                                 rc = GDK_FAIL;
    1623           0 :                                 BBPretain(orig->batCacheid);
    1624           0 :                                 BBPretain(b->batCacheid);
    1625           0 :                                 switch (tables[i].newid) {
    1626           0 :                                 case 2002:              /* sys.schemas.id */
    1627           0 :                                         bat_destroy(bats[0].idbat);
    1628           0 :                                         bats[0].idbat = b;
    1629           0 :                                         BBPfix(b->batCacheid);
    1630           0 :                                         break;
    1631           0 :                                 case 2068:              /* sys._tables.id */
    1632           0 :                                         bat_destroy(bats[1].idbat);
    1633           0 :                                         bats[1].idbat = b;
    1634           0 :                                         BBPfix(b->batCacheid);
    1635           0 :                                         break;
    1636           0 :                                 case 2070:              /* sys._tables.schema_id */
    1637           0 :                                         bat_destroy(bats[1].parbat);
    1638           0 :                                         bats[1].parbat = b;
    1639           0 :                                         BBPfix(b->batCacheid);
    1640           0 :                                         break;
    1641           0 :                                 case 2077:              /* sys._columns.id */
    1642           0 :                                         bat_destroy(bats[2].idbat);
    1643           0 :                                         bats[2].idbat = b;
    1644           0 :                                         BBPfix(b->batCacheid);
    1645           0 :                                         break;
    1646           0 :                                 case 2082:              /* sys._columns.table_id */
    1647           0 :                                         bat_destroy(bats[2].parbat);
    1648           0 :                                         bats[2].parbat = b;
    1649           0 :                                         BBPfix(b->batCacheid);
    1650           0 :                                         break;
    1651             :                                 }
    1652           0 :                                 bat_destroy(orig);
    1653             :                         }
    1654             :                         /* now b contains the updated values for the column in tables[i] */
    1655             :                 }
    1656             :                 /* here, b is either the original, unchanged bat or the updated one */
    1657           0 :                 if ((rc = BUNappend(lg->lg->catalog_bid, &b->batCacheid, true)) != GDK_SUCCEED ||
    1658           0 :                         (rc = BUNappend(lg->lg->catalog_id, &tables[i].newid, true)) != GDK_SUCCEED) {
    1659           0 :                         bat_destroy(b);
    1660           0 :                         goto bailout;
    1661             :                 }
    1662           0 :                 rc = GDK_FAIL;
    1663           0 :                 BBPretain(b->batCacheid);
    1664           0 :                 bat_destroy(b);
    1665             :         }
    1666             : 
    1667             :         /* add all extant non-system bats to the new catalog */
    1668           0 :         BAT *cands, *b;
    1669           0 :         if (BATcount(lg->dcatalog) == 0) {
    1670             :                 cands = NULL;
    1671             :         } else {
    1672           0 :                 if ((rc = BATsort(&b, NULL, NULL, lg->dcatalog, NULL, NULL, false, false, false)) != GDK_SUCCEED)
    1673           0 :                         goto bailout;
    1674           0 :                 rc = GDK_FAIL;
    1675           0 :                 cands = BATnegcands(BATcount(lg->catalog_oid), b);
    1676           0 :                 bat_destroy(b);
    1677           0 :                 if (cands == NULL)
    1678           0 :                         goto bailout;
    1679             :         }
    1680           0 :         b = BATselect(lg->catalog_oid, cands, &(lng){0}, NULL, true, true, true);
    1681           0 :         bat_destroy(cands);
    1682           0 :         if (b == NULL)
    1683           0 :                 goto bailout;
    1684           0 :         cands = b;
    1685           0 :         b = BATconvert(lg->catalog_oid, cands, TYPE_int, 0, 0, 0);
    1686           0 :         if (b == NULL) {
    1687           0 :                 bat_destroy(cands);
    1688           0 :                 goto bailout;
    1689             :         }
    1690           0 :         if ((rc = BATappend(lg->lg->catalog_id, b, NULL, true)) != GDK_SUCCEED ||
    1691           0 :                 (rc = BATappend(lg->lg->catalog_bid, lg->catalog_bid, cands, true)) != GDK_SUCCEED) {
    1692           0 :                 bat_destroy(cands);
    1693           0 :                 bat_destroy(b);
    1694           0 :                 goto bailout;
    1695             :         }
    1696           0 :         rc = GDK_FAIL;
    1697           0 :         const int *bids;
    1698           0 :         bids = (const int *) Tloc(lg->lg->catalog_bid, lg->lg->catalog_bid->batCount - BATcount(cands));
    1699           0 :         for (BUN j = BATcount(cands), i = 0; i < j; i++)
    1700           0 :                 BBPretain(bids[i]);
    1701           0 :         bat_destroy(cands);
    1702           0 :         bat_destroy(b);
    1703             : 
    1704             :         /* convert deleted rows bats (catalog id equals table id) from list
    1705             :          * of deleted rows to mask of deleted rows */
    1706           0 :         BAT *tabs;
    1707             :         /* 2164 is the largest fixed id, so select anything larger */
    1708           0 :         tabs = BATselect(lg->lg->catalog_id, NULL, &(int){2164}, &int_nil, false, true, false);
    1709           0 :         if (tabs == NULL)
    1710           0 :                 goto bailout;
    1711           0 :         BAT *b1;
    1712             :         /* extract those rows that refer to a known table (in bats[1].idbat) */
    1713           0 :         b1 = BATintersect(lg->lg->catalog_id, bats[1].idbat, tabs, bats[1].cands, false, false, BUN_NONE);
    1714           0 :         bat_destroy(tabs);
    1715           0 :         if (b1 == NULL)
    1716           0 :                 goto bailout;
    1717           0 :         BAT *b3, *b4;
    1718             :         /* find a column (any column) in each of the tables */
    1719           0 :         if ((rc = BATsemijoin(&b3, &b4, lg->lg->catalog_id, bats[2].parbat, b1, bats[2].cands, false, false, BUN_NONE)) != GDK_SUCCEED) {
    1720           0 :                 bat_destroy(b1);
    1721           0 :                 goto bailout;
    1722             :         }
    1723           0 :         rc = GDK_FAIL;
    1724           0 :         bat_destroy(b3);
    1725             :         /* extract column id */
    1726           0 :         b3 = BATproject(b4, bats[2].idbat);
    1727           0 :         bat_destroy(b4);
    1728           0 :         if (b3 == NULL) {
    1729           0 :                 bat_destroy(b1);
    1730           0 :                 goto bailout;
    1731             :         }
    1732           0 :         BAT *b2;
    1733           0 :         rc = BATleftjoin(&b2, &b4, b3, lg->lg->catalog_id, NULL, NULL, false, BUN_NONE);
    1734           0 :         bat_destroy(b3);
    1735           0 :         if (rc != GDK_SUCCEED) {
    1736           0 :                 bat_destroy(b1);
    1737           0 :                 goto bailout;
    1738             :         }
    1739           0 :         bat_destroy(b2);
    1740           0 :         struct canditer ci;
    1741           0 :         canditer_init(&ci, lg->lg->catalog_bid, b1);
    1742           0 :         const oid *cbids;
    1743           0 :         bids = Tloc(lg->lg->catalog_bid, 0);
    1744           0 :         cbids = Tloc(b4, 0);
    1745           0 :         for (BUN i = 0; i < ci.ncand; i++) {
    1746           0 :                 bat cbid = bids[cbids[i]];
    1747           0 :                 b = temp_descriptor(cbid);
    1748           0 :                 if (b == NULL) {
    1749           0 :                         bat_destroy(b1);
    1750           0 :                         bat_destroy(b3);
    1751           0 :                         goto bailout;
    1752             :                 }
    1753           0 :                 BUN len;
    1754           0 :                 len = BATcount(b);
    1755           0 :                 bat_destroy(b);
    1756           0 :                 oid o;
    1757           0 :                 o = canditer_next(&ci);
    1758           0 :                 bat tbid;
    1759           0 :                 tbid = bids[o - lg->lg->catalog_bid->hseqbase];
    1760           0 :                 b = temp_descriptor(tbid);
    1761           0 :                 BAT *bn;
    1762           0 :                 bn = BATconstant(0, TYPE_msk, &(msk){false}, len, PERSISTENT);
    1763           0 :                 if (b == NULL || bn == NULL) {
    1764           0 :                         bat_destroy(b);
    1765           0 :                         bat_destroy(bn);
    1766           0 :                         bat_destroy(b1);
    1767           0 :                         bat_destroy(b3);
    1768           0 :                         goto bailout;
    1769             :                 }
    1770           0 :                 const oid *dels;
    1771           0 :                 dels = Tloc(b, 0);
    1772           0 :                 for (BUN q = BATcount(b), p = 0; p < q; p++) {
    1773           0 :                         mskSetVal(bn, (BUN) dels[p], true);
    1774             :                 }
    1775           0 :                 bat_destroy(b);
    1776           0 :                 if ((rc = BUNappend(lg->del, &tbid, false)) != GDK_SUCCEED ||
    1777           0 :                     (rc = BUNappend(lg->add, &bn->batCacheid, false)) != GDK_SUCCEED ||
    1778           0 :                     (rc = BUNreplace(lg->lg->catalog_bid, o, &bn->batCacheid, true)) != GDK_SUCCEED) {
    1779           0 :                         bat_destroy(bn);
    1780           0 :                         bat_destroy(b1);
    1781           0 :                         bat_destroy(b3);
    1782           0 :                         goto bailout;
    1783             :                 }
    1784           0 :                 rc = GDK_FAIL;
    1785             :                 /* moving tbid from lg->lg->catalog_bid to lg->del does not change
    1786             :                  * lrefs of tbid (old location is overwritten by new table id) */
    1787           0 :                 BBPretain(bn->batCacheid);
    1788           0 :                 BBPretain(bn->batCacheid); /* yep, twice */
    1789           0 :                 bat_destroy(bn);
    1790             :         }
    1791           0 :         bat_destroy(b1);
    1792           0 :         bat_destroy(b4);
    1793             : 
    1794             :         /* map schema/table/column ids in other system tables */
    1795           0 :         if (mapold) {
    1796             :                 /* select tables in sys schema */
    1797           0 :                 b1 = BATselect(bats[1].parbat, bats[1].cands, &(int){2000}, NULL, true, true, false);
    1798           0 :                 if (b1 == NULL)
    1799           0 :                         goto bailout;
    1800           0 :                 bids = Tloc(lg->lg->catalog_bid, 0);
    1801           0 :                 for (int i = 0; mapids[i].column != NULL; i++) {
    1802             :                         /* row ids for table in sys schema */
    1803           0 :                         BAT *b2 = BATselect(bats[1].nmbat, b1, mapids[i].table, NULL, true, true, false);
    1804           0 :                         if (b2 == NULL) {
    1805           0 :                                 bat_destroy(b1);
    1806           0 :                                 goto bailout;
    1807             :                         }
    1808             :                         /* table ids for table */
    1809           0 :                         b3 = BATproject(b2, bats[1].idbat);
    1810           0 :                         bat_destroy(b2);
    1811           0 :                         if (b3 == NULL) {
    1812           0 :                                 bat_destroy(b1);
    1813           0 :                                 goto bailout;
    1814             :                         }
    1815             :                         /* row ids for columns of table */
    1816           0 :                         b2 = BATintersect(bats[2].parbat, b3, NULL, NULL, false, false, BUN_NONE);
    1817           0 :                         bat_destroy(b3);
    1818           0 :                         if (b2 == NULL) {
    1819           0 :                                 bat_destroy(b1);
    1820           0 :                                 goto bailout;
    1821             :                         }
    1822             :                         /* row id for the column in the table we're looking for */
    1823           0 :                         b3 = BATselect(bats[2].nmbat, b2, mapids[i].column, NULL, true, true, false);
    1824           0 :                         bat_destroy(b2);
    1825           0 :                         if (b3 == NULL) {
    1826           0 :                                 bat_destroy(b1);
    1827           0 :                                 goto bailout;
    1828             :                         }
    1829             :                         /* row ids in catalog for column in table */
    1830           0 :                         b2 = BATintersect(lg->lg->catalog_id, bats[2].idbat, NULL, b3, false, false, 1);
    1831           0 :                         bat_destroy(b3);
    1832           0 :                         if (b2 == NULL) {
    1833           0 :                                 bat_destroy(b1);
    1834           0 :                                 goto bailout;
    1835             :                         }
    1836           0 :                         for (BUN j = 0; j < BATcount(b2); j++) {
    1837           0 :                                 oid p = BUNtoid(b2, j);
    1838           0 :                                 b3 = BATdescriptor(bids[p]);
    1839           0 :                                 if (b3 == NULL) {
    1840           0 :                                         bat_destroy(b1);
    1841           0 :                                         bat_destroy(b2);
    1842           0 :                                         goto bailout;
    1843             :                                 }
    1844           0 :                                 BAT *b4, *b5;
    1845           0 :                                 if ((rc = BATjoin(&b4, &b5, b3, mapold, NULL, NULL, false, BUN_NONE)) != GDK_SUCCEED) {
    1846           0 :                                         bat_destroy(b1);
    1847           0 :                                         bat_destroy(b2);
    1848           0 :                                         bat_destroy(b3);
    1849           0 :                                         goto bailout;
    1850             :                                 }
    1851           0 :                                 rc = GDK_FAIL;
    1852           0 :                                 if (BATcount(b4) == 0) {
    1853           0 :                                         bat_destroy(b3);
    1854           0 :                                         bat_destroy(b4);
    1855           0 :                                         bat_destroy(b5);
    1856             :                                 } else {
    1857           0 :                                         BAT *b6;
    1858           0 :                                         b6 = COLcopy(b3, b3->ttype, true, PERSISTENT);
    1859           0 :                                         bat_destroy(b3);
    1860           0 :                                         b3 = BATproject(b5, mapnew);
    1861           0 :                                         bat_destroy(b5);
    1862           0 :                                         if (b3 == NULL || b6 == NULL) {
    1863           0 :                                                 bat_destroy(b1);
    1864           0 :                                                 bat_destroy(b2);
    1865           0 :                                                 bat_destroy(b3);
    1866           0 :                                                 bat_destroy(b4);
    1867           0 :                                                 bat_destroy(b6);
    1868           0 :                                                 goto bailout;
    1869             :                                         }
    1870           0 :                                         if ((rc = BATreplace(b6, b4, b3, false)) == GDK_SUCCEED &&
    1871           0 :                                                 (rc = BUNappend(lg->del, &bids[p], false)) == GDK_SUCCEED &&
    1872           0 :                                                 (rc = BUNappend(lg->add, &b6->batCacheid, false)) == GDK_SUCCEED)
    1873           0 :                                                 rc = BUNreplace(lg->lg->catalog_bid, p, &b6->batCacheid, true);
    1874           0 :                                         BBPretain(b6->batCacheid);
    1875           0 :                                         BBPretain(b6->batCacheid);
    1876           0 :                                         bat_destroy(b3);
    1877           0 :                                         bat_destroy(b4);
    1878           0 :                                         bat_destroy(b6);
    1879           0 :                                         if (rc != GDK_SUCCEED) {
    1880           0 :                                                 bat_destroy(b1);
    1881           0 :                                                 bat_destroy(b2);
    1882           0 :                                                 goto bailout;
    1883             :                                         }
    1884             :                                         rc = GDK_FAIL;
    1885             :                                 }
    1886             :                         }
    1887           0 :                         bat_destroy(b2);
    1888             :                 }
    1889           0 :                 bat_destroy(b1);
    1890             :         }
    1891             : 
    1892             :         /* add all bats that were added by processing the WAL and that have
    1893             :          * not been deleted since to the list of new bats */
    1894           0 :         bids = (const int *) Tloc(lg->catalog_bid, 0);
    1895           0 :         for (BUN p = lg->catalog_bid->batInserted, q = lg->catalog_bid->batCount;
    1896           0 :                  p < q;
    1897           0 :                  p++) {
    1898           0 :                 bat bid = bids[p];
    1899           0 :                 if (BUNfnd(lg->lg->catalog_bid, &(int){bid}) != BUN_NONE) {
    1900           0 :                         b = BATdescriptor(bid);
    1901           0 :                         if (b) {
    1902           0 :                                 if (BATmode(b, false) != GDK_SUCCEED ||
    1903           0 :                                         BUNappend(lg->add, &(int){bid}, false) != GDK_SUCCEED) {
    1904           0 :                                         BBPunfix(bid);
    1905           0 :                                         goto bailout;
    1906             :                                 }
    1907           0 :                                 BBPkeepref(b);
    1908             :                         }
    1909             :                 }
    1910             :         }
    1911             : 
    1912             :         rc = GDK_SUCCEED;
    1913             : 
    1914           0 :   bailout:
    1915           0 :         bat_destroy(mapold);
    1916           0 :         bat_destroy(mapnew);
    1917           0 :         for (int i = 0; i < 3; i++) {
    1918           0 :                 bat_destroy(bats[i].nmbat);
    1919           0 :                 bat_destroy(bats[i].idbat);
    1920           0 :                 bat_destroy(bats[i].parbat);
    1921           0 :                 bat_destroy(bats[i].cands);
    1922             :         }
    1923           0 :         return rc;
    1924             : }
    1925             : 
    1926             : static gdk_return
    1927           0 : bl_postversion(void *Store, void *Lg)
    1928             : {
    1929           0 :         sqlstore *store = Store;
    1930           0 :         old_logger *old_lg;
    1931           0 :         logger *lg;
    1932           0 :         gdk_return rc;
    1933             : 
    1934           0 :         if (store->catalog_version < 52300) { /* the watershed */
    1935             :                 /* called from gdk_logger_old.c; Lg is the old logger */
    1936           0 :                 old_lg = Lg;
    1937           0 :                 if (upgrade(old_lg) != GDK_SUCCEED)
    1938             :                         return GDK_FAIL;
    1939           0 :                 lg = old_lg->lg;
    1940             :         } else {
    1941             :                 /* called from gdk_logger.c; Lg is the new logger, there is no old */
    1942             :                 old_lg = NULL;
    1943             :                 lg = Lg;
    1944             :         }
    1945           0 :         bool tabins_first = true;
    1946             : 
    1947             : #ifdef CATALOG_NOV2019
    1948           0 :         if (store->catalog_version <= CATALOG_NOV2019) {
    1949           0 :                 BAT *te, *tne;
    1950           0 :                 const int *ocl; /* old eclass */
    1951           0 :                 int *ncl;       /* new eclass */
    1952             : 
    1953           0 :                 te = log_temp_descriptor(log_find_bat(lg, 2014)); /* sys.types.eclass */
    1954           0 :                 if (te == NULL)
    1955           0 :                         return GDK_FAIL;
    1956           0 :                 tne = COLnew(te->hseqbase, TYPE_int, BATcount(te), PERSISTENT);
    1957           0 :                 if (tne == NULL) {
    1958           0 :                         bat_destroy(te);
    1959           0 :                         return GDK_FAIL;
    1960             :                 }
    1961           0 :                 ocl = Tloc(te, 0);
    1962           0 :                 ncl = Tloc(tne, 0);
    1963           0 :                 for (BUN p = 0, q = BATcount(te); p < q; p++) {
    1964           0 :                         switch (ocl[p]) {
    1965           0 :                         case EC_TIME_TZ:                /* old EC_DATE */
    1966           0 :                                 ncl[p] = EC_DATE;
    1967           0 :                                 break;
    1968           0 :                         case EC_DATE:                   /* old EC_TIMESTAMP */
    1969           0 :                                 ncl[p] = EC_TIMESTAMP;
    1970           0 :                                 break;
    1971           0 :                         case EC_TIMESTAMP:              /* old EC_GEOM */
    1972           0 :                                 ncl[p] = EC_GEOM;
    1973           0 :                                 break;
    1974           0 :                         case EC_TIMESTAMP_TZ:   /* old EC_EXTERNAL */
    1975           0 :                                 ncl[p] = EC_EXTERNAL;
    1976           0 :                                 break;
    1977           0 :                         default:
    1978             :                                 /* others stay unchanged */
    1979           0 :                                 ncl[p] = ocl[p];
    1980           0 :                                 break;
    1981             :                         }
    1982             :                 }
    1983           0 :                 BATsetcount(tne, BATcount(te));
    1984           0 :                 tne->tnil = false;
    1985           0 :                 tne->tnonil = true;
    1986           0 :                 tne->tsorted = false;
    1987           0 :                 tne->trevsorted = false;
    1988           0 :                 tne->tkey = false;
    1989           0 :                 if (BUNappend(old_lg->del, &te->batCacheid, false) != GDK_SUCCEED ||
    1990           0 :                         BUNappend(old_lg->add, &tne->batCacheid, false) != GDK_SUCCEED ||
    1991           0 :                         BUNreplace(lg->catalog_bid, BUNfnd(lg->catalog_id, &(int){2014}), &tne->batCacheid, true) != GDK_SUCCEED) {
    1992           0 :                         bat_destroy(te);
    1993           0 :                         bat_destroy(tne);
    1994           0 :                         return GDK_FAIL;
    1995             :                 }
    1996           0 :                 BBPretain(tne->batCacheid);
    1997           0 :                 BBPretain(tne->batCacheid);
    1998           0 :                 bat_destroy(te);
    1999           0 :                 bat_destroy(tne);
    2000             :         }
    2001             : #endif
    2002             : 
    2003             : #ifdef CATALOG_JUN2020
    2004           0 :         if (store->catalog_version <= CATALOG_JUN2020
    2005             : #ifdef CATALOG_JUN2020_MMT
    2006           0 :                 || store->catalog_version == CATALOG_JUN2020_MMT
    2007             : #endif
    2008             :                 ) {
    2009           0 :                 BAT *b;                                                          /* temp variable */
    2010             :                 {
    2011             :                         /* new BOOLEAN column sys.functions.semantics */
    2012           0 :                         b = log_temp_descriptor(log_find_bat(lg, 2017)); /* sys.functions.id */
    2013           0 :                         if (b == NULL)
    2014           0 :                                 return GDK_FAIL;
    2015           0 :                         BAT *sem = BATconstant(b->hseqbase, TYPE_bit, &(bit){1}, BATcount(b), PERSISTENT);
    2016           0 :                         bat_destroy(b);
    2017           0 :                         if (sem == NULL)
    2018             :                                 return GDK_FAIL;
    2019           0 :                         if ((sem = BATsetaccess(sem, BAT_READ)) == NULL ||
    2020             :                                 /* 2162 is sys.functions.semantics */
    2021           0 :                                 BUNappend(lg->catalog_id, &(int) {2162}, true) != GDK_SUCCEED ||
    2022           0 :                                 BUNappend(lg->catalog_bid, &sem->batCacheid, true) != GDK_SUCCEED ||
    2023           0 :                                 BUNappend(old_lg->add, &sem->batCacheid, false) != GDK_SUCCEED) {
    2024           0 :                                 bat_destroy(sem);
    2025           0 :                                 return GDK_FAIL;
    2026             :                         }
    2027           0 :                         BBPretain(sem->batCacheid);
    2028           0 :                         BBPretain(sem->batCacheid); /* yep, twice */
    2029           0 :                         bat_destroy(sem);
    2030             : 
    2031           0 :                         if (tabins(lg, old_lg, tabins_first, -1, 0,
    2032           0 :                                            2076, &(msk) {false},    /* sys._columns */
    2033             :                                            /* 2162 is sys.functions.semantics */
    2034           0 :                                            2077, &(int) {2162},             /* sys._columns.id */
    2035             :                                            2078, "semantics",         /* sys._columns.name */
    2036             :                                            2079, "boolean",                   /* sys._columns.type */
    2037           0 :                                            2080, &(int) {1},                /* sys._columns.type_digits */
    2038           0 :                                            2081, &(int) {0},                /* sys._columns.type_scale */
    2039             :                                            /* 2016 is sys.functions */
    2040           0 :                                            2082, &(int) {2016},             /* sys._columns.table_id */
    2041             :                                            2083, str_nil,                       /* sys._columns.default */
    2042           0 :                                            2084, &(bit) {TRUE},             /* sys._columns.null */
    2043           0 :                                            2085, &(int) {11},               /* sys._columns.number */
    2044             :                                            2086, str_nil,                       /* sys._columns.storage */
    2045             :                                            0) != GDK_SUCCEED)
    2046           0 :                                 return GDK_FAIL;
    2047           0 :                         tabins_first = false;
    2048             :                 }
    2049             : 
    2050             :                 /* sys.functions i.e. deleted rows */
    2051           0 :                 BAT *del_funcs = log_temp_descriptor(log_find_bat(lg, 2016));
    2052             :                 {
    2053             :                         /* move sql.degrees, sql.radians, sql.like and sql.ilike functions
    2054             :                          * from 09_like.sql and 10_math.sql script to sql_types list */
    2055             :                         /* sys.functions.name */
    2056           0 :                         BAT *func_func = log_temp_descriptor(log_find_bat(lg, 2018));
    2057             :                         /* sys.functions.schema_id */
    2058           0 :                         BAT *func_schem = log_temp_descriptor(log_find_bat(lg, 2026));
    2059           0 :                         BAT *func_tid;
    2060           0 :                         BAT *cands;
    2061           0 :                         if (del_funcs == NULL || func_func == NULL || func_schem == NULL) {
    2062           0 :                                 bat_destroy(del_funcs);
    2063           0 :                                 bat_destroy(func_func);
    2064           0 :                                 bat_destroy(func_schem);
    2065           0 :                                 return GDK_FAIL;
    2066             :                         }
    2067           0 :                         func_tid = BATmaskedcands(0, BATcount(del_funcs), del_funcs, false);
    2068           0 :                         if (func_tid == NULL) {
    2069           0 :                                 bat_destroy(del_funcs);
    2070           0 :                                 bat_destroy(func_func);
    2071           0 :                                 bat_destroy(func_schem);
    2072           0 :                                 return GDK_FAIL;
    2073             :                         }
    2074             :                         /* select * from sys.functions where schema_id = 2000; */
    2075           0 :                         b = BATselect(func_schem, func_tid, &(int) {2000}, NULL, true, true, false);
    2076           0 :                         bat_destroy(func_schem);
    2077           0 :                         bat_destroy(func_tid);
    2078           0 :                         cands = b;
    2079           0 :                         if (cands == NULL) {
    2080           0 :                                 bat_destroy(del_funcs);
    2081           0 :                                 bat_destroy(func_func);
    2082           0 :                                 return GDK_FAIL;
    2083             :                         }
    2084             : 
    2085           0 :                         BAT *funcs;
    2086           0 :                         if ((funcs = COLnew(0, TYPE_str, 4, TRANSIENT)) == NULL ||
    2087           0 :                                 BUNappend(funcs, "degrees", false) != GDK_SUCCEED ||
    2088           0 :                                 BUNappend(funcs, "ilike", false) != GDK_SUCCEED ||
    2089           0 :                                 BUNappend(funcs, "like", false) != GDK_SUCCEED ||
    2090           0 :                                 BUNappend(funcs, "radians", false) != GDK_SUCCEED) {
    2091           0 :                                 bat_destroy(funcs);
    2092           0 :                                 bat_destroy(del_funcs);
    2093           0 :                                 bat_destroy(func_func);
    2094           0 :                                 return GDK_FAIL;
    2095             :                         }
    2096           0 :                         b = BATintersect(func_func, funcs, cands, NULL, false, false, 4);
    2097           0 :                         bat_destroy(func_func);
    2098           0 :                         bat_destroy(funcs);
    2099           0 :                         bat_destroy(cands);
    2100           0 :                         funcs = NULL;
    2101           0 :                         rc = GDK_FAIL;
    2102           0 :                         if (b != NULL &&
    2103           0 :                                 (funcs = BATconstant(0, TYPE_msk, &(msk){true}, BATcount(b), TRANSIENT)) != NULL)
    2104           0 :                                 rc = BATreplace(del_funcs, b, funcs, false);
    2105           0 :                         bat_destroy(b);
    2106           0 :                         bat_destroy(funcs);
    2107           0 :                         if (rc != GDK_SUCCEED)
    2108             :                                 return rc;
    2109             :                 }
    2110             : 
    2111             :                 {
    2112             :                         /* Fix SQL aggregation functions defined on the wrong modules:
    2113             :                          * sql.null, sql.all, sql.zero_or_one and sql.not_unique */
    2114           0 :                         BAT *func_tid = BATmaskedcands(0, BATcount(del_funcs), del_funcs, false);
    2115             :                         /* sys.functions.mod */
    2116           0 :                         BAT *func_mod = log_temp_descriptor(log_find_bat(lg, 2020));
    2117           0 :                         bat_destroy(del_funcs);
    2118           0 :                         if (func_tid == NULL || func_mod == NULL) {
    2119           0 :                                 bat_destroy(func_tid);
    2120           0 :                                 bat_destroy(func_mod);
    2121           0 :                                 return GDK_FAIL;
    2122             :                         }
    2123             : 
    2124             :                         /* find the (undeleted) functions defined on "sql" module */
    2125           0 :                         BAT *sqlfunc = BATselect(func_mod, func_tid, "sql", NULL, true, true, false);
    2126           0 :                         bat_destroy(func_tid);
    2127           0 :                         if (sqlfunc == NULL) {
    2128           0 :                                 bat_destroy(func_mod);
    2129           0 :                                 return GDK_FAIL;
    2130             :                         }
    2131             :                         /* sys.functions.type */
    2132           0 :                         BAT *func_type = log_temp_descriptor(log_find_bat(lg, 2022));
    2133           0 :                         if (func_type == NULL) {
    2134           0 :                                 bat_destroy(func_mod);
    2135           0 :                                 bat_destroy(sqlfunc);
    2136           0 :                                 return GDK_FAIL;
    2137             :                         }
    2138             :                         /* and are aggregates (3) */
    2139           0 :                         BAT *sqlaggr_func = BATselect(func_type, sqlfunc, &(int) {3}, NULL, true, true, false);
    2140           0 :                         bat_destroy(sqlfunc);
    2141           0 :                         bat_destroy(func_type);
    2142           0 :                         if (sqlaggr_func == NULL) {
    2143           0 :                                 bat_destroy(func_mod);
    2144           0 :                                 return GDK_FAIL;
    2145             :                         }
    2146             : 
    2147             :                         /* sys.functions.func */
    2148           0 :                         BAT *func_func = log_temp_descriptor(log_find_bat(lg, 2019));
    2149           0 :                         if (func_func == NULL) {
    2150           0 :                                 bat_destroy(func_mod);
    2151           0 :                                 bat_destroy(sqlaggr_func);
    2152           0 :                                 return GDK_FAIL;
    2153             :                         }
    2154           0 :                         b = COLcopy(func_mod, func_mod->ttype, true, PERSISTENT);
    2155           0 :                         if (b == NULL) {
    2156           0 :                                 bat_destroy(func_func);
    2157           0 :                                 bat_destroy(func_mod);
    2158           0 :                                 bat_destroy(sqlaggr_func);
    2159           0 :                                 return GDK_FAIL;
    2160             :                         }
    2161           0 :                         if (BUNappend(old_lg->del, &func_mod->batCacheid, false) != GDK_SUCCEED ||
    2162           0 :                                 BUNappend(old_lg->add, &b->batCacheid, false) != GDK_SUCCEED ||
    2163             :                                 /* 2020 is sys.functions.mod */
    2164           0 :                                 BUNreplace(lg->catalog_bid, BUNfnd(lg->catalog_id, &(int){2020}), &b->batCacheid, true) != GDK_SUCCEED) {
    2165           0 :                                 bat_destroy(func_func);
    2166           0 :                                 bat_destroy(func_mod);
    2167           0 :                                 bat_destroy(sqlaggr_func);
    2168           0 :                                 bat_destroy(b);
    2169           0 :                                 return GDK_FAIL;
    2170             :                         }
    2171           0 :                         BBPretain(b->batCacheid);
    2172           0 :                         BBPretain(b->batCacheid);
    2173           0 :                         bat_destroy(func_mod);
    2174           0 :                         func_mod = b;
    2175           0 :                         BAT *aggrs;
    2176           0 :                         if ((aggrs = COLnew(0, TYPE_str, 4, TRANSIENT)) == NULL ||
    2177           0 :                                 BUNappend(aggrs, "all", false) != GDK_SUCCEED ||
    2178           0 :                                 BUNappend(aggrs, "no_unique", false) != GDK_SUCCEED ||
    2179           0 :                                 BUNappend(aggrs, "null", false) != GDK_SUCCEED ||
    2180           0 :                                 BUNappend(aggrs, "zero_or_one", false) != GDK_SUCCEED) {
    2181           0 :                                 bat_destroy(aggrs);
    2182           0 :                                 bat_destroy(func_func);
    2183           0 :                                 bat_destroy(func_mod);
    2184           0 :                                 bat_destroy(sqlaggr_func);
    2185           0 :                                 return GDK_FAIL;
    2186             :                         }
    2187           0 :                         b = BATintersect(func_func, aggrs, sqlaggr_func, NULL, false, false, 4);
    2188           0 :                         bat_destroy(func_func);
    2189           0 :                         bat_destroy(aggrs);
    2190           0 :                         bat_destroy(sqlaggr_func);
    2191           0 :                         aggrs = NULL;
    2192           0 :                         rc = GDK_FAIL;
    2193           0 :                         if (b != NULL &&
    2194           0 :                                 (aggrs = BATconstant(0, TYPE_str, "aggr", BATcount(b), TRANSIENT)) != NULL)
    2195           0 :                                 rc = BATreplace(func_mod, b, aggrs, false);
    2196           0 :                         bat_destroy(b);
    2197           0 :                         bat_destroy(aggrs);
    2198           0 :                         bat_destroy(func_mod);
    2199           0 :                         if (rc != GDK_SUCCEED)
    2200             :                                 return rc;
    2201             :                 }
    2202             :         }
    2203             : #endif
    2204             : 
    2205             : #ifdef CATALOG_OCT2020
    2206           0 :         if (store->catalog_version <= CATALOG_OCT2020) { /* not for Jun2020-mmt! */
    2207             :                 /* add sub column to "objects" table. This is required for merge tables */
    2208             :                 /* alter table sys.objects add column sub integer; */
    2209           0 :                 if (tabins(lg, old_lg, tabins_first, -1, 0,
    2210           0 :                                    2076, &(msk) {false},    /* sys._columns */
    2211             :                                    /* 2163 is sys.objects.sub */
    2212           0 :                                    2077, &(int) {2163},             /* sys._columns.id */
    2213             :                                    2078, "sub",                               /* sys._columns.name */
    2214             :                                    2079, "int",                               /* sys._columns.type */
    2215           0 :                                    2080, &(int) {32},               /* sys._columns.type_digits */
    2216           0 :                                    2081, &(int) {0},                /* sys._columns.type_scale */
    2217             :                                    /* 2110 is sys.objects */
    2218           0 :                                    2082, &(int) {2110},             /* sys._columns.table_id */
    2219             :                                    2083, str_nil,                       /* sys._columns.default */
    2220           0 :                                    2084, &(bit) {TRUE},             /* sys._columns.null */
    2221           0 :                                    2085, &(int) {3},                /* sys._columns.number */
    2222             :                                    2086, str_nil,                       /* sys._columns.storage */
    2223             :                                    0) != GDK_SUCCEED)
    2224           0 :                         return GDK_FAIL;
    2225           0 :                 tabins_first = false;
    2226           0 :                 if (tabins(lg, old_lg, tabins_first, -1, 0,
    2227           0 :                                    2076, &(msk) {false},    /* sys._columns */
    2228             :                                    /* 2164 is tmp.objects.sub */
    2229           0 :                                    2077, &(int) {2164},             /* sys._columns.id */
    2230             :                                    2078, "sub",                               /* sys._columns.name */
    2231             :                                    2079, "int",                               /* sys._columns.type */
    2232           0 :                                    2080, &(int) {32},               /* sys._columns.type_digits */
    2233           0 :                                    2081, &(int) {0},                /* sys._columns.type_scale */
    2234             :                                    /* 2158 is tmp.objects */
    2235           0 :                                    2082, &(int) {2158},             /* sys._columns.table_id */
    2236             :                                    2083, str_nil,                       /* sys._columns.default */
    2237           0 :                                    2084, &(bit) {TRUE},             /* sys._columns.null */
    2238           0 :                                    2085, &(int) {3},                /* sys._columns.number */
    2239             :                                    2086, str_nil,                       /* sys._columns.storage */
    2240             :                                    0) != GDK_SUCCEED)
    2241           0 :                         return GDK_FAIL;
    2242             : 
    2243             :                 /* create bat for new column sys.objects.sub with value NULL, then
    2244             :                  * update sys.objects set sub = nr, nr = id where nr > 2000 */
    2245             :                 {
    2246           0 :                         BAT *objs_id = log_temp_descriptor(log_find_bat(lg, 2111)); /* sys.objects.id */
    2247           0 :                         BAT *objs_nr = log_temp_descriptor(log_find_bat(lg, 2113)); /* sys.objects.nr */
    2248           0 :                         BAT *objs_sub = BATconstant(objs_id->hseqbase, TYPE_int, &int_nil, BATcount(objs_id), PERSISTENT);
    2249           0 :                         BAT *b = log_temp_descriptor(log_find_bat(lg, 2110)); /* sys.objects */
    2250           0 :                         if (objs_id == NULL || objs_nr == NULL || objs_sub == NULL || b == NULL) {
    2251           0 :                                 bat_destroy(objs_id);
    2252           0 :                                 bat_destroy(objs_nr);
    2253           0 :                                 bat_destroy(b);
    2254           0 :                                 bat_destroy(objs_sub);
    2255           0 :                                 return GDK_FAIL;
    2256             :                         }
    2257           0 :                         BAT *cands = BATmaskedcands(0, BATcount(b), b, false);
    2258           0 :                         bat_destroy(b);
    2259           0 :                         if (cands == NULL) {
    2260           0 :                                 bat_destroy(objs_id);
    2261           0 :                                 bat_destroy(objs_nr);
    2262           0 :                                 bat_destroy(objs_sub);
    2263           0 :                                 return GDK_FAIL;
    2264             :                         }
    2265           0 :                         b = BATselect(objs_nr, cands, &(int) {2000}, &int_nil, false, false, false);
    2266           0 :                         bat_destroy(cands);
    2267           0 :                         if (b == NULL) {
    2268           0 :                                 bat_destroy(objs_id);
    2269           0 :                                 bat_destroy(objs_nr);
    2270           0 :                                 bat_destroy(objs_sub);
    2271           0 :                                 return GDK_FAIL;
    2272             :                         }
    2273           0 :                         cands = b;
    2274           0 :                         b = BATproject2(cands, objs_nr, NULL);
    2275           0 :                         if (b == NULL) {
    2276           0 :                                 bat_destroy(objs_id);
    2277           0 :                                 bat_destroy(objs_nr);
    2278           0 :                                 bat_destroy(objs_sub);
    2279           0 :                                 bat_destroy(cands);
    2280           0 :                                 return GDK_FAIL;
    2281             :                         }
    2282           0 :                         rc = BATreplace(objs_sub, cands, b, false);
    2283           0 :                         bat_destroy(b);
    2284           0 :                         if (rc != GDK_SUCCEED) {
    2285           0 :                                 bat_destroy(objs_id);
    2286           0 :                                 bat_destroy(objs_nr);
    2287           0 :                                 bat_destroy(objs_sub);
    2288           0 :                                 bat_destroy(cands);
    2289           0 :                                 return GDK_FAIL;
    2290             :                         }
    2291           0 :                         b = COLcopy(objs_nr, objs_nr->ttype, true, PERSISTENT);
    2292           0 :                         rc = BUNappend(old_lg->del, &objs_nr->batCacheid, false);
    2293           0 :                         bat_destroy(objs_nr);
    2294           0 :                         if (b == NULL || rc != GDK_SUCCEED) {
    2295           0 :                                 bat_destroy(objs_id);
    2296           0 :                                 bat_destroy(objs_sub);
    2297           0 :                                 bat_destroy(cands);
    2298           0 :                                 bat_destroy(b);
    2299           0 :                                 return GDK_FAIL;
    2300             :                         }
    2301           0 :                         objs_nr = b;
    2302           0 :                         if (BUNappend(old_lg->add, &objs_nr->batCacheid, false) != GDK_SUCCEED) {
    2303           0 :                                 bat_destroy(objs_id);
    2304           0 :                                 bat_destroy(objs_sub);
    2305           0 :                                 bat_destroy(objs_nr);
    2306           0 :                                 bat_destroy(cands);
    2307           0 :                                 return GDK_FAIL;
    2308             :                         }
    2309           0 :                         BBPretain(objs_nr->batCacheid);
    2310           0 :                         b = BATproject2(cands, objs_id, NULL);
    2311           0 :                         if (b == NULL) {
    2312           0 :                                 bat_destroy(objs_id);
    2313           0 :                                 bat_destroy(objs_nr);
    2314           0 :                                 bat_destroy(objs_sub);
    2315           0 :                                 bat_destroy(cands);
    2316           0 :                                 return GDK_FAIL;
    2317             :                         }
    2318           0 :                         rc = BATreplace(objs_nr, cands, b, false);
    2319           0 :                         bat_destroy(b);
    2320           0 :                         if (rc != GDK_SUCCEED) {
    2321           0 :                                 bat_destroy(objs_id);
    2322           0 :                                 bat_destroy(objs_nr);
    2323           0 :                                 bat_destroy(objs_sub);
    2324           0 :                                 bat_destroy(cands);
    2325           0 :                                 return GDK_FAIL;
    2326             :                         }
    2327             : 
    2328           0 :                         b = COLcopy(objs_id, objs_id->ttype, true, PERSISTENT);
    2329           0 :                         rc = BUNappend(old_lg->del, &objs_id->batCacheid, false);
    2330           0 :                         bat_destroy(objs_id);
    2331           0 :                         if (b == NULL || rc != GDK_SUCCEED) {
    2332           0 :                                 bat_destroy(objs_nr);
    2333           0 :                                 bat_destroy(objs_sub);
    2334           0 :                                 bat_destroy(cands);
    2335           0 :                                 bat_destroy(b);
    2336           0 :                                 return GDK_FAIL;
    2337             :                         }
    2338           0 :                         objs_id = b;
    2339           0 :                         if (BUNappend(old_lg->add, &objs_id->batCacheid, false) != GDK_SUCCEED) {
    2340           0 :                                 bat_destroy(objs_id);
    2341           0 :                                 bat_destroy(objs_nr);
    2342           0 :                                 bat_destroy(objs_sub);
    2343           0 :                                 bat_destroy(cands);
    2344           0 :                                 return GDK_FAIL;
    2345             :                         }
    2346           0 :                         BBPretain(objs_id->batCacheid);
    2347             : 
    2348           0 :                         BUN cnt = BATcount(cands), p;
    2349             : 
    2350           0 :                         if (!(b = COLnew(objs_id->hseqbase, TYPE_int, cnt, TRANSIENT)) ||
    2351           0 :                                 (p = BUNfnd(old_lg->seqs_id, &(int){OBJ_SID})) == BUN_NONE) {
    2352           0 :                                 bat_destroy(objs_id);
    2353           0 :                                 bat_destroy(objs_nr);
    2354           0 :                                 bat_destroy(objs_sub);
    2355           0 :                                 bat_destroy(cands);
    2356           0 :                                 bat_destroy(b);
    2357           0 :                                 return GDK_FAIL;
    2358             :                         }
    2359             : 
    2360           0 :                         int *bp = (int*)Tloc(b, 0), id = (int) *(lng *) Tloc(old_lg->seqs_val, p);
    2361           0 :                         for (BUN i = 0; i < cnt; i++)
    2362           0 :                                 bp[i] = id++;
    2363           0 :                         BATsetcount(b, cnt);
    2364           0 :                         b->tsorted = b->trevsorted = b->tkey = b->tnonil = b->tnil = false;
    2365           0 :                         b->tnosorted = b->tnorevsorted = 0;
    2366           0 :                         lng lid = (lng) id;
    2367             : 
    2368           0 :                         rc = BATreplace(objs_id, cands, b, false);
    2369           0 :                         bat_destroy(cands);
    2370           0 :                         bat_destroy(b);
    2371           0 :                         if (rc != GDK_SUCCEED) {
    2372           0 :                                 bat_destroy(objs_id);
    2373           0 :                                 bat_destroy(objs_nr);
    2374           0 :                                 bat_destroy(objs_sub);
    2375           0 :                                 return GDK_FAIL;
    2376             :                         }
    2377             : 
    2378             :                         /* 2111 is sys.objects.id and 2113 is sys.objects.nr */
    2379           0 :                         if (BUNreplace(old_lg->seqs_val, BUNfnd(old_lg->seqs_id, &(int){OBJ_SID}), &lid, true) != GDK_SUCCEED ||
    2380           0 :                                 BUNreplace(lg->catalog_bid, BUNfnd(lg->catalog_id, &(int){2111}), &objs_id->batCacheid, true) != GDK_SUCCEED ||
    2381           0 :                                 BUNreplace(lg->catalog_bid, BUNfnd(lg->catalog_id, &(int){2113}), &objs_nr->batCacheid, true) != GDK_SUCCEED ||
    2382           0 :                                 (objs_sub = BATsetaccess(objs_sub, BAT_READ)) == NULL ||
    2383           0 :                                 (objs_id = BATsetaccess(objs_id, BAT_READ)) == NULL ||
    2384           0 :                                 (objs_nr = BATsetaccess(objs_nr, BAT_READ)) == NULL ||
    2385             :                                 /* 2163 is sys.objects.sub */
    2386           0 :                                 BUNappend(lg->catalog_id, &(int) {2163}, true) != GDK_SUCCEED ||
    2387           0 :                                 BUNappend(old_lg->add, &objs_sub->batCacheid, false) != GDK_SUCCEED ||
    2388           0 :                                 BUNappend(lg->catalog_bid, &objs_sub->batCacheid, true) != GDK_SUCCEED) {
    2389           0 :                                 bat_destroy(objs_id);
    2390           0 :                                 bat_destroy(objs_nr);
    2391           0 :                                 bat_destroy(objs_sub);
    2392           0 :                                 return GDK_FAIL;
    2393             :                         }
    2394           0 :                         BBPretain(objs_sub->batCacheid);
    2395           0 :                         BBPretain(objs_sub->batCacheid);
    2396           0 :                         BBPretain(objs_nr->batCacheid);
    2397           0 :                         BBPretain(objs_id->batCacheid);
    2398           0 :                         bat_destroy(objs_id);
    2399           0 :                         bat_destroy(objs_nr);
    2400           0 :                         bat_destroy(objs_sub);
    2401             : 
    2402             :                         /* alter table tmp.objects add column sub integer; */
    2403           0 :                         objs_sub = BATconstant(0, TYPE_int, &int_nil, 0, PERSISTENT);
    2404           0 :                         if (objs_sub == NULL) {
    2405             :                                 return GDK_FAIL;
    2406             :                         }
    2407           0 :                         if ((objs_sub = BATsetaccess(objs_sub, BAT_READ)) == NULL ||
    2408             :                                 /* 2164 is tmp.objects.sub */
    2409           0 :                                 BUNappend(lg->catalog_id, &(int) {2164}, true) != GDK_SUCCEED ||
    2410           0 :                                 BUNappend(old_lg->add, &objs_sub->batCacheid, false) != GDK_SUCCEED ||
    2411           0 :                                 BUNappend(lg->catalog_bid, &objs_sub->batCacheid, true) != GDK_SUCCEED) {
    2412           0 :                                 bat_destroy(objs_sub);
    2413           0 :                                 return GDK_FAIL;
    2414             :                         }
    2415           0 :                         BBPretain(objs_sub->batCacheid);
    2416           0 :                         BBPretain(objs_sub->batCacheid);
    2417           0 :                         bat_destroy(objs_sub);
    2418             :                 }
    2419             :         }
    2420             : #endif
    2421             : #ifdef CATALOG_OCT2020
    2422           0 :         if (store->catalog_version <= CATALOG_OCT2020
    2423             : #ifdef CATALOG_JUN2020_MMT
    2424             :                 || store->catalog_version == CATALOG_JUN2020_MMT
    2425             : #endif
    2426             :                 ) {
    2427             :                 /* update sys.functions set mod = 'sql' where mod = 'user'; */
    2428             :                 {
    2429           0 :                         BAT *b1, *b2, *b3;
    2430           0 :                         b1 = log_temp_descriptor(log_find_bat(lg, 2016)); /* sys.functions */
    2431           0 :                         if (b1 == NULL)
    2432           0 :                                 return GDK_FAIL;
    2433             :                         /* undeleted rows of sys.functions */
    2434           0 :                         b2 = BATmaskedcands(0, BATcount(b1), b1, false);
    2435           0 :                         b3 = log_temp_descriptor(log_find_bat(lg, 2020)); /* sys.functions.mod */
    2436           0 :                         bat_destroy(b1);
    2437           0 :                         if (b2 == NULL || b3 == NULL) {
    2438           0 :                                 bat_destroy(b2);
    2439           0 :                                 bat_destroy(b3);
    2440           0 :                                 return GDK_FAIL;
    2441             :                         }
    2442             :                         /* mod = 'user' */
    2443           0 :                         b1 = BATselect(b3, b2, "user", NULL, true, true, false);
    2444           0 :                         bat_destroy(b2);
    2445           0 :                         if (b1 == NULL) {
    2446           0 :                                 bat_destroy(b3);
    2447           0 :                                 return GDK_FAIL;
    2448             :                         }
    2449           0 :                         if (BATcount(b1) > 0) {
    2450           0 :                                 if (BUNfnd(old_lg->add, &b3->batCacheid) == BUN_NONE) {
    2451             :                                         /* replace sys.functions.mod with a copy that we can modify */
    2452           0 :                                         b2 = COLcopy(b3, b3->ttype, true, PERSISTENT);
    2453           0 :                                         if (b2 == NULL) {
    2454           0 :                                                 bat_destroy(b1);
    2455           0 :                                                 bat_destroy(b3);
    2456           0 :                                                 return GDK_FAIL;
    2457             :                                         }
    2458           0 :                                         if (BUNappend(old_lg->del, &b3->batCacheid, false) != GDK_SUCCEED ||
    2459           0 :                                                 BUNappend(old_lg->add, &b2->batCacheid, false) != GDK_SUCCEED ||
    2460             :                                                 /* 2020 is sys.functions.mod */
    2461           0 :                                                 BUNreplace(lg->catalog_bid, BUNfnd(lg->catalog_id, &(int){2020}), &b2->batCacheid, true) != GDK_SUCCEED) {
    2462           0 :                                                 bat_destroy(b1);
    2463           0 :                                                 bat_destroy(b3);
    2464           0 :                                                 bat_destroy(b2);
    2465           0 :                                                 return GDK_FAIL;
    2466             :                                         }
    2467           0 :                                         BBPretain(b2->batCacheid);
    2468           0 :                                         BBPretain(b2->batCacheid);
    2469           0 :                                         bat_destroy(b3);
    2470           0 :                                         b3 = b2;
    2471             :                                 }
    2472           0 :                                 b2 = BATconstant(0, TYPE_str, "sql", BATcount(b1), TRANSIENT);
    2473           0 :                                 if (b2 == NULL) {
    2474           0 :                                         bat_destroy(b1);
    2475           0 :                                         bat_destroy(b3);
    2476           0 :                                         return GDK_FAIL;
    2477             :                                 }
    2478           0 :                                 rc = BATreplace(b3, b1, b2, false);
    2479           0 :                                 bat_destroy(b2);
    2480           0 :                                 bat_destroy(b3);
    2481           0 :                                 if (rc != GDK_SUCCEED) {
    2482           0 :                                         bat_destroy(b1);
    2483           0 :                                         return rc;
    2484             :                                 }
    2485             :                         }
    2486           0 :                         bat_destroy(b1);
    2487             :                 }
    2488             : 
    2489             :                 /* update sys.args set type = 'clob' where type = 'char' and type_digits = 0 and func_id > 2000 */
    2490             :                 {
    2491           0 :                         BAT *b1, *b2, *b3;
    2492           0 :                         b1 = log_temp_descriptor(log_find_bat(lg, 2028)); /* sys.args */
    2493           0 :                         if (b1 == NULL)
    2494           0 :                                 return GDK_FAIL;
    2495           0 :                         b2 = BATmaskedcands(0, BATcount(b1), b1, false);
    2496           0 :                         bat_destroy(b1);
    2497           0 :                         b3 = log_temp_descriptor(log_find_bat(lg, 2030)); /* sys.args.func_id */
    2498           0 :                         if (b2 == NULL || b3 == NULL) {
    2499           0 :                                 bat_destroy(b2);
    2500           0 :                                 bat_destroy(b3);
    2501           0 :                                 return GDK_FAIL;
    2502             :                         }
    2503             :                         /* func_id > 2000 */
    2504           0 :                         b1 = BATselect(b3, b2, &(int){2000}, &int_nil, false, false, false);
    2505           0 :                         bat_destroy(b2);
    2506           0 :                         bat_destroy(b3);
    2507           0 :                         b3 = log_temp_descriptor(log_find_bat(lg, 2033)); /* sys.args.type_digits */
    2508           0 :                         if (b1 == NULL || b3 == NULL) {
    2509           0 :                                 bat_destroy(b1);
    2510           0 :                                 bat_destroy(b3);
    2511           0 :                                 return GDK_FAIL;
    2512             :                         }
    2513             :                         /* and type_digits = 0 */
    2514           0 :                         b2 = BATselect(b3, b1, &(int){0}, NULL, true, false, false);
    2515           0 :                         bat_destroy(b3);
    2516           0 :                         bat_destroy(b1);
    2517           0 :                         b1 = log_temp_descriptor(log_find_bat(lg, 2032)); /* sys.args.type */
    2518           0 :                         if (b1 == NULL || b2 == NULL) {
    2519           0 :                                 bat_destroy(b1);
    2520           0 :                                 bat_destroy(b2);
    2521           0 :                                 return GDK_FAIL;
    2522             :                         }
    2523             :                         /* and type = 'char' */
    2524           0 :                         b3 = BATselect(b1, b2, "char", NULL, true, false, false);
    2525           0 :                         bat_destroy(b2);
    2526           0 :                         if (b3 == NULL) {
    2527           0 :                                 bat_destroy(b1);
    2528           0 :                                 return GDK_FAIL;
    2529             :                         }
    2530           0 :                         if (BATcount(b3) > 0) {
    2531           0 :                                 if (BUNfnd(old_lg->add, &b1->batCacheid) == BUN_NONE) {
    2532             :                                         /* replace sys.args.type with a copy that we can modify */
    2533           0 :                                         b2 = COLcopy(b1, b1->ttype, true, PERSISTENT);
    2534           0 :                                         if (b2 == NULL ||
    2535           0 :                                                 BUNappend(old_lg->del, &b1->batCacheid, false) != GDK_SUCCEED ||
    2536           0 :                                                 BUNappend(old_lg->add, &b2->batCacheid, false) != GDK_SUCCEED ||
    2537             :                                                 /* 2032 is sys.args.type */
    2538           0 :                                                 BUNreplace(lg->catalog_bid, BUNfnd(lg->catalog_id, &(int){2032}), &b2->batCacheid, true) != GDK_SUCCEED) {
    2539           0 :                                                 bat_destroy(b1);
    2540           0 :                                                 bat_destroy(b2);
    2541           0 :                                                 bat_destroy(b3);
    2542           0 :                                                 return GDK_FAIL;
    2543             :                                         }
    2544           0 :                                         BBPretain(b2->batCacheid);
    2545           0 :                                         BBPretain(b2->batCacheid);
    2546           0 :                                         bat_destroy(b1);
    2547           0 :                                         b1 = b2;
    2548             :                                 }
    2549           0 :                                 b2 = BATconstant(0, TYPE_str, "clob", BATcount(b3), TRANSIENT);
    2550           0 :                                 if (b2 == NULL ||
    2551             :                                         /* do the update */
    2552           0 :                                         BATreplace(b1, b3, b2, false) != GDK_SUCCEED) {
    2553           0 :                                         bat_destroy(b1);
    2554           0 :                                         bat_destroy(b2);
    2555           0 :                                         bat_destroy(b3);
    2556           0 :                                         return GDK_FAIL;
    2557             :                                 }
    2558           0 :                                 bat_destroy(b2);
    2559             :                         }
    2560           0 :                         bat_destroy(b1);
    2561           0 :                         bat_destroy(b3);
    2562             :                 }
    2563             : 
    2564             :                 {
    2565             :                         /* drop STREAM TABLEs
    2566             :                          * these tables don't actually have a disk presence, only
    2567             :                          * one in the catalog (so that's why we drop them and don't
    2568             :                          * convert them); we drop them by marking the relevant rows
    2569             :                          * in various catalog tables as deleted */
    2570           0 :                         BAT *dt = log_temp_descriptor(log_find_bat(lg, 2067)); /* sys._tables */
    2571           0 :                         BAT *tt = log_temp_descriptor(log_find_bat(lg, 2072)); /* sys._tables.type */
    2572           0 :                         if (dt == NULL || tt == NULL) {
    2573           0 :                                 bat_destroy(dt);
    2574           0 :                                 bat_destroy(tt);
    2575           0 :                                 return GDK_FAIL;
    2576             :                         }
    2577           0 :                         BAT *cands = BATmaskedcands(0, BATcount(dt), dt, false);
    2578           0 :                         if (cands == NULL) {
    2579           0 :                                 bat_destroy(dt);
    2580           0 :                                 bat_destroy(tt);
    2581           0 :                                 return GDK_FAIL;
    2582             :                         }
    2583           0 :                         BAT *strm = BATselect(tt, cands, &(sht){4}, NULL, true, true, false);
    2584           0 :                         bat_destroy(cands);
    2585           0 :                         bat_destroy(tt);
    2586           0 :                         if (strm == NULL) {
    2587           0 :                                 bat_destroy(dt);
    2588           0 :                                 return GDK_FAIL;
    2589             :                         }
    2590           0 :                         if (strm->batCount > 0) {
    2591           0 :                                 for (BUN p = 0; p < strm->batCount; p++)
    2592           0 :                                         mskSetVal(dt, BUNtoid(strm, p), true);
    2593           0 :                                 bat_destroy(dt);
    2594           0 :                                 BAT *ids = COLnew(0, TYPE_int, 0, TRANSIENT);
    2595           0 :                                 BAT *ti = log_temp_descriptor(log_find_bat(lg, 2068)); /* sys._tables.id */
    2596           0 :                                 if (ids == NULL || ti == NULL) {
    2597           0 :                                         bat_destroy(ids);
    2598           0 :                                         bat_destroy(ti);
    2599           0 :                                         bat_destroy(strm);
    2600           0 :                                         return GDK_FAIL;
    2601             :                                 }
    2602           0 :                                 struct {
    2603             :                                         int id, tabid, dels; /* id, table_id and deleted rows */
    2604           0 :                                 } foreign[10] = {
    2605             :                                         /* the first 7 entries are references to the table id
    2606             :                                          * the first column non-zero means the ids get collected */
    2607             :                                         {   0, 2049, 2047}, /* sys.table_partitions.table_id */
    2608             :                                         {   0, 2054, 2053}, /* sys.range_partitions.table_id */
    2609             :                                         {   0, 2060, 2059}, /* sys.value_partitions.table_id */
    2610             :                                         {2077, 2082, 2076}, /* sys._columns.table_id */
    2611             :                                         {2088, 2089, 2087}, /* sys.keys.table_id */
    2612             :                                         {2095, 2096, 2094}, /* sys.idxs.table_id */
    2613             :                                         {2100, 2102, 2099}, /* sys.triggers.table_id */
    2614             : 
    2615             :                                         /* the remaining 3 are references to collected object ids */
    2616             :                                         {   0, 2111, 2110}, /* sys.objects.id */
    2617             :                                         {   0, 2064, 2063}, /* sys.dependencies.id */
    2618             :                                         {   0, 2065, 2063}, /* sys.dependencies.depend_id */
    2619             :                                 };
    2620             : 
    2621           0 :                                 for (int i = 0; i < 10; i++) {
    2622           0 :                                         if (i == 7) {
    2623             :                                                 /* change gear: we now need to delete the
    2624             :                                                  * collected ids from sys.objects */
    2625           0 :                                                 bat_destroy(ti);
    2626           0 :                                                 ti = ids;
    2627           0 :                                                 ids = NULL;
    2628           0 :                                                 bat_destroy(strm);
    2629           0 :                                                 strm = NULL;
    2630             :                                         }
    2631           0 :                                         BAT *ct = log_temp_descriptor(log_find_bat(lg, foreign[i].tabid));
    2632           0 :                                         BAT *dc = log_temp_descriptor(log_find_bat(lg, foreign[i].dels));
    2633           0 :                                         if (ct == NULL || dc == NULL) {
    2634           0 :                                                 bat_destroy(ids);
    2635           0 :                                                 bat_destroy(strm);
    2636           0 :                                                 bat_destroy(ti);
    2637           0 :                                                 bat_destroy(ct);
    2638           0 :                                                 bat_destroy(dc);
    2639           0 :                                                 return GDK_FAIL;
    2640             :                                         }
    2641           0 :                                         cands = BATmaskedcands(0, BATcount(dc), dc, false);
    2642           0 :                                         if (cands == NULL) {
    2643           0 :                                                 bat_destroy(ids);
    2644           0 :                                                 bat_destroy(strm);
    2645           0 :                                                 bat_destroy(ti);
    2646           0 :                                                 bat_destroy(ct);
    2647           0 :                                                 bat_destroy(dc);
    2648           0 :                                                 return GDK_FAIL;
    2649             :                                         }
    2650           0 :                                         BAT *strc = BATintersect(ct, ti, cands, strm, false, false, BUN_NONE);
    2651           0 :                                         bat_destroy(cands);
    2652           0 :                                         if (strc == NULL) {
    2653           0 :                                                 bat_destroy(ids);
    2654           0 :                                                 bat_destroy(strm);
    2655           0 :                                                 bat_destroy(ti);
    2656           0 :                                                 bat_destroy(ct);
    2657           0 :                                                 bat_destroy(dc);
    2658           0 :                                                 return GDK_FAIL;
    2659             :                                         }
    2660           0 :                                         for (BUN p = 0; p < strc->batCount; p++)
    2661           0 :                                                 mskSetVal(dc, BUNtoid(strc, p), true);
    2662           0 :                                         if (foreign[i].id != 0) {
    2663           0 :                                                 BAT *ci = log_temp_descriptor(log_find_bat(lg, foreign[i].id));
    2664           0 :                                                 if (ci == NULL) {
    2665           0 :                                                         bat_destroy(ids);
    2666           0 :                                                         bat_destroy(strc);
    2667           0 :                                                         bat_destroy(ct);
    2668           0 :                                                         bat_destroy(dc);
    2669           0 :                                                         bat_destroy(strm);
    2670           0 :                                                         bat_destroy(ti);
    2671           0 :                                                         return GDK_FAIL;
    2672             :                                                 }
    2673           0 :                                                 if (BATappend(ids, ci, strc, false) != GDK_SUCCEED) {
    2674           0 :                                                         bat_destroy(ids);
    2675           0 :                                                         bat_destroy(strc);
    2676           0 :                                                         bat_destroy(ct);
    2677           0 :                                                         bat_destroy(dc);
    2678           0 :                                                         bat_destroy(strm);
    2679           0 :                                                         bat_destroy(ti);
    2680           0 :                                                         bat_destroy(ci);
    2681           0 :                                                         return GDK_FAIL;
    2682             :                                                 }
    2683           0 :                                                 bat_destroy(ci);
    2684             :                                         }
    2685           0 :                                         bat_destroy(strc);
    2686           0 :                                         bat_destroy(ct);
    2687           0 :                                         bat_destroy(dc);
    2688             :                                 }
    2689           0 :                                 bat_destroy(ti);
    2690             :                         } else {
    2691           0 :                                 bat_destroy(dt);
    2692           0 :                                 bat_destroy(strm);
    2693             :                         }
    2694             :                 }
    2695             :         }
    2696             : #endif
    2697             : 
    2698             : #ifdef CATALOG_JUL2021
    2699           0 :         if (store->catalog_version <= CATALOG_JUL2021) {
    2700             :                 /* change the language attribute in sys.functions for sys.env,
    2701             :                  * sys.var, and sys.db_users from SQL to MAL */
    2702             : 
    2703             :                 /* sys.functions i.e. deleted rows */
    2704           0 :                 BAT *del_funcs = log_temp_descriptor(log_find_bat(lg, 2016));
    2705           0 :                 if (del_funcs == NULL)
    2706           0 :                         return GDK_FAIL;
    2707           0 :                 BAT *func_tid = BATmaskedcands(0, BATcount(del_funcs), del_funcs, false);
    2708           0 :                 bat_destroy(del_funcs);
    2709             :                 /* sys.functions.schema_id */
    2710           0 :                 BAT *func_schem = log_temp_descriptor(log_find_bat(lg, 2026));
    2711           0 :                 if (func_tid == NULL || func_schem == NULL) {
    2712           0 :                         bat_destroy(func_tid);
    2713           0 :                         bat_destroy(func_schem);
    2714           0 :                         return GDK_FAIL;
    2715             :                 }
    2716             :                 /* select * from sys.functions where schema_id = 2000 */
    2717           0 :                 BAT *cands = BATselect(func_schem, func_tid, &(int) {2000}, NULL, true, true, false);
    2718           0 :                 bat_destroy(func_schem);
    2719           0 :                 if (cands == NULL) {
    2720           0 :                         bat_destroy(func_tid);
    2721           0 :                         return GDK_FAIL;
    2722             :                 }
    2723             :                 /* the functions we need to change */
    2724           0 :                 BAT *funcs = COLnew(0, TYPE_str, 3, TRANSIENT);
    2725           0 :                 if (funcs == NULL ||
    2726           0 :                         BUNappend(funcs, "db_users", false) != GDK_SUCCEED ||
    2727           0 :                         BUNappend(funcs, "env", false) != GDK_SUCCEED ||
    2728           0 :                         BUNappend(funcs, "var", false) != GDK_SUCCEED) {
    2729           0 :                         bat_destroy(cands);
    2730           0 :                         bat_destroy(funcs);
    2731           0 :                         bat_destroy(func_tid);
    2732           0 :                         return GDK_FAIL;
    2733             :                 }
    2734             :                 /* sys.functions.name */
    2735           0 :                 BAT *func_name = log_temp_descriptor(log_find_bat(lg, 2018));
    2736           0 :                 if (func_name == NULL) {
    2737           0 :                         bat_destroy(cands);
    2738           0 :                         bat_destroy(funcs);
    2739           0 :                         bat_destroy(func_tid);
    2740           0 :                         return GDK_FAIL;
    2741             :                 }
    2742             :                 /* select * from sys.functions where schema_id = 2000 and name in (...) */
    2743           0 :                 BAT *b = BATintersect(func_name, funcs, cands, NULL, false, false, 3);
    2744           0 :                 bat_destroy(cands);
    2745           0 :                 bat_destroy(func_name);
    2746           0 :                 bat_destroy(funcs);
    2747           0 :                 cands = b;
    2748           0 :                 if (cands == NULL) {
    2749           0 :                         bat_destroy(func_tid);
    2750           0 :                         return GDK_FAIL;
    2751             :                 }
    2752             :                 /* sys.functions.language */
    2753           0 :                 BAT *func_lang = log_temp_descriptor(log_find_bat(lg, 2021));
    2754           0 :                 if (func_lang == NULL) {
    2755           0 :                         bat_destroy(cands);
    2756           0 :                         bat_destroy(func_tid);
    2757           0 :                         return GDK_FAIL;
    2758             :                 }
    2759             :                 /* select * from sys.functions where schema_id = 2000 and name in (...)
    2760             :                  * and language = FUNC_LANG_SQL */
    2761           0 :                 b = BATselect(func_lang, cands, &(int) {FUNC_LANG_SQL}, NULL, true, true, false);
    2762           0 :                 bat_destroy(cands);
    2763           0 :                 cands = b;
    2764           0 :                 if (cands == NULL) {
    2765           0 :                         bat_destroy(func_lang);
    2766           0 :                         bat_destroy(func_tid);
    2767           0 :                         return GDK_FAIL;
    2768             :                 }
    2769           0 :                 b = BATconstant(0, TYPE_int, &(int) {FUNC_LANG_MAL}, BATcount(cands), TRANSIENT);
    2770           0 :                 if (b == NULL) {
    2771           0 :                         bat_destroy(func_lang);
    2772           0 :                         bat_destroy(cands);
    2773           0 :                         bat_destroy(func_tid);
    2774           0 :                         return GDK_FAIL;
    2775             :                 }
    2776           0 :                 rc = GDK_FAIL;
    2777           0 :                 BAT *b2 = COLcopy(func_lang, func_lang->ttype, true, PERSISTENT);
    2778           0 :                 bat bid = func_lang->batCacheid;
    2779           0 :                 if (b2 == NULL ||
    2780           0 :                         BATreplace(b2, cands, b, false) != GDK_SUCCEED) {
    2781           0 :                         bat_destroy(b2);
    2782           0 :                         bat_destroy(cands);
    2783           0 :                         bat_destroy(b);
    2784           0 :                         bat_destroy(func_tid);
    2785           0 :                         bat_destroy(func_lang);
    2786           0 :                         return GDK_FAIL;
    2787             :                 }
    2788           0 :                 bat_destroy(b);
    2789           0 :                 bat_destroy(cands);
    2790             : 
    2791             :                 /* additionally, update the language attribute for entries
    2792             :                  * that were declared using "EXTERNAL NAME" to be MAL functions
    2793             :                  * instead of SQL functions (a problem that seems to have
    2794             :                  * occurred in ancient databases) */
    2795             : 
    2796             :                 /* sys.functions.func */
    2797           0 :                 BAT *func_func = log_temp_descriptor(log_find_bat(lg, 2019));
    2798           0 :                 if (func_func == NULL) {
    2799           0 :                         bat_destroy(func_tid);
    2800           0 :                         bat_destroy(b2);
    2801           0 :                         return GDK_FAIL;
    2802             :                 }
    2803           0 :                 cands = BATselect(func_lang, func_tid, &(int){FUNC_LANG_SQL}, NULL, true, true, false);
    2804           0 :                 bat_destroy(func_lang);
    2805           0 :                 bat_destroy(func_tid);
    2806           0 :                 if (cands == NULL) {
    2807           0 :                         bat_destroy(b2);
    2808           0 :                         bat_destroy(func_func);
    2809           0 :                         return GDK_FAIL;
    2810             :                 }
    2811           0 :                 struct canditer ci;
    2812           0 :                 canditer_init(&ci, func_func, cands);
    2813           0 :                 BATiter ffi = bat_iterator_nolock(func_func);
    2814           0 :                 for (BUN p = 0; p < ci.ncand; p++) {
    2815           0 :                         oid o = canditer_next(&ci);
    2816           0 :                         const char *f = BUNtvar(ffi, o - func_func->hseqbase);
    2817           0 :                         const char *e;
    2818           0 :                         if (!strNil(f) &&
    2819           0 :                                 (e = strstr(f, "external")) != NULL &&
    2820           0 :                                 e > f && isspace((unsigned char) e[-1]) && isspace((unsigned char) e[8]) && strncmp(e + 9, "name", 4) == 0 && isspace((unsigned char) e[13]) &&
    2821           0 :                                 BUNreplace(b2, o, &(int){FUNC_LANG_MAL}, false) != GDK_SUCCEED) {
    2822           0 :                                 bat_destroy(b2);
    2823           0 :                                 bat_destroy(func_func);
    2824           0 :                                 return GDK_FAIL;
    2825             :                         }
    2826             :                 }
    2827           0 :                 rc = replace_bat(old_lg, lg, 2021, bid, b2);
    2828           0 :                 bat_destroy(b2);
    2829           0 :                 if (rc != GDK_SUCCEED)
    2830             :                         return rc;
    2831             :         }
    2832           0 :         if (store->catalog_version <= CATALOG_JUL2021) {
    2833             :                 /* change the side_effects attribute in sys.functions for
    2834             :                  * selected functions */
    2835             : 
    2836             :                 /* sys.functions i.e. deleted rows */
    2837           0 :                 BAT *del_funcs = log_temp_descriptor(log_find_bat(lg, 2016));
    2838           0 :                 if (del_funcs == NULL)
    2839           0 :                         return GDK_FAIL;
    2840           0 :                 BAT *func_tid = BATmaskedcands(0, BATcount(del_funcs), del_funcs, false);
    2841           0 :                 bat_destroy(del_funcs);
    2842             :                 /* sys.functions.schema_id */
    2843           0 :                 BAT *func_schem = log_temp_descriptor(log_find_bat(lg, 2026));
    2844           0 :                 if (func_tid == NULL || func_schem == NULL) {
    2845           0 :                         bat_destroy(func_tid);
    2846           0 :                         bat_destroy(func_schem);
    2847           0 :                         return GDK_FAIL;
    2848             :                 }
    2849             :                 /* select * from sys.functions where schema_id = 2000 */
    2850           0 :                 BAT *cands = BATselect(func_schem, func_tid, &(int) {2000}, NULL, true, true, false);
    2851           0 :                 bat_destroy(func_schem);
    2852           0 :                 bat_destroy(func_tid);
    2853           0 :                 if (cands == NULL) {
    2854             :                         return GDK_FAIL;
    2855             :                 }
    2856             :                 /* sys.functions.side_effect */
    2857           0 :                 BAT *func_se = log_temp_descriptor(log_find_bat(lg, 2023));
    2858           0 :                 if (func_se == NULL) {
    2859           0 :                         bat_destroy(cands);
    2860           0 :                         return GDK_FAIL;
    2861             :                 }
    2862           0 :                 bat bid = func_se->batCacheid;
    2863             :                 /* make a copy that we can modify */
    2864           0 :                 BAT *b = COLcopy(func_se, func_se->ttype, true, PERSISTENT);
    2865           0 :                 bat_destroy(func_se);
    2866           0 :                 if (b == NULL) {
    2867           0 :                         bat_destroy(cands);
    2868           0 :                         return GDK_FAIL;
    2869             :                 }
    2870           0 :                 func_se = b;
    2871             :                 /* sys.functions.func */
    2872           0 :                 BAT *func_func = log_temp_descriptor(log_find_bat(lg, 2019));
    2873           0 :                 if (func_func == NULL) {
    2874           0 :                         bat_destroy(cands);
    2875           0 :                         bat_destroy(func_se);
    2876           0 :                         return GDK_FAIL;
    2877             :                 }
    2878             :                 /* the functions we need to change to FALSE */
    2879           0 :                 BAT *funcs = COLnew(0, TYPE_str, 1, TRANSIENT);
    2880           0 :                 if (funcs == NULL ||
    2881           0 :                         BUNappend(funcs, "sqlrand", false) != GDK_SUCCEED) {
    2882           0 :                         bat_destroy(cands);
    2883           0 :                         bat_destroy(func_se);
    2884           0 :                         bat_destroy(func_func);
    2885           0 :                         bat_destroy(funcs);
    2886           0 :                         return GDK_FAIL;
    2887             :                 }
    2888             :                 /* select * from sys.functions where schema_id = 2000 and func in (...) */
    2889           0 :                 b = BATintersect(func_func, funcs, cands, NULL, false, false, 1);
    2890           0 :                 bat_destroy(funcs);
    2891           0 :                 if (b == NULL) {
    2892           0 :                         bat_destroy(cands);
    2893           0 :                         bat_destroy(func_se);
    2894           0 :                         bat_destroy(func_func);
    2895           0 :                         return GDK_FAIL;
    2896             :                 }
    2897             :                 /* while we're at it, also change sys.env and sys.db_users to
    2898             :                  * being without side effect (legacy from ancient databases) */
    2899             :                 /* sys.functions.name */
    2900           0 :                 BAT *func_name = log_temp_descriptor(log_find_bat(lg, 2018));
    2901           0 :                 if (func_name == NULL) {
    2902           0 :                         bat_destroy(cands);
    2903           0 :                         bat_destroy(func_se);
    2904           0 :                         bat_destroy(func_func);
    2905           0 :                         bat_destroy(b);
    2906           0 :                         return GDK_FAIL;
    2907             :                 }
    2908           0 :                 BAT *b2 = BATselect(func_name, cands, "env", NULL, true, true, false);
    2909           0 :                 if (b2 == NULL || BATappend(b, b2, NULL, false) != GDK_SUCCEED) {
    2910           0 :                         bat_destroy(cands);
    2911           0 :                         bat_destroy(func_se);
    2912           0 :                         bat_destroy(func_func);
    2913           0 :                         bat_destroy(b);
    2914           0 :                         bat_destroy(func_name);
    2915           0 :                         bat_destroy(b2);
    2916           0 :                         return GDK_FAIL;
    2917             :                 }
    2918           0 :                 bat_destroy(b2);
    2919           0 :                 b2 = BATselect(func_name, cands, "db_users", NULL, true, true, false);
    2920           0 :                 bat_destroy(func_name);
    2921           0 :                 if (b2 == NULL || BATappend(b, b2, NULL, false) != GDK_SUCCEED) {
    2922           0 :                         bat_destroy(cands);
    2923           0 :                         bat_destroy(func_se);
    2924           0 :                         bat_destroy(func_func);
    2925           0 :                         bat_destroy(b);
    2926           0 :                         bat_destroy(b2);
    2927           0 :                         return GDK_FAIL;
    2928             :                 }
    2929           0 :                 bat_destroy(b2);
    2930             : 
    2931           0 :                 BAT *vals = BATconstant(0, TYPE_bit, &(bit) {FALSE}, BATcount(b), TRANSIENT);
    2932           0 :                 if (vals == NULL) {
    2933           0 :                         bat_destroy(cands);
    2934           0 :                         bat_destroy(func_se);
    2935           0 :                         bat_destroy(func_func);
    2936           0 :                         bat_destroy(b);
    2937           0 :                         return GDK_FAIL;
    2938             :                 }
    2939           0 :                 rc = BATreplace(func_se, b, vals, false);
    2940           0 :                 bat_destroy(b);
    2941           0 :                 bat_destroy(vals);
    2942           0 :                 if (rc != GDK_SUCCEED) {
    2943           0 :                         bat_destroy(cands);
    2944           0 :                         bat_destroy(func_se);
    2945           0 :                         bat_destroy(func_func);
    2946           0 :                         return GDK_FAIL;
    2947             :                 }
    2948             :                 /* the functions we need to change to TRUE */
    2949           0 :                 funcs = COLnew(0, TYPE_str, 5, TRANSIENT);
    2950           0 :                 if (funcs == NULL ||
    2951           0 :                         BUNappend(funcs, "copy_from", false) != GDK_SUCCEED ||
    2952           0 :                         BUNappend(funcs, "next_value", false) != GDK_SUCCEED ||
    2953           0 :                         BUNappend(funcs, "update_schemas", false) != GDK_SUCCEED ||
    2954           0 :                         BUNappend(funcs, "update_tables", false) != GDK_SUCCEED) {
    2955           0 :                         bat_destroy(cands);
    2956           0 :                         bat_destroy(func_se);
    2957           0 :                         bat_destroy(func_func);
    2958           0 :                         bat_destroy(funcs);
    2959           0 :                         return GDK_FAIL;
    2960             :                 }
    2961             :                 /* select * from sys.functions where schema_id = 2000 and func in (...) */
    2962           0 :                 b = BATintersect(func_func, funcs, cands, NULL, false, false, 7);
    2963           0 :                 bat_destroy(funcs);
    2964           0 :                 bat_destroy(cands);
    2965           0 :                 bat_destroy(func_func);
    2966           0 :                 if (b == NULL) {
    2967           0 :                         bat_destroy(func_se);
    2968           0 :                         return GDK_FAIL;
    2969             :                 }
    2970           0 :                 vals = BATconstant(0, TYPE_bit, &(bit) {TRUE}, BATcount(b), TRANSIENT);
    2971           0 :                 if (vals == NULL) {
    2972           0 :                         bat_destroy(func_se);
    2973           0 :                         bat_destroy(b);
    2974           0 :                         return GDK_FAIL;
    2975             :                 }
    2976           0 :                 rc = BATreplace(func_se, b, vals, false);
    2977           0 :                 bat_destroy(b);
    2978           0 :                 bat_destroy(vals);
    2979           0 :                 if (rc != GDK_SUCCEED) {
    2980           0 :                         bat_destroy(func_se);
    2981           0 :                         return GDK_FAIL;
    2982             :                 }
    2983             :                 /* replace old column with modified copy */
    2984           0 :                 rc = replace_bat(old_lg, lg, 2023, bid, func_se);
    2985           0 :                 bat_destroy(func_se);
    2986           0 :                 if (rc != GDK_SUCCEED)
    2987             :                         return rc;
    2988             :         }
    2989           0 :         if (store->catalog_version <= CATALOG_JUL2021) {
    2990             :                 /* upgrade some columns in sys.sequences:
    2991             :                  * if increment is zero, set it to one (see ChangeLog);
    2992             :                  * if increment is greater than zero and maxvalue is zero,
    2993             :                  * set maxvalue to GDK_lng_max;
    2994             :                  * if increment is less than zero and minvalue is zero,
    2995             :                  * set minvalue to GDK_lng_min */
    2996             : 
    2997             :                 /* sys.sequences i.e. deleted rows */
    2998           0 :                 BAT *del_seqs = log_temp_descriptor(log_find_bat(lg, 2037));
    2999           0 :                 if (del_seqs == NULL)
    3000           0 :                         return GDK_FAIL;
    3001           0 :                 BAT *seq_tid = BATmaskedcands(0, BATcount(del_seqs), del_seqs, false);
    3002           0 :                 bat_destroy(del_seqs);
    3003           0 :                 BAT *seq_min = log_temp_descriptor(log_find_bat(lg, 2042)); /* sys.sequences.minvalue */
    3004           0 :                 BAT *seq_max = log_temp_descriptor(log_find_bat(lg, 2043)); /* sys.sequences.maxvalue */
    3005           0 :                 BAT *seq_inc = log_temp_descriptor(log_find_bat(lg, 2044)); /* sys.sequences.increment */
    3006           0 :                 if (seq_tid == NULL || seq_min == NULL || seq_max == NULL || seq_inc == NULL) {
    3007           0 :                         bat_destroy(seq_tid);
    3008           0 :                         bat_destroy(seq_min);
    3009           0 :                         bat_destroy(seq_max);
    3010           0 :                         bat_destroy(seq_inc);
    3011           0 :                         return GDK_FAIL;
    3012             :                 }
    3013             :                 /* select * from sys.sequences where increment = 0 */
    3014           0 :                 BAT *inczero = BATselect(seq_inc, seq_tid, &(lng){0}, NULL, false, true, false);
    3015           0 :                 if (inczero == NULL) {
    3016           0 :                         bat_destroy(seq_tid);
    3017           0 :                         bat_destroy(seq_min);
    3018           0 :                         bat_destroy(seq_max);
    3019           0 :                         bat_destroy(seq_inc);
    3020           0 :                         return GDK_FAIL;
    3021             :                 }
    3022           0 :                 if (BATcount(inczero) > 0) {
    3023           0 :                         BAT *b = BATconstant(0, TYPE_lng, &(lng) {1}, BATcount(inczero), TRANSIENT);
    3024           0 :                         if (b == NULL) {
    3025           0 :                                 bat_destroy(seq_tid);
    3026           0 :                                 bat_destroy(seq_min);
    3027           0 :                                 bat_destroy(seq_max);
    3028           0 :                                 bat_destroy(seq_inc);
    3029           0 :                                 bat_destroy(inczero);
    3030           0 :                                 return GDK_FAIL;
    3031             :                         }
    3032           0 :                         BAT *b2 = COLcopy(seq_inc, seq_inc->ttype, true, PERSISTENT);
    3033           0 :                         rc = GDK_FAIL;
    3034           0 :                         if (b2 == NULL)
    3035           0 :                                 rc = BATreplace(b2, inczero, b, false);
    3036           0 :                         bat_destroy(b);
    3037           0 :                         if (rc != GDK_SUCCEED) {
    3038           0 :                                 bat_destroy(b2);
    3039           0 :                                 bat_destroy(seq_tid);
    3040           0 :                                 bat_destroy(seq_min);
    3041           0 :                                 bat_destroy(seq_max);
    3042           0 :                                 bat_destroy(seq_inc);
    3043           0 :                                 bat_destroy(inczero);
    3044           0 :                                 return GDK_FAIL;
    3045             :                         }
    3046           0 :                         rc = replace_bat(old_lg, lg, 2044, seq_inc->batCacheid, b2);
    3047           0 :                         bat_destroy(seq_inc);
    3048           0 :                         seq_inc = b2;
    3049           0 :                         if (rc != GDK_SUCCEED) {
    3050           0 :                                 bat_destroy(seq_tid);
    3051           0 :                                 bat_destroy(seq_min);
    3052           0 :                                 bat_destroy(seq_max);
    3053           0 :                                 bat_destroy(seq_inc);
    3054           0 :                                 bat_destroy(inczero);
    3055           0 :                                 return rc;
    3056             :                         }
    3057             :                 }
    3058           0 :                 bat_destroy(inczero);
    3059             :                 /* select * from sys.sequences where increment > 0 */
    3060           0 :                 BAT *incpos = BATselect(seq_inc, seq_tid, &(lng){0}, &lng_nil, false, true, false);
    3061           0 :                 bat_destroy(seq_inc);
    3062           0 :                 if (incpos == NULL) {
    3063           0 :                         bat_destroy(seq_tid);
    3064           0 :                         bat_destroy(seq_min);
    3065           0 :                         bat_destroy(seq_max);
    3066           0 :                         return GDK_FAIL;
    3067             :                 }
    3068             :                 /* select * from sys.sequences where increment > 0 and maxvalue = 0 */
    3069           0 :                 BAT *cands = BATselect(seq_max, incpos, &(lng) {0}, NULL, true, true, false);
    3070           0 :                 bat_destroy(incpos);
    3071           0 :                 if (cands == NULL) {
    3072           0 :                         bat_destroy(seq_tid);
    3073           0 :                         bat_destroy(seq_min);
    3074           0 :                         bat_destroy(seq_max);
    3075           0 :                         return GDK_FAIL;
    3076             :                 }
    3077           0 :                 if (BATcount(cands) > 0) {
    3078           0 :                         BAT *b = BATconstant(0, TYPE_lng, &(lng){GDK_lng_max}, BATcount(cands), TRANSIENT);
    3079           0 :                         BAT *b2 = COLcopy(seq_max, seq_max->ttype, true, PERSISTENT);
    3080           0 :                         rc = GDK_FAIL;
    3081           0 :                         if (b != NULL && b2 != NULL)
    3082           0 :                                 rc = BATreplace(b2, cands, b, false);
    3083           0 :                         bat_destroy(b);
    3084           0 :                         if (rc == GDK_SUCCEED)
    3085           0 :                                 rc = replace_bat(old_lg, lg, 2043, seq_max->batCacheid, b2);
    3086           0 :                         bat_destroy(b2);
    3087           0 :                         if (rc != GDK_SUCCEED) {
    3088           0 :                                 bat_destroy(cands);
    3089           0 :                                 bat_destroy(seq_tid);
    3090           0 :                                 bat_destroy(seq_min);
    3091           0 :                                 bat_destroy(seq_max);
    3092           0 :                                 return rc;
    3093             :                         }
    3094             :                 }
    3095           0 :                 bat_destroy(seq_max);
    3096           0 :                 bat_destroy(cands);
    3097             :                 /* select * from sys.sequences where increment < 0 */
    3098           0 :                 BAT *incneg = BATselect(seq_inc, seq_tid, &lng_nil, &(lng){0}, false, true, false);
    3099           0 :                 bat_destroy(seq_tid);
    3100             :                 /* select * from sys.sequences where increment < 0 and minvalue = 0 */
    3101           0 :                 cands = BATselect(seq_min, incneg, &(lng) {0}, NULL, true, true, false);
    3102           0 :                 bat_destroy(incneg);
    3103           0 :                 if (cands == NULL) {
    3104           0 :                         bat_destroy(seq_min);
    3105           0 :                         return GDK_FAIL;
    3106             :                 }
    3107           0 :                 if (BATcount(cands) > 0) {
    3108           0 :                         BAT *b = BATconstant(0, TYPE_lng, &(lng){GDK_lng_min}, BATcount(cands), TRANSIENT);
    3109           0 :                         BAT *b2 = COLcopy(seq_min, seq_min->ttype, true, PERSISTENT);
    3110           0 :                         rc = GDK_FAIL;
    3111           0 :                         if (b != NULL && b2 != NULL)
    3112           0 :                                 rc = BATreplace(b2, cands, b, false);
    3113           0 :                         bat_destroy(b);
    3114           0 :                         if (rc == GDK_SUCCEED)
    3115           0 :                                 rc = replace_bat(old_lg, lg, 2042, seq_min->batCacheid, b2);
    3116           0 :                         bat_destroy(b2);
    3117           0 :                         if (rc != GDK_SUCCEED) {
    3118           0 :                                 bat_destroy(cands);
    3119           0 :                                 bat_destroy(seq_min);
    3120           0 :                                 return rc;
    3121             :                         }
    3122             :                 }
    3123           0 :                 bat_destroy(seq_min);
    3124           0 :                 bat_destroy(cands);
    3125             :         }
    3126             : #endif
    3127             : 
    3128             : #ifdef CATALOG_JAN2022
    3129           0 :         if (store->catalog_version <= CATALOG_JAN2022) {
    3130             :                 /* GRANT SELECT ON sys.db_user_info TO monetdb;
    3131             :                  * except the grantor is 0 instead of user monetdb
    3132             :                  *
    3133             :                  * we need to find the IDs of the sys.db_user_info table and of
    3134             :                  * the sys.privileges table and its columns since none of these
    3135             :                  * have fixed IDs */
    3136           0 :                 BAT *b = log_temp_descriptor(log_find_bat(lg, 2067)); /* sys._tables */
    3137           0 :                 if (b == NULL)
    3138           0 :                         return GDK_FAIL;
    3139           0 :                 BAT *del_tabs = BATmaskedcands(0, BATcount(b), b, false);
    3140           0 :                 bat_destroy(b);
    3141           0 :                 if (del_tabs == NULL)
    3142             :                         return GDK_FAIL;
    3143           0 :                 b = log_temp_descriptor(log_find_bat(lg, 2076)); /* sys._columns */
    3144           0 :                 if (b == NULL) {
    3145           0 :                         bat_destroy(del_tabs);
    3146           0 :                         return GDK_FAIL;
    3147             :                 }
    3148           0 :                 BAT *del_cols = BATmaskedcands(0, BATcount(b), b, false);
    3149           0 :                 bat_destroy(b);
    3150           0 :                 b = log_temp_descriptor(log_find_bat(lg, 2070)); /* sys._tables.schema_id */
    3151           0 :                 if (del_cols == NULL || b == NULL) {
    3152           0 :                         bat_destroy(del_cols);
    3153           0 :                         bat_destroy(b);
    3154           0 :                         bat_destroy(del_tabs);
    3155           0 :                         return GDK_FAIL;
    3156             :                 }
    3157           0 :                 BAT *cands = BATselect(b, del_tabs, &(int) {2000}, NULL, true, true, false);
    3158           0 :                 bat_destroy(b);
    3159           0 :                 bat_destroy(del_tabs);
    3160             :                 /* cands contains undeleted rows from sys._tables for tables in
    3161             :                  * sys schema */
    3162           0 :                 BAT *tabnme = log_temp_descriptor(log_find_bat(lg, 2069)); /* sys._tables.name */
    3163           0 :                 if (cands == NULL || tabnme == NULL) {
    3164           0 :                         bat_destroy(cands);
    3165           0 :                         bat_destroy(tabnme);
    3166           0 :                         bat_destroy(del_cols);
    3167           0 :                         return GDK_FAIL;
    3168             :                 }
    3169           0 :                 b = BATselect(tabnme, cands, "db_user_info", NULL, true, true, false);
    3170           0 :                 if (b == NULL) {
    3171           0 :                         bat_destroy(cands);
    3172           0 :                         bat_destroy(tabnme);
    3173           0 :                         bat_destroy(del_cols);
    3174           0 :                         return GDK_FAIL;
    3175             :                 }
    3176           0 :                 oid dbpos = BUNtoid(b, 0);
    3177           0 :                 bat_destroy(b);
    3178           0 :                 b = BATselect(tabnme, cands, "privileges", NULL, true, true, false);
    3179           0 :                 bat_destroy(tabnme);
    3180           0 :                 bat_destroy(cands);
    3181           0 :                 BAT *tabid = log_temp_descriptor(log_find_bat(lg, 2068)); /* sys._tables.id */
    3182           0 :                 if (b == NULL || tabid == NULL) {
    3183           0 :                         bat_destroy(b);
    3184           0 :                         bat_destroy(tabid);
    3185           0 :                         bat_destroy(del_cols);
    3186           0 :                         return GDK_FAIL;
    3187             :                 }
    3188           0 :                 int dbid = ((int *) tabid->theap->base)[dbpos];
    3189           0 :                 int prid = ((int *) tabid->theap->base)[BUNtoid(b, 0)];
    3190           0 :                 BAT *coltid = log_temp_descriptor(log_find_bat(lg, 2082)); /* sys._columns.table_id */
    3191           0 :                 if (coltid == NULL) {
    3192           0 :                         bat_destroy(b);
    3193           0 :                         bat_destroy(del_cols);
    3194           0 :                         bat_destroy(tabid);
    3195           0 :                         return GDK_FAIL;
    3196             :                 }
    3197           0 :                 BAT *b1;
    3198           0 :                 rc = BATjoin(&b1, NULL, coltid, tabid, del_cols, b, false, 5);
    3199           0 :                 bat_destroy(coltid);
    3200           0 :                 bat_destroy(tabid);
    3201           0 :                 bat_destroy(del_cols);
    3202           0 :                 bat_destroy(b);
    3203           0 :                 BAT *colnr = log_temp_descriptor(log_find_bat(lg, 2085)); /* sys._columns.number */
    3204           0 :                 BAT *colid = log_temp_descriptor(log_find_bat(lg, 2077)); /* sys._columns.id */
    3205           0 :                 if (rc != GDK_SUCCEED || colnr == NULL || colid == NULL) {
    3206           0 :                         if (rc == GDK_SUCCEED)
    3207           0 :                                 bat_destroy(b1);
    3208           0 :                         bat_destroy(colnr);
    3209           0 :                         bat_destroy(colid);
    3210           0 :                         return GDK_FAIL;
    3211             :                 }
    3212             :                 int privids[5];
    3213           0 :                 for (int i = 0; i < 5; i++) {
    3214           0 :                         oid p = BUNtoid(b1, i);
    3215           0 :                         privids[((int *) colnr->theap->base)[p]] = ((int *) colid->theap->base)[p];
    3216             :                 }
    3217           0 :                 bat_destroy(b1);
    3218           0 :                 bat_destroy(colnr);
    3219           0 :                 bat_destroy(colid);
    3220           0 :                 rc = tabins(lg, old_lg, true, -1, 0,
    3221           0 :                                         prid, &(msk) {false}, /* sys.privileges */
    3222             :                                         privids[0], &dbid, /* sys.privileges.obj_id */
    3223           0 :                                         privids[1], &(int) {USER_MONETDB}, /* sys.privileges.auth_id */
    3224           0 :                                         privids[2], &(int) {PRIV_SELECT}, /* sys.privileges.privileges */
    3225           0 :                                         privids[3], &(int) {0}, /* sys.privileges.grantor */
    3226           0 :                                         privids[4], &(int) {0}, /* sys.privileges.grantee */
    3227             :                                         0);
    3228           0 :                 if (rc != GDK_SUCCEED)
    3229             :                         return rc;
    3230             :         }
    3231             : #endif
    3232             : 
    3233             :         return GDK_SUCCEED;
    3234             : }
    3235             : 
    3236             : static int
    3237         336 : bl_create(sqlstore *store, int debug, const char *logdir, int cat_version)
    3238             : {
    3239         336 :         if (store->logger)
    3240             :                 return LOG_ERR;
    3241         336 :         store->logger = log_create(debug, "sql", logdir, cat_version, (preversionfix_fptr)&bl_preversion, (postversionfix_fptr)&bl_postversion, store);
    3242         336 :         if (store->logger)
    3243             :                 return LOG_OK;
    3244             :         return LOG_ERR;
    3245             : }
    3246             : 
    3247             : static void
    3248         335 : bl_destroy(sqlstore *store)
    3249             : {
    3250         335 :         logger *l = store->logger;
    3251             : 
    3252         335 :         store->logger = NULL;
    3253         335 :         if (l)
    3254         335 :                 log_destroy(l);
    3255         335 : }
    3256             : 
    3257             : static int
    3258        2553 : bl_flush(sqlstore *store, lng save_id)
    3259             : {
    3260        2553 :         if (store->logger)
    3261        2553 :                 return log_flush(store->logger, save_id) == GDK_SUCCEED ? LOG_OK : LOG_ERR;
    3262             :         return LOG_OK;
    3263             : }
    3264             : 
    3265             : static int
    3266       10991 : bl_activate(sqlstore *store)
    3267             : {
    3268       10991 :         if (store->logger)
    3269       10991 :                 return log_activate(store->logger) == GDK_SUCCEED ? LOG_OK : LOG_ERR;
    3270             :         return LOG_OK;
    3271             : }
    3272             : 
    3273             : static int
    3274       14873 : bl_changes(sqlstore *store)
    3275             : {
    3276       14873 :         return (int) MIN(log_changes(store->logger), GDK_int_max);
    3277             : }
    3278             : 
    3279             : static int
    3280         463 : bl_get_sequence(sqlstore *store, int seq, lng *id)
    3281             : {
    3282         463 :         return log_sequence(store->logger, seq, id);
    3283             : }
    3284             : 
    3285             : static int
    3286         336 : bl_log_isnew(sqlstore *store)
    3287             : {
    3288         336 :         logger *bat_logger = store->logger;
    3289         336 :         if (BATcount(bat_logger->catalog_bid) > 10) {
    3290         112 :                 return 0;
    3291             :         }
    3292             :         return 1;
    3293             : }
    3294             : 
    3295             : static int
    3296       61340 : bl_tstart(sqlstore *store, bool flush, ulng *log_file_id)
    3297             : {
    3298       61340 :         return log_tstart(store->logger, flush, log_file_id) == GDK_SUCCEED ? LOG_OK : LOG_ERR;
    3299             : }
    3300             : 
    3301             : static int
    3302       61340 : bl_tend(sqlstore *store)
    3303             : {
    3304       61340 :         return log_tend(store->logger) == GDK_SUCCEED ? LOG_OK : LOG_ERR;
    3305             : }
    3306             : 
    3307             : static int
    3308       61340 : bl_tflush(sqlstore *store, ulng log_file_id, ulng commit_ts)
    3309             : {
    3310       61340 :         return log_tflush(store->logger, log_file_id, commit_ts) == GDK_SUCCEED ? LOG_OK : LOG_ERR;
    3311             : }
    3312             : 
    3313             : static int
    3314        6658 : bl_sequence(sqlstore *store, int seq, lng id)
    3315             : {
    3316        6658 :         return log_tsequence(store->logger, seq, id) == GDK_SUCCEED ? LOG_OK : LOG_ERR;
    3317             : }
    3318             : 
    3319             : /* Write a plan entry to copy part of the given file.
    3320             :  * That part of the file must remain unchanged until the plan is executed.
    3321             :  */
    3322             : static gdk_return __attribute__((__warn_unused_result__))
    3323         992 : snapshot_lazy_copy_file(stream *plan, const char *name, uint64_t extent)
    3324             : {
    3325         992 :         if (mnstr_printf(plan, "c %" PRIu64 " %s\n", extent, name) < 0) {
    3326           0 :                 GDKerror("%s", mnstr_peek_error(plan));
    3327           0 :                 return GDK_FAIL;
    3328             :         }
    3329             :         return GDK_SUCCEED;
    3330             : }
    3331             : 
    3332             : /* Write a plan entry to write the current contents of the given file.
    3333             :  * The contents are included in the plan so the source file is allowed to
    3334             :  * change in the mean time.
    3335             :  */
    3336             : static gdk_return __attribute__((__warn_unused_result__))
    3337          10 : snapshot_immediate_copy_file(stream *plan, const char *path, const char *name)
    3338             : {
    3339          10 :         gdk_return ret = GDK_FAIL;
    3340          10 :         const size_t bufsize = 64 * 1024;
    3341          10 :         struct stat statbuf;
    3342          10 :         char *buf = NULL;
    3343          10 :         stream *s = NULL;
    3344          10 :         size_t to_copy;
    3345             : 
    3346          10 :         if (MT_stat(path, &statbuf) < 0) {
    3347           0 :                 GDKsyserror("stat failed on %s", path);
    3348           0 :                 goto end;
    3349             :         }
    3350          10 :         to_copy = (size_t) statbuf.st_size;
    3351             : 
    3352          10 :         s = open_rstream(path);
    3353          10 :         if (!s) {
    3354           0 :                 GDKerror("%s", mnstr_peek_error(NULL));
    3355           0 :                 goto end;
    3356             :         }
    3357             : 
    3358          10 :         buf = GDKmalloc(bufsize);
    3359          10 :         if (!buf) {
    3360           0 :                 GDKerror("GDKmalloc failed");
    3361           0 :                 goto end;
    3362             :         }
    3363             : 
    3364          10 :         if (mnstr_printf(plan, "w %zu %s\n", to_copy, name) < 0) {
    3365           0 :                 GDKerror("%s", mnstr_peek_error(plan));
    3366           0 :                 goto end;
    3367             :         }
    3368             : 
    3369          20 :         while (to_copy > 0) {
    3370          10 :                 size_t chunk = (to_copy <= bufsize) ? to_copy : bufsize;
    3371          10 :                 ssize_t bytes_read = mnstr_read(s, buf, 1, chunk);
    3372          10 :                 if (bytes_read < 0) {
    3373           0 :                         GDKerror("Reading bytes of component %s failed: %s", path, mnstr_peek_error(s));
    3374           0 :                         goto end;
    3375          10 :                 } else if (bytes_read < (ssize_t) chunk) {
    3376           0 :                         GDKerror("Read only %zu/%zu bytes of component %s: %s", (size_t) bytes_read, chunk, path, mnstr_peek_error(s));
    3377           0 :                         goto end;
    3378             :                 }
    3379             : 
    3380          10 :                 ssize_t bytes_written = mnstr_write(plan, buf, 1, chunk);
    3381          10 :                 if (bytes_written < 0) {
    3382           0 :                         GDKerror("Writing to plan failed: %s", mnstr_peek_error(plan));
    3383           0 :                         goto end;
    3384          10 :                 } else if (bytes_written < (ssize_t) chunk) {
    3385           0 :                         GDKerror("write to plan truncated");
    3386           0 :                         goto end;
    3387             :                 }
    3388          10 :                 to_copy -= chunk;
    3389             :         }
    3390             : 
    3391             :         ret = GDK_SUCCEED;
    3392          10 : end:
    3393          10 :         GDKfree(buf);
    3394          10 :         if (s)
    3395          10 :                 close_stream(s);
    3396          10 :         return ret;
    3397             : }
    3398             : 
    3399             : /* Add plan entries for all relevant files in the Write Ahead Log */
    3400             : static gdk_return __attribute__((__warn_unused_result__))
    3401           5 : snapshot_wal(logger *bat_logger, stream *plan, const char *db_dir)
    3402             : {
    3403           5 :         char log_file[FILENAME_MAX];
    3404           5 :         int len;
    3405             : 
    3406           5 :         len = snprintf(log_file, sizeof(log_file), "%s/%s%s", db_dir, bat_logger->dir, LOGFILE);
    3407           5 :         if (len == -1 || (size_t)len >= sizeof(log_file)) {
    3408           0 :                 GDKerror("Could not open %s, filename is too large", log_file);
    3409           0 :                 return GDK_FAIL;
    3410             :         }
    3411           5 :         if (snapshot_immediate_copy_file(plan, log_file, log_file + strlen(db_dir) + 1) != GDK_SUCCEED)
    3412             :                 return GDK_FAIL;
    3413             : 
    3414          12 :         for (ulng id = bat_logger->saved_id+1; id <= bat_logger->id; id++) {
    3415           7 :                 struct stat statbuf;
    3416             : 
    3417           7 :                 len = snprintf(log_file, sizeof(log_file), "%s/%s%s." LLFMT, db_dir, bat_logger->dir, LOGFILE, id);
    3418           7 :                 if (len == -1 || (size_t)len >= sizeof(log_file)) {
    3419           0 :                         GDKerror("Could not open %s, filename is too large", log_file);
    3420           0 :                         return GDK_FAIL;
    3421             :                 }
    3422           7 :                 if (MT_stat(log_file, &statbuf) == 0) {
    3423           7 :                         if (snapshot_lazy_copy_file(plan, log_file + strlen(db_dir) + 1, statbuf.st_size) != GDK_SUCCEED)
    3424             :                                 return GDK_FAIL;
    3425             :                 } else {
    3426           0 :                         GDKerror("Could not open %s", log_file);
    3427           0 :                         return GDK_FAIL;
    3428             :                 }
    3429             :         }
    3430             :         return GDK_SUCCEED;
    3431             : }
    3432             : 
    3433             : static gdk_return __attribute__((__warn_unused_result__))
    3434        1565 : snapshot_heap(stream *plan, const char *db_dir, bat batid, const char *filename, const char *suffix, uint64_t extent)
    3435             : {
    3436        1565 :         char path1[FILENAME_MAX];
    3437        1565 :         char path2[FILENAME_MAX];
    3438        1565 :         const size_t offset = strlen(db_dir) + 1;
    3439        1565 :         struct stat statbuf;
    3440        1565 :         int len;
    3441             : 
    3442        1565 :         if (extent == 0) {
    3443             :                 /* nothing to copy */
    3444             :                 return GDK_SUCCEED;
    3445             :         }
    3446             :         // first check the backup dir
    3447         985 :         len = snprintf(path1, FILENAME_MAX, "%s/%s/%o.%s", db_dir, BAKDIR, (int) batid, suffix);
    3448         985 :         if (len == -1 || len >= FILENAME_MAX) {
    3449           0 :                 path1[FILENAME_MAX - 1] = '\0';
    3450           0 :                 GDKerror("Could not open %s, filename is too large", path1);
    3451           0 :                 return GDK_FAIL;
    3452             :         }
    3453         985 :         if (MT_stat(path1, &statbuf) == 0) {
    3454           0 :                 return snapshot_lazy_copy_file(plan, path1 + offset, extent);
    3455             :         }
    3456         985 :         if (errno != ENOENT) {
    3457           0 :                 GDKsyserror("Error stat'ing %s", path1);
    3458           0 :                 return GDK_FAIL;
    3459             :         }
    3460             : 
    3461             :         // then check the regular location
    3462         985 :         len = snprintf(path2, FILENAME_MAX, "%s/%s/%s.%s", db_dir, BATDIR, filename, suffix);
    3463         985 :         if (len == -1 || len >= FILENAME_MAX) {
    3464           0 :                 path2[FILENAME_MAX - 1] = '\0';
    3465           0 :                 GDKerror("Could not open %s, filename is too large", path2);
    3466           0 :                 return GDK_FAIL;
    3467             :         }
    3468         985 :         if (MT_stat(path2, &statbuf) == 0) {
    3469         985 :                 return snapshot_lazy_copy_file(plan, path2 + offset, extent);
    3470             :         }
    3471           0 :         if (errno != ENOENT) {
    3472           0 :                 GDKsyserror("Error stat'ing %s", path2);
    3473           0 :                 return GDK_FAIL;
    3474             :         }
    3475             : 
    3476           0 :         GDKerror("One of %s and %s must exist", path1, path2);
    3477           0 :         return GDK_FAIL;
    3478             : }
    3479             : 
    3480             : /* Add plan entries for all persistent BATs by looping over the BBP.dir.
    3481             :  * Also include the BBP.dir itself.
    3482             :  */
    3483             : static gdk_return __attribute__((__warn_unused_result__))
    3484           5 : snapshot_bats(stream *plan, const char *db_dir)
    3485             : {
    3486           5 :         char bbpdir[FILENAME_MAX];
    3487           5 :         FILE *fp = NULL;
    3488           5 :         int len;
    3489           5 :         gdk_return ret = GDK_FAIL;
    3490           5 :         int lineno = 0;
    3491           5 :         bat bbpsize = 0;
    3492           5 :         lng logno, transid;
    3493           5 :         unsigned bbpversion;
    3494             : 
    3495           5 :         len = snprintf(bbpdir, FILENAME_MAX, "%s/%s/%s", db_dir, BAKDIR, "BBP.dir");
    3496           5 :         if (len == -1 || len >= FILENAME_MAX) {
    3497           0 :                 GDKerror("Could not open %s, filename is too large", bbpdir);
    3498           0 :                 return GDK_FAIL;
    3499             :         }
    3500           5 :         ret = snapshot_immediate_copy_file(plan, bbpdir, bbpdir + strlen(db_dir) + 1);
    3501           5 :         if (ret != GDK_SUCCEED)
    3502             :                 return ret;
    3503             : 
    3504             :         // Open the catalog and parse the header
    3505           5 :         fp = fopen(bbpdir, "r");
    3506           5 :         if (fp == NULL) {
    3507           0 :                 GDKerror("Could not open %s for reading: %s", bbpdir, mnstr_peek_error(NULL));
    3508           0 :                 return GDK_FAIL;
    3509             :         }
    3510           5 :         bbpversion = BBPheader(fp, &lineno, &bbpsize, &logno, &transid, false);
    3511           5 :         if (bbpversion == 0)
    3512           0 :                 goto end;
    3513           5 :         assert(bbpversion == GDKLIBRARY);
    3514             : 
    3515        1240 :         for (;;) {
    3516        1245 :                 BAT b;
    3517        1245 :                 Heap h;
    3518        1245 :                 Heap vh;
    3519        1245 :                 vh = h = (Heap) {
    3520             :                         .free = 0,
    3521             :                 };
    3522        1245 :                 b = (BAT) {
    3523             :                         .theap = &h,
    3524             :                         .tvheap = &vh,
    3525             :                 };
    3526        1245 :                 char *options;
    3527        1245 :                 char filename[sizeof(BBP_physical(0))];
    3528        1245 :                 char batname[129];
    3529             : #ifdef GDKLIBRARY_HASHASH
    3530        1245 :                 int hashash;
    3531             : #endif
    3532             : 
    3533        1245 :                 switch (BBPreadBBPline(fp, bbpversion, &lineno, &b,
    3534             : #ifdef GDKLIBRARY_HASHASH
    3535             :                                                            &hashash,
    3536             : #endif
    3537             :                                                            batname, filename, &options)) {
    3538           5 :                 case 0:
    3539             :                         /* end of file */
    3540           5 :                         fclose(fp);
    3541           5 :                         return GDK_SUCCEED;
    3542             :                 case 1:
    3543             :                         /* successfully read an entry */
    3544        1240 :                         break;
    3545           0 :                 default:
    3546             :                         /* error */
    3547           0 :                         fclose(fp);
    3548           0 :                         return GDK_FAIL;
    3549             :                 }
    3550             : #ifdef GDKLIBRARY_HASHASH
    3551        1240 :                 assert(hashash == 0);
    3552             : #endif
    3553        1240 :                 if (ATOMvarsized(b.ttype)) {
    3554         325 :                         ret = snapshot_heap(plan, db_dir, b.batCacheid, filename, "theap", b.tvheap->free);
    3555         325 :                         if (ret != GDK_SUCCEED)
    3556           0 :                                 goto end;
    3557             :                 }
    3558        1240 :                 ret = snapshot_heap(plan, db_dir, b.batCacheid, filename, BATtailname(&b), b.theap->free);
    3559        1240 :                 if (ret != GDK_SUCCEED)
    3560           0 :                         goto end;
    3561             :         }
    3562             : 
    3563           0 : end:
    3564           0 :         if (fp) {
    3565           0 :                 fclose(fp);
    3566             :         }
    3567           0 :         return ret;
    3568             : }
    3569             : 
    3570             : static gdk_return __attribute__((__warn_unused_result__))
    3571           5 : snapshot_vaultkey(stream *plan, const char *db_dir)
    3572             : {
    3573           5 :         char path[FILENAME_MAX];
    3574           5 :         struct stat statbuf;
    3575             : 
    3576           5 :         int len = snprintf(path, FILENAME_MAX, "%s/.vaultkey", db_dir);
    3577           5 :         if (len == -1 || len >= FILENAME_MAX) {
    3578           0 :                 path[FILENAME_MAX - 1] = '\0';
    3579           0 :                 GDKerror("Could not open %s, filename is too large", path);
    3580           0 :                 return GDK_FAIL;
    3581             :         }
    3582           5 :         if (MT_stat(path, &statbuf) == 0) {
    3583           0 :                 return snapshot_lazy_copy_file(plan, ".vaultkey", statbuf.st_size);
    3584             :         }
    3585           5 :         if (errno == ENOENT) {
    3586             :                 // No .vaultkey? Fine.
    3587             :                 return GDK_SUCCEED;
    3588             :         }
    3589             : 
    3590           0 :         GDKsyserror("Error stat'ing %s", path);
    3591           0 :         return GDK_FAIL;
    3592             : }
    3593             : 
    3594             : static gdk_return
    3595           5 : bl_snapshot(sqlstore *store, stream *plan)
    3596             : {
    3597           5 :         logger *bat_logger = store->logger;
    3598           5 :         gdk_return ret;
    3599           5 :         char *db_dir = NULL;
    3600           5 :         size_t db_dir_len;
    3601             : 
    3602             :         // Farm 0 is always the persistent farm.
    3603           5 :         db_dir = GDKfilepath(0, NULL, "", NULL);
    3604           5 :         if (db_dir == NULL)
    3605             :                 return GDK_FAIL;
    3606           5 :         db_dir_len = strlen(db_dir);
    3607           5 :         if (db_dir[db_dir_len - 1] == DIR_SEP)
    3608           5 :                 db_dir[db_dir_len - 1] = '\0';
    3609             : 
    3610          10 :         if (mnstr_printf(plan, "%s\n", db_dir) < 0 ||
    3611             :                 // Please monetdbd
    3612           5 :                 mnstr_printf(plan, "w 0 .uplog\n") < 0) {
    3613           0 :                 GDKerror("%s", mnstr_peek_error(plan));
    3614           0 :                 ret = GDK_FAIL;
    3615           0 :                 goto end;
    3616             :         }
    3617             : 
    3618           5 :         ret = snapshot_vaultkey(plan, db_dir);
    3619           5 :         if (ret != GDK_SUCCEED)
    3620           0 :                 goto end;
    3621             : 
    3622           5 :         ret = snapshot_bats(plan, db_dir);
    3623           5 :         if (ret != GDK_SUCCEED)
    3624           0 :                 goto end;
    3625             : 
    3626           5 :         ret = snapshot_wal(bat_logger, plan, db_dir);
    3627           5 :         if (ret != GDK_SUCCEED)
    3628             :                 goto end;
    3629             : 
    3630             :         ret = GDK_SUCCEED;
    3631           5 : end:
    3632           5 :         GDKfree(db_dir);
    3633           5 :         return ret;
    3634             : }
    3635             : 
    3636             : void
    3637         336 : bat_logger_init( logger_functions *lf )
    3638             : {
    3639         336 :         lf->create = bl_create;
    3640         336 :         lf->destroy = bl_destroy;
    3641         336 :         lf->flush = bl_flush;
    3642         336 :         lf->activate = bl_activate;
    3643         336 :         lf->changes = bl_changes;
    3644         336 :         lf->get_sequence = bl_get_sequence;
    3645         336 :         lf->log_isnew = bl_log_isnew;
    3646         336 :         lf->log_tstart = bl_tstart;
    3647         336 :         lf->log_tend = bl_tend;
    3648         336 :         lf->log_tflush = bl_tflush;
    3649         336 :         lf->log_tsequence = bl_sequence;
    3650         336 :         lf->get_snapshot_files = bl_snapshot;
    3651         336 : }

Generated by: LCOV version 1.14