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