LCOV - code coverage report
Current view: top level - gdk - gdk_project.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 397 716 55.4 %
Date: 2024-12-19 20:05:57 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, QryCtx *qry_ctx)                 \
      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, qry_ctx)             \
      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, qry_ctx) {           \
      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(qry_ctx, TIMEOUT_HANDLER(GDK_FAIL, qry_ctx));     \
      65             :         BATsetcount(bn, lo);                                            \
      66             :         return GDK_SUCCEED;                                             \
      67             : }
      68             : 
      69             : /* project type switch */
      70    66246495 : project1_loop(bte)
      71    32214540 : project1_loop(sht)
      72   738382238 : project1_loop(int)
      73        1721 : project1_loop(flt)
      74      971830 : project1_loop(dbl)
      75   198286620 : project1_loop(lng)
      76             : #ifdef HAVE_HGE
      77    76100846 : 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             :                QryCtx *qry_ctx)                                         \
      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, qry_ctx);           \
     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, qry_ctx) {           \
     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, qry_ctx) {           \
     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, qry_ctx) {           \
     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(qry_ctx, TIMEOUT_HANDLER(GDK_FAIL, qry_ctx));     \
     159             :         BATsetcount(bn, lo);                                            \
     160             :         return GDK_SUCCEED;                                             \
     161             : }
     162             : 
     163             : 
     164             : /* project type switch */
     165      338898 : project_loop(bte)
     166      763975 : project_loop(sht)
     167    64736385 : project_loop(int)
     168         100 : project_loop(flt)
     169        3784 : project_loop(dbl)
     170       24285 : project_loop(lng)
     171             : #ifdef HAVE_HGE
     172     1221924 : project_loop(hge)
     173             : #endif
     174          20 : project_loop(uuid)
     175             : 
     176             : static gdk_return
     177       18728 : project_oid(BAT *restrict bn, BATiter *restrict li,
     178             :             struct canditer *restrict lci,
     179             :             BATiter *restrict r1i, BATiter *restrict r2i, QryCtx *qry_ctx)
     180             : {
     181       18728 :         BUN lo;
     182       18728 :         oid *restrict bt;
     183       18728 :         oid r1seq, r1end;
     184       18728 :         oid r2seq, r2end;
     185       18728 :         const oid *restrict r1t = NULL;
     186       18728 :         const oid *restrict r2t = NULL;
     187       18728 :         struct canditer r1ci = {0}, r2ci = {0};
     188             : 
     189       18728 :         if ((!lci || (lci->tpe == cand_dense && BATtdensebi(li))) && r1i->type && !BATtdensebi(r1i) && !r2i && li->nonil) {
     190        7222 :                 if (sizeof(oid) == sizeof(lng))
     191        7222 :                         return project1_lng(bn, li, r1i, qry_ctx);
     192             :                 else
     193             :                         return project1_int(bn, li, r1i, qry_ctx);
     194             :         }
     195       11506 :         MT_thread_setalgorithm(__func__);
     196       11505 :         if (complex_cand(r1i->b))
     197          27 :                 canditer_init(&r1ci, NULL, r1i->b);
     198       11478 :         else if (!BATtdensebi(r1i))
     199           3 :                 r1t = (const oid *) r1i->base;
     200       11505 :         r1seq = r1i->b->hseqbase;
     201       11505 :         r1end = r1seq + r1i->count;
     202       11505 :         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       11505 :         bt = (oid *) Tloc(bn, 0);
     213       11505 :         if (lci) {
     214    61755611 :                 TIMEOUT_LOOP_IDX(lo, lci->ncand, qry_ctx) {
     215    61751705 :                         oid o = canditer_next(lci);
     216    61751705 :                         if (o < r1seq || o >= r2end) {
     217           0 :                                 goto nomatch;
     218             :                         }
     219    61751705 :                         if (o < r1end) {
     220    61751705 :                                 if (r1ci.s)
     221           0 :                                         bt[lo] = canditer_idx(&r1ci, o - r1seq);
     222    61751705 :                                 else if (r1t)
     223         179 :                                         bt[lo] = r1t[o - r1seq];
     224             :                                 else
     225    61751526 :                                         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       11410 :         } else if (BATtdensebi(li)) {
     236           0 :                 TIMEOUT_LOOP_IDX(lo, li->count, qry_ctx) {
     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       11410 :                 const oid *ot = (const oid *) li->base;
     259   178900658 :                 TIMEOUT_LOOP_IDX(lo, li->count, qry_ctx) {
     260   178866270 :                         oid o = ot[lo];
     261   178866270 :                         if (is_oid_nil(o)) {
     262           0 :                                 bt[lo] = oid_nil;
     263           0 :                                 bn->tnonil = false;
     264           0 :                                 bn->tnil = true;
     265   178866270 :                         } else if (o < r1seq || o >= r2end) {
     266           0 :                                 goto nomatch;
     267   178866270 :                         } else if (o < r1end) {
     268   178866270 :                                 if (r1ci.s)
     269        5778 :                                         bt[lo] = canditer_idx(&r1ci, o - r1seq);
     270   178860492 :                                 else if (r1t)
     271           0 :                                         bt[lo] = r1t[o - r1seq];
     272             :                                 else
     273   178860492 :                                         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       11506 :         TIMEOUT_CHECK(qry_ctx, TIMEOUT_HANDLER(GDK_FAIL, qry_ctx));
     285       11506 :         BATsetcount(bn, lo);
     286       11506 :         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        2788 : project_any(BAT *restrict bn, BATiter *restrict li,
     294             :             struct canditer *restrict ci,
     295             :             BATiter *restrict r1i, BATiter *restrict r2i, QryCtx *qry_ctx)
     296             : {
     297        2788 :         BUN lo;
     298        2788 :         const void *nil = ATOMnilptr(r1i->type);
     299        2788 :         const void *v;
     300        2788 :         oid r1seq, r1end;
     301        2788 :         oid r2seq, r2end;
     302             : 
     303        2788 :         MT_thread_setalgorithm(__func__);
     304        2788 :         r1seq = r1i->b->hseqbase;
     305        2788 :         r1end = r1seq + r1i->count;
     306        2788 :         if (r2i) {
     307           0 :                 r2seq = r2i->b->hseqbase;
     308           0 :                 r2end = r2seq + r2i->count;
     309             :         } else {
     310             :                 r2seq = r2end = r1end;
     311             :         }
     312        2788 :         if (ci) {
     313           0 :                 TIMEOUT_LOOP_IDX(lo, ci->ncand, qry_ctx) {
     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        2788 :         } else if (BATtdensebi(li)) {
     328           0 :                 TIMEOUT_LOOP_IDX(lo, li->count, qry_ctx) {
     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        2788 :                 const oid *restrict ot = (const oid *) li->base;
     344             : 
     345       58790 :                 TIMEOUT_LOOP_IDX(lo, li->count, qry_ctx) {
     346       53216 :                         oid o = ot[lo];
     347       53216 :                         if (is_oid_nil(o)) {
     348           2 :                                 v = nil;
     349           2 :                                 bn->tnil = true;
     350       53214 :                         } else if (o < r1seq || o >= r2end) {
     351           0 :                                 GDKerror("does not match always\n");
     352           0 :                                 return GDK_FAIL;
     353       53214 :                         } else if (o < r1end) {
     354       53214 :                                 v = BUNtail(*r1i, o - r1seq);
     355             :                         } else {
     356           0 :                                 v = BUNtail(*r2i, o - r2seq);
     357             :                         }
     358       53200 :                         if (tfastins_nocheck(bn, lo, v) != GDK_SUCCEED) {
     359             :                                 return GDK_FAIL;
     360             :                         }
     361             :                 }
     362             :         }
     363        2787 :         TIMEOUT_CHECK(qry_ctx, TIMEOUT_HANDLER(GDK_FAIL, qry_ctx));
     364        2787 :         BATsetcount(bn, lo);
     365        2786 :         bn->theap->dirty = true;
     366        2786 :         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             :             QryCtx *qry_ctx, 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, qry_ctx) {
     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, qry_ctx) {
     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, qry_ctx) {
     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(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
     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     1643871 : BATproject2(BAT *restrict l, BAT *restrict r1, BAT *restrict r2)
     588             : {
     589     1643871 :         BAT *bn = NULL;
     590     1643871 :         BAT *or1 = r1, *or2 = r2, *ol = l;
     591     1643871 :         oid lo, hi;
     592     1643871 :         gdk_return res;
     593     1643871 :         int tpe = ATOMtype(r1->ttype), otpe = tpe;
     594     1643871 :         bool stringtrick = false;
     595     1643871 :         struct canditer ci, *lci = NULL;
     596     1643871 :         const char *msg = "";
     597     1643871 :         lng t0 = 0;
     598     1643871 :         BATiter li = bat_iterator(l);
     599     1643332 :         BATiter r1i = bat_iterator(r1);
     600     1643269 :         BATiter r2i = bat_iterator(r2);
     601     1643858 :         BUN lcount = li.count;
     602             : 
     603     1643858 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
     604             : 
     605     1643858 :         assert(ATOMtype(li.type) == TYPE_oid || li.type == TYPE_msk);
     606     1643858 :         assert(r2 == NULL || tpe == ATOMtype(r2i.type));
     607           0 :         assert(r2 == NULL || r1->hseqbase + r1i.count == r2->hseqbase);
     608             : 
     609     1643858 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
     610             : 
     611     1643526 :         if (r2 && r1i.count == 0) {
     612             :                 /* unlikely special case: r1 is empty, so we just have r2 */
     613           0 :                 r1 = r2;
     614           0 :                 r2 = NULL;
     615           0 :                 bat_iterator_end(&r1i);
     616           0 :                 r1i = r2i;
     617           0 :                 r2i = bat_iterator(NULL);
     618             :         }
     619             : 
     620     1643526 :         if (BATtdensebi(&li) && lcount > 0) {
     621      598691 :                 lo = l->tseqbase;
     622      598691 :                 hi = l->tseqbase + lcount;
     623      598691 :                 if (lo >= r1->hseqbase && hi <= r1->hseqbase + r1i.count) {
     624      598691 :                         bn = BATslice(r1, lo - r1->hseqbase, hi - r1->hseqbase);
     625      598714 :                         BAThseqbase(bn, l->hseqbase);
     626      598772 :                         msg = " (slice)";
     627      598772 :                         goto doreturn;
     628             :                 }
     629           0 :                 if (lo < r1->hseqbase || r2 == NULL || hi > r2->hseqbase + r2i.count) {
     630           0 :                         GDKerror("does not match always\n");
     631           0 :                         bat_iterator_end(&li);
     632           0 :                         bat_iterator_end(&r1i);
     633           0 :                         bat_iterator_end(&r2i);
     634           0 :                         return NULL;
     635             :                 }
     636           0 :                 if (lo >= r2->hseqbase) {
     637           0 :                         bn = BATslice(r2, lo - r2->hseqbase, hi - r2->hseqbase);
     638           0 :                         BAThseqbase(bn, l->hseqbase);
     639           0 :                         msg = " (slice2)";
     640           0 :                         goto doreturn;
     641             :                 }
     642             :         }
     643     1044835 :         if (complex_cand(l)) {
     644             :                 /* l is candidate list with exceptions or is a bitmask */
     645       13983 :                 assert(li.type == TYPE_msk || !is_oid_nil(l->tseqbase));
     646       13983 :                 canditer_init(&ci, NULL, l);
     647       13984 :                 lcount = ci.ncand;
     648       13984 :                 lci = &ci;
     649     1030852 :         } else if (li.type == TYPE_msk) {
     650           0 :                 l = BATunmask(l);
     651           0 :                 if (l == NULL)
     652           0 :                         goto doreturn;
     653           0 :                 if (complex_cand(l)) {
     654           0 :                         canditer_init(&ci, NULL, l);
     655           0 :                         lcount = ci.ncand;
     656           0 :                         lci = &ci;
     657             :                 }
     658             :         }
     659     1044836 :         if (lcount == 0 ||
     660       14008 :             (li.type == TYPE_void && is_oid_nil(l->tseqbase)) ||
     661      208743 :             (r1i.type == TYPE_void && is_oid_nil(r1->tseqbase) &&
     662           0 :              (r2 == NULL ||
     663           0 :               (r2i.type == TYPE_void && is_oid_nil(r2->tseqbase))))) {
     664             :                 /* trivial: all values are nil (includes no entries at all) */
     665      836093 :                 const void *nil = r1i.type == TYPE_msk ? &oid_nil : ATOMnilptr(r1i.type);
     666             : 
     667     1665617 :                 bn = BATconstant(l->hseqbase, r1i.type == TYPE_oid || r1i.type == TYPE_msk ? TYPE_void : r1i.type,
     668             :                                  nil, lcount, TRANSIENT);
     669      836197 :                 if (bn != NULL &&
     670      836197 :                     ATOMtype(bn->ttype) == TYPE_oid &&
     671      127864 :                     BATcount(bn) == 0) {
     672      127865 :                         BATtseqbase(bn, 0);
     673             :                 }
     674      836196 :                 msg = " (constant)";
     675      836196 :                 goto doreturn;
     676             :         }
     677             : 
     678      208743 :         if (ATOMstorage(tpe) == TYPE_str) {
     679       27073 :                 if (li.nonil &&
     680       27071 :                     r2 == NULL &&
     681       27071 :                     (r1i.count == 0 ||
     682       27069 :                      lcount > (r1i.count >> 3) ||
     683        5745 :                      r1i.restricted == BAT_READ)) {
     684             :                         /* insert strings as ints, we need to copy the
     685             :                          * string heap whole sale; we can't do this if
     686             :                          * there are nils in the left column, and we
     687             :                          * won't do it if the left is much smaller than
     688             :                          * the right and the right is writable (meaning
     689             :                          * we have to actually copy the right string
     690             :                          * heap) */
     691       24301 :                         tpe = r1i.width == 1 ? TYPE_bte : (r1i.width == 2 ? TYPE_sht : (r1i.width == 4 ? TYPE_int : TYPE_lng));
     692             :                         stringtrick = true;
     693        2772 :                 } else if (li.nonil &&
     694           0 :                            r2 != NULL &&
     695           0 :                            (r1i.vh == r2i.vh ||
     696           0 :                             (!GDK_ELIMDOUBLES(r1i.vh) /* && size tests */))) {
     697             :                         /* r1 and r2 may explicitly share their vheap,
     698             :                          * if they do, the result will also share the
     699             :                          * vheap; this also means that for this case we
     700             :                          * don't care about duplicate elimination: it
     701             :                          * will remain the same */
     702           0 :                         bn = project_str(&li, lci, tpe, &r1i, &r2i, qry_ctx, t0);
     703           0 :                         bat_iterator_end(&li);
     704           0 :                         bat_iterator_end(&r1i);
     705           0 :                         bat_iterator_end(&r2i);
     706           0 :                         return bn;
     707             :                 }
     708      181670 :         } else if (ATOMvarsized(tpe) &&
     709         144 :                    li.nonil &&
     710         144 :                    r2 == NULL &&
     711         144 :                    (r1i.count == 0 ||
     712         144 :                     lcount > (r1i.count >> 3) ||
     713           0 :                     r1i.restricted == BAT_READ)) {
     714         144 :                 tpe = r1i.width == 4 ? TYPE_int : TYPE_lng;
     715             :                 stringtrick = true;
     716      181526 :         } else if (tpe == TYPE_msk || mask_cand(r1)) {
     717           8 :                 r1 = BATunmask(r1);
     718           8 :                 if (r1 == NULL)
     719           0 :                         goto doreturn;
     720           8 :                 if (r2) {
     721           0 :                         r2 = BATunmask(r2);
     722           0 :                         if (r2 == NULL)
     723           0 :                                 goto doreturn;
     724             :                 }
     725           8 :                 tpe = TYPE_oid;
     726           8 :                 bat_iterator_end(&r1i);
     727           8 :                 bat_iterator_end(&r2i);
     728           8 :                 r1i = bat_iterator(r1);
     729           8 :                 r2i = bat_iterator(r2);
     730             :         }
     731      406328 :         bn = COLnew2(l->hseqbase, ATOMtype(r1i.type), lcount, TRANSIENT, stringtrick ? r1i.width : 0);
     732      208749 :         if (bn == NULL) {
     733           0 :                 goto doreturn;
     734             :         }
     735      208749 :         bn->tnil = false;
     736      208749 :         if (r2) {
     737           0 :                 bn->tnonil = li.nonil & r1i.nonil & r2i.nonil;
     738           0 :                 bn->tsorted = li.count <= 1;
     739           0 :                 bn->trevsorted = li.count <= 1;
     740           0 :                 bn->tkey = li.count <= 1;
     741             :         } else {
     742      208758 :                 bn->tnonil = li.nonil & r1i.nonil;
     743      417516 :                 bn->tsorted = li.count <= 1
     744      208238 :                         || (li.sorted & r1i.sorted)
     745      154864 :                         || (li.revsorted & r1i.revsorted)
     746      363039 :                         || r1i.count <= 1;
     747      417516 :                 bn->trevsorted = li.count <= 1
     748      208243 :                         || (li.sorted & r1i.revsorted)
     749      193270 :                         || (li.revsorted & r1i.sorted)
     750      400841 :                         || r1i.count <= 1;
     751      241171 :                 bn->tkey = li.count <= 1 || (li.key & r1i.key);
     752             :         }
     753             : 
     754      208749 :         if (!stringtrick && tpe != TYPE_oid)
     755      165573 :                 tpe = ATOMbasetype(tpe);
     756      208749 :         switch (tpe) {
     757       17110 :         case TYPE_bte:
     758       17110 :                 res = project_bte(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     759       17110 :                 break;
     760       19299 :         case TYPE_sht:
     761       19299 :                 res = project_sht(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     762       19299 :                 break;
     763      138310 :         case TYPE_int:
     764      138310 :                 res = project_int(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     765      138310 :                 break;
     766          85 :         case TYPE_flt:
     767          85 :                 res = project_flt(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     768          85 :                 break;
     769        3675 :         case TYPE_dbl:
     770        3675 :                 res = project_dbl(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     771        3675 :                 break;
     772        8208 :         case TYPE_lng:
     773        8208 :                 res = project_lng(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     774        8208 :                 break;
     775             : #ifdef HAVE_HGE
     776         529 :         case TYPE_hge:
     777         529 :                 res = project_hge(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     778         529 :                 break;
     779             : #endif
     780       18728 :         case TYPE_oid:
     781       18728 :                 res = project_oid(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     782       18728 :                 break;
     783          17 :         case TYPE_uuid:
     784          17 :                 res = project_uuid(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     785          17 :                 break;
     786        2788 :         default:
     787        2788 :                 res = project_any(bn, &li, lci, &r1i, r2 ? &r2i : NULL, qry_ctx);
     788        2788 :                 break;
     789             :         }
     790             : 
     791      208710 :         if (res != GDK_SUCCEED)
     792          15 :                 goto bailout;
     793             : 
     794             :         /* handle string trick */
     795      208695 :         if (stringtrick) {
     796       24439 :                 assert(r1i.vh);
     797       24439 :                 if (r1i.restricted == BAT_READ || VIEWvtparent(r1)) {
     798             :                         /* really share string heap */
     799       20594 :                         assert(r1i.vh->parentid > 0);
     800             :                         /* there is no file, so we don't need to remove it */
     801       20594 :                         HEAPdecref(bn->tvheap, false);
     802       20595 :                         bn->tvheap = r1i.vh;
     803       20595 :                         HEAPincref(r1i.vh);
     804       20599 :                         assert(bn->tvheap->parentid != bn->batCacheid);
     805       20599 :                         BBPretain(bn->tvheap->parentid);
     806             :                 } else {
     807             :                         /* make copy of string heap */
     808        3845 :                         bn->tvheap->parentid = bn->batCacheid;
     809        3845 :                         bn->tvheap->farmid = BBPselectfarm(bn->batRole, otpe, varheap);
     810        3845 :                         strconcat_len(bn->tvheap->filename,
     811             :                                       sizeof(bn->tvheap->filename),
     812        3845 :                                       BBP_physical(bn->batCacheid), ".theap",
     813             :                                       NULL);
     814        3845 :                         if (HEAPcopy(bn->tvheap, r1i.vh, 0) != GDK_SUCCEED)
     815           0 :                                 goto bailout;
     816             :                 }
     817       24444 :                 bn->ttype = r1i.type;
     818       24444 :                 bn->twidth = r1i.width;
     819       24444 :                 bn->tshift = r1i.shift;
     820       24444 :                 bn->tascii = r1i.ascii;
     821             :         }
     822             : 
     823      208700 :         if (!BATtdensebi(&r1i) || (r2 && !BATtdensebi(&r2i)))
     824      197224 :                 BATtseqbase(bn, oid_nil);
     825             : 
     826       11476 :   doreturn:
     827     1643703 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT " r1=" ALGOBATFMT " r2=" ALGOOPTBATFMT
     828             :                   " -> " ALGOOPTBATFMT "%s%s " LLFMT "us\n",
     829             :                   ALGOBATPAR(l), ALGOBATPAR(or1), ALGOOPTBATPAR(or2),
     830             :                   ALGOOPTBATPAR(bn),
     831             :                   bn && bn->ttype == TYPE_str && bn->tvheap == r1i.vh ? " sharing string heap" : "",
     832             :                   msg, GDKusec() - t0);
     833     1643703 :         bat_iterator_end(&li);
     834     1643456 :         bat_iterator_end(&r1i);
     835     1643668 :         bat_iterator_end(&r2i);
     836     1643015 :         if (l != ol)
     837           0 :                 BBPreclaim(l);
     838     1643024 :         if (r1 != or1)
     839           8 :                 BBPreclaim(r1);
     840     1643014 :         if (r2 != or2)
     841           0 :                 BBPreclaim(r2);
     842             :         return bn;
     843             : 
     844          15 :   bailout:
     845          15 :         BBPreclaim(bn);
     846           0 :         bn = NULL;
     847           0 :         goto doreturn;
     848             : }
     849             : 
     850             : BAT *
     851      450379 : BATproject(BAT *restrict l, BAT *restrict r)
     852             : {
     853      450379 :         return BATproject2(l, r, NULL);
     854             : }
     855             : 
     856             : /* Calculate a chain of BATproject calls.
     857             :  * The argument is a NULL-terminated array of BAT pointers.
     858             :  * This function is equivalent (apart from reference counting) to a
     859             :  * sequence of calls
     860             :  * bn = BATproject(bats[0], bats[1]);
     861             :  * bn = BATproject(bn, bats[2]);
     862             :  * ...
     863             :  * bn = BATproject(bn, bats[n-1]);
     864             :  * return bn;
     865             :  * where none of the intermediates are actually produced (and bats[n]==NULL).
     866             :  * Note that all BATs except the last must have type oid/void or msk.
     867             :  *
     868             :  * We assume that all but the last BAT in the chain is temporary and
     869             :  * therefore there is no chance that another thread will modify it while
     870             :  * we're busy.  This is not necessarily the case for that last BAT, so
     871             :  * it uses a BAT iterator.
     872             :  */
     873             : BAT *
     874      363302 : BATprojectchain(BAT **bats)
     875             : {
     876      363302 :         struct ba {
     877             :                 BAT *b;
     878             :                 oid hlo;
     879             :                 oid hhi;
     880             :                 BUN cnt;
     881             :                 oid *t;
     882             :                 struct canditer ci; /* used if .ci.s != NULL */
     883             :         } *ba;
     884      363302 :         BAT **tobedeleted = NULL;
     885      363302 :         int ndelete = 0;
     886      363302 :         int n, i;
     887      363302 :         BAT *b = NULL, *bn = NULL;
     888      363302 :         BATiter bi;
     889      363302 :         bool allnil = false;
     890      363302 :         bool issorted = true;
     891      363302 :         bool nonil = true;
     892      363302 :         bool stringtrick = false;
     893      363302 :         const void *nil;
     894      363302 :         int tpe;
     895      363302 :         lng t0 = 0;
     896             : 
     897      363302 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
     898             : 
     899      363302 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
     900             : 
     901             :         /* count number of participating BATs and allocate some
     902             :          * temporary work space */
     903     3911225 :         for (n = 0; bats[n]; n++) {
     904     3184654 :                 b = bats[n];
     905     3184654 :                 ndelete += (b->ttype == TYPE_msk || mask_cand(b));
     906     3184654 :                 TRC_DEBUG(ALGO, "arg %d: " ALGOBATFMT "\n",
     907             :                           n + 1, ALGOBATPAR(b));
     908             :         }
     909      363269 :         if (n == 0) {
     910           0 :                 GDKerror("must have BAT arguments\n");
     911           0 :                 return NULL;
     912             :         }
     913      363269 :         if (n == 1) {
     914           0 :                 bn = COLcopy(b, b->ttype, true, TRANSIENT);
     915           0 :                 TRC_DEBUG(ALGO, "single bat: copy -> " ALGOOPTBATFMT
     916             :                           " " LLFMT " usec\n",
     917             :                           ALGOOPTBATPAR(bn), GDKusec() - t0);
     918           0 :                 return bn;
     919             :         }
     920             : 
     921      363269 :         if (ndelete > 0 &&
     922        5380 :             (tobedeleted = GDKmalloc(sizeof(BAT *) * ndelete)) == NULL)
     923             :                 return NULL;
     924      363269 :         ba = GDKmalloc(sizeof(*ba) * n);
     925      363234 :         if (ba == NULL) {
     926           0 :                 GDKfree(tobedeleted);
     927           0 :                 return NULL;
     928             :         }
     929             : 
     930             :         ndelete = 0;
     931     3543195 :         for (n = 0, i = 0; bats[n]; n++) {
     932     3179932 :                 b = bats[n];
     933     3179932 :                 if (b->ttype == TYPE_msk || mask_cand(b)) {
     934        5447 :                         if ((b = BATunmask(b)) == NULL) {
     935           0 :                                 goto bunins_failed;
     936             :                         }
     937        5380 :                         tobedeleted[ndelete++] = b;
     938             :                 }
     939     3179865 :                 if (bats[n+1] && BATtdense(b) && b->hseqbase == b->tseqbase && b->tseqbase == bats[n+1]->hseqbase && BATcount(b) == BATcount(bats[n+1]))
     940     2439967 :                         continue; /* skip dense bat */
     941      739898 :                 ba[i] = (struct ba) {
     942             :                         .b = b,
     943      739898 :                         .hlo = b->hseqbase,
     944      739898 :                         .hhi = b->hseqbase + b->batCount,
     945             :                         .cnt = b->batCount,
     946      739898 :                         .t = (oid *) Tloc(b, 0),
     947             :                 };
     948      739898 :                 allnil |= b->ttype == TYPE_void && is_oid_nil(b->tseqbase);
     949      739898 :                 issorted &= b->tsorted;
     950      739898 :                 if (bats[n + 1])
     951      376671 :                         nonil &= b->tnonil;
     952      739898 :                 if (b->tnonil && b->tkey && b->tsorted &&
     953      410848 :                     ATOMtype(b->ttype) == TYPE_oid) {
     954      339912 :                         canditer_init(&ba[i].ci, NULL, b);
     955             :                 }
     956      739994 :                 i++;
     957             :         }
     958      363263 :         n = i;
     959      363263 :         if (i<=2) {
     960      306950 :                 if (i == 1) {
     961       54756 :                         bn = ba[0].b;
     962       54756 :                         BBPfix(bn->batCacheid);
     963             :                 } else {
     964      252194 :                         bn = BATproject(ba[0].b, ba[1].b);
     965             :                 }
     966      307186 :                 while (ndelete-- > 0)
     967         284 :                         BBPunfix(tobedeleted[ndelete]->batCacheid);
     968      306902 :                 GDKfree(tobedeleted);
     969      306857 :                 GDKfree(ba);
     970      306857 :                 return bn;
     971             :         }
     972             :         /* b is last BAT in bats array */
     973       56313 :         tpe = ATOMtype(b->ttype);
     974       56313 :         nil = ATOMnilptr(tpe);
     975       56313 :         if (allnil || ba[0].cnt == 0) {
     976       11121 :                 bn = BATconstant(ba[0].hlo, tpe == TYPE_oid ? TYPE_void : tpe,
     977             :                                  nil, ba[0].cnt, TRANSIENT);
     978       11121 :                 while (ndelete-- > 0)
     979       15108 :                         BBPreclaim(tobedeleted[ndelete]);
     980       11123 :                 GDKfree(tobedeleted);
     981       11123 :                 GDKfree(ba);
     982       11123 :                 TRC_DEBUG(ALGO, "with %d bats: nil/empty -> " ALGOOPTBATFMT
     983             :                           " " LLFMT " usec\n",
     984             :                           n, ALGOOPTBATPAR(bn), GDKusec() - t0);
     985       11123 :                 return bn;
     986             :         }
     987             : 
     988       45192 :         bi = bat_iterator(b);
     989       45194 :         if (nonil && ATOMstorage(tpe) == TYPE_str && bi.restricted == BAT_READ) {
     990        5902 :                 stringtrick = true;
     991        5902 :                 bn = COLnew2(ba[0].hlo, tpe, ba[0].cnt, TRANSIENT, bi.width);
     992        5902 :                 if (bn && bn->tvheap) {
     993             :                         /* no need to remove any files since they were
     994             :                          * never created for this bat */
     995        5902 :                         HEAPdecref(bn->tvheap, false);
     996        5902 :                         bn->tvheap = NULL;
     997             :                 }
     998        5902 :                 tpe = bi.width == 1 ? TYPE_bte : (bi.width == 2 ? TYPE_sht : (bi.width == 4 ? TYPE_int : TYPE_lng));
     999             :         } else {
    1000       39292 :                 bn = COLnew(ba[0].hlo, tpe, ba[0].cnt, TRANSIENT);
    1001             :         }
    1002       45194 :         if (bn == NULL) {
    1003           0 :                 bat_iterator_end(&bi);
    1004           0 :                 goto bunins_failed;
    1005             :         }
    1006             : 
    1007       45194 :         assert(ba[n - 1].b == b);
    1008       45194 :         ba[n - 1].t = bi.base;
    1009       45194 :         if (ATOMtype(b->ttype) == TYPE_oid) {
    1010             :                 /* oid all the way */
    1011         377 :                 oid *d = (oid *) Tloc(bn, 0);
    1012         377 :                 assert(!stringtrick);
    1013    24972280 :                 TIMEOUT_LOOP_IDX_DECL(p, ba[0].cnt, qry_ctx) {
    1014    24969334 :                         oid o = ba[0].ci.s ? canditer_next(&ba[0].ci) : ba[0].t[p];
    1015   107014543 :                         for (int i = 1; i < n; i++) {
    1016    82045209 :                                 if (is_oid_nil(o)) {
    1017           0 :                                         bn->tnil = true;
    1018           0 :                                         break;
    1019             :                                 }
    1020    82045209 :                                 if (o < ba[i].hlo || o >= ba[i].hhi) {
    1021           0 :                                         GDKerror("does not match always\n");
    1022           0 :                                         bat_iterator_end(&bi);
    1023           0 :                                         goto bunins_failed;
    1024             :                                 }
    1025    82045209 :                                 o -= ba[i].hlo;
    1026    82045970 :                                 o = ba[i].ci.s ?
    1027    43904452 :                                     (ba[i].ci.tpe == cand_dense) ?
    1028    43904452 :                                         canditer_idx_dense(&ba[i].ci, o) :
    1029   124434231 :                                         canditer_idx(&ba[i].ci, o) : ba[i].t[o];
    1030             :                         }
    1031    24969334 :                         *d++ = o;
    1032             :                 }
    1033       44817 :         } else if (!ATOMvarsized(tpe)) {
    1034       44567 :                 const void *v;
    1035       44567 :                 char *d = Tloc(bn, 0);
    1036             : 
    1037       44567 :                 bn->tnil = false;
    1038       44567 :                 n--;    /* stop one before the end, also ba[n] is last */
    1039   172982016 :                 TIMEOUT_LOOP_IDX_DECL(p, ba[0].cnt, qry_ctx) {
    1040   172833862 :                         oid o = ba[0].ci.s ? canditer_next(&ba[0].ci) : ba[0].t[p];
    1041             : 
    1042   394488200 :                         for (int i = 1; i < n; i++) {
    1043   221710362 :                                 if (is_oid_nil(o)) {
    1044          15 :                                         bn->tnil = true;
    1045          15 :                                         break;
    1046             :                                 }
    1047   221710347 :                                 if (o < ba[i].hlo || o >= ba[i].hhi) {
    1048           0 :                                         GDKerror("does not match always\n");
    1049           0 :                                         bat_iterator_end(&bi);
    1050           0 :                                         goto bunins_failed;
    1051             :                                 }
    1052   221710347 :                                 o -= ba[i].hlo;
    1053   221755185 :                                 o = ba[i].ci.s ?
    1054    82857296 :                                     (ba[i].ci.tpe == cand_dense) ?
    1055    82857296 :                                         canditer_idx_dense(&ba[i].ci, o) :
    1056   288273653 :                                         canditer_idx(&ba[i].ci, o) : ba[i].t[o];
    1057             :                         }
    1058   172777853 :                         if (is_oid_nil(o)) {
    1059          15 :                                 assert(!stringtrick);
    1060          15 :                                 bn->tnil = true;
    1061          15 :                                 v = nil;
    1062   172777838 :                         } else if (o < ba[n].hlo || o >= ba[n].hhi) {
    1063           0 :                                 GDKerror("does not match always\n");
    1064           0 :                                 bat_iterator_end(&bi);
    1065           0 :                                 goto bunins_failed;
    1066             :                         } else {
    1067   172777838 :                                 o -= ba[n].hlo;
    1068   172777838 :                                 v = (const char *) bi.base + (o << bi.shift);
    1069             :                         }
    1070   172777853 :                         if (ATOMputFIX(tpe, d, v) != GDK_SUCCEED) {
    1071           0 :                                 bat_iterator_end(&bi);
    1072           0 :                                 goto bunins_failed;
    1073             :                         }
    1074   172833820 :                         d += bi.width;
    1075             :                 }
    1076       44531 :                 if (stringtrick) {
    1077        5900 :                         bn->tnil = false;
    1078        5900 :                         bn->tnonil = bi.nonil;
    1079        5900 :                         bn->tkey = false;
    1080        5900 :                         bn->tascii = bi.ascii;
    1081        5900 :                         assert(bn->tvheap == NULL);
    1082        5900 :                         bn->tvheap = bi.vh;
    1083        5900 :                         HEAPincref(bi.vh);
    1084        5902 :                         assert(bn->tvheap->parentid != bn->batCacheid);
    1085        5902 :                         BBPretain(bn->tvheap->parentid);
    1086        5902 :                         assert(bn->ttype == b->ttype);
    1087        5902 :                         assert(bn->twidth == bi.width);
    1088        5902 :                         assert(bn->tshift == bi.shift);
    1089             :                 }
    1090             :                 n++;            /* undo for debug print */
    1091             :         } else {
    1092         250 :                 const void *v;
    1093             : 
    1094         250 :                 assert(!stringtrick);
    1095         250 :                 bn->tnil = false;
    1096         250 :                 n--;    /* stop one before the end, also ba[n] is last */
    1097      685441 :                 TIMEOUT_LOOP_IDX_DECL(p, ba[0].cnt, qry_ctx) {
    1098      684901 :                         oid o = ba[0].ci.s ? canditer_next(&ba[0].ci) : ba[0].t[p];
    1099     1381444 :                         for (int i = 1; i < n; i++) {
    1100      696543 :                                 if (is_oid_nil(o)) {
    1101           0 :                                         bn->tnil = true;
    1102           0 :                                         break;
    1103             :                                 }
    1104      696543 :                                 if (o < ba[i].hlo || o >= ba[i].hhi) {
    1105           0 :                                         GDKerror("does not match always\n");
    1106           0 :                                         bat_iterator_end(&bi);
    1107           0 :                                         goto bunins_failed;
    1108             :                                 }
    1109      696543 :                                 o -= ba[i].hlo;
    1110      696543 :                                 o = ba[i].ci.s ?
    1111       27003 :                                     (ba[i].ci.tpe == cand_dense) ?
    1112       27003 :                                         canditer_idx_dense(&ba[i].ci, o) :
    1113      723297 :                                         canditer_idx(&ba[i].ci, o) : ba[i].t[o];
    1114             :                         }
    1115      684901 :                         if (is_oid_nil(o)) {
    1116           0 :                                 bn->tnil = true;
    1117           0 :                                 v = nil;
    1118      684901 :                         } else if (o < ba[n].hlo || o >= ba[n].hhi) {
    1119           0 :                                 GDKerror("does not match always\n");
    1120           0 :                                 bat_iterator_end(&bi);
    1121           0 :                                 goto bunins_failed;
    1122             :                         } else {
    1123      684901 :                                 o -= ba[n].hlo;
    1124      684901 :                                 v = BUNtail(bi, o);
    1125             :                         }
    1126      684901 :                         if (bunfastapp(bn, v) != GDK_SUCCEED) {
    1127           0 :                                 bat_iterator_end(&bi);
    1128           0 :                                 goto bunins_failed;
    1129             :                         }
    1130             :                 }
    1131             :                 n++;            /* undo for debug print */
    1132             :         }
    1133       45160 :         bat_iterator_end(&bi);
    1134       45195 :         TIMEOUT_CHECK(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(bunins_failed, qry_ctx));
    1135       45191 :         BATsetcount(bn, ba[0].cnt);
    1136       45188 :         bn->tsorted = (ba[0].cnt <= 1) | issorted;
    1137       45188 :         bn->trevsorted = ba[0].cnt <= 1;
    1138       45188 :         bn->tnonil = nonil & b->tnonil;
    1139       45188 :         bn->tseqbase = oid_nil;
    1140       45188 :         bn->tkey = (ba[0].cnt <= 1);
    1141             :         /* note, b may point to one of the bats in tobedeleted, so
    1142             :          * reclaim after the last use of b */
    1143       45188 :         while (ndelete-- > 0)
    1144       46295 :                 BBPreclaim(tobedeleted[ndelete]);
    1145       45186 :         GDKfree(tobedeleted);
    1146       45195 :         GDKfree(ba);
    1147       45195 :         TRC_DEBUG(ALGO, "with %d bats: " ALGOOPTBATFMT " " LLFMT " usec\n",
    1148             :                   n, ALGOOPTBATPAR(bn), GDKusec() - t0);
    1149             :         return bn;
    1150             : 
    1151           0 :   bunins_failed:
    1152           0 :         while (ndelete-- > 0)
    1153           0 :                 BBPreclaim(tobedeleted[ndelete]);
    1154           0 :         GDKfree(tobedeleted);
    1155           0 :         GDKfree(ba);
    1156           0 :         BBPreclaim(bn);
    1157           0 :         TRC_DEBUG(ALGO, "failed " LLFMT "usec\n", GDKusec() - t0);
    1158             :         return NULL;
    1159             : }

Generated by: LCOV version 1.14