LCOV - code coverage report
Current view: top level - gdk - gdk_project.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 397 719 55.2 %
Date: 2024-04-25 20:03:45 Functions: 21 22 95.5 %

          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 "gdk.h"
      15             : #include "gdk_private.h"
      16             : 
      17             : /*
      18             :  * BATproject returns a BAT aligned with the left input whose values
      19             :  * are the values from the right input that were referred to by the
      20             :  * OIDs in the left input.
      21             :  *
      22             :  * BATproject2 is similar, except instead of a single right input
      23             :  * there are two of which the second's hseqbase is equal to the first
      24             :  * hseqbase + its batCount.
      25             :  */
      26             : 
      27             : #define project1_loop(TYPE)                                             \
      28             : static gdk_return                                                       \
      29             : project1_##TYPE(BAT *restrict bn, BATiter *restrict li,                 \
      30             :                 BATiter *restrict r1i, lng timeoffset)                  \
      31             : {                                                                       \
      32             :         BUN lo;                                                         \
      33             :         const TYPE *restrict r1t;                                       \
      34             :         TYPE *restrict bt;                                              \
      35             :         oid r1seq, r1end;                                               \
      36             :                                                                         \
      37             :         MT_thread_setalgorithm(__func__);                               \
      38             :         r1t = (const TYPE *) r1i->base;                                      \
      39             :         bt = (TYPE *) Tloc(bn, 0);                                      \
      40             :         r1seq = r1i->b->hseqbase;                                 \
      41             :         r1end = r1seq + r1i->count;                                  \
      42             :         if (BATtdensebi(li)) {                                          \
      43             :                 if (li->tseq < r1seq ||                                   \
      44             :                     (li->tseq + li->count) >= r1end) {                 \
      45             :                         GDKerror("does not match always\n");          \
      46             :                         return GDK_FAIL;                                \
      47             :                 }                                                       \
      48             :                 oid off = li->tseq - r1seq;                          \
      49             :                 r1t += off;                                             \
      50             :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset)          \
      51             :                         bt[lo] = r1t[lo];                               \
      52             :         } else {                                                        \
      53             :                 assert(li->type);                                    \
      54             :                 const oid *restrict ot = (const oid *) li->base;     \
      55             :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {                \
      56             :                         oid o = ot[lo];                                 \
      57             :                         if (o < r1seq || o >= r1end) {                    \
      58             :                                 GDKerror("does not match always\n");  \
      59             :                                 return GDK_FAIL;                        \
      60             :                         }                                               \
      61             :                         bt[lo] = r1t[o - r1seq];                        \
      62             :                 }                                                       \
      63             :         }                                                               \
      64             :         TIMEOUT_CHECK(timeoffset, TIMEOUT_HANDLER(GDK_FAIL));           \
      65             :         BATsetcount(bn, lo);                                            \
      66             :         return GDK_SUCCEED;                                             \
      67             : }
      68             : 
      69             : /* project type switch */
      70    63765335 : project1_loop(bte)
      71    16632165 : project1_loop(sht)
      72   695108830 : project1_loop(int)
      73        1708 : project1_loop(flt)
      74      496658 : project1_loop(dbl)
      75   189172153 : project1_loop(lng)
      76             : #ifdef HAVE_HGE
      77    56180714 : project1_loop(hge)
      78             : #endif
      79         131 : project1_loop(uuid)
      80             : 
      81             : #define project_loop(TYPE)                                              \
      82             : static gdk_return                                                       \
      83             : project_##TYPE(BAT *restrict bn, BATiter *restrict li,                  \
      84             :                struct canditer *restrict ci,                            \
      85             :                BATiter *restrict r1i, BATiter *restrict r2i,            \
      86             :                lng timeoffset)                                          \
      87             : {                                                                       \
      88             :         BUN lo;                                                         \
      89             :         const TYPE *restrict r1t;                                       \
      90             :         const TYPE *restrict r2t;                                       \
      91             :         TYPE *restrict bt;                                              \
      92             :         TYPE v;                                                         \
      93             :         oid r1seq, r1end;                                               \
      94             :         oid r2seq, r2end;                                               \
      95             :                                                                         \
      96             :         if (r2i == NULL &&                                              \
      97             :             (ci == NULL || (ci->tpe == cand_dense && BATtdensebi(li))) && \
      98             :             li->nonil && r1i->type && !BATtdensebi(r1i))          \
      99             :                 return project1_##TYPE(bn, li, r1i, timeoffset);        \
     100             :         MT_thread_setalgorithm(__func__);                               \
     101             :         r1t = (const TYPE *) r1i->base;                                      \
     102             :         bt = (TYPE *) Tloc(bn, 0);                                      \
     103             :         r1seq = r1i->b->hseqbase;                                 \
     104             :         r1end = r1seq + r1i->count;                                  \
     105             :         if (r2i) {                                                      \
     106             :                 r2t = (const TYPE *) r2i->base;                              \
     107             :                 r2seq = r2i->b->hseqbase;                         \
     108             :                 r2end = r2seq + r2i->count;                          \
     109             :         } else {                                                        \
     110             :                 r2t = NULL;                                             \
     111             :                 r2seq = r2end = r1end;                                  \
     112             :         }                                                               \
     113             :         if (ci) {                                                       \
     114             :                 TIMEOUT_LOOP_IDX(lo, ci->ncand, timeoffset) {                \
     115             :                         oid o = canditer_next(ci);                      \
     116             :                         if (o < r1seq || o >= r2end) {                    \
     117             :                                 GDKerror("does not match always\n");  \
     118             :                                 return GDK_FAIL;                        \
     119             :                         }                                               \
     120             :                         if (o < r1end)                                       \
     121             :                                 v = r1t[o - r1seq];                     \
     122             :                         else                                            \
     123             :                                 v = r2t[o - r2seq];                     \
     124             :                         bt[lo] = v;                                     \
     125             :                 }                                                       \
     126             :         } else if (BATtdensebi(li)) {                                   \
     127             :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {                \
     128             :                         oid o = li->tseq + lo;                               \
     129             :                         if (o < r1seq || o >= r2end) {                    \
     130             :                                 GDKerror("does not match always\n");  \
     131             :                                 return GDK_FAIL;                        \
     132             :                         }                                               \
     133             :                         if (o < r1end)                                       \
     134             :                                 v = r1t[o - r1seq];                     \
     135             :                         else                                            \
     136             :                                 v = r2t[o - r2seq];                     \
     137             :                         bt[lo] = v;                                     \
     138             :                 }                                                       \
     139             :         } else {                                                        \
     140             :                 const oid *restrict ot = (const oid *) li->base;     \
     141             :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {                \
     142             :                         oid o = ot[lo];                                 \
     143             :                         if (is_oid_nil(o)) {                            \
     144             :                                 bt[lo] = v = TYPE##_nil;                \
     145             :                                 bn->tnil = true;                     \
     146             :                         } else if (o < r1seq || o >= r2end) {             \
     147             :                                 GDKerror("does not match always\n");  \
     148             :                                 return GDK_FAIL;                        \
     149             :                         } else if (o < r1end) {                              \
     150             :                                 v = r1t[o - r1seq];                     \
     151             :                                 bt[lo] = v;                             \
     152             :                         } else {                                        \
     153             :                                 v = r2t[o - r2seq];                     \
     154             :                                 bt[lo] = v;                             \
     155             :                         }                                               \
     156             :                 }                                                       \
     157             :         }                                                               \
     158             :         TIMEOUT_CHECK(timeoffset, TIMEOUT_HANDLER(GDK_FAIL));           \
     159             :         BATsetcount(bn, lo);                                            \
     160             :         return GDK_SUCCEED;                                             \
     161             : }
     162             : 
     163             : 
     164             : /* project type switch */
     165      187906 : project_loop(bte)
     166      417423 : project_loop(sht)
     167    32944932 : project_loop(int)
     168          79 : project_loop(flt)
     169        3774 : project_loop(dbl)
     170       26628 : project_loop(lng)
     171             : #ifdef HAVE_HGE
     172     1223123 : project_loop(hge)
     173             : #endif
     174          24 : project_loop(uuid)
     175             : 
     176             : static gdk_return
     177       18574 : project_oid(BAT *restrict bn, BATiter *restrict li,
     178             :             struct canditer *restrict lci,
     179             :             BATiter *restrict r1i, BATiter *restrict r2i, lng timeoffset)
     180             : {
     181       18574 :         BUN lo;
     182       18574 :         oid *restrict bt;
     183       18574 :         oid r1seq, r1end;
     184       18574 :         oid r2seq, r2end;
     185       18574 :         const oid *restrict r1t = NULL;
     186       18574 :         const oid *restrict r2t = NULL;
     187       18574 :         struct canditer r1ci = {0}, r2ci = {0};
     188             : 
     189       18574 :         if ((!lci || (lci->tpe == cand_dense && BATtdensebi(li))) && r1i->type && !BATtdensebi(r1i) && !r2i && li->nonil) {
     190        7095 :                 if (sizeof(oid) == sizeof(lng))
     191        7095 :                         return project1_lng(bn, li, r1i, timeoffset);
     192             :                 else
     193             :                         return project1_int(bn, li, r1i, timeoffset);
     194             :         }
     195       11479 :         MT_thread_setalgorithm(__func__);
     196       11476 :         if (complex_cand(r1i->b))
     197          24 :                 canditer_init(&r1ci, NULL, r1i->b);
     198       11452 :         else if (!BATtdensebi(r1i))
     199           1 :                 r1t = (const oid *) r1i->base;
     200       11476 :         r1seq = r1i->b->hseqbase;
     201       11476 :         r1end = r1seq + r1i->count;
     202       11476 :         if (r2i) {
     203           0 :                 if (complex_cand(r2i->b))
     204           0 :                         canditer_init(&r2ci, NULL, r2i->b);
     205           0 :                 else if (!BATtdensebi(r2i))
     206           0 :                         r2t = (const oid *) r2i->base;
     207           0 :                 r2seq = r2i->b->hseqbase;
     208           0 :                 r2end = r2seq + r2i->count;
     209             :         } else {
     210             :                 r2seq = r2end = r1end;
     211             :         }
     212       11476 :         bt = (oid *) Tloc(bn, 0);
     213       11476 :         if (lci) {
     214          13 :                 TIMEOUT_LOOP_IDX(lo, lci->ncand, timeoffset) {
     215           4 :                         oid o = canditer_next(lci);
     216           4 :                         if (o < r1seq || o >= r2end) {
     217           0 :                                 goto nomatch;
     218             :                         }
     219           4 :                         if (o < r1end) {
     220           4 :                                 if (r1ci.s)
     221           0 :                                         bt[lo] = canditer_idx(&r1ci, o - r1seq);
     222           4 :                                 else if (r1t)
     223           1 :                                         bt[lo] = r1t[o - r1seq];
     224             :                                 else
     225           3 :                                         bt[lo] = o - r1seq + r1i->tseq;
     226             :                         } else {
     227           0 :                                 if (r2ci.s)
     228           0 :                                         bt[lo] = canditer_idx(&r2ci, o - r2seq);
     229           0 :                                 else if (r2t)
     230           0 :                                         bt[lo] = r2t[o - r2seq];
     231             :                                 else
     232           0 :                                         bt[lo] = o - r2seq + r2i->tseq;
     233             :                         }
     234             :                 }
     235       11473 :         } else if (BATtdensebi(li)) {
     236           0 :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {
     237           0 :                         oid o = li->tseq + lo;
     238           0 :                         if (o < r1seq || o >= r2end) {
     239           0 :                                 goto nomatch;
     240             :                         }
     241           0 :                         if (o < r1end) {
     242           0 :                                 if (r1ci.s)
     243           0 :                                         bt[lo] = canditer_idx(&r1ci, o - r1seq);
     244           0 :                                 else if (r1t)
     245           0 :                                         bt[lo] = r1t[o - r1seq];
     246             :                                 else
     247           0 :                                         bt[lo] = o - r1seq + r1i->tseq;
     248             :                         } else {
     249           0 :                                 if (r2ci.s)
     250           0 :                                         bt[lo] = canditer_idx(&r2ci, o - r2seq);
     251           0 :                                 else if (r2t)
     252           0 :                                         bt[lo] = r2t[o - r2seq];
     253             :                                 else
     254           0 :                                         bt[lo] = o - r2seq + r2i->tseq;
     255             :                         }
     256             :                 }
     257             :         } else {
     258       11473 :                 const oid *ot = (const oid *) li->base;
     259   169470577 :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {
     260   169422225 :                         oid o = ot[lo];
     261   169422225 :                         if (is_oid_nil(o)) {
     262           0 :                                 bt[lo] = oid_nil;
     263           0 :                                 bn->tnonil = false;
     264           0 :                                 bn->tnil = true;
     265   169422225 :                         } else if (o < r1seq || o >= r2end) {
     266           0 :                                 goto nomatch;
     267   169422225 :                         } else if (o < r1end) {
     268   169422225 :                                 if (r1ci.s)
     269        7115 :                                         bt[lo] = canditer_idx(&r1ci, o - r1seq);
     270   169415110 :                                 else if (r1t)
     271           0 :                                         bt[lo] = r1t[o - r1seq];
     272             :                                 else
     273   169415110 :                                         bt[lo] = o - r1seq + r1i->tseq;
     274             :                         } else {
     275           0 :                                 if (r2ci.s)
     276           0 :                                         bt[lo] = canditer_idx(&r2ci, o - r2seq);
     277           0 :                                 else if (r2t)
     278           0 :                                         bt[lo] = r2t[o - r2seq];
     279             :                                 else
     280           0 :                                         bt[lo] = o - r2seq + r2i->tseq;
     281             :                         }
     282             :                 }
     283             :         }
     284       11476 :         TIMEOUT_CHECK(timeoffset, TIMEOUT_HANDLER(GDK_FAIL));
     285       11476 :         BATsetcount(bn, lo);
     286       11476 :         return GDK_SUCCEED;
     287           0 :   nomatch:
     288           0 :         GDKerror("does not match always\n");
     289           0 :         return GDK_FAIL;
     290             : }
     291             : 
     292             : static gdk_return
     293        1496 : project_any(BAT *restrict bn, BATiter *restrict li,
     294             :             struct canditer *restrict ci,
     295             :             BATiter *restrict r1i, BATiter *restrict r2i, lng timeoffset)
     296             : {
     297        1496 :         BUN lo;
     298        1496 :         const void *nil = ATOMnilptr(r1i->type);
     299        1496 :         const void *v;
     300        1496 :         oid r1seq, r1end;
     301        1496 :         oid r2seq, r2end;
     302             : 
     303        1496 :         MT_thread_setalgorithm(__func__);
     304        1496 :         r1seq = r1i->b->hseqbase;
     305        1496 :         r1end = r1seq + r1i->count;
     306        1496 :         if (r2i) {
     307           0 :                 r2seq = r2i->b->hseqbase;
     308           0 :                 r2end = r2seq + r2i->count;
     309             :         } else {
     310             :                 r2seq = r2end = r1end;
     311             :         }
     312        1496 :         if (ci) {
     313           0 :                 TIMEOUT_LOOP_IDX(lo, ci->ncand, timeoffset) {
     314           0 :                         oid o = canditer_next(ci);
     315           0 :                         if (o < r1seq || o >= r2end) {
     316           0 :                                 GDKerror("does not match always\n");
     317           0 :                                 return GDK_FAIL;
     318             :                         }
     319           0 :                         if (o < r1end)
     320           0 :                                 v = BUNtail(*r1i, o - r1seq);
     321             :                         else
     322           0 :                                 v = BUNtail(*r2i, o - r2seq);
     323           0 :                         if (tfastins_nocheck(bn, lo, v) != GDK_SUCCEED) {
     324             :                                 return GDK_FAIL;
     325             :                         }
     326             :                 }
     327        1497 :         } else if (BATtdensebi(li)) {
     328           0 :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {
     329           0 :                         oid o = li->tseq + lo;
     330           0 :                         if (o < r1seq || o >= r2end) {
     331           0 :                                 GDKerror("does not match always\n");
     332           0 :                                 return GDK_FAIL;
     333             :                         }
     334           0 :                         if (o < r1end)
     335           0 :                                 v = BUNtail(*r1i, o - r1seq);
     336             :                         else
     337           0 :                                 v = BUNtail(*r2i, o - r2seq);
     338           0 :                         if (tfastins_nocheck(bn, lo, v) != GDK_SUCCEED) {
     339             :                                 return GDK_FAIL;
     340             :                         }
     341             :                 }
     342             :         } else {
     343        1497 :                 const oid *restrict ot = (const oid *) li->base;
     344             : 
     345      190910 :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {
     346      186417 :                         oid o = ot[lo];
     347      186417 :                         if (is_oid_nil(o)) {
     348           2 :                                 v = nil;
     349           2 :                                 bn->tnil = true;
     350      186415 :                         } else if (o < r1seq || o >= r2end) {
     351           0 :                                 GDKerror("does not match always\n");
     352           0 :                                 return GDK_FAIL;
     353      186415 :                         } else if (o < r1end) {
     354      186415 :                                 v = BUNtail(*r1i, o - r1seq);
     355             :                         } else {
     356           0 :                                 v = BUNtail(*r2i, o - r2seq);
     357             :                         }
     358      185840 :                         if (tfastins_nocheck(bn, lo, v) != GDK_SUCCEED) {
     359             :                                 return GDK_FAIL;
     360             :                         }
     361             :                 }
     362             :         }
     363        1496 :         TIMEOUT_CHECK(timeoffset, TIMEOUT_HANDLER(GDK_FAIL));
     364        1496 :         BATsetcount(bn, lo);
     365        1496 :         bn->theap->dirty = true;
     366        1496 :         return GDK_SUCCEED;
     367             : }
     368             : 
     369             : static BAT *
     370           0 : project_str(BATiter *restrict li, struct canditer *restrict ci, int tpe,
     371             :             BATiter *restrict r1i, BATiter *restrict r2i,
     372             :             lng timeoffset, lng t0)
     373             : {
     374           0 :         BAT *bn;
     375           0 :         BUN lo;
     376           0 :         oid r1seq, r1end;
     377           0 :         oid r2seq, r2end;
     378           0 :         BUN h1off;
     379           0 :         BUN off;
     380           0 :         oid seq;
     381           0 :         var_t v;
     382           0 :         BATiter *ri;
     383             : 
     384           0 :         if ((bn = COLnew(li->b->hseqbase, tpe, ci ? ci->ncand : li->count,
     385             :                          TRANSIENT)) == NULL)
     386             :                 return NULL;
     387             : 
     388           0 :         v = (var_t) r1i->vhfree;
     389           0 :         if (r1i->vh == r2i->vh) {
     390           0 :                 h1off = 0;
     391           0 :                 assert(bn->tvheap->parentid == bn->batCacheid);
     392           0 :                 HEAPdecref(bn->tvheap, true);
     393           0 :                 HEAPincref(r1i->vh);
     394           0 :                 bn->tvheap = r1i->vh;
     395           0 :                 assert(bn->tvheap->parentid != bn->batCacheid);
     396           0 :                 BBPretain(bn->tvheap->parentid);
     397             :         } else {
     398           0 :                 v = (v + GDK_VARALIGN - 1) & ~(GDK_VARALIGN - 1);
     399           0 :                 h1off = (BUN) v;
     400           0 :                 v += ((var_t) r2i->vhfree + GDK_VARALIGN - 1) & ~(GDK_VARALIGN - 1);
     401           0 :                 if (HEAPextend(bn->tvheap, v, false) != GDK_SUCCEED) {
     402           0 :                         BBPreclaim(bn);
     403           0 :                         return NULL;
     404             :                 }
     405           0 :                 memcpy(bn->tvheap->base, r1i->vh->base, r1i->vhfree);
     406             : #ifndef NDEBUG
     407           0 :                 if (h1off > r1i->vhfree)
     408           0 :                         memset(bn->tvheap->base + r1i->vhfree, 0, h1off - r1i->vhfree);
     409             : #endif
     410           0 :                 memcpy(bn->tvheap->base + h1off, r2i->vh->base, r2i->vhfree);
     411           0 :                 bn->tvheap->free = h1off + r2i->vhfree;
     412           0 :                 bn->tvheap->dirty = true;
     413             :         }
     414             : 
     415           0 :         if (v >= ((var_t) 1 << (8 << bn->tshift)) &&
     416           0 :             GDKupgradevarheap(bn, v, false, 0) != GDK_SUCCEED) {
     417           0 :                 BBPreclaim(bn);
     418           0 :                 return NULL;
     419             :         }
     420             : 
     421           0 :         r1seq = r1i->b->hseqbase;
     422           0 :         r1end = r1seq + r1i->count;
     423           0 :         r2seq = r2i->b->hseqbase;
     424           0 :         r2end = r2seq + r2i->count;
     425           0 :         if (ci) {
     426           0 :                 TIMEOUT_LOOP_IDX(lo, ci->ncand, timeoffset) {
     427           0 :                         oid o = canditer_next(ci);
     428           0 :                         if (o < r1seq || o >= r2end) {
     429           0 :                                 GDKerror("does not match always\n");
     430           0 :                                 BBPreclaim(bn);
     431           0 :                                 return NULL;
     432             :                         }
     433           0 :                         if (o < r1end) {
     434             :                                 ri = r1i;
     435             :                                 off = 0;
     436             :                                 seq = r1seq;
     437             :                         } else {
     438           0 :                                 ri = r2i;
     439           0 :                                 off = h1off;
     440           0 :                                 seq = r2seq;
     441             :                         }
     442           0 :                         switch (ri->width) {
     443           0 :                         case 1:
     444           0 :                                 v = (var_t) ((uint8_t *) ri->base)[o - seq] + GDK_VAROFFSET;
     445           0 :                                 break;
     446           0 :                         case 2:
     447           0 :                                 v = (var_t) ((uint16_t *) ri->base)[o - seq] + GDK_VAROFFSET;
     448           0 :                                 break;
     449           0 :                         case 4:
     450           0 :                                 v = (var_t) ((uint32_t *) ri->base)[o - seq];
     451           0 :                                 break;
     452           0 :                         case 8:
     453           0 :                                 v = (var_t) ((uint64_t *) ri->base)[o - seq];
     454           0 :                                 break;
     455             :                         }
     456           0 :                         v += off;
     457           0 :                         switch (bn->twidth) {
     458           0 :                         case 1:
     459           0 :                                 ((uint8_t *) bn->theap->base)[lo] = (uint8_t) (v - GDK_VAROFFSET);
     460           0 :                                 break;
     461           0 :                         case 2:
     462           0 :                                 ((uint16_t *) bn->theap->base)[lo] = (uint16_t) (v - GDK_VAROFFSET);
     463           0 :                                 break;
     464           0 :                         case 4:
     465           0 :                                 ((uint32_t *) bn->theap->base)[lo] = (uint32_t) v;
     466           0 :                                 break;
     467           0 :                         case 8:
     468           0 :                                 ((uint64_t *) bn->theap->base)[lo] = (uint64_t) v;
     469           0 :                                 break;
     470             :                         }
     471             :                 }
     472           0 :         } else if (BATtdensebi(li)) {
     473           0 :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {
     474           0 :                         oid o = li->tseq + lo;
     475           0 :                         if (o < r1seq || o >= r2end) {
     476           0 :                                 GDKerror("does not match always\n");
     477           0 :                                 BBPreclaim(bn);
     478           0 :                                 return NULL;
     479             :                         }
     480           0 :                         if (o < r1end) {
     481             :                                 ri = r1i;
     482             :                                 off = 0;
     483             :                                 seq = r1seq;
     484             :                         } else {
     485           0 :                                 ri = r2i;
     486           0 :                                 off = h1off;
     487           0 :                                 seq = r2seq;
     488             :                         }
     489           0 :                         switch (ri->width) {
     490           0 :                         case 1:
     491           0 :                                 v = (var_t) ((uint8_t *) ri->base)[o - seq] + GDK_VAROFFSET;
     492           0 :                                 break;
     493           0 :                         case 2:
     494           0 :                                 v = (var_t) ((uint16_t *) ri->base)[o - seq] + GDK_VAROFFSET;
     495           0 :                                 break;
     496           0 :                         case 4:
     497           0 :                                 v = (var_t) ((uint32_t *) ri->base)[o - seq];
     498           0 :                                 break;
     499           0 :                         case 8:
     500           0 :                                 v = (var_t) ((uint64_t *) ri->base)[o - seq];
     501           0 :                                 break;
     502             :                         }
     503           0 :                         v += off;
     504           0 :                         switch (bn->twidth) {
     505           0 :                         case 1:
     506           0 :                                 ((uint8_t *) bn->theap->base)[lo] = (uint8_t) (v - GDK_VAROFFSET);
     507           0 :                                 break;
     508           0 :                         case 2:
     509           0 :                                 ((uint16_t *) bn->theap->base)[lo] = (uint16_t) (v - GDK_VAROFFSET);
     510           0 :                                 break;
     511           0 :                         case 4:
     512           0 :                                 ((uint32_t *) bn->theap->base)[lo] = (uint32_t) v;
     513           0 :                                 break;
     514           0 :                         case 8:
     515           0 :                                 ((uint64_t *) bn->theap->base)[lo] = (uint64_t) v;
     516           0 :                                 break;
     517             :                         }
     518             :                 }
     519             :         } else {
     520           0 :                 const oid *restrict ot = (const oid *) li->base;
     521           0 :                 TIMEOUT_LOOP_IDX(lo, li->count, timeoffset) {
     522           0 :                         oid o = ot[lo];
     523           0 :                         if (o < r1seq || o >= r2end) {
     524           0 :                                 GDKerror("does not match always\n");
     525           0 :                                 BBPreclaim(bn);
     526           0 :                                 return NULL;
     527             :                         }
     528           0 :                         if (o < r1end) {
     529             :                                 ri = r1i;
     530             :                                 off = 0;
     531             :                                 seq = r1seq;
     532             :                         } else {
     533           0 :                                 ri = r2i;
     534           0 :                                 off = h1off;
     535           0 :                                 seq = r2seq;
     536             :                         }
     537           0 :                         switch (ri->width) {
     538           0 :                         case 1:
     539           0 :                                 v = (var_t) ((uint8_t *) ri->base)[o - seq] + GDK_VAROFFSET;
     540           0 :                                 break;
     541           0 :                         case 2:
     542           0 :                                 v = (var_t) ((uint16_t *) ri->base)[o - seq] + GDK_VAROFFSET;
     543           0 :                                 break;
     544           0 :                         case 4:
     545           0 :                                 v = (var_t) ((uint32_t *) ri->base)[o - seq];
     546           0 :                                 break;
     547           0 :                         case 8:
     548           0 :                                 v = (var_t) ((uint64_t *) ri->base)[o - seq];
     549           0 :                                 break;
     550             :                         }
     551           0 :                         v += off;
     552           0 :                         switch (bn->twidth) {
     553           0 :                         case 1:
     554           0 :                                 ((uint8_t *) bn->theap->base)[lo] = (uint8_t) (v - GDK_VAROFFSET);
     555           0 :                                 break;
     556           0 :                         case 2:
     557           0 :                                 ((uint16_t *) bn->theap->base)[lo] = (uint16_t) (v - GDK_VAROFFSET);
     558           0 :                                 break;
     559           0 :                         case 4:
     560           0 :                                 ((uint32_t *) bn->theap->base)[lo] = (uint32_t) v;
     561           0 :                                 break;
     562           0 :                         case 8:
     563           0 :                                 ((uint64_t *) bn->theap->base)[lo] = (uint64_t) v;
     564           0 :                                 break;
     565             :                         }
     566             :                 }
     567             :         }
     568           0 :         TIMEOUT_CHECK(timeoffset, GOTO_LABEL_TIMEOUT_HANDLER(bailout));
     569           0 :         BATsetcount(bn, lo);
     570           0 :         bn->tsorted = bn->trevsorted = false;
     571           0 :         bn->tnil = false;
     572           0 :         bn->tnonil = r1i->nonil & r2i->nonil;
     573           0 :         bn->tkey = false;
     574           0 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT " r1=" ALGOBATFMT " r2=" ALGOBATFMT
     575             :                   " -> " ALGOBATFMT "%s " LLFMT "us\n",
     576             :                   ALGOBATPAR(li->b), ALGOBATPAR(r1i->b), ALGOBATPAR(r2i->b),
     577             :                   ALGOBATPAR(bn),
     578             :                   bn && bn->ttype == TYPE_str && bn->tvheap == r1i->vh ? " sharing string heap" : "",
     579             :                   GDKusec() - t0);
     580             :         return bn;
     581           0 :   bailout:
     582           0 :         BBPreclaim(bn);
     583           0 :         return NULL;
     584             : }
     585             : 
     586             : BAT *
     587     1403643 : BATproject2(BAT *restrict l, BAT *restrict r1, BAT *restrict r2)
     588             : {
     589     1403643 :         BAT *bn = NULL;
     590     1403643 :         BAT *or1 = r1, *or2 = r2, *ol = l;
     591     1403643 :         oid lo, hi;
     592     1403643 :         gdk_return res;
     593     1403643 :         int tpe = ATOMtype(r1->ttype), otpe = tpe;
     594     1403643 :         bool stringtrick = false;
     595     1403643 :         struct canditer ci, *lci = NULL;
     596     1403643 :         const char *msg = "";
     597     1403643 :         lng t0 = 0;
     598     1403643 :         BATiter li = bat_iterator(l);
     599     1403591 :         BATiter r1i = bat_iterator(r1);
     600     1403528 :         BATiter r2i = bat_iterator(r2);
     601     1403639 :         BUN lcount = li.count;
     602             : 
     603     1403639 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
     604             : 
     605     1403639 :         assert(ATOMtype(li.type) == TYPE_oid || li.type == TYPE_msk);
     606     1403639 :         assert(r2 == NULL || tpe == ATOMtype(r2i.type));
     607           0 :         assert(r2 == NULL || r1->hseqbase + r1i.count == r2->hseqbase);
     608             : 
     609     1403639 :         lng timeoffset = 0;
     610     1403639 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
     611     1403410 :         if (qry_ctx != NULL) {
     612     1369962 :                 timeoffset = (qry_ctx->starttime && qry_ctx->querytimeout) ? (qry_ctx->starttime + qry_ctx->querytimeout) : 0;
     613             :         }
     614             : 
     615     1403410 :         if (r2 && r1i.count == 0) {
     616             :                 /* unlikely special case: r1 is empty, so we just have r2 */
     617           0 :                 r1 = r2;
     618           0 :                 r2 = NULL;
     619           0 :                 bat_iterator_end(&r1i);
     620           0 :                 r1i = r2i;
     621           0 :                 r2i = bat_iterator(NULL);
     622             :         }
     623             : 
     624     1403410 :         if (BATtdensebi(&li) && lcount > 0) {
     625      568605 :                 lo = l->tseqbase;
     626      568605 :                 hi = l->tseqbase + lcount;
     627      568605 :                 if (lo >= r1->hseqbase && hi <= r1->hseqbase + r1i.count) {
     628      568605 :                         bn = BATslice(r1, lo - r1->hseqbase, hi - r1->hseqbase);
     629      568622 :                         BAThseqbase(bn, l->hseqbase);
     630      568659 :                         msg = " (slice)";
     631      568659 :                         goto doreturn;
     632             :                 }
     633           0 :                 if (lo < r1->hseqbase || r2 == NULL || hi > r2->hseqbase + r2i.count) {
     634           0 :                         GDKerror("does not match always\n");
     635           0 :                         bat_iterator_end(&li);
     636           0 :                         bat_iterator_end(&r1i);
     637           0 :                         bat_iterator_end(&r2i);
     638           0 :                         return NULL;
     639             :                 }
     640           0 :                 if (lo >= r2->hseqbase) {
     641           0 :                         bn = BATslice(r2, lo - r2->hseqbase, hi - r2->hseqbase);
     642           0 :                         BAThseqbase(bn, l->hseqbase);
     643           0 :                         msg = " (slice2)";
     644           0 :                         goto doreturn;
     645             :                 }
     646             :         }
     647      834805 :         if (complex_cand(l)) {
     648             :                 /* l is candidate list with exceptions or is a bitmask */
     649        9754 :                 assert(li.type == TYPE_msk || !is_oid_nil(l->tseqbase));
     650        9754 :                 canditer_init(&ci, NULL, l);
     651        9755 :                 lcount = ci.ncand;
     652        9755 :                 lci = &ci;
     653      825051 :         } else if (li.type == TYPE_msk) {
     654           0 :                 l = BATunmask(l);
     655           0 :                 if (l == NULL)
     656           0 :                         goto doreturn;
     657           0 :                 if (complex_cand(l)) {
     658           0 :                         canditer_init(&ci, NULL, l);
     659           0 :                         lcount = ci.ncand;
     660           0 :                         lci = &ci;
     661             :                 }
     662             :         }
     663      834806 :         if (lcount == 0 ||
     664        9761 :             (li.type == TYPE_void && is_oid_nil(l->tseqbase)) ||
     665      183253 :             (r1i.type == TYPE_void && is_oid_nil(r1->tseqbase) &&
     666           0 :              (r2 == NULL ||
     667           0 :               (r2i.type == TYPE_void && is_oid_nil(r2->tseqbase))))) {
     668             :                 /* trivial: all values are nil (includes no entries at all) */
     669      651553 :                 const void *nil = r1i.type == TYPE_msk ? &oid_nil : ATOMnilptr(r1i.type);
     670             : 
     671     1296209 :                 bn = BATconstant(l->hseqbase, r1i.type == TYPE_oid || r1i.type == TYPE_msk ? TYPE_void : r1i.type,
     672             :                                  nil, lcount, TRANSIENT);
     673      651673 :                 if (bn != NULL &&
     674      651673 :                     ATOMtype(bn->ttype) == TYPE_oid &&
     675       90769 :                     BATcount(bn) == 0) {
     676       90768 :                         BATtseqbase(bn, 0);
     677             :                 }
     678      651678 :                 msg = " (constant)";
     679      651678 :                 goto doreturn;
     680             :         }
     681             : 
     682      183253 :         if (ATOMstorage(tpe) == TYPE_str) {
     683       20033 :                 if (li.nonil &&
     684       20031 :                     r2 == NULL &&
     685       20031 :                     (r1i.count == 0 ||
     686       20030 :                      lcount > (r1i.count >> 3) ||
     687        4593 :                      r1i.restricted == BAT_READ)) {
     688             :                         /* insert strings as ints, we need to copy the
     689             :                          * string heap whole sale; we can't do this if
     690             :                          * there are nils in the left column, and we
     691             :                          * won't do it if the left is much smaller than
     692             :                          * the right and the right is writable (meaning
     693             :                          * we have to actually copy the right string
     694             :                          * heap) */
     695       18552 :                         tpe = r1i.width == 1 ? TYPE_bte : (r1i.width == 2 ? TYPE_sht : (r1i.width == 4 ? TYPE_int : TYPE_lng));
     696             :                         stringtrick = true;
     697        1481 :                 } else if (li.nonil &&
     698           0 :                            r2 != NULL &&
     699           0 :                            (r1i.vh == r2i.vh ||
     700           0 :                             (!GDK_ELIMDOUBLES(r1i.vh) /* && size tests */))) {
     701             :                         /* r1 and r2 may explicitly share their vheap,
     702             :                          * if they do, the result will also share the
     703             :                          * vheap; this also means that for this case we
     704             :                          * don't care about duplicate elimination: it
     705             :                          * will remain the same */
     706           0 :                         bn = project_str(&li, lci, tpe, &r1i, &r2i, timeoffset, t0);
     707           0 :                         bat_iterator_end(&li);
     708           0 :                         bat_iterator_end(&r1i);
     709           0 :                         bat_iterator_end(&r2i);
     710           0 :                         return bn;
     711             :                 }
     712      163220 :         } else if (ATOMvarsized(tpe) &&
     713         146 :                    li.nonil &&
     714         146 :                    r2 == NULL &&
     715         146 :                    (r1i.count == 0 ||
     716         146 :                     lcount > (r1i.count >> 3) ||
     717           0 :                     r1i.restricted == BAT_READ)) {
     718         146 :                 tpe = r1i.width == 4 ? TYPE_int : TYPE_lng;
     719             :                 stringtrick = true;
     720      163074 :         } else if (tpe == TYPE_msk || mask_cand(r1)) {
     721           8 :                 r1 = BATunmask(r1);
     722           8 :                 if (r1 == NULL)
     723           0 :                         goto doreturn;
     724           8 :                 if (r2) {
     725           0 :                         r2 = BATunmask(r2);
     726           0 :                         if (r2 == NULL)
     727           0 :                                 goto doreturn;
     728             :                 }
     729           8 :                 tpe = TYPE_oid;
     730           8 :                 bat_iterator_end(&r1i);
     731           8 :                 bat_iterator_end(&r2i);
     732           8 :                 r1i = bat_iterator(r1);
     733           8 :                 r2i = bat_iterator(r2);
     734             :         }
     735      355411 :         bn = COLnew2(l->hseqbase, ATOMtype(r1i.type), lcount, TRANSIENT, stringtrick ? r1i.width : 0);
     736      183238 :         if (bn == NULL) {
     737           0 :                 goto doreturn;
     738             :         }
     739      183238 :         bn->tnil = false;
     740      183238 :         if (r2) {
     741           0 :                 bn->tnonil = li.nonil & r1i.nonil & r2i.nonil;
     742           0 :                 bn->tsorted = li.count <= 1;
     743           0 :                 bn->trevsorted = li.count <= 1;
     744           0 :                 bn->tkey = li.count <= 1;
     745             :         } else {
     746      183256 :                 bn->tnonil = li.nonil & r1i.nonil;
     747      366512 :                 bn->tsorted = li.count <= 1
     748      182941 :                         || (li.sorted & r1i.sorted)
     749      130690 :                         || (li.revsorted & r1i.revsorted)
     750      313411 :                         || r1i.count <= 1;
     751      366512 :                 bn->trevsorted = li.count <= 1
     752      182936 :                         || (li.sorted & r1i.revsorted)
     753      168181 :                         || (li.revsorted & r1i.sorted)
     754      350231 :                         || r1i.count <= 1;
     755      214403 :                 bn->tkey = li.count <= 1 || (li.key & r1i.key);
     756             :         }
     757             : 
     758      183238 :         if (!stringtrick && tpe != TYPE_oid)
     759      145985 :                 tpe = ATOMbasetype(tpe);
     760      183238 :         switch (tpe) {
     761       12463 :         case TYPE_bte:
     762       12463 :                 res = project_bte(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     763       12463 :                 break;
     764       14975 :         case TYPE_sht:
     765       14975 :                 res = project_sht(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     766       14975 :                 break;
     767      119810 :         case TYPE_int:
     768      119810 :                 res = project_int(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     769      119810 :                 break;
     770          79 :         case TYPE_flt:
     771          79 :                 res = project_flt(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     772          79 :                 break;
     773        3681 :         case TYPE_dbl:
     774        3681 :                 res = project_dbl(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     775        3681 :                 break;
     776       10381 :         case TYPE_lng:
     777       10381 :                 res = project_lng(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     778       10381 :                 break;
     779             : #ifdef HAVE_HGE
     780        1766 :         case TYPE_hge:
     781        1766 :                 res = project_hge(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     782        1766 :                 break;
     783             : #endif
     784       18568 :         case TYPE_oid:
     785       18568 :                 res = project_oid(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     786       18568 :                 break;
     787          18 :         case TYPE_uuid:
     788          18 :                 res = project_uuid(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     789          18 :                 break;
     790        1497 :         default:
     791        1497 :                 res = project_any(bn, &li, lci, &r1i, r2 ? &r2i : NULL, timeoffset);
     792        1497 :                 break;
     793             :         }
     794             : 
     795      183211 :         if (res != GDK_SUCCEED)
     796           0 :                 goto bailout;
     797             : 
     798             :         /* handle string trick */
     799      183218 :         if (stringtrick) {
     800       18695 :                 assert(r1i.vh);
     801       18695 :                 if (r1i.restricted == BAT_READ || VIEWvtparent(r1)) {
     802             :                         /* really share string heap */
     803       15556 :                         assert(r1i.vh->parentid > 0);
     804             :                         /* there is no file, so we don't need to remove it */
     805       15556 :                         HEAPdecref(bn->tvheap, false);
     806       15558 :                         bn->tvheap = r1i.vh;
     807       15558 :                         HEAPincref(r1i.vh);
     808       15558 :                         assert(bn->tvheap->parentid != bn->batCacheid);
     809       15558 :                         BBPretain(bn->tvheap->parentid);
     810             :                 } else {
     811             :                         /* make copy of string heap */
     812        3139 :                         bn->tvheap->parentid = bn->batCacheid;
     813        3139 :                         bn->tvheap->farmid = BBPselectfarm(bn->batRole, otpe, varheap);
     814        3139 :                         strconcat_len(bn->tvheap->filename,
     815             :                                       sizeof(bn->tvheap->filename),
     816        3139 :                                       BBP_physical(bn->batCacheid), ".theap",
     817             :                                       NULL);
     818        3138 :                         if (HEAPcopy(bn->tvheap, r1i.vh, 0) != GDK_SUCCEED)
     819           0 :                                 goto bailout;
     820             :                 }
     821       18692 :                 bn->ttype = r1i.type;
     822       18692 :                 bn->twidth = r1i.width;
     823       18692 :                 bn->tshift = r1i.shift;
     824             :         }
     825             : 
     826      183215 :         if (!BATtdensebi(&r1i) || (r2 && !BATtdensebi(&r2i)))
     827      171765 :                 BATtseqbase(bn, oid_nil);
     828             : 
     829       11450 :   doreturn:
     830     1403525 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT " r1=" ALGOBATFMT " r2=" ALGOOPTBATFMT
     831             :                   " -> " ALGOOPTBATFMT "%s%s " LLFMT "us\n",
     832             :                   ALGOBATPAR(l), ALGOBATPAR(or1), ALGOOPTBATPAR(or2),
     833             :                   ALGOOPTBATPAR(bn),
     834             :                   bn && bn->ttype == TYPE_str && bn->tvheap == r1i.vh ? " sharing string heap" : "",
     835             :                   msg, GDKusec() - t0);
     836     1403525 :         bat_iterator_end(&li);
     837     1403431 :         bat_iterator_end(&r1i);
     838     1403361 :         bat_iterator_end(&r2i);
     839     1403150 :         if (l != ol)
     840           0 :                 BBPreclaim(l);
     841     1403305 :         if (r1 != or1)
     842           8 :                 BBPreclaim(r1);
     843     1403288 :         if (r2 != or2)
     844           0 :                 BBPreclaim(r2);
     845             :         return bn;
     846             : 
     847           0 :   bailout:
     848           0 :         BBPreclaim(bn);
     849           0 :         bn = NULL;
     850           0 :         goto doreturn;
     851             : }
     852             : 
     853             : BAT *
     854      390589 : BATproject(BAT *restrict l, BAT *restrict r)
     855             : {
     856      390589 :         return BATproject2(l, r, NULL);
     857             : }
     858             : 
     859             : /* Calculate a chain of BATproject calls.
     860             :  * The argument is a NULL-terminated array of BAT pointers.
     861             :  * This function is equivalent (apart from reference counting) to a
     862             :  * sequence of calls
     863             :  * bn = BATproject(bats[0], bats[1]);
     864             :  * bn = BATproject(bn, bats[2]);
     865             :  * ...
     866             :  * bn = BATproject(bn, bats[n-1]);
     867             :  * return bn;
     868             :  * where none of the intermediates are actually produced (and bats[n]==NULL).
     869             :  * Note that all BATs except the last must have type oid/void or msk.
     870             :  *
     871             :  * We assume that all but the last BAT in the chain is temporary and
     872             :  * therefore there is no chance that another thread will modify it while
     873             :  * we're busy.  This is not necessarily the case for that last BAT, so
     874             :  * it uses a BAT iterator.
     875             :  */
     876             : BAT *
     877      306857 : BATprojectchain(BAT **bats)
     878             : {
     879      306857 :         struct ba {
     880             :                 BAT *b;
     881             :                 oid hlo;
     882             :                 oid hhi;
     883             :                 BUN cnt;
     884             :                 oid *t;
     885             :                 struct canditer ci; /* used if .ci.s != NULL */
     886             :         } *ba;
     887      306857 :         BAT **tobedeleted = NULL;
     888      306857 :         int ndelete = 0;
     889      306857 :         int n, i;
     890      306857 :         BAT *b = NULL, *bn = NULL;
     891      306857 :         BATiter bi;
     892      306857 :         bool allnil = false;
     893      306857 :         bool issorted = true;
     894      306857 :         bool nonil = true;
     895      306857 :         bool stringtrick = false;
     896      306857 :         const void *nil;
     897      306857 :         int tpe;
     898      306857 :         lng t0 = 0;
     899             : 
     900      306857 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
     901             : 
     902      306857 :         lng timeoffset = 0;
     903      306857 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
     904      306815 :         if (qry_ctx != NULL) {
     905      304700 :                 timeoffset = (qry_ctx->starttime && qry_ctx->querytimeout) ? (qry_ctx->starttime + qry_ctx->querytimeout) : 0;
     906             :         }
     907             : 
     908             :         /* count number of participating BATs and allocate some
     909             :          * temporary work space */
     910     3301571 :         for (n = 0; bats[n]; n++) {
     911     2994709 :                 b = bats[n];
     912     2994709 :                 ndelete += (b->ttype == TYPE_msk || mask_cand(b));
     913     2994709 :                 TRC_DEBUG(ALGO, "arg %d: " ALGOBATFMT "\n",
     914             :                           n + 1, ALGOBATPAR(b));
     915             :         }
     916      306862 :         if (n == 0) {
     917           0 :                 GDKerror("must have BAT arguments\n");
     918           0 :                 return NULL;
     919             :         }
     920      306862 :         if (n == 1) {
     921           0 :                 bn = COLcopy(b, b->ttype, true, TRANSIENT);
     922           0 :                 TRC_DEBUG(ALGO, "single bat: copy -> " ALGOOPTBATFMT
     923             :                           " " LLFMT " usec\n",
     924             :                           ALGOOPTBATPAR(bn), GDKusec() - t0);
     925           0 :                 return bn;
     926             :         }
     927             : 
     928      306862 :         if (ndelete > 0 &&
     929       17095 :             (tobedeleted = GDKmalloc(sizeof(BAT *) * ndelete)) == NULL)
     930             :                 return NULL;
     931      306860 :         ba = GDKmalloc(sizeof(*ba) * n);
     932      306773 :         if (ba == NULL) {
     933           0 :                 GDKfree(tobedeleted);
     934           0 :                 return NULL;
     935             :         }
     936             : 
     937             :         ndelete = 0;
     938     3294196 :         for (n = 0, i = 0; bats[n]; n++) {
     939     2987384 :                 b = bats[n];
     940     2987384 :                 if (b->ttype == TYPE_msk || mask_cand(b)) {
     941       17160 :                         if ((b = BATunmask(b)) == NULL) {
     942           0 :                                 goto bunins_failed;
     943             :                         }
     944       17095 :                         tobedeleted[ndelete++] = b;
     945             :                 }
     946     2987319 :                 if (bats[n+1] && BATtdense(b) && b->hseqbase == b->tseqbase && b->tseqbase == bats[n+1]->hseqbase && BATcount(b) == BATcount(bats[n+1]))
     947     2352901 :                         continue; /* skip dense bat */
     948      634418 :                 ba[i] = (struct ba) {
     949             :                         .b = b,
     950      634418 :                         .hlo = b->hseqbase,
     951      634418 :                         .hhi = b->hseqbase + b->batCount,
     952             :                         .cnt = b->batCount,
     953      634418 :                         .t = (oid *) Tloc(b, 0),
     954             :                 };
     955      634418 :                 allnil |= b->ttype == TYPE_void && is_oid_nil(b->tseqbase);
     956      634418 :                 issorted &= b->tsorted;
     957      634418 :                 if (bats[n + 1])
     958      327622 :                         nonil &= b->tnonil;
     959      634418 :                 if (b->tnonil && b->tkey && b->tsorted &&
     960      321499 :                     ATOMtype(b->ttype) == TYPE_oid) {
     961      293250 :                         canditer_init(&ba[i].ci, NULL, b);
     962             :                 }
     963      634522 :                 i++;
     964             :         }
     965      306812 :         n = i;
     966      306812 :         if (i<=2) {
     967      260853 :                 if (i == 1) {
     968       35820 :                         bn = ba[0].b;
     969       35820 :                         BBPfix(bn->batCacheid);
     970             :                 } else {
     971      225033 :                         bn = BATproject(ba[0].b, ba[1].b);
     972             :                 }
     973      260944 :                 while (ndelete-- > 0)
     974          90 :                         BBPunfix(tobedeleted[ndelete]->batCacheid);
     975      260854 :                 GDKfree(tobedeleted);
     976      260833 :                 GDKfree(ba);
     977      260833 :                 return bn;
     978             :         }
     979             :         /* b is last BAT in bats array */
     980       45959 :         tpe = ATOMtype(b->ttype);
     981       45959 :         nil = ATOMnilptr(tpe);
     982       45959 :         if (allnil || ba[0].cnt == 0) {
     983        6607 :                 bn = BATconstant(ba[0].hlo, tpe == TYPE_oid ? TYPE_void : tpe,
     984             :                                  nil, ba[0].cnt, TRANSIENT);
     985        6607 :                 while (ndelete-- > 0)
     986        7502 :                         BBPreclaim(tobedeleted[ndelete]);
     987        6611 :                 GDKfree(tobedeleted);
     988        6610 :                 GDKfree(ba);
     989        6610 :                 TRC_DEBUG(ALGO, "with %d bats: nil/empty -> " ALGOOPTBATFMT
     990             :                           " " LLFMT " usec\n",
     991             :                           n, ALGOOPTBATPAR(bn), GDKusec() - t0);
     992        6610 :                 return bn;
     993             :         }
     994             : 
     995       39352 :         bi = bat_iterator(b);
     996       39358 :         if (nonil && ATOMstorage(tpe) == TYPE_str && bi.restricted == BAT_READ) {
     997        4877 :                 stringtrick = true;
     998        4877 :                 bn = COLnew2(ba[0].hlo, tpe, ba[0].cnt, TRANSIENT, bi.width);
     999        4877 :                 if (bn && bn->tvheap) {
    1000             :                         /* no need to remove any files since they were
    1001             :                          * never created for this bat */
    1002        4877 :                         HEAPdecref(bn->tvheap, false);
    1003        4878 :                         bn->tvheap = NULL;
    1004             :                 }
    1005        4878 :                 tpe = bi.width == 1 ? TYPE_bte : (bi.width == 2 ? TYPE_sht : (bi.width == 4 ? TYPE_int : TYPE_lng));
    1006             :         } else {
    1007       34481 :                 bn = COLnew(ba[0].hlo, tpe, ba[0].cnt, TRANSIENT);
    1008             :         }
    1009       39358 :         if (bn == NULL) {
    1010           0 :                 bat_iterator_end(&bi);
    1011           0 :                 goto bunins_failed;
    1012             :         }
    1013             : 
    1014       39358 :         assert(ba[n - 1].b == b);
    1015       39358 :         ba[n - 1].t = bi.base;
    1016       39358 :         if (ATOMtype(b->ttype) == TYPE_oid) {
    1017             :                 /* oid all the way */
    1018         539 :                 oid *d = (oid *) Tloc(bn, 0);
    1019         539 :                 assert(!stringtrick);
    1020     7215040 :                 TIMEOUT_LOOP_IDX_DECL(p, ba[0].cnt, timeoffset) {
    1021     7212762 :                         oid o = ba[0].ci.s ? canditer_next(&ba[0].ci) : ba[0].t[p];
    1022    28326133 :                         for (int i = 1; i < n; i++) {
    1023    21113371 :                                 if (is_oid_nil(o)) {
    1024           0 :                                         bn->tnil = true;
    1025           0 :                                         break;
    1026             :                                 }
    1027    21113371 :                                 if (o < ba[i].hlo || o >= ba[i].hhi) {
    1028           0 :                                         GDKerror("does not match always\n");
    1029           0 :                                         bat_iterator_end(&bi);
    1030           0 :                                         goto bunins_failed;
    1031             :                                 }
    1032    21113371 :                                 o -= ba[i].hlo;
    1033    21109396 :                                 o = ba[i].ci.s ?
    1034    12421434 :                                     (ba[i].ci.tpe == cand_dense) ?
    1035    12421434 :                                         canditer_idx_dense(&ba[i].ci, o) :
    1036    31364221 :                                         canditer_idx(&ba[i].ci, o) : ba[i].t[o];
    1037             :                         }
    1038     7212762 :                         *d++ = o;
    1039             :                 }
    1040       38819 :         } else if (!ATOMvarsized(tpe)) {
    1041       38565 :                 const void *v;
    1042       38565 :                 char *d = Tloc(bn, 0);
    1043             : 
    1044       38565 :                 bn->tnil = false;
    1045       38565 :                 n--;    /* stop one before the end, also ba[n] is last */
    1046   117396926 :                 TIMEOUT_LOOP_IDX_DECL(p, ba[0].cnt, timeoffset) {
    1047   117268666 :                         oid o = ba[0].ci.s ? canditer_next(&ba[0].ci) : ba[0].t[p];
    1048             : 
    1049   278995754 :                         for (int i = 1; i < n; i++) {
    1050   161866382 :                                 if (is_oid_nil(o)) {
    1051          46 :                                         bn->tnil = true;
    1052          46 :                                         break;
    1053             :                                 }
    1054   161866336 :                                 if (o < ba[i].hlo || o >= ba[i].hhi) {
    1055           0 :                                         GDKerror("does not match always\n");
    1056           0 :                                         bat_iterator_end(&bi);
    1057           0 :                                         goto bunins_failed;
    1058             :                                 }
    1059   161866336 :                                 o -= ba[i].hlo;
    1060   161745092 :                                 o = ba[i].ci.s ?
    1061    70380447 :                                     (ba[i].ci.tpe == cand_dense) ?
    1062    70380447 :                                         canditer_idx_dense(&ba[i].ci, o) :
    1063   217913425 :                                         canditer_idx(&ba[i].ci, o) : ba[i].t[o];
    1064             :                         }
    1065   117129418 :                         if (is_oid_nil(o)) {
    1066          46 :                                 assert(!stringtrick);
    1067          46 :                                 bn->tnil = true;
    1068          46 :                                 v = nil;
    1069   117129372 :                         } else if (o < ba[n].hlo || o >= ba[n].hhi) {
    1070           0 :                                 GDKerror("does not match always\n");
    1071           0 :                                 bat_iterator_end(&bi);
    1072           0 :                                 goto bunins_failed;
    1073             :                         } else {
    1074   117129372 :                                 o -= ba[n].hlo;
    1075   117129372 :                                 v = (const char *) bi.base + (o << bi.shift);
    1076             :                         }
    1077   117129418 :                         if (ATOMputFIX(tpe, d, v) != GDK_SUCCEED) {
    1078           0 :                                 bat_iterator_end(&bi);
    1079           0 :                                 goto bunins_failed;
    1080             :                         }
    1081   117268645 :                         d += bi.width;
    1082             :                 }
    1083       38539 :                 if (stringtrick) {
    1084        4874 :                         bn->tnil = false;
    1085        4874 :                         bn->tnonil = b->tnonil;
    1086        4874 :                         bn->tkey = false;
    1087        4874 :                         assert(bn->tvheap == NULL);
    1088        4874 :                         bn->tvheap = bi.vh;
    1089        4874 :                         HEAPincref(bi.vh);
    1090        4878 :                         assert(bn->tvheap->parentid != bn->batCacheid);
    1091        4878 :                         BBPretain(bn->tvheap->parentid);
    1092        4878 :                         assert(bn->ttype == b->ttype);
    1093        4878 :                         assert(bn->twidth == bi.width);
    1094        4878 :                         assert(bn->tshift == bi.shift);
    1095             :                 }
    1096             :                 n++;            /* undo for debug print */
    1097             :         } else {
    1098         254 :                 const void *v;
    1099             : 
    1100         254 :                 assert(!stringtrick);
    1101         254 :                 bn->tnil = false;
    1102         254 :                 n--;    /* stop one before the end, also ba[n] is last */
    1103      695705 :                 TIMEOUT_LOOP_IDX_DECL(p, ba[0].cnt, timeoffset) {
    1104      694903 :                         oid o = ba[0].ci.s ? canditer_next(&ba[0].ci) : ba[0].t[p];
    1105     1397707 :                         for (int i = 1; i < n; i++) {
    1106      702966 :                                 if (is_oid_nil(o)) {
    1107           0 :                                         bn->tnil = true;
    1108           0 :                                         break;
    1109             :                                 }
    1110      702966 :                                 if (o < ba[i].hlo || o >= ba[i].hhi) {
    1111           0 :                                         GDKerror("does not match always\n");
    1112           0 :                                         bat_iterator_end(&bi);
    1113           0 :                                         goto bunins_failed;
    1114             :                                 }
    1115      702966 :                                 o -= ba[i].hlo;
    1116      702929 :                                 o = ba[i].ci.s ?
    1117       19161 :                                     (ba[i].ci.tpe == cand_dense) ?
    1118       19161 :                                         canditer_idx_dense(&ba[i].ci, o) :
    1119      721556 :                                         canditer_idx(&ba[i].ci, o) : ba[i].t[o];
    1120             :                         }
    1121      694741 :                         if (is_oid_nil(o)) {
    1122           0 :                                 bn->tnil = true;
    1123           0 :                                 v = nil;
    1124      694741 :                         } else if (o < ba[n].hlo || o >= ba[n].hhi) {
    1125           0 :                                 GDKerror("does not match always\n");
    1126           0 :                                 bat_iterator_end(&bi);
    1127           0 :                                 goto bunins_failed;
    1128             :                         } else {
    1129      694741 :                                 o -= ba[n].hlo;
    1130      694741 :                                 v = BUNtail(bi, o);
    1131             :                         }
    1132      694732 :                         if (bunfastapp(bn, v) != GDK_SUCCEED) {
    1133           0 :                                 bat_iterator_end(&bi);
    1134           0 :                                 goto bunins_failed;
    1135             :                         }
    1136             :                 }
    1137             :                 n++;            /* undo for debug print */
    1138             :         }
    1139       39336 :         bat_iterator_end(&bi);
    1140       39354 :         TIMEOUT_CHECK(timeoffset, GOTO_LABEL_TIMEOUT_HANDLER(bunins_failed));
    1141       39354 :         BATsetcount(bn, ba[0].cnt);
    1142       39352 :         bn->tsorted = (ba[0].cnt <= 1) | issorted;
    1143       39352 :         bn->trevsorted = ba[0].cnt <= 1;
    1144       39352 :         bn->tnonil = nonil & b->tnonil;
    1145       39352 :         bn->tseqbase = oid_nil;
    1146             :         /* note, b may point to one of the bats in tobedeleted, so
    1147             :          * reclaim after the last use of b */
    1148       39352 :         while (ndelete-- > 0)
    1149       55465 :                 BBPreclaim(tobedeleted[ndelete]);
    1150       39352 :         GDKfree(tobedeleted);
    1151       39355 :         GDKfree(ba);
    1152       39360 :         TRC_DEBUG(ALGO, "with %d bats: " ALGOOPTBATFMT " " LLFMT " usec\n",
    1153             :                   n, ALGOOPTBATPAR(bn), GDKusec() - t0);
    1154             :         return bn;
    1155             : 
    1156           0 :   bunins_failed:
    1157           0 :         while (ndelete-- > 0)
    1158           0 :                 BBPreclaim(tobedeleted[ndelete]);
    1159           0 :         GDKfree(tobedeleted);
    1160           0 :         GDKfree(ba);
    1161           0 :         BBPreclaim(bn);
    1162           0 :         TRC_DEBUG(ALGO, "failed " LLFMT "usec\n", GDKusec() - t0);
    1163             :         return NULL;
    1164             : }

Generated by: LCOV version 1.14